/* Counts bytes written to regular files, per file, for * test/perf/measurement.py. * * Preloaded into a measured daemon (LD_PRELOAD), it adds the bytes each write * call returns to a slot in a table shared by every process of the tree: a * tmpfs file named by ALBEDO_PERF_WRITES, mapped by the process that loads it * and inherited across fork. Writes inside libc (stdio flushes) and through * shared mappings (SQLite's -shm index) are not seen; /proc//io is the * total, this table names the files. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #define SLOTS 2048 #define PATH_BYTES 216 struct slot { unsigned long long key; /* 0: free; set once with a compare-and-swap */ unsigned long long bytes; unsigned long long calls; unsigned long long device; unsigned long long inode; char path[PATH_BYTES]; }; static struct slot *table; __attribute__((constructor)) static void attach(void) { const char *name = getenv("ALBEDO_PERF_WRITES"); if (!name || table) return; int fd = open(name, O_RDWR | O_CLOEXEC); if (fd < 0) return; void *map = mmap(NULL, SLOTS * sizeof(struct slot), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close(fd); if (map != MAP_FAILED) table = map; } static void count(int fd, ssize_t written) { struct stat st; if (!table || written <= 0 || fstat(fd, &st) || !S_ISREG(st.st_mode)) return; unsigned long long key = (unsigned long long)st.st_ino ^ ((unsigned long long)st.st_dev * 0x9E3779B97F4A7C15ULL); if (!key) key = 1; /* 0 marks a free slot */ for (unsigned i = 0; i < SLOTS; i++) { struct slot *slot = &table[(key + i) % SLOTS]; unsigned long long seen = __atomic_load_n(&slot->key, __ATOMIC_ACQUIRE); if (seen == 0) { unsigned long long empty = 0; if (__atomic_compare_exchange_n(&slot->key, &empty, key, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) { slot->device = st.st_dev; slot->inode = st.st_ino; char link[32]; snprintf(link, sizeof link, "/proc/self/fd/%d", fd); ssize_t n = readlink(link, slot->path, PATH_BYTES - 1); slot->path[n > 0 ? n : 0] = 0; seen = key; } else { seen = empty; } } if (seen == key) { __atomic_add_fetch(&slot->bytes, (unsigned long long)written, __ATOMIC_RELAXED); __atomic_add_fetch(&slot->calls, 1, __ATOMIC_RELAXED); return; } } } #define REAL(name) \ static __typeof__(name) *real; \ if (!real) \ real = (__typeof__(name) *)dlsym(RTLD_NEXT, #name) ssize_t write(int fd, const void *buffer, size_t size) { REAL(write); ssize_t n = real(fd, buffer, size); count(fd, n); return n; } ssize_t pwrite(int fd, const void *buffer, size_t size, off_t offset) { REAL(pwrite); ssize_t n = real(fd, buffer, size, offset); count(fd, n); return n; } ssize_t pwrite64(int fd, const void *buffer, size_t size, off64_t offset) { REAL(pwrite64); ssize_t n = real(fd, buffer, size, offset); count(fd, n); return n; } ssize_t writev(int fd, const struct iovec *vector, int count_) { REAL(writev); ssize_t n = real(fd, vector, count_); count(fd, n); return n; } ssize_t pwritev(int fd, const struct iovec *vector, int count_, off_t offset) { REAL(pwritev); ssize_t n = real(fd, vector, count_, offset); count(fd, n); return n; } ssize_t pwritev64(int fd, const struct iovec *vector, int count_, off64_t offset) { REAL(pwritev64); ssize_t n = real(fd, vector, count_, offset); count(fd, n); return n; } ssize_t pwritev2(int fd, const struct iovec *vector, int count_, off_t offset, int flags) { REAL(pwritev2); ssize_t n = real(fd, vector, count_, offset, flags); count(fd, n); return n; }