Skip to content

Commit 6206d16

Browse files
committed
hwloc-ps: add --lstopo-misc to output something for --lstopo --misc-from
Closes #606 Signed-off-by: Brice Goglin <[email protected]>
1 parent 237ddc1 commit 6206d16

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

contrib/completion/bash/hwloc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ _hwloc_ps(){
493493
--pid-cmd
494494
--short-name
495495
--disallowed --whole-system
496+
--lstopo-misc
496497
--json-server
497498
--json-port
498499
-v --verbose

utils/hwloc/hwloc-ps.1in

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ in each process and display it at the end of the line.
8686
On Linux, try to find the process MPI rank (by querying some widespread
8787
environment variables) and display it at the end of the line.
8888

89+
.TP
90+
\fB\-\-lstopo\-misc\fR <file>
91+
Output a file that may be given to \fBlstopo \-\-misc\-from\fR for displaying
92+
processes/threads as Misc objects. See EXAMPLES below.
8993
.TP
9094
\fB\-\-json\-server\fR
9195
Run the tool as a JSON server that waits for other process' requests
@@ -175,6 +179,27 @@ it only appears in the thread-aware output (or if explicitly selected):
175179
$ hwloc-ps --pid 4759
176180
4759 Machine:0 myprogram
177181

182+
The output may be a file that lstopo uses for adding Misc objects
183+
(more flexible version of lstopo --top):
184+
185+
$ hwloc-ps --misc-from foo
186+
$ cat foo
187+
name=12444 myprogram
188+
cpuset=0x000000f0
189+
subtype=Process
190+
191+
name=12444 mythread1
192+
cpuset=0x00000050
193+
subtype=Thread
194+
195+
name=12444 mythread2
196+
cpuset=0x000000a0
197+
subtype=Thread
198+
199+
This may be directly given to lstopo:
200+
201+
$ hwloc-ps --misc-from - | lstopo --misc-from -
202+
178203
On Linux, hwloc-ps may also display some process specific environment
179204
variable at the end of the line. This is for instance useful
180205
for identify MPI ranks among processes:

utils/hwloc/hwloc-ps.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ static int json_server = 0;
4141
static int json_port = JSON_PORT;
4242
static FILE *json_output = NULL;
4343
static int verbose = 0;
44+
static FILE *lstopo_misc_output = NULL;
4445

4546
void usage(const char *name, FILE *where)
4647
{
@@ -66,6 +67,7 @@ void usage(const char *name, FILE *where)
6667
fprintf (where, " --pid-cmd <cmd> Append the output of <cmd> <pid> to each PID line\n");
6768
fprintf (where, " --short-name Show only the process short name instead of the path\n");
6869
fprintf (where, " --disallowed Include objects disallowed by administrative limitations\n");
70+
fprintf (where, " --lstopo-misc <file> Output Misc object to be given to lstopo --misc-from <file>\n");
6971
fprintf (where, " --json-server Run as a JSON server\n");
7072
fprintf (where, " --json-port <n> Use port <n> for JSON server (default is %d)\n", JSON_PORT);
7173
fprintf (where, " -v --verbose Increase verbosity\n");
@@ -135,6 +137,45 @@ static void print_process(hwloc_topology_t topology,
135137
print_task(topology, proc->threads[i].tid, proc->threads[i].name, proc->threads[i].cpuset, NULL, 1);
136138
}
137139

140+
static void print_process_lstopo_misc(hwloc_topology_t topology __hwloc_attribute_unused,
141+
struct hwloc_ps_process *proc)
142+
{
143+
/* sort of similar to foreach_process_cb() in lstopo.c */
144+
char name[100];
145+
char *s;
146+
unsigned i;
147+
148+
snprintf(name, sizeof(name), "%ld", proc->pid);
149+
if (*proc->name)
150+
snprintf(name, sizeof(name), "%ld %s", proc->pid, proc->name);
151+
hwloc_bitmap_asprintf(&s, proc->cpuset);
152+
fprintf(lstopo_misc_output,
153+
"name=%s\n"
154+
"cpuset=%s\n"
155+
"subtype=Process\n"
156+
"\n",
157+
name, s);
158+
free(s);
159+
160+
if (proc->nthreads)
161+
for(i=0; i<proc->nthreads; i++)
162+
if (proc->threads[i].cpuset) {
163+
char task_name[150];
164+
if (*proc->threads[i].name)
165+
snprintf(task_name, sizeof(task_name), "%s %li %s", name, proc->threads[i].tid, proc->threads[i].name);
166+
else
167+
snprintf(task_name, sizeof(task_name), "%s %li", name, proc->threads[i].tid);
168+
hwloc_bitmap_asprintf(&s, proc->threads[i].cpuset);
169+
fprintf(lstopo_misc_output,
170+
"name=%s\n"
171+
"cpuset=%s\n"
172+
"subtype=Thread\n"
173+
"\n",
174+
task_name, s);
175+
free(s);
176+
}
177+
}
178+
138179
static void print_process_json(hwloc_topology_t topology,
139180
struct hwloc_ps_process *proc)
140181
{
@@ -207,6 +248,8 @@ static void foreach_process_cb(hwloc_topology_t topology,
207248

208249
if (json_output)
209250
print_process_json(topology, proc);
251+
else if (lstopo_misc_output)
252+
print_process_lstopo_misc(topology, proc);
210253
else
211254
print_process(topology, proc);
212255
}
@@ -241,6 +284,8 @@ static int run(hwloc_topology_t topology, hwloc_const_bitmap_t topocpuset,
241284

242285
if (json_output)
243286
print_process_json(topology, &proc);
287+
else if (lstopo_misc_output)
288+
print_process_lstopo_misc(topology, &proc);
244289
else
245290
print_process(topology, &proc);
246291
}
@@ -467,6 +512,21 @@ int main(int argc, char *argv[])
467512
pidcmd = argv[1];
468513
opt = 1;
469514

515+
} else if (!strcmp (argv[0], "--lstopo-misc")) {
516+
if (argc < 2) {
517+
usage(callname, stderr);
518+
exit(EXIT_FAILURE);
519+
}
520+
if (!strcmp(argv[1], "-"))
521+
lstopo_misc_output = stdout;
522+
else
523+
lstopo_misc_output = fopen(argv[1], "w");
524+
if (!lstopo_misc_output) {
525+
fprintf(stderr, "Failed to open --lstopo-misc output `%s' for writing (%s)\n", argv[1], strerror(errno));
526+
exit(EXIT_FAILURE);
527+
}
528+
opt = 1;
529+
470530
} else if (!strcmp (argv[0], "--json-server")) {
471531
json_server = 1;
472532
} else if (!strcmp (argv[0], "--json-port")) {
@@ -540,5 +600,7 @@ int main(int argc, char *argv[])
540600
out_with_topology:
541601
hwloc_topology_destroy(topology);
542602
out:
603+
if (lstopo_misc_output && lstopo_misc_output != stdout)
604+
fclose(lstopo_misc_output);
543605
return err;
544606
}

utils/lstopo/lstopo-no-graphics.1in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ machine are shown. On Linux, kernel threads are not shown.
331331
If many processes appear, the output may become hard to read anyway,
332332
making the hwloc-ps program more practical.
333333

334+
See \fB\-\-misc\-from\fR for a customizable variant using \fBhwloc-ps\fR.
334335
.TP
335336
\fB\-\-misc\-from\fR <file>
336337
Add Misc objects as described in <file> containing entries such as:
@@ -342,6 +343,8 @@ Add Misc objects as described in <file> containing entries such as:
342343
cpuset=0x7
343344
subtype=myOptionalSubtype
344345

346+
This is useful for combining with \fBhwloc-ps --lstopo-misc\fR (see EXAMPLES below)
347+
because hwloc-ps is far more customizable than lstopo's \fB\-\-top\fR option.
345348
.TP
346349
\fB\-\-children\-order\fR <order>
347350
Change the order of the different kinds of children with respect to
@@ -894,6 +897,11 @@ To count the number of logical processors in the system
894897
To append the kernel release and version to the graphical legend:
895898

896899
lstopo --append-legend "Kernel release: $(uname -r)" --append-legend "Kernel version: $(uname -v)"
900+
901+
To show where a process and its children are bound by combining with hwloc-ps:
902+
903+
hwloc-ps --pid-children 23 --lstopo-misc - | lstopo --misc-from -
904+
897905
.
898906
.\" **************************
899907
.\" Notes Section

utils/lstopo/lstopo.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ static void foreach_process_cb(hwloc_topology_t topology,
117117
struct hwloc_ps_process *proc,
118118
void *cbdata __hwloc_attribute_unused)
119119
{
120+
/* sort of similar to print_process_lstopo_misc() in hwloc-ps.c */
120121
char name[100];
121122
unsigned i;
122123

0 commit comments

Comments
 (0)