]> git.ipfire.org Git - thirdparty/squid.git/blame - src/CpuAffinitySet.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / CpuAffinitySet.cc
CommitLineData
96c2bb61
AR
1/*
2 * $Id$
3 *
4 * DEBUG: section 54 Interprocess Communication
5 *
6 */
7
f7f3304a 8#include "squid.h"
96c2bb61
AR
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
22CpuAffinitySet::CpuAffinitySet()
23{
24 CPU_ZERO(&theCpuSet);
25 CPU_ZERO(&theOrigCpuSet);
26}
27
28void
29CpuAffinitySet::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
57void
58CpuAffinitySet::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
70bool
71CpuAffinitySet::applied() const
72{
73 return (CPU_COUNT(&theOrigCpuSet) > 0);
74}
75
76void
77CpuAffinitySet::set(const cpu_set_t &aCpuSet)
78{
41d00cd3 79 memcpy(&theCpuSet, &aCpuSet, sizeof(theCpuSet));
96c2bb61 80}