-
Notifications
You must be signed in to change notification settings - Fork 18
/
cycles.cc
50 lines (42 loc) · 1.19 KB
/
cycles.cc
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
49
50
#include <assert.h>
#include <common/cycles.h>
#include <stdio.h>
#include <string.h>
namespace Common
{
float get_rdtsc_frequency_mhz() {
FILE *fp;
char buffer[1024];
size_t bytes_read;
char *match;
float clock_speed = 0.0;
float clock_speed_in_MHz = 0;
/* Read the entire contents of /proc/cpuinfo into the buffer. */
fp = fopen("/proc/cpuinfo", "r");
bytes_read = fread(buffer, 1, sizeof(buffer) - 1, fp);
fclose(fp);
assert(bytes_read > 0);
buffer[bytes_read] = '\0';
/* First try to get cpu freq from model name. */
match = strstr(buffer, "model name");
assert(match);
while (*match != '@') match++;
/* Parse the line to extract the clock speed. */
sscanf(match, "@ %f", &clock_speed);
if (clock_speed > 0)
clock_speed_in_MHz = clock_speed * 1000.0;
else {
/* some qemu guest and the aep1 host don't have freq in model name */
match = strstr(buffer, "cpu MHz");
assert(match);
while (*match != ':') match++;
sscanf(match, ": %f", &clock_speed_in_MHz);
}
assert(clock_speed_in_MHz > 0);
return clock_speed_in_MHz;
}
float cycles_to_usec(cpu_time_t time)
{
return ((float) time) / get_rdtsc_frequency_mhz();
}
} // namespace Core