-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcpu.c
51 lines (46 loc) · 998 Bytes
/
cpu.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
//#define _GNU_SOURCE
#include <stdio.h>
#include <sched.h>
#include <pthread.h>
#include "cpu.h"
#define CPU_CORE 16
void pin_to_cpu(int cpu) {
cpu_set_t cpuset; // set of CPUs allocating to the LVRM
/*
if (getuid()) {
// exit if user is not root
LOG_ERR("Root required\n");
return;
}
*/
CPU_ZERO(&cpuset);
CPU_SET(cpu, &cpuset);
if (pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset) < 0) {
fprintf(stderr, "set thread affinity failed\n");
}
cpu_set_t get;
CPU_ZERO(&get);
if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) {
fprintf(stderr, "get thread affinity failed\n");
}
/*
for (int j = 0; j < CPU_CORE; j++) {
if (CPU_ISSET(j, &get)) {
fprintf(stderr, " running in processor %d\n", j);
}
}
*/
}
int pin_array[15] = {
0,
4,
1,
5,
2,
6,
3,
7
};
void pin_thread(int id) {
pin_to_cpu(pin_array[id]);
}