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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
//===- llvm/unittest/Support/JobserverTest.cpp ----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// Jobserver.h unit tests.
///
//===----------------------------------------------------------------------===//
#include "llvm/Support/Jobserver.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Parallel.h"
#include "llvm/Support/ThreadPool.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
#include <future>
#include <random>
#include <stdlib.h>
#if defined(LLVM_ON_UNIX)
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/FileSystem.h"
#include <atomic>
#include <condition_variable>
#include <fcntl.h>
#include <mutex>
#include <sys/stat.h>
#include <thread>
#include <unistd.h>
#elif defined(_WIN32)
#include <windows.h>
#endif
#define DEBUG_TYPE "jobserver-test"
using namespace llvm;
namespace {
// RAII helper to set an environment variable for the duration of a test.
class ScopedEnvironment {
std::string Name;
std::string OldValue;
bool HadOldValue;
public:
ScopedEnvironment(const char *Name, const char *Value) : Name(Name) {
#if defined(_WIN32)
char *Old = nullptr;
size_t OldLen;
errno_t err = _dupenv_s(&Old, &OldLen, Name);
if (err == 0 && Old != nullptr) {
HadOldValue = true;
OldValue = Old;
free(Old);
} else {
HadOldValue = false;
}
_putenv_s(Name, Value);
#else
const char *Old = getenv(Name);
if (Old) {
HadOldValue = true;
OldValue = Old;
} else {
HadOldValue = false;
}
setenv(Name, Value, 1);
#endif
}
~ScopedEnvironment() {
#if defined(_WIN32)
if (HadOldValue)
_putenv_s(Name.c_str(), OldValue.c_str());
else
// On Windows, setting an environment variable to an empty string
// unsets it, making getenv() return NULL.
_putenv_s(Name.c_str(), "");
#else
if (HadOldValue)
setenv(Name.c_str(), OldValue.c_str(), 1);
else
unsetenv(Name.c_str());
#endif
}
};
TEST(Jobserver, Slot) {
// Default constructor creates an invalid slot.
JobSlot S1;
EXPECT_FALSE(S1.isValid());
EXPECT_FALSE(S1.isImplicit());
// Create an implicit slot.
JobSlot S2 = JobSlot::createImplicit();
EXPECT_TRUE(S2.isValid());
EXPECT_TRUE(S2.isImplicit());
// Create an explicit slot.
JobSlot S3 = JobSlot::createExplicit(42);
EXPECT_TRUE(S3.isValid());
EXPECT_FALSE(S3.isImplicit());
// Test move construction.
JobSlot S4 = std::move(S2);
EXPECT_TRUE(S4.isValid());
EXPECT_TRUE(S4.isImplicit());
EXPECT_FALSE(S2.isValid()); // S2 is now invalid.
// Test move assignment.
S1 = std::move(S3);
EXPECT_TRUE(S1.isValid());
EXPECT_FALSE(S1.isImplicit());
EXPECT_FALSE(S3.isValid()); // S3 is now invalid.
}
// Test fixture for parsing tests to ensure the singleton state is
// reset between each test case.
class JobserverParsingTest : public ::testing::Test {
protected:
void TearDown() override { JobserverClient::resetForTesting(); }
};
TEST_F(JobserverParsingTest, NoMakeflags) {
// No MAKEFLAGS, should be null.
ScopedEnvironment Env("MAKEFLAGS", "");
// On Unix, setting an env var to "" makes getenv() return an empty
// string, not NULL. We must call unsetenv() to test the case where
// the variable is truly not present.
#if !defined(_WIN32)
unsetenv("MAKEFLAGS");
#endif
EXPECT_EQ(JobserverClient::getInstance(), nullptr);
}
TEST_F(JobserverParsingTest, EmptyMakeflags) {
// Empty MAKEFLAGS, should be null.
ScopedEnvironment Env("MAKEFLAGS", "");
EXPECT_EQ(JobserverClient::getInstance(), nullptr);
}
TEST_F(JobserverParsingTest, DryRunFlag) {
// Dry-run flag 'n', should be null.
ScopedEnvironment Env("MAKEFLAGS", "n -j --jobserver-auth=fifo:/tmp/foo");
EXPECT_EQ(JobserverClient::getInstance(), nullptr);
}
// Separate fixture for non-threaded client tests.
class JobserverClientTest : public JobserverParsingTest {};
#if defined(LLVM_ON_UNIX)
// RAII helper to create and clean up a temporary FIFO file.
class ScopedFifo {
SmallString<128> Path;
bool IsValid = false;
public:
ScopedFifo() {
// To get a unique, non-colliding name for a FIFO, we use the
// createTemporaryFile function to reserve a name in the filesystem.
std::error_code EC =
sys::fs::createTemporaryFile("jobserver-test", "fifo", Path);
if (EC)
return;
// Then we immediately remove the regular file it created, but keep the
// unique path.
sys::fs::remove(Path);
// Finally, we create the FIFO at that safe, unique path.
if (mkfifo(Path.c_str(), 0600) != 0)
return;
IsValid = true;
}
~ScopedFifo() {
if (IsValid)
sys::fs::remove(Path);
}
const char *c_str() const { return Path.data(); }
bool isValid() const { return IsValid; }
};
TEST_F(JobserverClientTest, UnixClientFifo) {
// This test covers basic FIFO client creation and behavior with an empty
// FIFO. No job tokens are available.
ScopedFifo F;
ASSERT_TRUE(F.isValid());
// Intentionally inserted \t in environment string.
std::string Makeflags = " \t -j4\t \t--jobserver-auth=fifo:";
Makeflags += F.c_str();
ScopedEnvironment Env("MAKEFLAGS", Makeflags.c_str());
JobserverClient *Client = JobserverClient::getInstance();
ASSERT_NE(Client, nullptr);
// Get the implicit token.
JobSlot S1 = Client->tryAcquire();
EXPECT_TRUE(S1.isValid());
EXPECT_TRUE(S1.isImplicit());
// FIFO is empty, next acquire fails.
JobSlot S2 = Client->tryAcquire();
EXPECT_FALSE(S2.isValid());
// Release does not write to the pipe for the implicit token.
Client->release(std::move(S1));
// Re-acquire the implicit token.
S1 = Client->tryAcquire();
EXPECT_TRUE(S1.isValid());
}
#if LLVM_ENABLE_THREADS
// Test fixture for tests that use the jobserver strategy. It creates a
// temporary FIFO, sets MAKEFLAGS, and provides a helper to pre-load the FIFO
// with job tokens, simulating `make -jN`.
class JobserverStrategyTest : public JobserverParsingTest {
protected:
std::unique_ptr<ScopedFifo> TheFifo;
std::thread MakeThread;
std::atomic<bool> StopMakeThread{false};
// Save and restore the global parallel strategy to avoid interfering with
// other tests in the same process.
ThreadPoolStrategy SavedStrategy;
void SetUp() override {
SavedStrategy = parallel::strategy;
TheFifo = std::make_unique<ScopedFifo>();
ASSERT_TRUE(TheFifo->isValid());
std::string MakeFlags = "--jobserver-auth=fifo:";
MakeFlags += TheFifo->c_str();
setenv("MAKEFLAGS", MakeFlags.c_str(), 1);
}
void TearDown() override {
if (MakeThread.joinable()) {
StopMakeThread = true;
MakeThread.join();
}
unsetenv("MAKEFLAGS");
TheFifo.reset();
// Restore the original strategy to ensure subsequent tests are unaffected.
parallel::strategy = SavedStrategy;
}
// Starts a background thread that emulates `make`. It populates the FIFO
// with initial tokens and then recycles tokens released by clients.
void startMakeProxy(int NumInitialJobs) {
MakeThread = std::thread([this, NumInitialJobs]() {
LLVM_DEBUG(dbgs() << "[MakeProxy] Thread started.\n");
// Open the FIFO for reading and writing. This call does not block.
int RWFd = open(TheFifo->c_str(), O_RDWR);
LLVM_DEBUG(dbgs() << "[MakeProxy] Opened FIFO " << TheFifo->c_str()
<< " with O_RDWR, FD=" << RWFd << "\n");
if (RWFd == -1) {
LLVM_DEBUG(
dbgs()
<< "[MakeProxy] ERROR: Failed to open FIFO with O_RDWR. Errno: "
<< errno << "\n");
return;
}
// Populate with initial jobs.
LLVM_DEBUG(dbgs() << "[MakeProxy] Writing " << NumInitialJobs
<< " initial tokens.\n");
for (int i = 0; i < NumInitialJobs; ++i) {
if (write(RWFd, "+", 1) != 1) {
LLVM_DEBUG(dbgs()
<< "[MakeProxy] ERROR: Failed to write initial token " << i
<< ".\n");
close(RWFd);
return;
}
}
LLVM_DEBUG(dbgs() << "[MakeProxy] Finished writing initial tokens.\n");
// Make the read non-blocking so we can periodically check StopMakeThread.
int flags = fcntl(RWFd, F_GETFL, 0);
fcntl(RWFd, F_SETFL, flags | O_NONBLOCK);
while (!StopMakeThread) {
char Token;
ssize_t Ret = read(RWFd, &Token, 1);
if (Ret == 1) {
LLVM_DEBUG(dbgs() << "[MakeProxy] Read token '" << Token
<< "' to recycle.\n");
// A client released a token, 'make' makes it available again.
std::this_thread::sleep_for(std::chrono::microseconds(100));
ssize_t WRet;
do {
WRet = write(RWFd, &Token, 1);
} while (WRet < 0 && errno == EINTR);
if (WRet <= 0) {
LLVM_DEBUG(
dbgs()
<< "[MakeProxy] ERROR: Failed to write recycled token.\n");
break; // Error, stop the proxy.
}
LLVM_DEBUG(dbgs()
<< "[MakeProxy] Wrote token '" << Token << "' back.\n");
} else if (Ret < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
LLVM_DEBUG(dbgs() << "[MakeProxy] ERROR: Read failed with errno "
<< errno << ".\n");
break; // Error, stop the proxy.
}
// Yield to prevent this thread from busy-waiting.
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
LLVM_DEBUG(dbgs() << "[MakeProxy] Thread stopping.\n");
close(RWFd);
});
// Give the proxy thread a moment to start and populate the FIFO.
// This is a simple way to avoid a race condition where the client starts
// before the initial tokens are in the pipe.
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
};
TEST_F(JobserverStrategyTest, ThreadPoolConcurrencyIsLimited) {
// This test simulates `make -j3`. We will have 1 implicit job slot and
// we will add 2 explicit job tokens to the FIFO, for a total of 3.
const int NumExplicitJobs = 2;
const int ConcurrencyLimit = NumExplicitJobs + 1; // +1 for the implicit slot
const int NumTasks = 8; // More tasks than available slots.
LLVM_DEBUG(dbgs() << "Calling startMakeProxy with " << NumExplicitJobs
<< " jobs.\n");
startMakeProxy(NumExplicitJobs);
LLVM_DEBUG(dbgs() << "MakeProxy is running.\n");
// Create the thread pool. Its constructor will call jobserver_concurrency()
// and create a client that reads from our pre-loaded FIFO.
StdThreadPool Pool(jobserver_concurrency());
std::atomic<int> ActiveTasks{0};
std::atomic<int> MaxActiveTasks{0};
std::atomic<int> CompletedTasks{0};
std::mutex M;
std::condition_variable CV;
// Dispatch more tasks than there are job slots. The pool should block
// and only run up to `ConcurrencyLimit` tasks at once.
for (int i = 0; i < NumTasks; ++i) {
Pool.async([&, i] {
// Track the number of concurrently running tasks.
int CurrentActive = ++ActiveTasks;
LLVM_DEBUG(dbgs() << "Task " << i << ": Active tasks: " << CurrentActive
<< "\n");
int OldMax = MaxActiveTasks.load();
while (CurrentActive > OldMax)
MaxActiveTasks.compare_exchange_weak(OldMax, CurrentActive);
std::this_thread::sleep_for(std::chrono::milliseconds(25));
--ActiveTasks;
if (++CompletedTasks == NumTasks) {
std::lock_guard<std::mutex> Lock(M);
CV.notify_one();
}
});
}
// Wait for all tasks to complete.
std::unique_lock<std::mutex> Lock(M);
CV.wait(Lock, [&] { return CompletedTasks == NumTasks; });
LLVM_DEBUG(dbgs() << "Test finished. Max active tasks was " << MaxActiveTasks
<< ".\n");
// The key assertion: the maximum number of concurrent tasks should
// not have exceeded the limit imposed by the jobserver.
EXPECT_LE(MaxActiveTasks, ConcurrencyLimit);
EXPECT_EQ(CompletedTasks, NumTasks);
}
TEST_F(JobserverStrategyTest, ParallelForIsLimited) {
// This test verifies that llvm::parallelFor respects the jobserver limit.
const int NumExplicitJobs = 3;
const int ConcurrencyLimit = NumExplicitJobs + 1; // +1 implicit
const int NumTasks = 20;
LLVM_DEBUG(dbgs() << "Calling startMakeProxy with " << NumExplicitJobs
<< " jobs.\n");
startMakeProxy(NumExplicitJobs);
LLVM_DEBUG(dbgs() << "MakeProxy is running.\n");
// Set the global strategy. parallelFor will use this.
parallel::strategy = jobserver_concurrency();
std::atomic<int> ActiveTasks{0};
std::atomic<int> MaxActiveTasks{0};
parallelFor(0, NumTasks, [&](int i) {
int CurrentActive = ++ActiveTasks;
LLVM_DEBUG(dbgs() << "Task " << i << ": Active tasks: " << CurrentActive
<< "\n");
int OldMax = MaxActiveTasks.load();
while (CurrentActive > OldMax)
MaxActiveTasks.compare_exchange_weak(OldMax, CurrentActive);
std::this_thread::sleep_for(std::chrono::milliseconds(20));
--ActiveTasks;
});
LLVM_DEBUG(dbgs() << "ParallelFor finished. Max active tasks was "
<< MaxActiveTasks << ".\n");
EXPECT_LE(MaxActiveTasks, ConcurrencyLimit);
}
TEST_F(JobserverStrategyTest, ParallelSortIsLimited) {
// This test serves as an integration test to ensure parallelSort completes
// correctly when running under the jobserver strategy. It doesn't directly
// measure concurrency but verifies correctness.
const int NumExplicitJobs = 3;
startMakeProxy(NumExplicitJobs);
parallel::strategy = jobserver_concurrency();
std::vector<int> V(1024);
// Fill with random data
std::mt19937 randEngine;
std::uniform_int_distribution<int> dist;
for (int &i : V)
i = dist(randEngine);
parallelSort(V.begin(), V.end());
ASSERT_TRUE(llvm::is_sorted(V));
}
#endif // LLVM_ENABLE_THREADS
#endif // defined(LLVM_ON_UNIX)
} // end anonymous namespace
|