]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: clock: add now_cpu_time_fast() function
authorAurelien DARRAGON <adarragon@haproxy.com>
Tue, 4 Apr 2023 15:21:40 +0000 (17:21 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 19 Apr 2023 09:03:31 +0000 (11:03 +0200)
Same as now_cpu_time(), but for fast queries (less accurate)
Relies on now_cpu_time() and now_mono_time_fast() is used
as a cache expiration hint to prevent now_cpu_time() from being
called too often since it is known to be quite expensive.

Depends on commit "MINOR: clock: add now_mono_time_fast() function"

include/haproxy/clock.h
src/clock.c

index 9d5cdca74bc500394ba963ff934e015cfaa9af27..fa392adca025e96291dd85e394a8798f0dbeb9bc 100644 (file)
@@ -36,6 +36,7 @@ uint64_t now_cpu_time_thread(int thr);
 uint64_t now_mono_time(void);
 uint64_t now_mono_time_fast(void);
 uint64_t now_cpu_time(void);
+uint64_t now_cpu_time_fast(void);
 void clock_set_local_source(void);
 void clock_update_local_date(int max_wait, int interrupted);
 void clock_update_global_date();
index d4ed2d54ab1a7a319c51cf7101ac5993176b98c7..f250feab670256f28fa494639dca342594a6199c 100644 (file)
@@ -93,6 +93,32 @@ uint64_t now_cpu_time(void)
        return ret;
 }
 
+/* Returns the current thread's cumulated CPU time in nanoseconds.
+ *
+ * thread_local timer is cached so that call is less precise but also less
+ * expensive if heavily used.
+ * We use the mono time as a cache expiration hint since now_cpu_time() is
+ * known to be much more expensive than now_mono_time_fast() on systems
+ * supporting the COARSE clock source.
+ *
+ * Returns 0 if either now_mono_time_fast() or now_cpu_time() are not
+ * supported.
+ */
+uint64_t now_cpu_time_fast(void)
+{
+       static THREAD_LOCAL uint64_t mono_cache = 0;
+       static THREAD_LOCAL uint64_t cpu_cache = 0;
+       uint64_t mono_cur;
+
+       mono_cur = now_mono_time_fast();
+       if (unlikely(mono_cur !=  mono_cache)) {
+               /* global mono clock was updated: local cache is outdated */
+               cpu_cache = now_cpu_time();
+               mono_cache = mono_cur;
+       }
+       return cpu_cache;
+}
+
 /* returns another thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
 uint64_t now_cpu_time_thread(int thr)
 {