-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_helper.h
More file actions
76 lines (63 loc) · 1.89 KB
/
Copy pathbenchmark_helper.h
File metadata and controls
76 lines (63 loc) · 1.89 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
double timeDiff(struct timespec *timeA_p, struct timespec *timeB_p);
int get_cpu_model(char *out, size_t out_sz);
// Various functions
double timeDiff(struct timespec *timeA_p, struct timespec *timeB_p) {
return ((timeA_p->tv_sec - timeB_p->tv_sec) * 1000000000 + timeA_p->tv_nsec -
timeB_p->tv_nsec) /
1.0e9;
}
int get_cpu_model(char *out, size_t out_sz) {
FILE *f = fopen("/proc/cpuinfo", "r");
if (!f)
return -1;
char model[256] = {0};
int have_model = 0;
int cores = 0;
char line[512];
while (fgets(line, sizeof line, f)) {
// Count logical CPUs (one "processor" entry per logical core on Linux)
if (strncmp(line, "processor", strlen("processor")) == 0) {
char *colon = strchr(line, ':');
if (colon)
cores++;
}
if (!have_model) {
// x86 usually has: "model name\t: Intel(R) ... "
const char *key1 = "model name";
const char *key2 = "Hardware"; // common on ARM
const char *key3 = "Processor"; // sometimes on ARM
const char *keys[] = {key1, key2, key3};
for (size_t i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) {
size_t klen = strlen(keys[i]);
if (strncmp(line, keys[i], klen) == 0) {
char *colon = strchr(line, ':');
if (!colon)
continue;
colon++; // skip ':'
while (*colon == ' ' || *colon == '\t')
colon++;
// trim trailing newline
char *nl = strchr(colon, '\n');
if (nl)
*nl = '\0';
snprintf(model, sizeof model, "%s", colon);
have_model = 1;
break;
}
}
}
}
fclose(f);
if (!have_model)
return -2; // not found
if (cores <= 0)
cores = 1;
snprintf(out, out_sz, "%d x %s", cores, model);
return 0;
}