Add stopwatch

This commit is contained in:
Quentin 2020-02-25 17:35:52 +01:00
parent 7b7a5a079b
commit 62af0344b1
4 changed files with 79 additions and 4 deletions

View file

@ -94,7 +94,6 @@ struct light_ctx {
int disable_scheduler; int disable_scheduler;
struct stat_entry stats[MAX_LINKS]; struct stat_entry stats[MAX_LINKS];
enum schedule_group_target sched_strat; enum schedule_group_target sched_strat;
struct timing_fx tfx;
}; };
void algo_lightning_free(void* v) { 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->disable_scheduler = 0;
lightc->active = 0; lightc->active = 0;
lightc->sched_strat = SCHEDULE_BOTH; lightc->sched_strat = SCHEDULE_BOTH;
timing_fx_init (&lightc->tfx, TIMING_ACTIVATED | TIMING_DISPLAY_END, "", "[udp-read] fn=%s");
uint64_t window = 2000; uint64_t window = 2000;
if (ap->algo_specific_params != NULL) { 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); dump_buffer_packet(bp_dup);
fprintf(stderr, " [algo_lightning] Will send this info\n"); fprintf(stderr, " [algo_lightning] Will send this info\n");
} }
timing_fx_start (&lightc->tfx);
main_on_tcp_write(ctx, to_fdinfo); main_on_tcp_write(ctx, to_fdinfo);
timing_fx_stop (&lightc->tfx, "write packet to tcp");
return 1; return 1;
} }

View file

@ -11,6 +11,8 @@ int main(int argc, char** argv) {
setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0);
printf("~ Donar ~\n"); printf("~ Donar ~\n");
timing_fx_init (&static_tfx, TIMING_ACTIVATED | TIMING_DISPLAY_END, "", "fn=%s");
struct donar_params dp = {0}; struct donar_params dp = {0};
donar_init_params (&dp); donar_init_params (&dp);

48
src/stopwatch.c Normal file
View file

@ -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;
}

29
src/stopwatch.h Normal file
View file

@ -0,0 +1,29 @@
#pragma once
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#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, ...);