From 62af0344b10b9b54dc3edbbf70b720a86e9544c7 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 25 Feb 2020 17:35:52 +0100 Subject: [PATCH] Add stopwatch --- src/algo_lightning.c | 4 ---- src/donar.c | 2 ++ src/stopwatch.c | 48 ++++++++++++++++++++++++++++++++++++++++++++ src/stopwatch.h | 29 ++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 src/stopwatch.c create mode 100644 src/stopwatch.h diff --git a/src/algo_lightning.c b/src/algo_lightning.c index 8f4f0be..dd73237 100644 --- a/src/algo_lightning.c +++ b/src/algo_lightning.c @@ -94,7 +94,6 @@ struct light_ctx { int disable_scheduler; struct stat_entry stats[MAX_LINKS]; enum schedule_group_target sched_strat; - struct timing_fx tfx; }; void algo_lightning_free(void* v) { @@ -122,7 +121,6 @@ void algo_lightning_init(struct evt_core_ctx* ctx, struct algo_ctx* app_ctx, str lightc->disable_scheduler = 0; lightc->active = 0; lightc->sched_strat = SCHEDULE_BOTH; - timing_fx_init (&lightc->tfx, TIMING_ACTIVATED | TIMING_DISPLAY_END, "", "[udp-read] fn=%s"); uint64_t window = 2000; if (ap->algo_specific_params != NULL) { @@ -440,9 +438,7 @@ int send_message(struct evt_core_ctx* ctx, struct buffer_packet* bp) { dump_buffer_packet(bp_dup); fprintf(stderr, " [algo_lightning] Will send this info\n"); } - timing_fx_start (&lightc->tfx); main_on_tcp_write(ctx, to_fdinfo); - timing_fx_stop (&lightc->tfx, "write packet to tcp"); return 1; } diff --git a/src/donar.c b/src/donar.c index fa283b5..861f599 100644 --- a/src/donar.c +++ b/src/donar.c @@ -11,6 +11,8 @@ int main(int argc, char** argv) { setvbuf(stdout, NULL, _IONBF, 0); printf("~ Donar ~\n"); + timing_fx_init (&static_tfx, TIMING_ACTIVATED | TIMING_DISPLAY_END, "", "fn=%s"); + struct donar_params dp = {0}; donar_init_params (&dp); diff --git a/src/stopwatch.c b/src/stopwatch.c new file mode 100644 index 0000000..aecd698 --- /dev/null +++ b/src/stopwatch.c @@ -0,0 +1,48 @@ +#include "stopwatch.h" + +void timing_fx_init(struct timing_fx* tfx, enum timing_config conf, char* startt, char* endt) { + tfx->config = conf; + strncpy (tfx->start_template, startt, sizeof(tfx->start_template) - 1); + strncpy (tfx->end_template, endt, sizeof(tfx->end_template) - 1); + tfx->start_template[sizeof(tfx->start_template) - 1] = 0; // Enforce null terminated string + tfx->end_template[sizeof(tfx->end_template) - 1] = 0; +} + +void timing_fx_start(struct timing_fx* tfx, ...) { + va_list args; + if (!(tfx->config & TIMING_ACTIVATED)) return; + if (tfx->config & (TIMING_DISPLAY_START | TIMING_DISPLAY_BOTH)) { + va_start(args, tfx); + vfprintf(stderr, tfx->start_template, args); + va_end(args); + } + + if (clock_gettime(CLOCK_MONOTONIC, &tfx->start) == -1) { + perror("clock_gettime"); + exit(EXIT_FAILURE); + } +} + +double timing_fx_stop(struct timing_fx* tfx, ...) { + va_list args; + struct timespec stop; + double elapsed_in_cb; + + if (!(tfx->config & TIMING_ACTIVATED)) return 0.; + + if (clock_gettime(CLOCK_MONOTONIC, &stop) == -1) { + perror("clock_gettime"); + exit(EXIT_FAILURE); + } + + elapsed_in_cb = (double)elapsed_micros (&tfx->start, &stop) / 1000000.; + + if (tfx->config & (TIMING_DISPLAY_END | TIMING_DISPLAY_BOTH)) { + va_start(args, tfx); + vfprintf(stderr, tfx->end_template, args); + fprintf(stderr, ": done in %f sec\n", elapsed_in_cb); + va_end(args); + } + return elapsed_in_cb; +} + diff --git a/src/stopwatch.h b/src/stopwatch.h new file mode 100644 index 0000000..9efaa4a --- /dev/null +++ b/src/stopwatch.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include "utils.h" + +enum timing_config { + TIMING_ACTIVATED = 1 << 0, + TIMING_DISPLAY_START = 1 << 1, + TIMING_DISPLAY_END = 1 << 2, + TIMING_DISPLAY_BOTH = 1 << 3 +}; + +struct timing_fx { + struct timespec start; + enum timing_config config; + uint8_t activated_start; + char start_template[255], end_template[255]; +}; + +struct timing_fx static_tfx; + +void timing_fx_init(struct timing_fx* tfx, enum timing_config conf, char* startt, char* endt); +void timing_fx_start(struct timing_fx* tfx, ...); +double timing_fx_stop(struct timing_fx* tfx, ...);