-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpufreqest.c
48 lines (42 loc) · 1.32 KB
/
cpufreqest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// file copied from sample code online:
// http://uob-hpc.github.io/2017/11/22/arm-clock-freq.html
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#ifndef ITRS
#define ITRS 1000000000
#endif
int main(int argc, char *argv[])
{
struct timeval tv;
gettimeofday(&tv, NULL);
double start = tv.tv_sec + tv.tv_usec*1e-6;
long instructions;
for (instructions = 0; instructions < ITRS; )
{
#if defined(__arm__) || defined(__aarch64__)
#define INST0 "add %[i], %[i], #1\n\t"
#else // x86 variants
#define INST0 "add $1, %[i]\n\t"
#endif
#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"
);
}
gettimeofday(&tv, NULL);
double end = tv.tv_sec + tv.tv_usec*1e-6;
double runtime = end-start;
printf("Runtime (seconds) = %lf\n", runtime);
printf("Instructions executed = %ld\n", instructions);
printf("Estimated frequency = %.2lf MHz\n", (instructions/runtime)*1e-6);
return 0;
}