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
|
#include <cassert>
#include <cstddef>
#include <deque>
#include <functional>
#include <iostream>
#include <vector>
#include <cm/optional>
#include <cm3p/uv.h>
#ifndef _WIN32
# include <unistd.h>
#endif
#include "cmGetPipes.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmUVHandlePtr.h"
#include "cmUVJobServerClient.h"
namespace {
std::size_t const kTOTAL_JOBS = 10;
std::size_t const kTOTAL_TOKENS = 3;
struct Job
{
cm::uv_timer_ptr Timer;
};
struct JobRunner
{
cm::uv_loop_ptr Loop;
cm::optional<cmUVJobServerClient> JSC;
std::vector<Job> Jobs;
std::size_t NextJobIndex = 0;
std::size_t ActiveJobs = 0;
std::deque<std::size_t> Queue;
bool Okay = true;
JobRunner()
: Jobs(kTOTAL_JOBS)
{
this->Loop.init(nullptr);
this->JSC = cmUVJobServerClient::Connect(
*this->Loop, [this]() { this->StartQueuedJob(); }, nullptr);
if (!this->JSC) {
std::cerr << "Failed to connect to job server.\n";
this->Okay = false;
}
}
~JobRunner() {}
bool Run()
{
if (this->Okay) {
this->QueueNextJobs();
uv_run(this->Loop, UV_RUN_DEFAULT);
std::cerr << "HeldTokens: " << this->JSC->GetHeldTokens() << '\n';
std::cerr << "NeedTokens: " << this->JSC->GetNeedTokens() << '\n';
}
#ifdef _WIN32
// FIXME: Windows job server client not yet implemented.
return true;
#else
return this->Okay;
#endif
}
void QueueNextJobs()
{
std::cerr << "QueueNextJobs()\n";
std::size_t queued = 0;
while (queued < 2 && this->NextJobIndex < this->Jobs.size()) {
this->QueueJob(this->NextJobIndex);
++this->NextJobIndex;
++queued;
}
std::cerr << "QueueNextJobs done\n";
}
void StartQueuedJob()
{
std::cerr << "StartQueuedJob()\n";
assert(!this->Queue.empty());
std::size_t index = this->Queue.front();
this->Queue.pop_front();
this->StartJob(index);
std::cerr << "StartQueuedJob done\n";
}
void StartJob(std::size_t index)
{
cm::uv_timer_ptr& job = this->Jobs[index].Timer;
job.init(*this->Loop, this);
uv_timer_start(
job,
[](uv_timer_t* handle) {
uv_timer_stop(handle);
auto self = static_cast<JobRunner*>(handle->data);
self->FinishJob();
},
/*timeout_ms=*/10 * (1 + (index % 3)), /*repeat_ms=*/0);
++this->ActiveJobs;
std::cerr << " StartJob(" << index
<< "): Active jobs: " << this->ActiveJobs << '\n';
if (this->ActiveJobs > kTOTAL_TOKENS) {
std::cerr << "Started more than " << kTOTAL_TOKENS << " jobs at once!\n";
this->Okay = false;
return;
}
}
void QueueJob(std::size_t index)
{
this->JSC->RequestToken();
this->Queue.push_back(index);
std::cerr << " QueueJob(" << index
<< "): Queue length: " << this->Queue.size() << '\n';
}
void FinishJob()
{
--this->ActiveJobs;
std::cerr << "FinishJob: Active jobs: " << this->ActiveJobs << '\n';
this->JSC->ReleaseToken();
this->QueueNextJobs();
}
};
bool testJobServer()
{
#ifdef _WIN32
// FIXME: Windows job server client not yet implemented.
#else
// Create a job server pipe.
int jobServerPipe[2];
if (cmGetPipes(jobServerPipe) < 0) {
std::cerr << "Failed to create job server pipe\n";
return false;
}
// Write N-1 tokens to the pipe.
std::vector<char> jobServerInit(kTOTAL_TOKENS - 1, '.');
if (write(jobServerPipe[1], jobServerInit.data(), jobServerInit.size()) !=
kTOTAL_TOKENS - 1) {
std::cerr << "Failed to initialize job server pipe\n";
return false;
}
// Establish the job server client context.
// Add a bogus server spec to verify we use the last spec.
cmSystemTools::PutEnv(cmStrCat("MAKEFLAGS=--flags-before"
" --jobserver-auth=bogus"
" --flags-between"
" --jobserver-fds=",
jobServerPipe[0], ',', jobServerPipe[1],
" --flags-after"));
#endif
JobRunner jobRunner;
return jobRunner.Run();
}
}
int testUVJobServerClient(int, char** const)
{
bool passed = true;
passed = testJobServer() && passed;
return passed ? 0 : -1;
}
|