aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/scheduler.test.cxx
blob: 1252575eb966f85db2986968e5ff83aa31c3ad8e (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
// file      : libbuild2/scheduler.test.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <chrono>
#include <thread>

#include <cassert>
#include <iostream>

#include <libbuild2/types.hxx>
#include <libbuild2/utility.hxx>

#include <libbuild2/scheduler.hxx>

using namespace std;

namespace build2
{
  // Usage argv[0] [-v <volume>] [-d <difficulty>] [-c <concurrency>]
  //               [-q <queue-depth>]
  //
  // -v  task tree volume (affects both depth and width), for example 100
  // -d  computational difficulty of each task, for example 10
  // -c  max active threads, if unspecified or 0, then hardware concurrency
  // -q  task queue depth, if unspecified or 0, then appropriate default used
  //
  // Specifying any option also turns on the verbose mode.
  //
  // Notes on testing:
  //
  // 1. Ideally you would want to test things on an SMP machine.
  //
  // 2. When need to compare performance, disable turbo boost since its
  //    availability depends on CPU utilization/temperature:
  //
  //    # echo '1' >/sys/devices/system/cpu/intel_pstate/no_turbo
  //
  // 3. Use turbostat(1) to see per-CPU details (utlization, frequency):
  //
  //    $ sudo turbostat --interval 1 ./driver -d 8 -v 300
  //
  static bool
  prime (uint64_t);

  // Find # of primes in the [x, y) range.
  //
  static void
  inner (uint64_t x, uint64_t y, uint64_t& r)
  {
    for (; x != y; ++x)
      if (prime (x))
        r++;
  };

  int
  main (int argc, char* argv[])
  {
    bool verb (false);

    // Adjust assert() below if changing these defaults.
    //
    size_t volume (100);
    uint32_t difficulty (10);

    size_t max_active (0);
    size_t queue_depth (0);

    for (int i (1); i != argc; ++i)
    {
      string a (argv[i]);

      if (a == "-v")
        volume = stoul (argv[++i]);
      else if (a == "-d")
        difficulty = stoul (argv[++i]);
      else if (a == "-c")
        max_active = stoul (argv[++i]);
      else if (a == "-q")
        queue_depth = stoul (argv[++i]);
      else
        assert (false);

      verb = true;
    }

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

    scheduler s (max_active, 1, 0, queue_depth);

    // Find # prime counts of primes in [i, d*i*i) ranges for i in (0, n].
    //
    auto outer = [difficulty, &s] (size_t n, vector<uint64_t>& o, uint64_t& r)
    {
      scheduler::atomic_count task_count (0);

      for (size_t i (1); i <= n; ++i)
      {
        o[i - 1] = 0;
        s.async (task_count,
                 inner,
                 i,
                 i * i * difficulty,
                 ref (o[i - 1]));
      }

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

      for (uint64_t v: o)
        r += prime (v) ? 1 : 0;
    };

    vector<uint64_t> r (volume, 0);
    vector<vector<uint64_t>> o (volume, vector<uint64_t> ());

    scheduler::atomic_count task_count (0);

    for (size_t i (0); i != volume; ++i)
    {
      o[i].resize (i);
      s.async (task_count,
               outer,
               i,
               ref (o[i]),
               ref (r[i]));
    }

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

    uint64_t n (0);
    for (uint64_t v: r)
      n += v;

    if (volume == 100 && difficulty == 10)
      assert (n == 580);

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

    if (verb)
    {
      cerr << "result                 " << n                       << endl
           << endl;

      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;
  }

  static bool
  prime (uint64_t x)
  {
    if (x == 2 || x == 3)
      return true;

    if (x < 2 || x % 2 == 0 || x % 3 == 0)
      return false;

    // Test divisors starting from 5 and incrementing alternatively by 2/4.
    //
    for (uint64_t d (5), i (2); d * d <= x; d += i, i = 6 - i)
    {
      if (x % d == 0)
        return false;
    }

    return true;
  }
}

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