Data-Histogram-Shared

 view release on metacpan or  search on metacpan

hist.h  view on Meta::CPAN

    hdr->counts_off       = L.counts;
    __atomic_thread_fence(__ATOMIC_SEQ_CST);
}

static inline HistHandle *hist_setup(void *base, size_t map_size,
                                     const char *path, int backing_fd) {
    HistHeader *hdr = (HistHeader *)base;
    HistHandle *h = (HistHandle *)calloc(1, sizeof(HistHandle));
    if (!h) {
        munmap(base, map_size);
        if (backing_fd >= 0) close(backing_fd);
        return NULL;
    }
    h->hdr          = hdr;
    h->base         = base;
    h->reader_slots = (HistReaderSlot *)((uint8_t *)base + hdr->reader_slots_off);
    h->mmap_size    = map_size;
    h->counts_off   = hdr->counts_off;   /* validated at open (== L.counts); cache so the bound and the pointer use one value */
    h->path         = path ? strdup(path) : NULL;
    h->backing_fd   = backing_fd;
    h->my_slot_idx  = UINT32_MAX;
    return h;
}

/* Validate a mapped header (shared by hist_create reopen and hist_open_fd).
 * Stored geometry wins on reopen; we re-derive it from lowest/highest/sig_figs
 * and require every cached field to match, then require total_size == the size
 * the geometry implies and == the actual file size. */
static inline int hist_validate_header(const HistHeader *hdr, uint64_t file_size) {
    if (hdr->magic != HIST_MAGIC) return 0;
    if (hdr->version != HIST_VERSION) return 0;
    if (hdr->sig_figs < HIST_MIN_SIG || hdr->sig_figs > HIST_MAX_SIG) return 0;
    if (hdr->lowest < 1) return 0;
    if (hdr->highest < 2 * hdr->lowest) return 0;

    HistGeometry g;
    if (!hist_validate_create_args(hdr->lowest, hdr->highest, hdr->sig_figs, &g, NULL))
        return 0;
    if (hdr->unit_magnitude                  != g.unit_magnitude) return 0;
    if (hdr->sub_bucket_count_magnitude      != g.sub_bucket_count_magnitude) return 0;
    if (hdr->sub_bucket_half_count_magnitude != g.sub_bucket_half_count_magnitude) return 0;
    if (hdr->sub_bucket_count                != g.sub_bucket_count) return 0;
    if (hdr->sub_bucket_half_count           != g.sub_bucket_half_count) return 0;
    if (hdr->sub_bucket_mask                 != g.sub_bucket_mask) return 0;
    if (hdr->bucket_count                    != g.bucket_count) return 0;
    if (hdr->counts_len                      != g.counts_len) return 0;

    if (hdr->total_size != file_size) return 0;
    if (hdr->total_size != hist_total_size(hdr->counts_len)) return 0;
    HistLayout L = hist_layout();
    if (hdr->reader_slots_off != L.reader_slots) return 0;
    if (hdr->counts_off != L.counts) return 0;
    return 1;
}

/* Securely obtain a fd: create exclusively (O_CREAT|O_EXCL|O_NOFOLLOW at mode,
 * default 0600), or attach an existing file (O_RDWR|O_NOFOLLOW, no O_CREAT). */
static int hist_secure_open(const char *path, mode_t mode, char *errbuf) {
    for (int attempt = 0; attempt < 100; attempt++) {
        int fd = open(path, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, mode);
        if (fd >= 0) { (void)fchmod(fd, mode); return fd; }   /* exact mode: umask narrowed the O_EXCL create */
        if (errno != EEXIST) { HIST_ERR("create %s: %s", path, strerror(errno)); return -1; }
        fd = open(path, O_RDWR|O_NOFOLLOW|O_CLOEXEC);
        if (fd >= 0) return fd;
        if (errno == ENOENT) continue;   /* creator unlinked between our two opens; retry */
        HIST_ERR("open %s: %s", path, strerror(errno));  /* ELOOP => symlink rejected */
        return -1;
    }
    HIST_ERR("open %s: create/attach kept racing", path);
    return -1;
}

static HistHandle *hist_create(const char *path, int64_t lowest, int64_t highest,
                               int32_t sig_figs, mode_t mode, char *errbuf) {
    HistGeometry g;
    if (!hist_validate_create_args(lowest, highest, sig_figs, &g, errbuf)) return NULL;

    uint64_t total = hist_total_size(g.counts_len);
    int anonymous = (path == NULL);
    int fd = -1;
    size_t map_size;
    void *base;

    if (anonymous) {
        map_size = (size_t)total;
        base = mmap(NULL, map_size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
        if (base == MAP_FAILED) { HIST_ERR("mmap: %s", strerror(errno)); return NULL; }
    } else {
        fd = hist_secure_open(path, mode, errbuf);
        if (fd < 0) return NULL;
        if (flock(fd, LOCK_EX) < 0) { HIST_ERR("flock: %s", strerror(errno)); close(fd); return NULL; }
        struct stat st;
        if (fstat(fd, &st) < 0) { HIST_ERR("fstat: %s", strerror(errno)); flock(fd, LOCK_UN); close(fd); return NULL; }
        int is_new = (st.st_size == 0);
        if (!is_new && (uint64_t)st.st_size < sizeof(HistHeader)) {
            HIST_ERR("%s: file too small (%lld)", path, (long long)st.st_size);
            flock(fd, LOCK_UN); close(fd); return NULL;
        }
        if (is_new && ftruncate(fd, (off_t)total) < 0) {
            HIST_ERR("ftruncate: %s", strerror(errno)); flock(fd, LOCK_UN); close(fd); return NULL;
        }
        map_size = is_new ? (size_t)total : (size_t)st.st_size;
        base = mmap(NULL, map_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
        if (base == MAP_FAILED) { HIST_ERR("mmap: %s", strerror(errno)); flock(fd, LOCK_UN); close(fd); return NULL; }
        if (!is_new) {
            if (!hist_validate_header((HistHeader *)base, (uint64_t)st.st_size)) {
                HIST_ERR("invalid histogram file"); munmap(base, map_size); flock(fd, LOCK_UN); close(fd); return NULL;
            }
            flock(fd, LOCK_UN); close(fd);
            return hist_setup(base, map_size, path, -1);
        }
    }
    hist_init_header(base, &g, total);
    if (fd >= 0) { flock(fd, LOCK_UN); close(fd); }
    return hist_setup(base, map_size, path, -1);
}

static HistHandle *hist_create_memfd(const char *name, int64_t lowest, int64_t highest,
                                     int32_t sig_figs, char *errbuf) {
    HistGeometry g;
    if (!hist_validate_create_args(lowest, highest, sig_figs, &g, errbuf)) return NULL;



( run in 1.815 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )