-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode.c
360 lines (295 loc) · 9.62 KB
/
node.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sysexits.h>
#include <sys/utsname.h>
#include <sys/socket.h> // MSG_WAIT_ALL
#include <xtend/file.h> // xt_dprintf()
#include "node-private.h"
#include "network.h"
#include "lpjs.h"
#include "misc.h"
/***************************************************************************
* Description:
*
* Returns:
*
* History:
* Date Name Modification
* 2024-02-01 Jason Bacon Begin
***************************************************************************/
node_t *node_new(void)
{
node_t *node;
if ( (node = malloc(sizeof(node_t))) == NULL )
{
lpjs_log("%s(): Error: malloc failed.\n", __FUNCTION__);
exit(EX_UNAVAILABLE);
}
node_init(node);
return node;
}
/***************************************************************************
* Description:
* Constructor for node_t
*
* History:
* Date Name Modification
* 2021-09-23 Jason Bacon Begin
***************************************************************************/
void node_init(node_t *node)
{
node->hostname = NULL;
node->phys_MiB = 0;
node->phys_MiB_used = 0;
node->processors = 0;
node->processors_used = 0;
node->zfs = 0;
node->os = "unknown";
node->arch = "unknown";
node->state = "offline";
node->msg_fd = NODE_MSG_FD_NOT_OPEN;
node->last_ping = 0;
}
/***************************************************************************
* Description:
* Detect hardware specs and OS of the node running this function
*
* History:
* Date Name Modification
* 2021-10-02 Jason Bacon Begin
***************************************************************************/
void node_detect_specs(node_t *node)
{
struct utsname u_name;
char temp_hostname[sysconf(_SC_HOST_NAME_MAX) + 1],
temp_osname[128],
*ostype_path;
FILE *fp;
struct stat st;
/*
* hwloc is extremely complex and we don't need most of its functionality
* here, so just gather info the simple way
*/
// FIXME: Verify malloc() success
gethostname(temp_hostname, sysconf(_SC_HOST_NAME_MAX));
// FIXME: Check malloc success
node->hostname = strdup(temp_hostname);
// May include SMT/Hyperthreading. Disable in BIOS for Linux or using
// sysctl on FreeBSD if you don't want to oversubscribe physical processors.
node->processors = sysconf(_SC_NPROCESSORS_ONLN);
node->phys_MiB = sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES)
/ 1024 / 1024;
/*
* Report 1 if ZFS filesystem found, so that additional memory
* can be reserved on compute nodes.
* FIXME: There should be a better approach to this.
*/
node->zfs = ! system("mount | fgrep -q zfs");
uname(&u_name);
ostype_path = LOCALBASE "/bin/auto-ostype";
if ( (stat(ostype_path, &st) == 0) && (fp = popen(ostype_path, "r")) != NULL )
{
xt_fgetline(fp, temp_osname, 128);
if ( (node->os = strdup(temp_osname)) == NULL )
{
lpjs_log("%s(): Error: strdup() failed.\n", __FUNCTION__);
exit(EX_UNAVAILABLE);
}
pclose(fp);
}
else
{
if ( (node->os = strdup(u_name.sysname)) == NULL )
{
lpjs_log("%s(): Error: strdup() failed.\n", __FUNCTION__);
exit(EX_UNAVAILABLE);
}
}
node->arch = strdup(u_name.machine);
}
/***************************************************************************
* Description:
* Print header for node status info
*
* History:
* Date Name Modification
* 2024-01-28 Jason Bacon Begin
***************************************************************************/
void node_print_status_header(FILE *stream)
{
fprintf(stderr, NODE_STATUS_HEADER_FORMAT, "Hostname", "State",
"Procs", "Used", "PhysMiB", "Used", "OS", "Arch");
}
/***************************************************************************
* Description:
* Print current nodes in human-readable form
*
* History:
* Date Name Modification
* 2021-09-23 Jason Bacon Begin
***************************************************************************/
void node_print_status(node_t *node, FILE *stream)
{
fprintf(stream, NODE_STATUS_FORMAT, node->hostname, node->state,
node->processors, node->processors_used,
node->phys_MiB, node->phys_MiB_used, node->os, node->arch);
}
void node_status_to_str(node_t *node, char *str, size_t array_size)
{
snprintf(str, array_size,
NODE_STATUS_FORMAT, node->hostname, node->state,
node->processors, node->processors_used,
node->phys_MiB, node->phys_MiB_used, node->os, node->arch);
}
/***************************************************************************
* Description:
* Send current node info to msg_fd in human-readable form, e.g. in
* response to lpjs-nodes
*
* History:
* Date Name Modification
* 2021-09-23 Jason Bacon Begin
***************************************************************************/
void node_send_status(node_t *node, int msg_fd)
{
char outgoing_msg[LPJS_MSG_LEN_MAX + 1];
node_status_to_str(node, outgoing_msg, LPJS_MSG_LEN_MAX + 1);
if ( lpjs_send_munge(msg_fd, outgoing_msg, close) != LPJS_MSG_SENT )
{
// This function should never be called by dispatchd, so
// use a simple close() vs lpjs_dispatchd_safe_close()
lpjs_log("%s(): Error: xt_dprintf() failed: %s", __FUNCTION__, strerror(errno));
exit(EX_IOERR);
}
}
int node_print_specs_header(FILE *stream)
{
return fprintf(stream,
"%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
"Hostname", "state", "processors",
"phys_MiB", "zfs", "os", "arch");
}
char *node_specs_to_str(node_t *node, char *str, size_t buff_len)
{
if ( snprintf(str, buff_len,
"%s\t%s\t%u\t%lu\t%u\t%s\t%s",
node->hostname, node->state, node->processors,
node->phys_MiB, node->zfs, node->os, node->arch) < 0 )
{
lpjs_log("%s(): Error: snprintf() failed\n", __FUNCTION__);
exit(EX_IOERR);
}
return str;
}
/***************************************************************************
* Description:
* Receive node hardware and OS specs via msg_fd in
* machine-readable for, e.g. from compd to dispatchd
*
* History:
* Date Name Modification
* 2021-10-02 Jason Bacon Begin
***************************************************************************/
ssize_t node_str_to_specs(node_t *node, const char *str)
{
char *temp_str,
*field,
*stringp,
*end;
node_init(node);
if ( (temp_str = strdup(str)) == NULL )
{
lpjs_log("%s(): Error: strdup() failed.\n", __FUNCTION__);
exit(EX_UNAVAILABLE);
}
node->state = "offline";
node->os = "unknown";
node->arch = "unknown";
stringp = temp_str;
if ( (field = strsep(&stringp, "\t")) == NULL )
{
lpjs_log("%s(): Bug: Failed to extract hostname from specs.\n", __FUNCTION__);
return -1;
}
node->hostname = strdup(field);
if ( (field = strsep(&stringp, "\t")) == NULL )
{
lpjs_log("%s(): Bug: Failed to extract state from specs.\n", __FUNCTION__);
return -1;
}
node->state = strdup(field);
if ( (field = strsep(&stringp, "\t")) == NULL )
{
lpjs_log("%s(): Bug: Failed to extract processors from specs.\n", __FUNCTION__);
return -1;
}
node->processors = strtoul(field, &end, 10);
if ( *end != '\0' )
{
lpjs_log("%s(): Bug: Procs field is not a valid number.\n", __FUNCTION__);
return -1;
}
if ( (field = strsep(&stringp, "\t")) == NULL )
{
lpjs_log("%s(): Bug: Failed to extract physMiB from specs.\n", __FUNCTION__);
return -1;
}
node->phys_MiB = strtoul(field, &end, 10);
if ( *end != '\0' )
{
lpjs_log("%s(): Bug: PhysMiB field is not a valid number.\n", __FUNCTION__);
return -1;
}
if ( (field = strsep(&stringp, "\t")) == NULL )
{
lpjs_log("%s(): Bug: Failed to extract ZFS Boolean from specs.\n", __FUNCTION__);
return -1;
}
node->zfs = strtoul(field, &end, 10);
if ( *end != '\0' )
{
lpjs_log("%s(): Bug: ZFS field is not a valid number (should be 0 or 1).\n", __FUNCTION__);
return -1;
}
if ( (field = strsep(&stringp, "\t")) == NULL )
{
lpjs_log("%s(): Bug: Failed to extract OS from specs.\n", __FUNCTION__);
return -1;
}
node->os = strdup(field);
if ( (field = strsep(&stringp, "\t\n")) == NULL )
{
lpjs_log("%s(): Bug: Failed to extract arch from specs.\n", __FUNCTION__);
return -1;
}
node->arch = strdup(field);
return 0;
}
/***************************************************************************
* Description:
* Release resources on node associated with job
*
* Arguments:
* node Pointer to node object
* job Pointer to job object
* direction NODE_RESOURCE_ALLOCATE | NODE_RESOURCE_RELEASE
*
* History:
* Date Name Modification
* 2024-12-08 Jason Bacon Begin
***************************************************************************/
int node_adjust_resources(node_t *node, job_t *job, node_resource_t direction)
{
// + for allocating, - for releasing
int processors = direction * job_get_processors_per_job(job);
long MiB = direction * job_get_phys_mib_per_processor(job) * job_get_processors_per_job(job);
lpjs_log("%s(): Allocating %d processors and %ld MiB on %s.\n",
__FUNCTION__, processors, MiB,
node_get_hostname(node));
node->processors_used += processors;
node->phys_MiB_used += MiB;
return 0; // FIXME: Define return codes
}