1 // SPDX-License-Identifier: GPL-2.0
13 #include "../kselftest.h"
15 void usage(char *name
) {
16 printf ("Usage: %s cpunum\n", name
);
19 int main(int argc
, char **argv
) {
20 unsigned int i
, cpu
, fd
;
21 char msr_file_name
[64];
22 long long tsc
, old_tsc
, new_tsc
;
23 long long aperf
, old_aperf
, new_aperf
;
24 long long mperf
, old_mperf
, new_mperf
;
25 struct timeb before
, after
;
26 long long int start
, finish
, total
;
35 cpu
= strtol(argv
[1], (char **) NULL
, 10);
42 sprintf(msr_file_name
, "/dev/cpu/%d/msr", cpu
);
43 fd
= open(msr_file_name
, O_RDONLY
);
46 printf("/dev/cpu/%d/msr: %s\n", cpu
, strerror(errno
));
51 CPU_SET(cpu
, &cpuset
);
53 if (sched_setaffinity(0, sizeof(cpu_set_t
), &cpuset
)) {
54 perror("Failed to set cpu affinity");
59 pread(fd
, &old_tsc
, sizeof(old_tsc
), 0x10);
60 pread(fd
, &old_aperf
, sizeof(old_mperf
), 0xe7);
61 pread(fd
, &old_mperf
, sizeof(old_aperf
), 0xe8);
63 for (i
=0; i
<0x8fffffff; i
++) {
68 pread(fd
, &new_tsc
, sizeof(new_tsc
), 0x10);
69 pread(fd
, &new_aperf
, sizeof(new_mperf
), 0xe7);
70 pread(fd
, &new_mperf
, sizeof(new_aperf
), 0xe8);
72 tsc
= new_tsc
-old_tsc
;
73 aperf
= new_aperf
-old_aperf
;
74 mperf
= new_mperf
-old_mperf
;
76 start
= before
.time
*1000 + before
.millitm
;
77 finish
= after
.time
*1000 + after
.millitm
;
78 total
= finish
- start
;
80 printf("runTime: %4.2f\n", 1.0*total
/1000);
81 printf("freq: %7.0f\n", tsc
/ (1.0*aperf
/ (1.0 * mperf
)) / total
);