aboutsummaryrefslogtreecommitdiffstats
path: root/fastos/src/tests/threadtest.cpp
blob: 563b41ac229e4f25525647197a115bee0d21815a (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "tests.h"
#include "job.h"
#include "thread_test_base.hpp"
#include <cstdlib>
#include <chrono>

#define MAX_THREADS 7

using namespace std::chrono;
using namespace std::chrono_literals;

class ThreadTest : public ThreadTestBase
{
   int Main () override;

   void TooManyThreadsTest ()
   {
      TestHeader("Too Many Threads Test");
      static constexpr size_t message_size = 100;

      FastOS_ThreadPool *pool = new FastOS_ThreadPool(MAX_THREADS);

      if (Progress(pool != nullptr, "Allocating ThreadPool")) {
         int i;
         Job jobs[MAX_THREADS+1];

         for (i=0; i<MAX_THREADS+1; i++) {
            jobs[i].code = WAIT_FOR_BREAK_FLAG;
            jobs[i].message = static_cast<char *>(malloc(message_size));
            if (jobs[i].message == nullptr) {
                abort(); // GCC may infer that a potentially null ptr is passed to sprintf
            }
            snprintf(jobs[i].message, message_size, "Thread %d invocation", i+1);
         }

         for (i=0; i<MAX_THREADS+1; i++) {
            if (i==MAX_THREADS) {
                while (pool->GetNumInactiveThreads() > 0);
                jobs[MAX_THREADS].code = PRINT_MESSAGE_AND_WAIT3MSEC;
               bool rc = (nullptr == pool->NewThread(this, static_cast<void *>(&jobs[MAX_THREADS])));
               Progress(rc, "Creating too many threads should fail.");
            } else {
               jobs[i].ownThread = pool->NewThread(this, static_cast<void *>(&jobs[i]));
               Progress(jobs[i].ownThread != nullptr, "Creating Thread");
            }
         }
          for (i=0; i<MAX_THREADS; i++) {
              jobs[i].ownThread->SetBreakFlag();
          }
          
         Progress(true, "Closing threadpool...");
         pool->Close();

         Progress(true, "Deleting threadpool...");
         delete(pool);
      }
      PrintSeparator();
   }

   void CreateSingleThreadAndJoin ()
   {
      TestHeader("Create Single Thread And Join Test");

      FastOS_ThreadPool *pool = new FastOS_ThreadPool;

      if (Progress(pool != nullptr, "Allocating ThreadPool")) {
         Job job;

         job.code = NOP;
         job.result = -1;

         bool rc = (nullptr != pool->NewThread(this, &job));
         Progress(rc, "Creating Thread");

         WaitForThreadsToFinish(&job, 1);
      }

      Progress(true, "Closing threadpool...");
      pool->Close();

      Progress(true, "Deleting threadpool...");
      delete(pool);
      PrintSeparator();
   }

  void ThreadCreatePerformance (bool silent, int count, int outercount) {
    int i;
    int j;
    bool rc;
    int threadsok;

    if (!silent)
      TestHeader("Thread Create Performance");

    FastOS_ThreadPool *pool = new FastOS_ThreadPool;

    if (!silent)
      Progress(pool != nullptr, "Allocating ThreadPool");
    if (pool != nullptr) {
      Job *jobs = new Job[count];

      threadsok = 0;
      steady_clock::time_point start = steady_clock::now();
      for (i = 0; i < count; i++) {
        jobs[i].code = SILENTNOP;
        jobs[i].ownThread = pool->NewThread(this, &jobs[i]);
        rc = (jobs[i].ownThread != nullptr);
        if (rc)
          threadsok++;
      }

      for (j = 0; j < outercount; j++) {
        for (i = 0; i < count; i++) {
          if (jobs[i].ownThread != nullptr)
            jobs[i].ownThread->Join();
          jobs[i].ownThread = pool->NewThread(this, &jobs[i]);
          rc = (jobs[i].ownThread != nullptr);
          if (rc)
            threadsok++;
        }
      }
      for (i = 0; i < count; i++) {
        if (jobs[i].ownThread != nullptr)
          jobs[i].ownThread->Join();
      }
      nanoseconds used = steady_clock::now() - start;

      if (!silent) {
          double timeused = used.count() / 1000000000.0;

          Progress(true, "Used time: %2.3f", timeused);
          ProgressFloat(true, "Threads/s: %6.1f",
                      static_cast<float>(static_cast<double>(threadsok) / timeused));
      }
      if (threadsok != ((outercount + 1) * count))
        Progress(false, "Only started %d of %d threads", threadsok,
                 (outercount + 1) * count);

      if (!silent)
        Progress(true, "Closing threadpool...");
      pool->Close();
      delete [] jobs;

      if (!silent)
        Progress(true, "Deleting threadpool...");
      delete(pool);
      if (!silent)
        PrintSeparator();
    }
  }

  void ClosePoolStability(void) {
    int i;
    TestHeader("ThreadPool close stability test");
    for (i = 0; i < 1000; i++) {
      // Progress(true, "Creating pool iteration %d", i + 1);
      ThreadCreatePerformance(true, 2, 1);
    }
    PrintSeparator();
  }



   void ClosePoolTest ()
   {
      TestHeader("Close Pool Test");

      FastOS_ThreadPool pool;
      const int closePoolThreads=9;
      Job jobs[closePoolThreads];

      number = 0;

      for(int i=0; i<closePoolThreads; i++) {
         jobs[i].code = INCREASE_NUMBER;

         bool rc = (nullptr != pool.NewThread(this, static_cast<void *>(&jobs[i])));
         Progress(rc, "Creating Thread %d", i+1);
      }

      Progress(true, "Waiting for threads to finish using pool.Close()...");
      pool.Close();
      Progress(true, "Pool closed.");
      PrintSeparator();
   }

   void BreakFlagTest () {
      TestHeader("BreakFlag Test");

      FastOS_ThreadPool pool;

      const int breakFlagThreads=4;

      Job jobs[breakFlagThreads];

      for (int i=0; i<breakFlagThreads; i++) {
         jobs[i].code = WAIT_FOR_BREAK_FLAG;

         bool rc = (nullptr != pool.NewThread(this, static_cast<void *>(&jobs[i])));
         Progress(rc, "Creating Thread %d", i+1);
      }

      Progress(true, "Waiting for threads to finish using pool.Close()...");
      pool.Close();
      Progress(true, "Pool closed.");
      PrintSeparator();
   }

   void ThreadIdTest () {
      constexpr int numThreads = 5;

      TestHeader ("Thread Id Test");

      FastOS_ThreadPool pool;
      Job jobs[numThreads];
      std::mutex slowStartMutex;

      slowStartMutex.lock();    // Halt all threads until we want them to run

      for (int i=0; i<numThreads; i++) {
         jobs[i].code = TEST_ID;
         jobs[i].result = -1;
         jobs[i]._threadId = 0;
         jobs[i].mutex = &slowStartMutex;
         jobs[i].ownThread = pool.NewThread(this, static_cast<void *>(&jobs[i]));
         bool rc=(jobs[i].ownThread != nullptr);
         if (rc) {
             jobs[i]._threadId = jobs[i].ownThread->GetThreadId();
         }
         Progress(rc, "CreatingThread %d id:%lu", i+1, (unsigned long)(jobs[i]._threadId));

         for (int j=0; j<i; j++) {
            if (jobs[j]._threadId == jobs[i]._threadId) {
               Progress(false, "Two different threads received the same thread id (%lu)",
                        (unsigned long)(jobs[i]._threadId));
            }
         }
      }

      slowStartMutex.unlock();    // Allow threads to run

      Progress(true, "Waiting for threads to finish using pool.Close()...");
      pool.Close();
      Progress(true, "Pool closed.");

      for (int i=0; i<numThreads; i++) {
         Progress(jobs[i].result == 1,
                  "Thread %lu: ID comparison (current vs stored)",
                  (unsigned long)(jobs[i]._threadId));
      }

      PrintSeparator();
   }

};

int ThreadTest::Main ()
{
   printf("grep for the string '%s' to detect failures.\n\n", failString);
   time_t before = time(0);

   ThreadIdTest();
   CreateSingleThreadAndJoin();
   TooManyThreadsTest();
   ClosePoolTest();
   BreakFlagTest();
   CreateSingleThreadAndJoin();
   ThreadCreatePerformance(false, 50, 10);
   ClosePoolStability();
   { time_t now = time(0); printf("[%ld seconds]\n", now-before); before = now; }

   printf("END OF TEST (%s)\n", _argv[0]);
   return allWasOk() ? 0 : 1;
}

int main (int argc, char **argv)
{
   ThreadTest app;
   setvbuf(stdout, nullptr, _IOLBF, 8192);
   return app.Entry(argc, argv);
}