parlib/linux: cache the value of the TSC frequency
authorBarret Rhoden <brho@cs.berkeley.edu>
Mon, 8 Jun 2020 23:17:58 +0000 (19:17 -0400)
committerBarret Rhoden <brho@cs.berkeley.edu>
Tue, 9 Jun 2020 17:10:18 +0000 (13:10 -0400)
Instead of computing it every time.  Note: we probably should get this
from an MSR, if possible.  Computing takes about a second, and is
usually close enough.

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
user/parlib/include/parlib/tsc-compat.h

index a124de9..66195c7 100644 (file)
@@ -91,9 +91,16 @@ static inline uint64_t read_tsc_serialized(void)
 
 static inline uint64_t get_tsc_freq(void)
 {
+       static uint64_t freq;
+
        struct timeval prev;
        struct timeval curr;
-       uint64_t beg = read_tsc_serialized();
+       uint64_t beg, end;
+
+       if (freq)
+               return freq;
+
+       beg = read_tsc_serialized();
 
        gettimeofday(&prev, 0);
        while (1) {
@@ -103,9 +110,10 @@ static inline uint64_t get_tsc_freq(void)
                         prev.tv_usec))
                        break;
        }
-       uint64_t end = read_tsc_serialized();
+       end = read_tsc_serialized();
 
-       return end - beg;
+       freq = end - beg;
+       return freq;
 }
 
 /* Don't have a good way to get the overhead on Linux in userspace. */