-
Notifications
You must be signed in to change notification settings - Fork 64
/
utils.c
64 lines (51 loc) · 1.23 KB
/
utils.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "headers.h"
#include "utils.h"
static uint64_t freq = 0;
static uint64_t get_cpu_freq(void) {
if(freq)
return freq;
FILE *fd;
float freqf = 0;
char *line = NULL;
size_t len = 0;
fd = fopen("/proc/cpuinfo", "r");
if (!fd) {
fprintf(stderr, "failed to get cpu frequency\n");
perror(NULL);
return freq;
}
while (getline(&line, &len, fd) != EOF) {
if (sscanf(line, "cpu MHz\t: %f", &freqf) == 1) {
freqf = freqf * 1000000UL;
freq = (uint64_t) freqf;
break;
}
}
fclose(fd);
return freq;
}
uint64_t cycles_to_us(uint64_t cycles) {
return cycles*1000000LU/get_cpu_freq();
}
void shuffle(size_t *array, size_t n) {
if (n > 1) {
size_t i;
for (i = 0; i < n - 1; i++) {
size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
size_t t = array[j];
array[j] = array[i];
array[i] = t;
}
}
}
void pin_me_on(int core) {
if(!PINNING)
return;
cpu_set_t cpuset;
pthread_t thread = pthread_self();
CPU_ZERO(&cpuset);
CPU_SET(core, &cpuset);
int s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
if (s != 0)
die("Cannot pin thread on core %d\n", core);
}