-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathaffinity.cpp
83 lines (61 loc) · 1.62 KB
/
affinity.cpp
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
77
78
79
80
81
82
83
/*
-- MAGMA (version 2.1.0) --
Univ. of Tennessee, Knoxville
Univ. of California, Berkeley
Univ. of Colorado, Denver
@date August 2016
@author Raffaele Solca
*/
#ifndef MAGMA_NOAFFINITY
#include "affinity.h"
#include <stdio.h>
affinity_set::affinity_set()
{
CPU_ZERO(&set);
}
affinity_set::affinity_set(int cpu)
{
CPU_ZERO(&set);
CPU_SET(cpu, &set);
}
void affinity_set::add(int cpu)
{
CPU_SET(cpu, &set);
}
int affinity_set::get_affinity()
{
return sched_getaffinity( 0, sizeof(set), &set);
}
int affinity_set::set_affinity()
{
return sched_setaffinity( 0, sizeof(set), &set);
}
void affinity_set::print_affinity(int id, const char* s)
{
if (get_affinity() == 0)
print_set(id, s);
else
printf("Error in sched_getaffinity\n");
}
void affinity_set::print_set(int id, const char* s)
{
// CPU_COUNT() first appeared in glibc 2.6.
#if __GLIBC_PREREQ(2,6)
char cpustring[1024];
int cpu_count = CPU_COUNT(&set);
int charcnt = 0;
charcnt = snprintf( cpustring, sizeof(cpustring), "thread %d has affinity with %d CPUS: ", id, cpu_count);
int nrcpu = 0;
for (int icpu=0; nrcpu < cpu_count && icpu < CPU_SETSIZE; ++icpu) {
if ( CPU_ISSET( icpu, &set )) {
charcnt += snprintf( &(cpustring[charcnt]), sizeof(cpustring)-charcnt, "%d,", icpu );
++nrcpu;
}
}
// charcnt-1 is used to remove "," after last cpu.
charcnt += snprintf( &(cpustring[charcnt-1]), sizeof(cpustring)-(charcnt-1), "\n" ) - 1;
printf("%s: %s", s, cpustring);
fflush(stdout);
#endif
}
#endif // MAGMA_NOAFFINITY