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.
This commit is contained in:
Amaury Denoyelle 2024-08-07 14:50:26 +02:00
parent 663416b4ef
commit a6e2523ca1
2 changed files with 47 additions and 0 deletions

10
include/haproxy/time-t.h Normal file
View File

@ -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 */

View File

@ -22,8 +22,11 @@
#ifndef _HAPROXY_TIME_H
#define _HAPROXY_TIME_H
#include <haproxy/time-t.h>
#include <sys/time.h>
#include <haproxy/api.h>
#include <haproxy/ticks.h>
#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 <timer>. */
static inline void tot_time_reset(struct tot_time *timer)
{
timer->curr = 0;
timer->tot = 0;
}
/* Start to account with <timer>. No-op if already started. */
static inline void tot_time_start(struct tot_time *timer)
{
if (!timer->curr)
timer->curr = now_ms;
}
/* Stop <timer> 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 <timer>, 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 */
/*