* read CPU frequency from a sysfs file, return an frequency in Megahertz as * an int or exit on any error. */
| 41 | * an int or exit on any error. |
| 42 | */ |
| 43 | static unsigned long arch_tick_frequency(void) |
| 44 | { |
| 45 | FILE *cpuf; |
| 46 | char freqs[100]; |
| 47 | int size; |
| 48 | char *endp; |
| 49 | uint64_t rv; |
| 50 | |
| 51 | const char* freq_file = |
| 52 | "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"; |
| 53 | |
| 54 | cpuf = fopen(freq_file, "r"); |
| 55 | if (!cpuf) { |
| 56 | fprintf(stderr, "Could not open %s: %s\n", |
| 57 | freq_file, strerror(errno)); |
| 58 | exit(1); |
| 59 | } |
| 60 | |
| 61 | memset(freqs, 0, sizeof(freqs)); |
| 62 | size = fread(freqs, 1, sizeof(freqs), cpuf); |
| 63 | if (!size || (size == sizeof(freqs))) { |
| 64 | fprintf(stderr, "Wrong number of bytes(%d) read from %s\n", |
| 65 | size, freq_file); |
| 66 | exit(1); |
| 67 | } |
| 68 | fclose(cpuf); |
| 69 | rv = strtoull(freqs, &endp, 10); |
| 70 | |
| 71 | if (*endp == '\0' || *endp == '\n') |
| 72 | /* cpuinfo_max_freq is in kHz. Convert it to MHz. */ |
| 73 | return rv / 1000; |
| 74 | fprintf(stderr, "Wrong formatted value ^%s^ read from %s\n", |
| 75 | freqs, freq_file); |
| 76 | exit(1); |
| 77 | } |
| 78 | #elif defined(__OpenBSD__) && (defined(__i386__) || defined(__x86_64__)) |
| 79 | static unsigned long arch_tick_frequency(void) |
| 80 | { |