]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/CpuAffinitySet.cc
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 54 Interprocess Communication */
12 #include "base/TextException.h"
13 #include "CpuAffinitySet.h"
14 #include "debug/Stream.h"
20 CpuAffinitySet::CpuAffinitySet()
23 CPU_ZERO(&theOrigCpuSet
);
27 CpuAffinitySet::apply()
29 Must(CPU_COUNT(&theCpuSet
) > 0); // CPU affinity mask set
33 if (sched_getaffinity(0, sizeof(theOrigCpuSet
), &theOrigCpuSet
)) {
35 debugs(54, DBG_IMPORTANT
, "ERROR: failed to get CPU affinity for "
36 "process PID " << getpid() << ", ignoring CPU affinity for "
37 "this process: " << xstrerr(xerrno
));
40 memcpy(&cpuSet
, &theCpuSet
, sizeof(cpuSet
));
41 CPU_AND(&cpuSet
, &cpuSet
, &theOrigCpuSet
);
42 if (CPU_COUNT(&cpuSet
) <= 0) {
43 debugs(54, DBG_IMPORTANT
, "ERROR: invalid CPU affinity for process "
44 "PID " << getpid() << ", may be caused by an invalid core in "
45 "'cpu_affinity_map' or by external affinity restrictions");
46 } else if (sched_setaffinity(0, sizeof(cpuSet
), &cpuSet
)) {
48 debugs(54, DBG_IMPORTANT
, "ERROR: failed to set CPU affinity for "
49 "process PID " << getpid() << ": " << xstrerr(xerrno
));
54 CPU_ZERO(&theOrigCpuSet
);
58 CpuAffinitySet::undo()
61 if (sched_setaffinity(0, sizeof(theOrigCpuSet
), &theOrigCpuSet
)) {
63 debugs(54, DBG_IMPORTANT
, "ERROR: failed to restore original CPU "
64 "affinity for process PID " << getpid() << ": " <<
67 CPU_ZERO(&theOrigCpuSet
);
72 CpuAffinitySet::applied()
74 // NOTE: cannot be const.
75 // According to CPU_SET(3) and, apparently, on some systems (e.g.,
76 // OpenSuSE 10.3) CPU_COUNT macro expects a non-const argument.
77 return (CPU_COUNT(&theOrigCpuSet
) > 0);
81 CpuAffinitySet::set(const cpu_set_t
&aCpuSet
)
83 memcpy(&theCpuSet
, &aCpuSet
, sizeof(theCpuSet
));