From: Amaury Denoyelle Date: Wed, 7 Aug 2024 12:50:26 +0000 (+0200) Subject: MINOR: time: define tot_time structure X-Git-Tag: v3.1-dev5~31 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a6e2523ca1f3dcc90b050d75af62bb867a2acc07;p=thirdparty%2Fhaproxy.git MINOR: time: define tot_time structure Define a new utility type tot_time. Its purpose is to be able to account elapsed time accross multiple periods. Functions are defined to easily start and stop measures, and return the current value. --- diff --git a/include/haproxy/time-t.h b/include/haproxy/time-t.h new file mode 100644 index 0000000000..5ca617773c --- /dev/null +++ b/include/haproxy/time-t.h @@ -0,0 +1,10 @@ +#ifndef _HAPROXY_TIME_T_H +#define _HAPROXY_TIME_T_H + +/* Type used to account a total time over distinct periods. */ +struct tot_time { + uint32_t curr; /* timestamp of start date or 0 if timer stopped */ + uint32_t tot; /* total already accounted since last stop */ +}; + +#endif /* _HAPROXY_TIME_T_H */ diff --git a/include/haproxy/time.h b/include/haproxy/time.h index 3ebc683e1b..b8240b26e4 100644 --- a/include/haproxy/time.h +++ b/include/haproxy/time.h @@ -22,8 +22,11 @@ #ifndef _HAPROXY_TIME_H #define _HAPROXY_TIME_H +#include + #include #include +#include #define TIME_ETERNITY (TV_ETERNITY_MS) @@ -510,6 +513,40 @@ static inline struct timeval *__tv_ms_add(struct timeval *tv, const struct timev tv1; \ }) +/* Initialize . */ +static inline void tot_time_reset(struct tot_time *timer) +{ + timer->curr = 0; + timer->tot = 0; +} + +/* Start to account with . No-op if already started. */ +static inline void tot_time_start(struct tot_time *timer) +{ + if (!timer->curr) + timer->curr = now_ms; +} + +/* Stop accounting and update its total. No-op if already stopped. */ +static inline void tot_time_stop(struct tot_time *timer) +{ + if (timer->curr) { + timer->tot += now_ms - timer->curr; + timer->curr = 0; + } +} + +/* Retrieve the total value accounted by , including the current period + * if currently started. + */ +static inline uint32_t tot_time_read(const struct tot_time *timer) +{ + uint32_t value = timer->tot; + if (timer->curr) + value += now_ms - timer->curr; + return value; +} + #endif /* _HAPROXY_TIME_H */ /*