Skip to content

Commit b91c59c

Browse files
committed
shell: add hwloc plugin with support for -o hwloc.xmlfile
Problem: Some use cases may benefit from stashing the job shell's copy of hwloc in a file referenced by HWLOC_XMLFILE, which causes hwloc to load topology from a file instead of discovering the topology from scratch. Add a new hwloc builtin plugin for handling optional behavior related to hwloc. Add initial support for writing shell XML to a file and setting HWLOC_XMLFILE if `-o hwloc.xmlfile` is used. Other `hwloc.` keys are reserved for possible future use. The plugin checks for hwloc.xmlfile in shell.post-init, which allows other shell plugins to set this option if required during shell.init or before without worrying about plugin loading order.
1 parent 7615ec5 commit b91c59c

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

src/shell/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ flux_shell_SOURCES = \
9292
cyclic.c \
9393
signal.c \
9494
files.c \
95-
oom.c
95+
oom.c \
96+
hwloc.c
9697

9798
flux_shell_LDADD = \
9899
$(builddir)/libshell.la \

src/shell/builtins.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ extern struct shell_builtin builtin_rlimit;
5151
extern struct shell_builtin builtin_cyclic;
5252
extern struct shell_builtin builtin_signal;
5353
extern struct shell_builtin builtin_oom;
54+
extern struct shell_builtin builtin_hwloc;
5455

5556
static struct shell_builtin * builtins [] = {
5657
&builtin_tmpdir,
@@ -74,6 +75,7 @@ static struct shell_builtin * builtins [] = {
7475
&builtin_cyclic,
7576
&builtin_signal,
7677
&builtin_oom,
78+
&builtin_hwloc,
7779
&builtin_list_end,
7880
};
7981

src/shell/hwloc.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/************************************************************\
2+
* Copyright 2024 Lawrence Livermore National Security, LLC
3+
* (c.f. AUTHORS, NOTICE.LLNS, COPYING)
4+
*
5+
* This file is part of the Flux resource manager framework.
6+
* For details, see https://github.com/flux-framework.
7+
*
8+
* SPDX-License-Identifier: LGPL-3.0
9+
\************************************************************/
10+
11+
/* hwloc options handler
12+
*/
13+
#define FLUX_SHELL_PLUGIN_NAME "hwloc"
14+
15+
#if HAVE_CONFIG_H
16+
#include "config.h"
17+
#endif
18+
19+
#include <sys/types.h>
20+
#include <sys/stat.h>
21+
#include <fcntl.h>
22+
#include <unistd.h>
23+
24+
#include <flux/core.h>
25+
#include <flux/shell.h>
26+
27+
#include "src/common/libutil/read_all.h"
28+
#include "ccan/str/str.h"
29+
30+
#include "builtins.h"
31+
32+
static int create_xmlfile (flux_shell_t *shell)
33+
{
34+
int fd = -1;
35+
char *xmlfile = NULL;
36+
const char *hwloc_xml;
37+
const char *tmpdir;
38+
39+
if (!(tmpdir = flux_shell_getenv (shell, "FLUX_JOB_TMPDIR")))
40+
tmpdir = "/tmp";
41+
42+
if (flux_shell_get_hwloc_xml (shell, &hwloc_xml) < 0) {
43+
shell_log_error ("failed to get shell hwloc xml");
44+
goto error;
45+
}
46+
if (asprintf (&xmlfile, "%s/hwloc.xml", tmpdir) < 0) {
47+
shell_log_error ("asprintf HWLOC_XMLFILE failed");
48+
goto error;
49+
}
50+
if ((fd = open (xmlfile, O_CREAT|O_EXCL|O_WRONLY, 0640)) < 0) {
51+
shell_log_errno ("%s", xmlfile);
52+
goto error;
53+
}
54+
shell_debug ("Writing %ld bytes to HWLOC_XMLFILE=%s\n",
55+
(long int) strlen (hwloc_xml),
56+
xmlfile);
57+
if (write_all (fd, hwloc_xml, strlen (hwloc_xml)) < 0 || close (fd) < 0) {
58+
shell_log_errno ("failed to write HWLOC_XMLFILE");
59+
goto error;
60+
}
61+
if (flux_shell_setenvf (shell, 0, "HWLOC_XMLFILE", "%s", xmlfile) < 0) {
62+
shell_log_errno ("failed to set HWLOC_XMLFILE in job environment");
63+
goto error;
64+
}
65+
return 0;
66+
error:
67+
if (fd >= 0)
68+
close (fd);
69+
free (xmlfile);
70+
return -1;
71+
}
72+
73+
static int hwloc_post_init (flux_plugin_t *p,
74+
const char *topic,
75+
flux_plugin_arg_t *args,
76+
void *data)
77+
{
78+
flux_shell_t *shell = flux_plugin_get_shell (p);
79+
int xmlfile = 0;
80+
81+
if (flux_shell_getopt_unpack (shell,
82+
"hwloc",
83+
"{s?i}",
84+
"xmlfile", &xmlfile) < 0)
85+
return shell_log_errno ("failed to unpack hwloc options");
86+
87+
if (xmlfile && create_xmlfile (shell) < 0)
88+
return shell_log_errno ("failed to write HWLOC_XMLFILE");
89+
return 0;
90+
}
91+
92+
struct shell_builtin builtin_hwloc = {
93+
.name = FLUX_SHELL_PLUGIN_NAME,
94+
.post_init = hwloc_post_init,
95+
};
96+
97+
/*
98+
* vi:tabstop=4 shiftwidth=4 expandtab
99+
*/

0 commit comments

Comments
 (0)