]>
Commit | Line | Data |
---|---|---|
96c2bb61 | 1 | /* |
bbc27441 | 2 | * Copyright (C) 1996-2014 The Squid Software Foundation and contributors |
96c2bb61 | 3 | * |
bbc27441 AJ |
4 | * Squid software is distributed under GPLv2+ license and includes |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
96c2bb61 AR |
7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 54 Interprocess Communication */ |
10 | ||
f7f3304a | 11 | #include "squid.h" |
96c2bb61 AR |
12 | #include "base/TextException.h" |
13 | #include "CpuAffinitySet.h" | |
14 | #include "Debug.h" | |
15 | #include "util.h" | |
16 | ||
074d6a40 AJ |
17 | #include <cerrno> |
18 | #include <cstring> | |
96c2bb61 | 19 | |
96c2bb61 AR |
20 | CpuAffinitySet::CpuAffinitySet() |
21 | { | |
22 | CPU_ZERO(&theCpuSet); | |
23 | CPU_ZERO(&theOrigCpuSet); | |
24 | } | |
25 | ||
26 | void | |
27 | CpuAffinitySet::apply() | |
28 | { | |
29 | Must(CPU_COUNT(&theCpuSet) > 0); // CPU affinity mask set | |
30 | Must(!applied()); | |
31 | ||
32 | bool success = false; | |
33 | if (sched_getaffinity(0, sizeof(theOrigCpuSet), &theOrigCpuSet)) { | |
34 | debugs(54, DBG_IMPORTANT, "ERROR: failed to get CPU affinity for " | |
35 | "process PID " << getpid() << ", ignoring CPU affinity for " | |
36 | "this process: " << xstrerror()); | |
37 | } else { | |
38 | cpu_set_t cpuSet; | |
41d00cd3 | 39 | memcpy(&cpuSet, &theCpuSet, sizeof(cpuSet)); |
6d03885d | 40 | (void) CPU_AND(&cpuSet, &cpuSet, &theOrigCpuSet); |
96c2bb61 AR |
41 | if (CPU_COUNT(&cpuSet) <= 0) { |
42 | debugs(54, DBG_IMPORTANT, "ERROR: invalid CPU affinity for process " | |
43 | "PID " << getpid() << ", may be caused by an invalid core in " | |
44 | "'cpu_affinity_map' or by external affinity restrictions"); | |
45 | } else if (sched_setaffinity(0, sizeof(cpuSet), &cpuSet)) { | |
46 | debugs(54, DBG_IMPORTANT, "ERROR: failed to set CPU affinity for " | |
47 | "process PID " << getpid() << ": " << xstrerror()); | |
48 | } else | |
49 | success = true; | |
50 | } | |
51 | if (!success) | |
52 | CPU_ZERO(&theOrigCpuSet); | |
53 | } | |
54 | ||
55 | void | |
56 | CpuAffinitySet::undo() | |
57 | { | |
58 | if (applied()) { | |
59 | if (sched_setaffinity(0, sizeof(theOrigCpuSet), &theOrigCpuSet)) { | |
60 | debugs(54, DBG_IMPORTANT, "ERROR: failed to restore original CPU " | |
61 | "affinity for process PID " << getpid() << ": " << | |
62 | xstrerror()); | |
63 | } | |
64 | CPU_ZERO(&theOrigCpuSet); | |
65 | } | |
66 | } | |
67 | ||
68 | bool | |
5ffe1090 | 69 | CpuAffinitySet::applied() |
96c2bb61 | 70 | { |
5ffe1090 DK |
71 | // NOTE: cannot be const. |
72 | // According to CPU_SET(3) and, apparently, on some systems (e.g., | |
73 | // OpenSuSE 10.3) CPU_COUNT macro expects a non-const argument. | |
96c2bb61 AR |
74 | return (CPU_COUNT(&theOrigCpuSet) > 0); |
75 | } | |
76 | ||
77 | void | |
78 | CpuAffinitySet::set(const cpu_set_t &aCpuSet) | |
79 | { | |
41d00cd3 | 80 | memcpy(&theCpuSet, &aCpuSet, sizeof(theCpuSet)); |
96c2bb61 | 81 | } |
f53969cc | 82 |