<commit_before>#ifndef __DMCE_PROBE_FUNCTION_BODY__
#define __DMCE_PROBE_FUNCTION_BODY__
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>

#ifndef DMCE_PROBE_NBR_TRACE_ENTRIES
#define DMCE_MAX_HITS 10000
#else
#define DMCE_MAX_HITS DMCE_PROBE_NBR_TRACE_ENTRIES
#endif

#define DMCE_TRACE_RINGBUFFER
#define NBR_STATUS_BITS 1

#ifndef DMCE_PROBE_LOCK_DIR_ENTRY
#define DMCE_PROBE_LOCK_DIR_ENTRY "/tmp/dmce-trace-buffer-lock-entry"
#endif

#ifndef DMCE_PROBE_OUTPUT_FILE_BIN
#define DMCE_PROBE_OUTPUT_FILE_BIN "/tmp/dmcebuffer.bin"
#endif

typedef struct {

    uint64_t timestamp;
    uint64_t probenbr;
    uint64_t vars[DMCE_PROBE_NBR_OPTIONAL_ELEMENTS];
    uint64_t cpu;
} dmce_probe_entry_t;

#ifdef __cplusplus
static dmce_probe_entry_t* dmce_buf_p = nullptr;
static unsigned int* dmce_probe_hitcount_p = nullptr;
#else
static dmce_probe_entry_t* dmce_buf_p = 0;
static unsigned int* dmce_probe_hitcount_p = 0;
#endif
static int dmce_buffer_setup_done = 0;
static __inline__ uint64_t dmce_tsc(void) {

#if defined(__x86_64__)
    unsigned msw, lsw;
    __asm__ __volatile__ ("rdtsc": "=a"(lsw), "=d"(msw): : "memory");
    return ( (((uint64_t) msw) << 32) | ((uint64_t) lsw));
#else
#error Time stamp for this dmce probe only available on __x86_64__ arch!
#endif
}

static void dmce_atexit(void) {

    int fp;

#ifdef DMCE_TRACE_RINGBUFFER
    unsigned int buf_pos;
#endif

    /* Only do this once (exit dir needs to be removed at startup) */

    if (! (mkdir(DMCE_PROBE_LOCK_DIR_EXIT, 0))) {

        fp = open(DMCE_PROBE_OUTPUT_FILE_BIN, O_CREAT, O_WRONLY);

#ifdef DMCE_TRACE_RINGBUFFER

        buf_pos = (*dmce_probe_hitcount_p >> NBR_STATUS_BITS) % DMCE_MAX_HITS;
        int i;

        for (i = 0; i < DMCE_MAX_HITS; i++) {

            unsigned int index = (buf_pos + i) % DMCE_MAX_HITS;
            if ( -1 == write(fp, &dmce_buf_p[index], sizeof(dmce_probe_entry_t) * 1))
                return;
        }
#else
        fwrite(dmce_buf_p, sizeof(dmce_probe_entry_t), *dmce_probe_hitcount_p, fp);
#endif
        close(fp);
        remove(DMCE_PROBE_LOCK_DIR_ENTRY);
    }
}

static void dmce_signal_handler(int sig) {

    /* Make other threads stop */
    __atomic_fetch_add (dmce_probe_hitcount_p, 1, __ATOMIC_RELAXED);

    /* Just call atexit and invoke the standard sig handler */
    dmce_atexit();
    signal(sig, SIG_DFL);
    kill(getpid(), sig);
}

#ifdef _GNU_SOURCE
#include <sched.h>
#else
int sched_getcpu(void);
#endif
<commit_msg>trace: error checks<commit_after>#ifndef __DMCE_PROBE_FUNCTION_BODY__
#define __DMCE_PROBE_FUNCTION_BODY__
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>

#ifndef DMCE_PROBE_NBR_TRACE_ENTRIES
#define DMCE_MAX_HITS 10000
#else
#define DMCE_MAX_HITS DMCE_PROBE_NBR_TRACE_ENTRIES
#endif

#define DMCE_TRACE_RINGBUFFER
#define NBR_STATUS_BITS 1

#ifndef DMCE_PROBE_LOCK_DIR_ENTRY
#define DMCE_PROBE_LOCK_DIR_ENTRY "/tmp/dmce-trace-buffer-lock-entry"
#endif

#ifndef DMCE_PROBE_OUTPUT_FILE_BIN
#define DMCE_PROBE_OUTPUT_FILE_BIN "/tmp/dmcebuffer.bin"
#endif

typedef struct {

    uint64_t timestamp;
    uint64_t probenbr;
    uint64_t vars[DMCE_PROBE_NBR_OPTIONAL_ELEMENTS];
    uint64_t cpu;
} dmce_probe_entry_t;

#ifdef __cplusplus
static dmce_probe_entry_t* dmce_buf_p = nullptr;
static unsigned int* dmce_probe_hitcount_p = nullptr;
#else
static dmce_probe_entry_t* dmce_buf_p = 0;
static unsigned int* dmce_probe_hitcount_p = 0;
#endif
static int dmce_buffer_setup_done = 0;
static __inline__ uint64_t dmce_tsc(void) {

#if defined(__x86_64__)
    unsigned msw, lsw;
    __asm__ __volatile__ ("rdtsc": "=a"(lsw), "=d"(msw): : "memory");
    return ( (((uint64_t) msw) << 32) | ((uint64_t) lsw));
#else
#error Time stamp for this dmce probe only available on __x86_64__ arch!
#endif
}

static void dmce_atexit(void) {

    int fp;

#ifdef DMCE_TRACE_RINGBUFFER
    unsigned int buf_pos;
#endif

    /* Only do this once (exit dir needs to be removed at startup) */

    if (! (mkdir(DMCE_PROBE_LOCK_DIR_EXIT, 0))) {

        if ( -1 == (fp = open(DMCE_PROBE_OUTPUT_FILE_BIN, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH))) {

            printf("DMCE trace: Error when opening trace file: %s\n", strerror(errno));
            return;
        }

#ifdef DMCE_TRACE_RINGBUFFER

        buf_pos = (*dmce_probe_hitcount_p >> NBR_STATUS_BITS) % DMCE_MAX_HITS;
        int i;

        for (i = 0; i < DMCE_MAX_HITS; i++) {

            unsigned int index = (buf_pos + i) % DMCE_MAX_HITS;
            if ( -1 == write(fp, &dmce_buf_p[index], sizeof(dmce_probe_entry_t) * 1)) {

                printf("DMCE trace: Error when writing trace buffer to disk: %s\n", strerror(errno));
                return;
            }
        }
#else
        fwrite(dmce_buf_p, sizeof(dmce_probe_entry_t), *dmce_probe_hitcount_p, fp);
#endif
        close(fp);
        remove(DMCE_PROBE_LOCK_DIR_ENTRY);
    }
}

#ifdef _GNU_SOURCE
#include <sched.h>
#else
int sched_getcpu(void);
#endif

static void dmce_signal_handler(int sig) {

    /* Make other threads stop */
    __atomic_fetch_add (dmce_probe_hitcount_p, 1, __ATOMIC_RELAXED);

    /* Just call atexit and invoke the standard sig handler */
    dmce_atexit();
    signal(sig, SIG_DFL);
    kill(getpid(), sig);
}

{'fraction_non_alphanumeric': 0.10638686131386861, 'fraction_numerical': 0.014051094890510948, 'mean_word_length': 4.103351955307263, 'pattern_counts': {'":': 2, '<': 28, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3623054', 'n_tokens_mistral': 2261, 'n_tokens_neox': 2132, 'n_words': 468}