This repository has been archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobclient.js
230 lines (194 loc) · 6.15 KB
/
jobclient.js
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
// jobclient for the gnumake jobserver
// based on https://github.com/olsner/jobClient/blob/master/jobClient.h
// license: MIT
// copyright: (c) 2022 Milan Hauth <[email protected]>
// NOTE n-1 error
// make counts this jobclient as one job
// so we dont need a token for the first worker (serial processing).
// we only need tokens for 2 or more workers (parallel processing).
// NOTE maxJobs
// maxJobs is the global limit for all make jobs,
// so this jobclient can get less than (maxJobs-1) tokens.
// to find the maximum number of free tokens,
// you must acquire them all.
const process = require('process');
const fs = require('fs');
const os = require('os');
const debug = Boolean(process.env.DEBUG_JOBCLIENT);
const log = (msg) => console.error(`jobclient.js ${process.pid} ${new Date().toLocaleString('af')}.${String(Date.now() % 1000).padStart(3, '0')}: ${msg}`); // print to stderr
function parseFlags(makeFlags) {
let fdRead, fdWrite, maxJobs, maxLoad;
for (const flag of makeFlags.split(/\s+/)) {
let match;
if (
(match = flag.match(/^--jobserver-auth=(\d+),(\d+)$/)) ||
(match = flag.match(/^--jobserver-fds=(\d+),(\d+)$/))
) {
fdRead = parseInt(match[1]);
fdWrite = parseInt(match[2]);
}
else if (match = flag.match(/^-j(\d+)$/)) {
maxJobs = parseInt(match[1]);
}
else if (match = flag.match(/^-l(\d+)$/)) {
maxLoad = parseInt(match[1]);
}
}
return { fdRead, fdWrite, maxJobs, maxLoad };
}
function validateToken(token) {
if (token == null) {
throw new Error('empty token');
}
if (typeof(token) != 'number' || token < 0 || 255 < token) {
throw new Error('invalid token');
}
}
// jobClient factory
exports.JobClient = function JobClient() {
debug && log("init");
const makeFlags = process.env.MAKEFLAGS;
if (!makeFlags) {
debug && log(`init failed: MAKEFLAGS is empty`);
return null;
}
debug && log(`init: MAKEFLAGS: ${makeFlags}`);
const { fdRead, fdWrite, maxJobs, maxLoad } = parseFlags(makeFlags);
debug && log(`init: fdRead = ${fdRead}, fdWrite = ${fdWrite}, maxJobs = ${maxJobs}, maxLoad = ${maxLoad}`);
if (fdRead == undefined) {
debug && log(`init failed: fds missing in MAKEFLAGS`);
return null;
}
if (maxJobs == 1) {
debug && log(`init failed: maxJobs == 1`);
return null;
}
const buffer = Buffer.alloc(1);
const jobClient = {
acquire: () => {
let bytesRead = 0;
debug && log(`acquire: read ...`);
try {
bytesRead = fs.readSync(fdRead, buffer);
}
catch (e) {
if (e.errno == -11) {
debug && log(`acquire: token = null`);
return null; // jobserver is full, try again later
}
throw e;
}
debug && log(`acquire: read done: ${bytesRead} bytes`);
if (bytesRead != 1) throw new Error('read failed');
const token = buffer.readInt8();
debug && log(`acquire: token = ${token}`);
return token;
},
release: (token=43) => { // default token: str(+) == int(43)
debug && log(`release: token = ${token}`);
validateToken(token);
buffer.writeInt8(token);
let bytesWritten = 0;
debug && log(`release: write ...`);
try {
bytesWritten = fs.writeSync(fdWrite, buffer);
}
catch (e) {
//if (e.errno == -11) return false; // TODO errno?
throw e;
}
debug && log(`release: write done: ${bytesWritten} bytes`);
if (bytesWritten != 1) throw new Error('write failed');
return true; // success
},
};
// add read-only properties
Object.defineProperties(jobClient, {
maxJobs: { value: maxJobs, enumerable: true },
maxLoad: { value: maxLoad, enumerable: true },
});
// TODO check fds
// examples:
// MAKEFLAGS="--jobserver-auth=3,4 -l32"
// ls -nlv /proc/self/fd/
//
// jobserver on:
// lr-x------ 1 1000 100 64 Jun 27 14:29 3 -> pipe:[102600042]
// l-wx------ 1 1000 100 64 Jun 27 14:29 4 -> pipe:[102600042]
//
// jobserver off:
// lr-x------ 1 1000 100 64 Jun 27 14:29 3 -> /proc/2370722/fd
//
// conditions for jobserver on:
// * maxJobs is undefined // no. this is a bug in ninja-tokenpool. gnumake always sets -j in MAKEFLAGS
// * fds 3 and 4 are connected
// * fd 3 is readable
// * fd 4 is writable
// * fds 3 and 4 are pipes
// * fds 3 and 4 are pipes with the same ID (?)
// TODO windows: named semaphore
function checkAccess(s, check="r") {
const { uid: u, gid: g } = os.userInfo();
const m = s.mode;
if (check == "r") {
return (
((s.uid == u) && (m & fs.constants.S_IRUSR)) ||
((s.gid == g) && (m & fs.constants.S_IRGRP)) ||
(m & fs.constants.S_IROTH)
) != 0;
}
if (check == "w") {
return (
((s.uid == u) && (m & fs.constants.S_IWUSR)) ||
((s.gid == g) && (m & fs.constants.S_IWGRP)) ||
(m & fs.constants.S_IWOTH)
) != 0;
}
throw Exception("check must be r or w");
}
debug && log(`init: test fdRead`);
const statsRead = fs.fstatSync(fdRead);
if (!statsRead.isFIFO()) {
debug && log(`init failed: fd ${fdRead} is no pipe`);
return null;
}
if(!checkAccess(statsRead, 'r')) {
debug && log(`init failed: fd ${fdRead} is not readable`);
return null;
}
debug && log(`init: test fdWrite`);
const statsWrite = fs.fstatSync(fdWrite);
if (!statsWrite.isFIFO()) {
debug && log(`init failed: fd ${fdWrite} is no pipe`);
return null;
}
if(!checkAccess(statsWrite, 'w')) {
debug && log(`init failed: fd ${fdWrite} is not writable`);
return null;
}
debug && log(`init: test acquire`);
// test acquire + release
let token = null;
try {
token = jobClient.acquire();
if (token == null) {
debug && log("init ok: jobserver is full");
return jobClient; // ok
}
}
catch (e) {
if (e.errno == -22) {
debug && log("init failed: jobserver off");
return null; // jobserver off
}
throw e; // unexpected error
}
debug && log(`init: test release`);
if (jobClient.release(token) == false) {
// TODO?
//return null;
throw new Error('init failed: release failed');
}
debug && log("init ok");
return jobClient; // ok
}