clarify cpu/tics clocks
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Thu, 16 Oct 2025 15:38:28 +0000 (17:38 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Thu, 16 Oct 2025 15:38:28 +0000 (17:38 +0200)
load.c

diff --git a/load.c b/load.c
index ff39552dc08bf71a99c54844bab2d5b1594ec193..6a10751598d012dca3072b8f605ff697232ae992 100644 (file)
--- a/load.c
+++ b/load.c
@@ -151,7 +151,7 @@ void *work (__attribute__((unused)) void *arg)
     pthread_exit (NULL);
 }
 
-double estimate_clock (void)
+double estimate_tics_clock (void)
 {
     int n = 4;
     int64_t diff_nc = 0;
@@ -173,14 +173,54 @@ double estimate_clock (void)
         int64_t nc2 = rdtsc ();
 
         diff_nc = nc2 - nc1;
-        double delta = ((double)tv2.tv_sec + tv2.tv_usec * 1e-6) -
+        double runtime = ((double)tv2.tv_sec + tv2.tv_usec * 1e-6) -
             ((double)tv1.tv_sec + tv1.tv_usec * 1e-6);
-        clock = diff_nc / delta;
+
+        clock = diff_nc / runtime;
     }
 
     return clock;
 }
 
+double estimate_cpu_clock (void)
+{
+    struct timeval tv1;
+    gettimeofday (&tv1, NULL);
+
+    int instructions = 0;
+    while (instructions < 10000000) {
+#define INST0 "add %[i], %[i], #1\n\t"
+#define INST1 INST0 INST0 INST0 INST0  INST0 INST0 INST0 INST0 \
+              INST0 INST0 INST0 INST0  INST0 INST0 INST0 INST0
+#define INST2 INST1 INST1 INST1 INST1  INST1 INST1 INST1 INST1 \
+              INST1 INST1 INST1 INST1  INST1 INST1 INST1 INST1
+#define INST3 INST2 INST2 INST2 INST2  INST2 INST2 INST2 INST2 \
+              INST2 INST2 INST2 INST2  INST2 INST2 INST2 INST2
+        asm volatile (INST3 : [i] "+r" (instructions) :: "cc");
+        asm volatile (INST3 : [i] "+r" (instructions) :: "cc");
+        asm volatile (INST3 : [i] "+r" (instructions) :: "cc");
+        asm volatile (INST3 : [i] "+r" (instructions) :: "cc");
+        asm volatile (INST3 : [i] "+r" (instructions) :: "cc");
+        asm volatile (INST3 : [i] "+r" (instructions) :: "cc");
+        asm volatile (INST3 : [i] "+r" (instructions) :: "cc");
+        asm volatile (INST3 : [i] "+r" (instructions) :: "cc");
+        asm volatile (INST3 : [i] "+r" (instructions) :: "cc");
+        asm volatile (INST3 : [i] "+r" (instructions) :: "cc");
+        asm volatile (INST3 : [i] "+r" (instructions) :: "cc");
+        asm volatile (INST3 : [i] "+r" (instructions) :: "cc");
+        asm volatile (INST3 : [i] "+r" (instructions) :: "cc");
+        asm volatile (INST3 : [i] "+r" (instructions) :: "cc");
+    }
+
+    struct timeval tv2;
+    gettimeofday (&tv2, NULL);
+
+    double runtime = ((double)tv2.tv_sec + tv2.tv_usec * 1e-6) -
+        ((double)tv1.tv_sec + tv1.tv_usec * 1e-6);
+
+    return instructions / runtime;
+}
+
 /* main function */
 
 int main (int argc, char *argv[])
@@ -258,8 +298,12 @@ int main (int argc, char *argv[])
 
     if (mode)  {
         while (mode--) {
-            printf ("Clock: %.0fMHz\n", estimate_clock () / 1e6);
+            printf ("\r");
+            printf ("Tics clock: %.0fMHz", estimate_tics_clock () / 1e6);
+            printf ("\t");
+            printf ("CPU clock: %.0fMHz", estimate_cpu_clock () / 1e6);
         }
+        printf ("\n");
     } else if (nb_threads == 0) {
         while (1) {
             sleep (60);