<commit_before>/**
 Copyright 2016 <NAME>
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
#include "statistics.h"
#include "contrib/options.h"

#ifndef __linux__

/*
 * Timer subtraction, highly inspired on the code show at: 
 * http://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html
 */

static int timersub(struct timeval *start, struct timeval *end, struct timeval *result)
{
    if (start->tv_usec < end->tv_usec) {
        int nsec = (end->tv_usec - start->tv_usec) / 1000000 + 1;
        end->tv_usec -= 1000000 * nsec;
        end->tv_sec += nsec;
    }
    if (start->tv_usec - end->tv_usec > 1000000) {
        int nsec = (start->tv_usec - end->tv_usec) / 1000000;
        end->tv_usec += 1000000 * nsec;
        end->tv_sec -= nsec;
    }
    
    result->tv_sec = start->tv_sec - end->tv_sec;
    result->tv_usec = start->tv_usec - end->tv_usec;
    
    return start->tv_sec < end->tv_sec;
}

#endif // __linux__

static FILE *open_stats_file(const char *prefix, const char *suffix, 
                             gru_status_t *status) {
    const options_t *options = get_options_object();
    char name[64] = {0};

    snprintf(name, sizeof (name) - 1, "%s-%s-%d.csv", prefix, suffix, getpid());
    
    return gru_io_open_unique_file(options->logdir, name, status);
}

static FILE *open_receiver_latency_file(gru_status_t *status) {
    return open_stats_file("receiver", "latency", status);
}

static FILE *open_receiver_throughput_file(gru_status_t *status) {
    return open_stats_file("receiver", "throughput", status);
}
static FILE *open_sender_throughput_file(gru_status_t *status) {
    return open_stats_file("sender", "throughput", status);
}

stat_io_t *statistics_init(stat_direction_t direction, gru_status_t *status) {
    logger_t logger = gru_logger_get();
    stat_io_t *ret = malloc(sizeof(stat_io_t));
    
    if (direction == SENDER) {
        ret->latency = NULL;
        ret->throughput = open_sender_throughput_file(status);
    }
    else {
        ret->latency = open_receiver_latency_file(status);
        ret->throughput = open_receiver_throughput_file(status);
    }
    
    if (!ret->throughput) {
        logger(ERROR, "Unable to initialize the statistics IO engine: %s", 
               status->message);

        goto err_exit;
    }
    
    if (direction == RECEIVER) {
        if (!ret->latency) {
            logger(ERROR, "Unable to initialize the statistics IO engine: %s", 
                   status->message);

            goto err_exit;
        }
    }
    
    ret->direction = direction;
    return ret;
    
    err_exit:
    statistics_destroy(&ret);
    return NULL;
}


void statistics_destroy(stat_io_t **stat_io) {
    if ((*stat_io)->latency) {
        fclose((*stat_io)->latency);
    }
    
    if ((*stat_io)->throughput) {
        fclose((*stat_io)->throughput);
    }
    
    free(*stat_io);
    *stat_io = NULL;
}

/*
    logger(STAT, "ts;%s;count;%"PRIu64";duration;%"PRIu64";rate;%.2f", 
           last_buff, count, partial, rate);
 */

void statistics_latency_header(stat_io_t *stat_io) {
    fprintf(stat_io->latency, "creation;latency\n");
}

void statistics_throughput_header(stat_io_t *stat_io) {
    fprintf(stat_io->throughput, "timestamp;count;duration;rate\n");
}

void statistics_latency_data(stat_io_t *stat_io, uint64_t latency, 
                             const char *time, int32_t milli) 
{
    fprintf(stat_io->latency, "%s.%03"PRId32";%"PRIu64"\n", time, milli, latency);
}


void statistics_throughput_data(stat_io_t *stat_io, const char *last_buff, 
                                uint64_t count, uint64_t partial, double rate) {
    fprintf(stat_io->throughput, "%s;%"PRIu64";%"PRIu64";%.2f\n", last_buff, count, partial, rate);
}

static uint64_t statistics_convert_to_milli(mpt_timestamp_t ts) {
    return (((ts.tv_sec) * 1000) + (ts.tv_usec / 1000));<commit_msg>REF: removed unused comment<commit_after>/**
 Copyright 2016 <NAME>
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
#include "statistics.h"
#include "contrib/options.h"

#ifndef __linux__

/*
 * Timer subtraction, highly inspired on the code show at: 
 * http://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html
 */

static int timersub(struct timeval *start, struct timeval *end, struct timeval *result)
{
    if (start->tv_usec < end->tv_usec) {
        int nsec = (end->tv_usec - start->tv_usec) / 1000000 + 1;
        end->tv_usec -= 1000000 * nsec;
        end->tv_sec += nsec;
    }
    if (start->tv_usec - end->tv_usec > 1000000) {
        int nsec = (start->tv_usec - end->tv_usec) / 1000000;
        end->tv_usec += 1000000 * nsec;
        end->tv_sec -= nsec;
    }
    
    result->tv_sec = start->tv_sec - end->tv_sec;
    result->tv_usec = start->tv_usec - end->tv_usec;
    
    return start->tv_sec < end->tv_sec;
}

#endif // __linux__

static FILE *open_stats_file(const char *prefix, const char *suffix, 
                             gru_status_t *status) {
    const options_t *options = get_options_object();
    char name[64] = {0};

    snprintf(name, sizeof (name) - 1, "%s-%s-%d.csv", prefix, suffix, getpid());
    
    return gru_io_open_unique_file(options->logdir, name, status);
}

static FILE *open_receiver_latency_file(gru_status_t *status) {
    return open_stats_file("receiver", "latency", status);
}

static FILE *open_receiver_throughput_file(gru_status_t *status) {
    return open_stats_file("receiver", "throughput", status);
}
static FILE *open_sender_throughput_file(gru_status_t *status) {
    return open_stats_file("sender", "throughput", status);
}

stat_io_t *statistics_init(stat_direction_t direction, gru_status_t *status) {
    logger_t logger = gru_logger_get();
    stat_io_t *ret = malloc(sizeof(stat_io_t));
    
    if (direction == SENDER) {
        ret->latency = NULL;
        ret->throughput = open_sender_throughput_file(status);
    }
    else {
        ret->latency = open_receiver_latency_file(status);
        ret->throughput = open_receiver_throughput_file(status);
    }
    
    if (!ret->throughput) {
        logger(ERROR, "Unable to initialize the statistics IO engine: %s", 
               status->message);

        goto err_exit;
    }
    
    if (direction == RECEIVER) {
        if (!ret->latency) {
            logger(ERROR, "Unable to initialize the statistics IO engine: %s", 
                   status->message);

            goto err_exit;
        }
    }
    
    ret->direction = direction;
    return ret;
    
    err_exit:
    statistics_destroy(&ret);
    return NULL;
}


void statistics_destroy(stat_io_t **stat_io) {
    if ((*stat_io)->latency) {
        fclose((*stat_io)->latency);
    }
    
    if ((*stat_io)->throughput) {
        fclose((*stat_io)->throughput);
    }
    
    free(*stat_io);
    *stat_io = NULL;
}

void statistics_latency_header(stat_io_t *stat_io) {
    fprintf(stat_io->latency, "creation;latency\n");
}

void statistics_throughput_header(stat_io_t *stat_io) {
    fprintf(stat_io->throughput, "timestamp;count;duration;rate\n");
}

void statistics_latency_data(stat_io_t *stat_io, uint64_t latency, 
                             const char *time, int32_t milli) 
{
    fprintf(stat_io->latency, "%s.%03"PRId32";%"PRIu64"\n", time, milli, latency);
}


void statistics_throughput_data(stat_io_t *stat_io, const char *last_buff, 
                                uint64_t count, uint64_t partial, double rate) {
    fprintf(stat_io->throughput, "%s;%"PRIu64";%"PRIu64";%.2f\n", last_buff, count, partial, rate);
}

static uint64_t statistics_convert_to_milli(mpt_timestamp_t ts) {
    return (((ts.tv_sec) * 1000) + (ts.tv_usec / 1000));
{'fraction_non_alphanumeric': 0.1242027136727357, 'fraction_numerical': 0.018439058332366927, 'mean_word_length': 3.1047120418848166, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '4788342', 'n_tokens_mistral': 3027, 'n_tokens_neox': 2817, 'n_words': 662}