passenger 5.0.0.rc2 → 5.0.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of passenger might be problematic. Click here for more details.
- checksums.yaml +8 -8
- checksums.yaml.gz.asc +7 -7
- data.tar.gz.asc +7 -7
- data/CHANGELOG +10 -0
- data/CONTRIBUTORS +1 -0
- data/build/agents.rb +6 -0
- data/build/cxx_tests.rb +6 -0
- data/build/misc.rb +22 -1
- data/doc/Users guide Nginx.txt +12 -0
- data/ext/common/ApplicationPool2/Options.h +2 -1
- data/ext/common/ApplicationPool2/Pool.h +15 -744
- data/ext/common/ApplicationPool2/Pool/AnalyticsCollection.h +255 -0
- data/ext/common/ApplicationPool2/Pool/Debug.h +63 -0
- data/ext/common/ApplicationPool2/Pool/GarbageCollection.h +197 -0
- data/ext/common/ApplicationPool2/Pool/GeneralUtils.h +127 -0
- data/ext/common/ApplicationPool2/Pool/Inspection.h +214 -0
- data/ext/common/ApplicationPool2/Pool/ProcessUtils.h +85 -0
- data/ext/common/ApplicationPool2/Process.h +5 -7
- data/ext/common/Constants.h +1 -1
- data/ext/common/Hooks.h +2 -1
- data/ext/common/Utils.cpp +1 -1
- data/ext/common/Utils/JsonUtils.h +37 -1
- data/ext/common/agents/Base.cpp +45 -40
- data/ext/common/agents/Base.h +3 -2
- data/ext/common/agents/HelperAgent/RequestHandler/InitRequest.cpp +12 -10
- data/ext/nginx/Configuration.c +4 -1
- data/lib/phusion_passenger.rb +1 -1
- data/lib/phusion_passenger/common_library.rb +7 -1
- data/lib/phusion_passenger/config/restart_app_command.rb +40 -2
- data/lib/phusion_passenger/config/utils.rb +3 -5
- data/lib/phusion_passenger/utils/ansi_colors.rb +28 -21
- data/lib/phusion_passenger/utils/terminal_choice_menu.rb +27 -10
- data/resources/templates/standalone/config.erb +4 -4
- data/test/cxx/CxxTestMain.cpp +10 -22
- metadata +10 -4
- metadata.gz.asc +7 -7
@@ -0,0 +1,255 @@
|
|
1
|
+
/*
|
2
|
+
* Phusion Passenger - https://www.phusionpassenger.com/
|
3
|
+
* Copyright (c) 2011-2015 Phusion
|
4
|
+
*
|
5
|
+
* "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
|
6
|
+
*
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
12
|
+
* furnished to do so, subject to the following conditions:
|
13
|
+
*
|
14
|
+
* The above copyright notice and this permission notice shall be included in
|
15
|
+
* all copies or substantial portions of the Software.
|
16
|
+
*
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23
|
+
* THE SOFTWARE.
|
24
|
+
*/
|
25
|
+
|
26
|
+
// This file is included inside the Pool class.
|
27
|
+
|
28
|
+
private:
|
29
|
+
|
30
|
+
struct UnionStationLogEntry {
|
31
|
+
string groupName;
|
32
|
+
const char *category;
|
33
|
+
string key;
|
34
|
+
string data;
|
35
|
+
};
|
36
|
+
|
37
|
+
SystemMetricsCollector systemMetricsCollector;
|
38
|
+
SystemMetrics systemMetrics;
|
39
|
+
|
40
|
+
|
41
|
+
static void collectAnalytics(PoolPtr self) {
|
42
|
+
TRACE_POINT();
|
43
|
+
syscalls::usleep(3000000);
|
44
|
+
while (!this_thread::interruption_requested()) {
|
45
|
+
try {
|
46
|
+
UPDATE_TRACE_POINT();
|
47
|
+
self->realCollectAnalytics();
|
48
|
+
} catch (const thread_interrupted &) {
|
49
|
+
break;
|
50
|
+
} catch (const tracable_exception &e) {
|
51
|
+
P_WARN("ERROR: " << e.what() << "\n Backtrace:\n" << e.backtrace());
|
52
|
+
}
|
53
|
+
|
54
|
+
// Sleep for about 4 seconds, aligned to seconds boundary
|
55
|
+
// for saving power on laptops.
|
56
|
+
UPDATE_TRACE_POINT();
|
57
|
+
unsigned long long currentTime = SystemTime::getUsec();
|
58
|
+
unsigned long long deadline =
|
59
|
+
roundUp<unsigned long long>(currentTime, 1000000) + 4000000;
|
60
|
+
P_DEBUG("Analytics collection done; next analytics collection in " <<
|
61
|
+
std::fixed << std::setprecision(3) << ((deadline - currentTime) / 1000000.0) <<
|
62
|
+
" sec");
|
63
|
+
try {
|
64
|
+
syscalls::usleep(deadline - currentTime);
|
65
|
+
} catch (const thread_interrupted &) {
|
66
|
+
break;
|
67
|
+
} catch (const tracable_exception &e) {
|
68
|
+
P_WARN("ERROR: " << e.what() << "\n Backtrace:\n" << e.backtrace());
|
69
|
+
}
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
static void collectPids(const ProcessList &processes, vector<pid_t> &pids) {
|
74
|
+
foreach (const ProcessPtr &process, processes) {
|
75
|
+
pids.push_back(process->pid);
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
static void updateProcessMetrics(const ProcessList &processes,
|
80
|
+
const ProcessMetricMap &allMetrics,
|
81
|
+
vector<ProcessPtr> &processesToDetach)
|
82
|
+
{
|
83
|
+
foreach (const ProcessPtr &process, processes) {
|
84
|
+
ProcessMetricMap::const_iterator metrics_it =
|
85
|
+
allMetrics.find(process->pid);
|
86
|
+
if (metrics_it != allMetrics.end()) {
|
87
|
+
process->metrics = metrics_it->second;
|
88
|
+
// If the process is missing from 'allMetrics' then either 'ps'
|
89
|
+
// failed or the process really is gone. We double check by sending
|
90
|
+
// it a signal.
|
91
|
+
} else if (!process->dummy && !process->osProcessExists()) {
|
92
|
+
P_WARN("Process " << process->inspect() << " no longer exists! "
|
93
|
+
"Detaching it from the pool.");
|
94
|
+
processesToDetach.push_back(process);
|
95
|
+
}
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
void prepareUnionStationProcessStateLogs(vector<UnionStationLogEntry> &logEntries,
|
100
|
+
const GroupPtr &group) const
|
101
|
+
{
|
102
|
+
const UnionStation::CorePtr &unionStationCore = getUnionStationCore();
|
103
|
+
if (group->options.analytics && unionStationCore != NULL) {
|
104
|
+
logEntries.push_back(UnionStationLogEntry());
|
105
|
+
UnionStationLogEntry &entry = logEntries.back();
|
106
|
+
stringstream stream;
|
107
|
+
|
108
|
+
stream << "Group: <group>";
|
109
|
+
group->inspectXml(stream, false);
|
110
|
+
stream << "</group>";
|
111
|
+
|
112
|
+
entry.groupName = group->options.getAppGroupName();
|
113
|
+
entry.category = "processes";
|
114
|
+
entry.key = group->options.unionStationKey;
|
115
|
+
entry.data = stream.str();
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
void prepareUnionStationSystemMetricsLogs(vector<UnionStationLogEntry> &logEntries,
|
120
|
+
const GroupPtr &group) const
|
121
|
+
{
|
122
|
+
const UnionStation::CorePtr &unionStationCore = getUnionStationCore();
|
123
|
+
if (group->options.analytics && unionStationCore != NULL) {
|
124
|
+
logEntries.push_back(UnionStationLogEntry());
|
125
|
+
UnionStationLogEntry &entry = logEntries.back();
|
126
|
+
stringstream stream;
|
127
|
+
|
128
|
+
stream << "System metrics: ";
|
129
|
+
systemMetrics.toXml(stream);
|
130
|
+
|
131
|
+
entry.groupName = group->options.getAppGroupName();
|
132
|
+
entry.category = "system_metrics";
|
133
|
+
entry.key = group->options.unionStationKey;
|
134
|
+
entry.data = stream.str();
|
135
|
+
}
|
136
|
+
}
|
137
|
+
|
138
|
+
void realCollectAnalytics() {
|
139
|
+
TRACE_POINT();
|
140
|
+
this_thread::disable_interruption di;
|
141
|
+
this_thread::disable_syscall_interruption dsi;
|
142
|
+
vector<pid_t> pids;
|
143
|
+
unsigned int max;
|
144
|
+
|
145
|
+
P_DEBUG("Analytics collection time...");
|
146
|
+
// Collect all the PIDs.
|
147
|
+
{
|
148
|
+
UPDATE_TRACE_POINT();
|
149
|
+
LockGuard l(syncher);
|
150
|
+
max = this->max;
|
151
|
+
}
|
152
|
+
pids.reserve(max);
|
153
|
+
{
|
154
|
+
UPDATE_TRACE_POINT();
|
155
|
+
LockGuard l(syncher);
|
156
|
+
SuperGroupMap::ConstIterator sg_it(superGroups);
|
157
|
+
|
158
|
+
while (*sg_it != NULL) {
|
159
|
+
const SuperGroupPtr &superGroup = sg_it.getValue();
|
160
|
+
SuperGroup::GroupList::const_iterator g_it, g_end = superGroup->groups.end();
|
161
|
+
|
162
|
+
for (g_it = superGroup->groups.begin(); g_it != g_end; g_it++) {
|
163
|
+
const GroupPtr &group = *g_it;
|
164
|
+
collectPids(group->enabledProcesses, pids);
|
165
|
+
collectPids(group->disablingProcesses, pids);
|
166
|
+
collectPids(group->disabledProcesses, pids);
|
167
|
+
}
|
168
|
+
sg_it.next();
|
169
|
+
}
|
170
|
+
}
|
171
|
+
|
172
|
+
// Collect process metrics and system and store them in the
|
173
|
+
// data structures. Later, we log them to Union Station.
|
174
|
+
ProcessMetricMap processMetrics;
|
175
|
+
try {
|
176
|
+
UPDATE_TRACE_POINT();
|
177
|
+
processMetrics = ProcessMetricsCollector().collect(pids);
|
178
|
+
} catch (const ParseException &) {
|
179
|
+
P_WARN("Unable to collect process metrics: cannot parse 'ps' output.");
|
180
|
+
return;
|
181
|
+
}
|
182
|
+
try {
|
183
|
+
UPDATE_TRACE_POINT();
|
184
|
+
systemMetricsCollector.collect(systemMetrics);
|
185
|
+
} catch (const RuntimeException &e) {
|
186
|
+
P_WARN("Unable to collect system metrics: " << e.what());
|
187
|
+
return;
|
188
|
+
}
|
189
|
+
|
190
|
+
{
|
191
|
+
UPDATE_TRACE_POINT();
|
192
|
+
vector<UnionStationLogEntry> logEntries;
|
193
|
+
vector<ProcessPtr> processesToDetach;
|
194
|
+
boost::container::vector<Callback> actions;
|
195
|
+
ScopedLock l(syncher);
|
196
|
+
SuperGroupMap::ConstIterator sg_it(superGroups);
|
197
|
+
|
198
|
+
UPDATE_TRACE_POINT();
|
199
|
+
while (*sg_it != NULL) {
|
200
|
+
const SuperGroupPtr &superGroup = sg_it.getValue();
|
201
|
+
SuperGroup::GroupList::iterator g_it, g_end = superGroup->groups.end();
|
202
|
+
|
203
|
+
for (g_it = superGroup->groups.begin(); g_it != g_end; g_it++) {
|
204
|
+
const GroupPtr &group = *g_it;
|
205
|
+
|
206
|
+
updateProcessMetrics(group->enabledProcesses, processMetrics, processesToDetach);
|
207
|
+
updateProcessMetrics(group->disablingProcesses, processMetrics, processesToDetach);
|
208
|
+
updateProcessMetrics(group->disabledProcesses, processMetrics, processesToDetach);
|
209
|
+
prepareUnionStationProcessStateLogs(logEntries, group);
|
210
|
+
prepareUnionStationSystemMetricsLogs(logEntries, group);
|
211
|
+
}
|
212
|
+
sg_it.next();
|
213
|
+
}
|
214
|
+
|
215
|
+
UPDATE_TRACE_POINT();
|
216
|
+
foreach (const ProcessPtr process, processesToDetach) {
|
217
|
+
detachProcessUnlocked(process, actions);
|
218
|
+
}
|
219
|
+
UPDATE_TRACE_POINT();
|
220
|
+
processesToDetach.clear();
|
221
|
+
|
222
|
+
l.unlock();
|
223
|
+
UPDATE_TRACE_POINT();
|
224
|
+
if (!logEntries.empty()) {
|
225
|
+
const UnionStation::CorePtr &unionStationCore = getUnionStationCore();
|
226
|
+
while (!logEntries.empty()) {
|
227
|
+
UnionStationLogEntry &entry = logEntries.back();
|
228
|
+
UnionStation::TransactionPtr transaction =
|
229
|
+
unionStationCore->newTransaction(
|
230
|
+
entry.groupName,
|
231
|
+
entry.category,
|
232
|
+
entry.key);
|
233
|
+
transaction->message(entry.data);
|
234
|
+
logEntries.pop_back();
|
235
|
+
}
|
236
|
+
}
|
237
|
+
|
238
|
+
UPDATE_TRACE_POINT();
|
239
|
+
runAllActions(actions);
|
240
|
+
UPDATE_TRACE_POINT();
|
241
|
+
// Run destructors with updated trace point.
|
242
|
+
actions.clear();
|
243
|
+
}
|
244
|
+
}
|
245
|
+
|
246
|
+
|
247
|
+
protected:
|
248
|
+
|
249
|
+
void initializeAnalyticsCollection() {
|
250
|
+
interruptableThreads.create_thread(
|
251
|
+
boost::bind(collectAnalytics, shared_from_this()),
|
252
|
+
"Pool analytics collector",
|
253
|
+
POOL_HELPER_THREAD_STACK_SIZE
|
254
|
+
);
|
255
|
+
}
|
@@ -0,0 +1,63 @@
|
|
1
|
+
/*
|
2
|
+
* Phusion Passenger - https://www.phusionpassenger.com/
|
3
|
+
* Copyright (c) 2011-2015 Phusion
|
4
|
+
*
|
5
|
+
* "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
|
6
|
+
*
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
12
|
+
* furnished to do so, subject to the following conditions:
|
13
|
+
*
|
14
|
+
* The above copyright notice and this permission notice shall be included in
|
15
|
+
* all copies or substantial portions of the Software.
|
16
|
+
*
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23
|
+
* THE SOFTWARE.
|
24
|
+
*/
|
25
|
+
|
26
|
+
// This file is included inside the Pool class.
|
27
|
+
|
28
|
+
protected:
|
29
|
+
|
30
|
+
struct DebugSupport {
|
31
|
+
/** Mailbox for the unit tests to receive messages on. */
|
32
|
+
MessageBoxPtr debugger;
|
33
|
+
/** Mailbox for the ApplicationPool code to receive messages on. */
|
34
|
+
MessageBoxPtr messages;
|
35
|
+
|
36
|
+
// Choose aspects to debug.
|
37
|
+
bool restarting;
|
38
|
+
bool spawning;
|
39
|
+
bool superGroup;
|
40
|
+
bool oobw;
|
41
|
+
bool testOverflowRequestQueue;
|
42
|
+
bool detachedProcessesChecker;
|
43
|
+
|
44
|
+
// The following fields may only be accessed by Pool.
|
45
|
+
boost::mutex syncher;
|
46
|
+
unsigned int spawnLoopIteration;
|
47
|
+
|
48
|
+
DebugSupport() {
|
49
|
+
debugger = boost::make_shared<MessageBox>();
|
50
|
+
messages = boost::make_shared<MessageBox>();
|
51
|
+
restarting = true;
|
52
|
+
spawning = true;
|
53
|
+
superGroup = false;
|
54
|
+
oobw = false;
|
55
|
+
detachedProcessesChecker = false;
|
56
|
+
testOverflowRequestQueue = false;
|
57
|
+
spawnLoopIteration = 0;
|
58
|
+
}
|
59
|
+
};
|
60
|
+
|
61
|
+
typedef boost::shared_ptr<DebugSupport> DebugSupportPtr;
|
62
|
+
|
63
|
+
DebugSupportPtr debugSupport;
|
@@ -0,0 +1,197 @@
|
|
1
|
+
/*
|
2
|
+
* Phusion Passenger - https://www.phusionpassenger.com/
|
3
|
+
* Copyright (c) 2011-2015 Phusion
|
4
|
+
*
|
5
|
+
* "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
|
6
|
+
*
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
12
|
+
* furnished to do so, subject to the following conditions:
|
13
|
+
*
|
14
|
+
* The above copyright notice and this permission notice shall be included in
|
15
|
+
* all copies or substantial portions of the Software.
|
16
|
+
*
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23
|
+
* THE SOFTWARE.
|
24
|
+
*/
|
25
|
+
|
26
|
+
// This file is included inside the Pool class.
|
27
|
+
|
28
|
+
private:
|
29
|
+
|
30
|
+
struct GarbageCollectorState {
|
31
|
+
unsigned long long now;
|
32
|
+
unsigned long long nextGcRunTime;
|
33
|
+
boost::container::vector<Callback> actions;
|
34
|
+
};
|
35
|
+
|
36
|
+
boost::condition_variable garbageCollectionCond;
|
37
|
+
|
38
|
+
|
39
|
+
static void garbageCollect(PoolPtr self) {
|
40
|
+
TRACE_POINT();
|
41
|
+
{
|
42
|
+
ScopedLock lock(self->syncher);
|
43
|
+
self->garbageCollectionCond.timed_wait(lock,
|
44
|
+
posix_time::seconds(5));
|
45
|
+
}
|
46
|
+
while (!this_thread::interruption_requested()) {
|
47
|
+
try {
|
48
|
+
UPDATE_TRACE_POINT();
|
49
|
+
unsigned long long sleepTime = self->realGarbageCollect();
|
50
|
+
UPDATE_TRACE_POINT();
|
51
|
+
ScopedLock lock(self->syncher);
|
52
|
+
self->garbageCollectionCond.timed_wait(lock,
|
53
|
+
posix_time::microseconds(sleepTime));
|
54
|
+
} catch (const thread_interrupted &) {
|
55
|
+
break;
|
56
|
+
} catch (const tracable_exception &e) {
|
57
|
+
P_WARN("ERROR: " << e.what() << "\n Backtrace:\n" << e.backtrace());
|
58
|
+
}
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
void maybeUpdateNextGcRuntime(GarbageCollectorState &state, unsigned long candidate) {
|
63
|
+
if (state.nextGcRunTime == 0 || candidate < state.nextGcRunTime) {
|
64
|
+
state.nextGcRunTime = candidate;
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
void checkWhetherProcessCanBeGarbageCollected(GarbageCollectorState &state,
|
69
|
+
const GroupPtr &group, const ProcessPtr &process, ProcessList &output)
|
70
|
+
{
|
71
|
+
assert(maxIdleTime > 0);
|
72
|
+
unsigned long long processGcTime = process->lastUsed + maxIdleTime;
|
73
|
+
if (process->sessions == 0
|
74
|
+
&& state.now >= processGcTime
|
75
|
+
&& (unsigned long) group->getProcessCount() > group->options.minProcesses)
|
76
|
+
{
|
77
|
+
if (output.capacity() == 0) {
|
78
|
+
output.reserve(group->enabledCount);
|
79
|
+
}
|
80
|
+
output.push_back(process);
|
81
|
+
} else {
|
82
|
+
maybeUpdateNextGcRuntime(state, processGcTime);
|
83
|
+
}
|
84
|
+
}
|
85
|
+
|
86
|
+
void garbageCollectProcessesInGroup(GarbageCollectorState &state,
|
87
|
+
const GroupPtr &group)
|
88
|
+
{
|
89
|
+
ProcessList &processes = group->enabledProcesses;
|
90
|
+
ProcessList processesToGc;
|
91
|
+
ProcessList::iterator p_it, p_end = processes.end();
|
92
|
+
|
93
|
+
for (p_it = processes.begin(); p_it != p_end; p_it++) {
|
94
|
+
const ProcessPtr &process = *p_it;
|
95
|
+
checkWhetherProcessCanBeGarbageCollected(state, group, process,
|
96
|
+
processesToGc);
|
97
|
+
}
|
98
|
+
|
99
|
+
p_end = processesToGc.end();
|
100
|
+
for (p_it = processesToGc.begin(); p_it != p_end; p_it++) {
|
101
|
+
ProcessPtr process = *p_it;
|
102
|
+
P_DEBUG("Garbage collect idle process: " << process->inspect() <<
|
103
|
+
", group=" << group->name);
|
104
|
+
group->detach(process, state.actions);
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
void maybeCleanPreloader(GarbageCollectorState &state, const GroupPtr &group) {
|
109
|
+
if (group->spawner->cleanable() && group->options.getMaxPreloaderIdleTime() != 0) {
|
110
|
+
unsigned long long spawnerGcTime =
|
111
|
+
group->spawner->lastUsed() +
|
112
|
+
group->options.getMaxPreloaderIdleTime() * 1000000;
|
113
|
+
if (state.now >= spawnerGcTime) {
|
114
|
+
P_DEBUG("Garbage collect idle spawner: group=" << group->name);
|
115
|
+
group->cleanupSpawner(state.actions);
|
116
|
+
} else {
|
117
|
+
maybeUpdateNextGcRuntime(state, spawnerGcTime);
|
118
|
+
}
|
119
|
+
}
|
120
|
+
}
|
121
|
+
|
122
|
+
unsigned long long
|
123
|
+
realGarbageCollect() {
|
124
|
+
TRACE_POINT();
|
125
|
+
ScopedLock lock(syncher);
|
126
|
+
SuperGroupMap::ConstIterator sg_it(superGroups);
|
127
|
+
GarbageCollectorState state;
|
128
|
+
state.now = SystemTime::getUsec();
|
129
|
+
state.nextGcRunTime = 0;
|
130
|
+
|
131
|
+
P_DEBUG("Garbage collection time...");
|
132
|
+
verifyInvariants();
|
133
|
+
|
134
|
+
// For all supergroups and groups...
|
135
|
+
while (*sg_it != NULL) {
|
136
|
+
const SuperGroupPtr superGroup = sg_it.getValue();
|
137
|
+
SuperGroup::GroupList &groups = superGroup->groups;
|
138
|
+
SuperGroup::GroupList::iterator g_it, g_end = groups.end();
|
139
|
+
|
140
|
+
superGroup->verifyInvariants();
|
141
|
+
|
142
|
+
for (g_it = groups.begin(); g_it != g_end; g_it++) {
|
143
|
+
GroupPtr group = *g_it;
|
144
|
+
|
145
|
+
if (maxIdleTime > 0) {
|
146
|
+
// ...detach processes that have been idle for more than maxIdleTime.
|
147
|
+
garbageCollectProcessesInGroup(state, group);
|
148
|
+
}
|
149
|
+
|
150
|
+
group->verifyInvariants();
|
151
|
+
|
152
|
+
// ...cleanup the spawner if it's been idle for more than preloaderIdleTime.
|
153
|
+
maybeCleanPreloader(state, group);
|
154
|
+
}
|
155
|
+
|
156
|
+
superGroup->verifyInvariants();
|
157
|
+
sg_it.next();
|
158
|
+
}
|
159
|
+
|
160
|
+
verifyInvariants();
|
161
|
+
lock.unlock();
|
162
|
+
|
163
|
+
// Schedule next garbage collection run.
|
164
|
+
unsigned long long sleepTime;
|
165
|
+
if (state.nextGcRunTime == 0 || state.nextGcRunTime <= state.now) {
|
166
|
+
if (maxIdleTime == 0) {
|
167
|
+
sleepTime = 10 * 60 * 1000000;
|
168
|
+
} else {
|
169
|
+
sleepTime = maxIdleTime;
|
170
|
+
}
|
171
|
+
} else {
|
172
|
+
sleepTime = state.nextGcRunTime - state.now;
|
173
|
+
}
|
174
|
+
P_DEBUG("Garbage collection done; next garbage collect in " <<
|
175
|
+
std::fixed << std::setprecision(3) << (sleepTime / 1000000.0) << " sec");
|
176
|
+
|
177
|
+
UPDATE_TRACE_POINT();
|
178
|
+
runAllActions(state.actions);
|
179
|
+
UPDATE_TRACE_POINT();
|
180
|
+
state.actions.clear();
|
181
|
+
return sleepTime;
|
182
|
+
}
|
183
|
+
|
184
|
+
|
185
|
+
protected:
|
186
|
+
|
187
|
+
void initializeGarbageCollection() {
|
188
|
+
interruptableThreads.create_thread(
|
189
|
+
boost::bind(garbageCollect, shared_from_this()),
|
190
|
+
"Pool garbage collector",
|
191
|
+
POOL_HELPER_THREAD_STACK_SIZE
|
192
|
+
);
|
193
|
+
}
|
194
|
+
|
195
|
+
void wakeupGarbageCollector() {
|
196
|
+
garbageCollectionCond.notify_all();
|
197
|
+
}
|