MINOR: time: define tot_time structure
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Wed, 7 Aug 2024 12:50:26 +0000 (14:50 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Thu, 9 Jan 2025 09:28:04 +0000 (10:28 +0100)
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.

(cherry picked from commit a6e2523ca1f3dcc90b050d75af62bb867a2acc07)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>

include/haproxy/time-t.h [new file with mode: 0644]
include/haproxy/time.h

diff --git a/include/haproxy/time-t.h b/include/haproxy/time-t.h
new file mode 100644 (file)
index 0000000..5ca6177
--- /dev/null
@@ -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 */
index 3ebc683..b8240b2 100644 (file)
 #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 */
 
 /*