CpuAffinitySet.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 "CpuAffinitySet.h"
14 #include "Debug.h"
15 #include "util.h"
16 
17 #include <cerrno>
18 #include <cstring>
19 
21 {
24 }
25 
26 void
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  int xerrno = errno;
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));
38  } else {
39  cpu_set_t cpuSet;
40  memcpy(&cpuSet, &theCpuSet, sizeof(cpuSet));
41  (void) 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)) {
47  int xerrno = errno;
48  debugs(54, DBG_IMPORTANT, "ERROR: failed to set CPU affinity for "
49  "process PID " << getpid() << ": " << xstrerr(xerrno));
50  } else
51  success = true;
52  }
53  if (!success)
55 }
56 
57 void
59 {
60  if (applied()) {
61  if (sched_setaffinity(0, sizeof(theOrigCpuSet), &theOrigCpuSet)) {
62  int xerrno = errno;
63  debugs(54, DBG_IMPORTANT, "ERROR: failed to restore original CPU "
64  "affinity for process PID " << getpid() << ": " <<
65  xstrerr(xerrno));
66  }
68  }
69 }
70 
71 bool
73 {
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);
78 }
79 
80 void
82 {
83  memcpy(&theCpuSet, &aCpuSet, sizeof(theCpuSet));
84 }
85 
bool applied()
whether apply() was called and was not undone
#define CPU_ZERO(set)
Definition: cpu.h:35
int sched_setaffinity(int, size_t, cpu_set_t *)
Definition: cpu.h:26
const char * xstrerr(int error)
Definition: xstrerror.cc:83
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
#define DBG_IMPORTANT
Definition: Debug.h:45
#define CPU_COUNT(set)
Definition: cpu.h:52
cpu_set_t theCpuSet
configured CPU affinity for this process
cpu_set_t theOrigCpuSet
CPU affinity for this process before apply()
#define Must(cond)
Definition: TextException.h:89
int sched_getaffinity(int, size_t, cpu_set_t *)
Definition: cpu.h:27
Definition: cpu.h:22
void apply()
set CPU affinity for this process
void set(const cpu_set_t &aCpuSet)
set CPU affinity mask
void undo()
undo CPU affinity changes for this process
#define CPU_AND(destset, srcset1, srcset2)
Definition: cpu.h:68

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors