Debian Codegraph @

utils/lstopo/lstopo.c

raw
1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * Copyright © 2009 CNRS
4 * Copyright © 2009-2024 Inria. All rights reserved.
5 * Copyright © 2009-2012, 2015, 2017 Université Bordeaux
6 * Copyright © 2009-2011 Cisco Systems, Inc. All rights reserved.
7 * Copyright © 2020 Hewlett Packard Enterprise. All rights reserved.
8 * Copyright © 2023 Université de Reims Champagne-Ardenne. All rights reserved.
9 * See COPYING in top-level directory.
10 */
11
12 #include "private/autogen/config.h"
13 #include "hwloc.h"
14 #ifdef HWLOC_LINUX_SYS
15 #include "hwloc/linux.h"
16 #endif /* HWLOC_LINUX_SYS */
17 #include "hwloc/shmem.h"
18
19 #include "private/debug.h" /* for HWLOC_BUILD_ASSERT() */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 #ifdef HAVE_DIRENT_H
28 #include <dirent.h>
29 #endif
30 #include <fcntl.h>
31 #include <sys/stat.h>
32 #include <assert.h>
33 #ifdef HAVE_TIME_H
34 #include <time.h>
35 #endif
36
37 #ifdef LSTOPO_HAVE_GRAPHICS
38 #ifdef HWLOC_HAVE_CAIRO
39 #include <cairo.h>
40 #endif
41 #endif
42
43 #ifdef HAVE_SETLOCALE
44 #include <locale.h>
45 #endif
46
47 #include "lstopo.h"
48 #include "common-ps.h"
49 #include "misc.h"
50
51 #ifdef __MINGW32__
52 # ifdef HAVE_CLOCK_GETTIME
53 # undef HAVE_CLOCK_GETTIME
54 # endif
55 #endif
56
57 #ifdef HAVE_CLOCK_GETTIME
58 # ifndef CLOCK_MONOTONIC /* HP-UX doesn't have CLOCK_MONOTONIC */
59 # define CLOCK_MONOTONIC CLOCK_REALTIME
60 # endif
61 #endif
62
63 #ifdef ANDROID
64 extern void setJNIEnv();
65 #endif
66
67 FILE *open_output(const char *filename, int overwrite)
68 {
69 struct stat st;
70
71 if (!filename || !strcmp(filename, "-"))
72 return stdout;
73
74 if (!stat(filename, &st) && !overwrite) {
75 errno = EEXIST;
76 return NULL;
77 }
78
79 return fopen(filename, "w");
80 }
81
82 static hwloc_obj_t insert_misc(hwloc_topology_t topology, hwloc_cpuset_t cpuset, const char *subtype, const char * name)
83 {
84 hwloc_obj_t group, obj;
85
86 hwloc_bitmap_and(cpuset, cpuset, hwloc_topology_get_topology_cpuset(topology));
87 if (hwloc_bitmap_iszero(cpuset))
88 return NULL;
89
90 /* try to insert a group at exact position */
91 group = hwloc_topology_alloc_group_object(topology);
92 if (!group)
93 return NULL;
94 group->cpuset = hwloc_bitmap_dup(cpuset);
95 group->attr->group.kind = (unsigned) -1;
96 group = hwloc_topology_insert_group_object(topology, group);
97 if (!group) {
98 /* try to insert in a larger parent */
99 char *s, *gs;
100 hwloc_bitmap_asprintf(&s, cpuset);
101 group = hwloc_get_obj_covering_cpuset(topology, cpuset);
102 hwloc_bitmap_asprintf(&gs, group->cpuset);
103 fprintf(stderr, "%s `%s' binding %s doesn't match any object, extended to %s before inserting the object.\n",
104 subtype, name, s, gs);
105 free(s);
106 free(gs);
107 }
108 obj = hwloc_topology_insert_misc_object(topology, group, name);
109 if (!obj)
110 fprintf(stderr, "Failed to insert process `%s'\n", name);
111 else if (subtype)
112 obj->subtype = strdup(subtype);
113
114 return obj;
115 }
116
117 static void foreach_process_cb(hwloc_topology_t topology,
118 struct hwloc_ps_process *proc,
119 void *cbdata __hwloc_attribute_unused)
120 {
121 /* sort of similar to print_process_lstopo_misc() in hwloc-ps.c */
122 char name[100];
123 unsigned i;
124
125 snprintf(name, sizeof(name), "%ld", proc->pid);
126 if (*proc->name)
127 snprintf(name, sizeof(name), "%ld %s", proc->pid, proc->name);
128
129 if (proc->bound)
130 insert_misc(topology, proc->cpuset, "Process", name);
131
132 if (proc->nthreads)
133 for(i=0; i<proc->nthreads; i++)
134 if (proc->threads[i].cpuset
135 && !hwloc_bitmap_isequal(proc->threads[i].cpuset, proc->cpuset)) {
136 char task_name[150];
137 if (*proc->threads[i].name)
138 snprintf(task_name, sizeof(task_name), "%s %li %s", name, proc->threads[i].tid, proc->threads[i].name);
139 else
140 snprintf(task_name, sizeof(task_name), "%s %li", name, proc->threads[i].tid);
141
142 insert_misc(topology, proc->threads[i].cpuset, "Thread", task_name);
143 }
144 }
145
146 static void add_process_objects(hwloc_topology_t topology)
147 {
148 const struct hwloc_topology_support *support = hwloc_topology_get_support(topology);
149 hwloc_obj_t root = hwloc_get_root_obj(topology);
150
151 if (!support->cpubind->get_proc_cpubind)
152 return;
153
154 hwloc_ps_foreach_process(topology, root->cpuset,
155 foreach_process_cb, NULL,
156 HWLOC_PS_FLAG_THREADS | HWLOC_PS_FLAG_SHORTNAME, NULL, HWLOC_PS_ALL_UIDS);
157 }
158
159 static void add_one_misc_object_from(hwloc_topology_t topology,
160 char *subtype, char *name, hwloc_bitmap_t cpuset)
161 {
162 if (!hwloc_bitmap_iszero(cpuset) && subtype && name) {
163 insert_misc(topology, cpuset, subtype, name);
164 } else {
165 char *s;
166 hwloc_bitmap_asprintf(&s, cpuset);
167 fprintf(stderr, "Ignoring misc object subtype %s name %s cpuset %s\n", subtype, name, s);
168 free(s);
169 }
170 }
171
172 /* reads Misc description from the FILE*
173 * entries must look like:
174 * name=... (must be first)
175 * cpuset=... (cannot be 0)
176 * subtype=... (optional)
177 */
178 static void add_misc_objects_from(hwloc_topology_t topology, FILE *from)
179 {
180 char line[256];
181 hwloc_bitmap_t cpuset;
182 char *subtype = NULL;
183 char *name = NULL;
184 cpuset = hwloc_bitmap_alloc();
185 if (!cpuset)
186 return;
187
188 while (fgets(line, sizeof line, from)) {
189 char *end;
190
191 /* remove ending \n */
192 end = strchr(line, '\n');
193 if (end)
194 *end = '\0';
195 /* ignoring empty lines */
196 if (line[0] == '\0')
197 continue;
198
199 if (!strncmp(line, "name=", 5)) {
200 /* commit (or ignore) the previous entry */
201 if (name)
202 add_one_misc_object_from(topology, subtype, name, cpuset);
203 /* start a new entry */
204 free(subtype);
205 subtype = NULL;
206 free(name);
207 name = strdup(line+5);
208 hwloc_bitmap_zero(cpuset);
209
210 } else if (!strncmp(line, "cpuset=", 7)) {
211 hwloc_bitmap_sscanf(cpuset, line+7);
212
213 } else if (!strncmp(line, "subtype=", 8)) {
214 free(subtype);
215 subtype = strdup(line+8);
216
217 } else {
218 fprintf(stderr, "Unrecognized --misc-from line `%s', ignored\n", line);
219 }
220 }
221
222 /* commit (or ignore) the last entry */
223 if (name)
224 add_one_misc_object_from(topology, subtype, name, cpuset);
225 free(name);
226 free(subtype);
227 hwloc_bitmap_free(cpuset);
228 }
229
230 static __hwloc_inline void lstopo_update_factorize_bounds(unsigned min, unsigned *first, unsigned *last)
231 {
232 switch (min) {
233 case 0:
234 case 1:
235 case 2:
236 *first = 1;
237 *last = 0;
238 break;
239 case 3:
240 *first = 1;
241 *last = 1;
242 break;
243 default:
244 *first = 2;
245 *last = 1;
246 break;
247 }
248 }
249
250 static __hwloc_inline void lstopo_update_factorize_alltypes_bounds(struct lstopo_output *loutput)
251 {
252 hwloc_obj_type_t type;
253 for(type = 0; type < HWLOC_OBJ_TYPE_MAX; type++)
254 lstopo_update_factorize_bounds(loutput->factorize_min[type], &loutput->factorize_first[type], &loutput->factorize_last[type]);
255 }
256
257 static void
258 lstopo_add_factorized_attributes(struct lstopo_output *loutput, hwloc_topology_t topology, hwloc_obj_t obj)
259 {
260 hwloc_obj_t child;
261
262 if (!obj->first_child)
263 return;
264
265 if (obj->symmetric_subtree && obj->arity > loutput->factorize_min[obj->first_child->type]){
266 int may_factorize = 1;
267 /* check that the object is in a single cpukind */
268 if (loutput->nr_cpukind_styles) {
269 int err = hwloc_cpukinds_get_by_cpuset(topology, obj->cpuset, 0);
270 if (err < 0 && errno == EXDEV)
271 may_factorize = 0;
272 }
273 if (may_factorize) {
274 /* factorize those children */
275 for_each_child(child, obj) {
276 unsigned factorized;
277 if (child->sibling_rank < loutput->factorize_first[child->type]
278 || child->sibling_rank >= obj->arity - loutput->factorize_last[child->type])
279 factorized = 0; /* keep first and last */
280 else if (child->sibling_rank == loutput->factorize_first[child->type])
281 factorized = 1; /* replace with dots */
282 else
283 factorized = -1; /* remove that one */
284 ((struct lstopo_obj_userdata *)child->userdata)->factorized = factorized;
285 }
286 }
287 }
288 /* recurse */
289 for_each_child(child, obj)
290 lstopo_add_factorized_attributes(loutput, topology, child);
291 }
292
293 static void
294 lstopo_add_collapse_attributes(hwloc_topology_t topology)
295 {
296 hwloc_obj_t obj, collapser = NULL;
297 unsigned collapsed = 0;
298 /* collapse identical PCI devs */
299 for(obj = hwloc_get_next_pcidev(topology, NULL); obj; obj = hwloc_get_next_pcidev(topology, obj)) {
300 if (collapser) {
301 if (!obj->io_arity && !obj->misc_arity
302 && obj->parent == collapser->parent
303 && obj->attr->pcidev.vendor_id == collapser->attr->pcidev.vendor_id
304 && obj->attr->pcidev.device_id == collapser->attr->pcidev.device_id
305 && obj->attr->pcidev.subvendor_id == collapser->attr->pcidev.subvendor_id
306 && obj->attr->pcidev.subdevice_id == collapser->attr->pcidev.subdevice_id) {
307 /* collapse another one */
308 ((struct lstopo_obj_userdata *)obj->userdata)->pci_collapsed = -1;
309 collapsed++;
310 continue;
311 } else if (collapsed > 1) {
312 /* end this collapsing */
313 ((struct lstopo_obj_userdata *)collapser->userdata)->pci_collapsed = collapsed;
314 collapser = NULL;
315 collapsed = 0;
316 }
317 }
318 if (!obj->io_arity && !obj->misc_arity) {
319 /* start a new collapsing */
320 collapser = obj;
321 collapsed = 1;
322 }
323 }
324 if (collapsed > 1) {
325 /* end this collapsing */
326 ((struct lstopo_obj_userdata *)collapser->userdata)->pci_collapsed = collapsed;
327 }
328 }
329
330 static void
331 lstopo_add_cpukind_style(struct lstopo_output *loutput, hwloc_topology_t topology)
332 {
333 unsigned i, nr;
334 hwloc_bitmap_t cpuset = hwloc_bitmap_alloc();
335 if (!cpuset)
336 return;
337 nr = hwloc_cpukinds_get_nr(topology, 0);
338 for(i=0; i<nr; i++) {
339 hwloc_obj_t obj;
340 hwloc_cpukinds_get_info(topology, i, cpuset, NULL, NULL, NULL, 0);
341 obj = NULL;
342 while ((obj = hwloc_get_next_obj_inside_cpuset_by_type(topology, cpuset, HWLOC_OBJ_PU, obj)) != NULL)
343 ((struct lstopo_obj_userdata *)obj->userdata)->cpukind_style = i;
344 }
345 hwloc_bitmap_free(cpuset);
346
347 loutput->nr_cpukind_styles = nr;
348 }
349
350 static int
351 lstopo_check_pci_domains(hwloc_topology_t topology)
352 {
353 hwloc_obj_t obj;
354
355 /* check PCI devices for domains.
356 * they are listed by depth-first search, the order doesn't guarantee a domain at the end.
357 */
358 obj = NULL;
359 while ((obj = hwloc_get_next_pcidev(topology, obj)) != NULL) {
360 if (obj->attr->pcidev.domain)
361 return 1;
362 }
363
364 /* check PCI Bridges for domains.
365 * they are listed by depth-first search, the order doesn't guarantee a domain at the end.
366 */
367 obj = NULL;
368 while ((obj = hwloc_get_next_bridge(topology, obj)) != NULL) {
369 if (obj->attr->bridge.upstream_type != HWLOC_OBJ_BRIDGE_PCI)
370 break;
371 if (obj->attr->pcidev.domain)
372 return 1;
373 }
374
375 return 0;
376 }
377
378 static void
379 lstopo_parse_children_order(char *s, unsigned *children_order_p,
380 enum lstopo_orient_e *above_force_orient_p,
381 enum lstopo_orient_e *right_force_orient_p,
382 enum lstopo_orient_e *below_force_orient_p)
383 {
384 char *tmp, *next;
385 unsigned children_order;
386 enum lstopo_orient_e above_force_orient = LSTOPO_ORIENT_NONE;
387 enum lstopo_orient_e right_force_orient = LSTOPO_ORIENT_NONE;
388 enum lstopo_orient_e below_force_orient= LSTOPO_ORIENT_NONE;
389
390 if (!strcmp(s, "plain")) {
391 *children_order_p = LSTOPO_ORDER_PLAIN;
392 return;
393 }
394
395 tmp = s;
396 children_order = LSTOPO_ORDER_PLAIN;
397 while (tmp && *tmp) {
398 next = strchr(tmp, ',');
399 if (next) {
400 *next = 0;
401 next++;
402 }
403
404 if (!strcmp(tmp, "memory:above") || !strcmp(tmp, "memoryabove") /* backward compat with 2.5 */) {
405 children_order |= LSTOPO_ORDER_MEMORY_ABOVE;
406 } else if (!strcmp(tmp, "memory:above:horiz")) {
407 children_order |= LSTOPO_ORDER_MEMORY_ABOVE;
408 above_force_orient = LSTOPO_ORIENT_HORIZ;
409 } else if (!strcmp(tmp, "memory:above:vert")) {
410 children_order |= LSTOPO_ORDER_MEMORY_ABOVE;
411 above_force_orient = LSTOPO_ORIENT_VERT;
412 } else if (!strcmp(tmp, "memory:above:rect")) {
413 children_order |= LSTOPO_ORDER_MEMORY_ABOVE;
414 above_force_orient = LSTOPO_ORIENT_RECT;
415
416 } else if (!strcmp(tmp, "io:right")) {
417 children_order |= LSTOPO_ORDER_IO_RIGHT;
418 } else if (!strcmp(tmp, "io:right:horiz")) {
419 children_order |= LSTOPO_ORDER_IO_RIGHT;
420 right_force_orient = LSTOPO_ORIENT_HORIZ;
421 } else if (!strcmp(tmp, "io:right:vert")) {
422 children_order |= LSTOPO_ORDER_IO_RIGHT;
423 right_force_orient = LSTOPO_ORIENT_VERT;
424 } else if (!strcmp(tmp, "io:right:rect")) {
425 children_order |= LSTOPO_ORDER_IO_RIGHT;
426 right_force_orient = LSTOPO_ORIENT_RECT;
427
428 } else if (!strcmp(tmp, "io:below")) {
429 children_order |= LSTOPO_ORDER_IO_BELOW;
430 } else if (!strcmp(tmp, "io:below:horiz")) {
431 children_order |= LSTOPO_ORDER_IO_BELOW;
432 below_force_orient = LSTOPO_ORIENT_HORIZ;
433 } else if (!strcmp(tmp, "io:below:vert")) {
434 children_order |= LSTOPO_ORDER_IO_BELOW;
435 below_force_orient = LSTOPO_ORIENT_VERT;
436 } else if (!strcmp(tmp, "io:below:rect")) {
437 children_order |= LSTOPO_ORDER_IO_BELOW;
438 below_force_orient = LSTOPO_ORIENT_RECT;
439
440 } else if (!strcmp(tmp, "misc:right")) {
441 children_order |= LSTOPO_ORDER_MISC_RIGHT;
442 } else if (!strcmp(tmp, "misc:right:horiz")) {
443 children_order |= LSTOPO_ORDER_MISC_RIGHT;
444 right_force_orient = LSTOPO_ORIENT_HORIZ;
445 } else if (!strcmp(tmp, "misc:right:vert")) {
446 children_order |= LSTOPO_ORDER_MISC_RIGHT;
447 right_force_orient = LSTOPO_ORIENT_VERT;
448 } else if (!strcmp(tmp, "misc:right:rect")) {
449 children_order |= LSTOPO_ORDER_MISC_RIGHT;
450 right_force_orient = LSTOPO_ORIENT_RECT;
451
452 } else if (!strcmp(tmp, "misc:below")) {
453 children_order |= LSTOPO_ORDER_MISC_BELOW;
454 } else if (!strcmp(tmp, "misc:below:horiz")) {
455 children_order |= LSTOPO_ORDER_MISC_BELOW;
456 below_force_orient = LSTOPO_ORIENT_HORIZ;
457 } else if (!strcmp(tmp, "misc:below:vert")) {
458 children_order |= LSTOPO_ORDER_MISC_BELOW;
459 below_force_orient = LSTOPO_ORIENT_VERT;
460 } else if (!strcmp(tmp, "misc:below:rect")) {
461 children_order |= LSTOPO_ORDER_MISC_BELOW;
462 below_force_orient = LSTOPO_ORIENT_RECT;
463
464 } else if (strcmp(tmp, "plain")) {
465 fprintf(stderr, "Unsupported children order `%s', ignoring.\n", tmp);
466 }
467
468 tmp = next;
469 }
470
471 *children_order_p = children_order;
472 *above_force_orient_p = above_force_orient;
473 *right_force_orient_p = right_force_orient;
474 *below_force_orient_p = below_force_orient;
475 }
476
477 static void
478 lstopo_populate_userdata(hwloc_obj_t parent)
479 {
480 hwloc_obj_t child;
481 struct lstopo_obj_userdata *save = malloc(sizeof(*save));
482
483 save->common.buffer = NULL; /* so that it is ignored on XML export */
484 save->common.next = parent->userdata;
485 save->factorized = 0;
486 save->pci_collapsed = 0;
487 save->cpukind_style = 0;
488 parent->userdata = save;
489
490 for_each_child(child, parent)
491 lstopo_populate_userdata(child);
492 for_each_memory_child(child, parent)
493 lstopo_populate_userdata(child);
494 for_each_io_child(child, parent)
495 lstopo_populate_userdata(child);
496 for_each_misc_child(child, parent)
497 lstopo_populate_userdata(child);
498 }
499
500 static void
501 lstopo_destroy_userdata(hwloc_obj_t parent)
502 {
503 hwloc_obj_t child;
504 struct lstopo_obj_userdata *save = parent->userdata;
505
506 if (save) {
507 parent->userdata = save->common.next;
508 free(save);
509 }
510
511 for_each_child(child, parent)
512 lstopo_destroy_userdata(child);
513 for_each_memory_child(child, parent)
514 lstopo_destroy_userdata(child);
515 for_each_io_child(child, parent)
516 lstopo_destroy_userdata(child);
517 for_each_misc_child(child, parent)
518 lstopo_destroy_userdata(child);
519 }
520
521 void usage(const char *name, FILE *where)
522 {
523 fprintf (where, "Usage: %s [ options ] ... [ filename.format ]\n\n", name);
524 fprintf (where, "See lstopo(1) for more details.\n");
525
526 fprintf (where, "\nDefault output is "
527 #if (defined LSTOPO_HAVE_GRAPHICS) && (defined HWLOC_WIN_SYS)
528 "graphical window"
529 #elif (defined LSTOPO_HAVE_GRAPHICS) && (defined LSTOPO_HAVE_X11)
530 "graphical window (X11) if DISPLAY is set, console otherwise"
531 #else
532 "console"
533 #endif
534 ".\n");
535
536 fprintf (where, "Supported output file formats: console, ascii, tikz, fig"
537 #ifdef LSTOPO_HAVE_GRAPHICS
538 #ifdef CAIRO_HAS_PDF_SURFACE
539 ", pdf"
540 #endif /* CAIRO_HAS_PDF_SURFACE */
541 #ifdef CAIRO_HAS_PS_SURFACE
542 ", ps"
543 #endif /* CAIRO_HAS_PS_SURFACE */
544 #ifdef CAIRO_HAS_PNG_FUNCTIONS
545 ", png"
546 #endif /* CAIRO_HAS_PNG_FUNCTIONS */
547 #ifdef CAIRO_HAS_SVG_SURFACE
548 ", svg(cairo,native)"
549 #endif /* CAIRO_HAS_SVG_SURFACE */
550 #endif /* LSTOPO_HAVE_GRAPHICS */
551 #if !(defined LSTOPO_HAVE_GRAPHICS) || !(defined CAIRO_HAS_SVG_SURFACE)
552 ", svg(native)"
553 #endif
554 ", xml, synthetic"
555 "\n");
556 fprintf (where, "\nFormatting options:\n");
557 fprintf (where, " -l --logical Display hwloc logical object indexes\n");
558 fprintf (where, " -p --physical Display OS/physical object indexes\n");
559 fprintf (where, "Output options:\n");
560 fprintf (where, " --output-format <format>\n");
561 fprintf (where, " --of <format> Force the output to use the given format\n");
562 fprintf (where, " -f --force Overwrite the output file if it exists\n");
563 fprintf (where, "Textual output options:\n");
564 fprintf (where, " --only <type> Only show objects of the given type in the textual output\n");
565 fprintf (where, " -v --verbose Include additional details\n");
566 fprintf (where, " -q --quiet -s Reduce the amount of details to show\n");
567 fprintf (where, " --distances Only show distance matrices\n");
568 fprintf (where, " --distances-transform <links|merge-switch-ports|transitive-closure>\n");
569 fprintf (where, " Transform distances before displaying them\n");
570 fprintf (where, " --memattrs Only show memory attributes\n");
571 fprintf (where, " --cpukinds Only show CPU kinds\n");
572 #ifdef HWLOC_WIN_SYS
573 fprintf (where, " --windows-processor-groups Only show Windows processor groups\n");
574 #endif
575 fprintf (where, " -c --cpuset Show the cpuset of each object\n");
576 fprintf (where, " -C --cpuset-only Only show the cpuset of each object\n");
577 fprintf (where, " --cpuset-output-format <hwloc|list|taskset>\n"
578 " --cof <hwloc|list|taskset>\n"
579 " Change the format of cpuset outputs\n");
580 fprintf (where, "Object filtering options:\n");
581 fprintf (where, " --filter <type>:<knd> Filter objects of the given type, or all.\n");
582 fprintf (where, " <knd> may be `all' (keep all), `none' (remove all), `structure' or `important'\n");
583 fprintf (where, " --ignore <type> Ignore objects of the given type\n");
584 fprintf (where, " --no-smt Ignore PUs\n");
585 fprintf (where, " --no-caches Do not show caches\n");
586 fprintf (where, " --no-useless-caches Do not show caches which do not have a hierarchical\n"
587 " impact\n");
588 fprintf (where, " --no-icaches Do not show instruction caches\n");
589 fprintf (where, " --merge Do not show levels that do not have a hierarchical\n"
590 " impact\n");
591 fprintf (where, " --no-collapse Do not collapse identical PCI devices\n");
592 fprintf (where, " --restrict [nodeset=]<bitmap>\n");
593 fprintf (where, " Restrict the topology to some processors or NUMA nodes.\n");
594 fprintf (where, " --restrict binding Restrict the topology to the current process binding\n");
595 fprintf (where, " --restrict-flags <n> Set the flags to be used during restrict\n");
596 fprintf (where, " --no-io Do not show any I/O device or bridge\n");
597 fprintf (where, " --no-bridges Do not any I/O bridge except hostbridges\n");
598 fprintf (where, " --whole-io Show all I/O devices and bridges\n");
599 fprintf (where, "Input options:\n");
600 hwloc_utils_input_format_usage(where, 6);
601 fprintf (where, " --thissystem Assume that the input topology provides the topology\n"
602 " for the system on which we are running\n");
603 fprintf (where, " --pid <pid> Detect topology as seen by process <pid>\n");
604 fprintf (where, " --disallowed Include objects disallowed by administrative limitations\n");
605 fprintf (where, " --allow <all|local|...> Change the set of objects marked as allowed\n");
606 fprintf (where, " --flags <n> Set the topology flags\n");
607 fprintf (where, "Graphical output options:\n");
608 fprintf (where, " --children-order <memory:above|io:right:vert|...|plain>\n"
609 " Change the layout of Memory, I/O or Misc children\n");
610 fprintf (where, " --no-factorize Do not factorize identical objects\n");
611 fprintf (where, " --no-factorize=<type> Do not factorize identical objects of type <type>\n");
612 fprintf (where, " --factorize Factorize identical objects (default)\n");
613 fprintf (where, " --factorize=[<type>,]<N>[,<L>[,<F>]]\n");
614 fprintf (where, " Set the minimum number <N> of objects to factorize,\n");
615 fprintf (where, " the numbers of first <F> and last <L> to keep,\n");
616 fprintf (where, " for all or only the given object type <type>\n");
617 fprintf (where, " --no-cpukinds Do not show CPU kinds\n");
618 fprintf (where, " --fontsize 10 Set size of text font\n");
619 fprintf (where, " --gridsize 7 Set size of margin between elements\n");
620 fprintf (where, " --linespacing 4 Set spacing between lines of text\n");
621 fprintf (where, " --thickness 1 Set thickness of lines and boxes\n");
622 fprintf (where, " --horiz[=<type,...>] Horizontal graphical layout instead of nearly 4/3 ratio\n");
623 fprintf (where, " --vert[=<type,...>] Vertical graphical layout instead of nearly 4/3 ratio\n");
624 fprintf (where, " --rect[=<type,...>] Rectangular graphical layout with nearly 4/3 ratio\n");
625 fprintf (where, " --text[=<type,...>] Display text for the given object types\n");
626 fprintf (where, " --no-text[=<type,..>] Do not display text for the given object types\n");
627 fprintf (where, " --index=[<type,...>] Display indexes for the given object types\n");
628 fprintf (where, " --no-index=[<type,.>] Do not display indexes for the given object types\n");
629 fprintf (where, " --attrs=[<type,...>] Display attributes for the given object types\n");
630 fprintf (where, " --no-attrs=[<type,.>] Do not display attributes for the given object types\n");
631 fprintf (where, " --no-legend Remove all text legend lines at the bottom\n");
632 fprintf (where, " --no-default-legend Remove default text legend lines at the bottom\n");
633 fprintf (where, " --append-legend <s> Append a new line of text at the bottom of the legend\n");
634 fprintf (where, " --grey --palette grey Use greyscale instead of colors\n");
635 fprintf (where, " --palette white Use white instead of colors for background\n");
636 fprintf (where, " --palette <type>=<#xxyyzz>\n"
637 " Replace the color for object of the given type\n");
638 fprintf (where, " --binding-color <none|#xxyyzz>\n"
639 " Disable or change binding PU and NUMA nodes color\n");
640 fprintf (where, " --disallowed-color <none|#xxyyzz>\n"
641 " Disable or change disallowed PU and NUMA nodes color\n");
642 fprintf (where, " --top-color <none|#xxyyzz>\n"
643 " Disable or change task background color for --top\n");
644 fprintf (where, "Miscellaneous options:\n");
645 fprintf (where, " --logical-index-prefix <s> --os-index-prefix <s>\n");
646 fprintf (where, " Use <s> as a prefix for logical or physical/OS indexes\n");
647 fprintf (where, " --export-xml-flags <n>\n"
648 " Set flags during the XML topology export\n");
649 fprintf (where, " --export-synthetic-flags <n>\n"
650 " Set flags during the synthetic topology export\n");
651 /* --shmem-output-addr is undocumented on purpose */
652 fprintf (where, " --ps --top Display processes within the hierarchy\n");
653 fprintf (where, " --misc-from <file> Create Misc objects as defined in <file>");
654 fprintf (where, " --version Report version and exit\n");
655 fprintf (where, " -h --help Show this usage\n");
656 }
657
658 void lstopo_show_interactive_help(void)
659 {
660 #ifdef HAVE_ISATTY
661 if (!isatty(STDOUT_FILENO))
662 /* don't send the interactive help when not a terminal */
663 return;
664 #endif
665
666 printf("\n");
667 printf("Keyboard shortcuts:\n");
668 printf(" Zooming, scrolling and closing:\n");
669 printf(" Zoom-in or out ...................... + -\n");
670 printf(" Reset scale to default .............. 1\n");
671 printf(" Try to fit scale to window .......... F\n");
672 printf(" Resize window to the drawing ........ r\n");
673 printf(" Toggle auto-resizing of the window .. R\n");
674 printf(" Scroll vertically ................... Up Down PageUp PageDown\n");
675 printf(" Scroll horizontally ................. Left Right Ctrl+PageUp/Down\n");
676 printf(" Scroll to the top-left corner ....... Home\n");
677 printf(" Scroll to the bottom-right corner ... End\n");
678 printf(" Refresh the topology ................ F5\n");
679 printf(" Show this help ...................... h H ?\n");
680 printf(" Exit ................................ q Q Esc\n");
681 printf(" Configuration tweaks:\n");
682 printf(" Toggle factorizing or collapsing .... f\n");
683 printf(" Switch display mode for indexes ..... i\n");
684 printf(" Toggle displaying of object text .... t\n");
685 printf(" Toggle displaying of obj attributes . a\n");
686 printf(" Toggle displaying of CPU kinds ...... k\n");
687 printf(" Toggle color for disallowed objects . d\n");
688 printf(" Toggle color for binding objects .... b\n");
689 printf(" Toggle displaying of legend lines ... l\n");
690 printf(" Export to file with current config .. E\n");
691 printf("\n\n");
692 fflush(stdout);
693 }
694
695 static void lstopo__show_interactive_cli_options(const struct lstopo_output *loutput)
696 {
697 if (loutput->index_type == LSTOPO_INDEX_TYPE_PHYSICAL)
698 printf(" -p");
699 else if (loutput->index_type == LSTOPO_INDEX_TYPE_LOGICAL)
700 printf(" -l");
701 else if (loutput->index_type == LSTOPO_INDEX_TYPE_NONE)
702 printf(" --no-index");
703
704 if (!loutput->show_attrs_enabled)
705 printf(" --no-attrs");
706 if (!loutput->show_text_enabled)
707 printf(" --no-text");
708
709 if(!loutput->factorize_enabled)
710 printf(" --no-factorize");
711 if (!loutput->pci_collapse_enabled)
712 printf(" --no-collapse");
713 if (!loutput->show_cpukinds)
714 printf(" --no-cpukinds");
715 if (!loutput->show_binding)
716 printf(" --binding-color none");
717 if (!loutput->show_disallowed)
718 printf(" --disallowed-color none");
719 if (loutput->show_legend == LSTOPO_SHOW_LEGEND_NONE)
720 printf(" --no-legend");
721 else if (loutput->show_legend == LSTOPO_SHOW_LEGEND_NO_DEFAULT)
722 printf(" --no-default-legend");
723 }
724
725 void lstopo_show_interactive_cli_options(const struct lstopo_output *loutput)
726 {
727 #if (defined LSTOPO_HAVE_GRAPHICS) && (defined CAIRO_HAS_PDF_SURFACE)
728 const char *format = "PDF";
729 const char *extension = "pdf";
730 #else
731 const char *format = "SVG";
732 const char *extension = "svg";
733 #endif
734 printf("\nCommand-line options for the current configuration tweaks:\n");
735 lstopo__show_interactive_cli_options(loutput);
736 printf("\n\nTo export to %s:\n", format);
737 printf(" lstopo <your options>");
738 lstopo__show_interactive_cli_options(loutput);
739 printf(" topology.%s\n\n", extension);
740 }
741
742 enum output_format {
743 LSTOPO_OUTPUT_DEFAULT,
744 LSTOPO_OUTPUT_WINDOW,
745 LSTOPO_OUTPUT_CONSOLE,
746 LSTOPO_OUTPUT_SYNTHETIC,
747 LSTOPO_OUTPUT_ASCII,
748 LSTOPO_OUTPUT_TIKZ,
749 LSTOPO_OUTPUT_FIG,
750 LSTOPO_OUTPUT_PNG,
751 LSTOPO_OUTPUT_PDF,
752 LSTOPO_OUTPUT_PS,
753 LSTOPO_OUTPUT_SVG,
754 LSTOPO_OUTPUT_CAIROSVG,
755 LSTOPO_OUTPUT_NATIVESVG,
756 LSTOPO_OUTPUT_XML,
757 LSTOPO_OUTPUT_SHMEM,
758 LSTOPO_OUTPUT_ERROR
759 };
760
761 static enum output_format
762 parse_output_format(const char *name, char *callname __hwloc_attribute_unused)
763 {
764 if (!hwloc_strncasecmp(name, "default", 3))
765 return LSTOPO_OUTPUT_DEFAULT;
766 else if (!hwloc_strncasecmp(name, "window", 3))
767 return LSTOPO_OUTPUT_WINDOW;
768 else if (!hwloc_strncasecmp(name, "console", 3))
769 return LSTOPO_OUTPUT_CONSOLE;
770 else if (!strcasecmp(name, "synthetic"))
771 return LSTOPO_OUTPUT_SYNTHETIC;
772 else if (!strcasecmp(name, "ascii")
773 || !strcasecmp(name, "txt") /* backward compat with 1.10 */)
774 return LSTOPO_OUTPUT_ASCII;
775 else if (!strcasecmp(name, "tikz") || !strcasecmp(name, "tex"))
776 return LSTOPO_OUTPUT_TIKZ;
777 else if (!strcasecmp(name, "fig"))
778 return LSTOPO_OUTPUT_FIG;
779 else if (!strcasecmp(name, "png"))
780 return LSTOPO_OUTPUT_PNG;
781 else if (!strcasecmp(name, "pdf"))
782 return LSTOPO_OUTPUT_PDF;
783 else if (!strcasecmp(name, "ps"))
784 return LSTOPO_OUTPUT_PS;
785 else if (!strcasecmp(name, "svg"))
786 return LSTOPO_OUTPUT_SVG;
787 else if (!strcasecmp(name, "cairosvg") || !strcasecmp(name, "svg(cairo)"))
788 return LSTOPO_OUTPUT_CAIROSVG;
789 else if (!strcasecmp(name, "nativesvg") || !strcasecmp(name, "svg(native)"))
790 return LSTOPO_OUTPUT_NATIVESVG;
791 else if (!strcasecmp(name, "xml"))
792 return LSTOPO_OUTPUT_XML;
793 else if (!strcasecmp(name, "shmem"))
794 return LSTOPO_OUTPUT_SHMEM;
795 else
796 return LSTOPO_OUTPUT_ERROR;
797 }
798
799 static const char *
800 output_format_name(enum output_format format)
801 {
802 switch (format) {
803 case LSTOPO_OUTPUT_DEFAULT: return "default";
804 case LSTOPO_OUTPUT_WINDOW: return "window";
805 case LSTOPO_OUTPUT_CONSOLE: return "console";
806 case LSTOPO_OUTPUT_SYNTHETIC: return "synthetic";
807 case LSTOPO_OUTPUT_ASCII: return "ascii";
808 case LSTOPO_OUTPUT_TIKZ: return "tikz";
809 case LSTOPO_OUTPUT_FIG: return "fig";
810 case LSTOPO_OUTPUT_PNG: return "png";
811 case LSTOPO_OUTPUT_PDF: return "pdf";
812 case LSTOPO_OUTPUT_PS: return "ps";
813 case LSTOPO_OUTPUT_SVG: return "svg";
814 case LSTOPO_OUTPUT_CAIROSVG: return "cairosvg";
815 case LSTOPO_OUTPUT_NATIVESVG: return "nativesvg";
816 case LSTOPO_OUTPUT_XML: return "xml";
817 case LSTOPO_OUTPUT_SHMEM: return "shmem";
818 case LSTOPO_OUTPUT_ERROR: return "error";
819 }
820 abort();
821 }
822
823 /****************************************************
824 * Store filters during parsing and apply them later
825 */
826
827 struct lstopo_type_filter { enum hwloc_type_filter_e filter; int changed; };
828
829 /* if these assert fails, some types were added,
830 * assumptions in macros below must be rechecked
831 */
832 #define init_type_filters() do { \
833 unsigned _i; \
834 HWLOC_BUILD_ASSERT(HWLOC_OBJ_TYPE_MIN == 0); \
835 HWLOC_BUILD_ASSERT(HWLOC_OBJ_TYPE_MAX == 20); \
836 for(_i=HWLOC_OBJ_TYPE_MIN; _i<HWLOC_OBJ_TYPE_MAX; _i++) \
837 type_filter[_i].changed = 0; \
838 } while (0)
839
840 #define set_type_filter(_type, _filter) do { \
841 type_filter[_type].filter = _filter; \
842 type_filter[_type].changed = 1; \
843 } while (0)
844
845 #define set_all_types_filter(_filter) do { \
846 unsigned _i; \
847 for(_i=HWLOC_OBJ_TYPE_MIN; _i<HWLOC_OBJ_TYPE_MAX; _i++) \
848 set_type_filter(_i, _filter); \
849 } while (0)
850
851 /* must operate on same types as hwloc_topology_set_io_types_filter() */
852 #define set_io_types_filter(_filter) do { \
853 set_type_filter(HWLOC_OBJ_BRIDGE, _filter); \
854 set_type_filter(HWLOC_OBJ_PCI_DEVICE, _filter); \
855 set_type_filter(HWLOC_OBJ_OS_DEVICE, _filter); \
856 } while (0)
857
858 /* must operate on same types as hwloc_topology_set_cache_types_filter() */
859 #define set_cache_types_filter(_filter) do { \
860 unsigned _i; \
861 for(_i=HWLOC_OBJ_L1CACHE; _i<=HWLOC_OBJ_L3ICACHE; _i++) \
862 set_type_filter(_i, _filter); \
863 } while (0)
864
865 /* must operate on same types as hwloc_topology_set_icache_types_filter() */
866 #define set_icache_types_filter(_filter) do { \
867 unsigned _i; \
868 for(_i=HWLOC_OBJ_L1ICACHE; _i<=HWLOC_OBJ_L3ICACHE; _i++) \
869 set_type_filter(_i, _filter); \
870 } while (0)
871
872 #define apply_type_filters(_topo) do { \
873 unsigned _i; \
874 for(_i=HWLOC_OBJ_TYPE_MIN; _i<HWLOC_OBJ_TYPE_MAX; _i++) \
875 if (type_filter[_i].changed) \
876 hwloc_topology_set_type_filter(_topo, _i, type_filter[_i].filter); \
877 } while (0)
878
879
880 #define LSTOPO_VERBOSE_MODE_DEFAULT 1
881
882 int
883 main (int argc, char *argv[])
884 {
885 int err;
886 hwloc_topology_t topology;
887 const char *filename = NULL;
888 unsigned long flags = HWLOC_TOPOLOGY_FLAG_IMPORT_SUPPORT;
889 unsigned long restrict_flags = 0;
890 unsigned long allow_flags = 0;
891 hwloc_bitmap_t allow_cpuset = NULL, allow_nodeset = NULL;
892 char * callname;
893 char * input = NULL;
894 const char *show_only_string = NULL;
895 struct hwloc_utils_input_format_s input_format = HWLOC_UTILS_INPUT_FORMAT_DEFAULT;
896 enum output_format output_format = LSTOPO_OUTPUT_DEFAULT;
897 struct lstopo_type_filter type_filter[HWLOC_OBJ_TYPE_MAX];
898 char *restrictstring = NULL;
899 struct lstopo_output loutput;
900 output_method *output_func;
901 hwloc_membind_policy_t policy;
902 #ifdef HAVE_CLOCK_GETTIME
903 struct timespec ts1, ts2;
904 unsigned long ms;
905 int measure_load_time = !!getenv("HWLOC_DEBUG_LOAD_TIME");
906 #endif
907 char *env;
908 int top = 0;
909 FILE * miscfrom = NULL;
910 int opt;
911 unsigned i;
912
913 callname = strrchr(argv[0], '/');
914 if (!callname)
915 callname = argv[0];
916 else
917 callname++;
918 /* skip argv[0], handle options */
919 argc--;
920 argv++;
921
922 hwloc_utils_check_api_version(callname);
923
924 loutput.refreshing = 0;
925
926 loutput.overwrite = 0;
927
928 loutput.index_type = LSTOPO_INDEX_TYPE_DEFAULT;
929 loutput.verbose_mode = LSTOPO_VERBOSE_MODE_DEFAULT;
930 loutput.ignore_pus = 0;
931 loutput.ignore_numanodes = 0;
932 loutput.pci_collapse_enabled = 1;
933 loutput.pid_number = -1;
934 loutput.pid = 0;
935 loutput.need_pci_domain = 0;
936
937 init_type_filters();
938
939 loutput.factorize_enabled = 1;
940 for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
941 loutput.factorize_min[i] = FACTORIZE_MIN_DEFAULT;
942 lstopo_update_factorize_alltypes_bounds(&loutput);
943
944 loutput.export_synthetic_flags = 0;
945 loutput.export_xml_flags = 0;
946 loutput.shmem_output_addr = 0;
947
948 loutput.show_legend = LSTOPO_SHOW_LEGEND_ALL;
949 loutput.legend_append = NULL;
950 loutput.legend_append_nr = 0;
951 snprintf(loutput.title, sizeof(loutput.title), "lstopo");
952
953 loutput.show_distances_only = 0;
954 loutput.show_memattrs_only = 0;
955 loutput.show_cpukinds_only = 0;
956 loutput.show_windows_processor_groups_only = 0;
957 loutput.show_cpuset = 0;
958 loutput.cpuset_output_format = HWLOC_UTILS_CPUSET_FORMAT_HWLOC;
959 loutput.transform_distances = -1;
960
961 loutput.nr_cpukind_styles = 0;
962
963 loutput.backend_data = NULL;
964 loutput.backend_flags = 0;
965 loutput.methods = NULL;
966
967 loutput.children_order = LSTOPO_ORDER_MEMORY_ABOVE | LSTOPO_ORDER_IO_RIGHT | LSTOPO_ORDER_MISC_RIGHT;
968 loutput.fontsize = 10;
969 loutput.gridsize = 7;
970 loutput.linespacing = 4;
971 loutput.thickness = 1;
972
973 loutput.text_xscale = 1.0f;
974 env = getenv("LSTOPO_TEXT_XSCALE");
975 if (env)
976 loutput.text_xscale = (float) atof(env);
977
978 for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
979 loutput.force_orient[i] = LSTOPO_ORIENT_NONE;
980 loutput.force_orient[HWLOC_OBJ_PU] = LSTOPO_ORIENT_HORIZ;
981 for(i=HWLOC_OBJ_L1CACHE; i<=HWLOC_OBJ_L3ICACHE; i++)
982 loutput.force_orient[i] = LSTOPO_ORIENT_HORIZ;
983 loutput.force_orient[HWLOC_OBJ_NUMANODE] = LSTOPO_ORIENT_HORIZ;
984 loutput.force_orient[HWLOC_OBJ_MEMCACHE] = LSTOPO_ORIENT_HORIZ;
985 loutput.above_force_orient = LSTOPO_ORIENT_NONE;
986 loutput.right_force_orient = LSTOPO_ORIENT_NONE;
987 loutput.below_force_orient = LSTOPO_ORIENT_NONE;
988 for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++) {
989 loutput.show_indexes[i] = 1;
990 loutput.show_attrs[i] = 1;
991 loutput.show_text[i] = 1;
992 }
993 loutput.show_attrs_enabled = 1;
994 loutput.show_text_enabled = 1;
995 loutput.os_index_prefix = (char *) " P#";
996 loutput.logical_index_prefix = (char *) " L#";
997
998 loutput.show_binding = 1;
999 loutput.show_disallowed = 1;
1000 loutput.show_cpukinds = 1;
1001
1002 loutput.show_process_color = 1;
1003 lstopo_palette_init(&loutput);
1004
1005 /* show all error messages */
1006 if (!getenv("HWLOC_HIDE_ERRORS"))
1007 putenv((char *) "HWLOC_HIDE_ERRORS=0");
1008 /* enable verbose backends */
1009 if (!getenv("HWLOC_XML_VERBOSE"))
1010 putenv((char *) "HWLOC_XML_VERBOSE=1");
1011 if (!getenv("HWLOC_SYNTHETIC_VERBOSE"))
1012 putenv((char *) "HWLOC_SYNTHETIC_VERBOSE=1");
1013
1014 /* Use localized time prints, and utf-8 characters in the ascii output */
1015 #ifdef HAVE_SETLOCALE
1016 setlocale(LC_ALL, "");
1017 #endif
1018
1019 loutput.cpubind_set = hwloc_bitmap_alloc();
1020 loutput.membind_set = hwloc_bitmap_alloc();
1021 if (!loutput.cpubind_set || !loutput.membind_set)
1022 goto out;
1023
1024 while (argc >= 1)
1025 {
1026 opt = 0;
1027 if (!strcmp (argv[0], "-v") || !strcmp (argv[0], "--verbose")) {
1028 loutput.verbose_mode++;
1029 } else if (!strcmp (argv[0], "-q") || !strcmp (argv[0], "--quiet")
1030 || !strcmp (argv[0], "-s") || !strcmp (argv[0], "--silent")) {
1031 loutput.verbose_mode--;
1032 } else if (!strcmp (argv[0], "--distances")) {
1033 loutput.show_distances_only = 1;
1034 } else if (!strcmp (argv[0], "--distances-transform")) {
1035 if (argc < 2)
1036 goto out_usagefailure;
1037 if (!strcmp (argv[1], "links"))
1038 loutput.transform_distances = HWLOC_DISTANCES_TRANSFORM_LINKS;
1039 else if (!strcmp (argv[1], "merge-switch-ports"))
1040 loutput.transform_distances = HWLOC_DISTANCES_TRANSFORM_MERGE_SWITCH_PORTS;
1041 else if (!strcmp (argv[1], "transitive-closure"))
1042 loutput.transform_distances = HWLOC_DISTANCES_TRANSFORM_TRANSITIVE_CLOSURE;
1043 else {
1044 fprintf(stderr, "Unrecognized argument `%s' passed to --distances-transform\n", argv[1]);
1045 goto out_usagefailure;
1046 }
1047 opt = 1;
1048 } else if (!strcmp (argv[0], "--memattrs")) {
1049 loutput.show_memattrs_only = 1;
1050 } else if (!strcmp (argv[0], "--cpukinds")) {
1051 loutput.show_cpukinds_only = 1;
1052 #ifdef HWLOC_WIN_SYS
1053 } else if (!strcmp (argv[0], "--windows-processor-groups")) {
1054 loutput.show_windows_processor_groups_only = 1;
1055 #endif
1056 } else if (!strcmp (argv[0], "-h") || !strcmp (argv[0], "--help")) {
1057 usage(callname, stdout);
1058 exit(EXIT_SUCCESS);
1059 } else if (!strcmp (argv[0], "-f") || !strcmp (argv[0], "--force"))
1060 loutput.overwrite = 1;
1061 else if (!strcmp (argv[0], "-l") || !strcmp (argv[0], "--logical"))
1062 loutput.index_type = LSTOPO_INDEX_TYPE_LOGICAL;
1063 else if (!strcmp (argv[0], "-p") || !strcmp (argv[0], "--physical"))
1064 loutput.index_type = LSTOPO_INDEX_TYPE_PHYSICAL;
1065 else if (!strcmp (argv[0], "-c") || !strcmp (argv[0], "--cpuset"))
1066 loutput.show_cpuset = 1;
1067 else if (!strcmp (argv[0], "-C") || !strcmp (argv[0], "--cpuset-only"))
1068 loutput.show_cpuset = 2;
1069 else if (!strcmp(argv[0], "--cpuset-output-format") || !strcmp(argv[0], "--cof")) {
1070 if (argc < 2) {
1071 usage (callname, stderr);
1072 exit(EXIT_FAILURE);
1073 }
1074 loutput.cpuset_output_format = hwloc_utils_parse_cpuset_format(argv[1]);
1075 if (HWLOC_UTILS_CPUSET_FORMAT_UNKNOWN == loutput.cpuset_output_format) {
1076 fprintf(stderr, "Unrecognized %s argument %s\n", argv[0], argv[1]);
1077 goto out_usagefailure;
1078 }
1079 if (!loutput.show_cpuset)
1080 loutput.show_cpuset = 1;
1081 opt = 1;
1082 }
1083 else if (!strcmp (argv[0], "--taskset")) {
1084 loutput.cpuset_output_format = HWLOC_UTILS_CPUSET_FORMAT_TASKSET;
1085 if (!loutput.show_cpuset)
1086 loutput.show_cpuset = 1;
1087 } else if (!strcmp (argv[0], "--only")) {
1088 if (argc < 2)
1089 goto out_usagefailure;
1090 show_only_string = argv[1];
1091 opt = 1;
1092 }
1093 else if (!strcmp (argv[0], "--filter")) {
1094 hwloc_obj_type_t type = HWLOC_OBJ_TYPE_NONE;
1095 char *colon;
1096 enum hwloc_type_filter_e filter = HWLOC_TYPE_FILTER_KEEP_ALL;
1097 int all = 0;
1098 int allio = 0;
1099 int allcaches = 0;
1100 int allicaches = 0;
1101 if (argc < 2)
1102 goto out_usagefailure;
1103 colon = strchr(argv[1], ':');
1104 if (colon) {
1105 *colon = '\0';
1106 if (!strcmp(colon+1, "none"))
1107 filter = HWLOC_TYPE_FILTER_KEEP_NONE;
1108 else if (!strcmp(colon+1, "all"))
1109 filter = HWLOC_TYPE_FILTER_KEEP_ALL;
1110 else if (!strcmp(colon+1, "structure"))
1111 filter = HWLOC_TYPE_FILTER_KEEP_STRUCTURE;
1112 else if (!strcmp(colon+1, "important"))
1113 filter = HWLOC_TYPE_FILTER_KEEP_IMPORTANT;
1114 else {
1115 fprintf(stderr, "Unsupported filtering kind `%s' passed to --filter.\n", colon+1);
1116 goto out_usagefailure;
1117 }
1118 }
1119 if (!strcmp(argv[1], "all"))
1120 all = 1;
1121 else if (!strcmp(argv[1], "io"))
1122 allio = 1;
1123 else if (!strcmp(argv[1], "cache"))
1124 allcaches = 1;
1125 else if (!strcmp(argv[1], "icache"))
1126 allicaches = 1;
1127 else if (hwloc_type_sscanf(argv[1], &type, NULL, 0) < 0) {
1128 fprintf(stderr, "Unsupported type `%s' passed to --filter.\n", argv[1]);
1129 goto out_usagefailure;
1130 }
1131 if (type == HWLOC_OBJ_PU) {
1132 if (filter == HWLOC_TYPE_FILTER_KEEP_NONE)
1133 loutput.ignore_pus = 1;
1134 }
1135 else if (type == HWLOC_OBJ_NUMANODE) {
1136 if (filter == HWLOC_TYPE_FILTER_KEEP_NONE)
1137 loutput.ignore_numanodes = 1;
1138 }
1139 else if (all)
1140 set_all_types_filter(filter);
1141 else if (allio)
1142 set_io_types_filter(filter);
1143 else if (allcaches) {
1144 set_cache_types_filter(filter);
1145 set_type_filter(HWLOC_OBJ_MEMCACHE, filter);
1146 } else if (allicaches)
1147 set_icache_types_filter(filter);
1148 else
1149 set_type_filter(type, filter);
1150 opt = 1;
1151 }
1152 else if (!strcmp (argv[0], "--ignore")) {
1153 hwloc_obj_type_t type;
1154 if (argc < 2)
1155 goto out_usagefailure;
1156 if (!strcasecmp(argv[1], "cache")) {
1157 fprintf(stderr, "--ignore Cache not supported anymore, use --no-caches instead.\n");
1158 goto out_usagefailure;
1159 }
1160 if (hwloc_type_sscanf(argv[1], &type, NULL, 0) < 0)
1161 fprintf(stderr, "Unsupported type `%s' passed to --ignore, ignoring.\n", argv[1]);
1162 else if (type == HWLOC_OBJ_PU)
1163 loutput.ignore_pus = 1;
1164 else if (type == HWLOC_OBJ_NUMANODE)
1165 loutput.ignore_numanodes = 1;
1166 else
1167 set_type_filter(type, HWLOC_TYPE_FILTER_KEEP_NONE);
1168 opt = 1;
1169 }
1170 else if (!strcmp (argv[0], "--no-smt")) {
1171 loutput.ignore_pus = 1;
1172 }
1173 else if (!strcmp (argv[0], "--no-caches")) {
1174 set_cache_types_filter(HWLOC_TYPE_FILTER_KEEP_NONE);
1175 set_type_filter(HWLOC_OBJ_MEMCACHE, HWLOC_TYPE_FILTER_KEEP_NONE);
1176 }
1177 else if (!strcmp (argv[0], "--no-useless-caches")) {
1178 set_cache_types_filter(HWLOC_TYPE_FILTER_KEEP_STRUCTURE);
1179 set_type_filter(HWLOC_OBJ_MEMCACHE, HWLOC_TYPE_FILTER_KEEP_STRUCTURE);
1180 }
1181 else if (!strcmp (argv[0], "--no-icaches")) {
1182 set_icache_types_filter(HWLOC_TYPE_FILTER_KEEP_NONE);
1183 }
1184 else if (!strcmp (argv[0], "--disallowed") || !strcmp (argv[0], "--whole-system"))
1185 flags |= HWLOC_TOPOLOGY_FLAG_INCLUDE_DISALLOWED;
1186 else if (!strcmp (argv[0], "--allow")) {
1187 if (argc < 2)
1188 goto out_usagefailure;
1189 if (!strcmp(argv[1], "all")) {
1190 allow_flags = HWLOC_ALLOW_FLAG_ALL;
1191 } else if (!strcmp(argv[1], "local")) {
1192 allow_flags = HWLOC_ALLOW_FLAG_LOCAL_RESTRICTIONS;
1193 flags |= HWLOC_TOPOLOGY_FLAG_IS_THISSYSTEM;
1194 } else {
1195 hwloc_bitmap_t set = hwloc_bitmap_alloc();
1196 const char *begin = argv[1];
1197 if (!strncmp(begin, "nodeset=", 8))
1198 begin += 8;
1199 hwloc_bitmap_sscanf(set, begin);
1200 if (begin == argv[1])
1201 allow_cpuset = set;
1202 else
1203 allow_nodeset = set;
1204 allow_flags = HWLOC_ALLOW_FLAG_CUSTOM;
1205 }
1206 opt = 1;
1207 flags |= HWLOC_TOPOLOGY_FLAG_INCLUDE_DISALLOWED;
1208
1209 } else if (!strcmp (argv[0], "--no-io")) {
1210 set_io_types_filter(HWLOC_TYPE_FILTER_KEEP_NONE);
1211 } else if (!strcmp (argv[0], "--no-bridges")) {
1212 set_type_filter(HWLOC_OBJ_BRIDGE, HWLOC_TYPE_FILTER_KEEP_NONE);
1213 } else if (!strcmp (argv[0], "--whole-io")) {
1214 set_io_types_filter(HWLOC_TYPE_FILTER_KEEP_ALL);
1215 } else if (!strcmp (argv[0], "--merge")) {
1216 set_all_types_filter(HWLOC_TYPE_FILTER_KEEP_STRUCTURE);
1217 }
1218 else if (!strcmp (argv[0], "--no-collapse"))
1219 loutput.pci_collapse_enabled = 0;
1220
1221 else if (!strcmp (argv[0], "--no-factorize")) {
1222 for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
1223 loutput.factorize_min[i] = FACTORIZE_MIN_DISABLED;
1224 }
1225 else if (!strncmp (argv[0], "--no-factorize=", 15)) {
1226 hwloc_obj_type_t type;
1227 const char *tmp = argv[0]+15;
1228 if (hwloc_type_sscanf(tmp, &type, NULL, 0) < 0) {
1229 fprintf(stderr, "Unsupported parameter `%s' passed to %s, ignoring.\n", tmp, argv[0]);
1230 goto out_usagefailure;
1231 }
1232 loutput.factorize_min[type] = FACTORIZE_MIN_DISABLED;
1233 }
1234 else if (!strcmp (argv[0], "--factorize")) {
1235 for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
1236 loutput.factorize_min[i] = FACTORIZE_MIN_DEFAULT;
1237 lstopo_update_factorize_alltypes_bounds(&loutput);
1238 }
1239 else if (!strncmp (argv[0], "--factorize=", 12)) {
1240 hwloc_obj_type_t type, type_min, type_max;
1241 unsigned min, first, last;
1242 const char *tmp = argv[0]+12;
1243 const char *sep1, *sep2, *sep3;
1244
1245 if (*tmp < '0' || *tmp > '9') {
1246 if (hwloc_type_sscanf(tmp, &type, NULL, 0) < 0) {
1247 fprintf(stderr, "Unsupported type `%s' passed to %s, ignoring.\n", tmp, argv[0]);
1248 goto out_usagefailure;
1249 }
1250 type_min = type;
1251 type_max = type+1;
1252 sep1 = strchr(tmp, ',');
1253 } else {
1254 type_min = HWLOC_OBJ_TYPE_MIN;
1255 type_max = HWLOC_OBJ_TYPE_MAX;
1256 sep1 = tmp-1;
1257 }
1258
1259 if (!sep1) {
1260 min = FACTORIZE_MIN_DEFAULT;
1261 lstopo_update_factorize_bounds(min, &first, &last);
1262 } else {
1263 min = atoi(sep1+1);
1264 lstopo_update_factorize_bounds(min, &first, &last);
1265 sep2 = strchr(sep1+1, ',');
1266 if (sep2) {
1267 first = atoi(sep2+1);
1268 sep3 = strchr(sep2+1, ',');
1269 if (sep3)
1270 last = atoi(sep3+1);
1271 }
1272 }
1273
1274 for(i=type_min; i<(unsigned)type_max; i++) {
1275 loutput.factorize_min[i] = min;
1276 loutput.factorize_first[i] = first;
1277 loutput.factorize_last[i] = last;
1278 }
1279 }
1280
1281 else if (!strcmp (argv[0], "--thissystem"))
1282 flags |= HWLOC_TOPOLOGY_FLAG_IS_THISSYSTEM;
1283 else if (!strcmp (argv[0], "--flags")) {
1284 if (argc < 2)
1285 goto out_usagefailure;
1286 flags = hwloc_utils_parse_topology_flags(argv[1]);
1287 if(flags == (unsigned long)-1)
1288 goto out;
1289 opt = 1;
1290 }
1291 else if (!strcmp (argv[0], "--restrict")) {
1292 if (argc < 2)
1293 goto out_usagefailure;
1294 if(strncmp(argv[1], "nodeset=", 8)) {
1295 restrictstring = strdup(argv[1]);
1296 } else {
1297 restrictstring = strdup(argv[1]+8);
1298 restrict_flags |= HWLOC_RESTRICT_FLAG_BYNODESET;
1299 }
1300 opt = 1;
1301 }
1302 else if (!strcmp (argv[0], "--restrict-flags")) {
1303 if (argc < 2)
1304 goto out_usagefailure;
1305 restrict_flags = hwloc_utils_parse_restrict_flags(argv[1]);
1306 if(restrict_flags == (unsigned long)-1)
1307 goto out;
1308 opt = 1;
1309 }
1310 else if (!strcmp (argv[0], "--export-xml-flags")) {
1311 if (argc < 2)
1312 goto out_usagefailure;
1313 loutput.export_xml_flags = hwloc_utils_parse_export_xml_flags(argv[1]);
1314 if(loutput.export_xml_flags == (unsigned long)-1)
1315 goto out;
1316 opt = 1;
1317 }
1318 else if (!strcmp (argv[0], "--export-synthetic-flags")) {
1319 if (argc < 2)
1320 goto out_usagefailure;
1321 loutput.export_synthetic_flags = hwloc_utils_parse_export_synthetic_flags(argv[1]);
1322 if(loutput.export_synthetic_flags == (unsigned long)-1)
1323 goto out;
1324 opt = 1;
1325 }
1326 else if (!strcmp (argv[0], "--horiz"))
1327 for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
1328 loutput.force_orient[i] = LSTOPO_ORIENT_HORIZ;
1329 else if (!strcmp (argv[0], "--vert"))
1330 for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
1331 loutput.force_orient[i] = LSTOPO_ORIENT_VERT;
1332 else if (!strcmp (argv[0], "--rect"))
1333 for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
1334 loutput.force_orient[i] = LSTOPO_ORIENT_RECT;
1335 else if (!strncmp (argv[0], "--horiz=", 8)
1336 || !strncmp (argv[0], "--vert=", 7)
1337 || !strncmp (argv[0], "--rect=", 7)) {
1338 enum lstopo_orient_e orient = (argv[0][2] == 'h') ? LSTOPO_ORIENT_HORIZ : (argv[0][2] == 'v') ? LSTOPO_ORIENT_VERT : LSTOPO_ORIENT_RECT;
1339 char *tmp = argv[0] + ((argv[0][2] == 'h') ? 8 : 7);
1340 while (tmp) {
1341 char *end = strchr(tmp, ',');
1342 hwloc_obj_type_t type;
1343 if (end)
1344 *end = '\0';
1345 if (hwloc_type_sscanf(tmp, &type, NULL, 0) < 0)
1346 fprintf(stderr, "Unsupported type `%s' passed to %s, ignoring.\n", tmp, argv[0]);
1347 else
1348 loutput.force_orient[type] = orient;
1349 if (!end)
1350 break;
1351 tmp = end+1;
1352 }
1353 }
1354
1355 else if (!strcmp (argv[0], "--grey") || !strcmp (argv[0], "--greyscale"))
1356 lstopo_palette_select(&loutput, argv[0]+2);
1357
1358 else if (!strcmp (argv[0], "--palette")) {
1359 char *equal;
1360 if (argc < 2)
1361 goto out_usagefailure;
1362 equal = strchr(argv[1], '=');
1363 if (equal) {
1364 if (equal[1] != '#')
1365 fprintf(stderr, "Unsupported palette color modification `%s' passed to %s, ignoring.\n", argv[1], argv[0]);
1366 else {
1367 *equal = '\0';
1368 lstopo_palette_set_color_by_name(&loutput, argv[1], strtoul(equal+2, NULL, 16));
1369 }
1370 } else {
1371 lstopo_palette_select(&loutput, argv[1]);
1372 }
1373 opt = 1;
1374 }
1375 else if (!strcmp (argv[0], "--binding-color")) {
1376 if (argc < 2)
1377 goto out_usagefailure;
1378 if (!strcmp(argv[1], "none"))
1379 loutput.show_binding = 0;
1380 else if (*argv[1] == '#')
1381 lstopo_palette_set_color(&loutput.palette->binding, strtoul(argv[1]+1, NULL, 16));
1382 else
1383 fprintf(stderr, "Unsupported color `%s' passed to %s, ignoring.\n", argv[1], argv[0]);
1384 opt = 1;
1385 }
1386 else if (!strcmp (argv[0], "--disallowed-color")) {
1387 if (argc < 2)
1388 goto out_usagefailure;
1389 if (!strcmp(argv[1], "none"))
1390 loutput.show_disallowed = 0;
1391 else if (*argv[1] == '#')
1392 lstopo_palette_set_color(&loutput.palette->disallowed, strtoul(argv[1]+1, NULL, 16));
1393 else
1394 fprintf(stderr, "Unsupported color `%s' passed to %s, ignoring.\n", argv[1], argv[0]);
1395 opt = 1;
1396 }
1397 else if (!strcmp (argv[0], "--top-color")) {
1398 if (argc < 2)
1399 goto out_usagefailure;
1400 if (!strcmp(argv[1], "none"))
1401 loutput.show_process_color = 0;
1402 else if (*argv[1] == '#')
1403 lstopo_palette_set_color(&loutput.palette->process, strtoul(argv[1]+1, NULL, 16));
1404 else
1405 fprintf(stderr, "Unsupported color `%s' passed to %s, ignoring.\n", argv[1], argv[0]);
1406 opt = 1;
1407 }
1408 else if (!strcmp(argv[0], "--os-index-prefix")) {
1409 if (argc < 2)
1410 goto out_usagefailure;
1411 loutput.os_index_prefix = argv[1];
1412 opt = 1;
1413 }
1414 else if (!strcmp(argv[0], "--logical-index-prefix")) {
1415 if (argc < 2)
1416 goto out_usagefailure;
1417 loutput.logical_index_prefix = argv[1];
1418 opt = 1;
1419 }
1420 else if (!strncmp (argv[0], "--no-text", 9)
1421 || !strncmp (argv[0], "--text", 6)
1422 || !strncmp (argv[0], "--no-index", 10)
1423 || !strncmp (argv[0], "--index", 7)
1424 || !strncmp (argv[0], "--no-attrs", 10)
1425 || !strncmp (argv[0], "--attrs", 7)) {
1426 int enable = argv[0][2] != 'n';
1427 const char *kind = enable ? argv[0]+2 : argv[0]+5;
1428 const char *end;
1429 int *array;
1430 if (*kind == 't') {
1431 array = loutput.show_text;
1432 end = kind+4;
1433 } else if (*kind == 'a') {
1434 array = loutput.show_attrs;
1435 end = kind+5;
1436 } else if (*kind == 'i') {
1437 array = loutput.show_indexes;
1438 end = kind+5;
1439 } else {
1440 abort();
1441 }
1442 if (!*end) {
1443 for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
1444 array[i] = enable;
1445
1446 } else if (*end == '=') {
1447 const char *tmp = end+1;
1448 while (tmp) {
1449 char *sep = strchr(tmp, ',');
1450 hwloc_obj_type_t type;
1451 if (sep)
1452 *sep = '\0';
1453 if (hwloc_type_sscanf(tmp, &type, NULL, 0) < 0)
1454 if (!hwloc_strncasecmp(tmp, "cache", 5)) {
1455 for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
1456 if (hwloc_obj_type_is_cache(i))
1457 array[i] = enable;
1458 } else if (!hwloc_strncasecmp(tmp, "io", 2)) {
1459 for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
1460 if (hwloc_obj_type_is_io(i))
1461 array[i] = enable;
1462 } else
1463 fprintf(stderr, "Unsupported type `%s' passed to %s, ignoring.\n", tmp, argv[0]);
1464 else
1465 array[type] = enable;
1466 if (!sep)
1467 break;
1468 tmp = sep+1;
1469 }
1470
1471 } else {
1472 fprintf(stderr, "Unexpected character %c in option %s\n", *end, argv[0]);
1473 goto out_usagefailure;
1474 }
1475 }
1476
1477 else if (!strcmp (argv[0], "--children-order")) {
1478 if (argc < 2)
1479 goto out_usagefailure;
1480 lstopo_parse_children_order(argv[1], &loutput.children_order,
1481 &loutput.above_force_orient, &loutput.right_force_orient, &loutput.below_force_orient);
1482 opt = 1;
1483 }
1484 else if (!strcmp (argv[0], "--no-cpukinds")) {
1485 loutput.show_cpukinds = 0;
1486 }
1487 else if (!strcmp (argv[0], "--fontsize")) {
1488 if (argc < 2)
1489 goto out_usagefailure;
1490 loutput.fontsize = atoi(argv[1]);
1491 opt = 1;
1492 }
1493 else if (!strcmp (argv[0], "--gridsize")) {
1494 if (argc < 2)
1495 goto out_usagefailure;
1496 loutput.gridsize = atoi(argv[1]);
1497 opt = 1;
1498 }
1499 else if (!strcmp (argv[0], "--linespacing")) {
1500 if (argc < 2)
1501 goto out_usagefailure;
1502 loutput.linespacing = atoi(argv[1]);
1503 opt = 1;
1504 }
1505 else if (!strcmp (argv[0], "--thickness")) {
1506 if (argc < 2)
1507 goto out_usagefailure;
1508 loutput.thickness = atoi(argv[1]);
1509 opt = 1;
1510 }
1511 else if (!strcmp (argv[0], "--no-legend")) {
1512 loutput.show_legend = LSTOPO_SHOW_LEGEND_NONE;
1513 }
1514 else if (!strcmp (argv[0], "--no-default-legend")) {
1515 loutput.show_legend = LSTOPO_SHOW_LEGEND_NO_DEFAULT;
1516 }
1517 else if (!strcmp (argv[0], "--append-legend")) {
1518 char **tmp;
1519 if (argc < 2)
1520 goto out_usagefailure;
1521 tmp = realloc(loutput.legend_append, (loutput.legend_append_nr+1) * sizeof(*loutput.legend_append));
1522 if (!tmp) {
1523 fprintf(stderr, "Failed to realloc legend append array, legend ignored.\n");
1524 } else {
1525 loutput.legend_append = tmp;
1526 loutput.legend_append[loutput.legend_append_nr] = strdup(argv[1]);
1527 loutput.legend_append_nr++;
1528 }
1529 opt = 1;
1530 }
1531
1532 else if (!strcmp (argv[0], "--shmem-output-addr")) {
1533 if (argc < 2)
1534 goto out_usagefailure;
1535 loutput.shmem_output_addr = strtoull(argv[1], NULL, 0);
1536 opt = 1;
1537 }
1538
1539 else if (hwloc_utils_lookup_input_option(argv, argc, &opt,
1540 &input, &input_format,
1541 callname)) {
1542 /* nothing to do anymore */
1543
1544 } else if (!strcmp (argv[0], "--pid")) {
1545 if (argc < 2)
1546 goto out_usagefailure;
1547 loutput.pid_number = atoi(argv[1]); opt = 1;
1548 } else if (!strcmp (argv[0], "--ps") || !strcmp (argv[0], "--top"))
1549 top = 1;
1550 else if (!strcmp (argv[0], "--misc-from")) {
1551 if (argc < 2)
1552 goto out_usagefailure;
1553 if (!strcmp(argv[1], "-"))
1554 miscfrom = stdin;
1555 else
1556 miscfrom = fopen(argv[1], "r");
1557 if (!miscfrom) {
1558 fprintf(stderr, "Failed open --misc-from %s file for reading (%s)\n", argv[1], strerror(errno));
1559 exit(EXIT_FAILURE);
1560 }
1561 opt = 1;
1562 } else if (!strcmp (argv[0], "--version")) {
1563 printf("%s %s\n", callname, HWLOC_VERSION);
1564 exit(EXIT_SUCCESS);
1565 } else if (!strcmp (argv[0], "--output-format") || !strcmp (argv[0], "--of")) {
1566 if (argc < 2)
1567 goto out_usagefailure;
1568 output_format = parse_output_format(argv[1], callname);
1569 opt = 1;
1570 } else {
1571 if (filename) {
1572 fprintf (stderr, "Unrecognized option `%s', cannot be used as output filename (`%s' already given).\n",
1573 argv[0], filename);
1574 goto out_usagefailure;
1575 } else
1576 filename = argv[0];
1577 }
1578 argc -= opt+1;
1579 argv += opt+1;
1580 }
1581
1582 if (!loutput.fontsize) {
1583 for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
1584 loutput.show_text[i] = 0;
1585 loutput.show_legend = LSTOPO_SHOW_LEGEND_NONE;
1586 }
1587
1588 /***********************
1589 * Configure the output
1590 */
1591
1592 /* if output is -.format but --of was given, ignore .format */
1593 if (output_format != LSTOPO_OUTPUT_DEFAULT
1594 && filename && filename[0] == '-' && filename[1] == '.') {
1595 fprintf(stderr, "Ignoring extension in stdout output `%s' since --of was also given format `%s'.\n",
1596 filename, output_format_name(output_format));
1597 filename = "-"; /* to simplify things later */
1598 }
1599
1600 /* if the output format wasn't enforced, look at the filename */
1601 if (filename && output_format == LSTOPO_OUTPUT_DEFAULT) {
1602 if (!strcmp(filename, "-")
1603 || !strcmp(filename, "/dev/stdout")) {
1604 output_format = LSTOPO_OUTPUT_CONSOLE;
1605 filename = "-"; /* to simplify things later */
1606 } else {
1607 char *dot = strrchr(filename, '.');
1608 if (dot) {
1609 output_format = parse_output_format(dot+1, callname);
1610 if (dot == filename+1 && filename[0] == '-' && output_format != LSTOPO_OUTPUT_ERROR)
1611 filename = "-"; /* to simplify things later */
1612 } else {
1613 fprintf(stderr, "Cannot infer output type for file `%s' without any extension, using default output.\n", filename);
1614 filename = NULL;
1615 }
1616 }
1617 }
1618 if (output_format == LSTOPO_OUTPUT_ERROR)
1619 goto out_usagefailure;
1620
1621 /* if the output format wasn't enforced, think a bit about what the user probably want */
1622 if (output_format == LSTOPO_OUTPUT_DEFAULT) {
1623 if (loutput.show_cpuset
1624 || show_only_string
1625 || loutput.show_distances_only
1626 || loutput.show_memattrs_only
1627 || loutput.show_cpukinds_only
1628 || loutput.show_windows_processor_groups_only
1629 || loutput.verbose_mode != LSTOPO_VERBOSE_MODE_DEFAULT)
1630 output_format = LSTOPO_OUTPUT_CONSOLE;
1631 }
1632
1633 switch (output_format) {
1634 case LSTOPO_OUTPUT_DEFAULT: {
1635 #ifdef LSTOPO_HAVE_GRAPHICS
1636 #if (defined LSTOPO_HAVE_X11)
1637 int want_console = 0;
1638 #if (defined HAVE_ISATTY) && (defined HAVE_TCGETPGRP)
1639 /* If stdout isn't a tty, we're likely redirecting stdout, use console mode.
1640 * However, if launched from the window manager, we still want graphical mode.
1641 * tcgetpgrp(STDIN) should fail with ENOTTY in this case.
1642 * We don't specifically check for errno==ENOTTY: if tcgetpgrp() ever fails
1643 * for another reason, don't assume we're redirected, keep the graphical mode too.
1644 */
1645 if (!isatty(STDOUT_FILENO) && tcgetpgrp(STDIN_FILENO) != -1)
1646 want_console = 1;
1647 #endif
1648 if (getenv("DISPLAY") && !want_console) {
1649 output_func = output_x11;
1650 output_format = LSTOPO_OUTPUT_WINDOW;
1651 } else
1652 #endif /* LSTOPO_HAVE_X11 */
1653 #ifdef HWLOC_WIN_SYS
1654 {
1655 output_func = output_windows;
1656 output_format = LSTOPO_OUTPUT_WINDOW;
1657 }
1658 #endif
1659 #endif /* !LSTOPO_HAVE_GRAPHICS */
1660 #if !defined HWLOC_WIN_SYS || !defined LSTOPO_HAVE_GRAPHICS
1661 {
1662 output_func = output_console;
1663 output_format = LSTOPO_OUTPUT_CONSOLE;
1664 }
1665 #endif
1666 #ifdef ANDROID
1667 setJNIEnv();
1668 output_func = output_android;
1669 output_format = LSTOPO_OUTPUT_WINDOW;
1670 #endif
1671 }
1672 break;
1673
1674 case LSTOPO_OUTPUT_WINDOW:
1675 #if (defined LSTOPO_HAVE_GRAPHICS) && (defined LSTOPO_HAVE_X11)
1676 if (getenv("DISPLAY")) {
1677 output_func = output_x11;
1678 } else {
1679 fprintf(stderr, "X11 graphical window output requires a DISPLAY environment variable.\n");
1680 goto out;
1681 }
1682 #elif (defined LSTOPO_HAVE_GRAPHICS) && (defined HWLOC_WIN_SYS)
1683 output_func = output_windows;
1684 #else
1685 fprintf(stderr, "Graphical window output not supported.\n");
1686 goto out;
1687 #endif
1688 break;
1689
1690 case LSTOPO_OUTPUT_CONSOLE:
1691 output_func = output_console;
1692 break;
1693 case LSTOPO_OUTPUT_SYNTHETIC:
1694 output_func = output_synthetic;
1695 break;
1696 case LSTOPO_OUTPUT_ASCII:
1697 output_func = output_ascii;
1698 break;
1699 case LSTOPO_OUTPUT_TIKZ:
1700 output_func = output_tikz;
1701 break;
1702 case LSTOPO_OUTPUT_FIG:
1703 output_func = output_fig;
1704 break;
1705 #ifdef LSTOPO_HAVE_GRAPHICS
1706 # ifdef CAIRO_HAS_PNG_FUNCTIONS
1707 case LSTOPO_OUTPUT_PNG:
1708 output_func = output_png;
1709 break;
1710 # endif /* CAIRO_HAS_PNG_FUNCTIONS */
1711 # ifdef CAIRO_HAS_PDF_SURFACE
1712 case LSTOPO_OUTPUT_PDF:
1713 output_func = output_pdf;
1714 break;
1715 # endif /* CAIRO_HAS_PDF_SURFACE */
1716 # ifdef CAIRO_HAS_PS_SURFACE
1717 case LSTOPO_OUTPUT_PS:
1718 output_func = output_ps;
1719 break;
1720 # endif /* CAIRO_HAS_PS_SURFACE */
1721 # ifdef CAIRO_HAS_SVG_SURFACE
1722 case LSTOPO_OUTPUT_SVG:
1723 case LSTOPO_OUTPUT_CAIROSVG:
1724 output_func = output_cairosvg;
1725 output_format = LSTOPO_OUTPUT_CAIROSVG;
1726 break;
1727 # endif /* CAIRO_HAS_SVG_SURFACE */
1728 #endif /* LSTOPO_HAVE_GRAPHICS */
1729 #if !(defined LSTOPO_HAVE_GRAPHICS) || !(defined CAIRO_HAS_SVG_SURFACE)
1730 case LSTOPO_OUTPUT_SVG:
1731 #endif
1732 case LSTOPO_OUTPUT_NATIVESVG:
1733 output_func = output_nativesvg;
1734 output_format = LSTOPO_OUTPUT_NATIVESVG;
1735 break;
1736 case LSTOPO_OUTPUT_XML:
1737 output_func = output_xml;
1738 break;
1739 #ifndef HWLOC_WIN_SYS
1740 case LSTOPO_OUTPUT_SHMEM:
1741 output_func = output_shmem;
1742 break;
1743 #endif
1744 default:
1745 fprintf(stderr, "file format not supported\n");
1746 goto out_usagefailure;
1747 }
1748
1749 refresh:
1750 loutput.needs_topology_refresh = 0;
1751
1752 /*************************
1753 * Configure the topology
1754 */
1755
1756 err = hwloc_topology_init (&topology);
1757 if (err)
1758 goto out;
1759 hwloc_topology_set_all_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_ALL);
1760 hwloc_topology_set_io_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_IMPORTANT);
1761
1762 err = hwloc_topology_set_flags(topology, flags);
1763 if (err < 0) {
1764 fprintf(stderr, "Failed to set flags 0x%lx (%s).\n", flags, strerror(errno));
1765 goto out_with_topology;
1766 }
1767
1768 if (input) {
1769 err = hwloc_utils_enable_input_format(topology, flags, input, &input_format, loutput.verbose_mode > 1, callname);
1770 if (err)
1771 goto out_with_topology;
1772
1773 if (input_format.format != HWLOC_UTILS_INPUT_DEFAULT) {
1774 /* add the input path to the window title */
1775 snprintf(loutput.title, sizeof(loutput.title), "lstopo - %s", input);
1776
1777 #ifndef HWLOC_WIN_SYS
1778 /* try to only add the last part of the input path to the window title.
1779 * disabled on windows because it requires to deal with / or \ in both cygwin and native paths.
1780 * looks like _fullpath() is good way to replace realpath() on !cygwin.
1781 */
1782 /* sanitize the path to avoid / ./ or ../ at the end */
1783 char *fullpath = realpath(input, NULL);
1784 if (fullpath) {
1785 char *pos = strrchr(fullpath, '/');
1786 /* now only keep the last part */
1787 if (pos)
1788 pos++;
1789 else
1790 pos = fullpath;
1791 snprintf(loutput.title, sizeof(loutput.title), "lstopo - %s", pos);
1792 free(fullpath);
1793 }
1794 #endif
1795 }
1796 }
1797
1798 if (loutput.pid_number > 0) {
1799 if (hwloc_pid_from_number(&loutput.pid, loutput.pid_number, 0, 1 /* verbose */) < 0
1800 || hwloc_topology_set_pid(topology, loutput.pid)) {
1801 perror("Setting target pid");
1802 goto out_with_topology;
1803 }
1804 }
1805
1806 if (input_format.format == HWLOC_UTILS_INPUT_XML
1807 && output_format == LSTOPO_OUTPUT_XML) {
1808 /* must be after parsing output format and before loading the topology */
1809 putenv((char *) "HWLOC_XML_USERDATA_NOT_DECODED=1");
1810 hwloc_topology_set_userdata_import_callback(topology, hwloc_utils_userdata_import_cb);
1811 hwloc_topology_set_userdata_export_callback(topology, hwloc_utils_userdata_export_cb);
1812 }
1813
1814 apply_type_filters(topology);
1815
1816 /*********************
1817 * Build the topology
1818 */
1819
1820 #ifdef HAVE_CLOCK_GETTIME
1821 if (measure_load_time)
1822 clock_gettime(CLOCK_MONOTONIC, &ts1);
1823 #endif
1824
1825 if (input_format.format == HWLOC_UTILS_INPUT_SHMEM) {
1826 #ifdef HWLOC_WIN_SYS
1827 fprintf(stderr, "shmem topology not supported\n"); /* this line must match the grep line in test-lstopo-shmem */
1828 goto out_with_topology;
1829 #else /* !HWLOC_WIN_SYS */
1830 /* load from shmem, and duplicate onto topology, so that we may modify it */
1831 hwloc_topology_destroy(topology);
1832 err = lstopo_shmem_adopt(input, &topology);
1833 if (err < 0)
1834 goto out;
1835 hwloc_utils_userdata_clear_recursive(hwloc_get_root_obj(topology));
1836 #endif /* !HWLOC_WIN_SYS */
1837
1838 } else {
1839 /* normal load */
1840 err = hwloc_topology_load (topology);
1841 if (err) {
1842 fprintf(stderr, "hwloc_topology_load() failed (%s).\n", strerror(errno));
1843 goto out_with_topology;
1844 }
1845 }
1846
1847 #ifdef HAVE_CLOCK_GETTIME
1848 if (measure_load_time) {
1849 clock_gettime(CLOCK_MONOTONIC, &ts2);
1850 ms = (ts2.tv_nsec-ts1.tv_nsec)/1000000+(ts2.tv_sec-ts1.tv_sec)*1000UL;
1851 printf("hwloc_topology_load() took %lu ms\n", ms);
1852 }
1853 #endif
1854
1855 /********************************
1856 * Clean input format's internal variables
1857 */
1858
1859 if (input) {
1860 hwloc_utils_disable_input_format(&input_format);
1861 }
1862
1863 /********************************
1864 * Tweak the topology and output
1865 */
1866
1867 if (allow_flags) {
1868 if (allow_flags == HWLOC_ALLOW_FLAG_CUSTOM) {
1869 err = hwloc_topology_allow(topology, allow_cpuset, allow_nodeset, HWLOC_ALLOW_FLAG_CUSTOM);
1870 } else {
1871 err = hwloc_topology_allow(topology, NULL, NULL, allow_flags);
1872 }
1873 if (err < 0) {
1874 fprintf(stderr, "hwloc_topology_allow() failed (%s)\n", strerror(errno));
1875 goto out_with_topology;
1876 }
1877 }
1878
1879 hwloc_bitmap_fill(loutput.cpubind_set);
1880 if (loutput.pid_number != -1 && loutput.pid_number != 0)
1881 hwloc_get_proc_cpubind(topology, loutput.pid, loutput.cpubind_set, 0);
1882 else
1883 /* get our binding even if --pid not given, it may be used by --restrict */
1884 hwloc_get_cpubind(topology, loutput.cpubind_set, 0);
1885
1886 hwloc_bitmap_fill(loutput.membind_set);
1887 if (loutput.pid_number != -1 && loutput.pid_number != 0)
1888 hwloc_get_proc_membind(topology, loutput.pid, loutput.membind_set, &policy, HWLOC_MEMBIND_BYNODESET);
1889 else
1890 /* get our binding even if --pid not given, it may be used by --restrict */
1891 hwloc_get_membind(topology, loutput.membind_set, &policy, HWLOC_MEMBIND_BYNODESET);
1892
1893 loutput.need_pci_domain = lstopo_check_pci_domains(topology);
1894
1895 if (top)
1896 add_process_objects(topology);
1897 if (miscfrom)
1898 add_misc_objects_from(topology, miscfrom);
1899
1900 if (restrictstring) {
1901 hwloc_bitmap_t restrictset = hwloc_bitmap_alloc();
1902 if (!strcmp (restrictstring, "binding")) {
1903 hwloc_bitmap_copy(restrictset, loutput.cpubind_set);
1904 } else {
1905 hwloc_bitmap_sscanf(restrictset, restrictstring);
1906 }
1907 err = hwloc_topology_restrict (topology, restrictset, restrict_flags);
1908 if (err) {
1909 perror("Restricting the topology");
1910 /* FALLTHRU */
1911 }
1912 hwloc_bitmap_free(restrictset);
1913 free(restrictstring);
1914 }
1915
1916 loutput.topology = topology;
1917 loutput.depth = hwloc_topology_get_depth(topology);
1918 loutput.file = NULL;
1919
1920 if (filename && output_format == LSTOPO_OUTPUT_WINDOW) {
1921 fprintf(stderr, "Output filename `%s' ignored when using graphical window output.\n",
1922 filename);
1923 }
1924
1925 #ifdef HAVE_ISATTY
1926 /* if exporting to a file (which is not stdout, rewritten as "-" above),
1927 * and stdout is a terminal,
1928 * show a message about the export filename and format.
1929 */
1930 if (filename && strcmp(filename, "-") && output_format != LSTOPO_OUTPUT_WINDOW && isatty(STDOUT_FILENO))
1931 printf("Exporting format `%s' to file `%s'\n", output_format_name(output_format), filename);
1932 #endif
1933
1934 if (output_format != LSTOPO_OUTPUT_XML) {
1935 /* there might be some xml-imported userdata in objects, add lstopo-specific userdata in front of them */
1936 lstopo_populate_userdata(hwloc_get_root_obj(topology));
1937 lstopo_add_cpukind_style(&loutput, topology);
1938 /* cpukinds must be before factorizing */
1939 lstopo_add_factorized_attributes(&loutput, topology, hwloc_get_root_obj(topology));
1940 lstopo_add_collapse_attributes(topology);
1941 }
1942
1943 loutput.show_only.depth = HWLOC_TYPE_DEPTH_UNKNOWN; /* disable this feature by default */
1944 if (show_only_string) {
1945 err = hwloc_calc_parse_level(NULL, topology, show_only_string, strlen(show_only_string), &loutput.show_only);
1946 if (err < 0 && loutput.show_only.depth == HWLOC_TYPE_DEPTH_UNKNOWN) {
1947 fprintf(stderr, "level %s passed to --only is unavailable.\n", show_only_string);
1948 goto out_with_topology;
1949 }
1950 /* multiple depth is ok */
1951 }
1952
1953 /******************
1954 * Output for real
1955 */
1956 err = output_func(&loutput, filename);
1957
1958 if (output_format != LSTOPO_OUTPUT_XML) {
1959 /* remove lstopo-specific userdata in front of the list of userdata */
1960 lstopo_destroy_userdata(hwloc_get_root_obj(topology));
1961 }
1962 /* remove the remaining lists of xml-imported userdata */
1963 hwloc_utils_userdata_free_recursive(hwloc_get_root_obj(topology));
1964
1965 hwloc_topology_destroy (topology);
1966
1967 if (loutput.needs_topology_refresh) {
1968 loutput.refreshing = 1;
1969 goto refresh;
1970 }
1971
1972 for(i=0; i<loutput.legend_append_nr; i++)
1973 free(loutput.legend_append[i]);
1974 free(loutput.legend_append);
1975
1976 hwloc_bitmap_free(loutput.cpubind_set);
1977 hwloc_bitmap_free(loutput.membind_set);
1978
1979 return err ? EXIT_FAILURE : EXIT_SUCCESS;
1980
1981 out_usagefailure:
1982 usage (callname, stderr);
1983 goto out;
1984
1985 out_with_topology:
1986 lstopo_destroy_userdata(hwloc_get_root_obj(topology));
1987 hwloc_topology_destroy(topology);
1988 out:
1989 if (input) hwloc_utils_disable_input_format(&input_format);
1990 hwloc_bitmap_free(allow_cpuset);
1991 hwloc_bitmap_free(allow_nodeset);
1992 hwloc_bitmap_free(loutput.cpubind_set);
1993 hwloc_bitmap_free(loutput.membind_set);
1994 if (miscfrom && miscfrom != stdin)
1995 fclose(miscfrom);
1996 return EXIT_FAILURE;
1997 }