/
scheduling_loop.hpp
407 lines (350 loc) · 15.2 KB
/
scheduling_loop.hpp
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright (c) 2007-2014 Hartmut Kaiser
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#if !defined(HPX_RUNTIME_THREADS_DETAIL_SCHEDULING_LOOP_JAN_11_2013_0838PM)
#define HPX_RUNTIME_THREADS_DETAIL_SCHEDULING_LOOP_JAN_11_2013_0838PM
#include <hpx/hpx_fwd.hpp>
#include <hpx/state.hpp>
#include <hpx/runtime/threads/thread_data.hpp>
#include <hpx/runtime/threads/detail/periodic_maintenance.hpp>
#include <hpx/runtime/agas/interface.hpp>
#include <hpx/util/itt_notify.hpp>
#include <hpx/util/hardware/timestamp.hpp>
#include <boost/cstdint.hpp>
namespace hpx { namespace threads { namespace detail
{
///////////////////////////////////////////////////////////////////////
inline void write_new_state_log_debug(std::size_t num_thread,
thread_data_base* thrd, thread_state_enum state, char const* info)
{
LTM_(debug) << "tfunc(" << num_thread << "): " //-V128
<< "thread(" << thrd->get_thread_id().get() << "), "
<< "description(" << thrd->get_description() << "), "
<< "new state(" << get_thread_state_name(state) << "), "
<< info;
}
inline void write_new_state_log_warning(std::size_t num_thread,
thread_data_base* thrd, thread_state_enum state, char const* info)
{
// log this in any case
LTM_(warning) << "tfunc(" << num_thread << "): " //-V128
<< "thread(" << thrd->get_thread_id().get() << "), "
<< "description(" << thrd->get_description() << "), "
<< "new state(" << get_thread_state_name(state) << "), "
<< info;
}
inline void write_old_state_log(std::size_t num_thread,
thread_data_base* thrd, thread_state_enum state)
{
LTM_(debug) << "tfunc(" << num_thread << "): " //-V128
<< "thread(" << thrd->get_thread_id().get() << "), "
<< "description(" << thrd->get_description() << "), "
<< "old state(" << get_thread_state_name(state) << ")";
}
///////////////////////////////////////////////////////////////////////
// helper class for switching thread state in and out during execution
class switch_status
{
public:
switch_status (thread_data_base* t, thread_state prev_state)
: thread_(t), prev_state_(prev_state),
need_restore_state_(t->set_state_tagged(active, prev_state_, orig_state_))
{}
~switch_status ()
{
if (need_restore_state_)
store_state(prev_state_);
}
bool is_valid() const { return need_restore_state_; }
// allow to change the state the thread will be switched to after
// execution
thread_state operator=(thread_state_enum new_state)
{
return prev_state_ = thread_state(new_state, prev_state_.get_tag() + 1);
}
// Get the state this thread was in before execution (usually pending),
// this helps making sure no other worker-thread is started to execute this
// HPX-thread in the meantime.
thread_state get_previous() const
{
return prev_state_;
}
// This restores the previous state, while making sure that the
// original state has not been changed since we started executing this
// thread. The function returns true if the state has been set, false
// otherwise.
bool store_state(thread_state& newstate)
{
disable_restore();
if (thread_->restore_state(prev_state_, orig_state_)) {
newstate = prev_state_;
return true;
}
return false;
}
// disable default handling in destructor
void disable_restore() { need_restore_state_ = false; }
private:
thread_data_base* thread_;
thread_state prev_state_;
thread_state orig_state_;
bool need_restore_state_;
};
#ifdef HPX_HAVE_THREAD_IDLE_RATES
struct idle_collect_rate
{
idle_collect_rate(boost::uint64_t& tfunc_time, boost::uint64_t& exec_time)
: start_timestamp_(util::hardware::timestamp())
, tfunc_time_(tfunc_time)
, exec_time_(exec_time)
{}
void collect_exec_time(boost::uint64_t timestamp)
{
exec_time_ += util::hardware::timestamp() - timestamp;
}
void take_snapshot()
{
if (tfunc_time_ == boost::uint64_t(-1))
{
start_timestamp_ = util::hardware::timestamp();
tfunc_time_ = 0;
exec_time_ = 0;
}
else
{
tfunc_time_ = util::hardware::timestamp() - start_timestamp_;
}
}
boost::uint64_t start_timestamp_;
boost::uint64_t& tfunc_time_;
boost::uint64_t& exec_time_;
};
struct exec_time_wrapper
{
exec_time_wrapper(idle_collect_rate& idle_rate)
: timestamp_(util::hardware::timestamp())
, idle_rate_(idle_rate)
{}
~exec_time_wrapper()
{
idle_rate_.collect_exec_time(timestamp_);
}
boost::uint64_t timestamp_;
idle_collect_rate& idle_rate_;
};
struct tfunc_time_wrapper
{
tfunc_time_wrapper(idle_collect_rate& idle_rate)
: idle_rate_(idle_rate)
{
}
~tfunc_time_wrapper()
{
idle_rate_.take_snapshot();
}
idle_collect_rate& idle_rate_;
};
#else
struct idle_collect_rate
{
idle_collect_rate(boost::uint64_t&, boost::uint64_t&) {}
};
struct exec_time_wrapper
{
exec_time_wrapper(idle_collect_rate&) {}
};
struct tfunc_time_wrapper
{
tfunc_time_wrapper(idle_collect_rate&) {}
};
#endif
///////////////////////////////////////////////////////////////////////////
template <typename SchedulingPolicy>
void scheduling_loop(std::size_t num_thread, SchedulingPolicy& scheduler,
boost::atomic<hpx::state>& global_state, boost::int64_t& executed_threads,
boost::int64_t& executed_thread_phases, boost::uint64_t& tfunc_time,
boost::uint64_t& exec_time,
util::function_nonser<void()> const& cb_outer = util::function_nonser<void()>(),
util::function_nonser<void()> const& cb_inner = util::function_nonser<void()>())
{
util::itt::stack_context ctx; // helper for itt support
util::itt::domain domain(get_thread_name().data());
// util::itt::id threadid(domain, this);
util::itt::frame_context fctx(domain);
boost::int64_t idle_loop_count = 0;
boost::int64_t busy_loop_count = 0;
idle_collect_rate idle_rate(tfunc_time, exec_time);
tfunc_time_wrapper tfunc_time_collector(idle_rate);
scheduler.SchedulingPolicy::start_periodic_maintenance(global_state);
// spin for some time after queues have become empty
bool may_exit = false;
while (true) {
// Get the next HPX thread from the queue
thread_data_base* thrd = NULL;
if (scheduler.SchedulingPolicy::get_next_thread(
num_thread, idle_loop_count, thrd))
{
tfunc_time_wrapper tfunc_time_collector(idle_rate);
idle_loop_count = 0;
++busy_loop_count;
may_exit = false;
// Only pending HPX threads will be executed.
// Any non-pending HPX threads are leftovers from a set_state()
// call for a previously pending HPX thread (see comments above).
thread_state state = thrd->get_state();
thread_state_enum state_val = state;
detail::write_old_state_log(num_thread, thrd, state_val);
if (pending == state_val) {
// switch the state of the thread to active and back to
// what the thread reports as its return value
{
// tries to set state to active (only if state is still
// the same as 'state')
detail::switch_status thrd_stat (thrd, state);
if (thrd_stat.is_valid() && thrd_stat.get_previous() == pending)
{
tfunc_time_wrapper tfunc_time_collector(idle_rate);
// thread returns new required state
// store the returned state in the thread
{
#ifdef HPX_HAVE_ITTNOTIFY
util::itt::caller_context cctx(ctx);
util::itt::undo_frame_context undoframe(fctx);
util::itt::task task(domain, thrd->get_description());
#endif
// Record time elapsed in thread changing state
// and add to aggregate execution time.
exec_time_wrapper exec_time_collector(idle_rate);
thrd_stat = (*thrd)();
}
#ifdef HPX_HAVE_THREAD_CUMULATIVE_COUNTS
++executed_thread_phases;
#endif
}
else {
// some other worker-thread got in between and started
// executing this HPX-thread, we just continue with
// the next one
thrd_stat.disable_restore();
detail::write_new_state_log_warning(
num_thread, thrd, state_val, "no execution");
continue;
}
// store and retrieve the new state in the thread
if (!thrd_stat.store_state(state)) {
// some other worker-thread got in between and changed
// the state of this thread, we just continue with
// the next one
detail::write_new_state_log_warning(
num_thread, thrd, state_val, "no state change");
continue;
}
state_val = state;
// any exception thrown from the thread will reset its
// state at this point
}
//detail::write_new_state_log_debug(num_thread, thrd,
// state_val, "normal");
// Re-add this work item to our list of work items if the HPX
// thread should be re-scheduled. If the HPX thread is suspended
// now we just keep it in the map of threads.
if (state_val == pending) {
// schedule other work
scheduler.SchedulingPolicy::wait_or_add_new(num_thread,
is_running_state(global_state.load()), idle_loop_count);
// schedule this thread again, make sure it ends up at
// the end of the queue
scheduler.SchedulingPolicy::schedule_thread_last(thrd, num_thread);
scheduler.SchedulingPolicy::do_some_work(num_thread);
}
}
else if (active == state_val) {
LTM_(warning) << "tfunc(" << num_thread << "): " //-V128
"thread(" << thrd->get_thread_id().get() << "), "
"description(" << thrd->get_description() << "), "
"rescheduling";
// re-schedule thread, if it is still marked as active
// this might happen, if some thread has been added to the
// scheduler queue already but the state has not been reset
// yet
//
// REVIEW: Passing a specific target thread may set off
// the round robin queuing.
scheduler.SchedulingPolicy::schedule_thread(thrd, num_thread);
}
// Remove the mapping from thread_map_ if HPX thread is depleted
// or terminated, this will delete the HPX thread as all
// references go out of scope.
// REVIEW: what has to be done with depleted HPX threads?
if (state_val == depleted || state_val == terminated)
{
#ifdef HPX_HAVE_THREAD_CUMULATIVE_COUNTS
++executed_threads;
#endif
scheduler.SchedulingPolicy::destroy_thread(thrd, busy_loop_count);
}
}
// if nothing else has to be done either wait or terminate
else {
++idle_loop_count;
if (scheduler.SchedulingPolicy::wait_or_add_new(num_thread,
is_running_state(global_state.load()), idle_loop_count))
{
// clean up terminated threads one more time before existing
if (scheduler.SchedulingPolicy::cleanup_terminated(true))
{
// keep idling for some time
if (!may_exit)
idle_loop_count = 0;
may_exit = true;
}
}
// do background work in parcel layer and in agas
if (hpx::parcelset::do_background_work(num_thread))
idle_loop_count = 0;
if (0 == num_thread)
{
hpx::agas::garbage_collect_non_blocking();
}
// call back into invoking context
if (!cb_inner.empty())
cb_inner();
}
// something went badly wrong, give up
if (global_state == state_terminating)
break;
if (busy_loop_count > HPX_BUSY_LOOP_COUNT_MAX)
{
busy_loop_count = 0;
// do background work in parcel layer and in agas
if (hpx::parcelset::do_background_work(num_thread))
idle_loop_count = 0;
if (0 == num_thread)
{
hpx::agas::garbage_collect_non_blocking();
}
}
else if (idle_loop_count > HPX_IDLE_LOOP_COUNT_MAX)
{
// call back into invoking context
if (!cb_outer.empty())
cb_outer();
// clean up terminated threads
idle_loop_count = 0;
// break if we were idling after 'may_exit'
if (may_exit)
{
if (scheduler.SchedulingPolicy::cleanup_terminated(true))
break;
may_exit = false;
}
else
{
scheduler.SchedulingPolicy::cleanup_terminated(true);
}
}
}
}
}}}
#endif