]>
Commit | Line | Data |
---|---|---|
96c2bb61 AR |
1 | /* |
2 | * $Id$ | |
3 | * | |
4 | * DEBUG: section 54 Interprocess Communication | |
5 | * | |
6 | */ | |
7 | ||
8 | #include "config.h" | |
9 | #include "base/TextException.h" | |
10 | #include "CpuAffinitySet.h" | |
11 | #include "Debug.h" | |
12 | #include "util.h" | |
13 | ||
a1fe225c AJ |
14 | #if HAVE_ERRNO_H |
15 | #include <errno.h> | |
16 | #endif | |
96c2bb61 | 17 | #if HAVE_STRING_H |
a1fe225c | 18 | #include <string.h> |
96c2bb61 AR |
19 | #endif |
20 | ||
21 | ||
22 | CpuAffinitySet::CpuAffinitySet() | |
23 | { | |
24 | CPU_ZERO(&theCpuSet); | |
25 | CPU_ZERO(&theOrigCpuSet); | |
26 | } | |
27 | ||
28 | void | |
29 | CpuAffinitySet::apply() | |
30 | { | |
31 | Must(CPU_COUNT(&theCpuSet) > 0); // CPU affinity mask set | |
32 | Must(!applied()); | |
33 | ||
34 | bool success = false; | |
35 | if (sched_getaffinity(0, sizeof(theOrigCpuSet), &theOrigCpuSet)) { | |
36 | debugs(54, DBG_IMPORTANT, "ERROR: failed to get CPU affinity for " | |
37 | "process PID " << getpid() << ", ignoring CPU affinity for " | |
38 | "this process: " << xstrerror()); | |
39 | } else { | |
40 | cpu_set_t cpuSet; | |
41d00cd3 | 41 | memcpy(&cpuSet, &theCpuSet, sizeof(cpuSet)); |
96c2bb61 AR |
42 | CPU_AND(&cpuSet, &cpuSet, &theOrigCpuSet); |
43 | if (CPU_COUNT(&cpuSet) <= 0) { | |
44 | debugs(54, DBG_IMPORTANT, "ERROR: invalid CPU affinity for process " | |
45 | "PID " << getpid() << ", may be caused by an invalid core in " | |
46 | "'cpu_affinity_map' or by external affinity restrictions"); | |
47 | } else if (sched_setaffinity(0, sizeof(cpuSet), &cpuSet)) { | |
48 | debugs(54, DBG_IMPORTANT, "ERROR: failed to set CPU affinity for " | |
49 | "process PID " << getpid() << ": " << xstrerror()); | |
50 | } else | |
51 | success = true; | |
52 | } | |
53 | if (!success) | |
54 | CPU_ZERO(&theOrigCpuSet); | |
55 | } | |
56 | ||
57 | void | |
58 | CpuAffinitySet::undo() | |
59 | { | |
60 | if (applied()) { | |
61 | if (sched_setaffinity(0, sizeof(theOrigCpuSet), &theOrigCpuSet)) { | |
62 | debugs(54, DBG_IMPORTANT, "ERROR: failed to restore original CPU " | |
63 | "affinity for process PID " << getpid() << ": " << | |
64 | xstrerror()); | |
65 | } | |
66 | CPU_ZERO(&theOrigCpuSet); | |
67 | } | |
68 | } | |
69 | ||
70 | bool | |
71 | CpuAffinitySet::applied() const | |
72 | { | |
73 | return (CPU_COUNT(&theOrigCpuSet) > 0); | |
74 | } | |
75 | ||
76 | void | |
77 | CpuAffinitySet::set(const cpu_set_t &aCpuSet) | |
78 | { | |
41d00cd3 | 79 | memcpy(&theCpuSet, &aCpuSet, sizeof(theCpuSet)); |
96c2bb61 | 80 | } |