Skip to content

Instantly share code, notes, and snippets.

@jbd
Created September 15, 2023 09:45
Show Gist options
  • Save jbd/3a4b24fa19d04b64f50e4ad3543c9bad to your computer and use it in GitHub Desktop.
Save jbd/3a4b24fa19d04b64f50e4ad3543c9bad to your computer and use it in GitHub Desktop.
LD_PRELOAD override to add AT_STATX_DONT_SYNC to the statx syscall flags
## Create 10000 files in an nfs directory
$ dd if=/dev/zero bs=10000 count=1 | split -b1
## in another shell, write some data in the same directory
$ dd if=/dev/zero of=data bs=1M
## run stat with and without --cached option to spot the runtime difference
$ hyperfine --warmup 1 --runs 3 'find . -type f -print0 | xargs -0 stat --cached=always >/dev/null' 'find . -type f -print0 | xargs -0 stat >/dev/null'
Benchmark 1: find . -type f -print0 | xargs -0 stat --cached=always >/dev/null
Time (mean ± σ): 1.233 s ± 0.119 s [User: 0.064 s, System: 0.104 s]
Range (min … max): 1.119 s … 1.357 s 3 runs
Benchmark 2: find . -type f -print0 | xargs -0 stat >/dev/null
Time (mean ± σ): 11.099 s ± 4.458 s [User: 0.062 s, System: 5.520 s]
Range (min … max): 7.721 s … 16.152 s 3 runs
Summary
find . -type f -print0 | xargs -0 stat --cached=always >/dev/null ran
9.00 ± 3.72 times faster than find . -type f -print0 | xargs -0 stat >/dev/null
## without the dd write in the same directory
$ hyperfine --warmup 1 --runs 10 'ls -l > /dev/null' 'LD_PRELOAD=/home/jbdenis/statx_override/statx_dont_sync_override.so ls -l >/dev/null'
Benchmark 1: ls -l > /dev/null
Time (mean ± σ): 319.2 ms ± 365.9 ms [User: 17.2 ms, System: 29.8 ms]
Range (min … max): 35.4 ms … 853.4 ms 10 runs
Benchmark 2: LD_PRELOAD=/home/jbdenis/statx_override/statx_dont_sync_override.so ls -l >/dev/null
Time (mean ± σ): 34.4 ms ± 0.7 ms [User: 16.5 ms, System: 17.2 ms]
Range (min … max): 33.1 ms … 35.7 ms 10 runs
Summary
LD_PRELOAD=/home/jbdenis/statx_override/statx_dont_sync_override.so ls -l >/dev/null ran
9.27 ± 10.63 times faster than ls -l > /dev/null
## with the dd write in the same directory
$ hyperfine --warmup 1 --runs 10 'ls -l > /dev/null' 'LD_PRELOAD=/home/jbdenis/statx_override/statx_dont_sync_override.so ls -l >/dev/null'
Benchmark 1: ls -l > /dev/null
Time (mean ± σ): 3.278 s ± 1.579 s [User: 0.017 s, System: 1.987 s]
Range (min … max): 2.169 s … 7.638 s 10 runs
Benchmark 2: LD_PRELOAD=/home/jbdenis/statx_override/statx_dont_sync_override.so ls -l >/dev/null
Time (mean ± σ): 35.4 ms ± 1.5 ms [User: 16.6 ms, System: 18.2 ms]
Range (min … max): 33.9 ms … 38.4 ms 10 runs
Summary
LD_PRELOAD=/home/jbdenis/statx_override/statx_dont_sync_override.so ls -l >/dev/null ran
92.50 ± 44.70 times faster than ls -l > /dev/null
CC=gcc
CFLAGS=-shared -fPIC -Wall
LIBS=-ldl
TARGET=statx_dont_sync_override.so
SRC=statx_dont_sync_override.c
all: $(TARGET)
$(TARGET): $(SRC)
$(CC) $(CFLAGS) -o $(TARGET) $(SRC) $(LIBS)
test: $(TARGET)
@LD_PRELOAD=./$(TARGET) ls -l
clean:
rm -f $(TARGET)
#define _GNU_SOURCE
#include <dlfcn.h>
#include <fcntl.h>
#include <sys/stat.h>
// Define the type of the original function
typedef int (*orig_statx_type)(int dirfd, const char *pathname, int flags,
unsigned int mask, struct statx *statxbuf);
int statx(int dirfd, const char *pathname, int flags, unsigned int mask,
struct statx *statxbuf) {
// Get a reference to the original function
orig_statx_type orig_statx;
orig_statx = dlsym(RTLD_NEXT, "statx");
// Set STATX_DONT_SYNC flag and unset STATX_FORCE_SYNC
// flags &= ~AT_STATX_FORCE_SYNC;
flags |= AT_STATX_DONT_SYNC;
// Call the original function with the modified flags
return orig_statx(dirfd, pathname, flags, mask, statxbuf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment