Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139/* 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/<pid>/io is the * total, this table names the files. */#define _GNU_SOURCE#include <dlfcn.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <sys/mman.h>#include <sys/stat.h>#include <sys/uio.h>#include <unistd.h>
#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;}