<commit_before>/* time.c
   <NAME> (<EMAIL>), 22 Sep 2015
   FreeBSD-style copyright and disclaimer apply
*/

// -----------------------------------------------------------------------------
// timer
// -----------------------------------------------------------------------------

struct timespec ilka_now()
{
    struct timespec ts;
    if (!clock_gettime(CLOCK_MONOTONIC_RAW, &ts)) return ts;

    ilka_fail_errno("unable to read monotonic clock");
    ilka_abort();
}

double ilka_elapsed(struct timespec *start)
{
    struct timespec end = ilka_now();

    double secs = end.tv_sec - start->tv_sec;

    int64_t nsecs = end.tv_nsec - start->tv_nsec;
    if (nsecs < 0) nsecs *= -1;

    return secs + nsecs * 0.000000001;
}

double ilka_scale_elapsed(double t, char *s)
{
    static const char scale[] = "smunp";

    size_t i = 0;
    for (i = 0; i < sizeof(scale); ++i) {
        if (t >= 1.0) break;
        t *= 1000.0;
    }

    *s = scale[i];
    return t;
}


// -----------------------------------------------------------------------------
// sleep
// -----------------------------------------------------------------------------

bool ilka_nsleep(uint64_t nanos)
{
    struct timespec t = ilka_now();

    t.tv_nsec += nanos;
    if (t.tv_nsec >= 1000000000) {
        t.tv_sec += t.tv_nsec / 1000000000;
        t.tv_nsec = t.tv_nsec % 1000000000;
    }

    while (true) {
        int ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
        if (!ret) return true;
        if (errno == EINTR) continue;<commit_msg>utils: improved resolution of ilka_nsleep<commit_after>/* time.c
   <NAME> (<EMAIL>), 22 Sep 2015
   FreeBSD-style copyright and disclaimer apply
*/

// -----------------------------------------------------------------------------
// timer
// -----------------------------------------------------------------------------

struct timespec ilka_now()
{
    struct timespec ts;
    if (!clock_gettime(CLOCK_REALTIME, &ts)) return ts;

    ilka_fail_errno("unable to read monotonic clock");
    ilka_abort();
}

double ilka_elapsed(struct timespec *start)
{
    struct timespec end = ilka_now();

    double secs = end.tv_sec - start->tv_sec;

    int64_t nsecs = end.tv_nsec - start->tv_nsec;
    if (nsecs < 0) nsecs *= -1;

    return secs + nsecs * 0.000000001;
}

double ilka_scale_elapsed(double t, char *s)
{
    static const char scale[] = "smunp";

    size_t i = 0;
    for (i = 0; i < sizeof(scale); ++i) {
        if (t >= 1.0) break;
        t *= 1000.0;
    }

    *s = scale[i];
    return t;
}


// -----------------------------------------------------------------------------
// sleep
// -----------------------------------------------------------------------------

bool ilka_nsleep(uint64_t nanos)
{
    struct timespec t = ilka_now();

    t.tv_nsec += nanos;
    if (t.tv_nsec >= 1000000000) {
        t.tv_sec += t.tv_nsec / 1000000000;
        t.tv_nsec = t.tv_nsec % 1000000000;
    }

    while (true) {
        int ret = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &t, NULL);
        if (!ret) return true;
        if (errno == EINTR) continue;
{'fraction_non_alphanumeric': 0.308408215661104, 'fraction_numerical': 0.03915275994865212, 'mean_word_length': 1.3383345836459115, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, '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': '1498809', 'n_tokens_mistral': 1109, 'n_tokens_neox': 972, 'n_words': 241}