aboutsummaryrefslogtreecommitdiff
path: root/unit-tests/scheduler/driver.cxx
blob: 726415a5657e22382a960ddf15fd9dd93907fc79 (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
// file      : unit-tests/scheduler/driver.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <chrono>
#include <thread>

#include <cassert>
#include <iostream>

#include <build2/types>
#include <build2/utility>

#include <build2/scheduler>

using namespace std;

namespace build2
{
  // Usage argv[0] <max-active-threads>
  //
  int
  main (int argc, char* argv[])
  {
    bool verb (false);
    size_t max_active (0);

    if (argc > 1)
    {
      verb = true;
      max_active = stoul (argv[1]);
    }

    if (max_active == 0)
      max_active = scheduler::hardware_concurrency ();

    scheduler s (max_active);

    auto inner = [] (size_t x, size_t y, size_t& out)
    {
      out = x + y;
      this_thread::sleep_for (chrono::microseconds (out * 10));
    };

    auto outer = [&s, &inner] (size_t n, size_t& out)
    {
      vector<size_t> result (2 * n, 0);
      scheduler::atomic_count task_count (0);

      for (size_t i (0); i != 2 * n; ++i)
      {
        s.async (task_count,
                 inner,
                 i,
                 i,
                 std::ref (result[i]));
      }

      s.wait (task_count);
      assert (task_count == 0);

      for (size_t i (0); i != n; ++i)
        out += result[i];

      this_thread::sleep_for (chrono::microseconds (out * 10));
    };

    const size_t tasks (50);

    vector<size_t> result (tasks, 0);
    scheduler::atomic_count task_count (0);

    for (size_t i (0); i != tasks; ++i)
    {
      s.async (task_count,
               outer,
               i,
               std::ref (result[i]));
    }

    s.wait (task_count);
    assert (task_count == 0);

    scheduler::stat st (s.shutdown ());

    if (verb)
    {
      cerr << "thread_max_active      " << st.thread_max_active     << endl
           << "thread_max_total       " << st.thread_max_total      << endl
           << "thread_helpers         " << st.thread_helpers        << endl
           << "thread_max_waiting     " << st.thread_max_waiting    << endl
           << endl
           << "task_queue_depth       " << st.task_queue_depth      << endl
           << "task_queue_full        " << st.task_queue_full       << endl
           << endl
           << "wait_queue_slots       " << st.wait_queue_slots      << endl
           << "wait_queue_collisions  " << st.wait_queue_collisions << endl;
    }

    return 0;
  }
}

int
main (int argc, char* argv[])
{
  return build2::main (argc, argv);
}