]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/Kids.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / ipc / Kids.cc
1 /*
2 * DEBUG: section 54 Interprocess Communication
3 */
4
5 #include "squid.h"
6 #include "base/TextException.h"
7 #include "globals.h"
8 #include "ipc/Kids.h"
9 #include "SquidConfig.h"
10 #include "tools.h"
11
12 Kids TheKids;
13 KidName TheKidName;
14
15 Kids::Kids()
16 {
17 }
18
19 /// maintain n kids
20 void Kids::init()
21 {
22 if (storage.size() > 0)
23 storage.clean();
24
25 storage.reserve(NumberOfKids());
26
27 char kid_name[32];
28
29 // add Kid records for all workers
30 for (int i = 0; i < Config.workers; ++i) {
31 snprintf(kid_name, sizeof(kid_name), "(squid-%d)", (int)(storage.size()+1));
32 storage.push_back(Kid(kid_name));
33 }
34
35 // add Kid records for all disk processes
36 for (int i = 0; i < Config.cacheSwap.n_strands; ++i) {
37 snprintf(kid_name, sizeof(kid_name), "(squid-disk-%d)", (int)(storage.size()+1));
38 storage.push_back(Kid(kid_name));
39 }
40
41 // if coordination is needed, add a Kid record for Coordinator
42 if (storage.size() > 1) {
43 snprintf(kid_name, sizeof(kid_name), "(squid-coord-%d)", (int)(storage.size()+1));
44 storage.push_back(Kid(kid_name));
45 }
46
47 Must(storage.size() == static_cast<size_t>(NumberOfKids()));
48 }
49
50 /// returns kid by pid
51 Kid* Kids::find(pid_t pid)
52 {
53 assert(pid > 0);
54 assert(count() > 0);
55
56 for (size_t i = 0; i < storage.size(); ++i) {
57 if (storage[i].getPid() == pid)
58 return &storage[i];
59 }
60 return NULL;
61 }
62
63 /// returns the kid by index, useful for kids iteration
64 Kid& Kids::get(size_t i)
65 {
66 assert(i < count());
67 return storage[i];
68 }
69
70 /// whether all kids are hopeless
71 bool Kids::allHopeless() const
72 {
73 for (size_t i = 0; i < storage.size(); ++i) {
74 if (!storage[i].hopeless())
75 return false;
76 }
77 return true;
78 }
79
80 /// whether all kids called exited happy
81 bool Kids::allExitedHappy() const
82 {
83 for (size_t i = 0; i < storage.size(); ++i) {
84 if (!storage[i].exitedHappy())
85 return false;
86 }
87 return true;
88 }
89
90 /// whether some kids died from a given signal
91 bool Kids::someSignaled(const int sgnl) const
92 {
93 for (size_t i = 0; i < storage.size(); ++i) {
94 if (storage[i].signaled(sgnl))
95 return true;
96 }
97 return false;
98 }
99
100 /// whether some kids are running
101 bool Kids::someRunning() const
102 {
103 for (size_t i = 0; i < storage.size(); ++i) {
104 if (storage[i].running())
105 return true;
106 }
107 return false;
108 }
109
110 /// whether some kids should be restarted by master
111 bool Kids::shouldRestartSome() const
112 {
113 for (size_t i = 0; i < storage.size(); ++i) {
114 if (storage[i].shouldRestart())
115 return true;
116 }
117 return false;
118 }
119
120 /// returns the number of kids
121 size_t Kids::count() const
122 {
123 return storage.size();
124 }