-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscheduler.c
497 lines (428 loc) · 14.9 KB
/
scheduler.c
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include <stdio.h>
#include <stdlib.h> // strtoul()
#include <limits.h> // ULONG_MAX
#include <string.h> // strerror()
#include <errno.h>
#include <unistd.h> // close()
#include <sys/wait.h>
#include <sysexits.h>
#include <xtend/file.h>
#include <xtend/math.h> // XT_MIN()
#include <xtend/string.h> // strlcat() on Linux
#include "lpjs.h"
#include "node-list.h"
#include "scheduler.h"
#include "network.h"
#include "misc.h" // lpjs_log()
/***************************************************************************
* Description:
* Select nodes to run a pending job
*
* Default to packing jobs as densely as possible, i.e. use up
* available processors on already busy nodes before allocating processors on
* idle nodes. This will allow faster deployment of shared memory
* parallel jobs, which need many processors on the same node.
*
* Returns:
*
* History:
* Date Name Modification
* 2021-10-05 Jason Bacon Begin
***************************************************************************/
int lpjs_select_nodes()
{
return 0;
}
/***************************************************************************
* Description:
* Check available nodes and the job queue, and dispatch the
* next job, if possible.
*
* Returns:
* The number of nodes matched, or a negative error
* code if something went wrong. A positive number indicates
* that nodes were available and this function should be called
* again until it returns 0 or an error.
*
* History:
* Date Name Modification
* 2024-01-22 Jason Bacon Begin
***************************************************************************/
int lpjs_dispatch_next_job(node_list_t *node_list,
job_list_t *pending_jobs,
job_list_t *running_jobs)
{
job_t *job;
// Terminates process if malloc() fails, no check required
node_list_t *matched_nodes = node_list_new();
char pending_path[PATH_MAX + 1],
script_path[PATH_MAX + 2],
script_buff[LPJS_SCRIPT_SIZE_MAX + 1],
outgoing_msg[LPJS_JOB_MSG_MAX + 1],
*munge_payload;
int compd_msg_fd,
node_count;
ssize_t script_size,
payload_bytes;
uid_t uid;
gid_t gid;
/*
* Look through spool dir and determine requirements of the
* next job in the queue
*/
if ( lpjs_select_next_job(pending_jobs, &job) < 1 )
{
lpjs_log("%s(): No pending jobs.\n", __FUNCTION__);
return 0;
}
/*
* Look through available nodes and select the best match
* for the job requirements
*/
if ( (node_count = lpjs_match_nodes(job, node_list, matched_nodes)) > 0 )
{
lpjs_log("%s(): Found %u available nodes.\n",
__FUNCTION__, node_list_get_compute_node_count(matched_nodes));
/*
* Do not move from pending to running yet.
* Wait until chaperone checks in and provides the compute node
* and PIDs.
*/
/*
* Load script from spool/lpjs/pending
*/
snprintf(pending_path, PATH_MAX + 1, "%s/%lu",
LPJS_PENDING_DIR, job_get_job_id(job));
snprintf(script_path, PATH_MAX + 2, "%s/%s",
pending_path, job_get_script_name(job));
script_size = lpjs_load_script(script_path, script_buff,
LPJS_SCRIPT_SIZE_MAX + 1);
if ( script_size < LPJS_SCRIPT_MIN_SIZE )
{
lpjs_log("%s(): Error: Script %s < %d characters.\n",
__FUNCTION__, script_path, LPJS_SCRIPT_MIN_SIZE);
return node_count;
}
/*
* For each matching node
* Update mem and proc availability
* Send script to node and run
* Use script cached in spool dir at submission
*/
// FIXME: Revamp and verify handling of failed dispatches
for (int c = 0; c < node_list_get_compute_node_count(matched_nodes); ++c)
{
node_t *node = node_list_get_compute_nodes_ae(matched_nodes, c);
compd_msg_fd = node_get_msg_fd(node);
lpjs_log("%s(): Dispatching job %lu to %s on socket fd %d...\n",
__FUNCTION__, job_get_job_id(job),
node_get_hostname(node), compd_msg_fd);
outgoing_msg[0] = LPJS_COMPD_REQUEST_NEW_JOB;
job_print_to_string(job, outgoing_msg + 1, LPJS_JOB_MSG_MAX + 1);
lpjs_log("%s(): Job specs: %s\n", __FUNCTION__, outgoing_msg + 1);
// FIXME: Check for truncation
strlcat(outgoing_msg, script_buff, LPJS_JOB_MSG_MAX + 1);
if ( lpjs_send_munge(compd_msg_fd, outgoing_msg,
lpjs_dispatchd_safe_close) != LPJS_MSG_SENT )
{
lpjs_log("%s(): Error: Failed to send job to compd.\n", __FUNCTION__);
free(matched_nodes);
return node_count;
}
/*
* Get chaperone launch status back from compd.
* FIXME: This can take a while and timeouts happen
* Don't wait, but let compd/chaperone connect again
* to send status. Modify lpjs_compd.c
* in tandem with this section to make this happen.
* Add new LPJS_DISPATCHD_REQUEST_JOB_UPDATE
* case to lpjs_check_listen_fd() for receiving
* updates such as job failures? Or just use
* LPJS_DISPATCHD_REQUEST_JOB_COMPLETE?
*/
lpjs_log("%s(): Awaiting chaperone fork verification from %s compd...\n",
__FUNCTION__, node_get_hostname(node));
payload_bytes = lpjs_recv_munge(compd_msg_fd, &munge_payload,
0, LPJS_CHAPERONE_STATUS_TIMEOUT,
&uid, &gid,
lpjs_dispatchd_safe_close);
if ( payload_bytes == LPJS_RECV_TIMEOUT )
{
lpjs_log("%s(): Error: Timed out awaiting dispatch status.\n",
__FUNCTION__);
lpjs_log("%s(): Setting %s to down.\n", __FUNCTION__,
node_get_hostname(node));
node_set_state(node, "down");
}
else if ( munge_payload[0] != LPJS_CHAPERONE_FORKED )
{
lpjs_log("%s(): Bug: Should have received LPJS_CHAPERONE_FORKED.\n",
__FUNCTION__);
lpjs_log("%s(): Got %d instead.\n",
__FUNCTION__, munge_payload[0]);
lpjs_log("%s(): Setting %s to down.\n", __FUNCTION__,
node_get_hostname(node));
node_set_state(node, "down");
}
else
{
/*
* At this point, all we know is that the chaperone
* process was forked successfully by lpjs_run_chaperone().
* It has more work to do setting up the job, but does
* not keep dispatchd waiting, as it can take a long time.
* (more than 1 second in rare cases on a busy compute node).
* But we must assume that job is running and update
* available resources immediately, or this function
* will never return 0 to lpjs_dispatch_jobs(), and
* it will never exit the loop. lpjs_compd and chaperone
* must be good about reporting failures and job completion
* to dispatchd, so we can free these resources ASAP.
*/
lpjs_debug("%s(): Chaperone fork verification received.\n",
__FUNCTION__);
job_set_state(job, JOB_STATE_DISPATCHED);
char *end;
pid_t chaperone_pid = strtol(munge_payload+1, &end, 10);
if ( *end != '\0' )
{
lpjs_log("%s(): Bug: No PID found in chaperone fork verification message.\n",
__FUNCTION__);
}
lpjs_log("%s(): Job %lu chaperone_pid = %d\n", __FUNCTION__,
job_get_job_id(job), chaperone_pid);
job_set_chaperone_pid(job, chaperone_pid);
/*
* Reserve resources immediately to prevent a race
* between chaperone checkin and new job submissions.
* FIXME: This will need adjustment for MPI jobs at the least
*/
node_adjust_resources(node, job, NODE_RESOURCE_ALLOCATE);
}
}
/*
* Log submission time and job specs
*/
free(matched_nodes);
}
return node_count;
}
/***************************************************************************
* Description:
* Check available nodes and the job queue, and dispatch as many new
* jobs as possible. This should be called following any changes
* to the job queue (new submissions, completed jobs), and when
* a new node is added. I.e. whenever it might become possible
* to start new jobs.
*
* Returns:
* The number of jobs dispatched (0 or 1), or a negative error
* code if something went wrong.
*
* History:
* Date Name Modification
* 2024-01-29 Jason Bacon Begin
***************************************************************************/
int lpjs_dispatch_jobs(node_list_t *node_list,
job_list_t *pending_jobs,
job_list_t *running_jobs)
{
int nodes;
// Dispatch as many jobs as possible before resuming
while ( (nodes = lpjs_dispatch_next_job(node_list, pending_jobs,
running_jobs)) > 0 )
lpjs_log("%s(): %d nodes available.\n", __FUNCTION__, nodes);
return 0;
}
/***************************************************************************
* Description:
* Examine the spooled jobs, if any, and determine the next one
* to be dispatched.
*
* Returns:
* The number of jobs selected (0 or 1)
*
* History:
* Date Name Modification
* 2024-01-29 Jason Bacon Begin
***************************************************************************/
unsigned long lpjs_select_next_job(job_list_t *pending_jobs, job_t **job)
{
unsigned long low_job_id;
extern FILE *Log_stream;
size_t c;
job_t *temp_job;
if ( job_list_get_count(pending_jobs) == 0 )
return 0;
else
{
for (c = 0; c < job_list_get_count(pending_jobs); ++c)
{
temp_job = job_list_get_jobs_ae(pending_jobs, c);
if ( job_get_state(temp_job) == JOB_STATE_PENDING )
// Found a pending job not yet dispatched
break;
}
if ( c == job_list_get_count(pending_jobs) )
{
lpjs_log("%s(): All jobs already dispatched.\n", __FUNCTION__);
return 0;
}
*job = job_list_get_jobs_ae(pending_jobs, c);
low_job_id = job_get_job_id(*job);
lpjs_log("%s(): Selected job %lu to dispatch.\n",
__FUNCTION__, low_job_id);
job_print_full_specs(*job, Log_stream);
return low_job_id;
}
}
/***************************************************************************
* Description:
*
* History:
* Date Name Modification
* 2024-02-23 Jason Bacon Begin
***************************************************************************/
int lpjs_match_nodes(job_t *job, node_list_t *node_list,
node_list_t *matched_nodes)
{
node_t *node;
unsigned node_count,
c,
usable_processors, // Procs with enough mem
total_usable,
total_required;
lpjs_log("%s(): Job %u requires %u processors, %lu MiB / proc.\n",
__FUNCTION__,
job_get_job_id(job), job_get_threads_per_process(job),
job_get_phys_mib_per_processor(job));
total_usable = 0;
total_required = job_get_processors_per_job(job);
for (c = node_count = 0;
(c < node_list_get_compute_node_count(node_list)) &&
(total_usable < total_required); ++c)
{
node = node_list_get_compute_nodes_ae(node_list, c);
if ( strcmp(node_get_state(node), "up") != 0 )
lpjs_log("%s(): %s is unavailable.\n",
__FUNCTION__, node_get_hostname(node));
else
{
// lpjs_log("Checking %s...\n", node_get_hostname(node));
usable_processors = lpjs_get_usable_processors(job, node);
usable_processors = XT_MIN(usable_processors, total_required - total_usable);
if ( usable_processors > 0 )
{
lpjs_log("%s(): Using %u processors on %s.\n", __FUNCTION__,
usable_processors, node_get_hostname(node));
// FIXME: Set # processors to use on node
node_list_add_compute_node(matched_nodes, node);
total_usable += usable_processors;
++node_count;
}
}
}
if ( total_usable == total_required )
{
lpjs_log("%s(): Using nodes:\n", __FUNCTION__);
for (c = 0; c < node_list_get_compute_node_count(matched_nodes); ++c)
{
node = node_list_get_compute_nodes_ae(matched_nodes, c);
lpjs_log("%s(): %s\n", __FUNCTION__, node_get_hostname(node));
}
}
else
lpjs_log("%s(): Insufficient resources available.\n", __FUNCTION__);
return node_count;
}
/***************************************************************************
* Description:
*
* History:
* Date Name Modification
* 2024-02-23 Jason Bacon Begin
***************************************************************************/
int lpjs_get_usable_processors(job_t *job, node_t *node)
{
int required_processors,
available_processors, // Total free
usable_processors; // Total free with enough mem
size_t available_mem;
required_processors = job_get_threads_per_process(job);
available_mem = node_get_phys_MiB_available(node);
available_processors = node_get_processors(node) - node_get_processors_used(node);
lpjs_log("%s(): %s: processors = %u mem = %lu\n", __FUNCTION__,
node_get_hostname(node), available_processors, available_mem);
if ( available_processors >= required_processors )
{
if ( (available_mem >= job_get_phys_mib_per_processor(job) * required_processors) )
usable_processors = required_processors;
else
{
lpjs_log("%s(): Not enough memory available.\n", __FUNCTION__);
usable_processors = 0;
}
}
else
{
lpjs_log("%s(): Not enough processors available.\n", __FUNCTION__);
usable_processors = 0;
}
return usable_processors;
}
/***************************************************************************
* Description:
*
* Returns:
*
* History:
* Date Name Modification
* 2024-05-03 Jason Bacon Begin
***************************************************************************/
job_t *lpjs_remove_pending_job(job_list_t *pending_jobs, unsigned long job_id)
{
char pending_path[PATH_MAX + 1];
pid_t pid;
int status;
if ( (pid = fork()) == 0 )
{
snprintf(pending_path, PATH_MAX + 1, "%s/%lu",
LPJS_PENDING_DIR, job_id);
lpjs_log("%s(): Removing job %s...\n", __FUNCTION__, pending_path);
execlp("rm", "rm", "-rf", pending_path, NULL);
lpjs_log("%s(): Error: exec(rm -rf %s) failed.\n", __FUNCTION__, pending_path);
exit(EX_OSERR);
}
else
{
// WEXITED is implicitly set for waitpid(), but specify for readability
waitpid(pid, &status, WEXITED);
if ( status != 0 )
lpjs_log("%s(): rm failed, status = %d.\n", __FUNCTION__, status);
}
return job_list_remove_job(pending_jobs, job_id);
}
// FIXME: merge this with above
job_t *lpjs_remove_running_job(job_list_t *running_jobs, unsigned long job_id)
{
char running_path[PATH_MAX + 1];
pid_t pid;
int status;
if ( (pid = fork()) == 0 )
{
snprintf(running_path, PATH_MAX + 1, "%s/%lu",
LPJS_RUNNING_DIR, job_id);
lpjs_log("%s(): Removing job %s...\n", __FUNCTION__, running_path);
execlp("rm", "rm", "-rf", running_path, NULL);
lpjs_log("%s(): exec(rm -rf %s) failed.\n", __FUNCTION__, running_path);
exit(EX_OSERR);
}
else
{
// WEXITED is implicitly set for waitpid(), but specify for readability
waitpid(pid, &status, WEXITED);
if ( status != 0 )
lpjs_log("%s(): rm failed, status = %d.\n", __FUNCTION__, status);
}
return job_list_remove_job(running_jobs, job_id);
}