]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: time: define tot_time structure
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Wed, 7 Aug 2024 12:50:26 +0000 (14:50 +0200)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Wed, 7 Aug 2024 13:40:52 +0000 (15:40 +0200)
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.

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 3ebc683e1b968a6173170bad6050b5736e7b1d6c..b8240b26e41280cf1f0bbf812c8f6ebf91aa5472 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 */
 
 /*