aboutsummaryrefslogtreecommitdiff
path: root/build2/scheduler.cxx
blob: 4619e854d6b3e2746a136f76477983b29177686f (plain)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// file      : build2/scheduler.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <build2/scheduler>

#include <cerrno>

using namespace std;

namespace build2
{
  void scheduler::
  wait (atomic_count& task_count)
  {
    if (task_count == 0)
      return;

    // See if we can run some of our own tasks.
    //
    task_queue& tq (*task_queue_); // Must have been set by async() or task
                                   // would have been 0.

    for (lock ql (tq.mutex); !tq.shutdown && !empty_back (tq); )
      pop_back (tq, ql);

    // Note that empty task queue doesn't automatically mean the task count
    // is zero (some might still be executing asynchronously).
    //
    if (task_count == 0)
      return;

    suspend (task_count);
  }

  void scheduler::
  suspend (atomic_count& tc)
  {
    wait_slot& s (
      wait_queue_[std::hash<atomic_count*> () (&tc) % wait_queue_size_]);

    // This thread is no longer active.
    //
    {
      lock l (mutex_);
      active_--;
      waiting_++;

      if (waiting_ > stat_max_waiters_)
        stat_max_waiters_ = waiting_;

      // A spare active thread has become available. If there are ready
      // masters or eager helpers, wake someone up.
      //
      if (ready_ != 0)
        ready_condv_.notify_one ();
      else if (task_)
        activate_helper (l);
    }

    // Note that the task count is checked while holding the lock. We also
    // have to notify while holding the lock (see resume()). The aim here
    // is not to end up with a notification that happens between the check
    // and the wait.
    //
    bool collision;
    {
      lock l (s.mutex);
      collision = (s.waiters++ != 0);

      // Since we use a mutex for synchronization, we can relax the atomic
      // access.
      //
      while (!s.shutdown && tc.load (std::memory_order_relaxed) != 0)
        s.condv.wait (l);

      s.waiters--;
    }

    // This thread is no longer waiting.
    //
    {
      lock l (mutex_);
      waiting_--;

      if (collision)
        stat_wait_collisions_++;

      // If we have spare active threads, then become active. Otherwise it
      // enters the ready queue.
      //
      ready_++;

      while (!shutdown_ && active_ >= max_active_)
        ready_condv_.wait (l);

      ready_--;

      if (shutdown_)
        throw system_error (ECANCELED, system_category ());

      active_++;
    }
  }

  void scheduler::
  resume (atomic_count& tc)
  {
    wait_slot& s (
      wait_queue_[std::hash<atomic_count*> () (&tc) % wait_queue_size_]);

    // See suspend() for why we must hold the lock.
    //
    lock l (s.mutex);

    if (s.waiters != 0)
      s.condv.notify_all ();
  }

  scheduler::
  ~scheduler ()
  {
    try { shutdown (); } catch (system_error&) {}
  }

  void scheduler::
  startup (size_t max_active,
           size_t init_active,
           size_t max_threads,
           size_t queue_depth)
  {
    // Lock the mutex to make sure our changes are visible in (other) active
    // threads.
    //
    lock l (mutex_);

    // Use 4x max_active on 32-bit and 8x max_active on 64-bit. Unless we were
    // asked to run serially.
    //
    if (max_threads == 0)
      max_threads = max_active * (max_active == 1 ? 1 : sizeof (void*));

    assert (shutdown_ &&
            init_active != 0 &&
            init_active <= max_active &&
            max_active <= max_threads);

    active_ = init_active_ = init_active;
    max_active_ = max_active;
    max_threads_ = max_threads;

    // This value should be proportional to the amount of hardware concurrency
    // we have (no use queing things if helpers cannot keep up). Note that the
    // queue entry is quite sizable.
    //
    task_queue_depth_ = queue_depth != 0
      ? queue_depth
      : max_active * sizeof (void*) * 2;

    task_queues_.reserve (max_threads_);

    // Pick a nice prime for common max_threads numbers. Experience shows that
    // we want something close to 2x for small numbers, then reduce to 1.5x
    // in-between, and 1x for large ones.
    //
    // Note that Intel Xeons are all over the map when it comes to cores (6,
    // 8, 10, 12, 14, 16, 18, 20, 22).
    //
    wait_queue_size_ =            // HW threads x bits
      //
      // 2x
      //
      max_threads ==   8 ?   17 : // 2 x 4
      max_threads ==  16 ?   31 : // 4 x 4, 2 x 8
      //
      // 1.5x
      //
      max_threads ==  32 ?   47 : // 4 x 8
      max_threads ==  48 ?   53 : // 6 x 8
      max_threads ==  64 ?   67 : // 8 x 8
      max_threads ==  80 ?   89 : // 10 x 8
      //
      // 1x
      //
      max_threads ==  96 ?  101 : // 12 x 8
      max_threads == 112 ?  127 : // 14 x 8
      max_threads == 128 ?  131 : // 16 x 8
      max_threads == 144 ?  139 : // 18 x 8
      max_threads == 160 ?  157 : // 20 x 8
      max_threads == 176 ?  173 : // 22 x 8
      max_threads == 192 ?  191 : // 24 x 8
      max_threads == 224 ?  223 : // 28 x 8
      max_threads == 256 ?  251 : // 32 x 8
      max_threads == 288 ?  271 : // 36 x 8
      max_threads == 320 ?  313 : // 40 x 8
      max_threads == 352 ?  331 : // 44 x 8
      max_threads == 384 ?  367 : // 48 x 8
      max_threads == 512 ?  499 : // 64 x 8
      max_threads - 1;            // Assume max_threads is even.

    wait_queue_.reset (new wait_slot[wait_queue_size_]);

    // Reset stats counters.
    //
    stat_max_waiters_     = 0;
    stat_wait_collisions_ = 0;

    task_ = false;
    shutdown_ = false;

    for (size_t i (0); i != wait_queue_size_; ++i)
      wait_queue_[i].shutdown = false;
  }

  auto scheduler::
  shutdown () -> stat
  {
    // Our overall approach to shutdown is not to try and stop everything as
    // quickly as possible but rather to avoid performing any tasks. This
    // avoids having code littered with if(shutdown) on every second line.

    stat r;
    lock l (mutex_);

    if (!shutdown_)
    {
      // Signal shutdown and collect statistics.
      //
      shutdown_ = true;

      for (size_t i (0); i != wait_queue_size_; ++i)
      {
        wait_slot& ws (wait_queue_[i]);
        lock l (ws.mutex);
        ws.shutdown = true;
      }

      for (unique_ptr<task_queue>& tq: task_queues_)
      {
        lock l (tq->mutex);
        r.task_queue_full += tq->stat_full;
        tq->shutdown = true;
      }

      // Wait for all the helpers to terminate waking up any thread that
      // sleeps.
      //
      r.thread_helpers = helpers_;

      while (helpers_ != 0)
      {
        bool i (idle_ != 0);
        bool r (ready_ != 0);
        bool w (waiting_ != 0);

        l.unlock ();

        if (i)
          idle_condv_.notify_all ();

        if (r)
          ready_condv_.notify_all ();

        if (w)
          for (size_t i (0); i != wait_queue_size_; ++i)
            wait_queue_[i].condv.notify_all ();

        this_thread::yield ();
        l.lock ();
      }

      // Free the memory.
      //
      wait_queue_.reset ();
      task_queues_.clear ();

      r.thread_max_active     = max_active_;
      r.thread_max_total      = max_threads_;
      r.thread_max_waiting    = stat_max_waiters_;

      r.task_queue_depth      = task_queue_depth_;

      r.wait_queue_slots      = wait_queue_size_;
      r.wait_queue_collisions = stat_wait_collisions_;
    }

    return r;
  }

  void scheduler::
  activate_helper (lock& l)
  {
    if (!shutdown_)
    {
      if (idle_ != 0)
        idle_condv_.notify_one ();
      else if (init_active_ + helpers_ < max_threads_)
        create_helper (l);
    }
  }

  void scheduler::
  create_helper (lock& l)
  {
    helpers_++;
    starting_++;
    l.unlock ();

    // Restore the counters if the thread creation fails.
    //
    struct guard
    {
      lock* l;
      size_t& h;
      size_t& s;

      ~guard () {if (l != nullptr) {l->lock (); h--; s--;}}

    } g {&l, helpers_, starting_};

    thread t (helper, this);
    g.l = nullptr; // Disarm.

    t.detach ();
  }

  void scheduler::
  helper (void* d)
  {
    scheduler& s (*static_cast<scheduler*> (d));

    // Note that this thread can be in an in-between state (not active or
    // idle) but only while holding the lock. Which means that if we have the
    // lock then we can account for all of them (this is important during
    // shutdown). Except when the thread is just starting, before acquiring
    // the lock for the first time, which we handle with the starting count.
    //
    lock l (s.mutex_);
    s.starting_--;

    while (!s.shutdown_)
    {
      // If there is a spare active thread, become active and go looking for
      // some work.
      //
      if (s.active_ < s.max_active_)
      {
        s.active_++;

        while (s.task_) // There might be a task.
        {
          s.task_ = false; // We will process all that are currently there.

          // Queues are never removed and there shouldn't be any reallocations
          // since we reserve maximum possible size upfront. Which means we
          // can get the current number of queues and release the main lock
          // while examining each of them.
          //
          size_t n (s.task_queues_.size ());
          l.unlock ();

          for (size_t i (0); i != n; ++i)
          {
            task_queue& tq (*s.task_queues_[i]);

            for (lock ql (tq.mutex); !tq.shutdown && !s.empty_front (tq); )
              s.pop_front (tq, ql);
          }

          l.lock ();
          // If task_ became true, then there might be new tasks.
        }

        s.active_--;

        // While executing the tasks a thread might have become ready.
        //
        if (s.ready_ != 0)
          s.ready_condv_.notify_one ();
      }

      // Become idle and wait for a notification (note that task_ is false
      // here).
      //
      s.idle_++;
      s.idle_condv_.wait (l);
      s.idle_--;
    }

    s.helpers_--;
  }

#if defined(__apple_build_version__) && __apple_build_version__ < 8000000
  __thread
#else
  thread_local
#endif
  scheduler::task_queue* scheduler::task_queue_ = nullptr;

  auto scheduler::
  create_queue () -> task_queue&
  {
    // Note that task_queue_depth is immutable between startup() and
    // shutdown() (but see join()).
    //
    unique_ptr<task_queue> tqp (new task_queue (task_queue_depth_));
    task_queue& tq (*tqp);

    {
      lock l (mutex_);
      tq.shutdown = shutdown_;
      task_queues_.push_back (move (tqp));
    }

    task_queue_ = &tq;
    return tq;
  }

  scheduler sched;
}