Skip to content

Commit

Permalink
lstopo: add --misc-from
Browse files Browse the repository at this point in the history
Allows to give a list of Misc objects in a separate file (or stdin),
will be used to bring hwloc-ps flexibility without adding many options to lstopo.

Refs #606

Signed-off-by: Brice Goglin <Brice.Goglin@inria.fr>
  • Loading branch information
bgoglin committed Aug 1, 2023
1 parent 64fdad2 commit 237ddc1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
1 change: 1 addition & 0 deletions contrib/completion/bash/hwloc
Expand Up @@ -80,6 +80,7 @@ _lstopo() {
--export-xml-flags
--export-synthetic-flags
--ps --top
--misc-from
--version
-h --help
)
Expand Down
12 changes: 12 additions & 0 deletions utils/lstopo/lstopo-no-graphics.1in
Expand Up @@ -330,6 +330,18 @@ cluttering the output, only processes that are restricted to some part of the
machine are shown. On Linux, kernel threads are not shown.
If many processes appear, the output may become hard to read anyway,
making the hwloc-ps program more practical.

.TP
\fB\-\-misc\-from\fR <file>
Add Misc objects as described in <file> containing entries such as:

name=myMisc1
cpuset=0x5

name=myMisc2
cpuset=0x7
subtype=myOptionalSubtype

.TP
\fB\-\-children\-order\fR <order>
Change the order of the different kinds of children with respect to
Expand Down
91 changes: 90 additions & 1 deletion utils/lstopo/lstopo.c
Expand Up @@ -154,6 +154,77 @@ static void add_process_objects(hwloc_topology_t topology)
HWLOC_PS_FLAG_THREADS | HWLOC_PS_FLAG_SHORTNAME, NULL, HWLOC_PS_ALL_UIDS);
}

static void add_one_misc_object_from(hwloc_topology_t topology,
char *subtype, char *name, hwloc_bitmap_t cpuset)
{
if (!hwloc_bitmap_iszero(cpuset) && subtype && name) {
insert_misc(topology, cpuset, subtype, name);
} else {
char *s;
hwloc_bitmap_asprintf(&s, cpuset);
fprintf(stderr, "Ignoring misc object subtype %s name %s cpuset %s\n", subtype, name, s);
free(s);
}
}

/* reads Misc description from the FILE*
* entries must look like:
* name=... (must be first)
* cpuset=... (cannot be 0)
* subtype=... (optional)
*/
static void add_misc_objects_from(hwloc_topology_t topology, FILE *from)
{
char line[256];
hwloc_bitmap_t cpuset;
char *subtype = NULL;
char *name = NULL;
cpuset = hwloc_bitmap_alloc();
if (!cpuset)
return;

while (fgets(line, sizeof line, from)) {
char *end;

/* remove ending \n */
end = strchr(line, '\n');
if (end)
*end = '\0';
/* ignoring empty lines */
if (line[0] == '\0')
continue;

if (!strncmp(line, "name=", 5)) {
/* commit (or ignore) the previous entry */
if (name)
add_one_misc_object_from(topology, subtype, name, cpuset);
/* start a new entry */
free(subtype);
subtype = NULL;
free(name);
name = strdup(line+5);
hwloc_bitmap_zero(cpuset);

} else if (!strncmp(line, "cpuset=", 7)) {
hwloc_bitmap_sscanf(cpuset, line+7);

} else if (!strncmp(line, "subtype=", 8)) {
free(subtype);
subtype = strdup(line+8);

} else {
fprintf(stderr, "Unrecognized --misc-from line `%s', ignored\n", line);
}
}

/* commit (or ignore) the last entry */
if (name)
add_one_misc_object_from(topology, subtype, name, cpuset);
free(name);
free(subtype);
hwloc_bitmap_free(cpuset);
}

static __hwloc_inline void lstopo_update_factorize_bounds(unsigned min, unsigned *first, unsigned *last)
{
switch (min) {
Expand Down Expand Up @@ -577,6 +648,7 @@ void usage(const char *name, FILE *where)
" Set flags during the synthetic topology export\n");
/* --shmem-output-addr is undocumented on purpose */
fprintf (where, " --ps --top Display processes within the hierarchy\n");
fprintf (where, " --misc-from <file> Create Misc objects as defined in <file>");
fprintf (where, " --version Report version and exit\n");
fprintf (where, " -h --help Show this usage\n");
}
Expand Down Expand Up @@ -838,6 +910,7 @@ main (int argc, char *argv[])
#endif
char *env;
int top = 0;
FILE * miscfrom = NULL;
int opt;
unsigned i;

Expand Down Expand Up @@ -1472,7 +1545,19 @@ main (int argc, char *argv[])
loutput.pid_number = atoi(argv[1]); opt = 1;
} else if (!strcmp (argv[0], "--ps") || !strcmp (argv[0], "--top"))
top = 1;
else if (!strcmp (argv[0], "--version")) {
else if (!strcmp (argv[0], "--misc-from")) {
if (argc < 2)
goto out_usagefailure;
if (!strcmp(argv[1], "-"))
miscfrom = stdin;
else
miscfrom = fopen(argv[1], "r");
if (!miscfrom) {
fprintf(stderr, "Failed open --misc-from %s file for reading (%s)\n", argv[1], strerror(errno));
exit(EXIT_FAILURE);
}
opt = 1;
} else if (!strcmp (argv[0], "--version")) {
printf("%s %s\n", callname, HWLOC_VERSION);
exit(EXIT_SUCCESS);
} else if (!strcmp (argv[0], "--output-format") || !strcmp (argv[0], "--of")) {
Expand Down Expand Up @@ -1815,6 +1900,8 @@ main (int argc, char *argv[])

if (top)
add_process_objects(topology);
if (miscfrom)
add_misc_objects_from(topology, miscfrom);

if (restrictstring) {
hwloc_bitmap_t restrictset = hwloc_bitmap_alloc();
Expand Down Expand Up @@ -1900,5 +1987,7 @@ main (int argc, char *argv[])
hwloc_bitmap_free(allow_nodeset);
hwloc_bitmap_free(loutput.cpubind_set);
hwloc_bitmap_free(loutput.membind_set);
if (miscfrom && miscfrom != stdin)
fclose(miscfrom);
return EXIT_FAILURE;
}

0 comments on commit 237ddc1

Please sign in to comment.