]> git.ipfire.org Git - thirdparty/squid.git/blob - src/CpuAffinityMap.cc
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / CpuAffinityMap.cc
1 /*
2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 /* DEBUG: section 54 Interprocess Communication */
10
11 #include "squid.h"
12 #include "base/TextException.h"
13 #include "CpuAffinityMap.h"
14 #include "CpuAffinitySet.h"
15 #include "Debug.h"
16
17 bool
18 CpuAffinityMap::add(const std::vector<int> &aProcesses, const std::vector<int> &aCores)
19 {
20 if (aProcesses.size() != aCores.size())
21 return false;
22
23 for (size_t i = 0; i < aProcesses.size(); ++i) {
24 const int process = aProcesses[i];
25 const int core = aCores[i];
26 if (process <= 0 || core <= 0)
27 return false;
28 theProcesses.push_back(process);
29 theCores.push_back(core);
30 }
31
32 return true;
33 }
34
35 CpuAffinitySet *
36 CpuAffinityMap::calculateSet(const int targetProcess) const
37 {
38 Must(theProcesses.size() == theCores.size());
39 int core = 0;
40 for (size_t i = 0; i < theProcesses.size(); ++i) {
41 const int process = theProcesses[i];
42 if (process == targetProcess) {
43 if (core > 0) {
44 debugs(54, DBG_CRITICAL, "WARNING: conflicting "
45 "'cpu_affinity_map' for process number " << process <<
46 ", using the last core seen: " << theCores[i]);
47 }
48 core = theCores[i];
49 }
50 }
51 CpuAffinitySet *cpuAffinitySet = NULL;
52 if (core > 0) {
53 cpuAffinitySet = new CpuAffinitySet;
54 cpu_set_t cpuSet;
55 CPU_ZERO(&cpuSet);
56 CPU_SET(core - 1, &cpuSet);
57 cpuAffinitySet->set(cpuSet);
58 }
59 return cpuAffinitySet;
60 }
61