1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include "include/types.h"
#include <sstream>
#include "include/utime.h"
#include "common/config.h"
#include "common/HeartbeatMap.h"
#include "WorkQueue.h"
#define dout_subsys ceph_subsys_tp
#undef dout_prefix
#define dout_prefix *_dout << name << " "
ThreadPool::ThreadPool(CephContext *cct_, string nm, int n, const char *option)
: cct(cct_), name(nm),
lockname(nm + "::lock"),
_lock(lockname.c_str()), // this should be safe due to declaration order
_stop(false),
_pause(0),
_draining(0),
_num_threads(n),
last_work_queue(0),
processing(0)
{
if (option) {
_thread_num_option = option;
// set up conf_keys
_conf_keys = new const char*[2];
_conf_keys[0] = _thread_num_option.c_str();
_conf_keys[1] = NULL;
} else {
_conf_keys = new const char*[1];
_conf_keys[0] = NULL;
}
}
ThreadPool::~ThreadPool()
{
assert(_threads.empty());
delete[] _conf_keys;
}
void ThreadPool::handle_conf_change(const struct md_config_t *conf,
const std::set <std::string> &changed)
{
if (changed.count(_thread_num_option)) {
char *buf;
int r = conf->get_val(_thread_num_option.c_str(), &buf, -1);
assert(r >= 0);
int v = atoi(buf);
free(buf);
if (v > 0) {
_lock.Lock();
_num_threads = v;
start_threads();
_cond.SignalAll();
_lock.Unlock();
}
}
}
void ThreadPool::worker(WorkThread *wt)
{
_lock.Lock();
ldout(cct,10) << "worker start" << dendl;
std::stringstream ss;
ss << name << " thread " << (void*)pthread_self();
heartbeat_handle_d *hb = cct->get_heartbeat_map()->add_worker(ss.str());
while (!_stop) {
// manage dynamic thread pool
join_old_threads();
if (_threads.size() > _num_threads) {
ldout(cct,1) << " worker shutting down; too many threads (" << _threads.size() << " > " << _num_threads << ")" << dendl;
_threads.erase(wt);
_old_threads.push_back(wt);
break;
}
if (!_pause && work_queues.size()) {
WorkQueue_* wq;
int tries = work_queues.size();
bool did = false;
while (tries--) {
last_work_queue++;
last_work_queue %= work_queues.size();
wq = work_queues[last_work_queue];
void *item = wq->_void_dequeue();
if (item) {
processing++;
ldout(cct,12) << "worker wq " << wq->name << " start processing " << item
<< " (" << processing << " active)" << dendl;
cct->get_heartbeat_map()->reset_timeout(hb, wq->timeout_interval, wq->suicide_interval);
_lock.Unlock();
wq->_void_process(item);
_lock.Lock();
wq->_void_process_finish(item);
processing--;
ldout(cct,15) << "worker wq " << wq->name << " done processing " << item
<< " (" << processing << " active)" << dendl;
if (_pause || _draining)
_wait_cond.Signal();
did = true;
break;
}
}
if (did)
continue;
}
ldout(cct,20) << "worker waiting" << dendl;
cct->get_heartbeat_map()->reset_timeout(hb, 4, 0);
_cond.WaitInterval(cct, _lock, utime_t(2, 0));
}
ldout(cct,1) << "worker finish" << dendl;
cct->get_heartbeat_map()->remove_worker(hb);
_lock.Unlock();
}
void ThreadPool::start_threads()
{
assert(_lock.is_locked());
while (_threads.size() < _num_threads) {
WorkThread *wt = new WorkThread(this);
ldout(cct, 10) << "start_threads creating and starting " << wt << dendl;
_threads.insert(wt);
wt->create();
}
}
void ThreadPool::join_old_threads()
{
assert(_lock.is_locked());
while (!_old_threads.empty()) {
ldout(cct, 10) << "join_old_threads joining and deleting " << _old_threads.front() << dendl;
_old_threads.front()->join();
delete _old_threads.front();
_old_threads.pop_front();
}
}
void ThreadPool::start()
{
ldout(cct,10) << "start" << dendl;
if (_thread_num_option.length()) {
ldout(cct, 10) << " registering config observer on " << _thread_num_option << dendl;
cct->_conf->add_observer(this);
}
_lock.Lock();
start_threads();
_lock.Unlock();
ldout(cct,15) << "started" << dendl;
}
void ThreadPool::stop(bool clear_after)
{
ldout(cct,10) << "stop" << dendl;
if (_thread_num_option.length()) {
ldout(cct, 10) << " unregistering config observer on " << _thread_num_option << dendl;
cct->_conf->remove_observer(this);
}
_lock.Lock();
_stop = true;
_cond.Signal();
join_old_threads();
_lock.Unlock();
for (set<WorkThread*>::iterator p = _threads.begin();
p != _threads.end();
p++) {
(*p)->join();
delete *p;
}
_threads.clear();
_lock.Lock();
for (unsigned i=0; i<work_queues.size(); i++)
work_queues[i]->_clear();
_lock.Unlock();
ldout(cct,15) << "stopped" << dendl;
}
void ThreadPool::pause()
{
ldout(cct,10) << "pause" << dendl;
_lock.Lock();
_pause++;
while (processing)
_wait_cond.Wait(_lock);
_lock.Unlock();
ldout(cct,15) << "paused" << dendl;
}
void ThreadPool::pause_new()
{
ldout(cct,10) << "pause_new" << dendl;
_lock.Lock();
_pause++;
_lock.Unlock();
}
void ThreadPool::unpause()
{
ldout(cct,10) << "unpause" << dendl;
_lock.Lock();
assert(_pause > 0);
_pause--;
_cond.Signal();
_lock.Unlock();
}
void ThreadPool::drain(WorkQueue_* wq)
{
ldout(cct,10) << "drain" << dendl;
_lock.Lock();
_draining++;
while (processing || (wq != NULL && !wq->_empty()))
_wait_cond.Wait(_lock);
_draining--;
_lock.Unlock();
}
|