From 88d749804ba41591aa8a52d985457e87ec542be8 Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Thu, 16 Oct 2025 17:38:28 +0200 Subject: [PATCH] clarify cpu/tics clocks --- load.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/load.c b/load.c index ff39552..6a10751 100644 --- 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); -- 2.30.2