/
lstopo.c
1427 lines (1312 loc) · 47 KB
/
lstopo.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2009 CNRS
* Copyright © 2009-2020 Inria. All rights reserved.
* Copyright © 2009-2012, 2015, 2017 Université Bordeaux
* Copyright © 2009-2011 Cisco Systems, Inc. All rights reserved.
* See COPYING in top-level directory.
*/
#include "private/autogen/config.h"
#include "hwloc.h"
#ifdef HWLOC_LINUX_SYS
#include "hwloc/linux.h"
#endif /* HWLOC_LINUX_SYS */
#include "hwloc/shmem.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#include <fcntl.h>
#include <sys/stat.h>
#include <assert.h>
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#ifdef LSTOPO_HAVE_GRAPHICS
#ifdef HWLOC_HAVE_CAIRO
#include <cairo.h>
#endif
#endif
#ifdef HAVE_SETLOCALE
#include <locale.h>
#endif
#include "lstopo.h"
#include "common-ps.h"
#include "misc.h"
#ifdef __MINGW32__
# ifdef HAVE_CLOCK_GETTIME
# undef HAVE_CLOCK_GETTIME
# endif
#endif
#ifdef HAVE_CLOCK_GETTIME
# ifndef CLOCK_MONOTONIC /* HP-UX doesn't have CLOCK_MONOTONIC */
# define CLOCK_MONOTONIC CLOCK_REALTIME
# endif
#endif
FILE *open_output(const char *filename, int overwrite)
{
const char *extn;
struct stat st;
if (!filename || !strcmp(filename, "-"))
return stdout;
extn = strrchr(filename, '.');
if (filename[0] == '-' && extn == filename + 1)
return stdout;
if (!stat(filename, &st) && !overwrite) {
errno = EEXIST;
return NULL;
}
return fopen(filename, "w");
}
const char *task_background_color_string = "#ffff00";
static hwloc_obj_t insert_task(hwloc_topology_t topology, hwloc_cpuset_t cpuset, const char * name, int thread)
{
hwloc_obj_t group, obj;
hwloc_bitmap_and(cpuset, cpuset, hwloc_topology_get_topology_cpuset(topology));
if (hwloc_bitmap_iszero(cpuset))
return NULL;
/* try to insert a group at exact position */
group = hwloc_topology_alloc_group_object(topology);
if (!group)
return NULL;
group->cpuset = hwloc_bitmap_dup(cpuset);
group = hwloc_topology_insert_group_object(topology, group);
if (!group) {
/* try to insert in a larger parent */
char *s, *gs;
hwloc_bitmap_asprintf(&s, cpuset);
group = hwloc_get_obj_covering_cpuset(topology, cpuset);
hwloc_bitmap_asprintf(&gs, group->cpuset);
fprintf(stderr, "%s `%s' binding %s doesn't match any object, extended to %s before inserting the %s in the topology.\n",
thread ? "Thread" : "Process", name, s, gs, thread ? "thread" : "process");
free(s);
free(gs);
}
obj = hwloc_topology_insert_misc_object(topology, group, name);
if (!obj)
fprintf(stderr, "Failed to insert process `%s'\n", name);
else {
obj->subtype = strdup("Process");
if (strcmp(task_background_color_string, "none")) {
char style[19];
snprintf(style, sizeof(style), "Background=%s", task_background_color_string);
hwloc_obj_add_info(obj, "lstopoStyle", style);
}
}
return obj;
}
static void foreach_process_cb(hwloc_topology_t topology,
struct hwloc_ps_process *proc,
void *cbdata __hwloc_attribute_unused)
{
char name[100];
unsigned i;
snprintf(name, sizeof(name), "%ld", proc->pid);
if (*proc->name)
snprintf(name, sizeof(name), "%ld %s", proc->pid, proc->name);
if (proc->bound)
insert_task(topology, proc->cpuset, name, 0);
if (proc->nthreads)
for(i=0; i<proc->nthreads; i++)
if (proc->threads[i].cpuset
&& !hwloc_bitmap_isequal(proc->threads[i].cpuset, proc->cpuset)) {
char task_name[150];
if (*proc->threads[i].name)
snprintf(task_name, sizeof(task_name), "%s %li %s", name, proc->threads[i].tid, proc->threads[i].name);
else
snprintf(task_name, sizeof(task_name), "%s %li", name, proc->threads[i].tid);
insert_task(topology, proc->threads[i].cpuset, task_name, 1);
}
}
static void add_process_objects(hwloc_topology_t topology)
{
const struct hwloc_topology_support *support = hwloc_topology_get_support(topology);
hwloc_obj_t root = hwloc_get_root_obj(topology);
if (!support->cpubind->get_proc_cpubind)
return;
hwloc_ps_foreach_process(topology, root->cpuset,
foreach_process_cb, NULL,
HWLOC_PS_FLAG_THREADS | HWLOC_PS_FLAG_SHORTNAME, NULL, HWLOC_PS_ALL_UIDS, NULL);
}
static __hwloc_inline void lstopo_update_factorize_bounds(unsigned min, unsigned *first, unsigned *last)
{
switch (min) {
case 0:
case 1:
case 2:
*first = 1;
*last = 0;
break;
case 3:
*first = 1;
*last = 1;
break;
default:
*first = 2;
*last = 1;
break;
}
}
static __hwloc_inline void lstopo_update_factorize_alltypes_bounds(struct lstopo_output *loutput)
{
hwloc_obj_type_t type;
for(type = 0; type < HWLOC_OBJ_TYPE_MAX; type++)
lstopo_update_factorize_bounds(loutput->factorize_min[type], &loutput->factorize_first[type], &loutput->factorize_last[type]);
}
static void
lstopo_add_factorized_attributes(struct lstopo_output *loutput, hwloc_obj_t obj)
{
hwloc_obj_t child;
if (!obj->first_child)
return;
if (obj->symmetric_subtree && obj->arity > loutput->factorize_min[obj->first_child->type]){
/* factorize those children */
for_each_child(child, obj) {
unsigned factorized;
if (child->sibling_rank < loutput->factorize_first[child->type]
|| child->sibling_rank >= obj->arity - loutput->factorize_last[child->type])
factorized = 0; /* keep first and last */
else if (child->sibling_rank == loutput->factorize_first[child->type])
factorized = 1; /* replace with dots */
else
factorized = -1; /* remove that one */
((struct lstopo_obj_userdata *)child->userdata)->factorized = factorized;
}
}
/* recurse */
for_each_child(child, obj)
lstopo_add_factorized_attributes(loutput, child);
}
static void
lstopo_add_collapse_attributes(hwloc_topology_t topology)
{
hwloc_obj_t obj, collapser = NULL;
unsigned collapsed = 0;
/* collapse identical PCI devs */
for(obj = hwloc_get_next_pcidev(topology, NULL); obj; obj = hwloc_get_next_pcidev(topology, obj)) {
if (collapser) {
if (!obj->io_arity && !obj->misc_arity
&& obj->parent == collapser->parent
&& obj->attr->pcidev.vendor_id == collapser->attr->pcidev.vendor_id
&& obj->attr->pcidev.device_id == collapser->attr->pcidev.device_id
&& obj->attr->pcidev.subvendor_id == collapser->attr->pcidev.subvendor_id
&& obj->attr->pcidev.subdevice_id == collapser->attr->pcidev.subdevice_id) {
/* collapse another one */
((struct lstopo_obj_userdata *)obj->userdata)->pci_collapsed = -1;
collapsed++;
continue;
} else if (collapsed > 1) {
/* end this collapsing */
((struct lstopo_obj_userdata *)collapser->userdata)->pci_collapsed = collapsed;
collapser = NULL;
collapsed = 0;
}
}
if (!obj->io_arity && !obj->misc_arity) {
/* start a new collapsing */
collapser = obj;
collapsed = 1;
}
}
if (collapsed > 1) {
/* end this collapsing */
((struct lstopo_obj_userdata *)collapser->userdata)->pci_collapsed = collapsed;
}
}
static int
lstopo_check_pci_domains(hwloc_topology_t topology)
{
hwloc_obj_t obj;
/* check PCI devices for domains.
* they are listed by depth-first search, the order doesn't guarantee a domain at the end.
*/
obj = NULL;
while ((obj = hwloc_get_next_pcidev(topology, obj)) != NULL) {
if (obj->attr->pcidev.domain)
return 1;
}
/* check PCI Bridges for domains.
* they are listed by depth-first search, the order doesn't guarantee a domain at the end.
*/
obj = NULL;
while ((obj = hwloc_get_next_bridge(topology, obj)) != NULL) {
if (obj->attr->bridge.upstream_type != HWLOC_OBJ_BRIDGE_PCI)
break;
if (obj->attr->pcidev.domain)
return 1;
}
return 0;
}
static void
lstopo_populate_userdata(hwloc_obj_t parent)
{
hwloc_obj_t child;
struct lstopo_obj_userdata *save = malloc(sizeof(*save));
save->common.buffer = NULL; /* so that it is ignored on XML export */
save->common.next = parent->userdata;
save->factorized = 0;
save->pci_collapsed = 0;
parent->userdata = save;
for_each_child(child, parent)
lstopo_populate_userdata(child);
for_each_memory_child(child, parent)
lstopo_populate_userdata(child);
for_each_io_child(child, parent)
lstopo_populate_userdata(child);
for_each_misc_child(child, parent)
lstopo_populate_userdata(child);
}
static void
lstopo_destroy_userdata(hwloc_obj_t parent)
{
hwloc_obj_t child;
struct lstopo_obj_userdata *save = parent->userdata;
if (save) {
parent->userdata = save->common.next;
free(save);
}
for_each_child(child, parent)
lstopo_destroy_userdata(child);
for_each_memory_child(child, parent)
lstopo_destroy_userdata(child);
for_each_io_child(child, parent)
lstopo_destroy_userdata(child);
for_each_misc_child(child, parent)
lstopo_destroy_userdata(child);
}
void usage(const char *name, FILE *where)
{
fprintf (where, "Usage: %s [ options ] ... [ filename.format ]\n\n", name);
fprintf (where, "See lstopo(1) for more details.\n");
fprintf (where, "\nDefault output is "
#ifdef LSTOPO_HAVE_GRAPHICS
#ifdef HWLOC_WIN_SYS
"graphical"
#elif (defined LSTOPO_HAVE_X11)
"graphical (X11) if DISPLAY is set, console otherwise"
#else
"console"
#endif
#else
"console"
#endif
".\n");
fprintf (where, "Supported output file formats: console, ascii, fig"
#ifdef LSTOPO_HAVE_GRAPHICS
#ifdef CAIRO_HAS_PDF_SURFACE
", pdf"
#endif /* CAIRO_HAS_PDF_SURFACE */
#ifdef CAIRO_HAS_PS_SURFACE
", ps"
#endif /* CAIRO_HAS_PS_SURFACE */
#ifdef CAIRO_HAS_PNG_FUNCTIONS
", png"
#endif /* CAIRO_HAS_PNG_FUNCTIONS */
#ifdef CAIRO_HAS_SVG_SURFACE
", svg(cairo,native)"
#endif /* CAIRO_HAS_SVG_SURFACE */
#endif /* LSTOPO_HAVE_GRAPHICS */
#if !(defined LSTOPO_HAVE_GRAPHICS) || !(defined CAIRO_HAS_SVG_SURFACE)
", svg(native)"
#endif
", xml, synthetic"
"\n");
fprintf (where, "\nFormatting options:\n");
fprintf (where, " -l --logical Display hwloc logical object indexes\n");
fprintf (where, " -p --physical Display OS/physical object indexes\n");
fprintf (where, "Output options:\n");
fprintf (where, " --output-format <format>\n");
fprintf (where, " --of <format> Force the output to use the given format\n");
fprintf (where, " -f --force Overwrite the output file if it exists\n");
fprintf (where, "Textual output options:\n");
fprintf (where, " --only <type> Only show objects of the given type in the textual output\n");
fprintf (where, " -v --verbose Include additional details\n");
fprintf (where, " -s --silent Reduce the amount of details to show\n");
fprintf (where, " --distances Only show distance matrices\n");
fprintf (where, " -c --cpuset Show the cpuset of each object\n");
fprintf (where, " -C --cpuset-only Only show the cpuset of each object\n");
fprintf (where, " --taskset Show taskset-specific cpuset strings\n");
fprintf (where, "Object filtering options:\n");
fprintf (where, " --filter <type>:<knd> Filter objects of the given type, or all.\n");
fprintf (where, " <knd> may be `all' (keep all), `none' (remove all), `structure' or `important'\n");
fprintf (where, " --ignore <type> Ignore objects of the given type\n");
fprintf (where, " --no-smt Ignore PUs\n");
fprintf (where, " --no-caches Do not show caches\n");
fprintf (where, " --no-useless-caches Do not show caches which do not have a hierarchical\n"
" impact\n");
fprintf (where, " --no-icaches Do not show instruction caches\n");
fprintf (where, " --merge Do not show levels that do not have a hierarchical\n"
" impact\n");
fprintf (where, " --no-collapse Do not collapse identical PCI devices\n");
fprintf (where, " --restrict [nodeset=]<bitmap>\n");
fprintf (where, " Restrict the topology to some processors or NUMA nodes.\n");
fprintf (where, " --restrict binding Restrict the topology to the current process binding\n");
fprintf (where, " --restrict-flags <n> Set the flags to be used during restrict\n");
fprintf (where, " --no-io Do not show any I/O device or bridge\n");
fprintf (where, " --no-bridges Do not any I/O bridge except hostbridges\n");
fprintf (where, " --whole-io Show all I/O devices and bridges\n");
fprintf (where, "Input options:\n");
hwloc_utils_input_format_usage(where, 6);
fprintf (where, " --thissystem Assume that the input topology provides the topology\n"
" for the system on which we are running\n");
fprintf (where, " --pid <pid> Detect topology as seen by process <pid>\n");
fprintf (where, " --disallowed Include objects disallowed by administrative limitations\n");
fprintf (where, " --allow <all|local|...> Change the set of objects marked as allowed\n");
fprintf (where, "Graphical output options:\n");
fprintf (where, " --children-order plain\n"
" Display memory children below the parent like any other child\n");
fprintf (where, " --no-factorize Do not factorize identical objects\n");
fprintf (where, " --no-factorize=<type> Do not factorize identical objects of type <type>\n");
fprintf (where, " --factorize Factorize identical objects (default)\n");
fprintf (where, " --factorize=[<type>,]<N>[,<L>[,<F>]]\n");
fprintf (where, " Set the minimum number <N> of objects to factorize,\n");
fprintf (where, " the numbers of first <F> and last <L> to keep,\n");
fprintf (where, " for all or only the given object type <type>\n");
fprintf (where, " --fontsize 10 Set size of text font\n");
fprintf (where, " --gridsize 7 Set size of margin between elements\n");
fprintf (where, " --linespacing 4 Set spacing between lines of text\n");
fprintf (where, " --horiz[=<type,...>] Horizontal graphical layout instead of nearly 4/3 ratio\n");
fprintf (where, " --vert[=<type,...>] Vertical graphical layout instead of nearly 4/3 ratio\n");
fprintf (where, " --rect[=<type,...>] Rectangular graphical layout with nearly 4/3 ratio\n");
fprintf (where, " --text[=<type,...>] Display text for the given object types\n");
fprintf (where, " --no-text[=<type,..>] Do not display text for the given object types\n");
fprintf (where, " --index=[<type,...>] Display indexes for the given object types\n");
fprintf (where, " --no-index=[<type,.>] Do not display indexes for the given object types\n");
fprintf (where, " --attrs=[<type,...>] Display attributes for the given object types\n");
fprintf (where, " --no-attrs=[<type,.>] Do not display attributes for the given object types\n");
fprintf (where, " --no-legend Remove the text legend at the bottom\n");
fprintf (where, " --append-legend <s> Append a new line of text at the bottom of the legend\n");
fprintf (where, " --binding-color none Do not colorize PU and NUMA nodes according to the binding\n");
fprintf (where, " --disallowed-color none Do not colorize disallowed PU and NUMA nodes\n");
fprintf (where, " --top-color <none|#xxyyzz> Change task background color for --top\n");
fprintf (where, "Miscellaneous options:\n");
fprintf (where, " --export-xml-flags <n>\n"
" Set flags during the XML topology export\n");
fprintf (where, " --export-synthetic-flags <n>\n"
" 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, " --version Report version and exit\n");
}
void lstopo_show_interactive_help(void)
{
printf("\n");
printf("Keyboard shortcuts:\n");
printf(" Zooming, scrolling and closing:\n");
printf(" Zoom-in or out ...................... + -\n");
printf(" Reset scale to default .............. 1\n");
printf(" Try to fit scale to window .......... F\n");
printf(" Resize window to the drawing ........ r\n");
printf(" Toggle auto-resizing of the window .. R\n");
printf(" Scroll vertically ................... Up Down PageUp PageDown\n");
printf(" Scroll horizontally ................. Left Right Ctrl+PageUp/Down\n");
printf(" Scroll to the top-left corner ....... Home\n");
printf(" Scroll to the bottom-right corner ... End\n");
printf(" Show this help ...................... h H ?\n");
printf(" Exit ................................ q Q Esc\n");
printf(" Configuration tweaks:\n");
printf(" Toggle factorizing or collapsing .... f\n");
printf(" Switch display mode for indexes ..... i\n");
printf(" Toggle displaying of object text .... t\n");
printf(" Toggle displaying of obj attributes . a\n");
printf(" Toggle color for disallowed objects . d\n");
printf(" Toggle color for binding objects .... b\n");
printf(" Toggle displaying of the legend ..... l\n");
printf(" Export to file with current config .. E\n");
printf("\n\n");
fflush(stdout);
}
static void lstopo__show_interactive_cli_options(const struct lstopo_output *loutput)
{
if (loutput->index_type == LSTOPO_INDEX_TYPE_PHYSICAL)
printf(" -p");
else if (loutput->index_type == LSTOPO_INDEX_TYPE_LOGICAL)
printf(" -l");
else if (loutput->index_type == LSTOPO_INDEX_TYPE_NONE)
printf(" --no-index");
if (!loutput->show_attrs_enabled)
printf(" --no-attrs");
if (!loutput->show_text_enabled)
printf(" --no-text");
if(!loutput->factorize_enabled)
printf(" --no-factorize");
if (!loutput->pci_collapse_enabled)
printf(" --no-collapse");
if (!loutput->show_binding)
printf(" --binding-color none");
if (!loutput->show_disallowed)
printf(" --disallowed-color none");
if (!loutput->legend)
printf(" --no-legend");
}
void lstopo_show_interactive_cli_options(const struct lstopo_output *loutput)
{
#if (defined LSTOPO_HAVE_GRAPHICS) && (defined CAIRO_HAS_PDF_SURFACE)
const char *format = "PDF";
const char *extension = "pdf";
#else
const char *format = "SVG";
const char *extension = "svg";
#endif
printf("\nCommand-line options for the current configuration tweaks:\n");
lstopo__show_interactive_cli_options(loutput);
printf("\n\nTo export to %s:\n", format);
printf(" lstopo <your options>");
lstopo__show_interactive_cli_options(loutput);
printf(" topology.%s\n\n", extension);
}
enum output_format {
LSTOPO_OUTPUT_DEFAULT,
LSTOPO_OUTPUT_CONSOLE,
LSTOPO_OUTPUT_SYNTHETIC,
LSTOPO_OUTPUT_ASCII,
LSTOPO_OUTPUT_FIG,
LSTOPO_OUTPUT_PNG,
LSTOPO_OUTPUT_PDF,
LSTOPO_OUTPUT_PS,
LSTOPO_OUTPUT_SVG,
LSTOPO_OUTPUT_CAIROSVG,
LSTOPO_OUTPUT_NATIVESVG,
LSTOPO_OUTPUT_XML,
LSTOPO_OUTPUT_SHMEM,
LSTOPO_OUTPUT_ERROR
};
static enum output_format
parse_output_format(const char *name, char *callname __hwloc_attribute_unused)
{
if (!hwloc_strncasecmp(name, "default", 3))
return LSTOPO_OUTPUT_DEFAULT;
else if (!hwloc_strncasecmp(name, "console", 3))
return LSTOPO_OUTPUT_CONSOLE;
else if (!strcasecmp(name, "synthetic"))
return LSTOPO_OUTPUT_SYNTHETIC;
else if (!strcasecmp(name, "ascii")
|| !strcasecmp(name, "txt") /* backward compat with 1.10 */)
return LSTOPO_OUTPUT_ASCII;
else if (!strcasecmp(name, "fig"))
return LSTOPO_OUTPUT_FIG;
else if (!strcasecmp(name, "png"))
return LSTOPO_OUTPUT_PNG;
else if (!strcasecmp(name, "pdf"))
return LSTOPO_OUTPUT_PDF;
else if (!strcasecmp(name, "ps"))
return LSTOPO_OUTPUT_PS;
else if (!strcasecmp(name, "svg"))
return LSTOPO_OUTPUT_SVG;
else if (!strcasecmp(name, "cairosvg") || !strcasecmp(name, "svg(cairo)"))
return LSTOPO_OUTPUT_CAIROSVG;
else if (!strcasecmp(name, "nativesvg") || !strcasecmp(name, "svg(native)"))
return LSTOPO_OUTPUT_NATIVESVG;
else if (!strcasecmp(name, "xml"))
return LSTOPO_OUTPUT_XML;
else if (!strcasecmp(name, "shmem"))
return LSTOPO_OUTPUT_SHMEM;
else
return LSTOPO_OUTPUT_ERROR;
}
#define LSTOPO_VERBOSE_MODE_DEFAULT 1
int
main (int argc, char *argv[])
{
int err;
hwloc_topology_t topology;
const char *filename = NULL;
unsigned long flags = 0;
unsigned long restrict_flags = 0;
unsigned long allow_flags = 0;
hwloc_bitmap_t allow_cpuset = NULL, allow_nodeset = NULL;
char * callname;
char * input = NULL;
enum hwloc_utils_input_format input_format = HWLOC_UTILS_INPUT_DEFAULT;
enum output_format output_format = LSTOPO_OUTPUT_DEFAULT;
char *restrictstring = NULL;
struct lstopo_output loutput;
output_method *output_func;
hwloc_membind_policy_t policy;
#ifdef HAVE_CLOCK_GETTIME
struct timespec ts1, ts2;
unsigned long ms;
int measure_load_time = !!getenv("HWLOC_DEBUG_LOAD_TIME");
#endif
char *env;
int top = 0;
int opt;
unsigned i;
callname = strrchr(argv[0], '/');
if (!callname)
callname = argv[0];
else
callname++;
/* skip argv[0], handle options */
argc--;
argv++;
hwloc_utils_check_api_version(callname);
loutput.overwrite = 0;
loutput.index_type = LSTOPO_INDEX_TYPE_DEFAULT;
loutput.verbose_mode = LSTOPO_VERBOSE_MODE_DEFAULT;
loutput.ignore_pus = 0;
loutput.ignore_numanodes = 0;
loutput.pci_collapse_enabled = 1;
loutput.pid_number = -1;
loutput.pid = 0;
loutput.need_pci_domain = 0;
loutput.factorize_enabled = 1;
for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
loutput.factorize_min[i] = FACTORIZE_MIN_DEFAULT;
lstopo_update_factorize_alltypes_bounds(&loutput);
loutput.export_synthetic_flags = 0;
loutput.export_xml_flags = 0;
loutput.shmem_output_addr = 0;
loutput.legend = 1;
loutput.legend_append = NULL;
loutput.legend_append_nr = 0;
snprintf(loutput.title, sizeof(loutput.title), "lstopo");
loutput.show_distances_only = 0;
loutput.show_only = HWLOC_OBJ_TYPE_NONE;
loutput.show_cpuset = 0;
loutput.show_taskset = 0;
loutput.backend_data = NULL;
loutput.methods = NULL;
loutput.no_half_lines = 0;
loutput.plain_children_order = 0;
loutput.fontsize = 10;
loutput.gridsize = 7;
loutput.linespacing = 4;
loutput.text_xscale = 1.0f;
env = getenv("LSTOPO_TEXT_XSCALE");
if (env)
loutput.text_xscale = (float) atof(env);
for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
loutput.force_orient[i] = LSTOPO_ORIENT_NONE;
loutput.force_orient[HWLOC_OBJ_PU] = LSTOPO_ORIENT_HORIZ;
for(i=HWLOC_OBJ_L1CACHE; i<=HWLOC_OBJ_L3ICACHE; i++)
loutput.force_orient[i] = LSTOPO_ORIENT_HORIZ;
loutput.force_orient[HWLOC_OBJ_NUMANODE] = LSTOPO_ORIENT_HORIZ;
loutput.force_orient[HWLOC_OBJ_MEMCACHE] = LSTOPO_ORIENT_HORIZ;
for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++) {
loutput.show_indexes[i] = 1;
loutput.show_attrs[i] = 1;
loutput.show_text[i] = 1;
}
loutput.show_attrs_enabled = 1;
loutput.show_text_enabled = 1;
loutput.show_binding = 1;
loutput.show_disallowed = 1;
/* enable verbose backends */
if (!getenv("HWLOC_XML_VERBOSE"))
putenv((char *) "HWLOC_XML_VERBOSE=1");
if (!getenv("HWLOC_SYNTHETIC_VERBOSE"))
putenv((char *) "HWLOC_SYNTHETIC_VERBOSE=1");
/* Use localized time prints, and utf-8 characters in the ascii output */
#ifdef HAVE_SETLOCALE
setlocale(LC_ALL, "");
#endif
loutput.cpubind_set = hwloc_bitmap_alloc();
loutput.membind_set = hwloc_bitmap_alloc();
if (!loutput.cpubind_set || !loutput.membind_set)
goto out;
err = hwloc_topology_init (&topology);
if (err)
goto out;
hwloc_topology_set_all_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_ALL);
hwloc_topology_set_io_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_IMPORTANT);
while (argc >= 1)
{
opt = 0;
if (!strcmp (argv[0], "-v") || !strcmp (argv[0], "--verbose")) {
loutput.verbose_mode++;
} else if (!strcmp (argv[0], "-s") || !strcmp (argv[0], "--silent")) {
loutput.verbose_mode--;
} else if (!strcmp (argv[0], "--distances")) {
loutput.show_distances_only = 1;
} else if (!strcmp (argv[0], "-h") || !strcmp (argv[0], "--help")) {
usage(callname, stdout);
exit(EXIT_SUCCESS);
} else if (!strcmp (argv[0], "-f") || !strcmp (argv[0], "--force"))
loutput.overwrite = 1;
else if (!strcmp (argv[0], "-l") || !strcmp (argv[0], "--logical"))
loutput.index_type = LSTOPO_INDEX_TYPE_LOGICAL;
else if (!strcmp (argv[0], "-p") || !strcmp (argv[0], "--physical"))
loutput.index_type = LSTOPO_INDEX_TYPE_PHYSICAL;
else if (!strcmp (argv[0], "-c") || !strcmp (argv[0], "--cpuset"))
loutput.show_cpuset = 1;
else if (!strcmp (argv[0], "-C") || !strcmp (argv[0], "--cpuset-only"))
loutput.show_cpuset = 2;
else if (!strcmp (argv[0], "--taskset")) {
loutput.show_taskset = 1;
if (!loutput.show_cpuset)
loutput.show_cpuset = 1;
} else if (!strcmp (argv[0], "--only")) {
if (argc < 2)
goto out_usagefailure;
if (hwloc_type_sscanf(argv[1], &loutput.show_only, NULL, 0) < 0)
fprintf(stderr, "Unsupported type `%s' passed to --only, ignoring.\n", argv[1]);
opt = 1;
}
else if (!strcmp (argv[0], "--filter")) {
hwloc_obj_type_t type = HWLOC_OBJ_TYPE_NONE;
char *colon;
enum hwloc_type_filter_e filter = HWLOC_TYPE_FILTER_KEEP_ALL;
int all = 0;
int allio = 0;
int allcaches = 0;
int allicaches = 0;
if (argc < 2)
goto out_usagefailure;
colon = strchr(argv[1], ':');
if (colon) {
*colon = '\0';
if (!strcmp(colon+1, "none"))
filter = HWLOC_TYPE_FILTER_KEEP_NONE;
else if (!strcmp(colon+1, "all"))
filter = HWLOC_TYPE_FILTER_KEEP_ALL;
else if (!strcmp(colon+1, "structure"))
filter = HWLOC_TYPE_FILTER_KEEP_STRUCTURE;
else if (!strcmp(colon+1, "important"))
filter = HWLOC_TYPE_FILTER_KEEP_IMPORTANT;
else {
fprintf(stderr, "Unsupported filtering kind `%s' passed to --filter.\n", colon+1);
goto out_usagefailure;
}
}
if (!strcmp(argv[1], "all"))
all = 1;
else if (!strcmp(argv[1], "io"))
allio = 1;
else if (!strcmp(argv[1], "cache"))
allcaches = 1;
else if (!strcmp(argv[1], "icache"))
allicaches = 1;
else if (hwloc_type_sscanf(argv[1], &type, NULL, 0) < 0) {
fprintf(stderr, "Unsupported type `%s' passed to --filter.\n", argv[1]);
goto out_usagefailure;
}
if (type == HWLOC_OBJ_PU) {
if (filter == HWLOC_TYPE_FILTER_KEEP_NONE)
loutput.ignore_pus = 1;
}
else if (type == HWLOC_OBJ_NUMANODE) {
if (filter == HWLOC_TYPE_FILTER_KEEP_NONE)
loutput.ignore_numanodes = 1;
}
else if (all)
hwloc_topology_set_all_types_filter(topology, filter);
else if (allio)
hwloc_topology_set_io_types_filter(topology, filter);
else if (allcaches) {
hwloc_topology_set_cache_types_filter(topology, filter);
hwloc_topology_set_type_filter(topology, HWLOC_OBJ_MEMCACHE, filter);
} else if (allicaches)
hwloc_topology_set_icache_types_filter(topology, filter);
else
hwloc_topology_set_type_filter(topology, type, filter);
opt = 1;
}
else if (!strcmp (argv[0], "--ignore")) {
hwloc_obj_type_t type;
if (argc < 2)
goto out_usagefailure;
if (!strcasecmp(argv[1], "cache")) {
fprintf(stderr, "--ignore Cache not supported anymore, use --no-caches instead.\n");
goto out_usagefailure;
}
if (hwloc_type_sscanf(argv[1], &type, NULL, 0) < 0)
fprintf(stderr, "Unsupported type `%s' passed to --ignore, ignoring.\n", argv[1]);
else if (type == HWLOC_OBJ_PU)
loutput.ignore_pus = 1;
else if (type == HWLOC_OBJ_NUMANODE)
loutput.ignore_numanodes = 1;
else
hwloc_topology_set_type_filter(topology, type, HWLOC_TYPE_FILTER_KEEP_NONE);
opt = 1;
}
else if (!strcmp (argv[0], "--no-smt")) {
loutput.ignore_pus = 1;
}
else if (!strcmp (argv[0], "--no-caches")) {
hwloc_topology_set_cache_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_NONE);
hwloc_topology_set_type_filter(topology, HWLOC_OBJ_MEMCACHE, HWLOC_TYPE_FILTER_KEEP_NONE);
}
else if (!strcmp (argv[0], "--no-useless-caches")) {
hwloc_topology_set_cache_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_STRUCTURE);
hwloc_topology_set_type_filter(topology, HWLOC_OBJ_MEMCACHE, HWLOC_TYPE_FILTER_KEEP_STRUCTURE);
}
else if (!strcmp (argv[0], "--no-icaches")) {
hwloc_topology_set_icache_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_NONE);
}
else if (!strcmp (argv[0], "--disallowed") || !strcmp (argv[0], "--whole-system"))
flags |= HWLOC_TOPOLOGY_FLAG_INCLUDE_DISALLOWED;
else if (!strcmp (argv[0], "--allow")) {
if (argc < 2)
goto out_usagefailure;
if (!strcmp(argv[1], "all")) {
allow_flags = HWLOC_ALLOW_FLAG_ALL;
} else if (!strcmp(argv[1], "local")) {
allow_flags = HWLOC_ALLOW_FLAG_LOCAL_RESTRICTIONS;
flags |= HWLOC_TOPOLOGY_FLAG_IS_THISSYSTEM;
} else {
hwloc_bitmap_t set = hwloc_bitmap_alloc();
const char *begin = argv[1];
if (!strncmp(begin, "nodeset=", 8))
begin += 8;
hwloc_bitmap_sscanf(set, begin);
if (begin == argv[1])
allow_cpuset = set;
else
allow_nodeset = set;
allow_flags = HWLOC_ALLOW_FLAG_CUSTOM;
}
opt = 1;
flags |= HWLOC_TOPOLOGY_FLAG_INCLUDE_DISALLOWED;
} else if (!strcmp (argv[0], "--no-io")) {
hwloc_topology_set_io_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_NONE);
} else if (!strcmp (argv[0], "--no-bridges")) {
hwloc_topology_set_type_filter(topology, HWLOC_OBJ_BRIDGE, HWLOC_TYPE_FILTER_KEEP_NONE);
} else if (!strcmp (argv[0], "--whole-io")) {
hwloc_topology_set_io_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_ALL);
} else if (!strcmp (argv[0], "--merge")) {
hwloc_topology_set_all_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_STRUCTURE);
}
else if (!strcmp (argv[0], "--no-collapse"))
loutput.pci_collapse_enabled = 0;
else if (!strcmp (argv[0], "--no-factorize")) {
for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
loutput.factorize_min[i] = FACTORIZE_MIN_DISABLED;
}
else if (!strncmp (argv[0], "--no-factorize=", 15)) {
hwloc_obj_type_t type;
const char *tmp = argv[0]+15;
if (hwloc_type_sscanf(tmp, &type, NULL, 0) < 0) {
fprintf(stderr, "Unsupported parameter `%s' passed to %s, ignoring.\n", tmp, argv[0]);
goto out_usagefailure;
}
loutput.factorize_min[type] = FACTORIZE_MIN_DISABLED;
}
else if (!strcmp (argv[0], "--factorize")) {
for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
loutput.factorize_min[i] = FACTORIZE_MIN_DEFAULT;
lstopo_update_factorize_alltypes_bounds(&loutput);
}
else if (!strncmp (argv[0], "--factorize=", 12)) {
hwloc_obj_type_t type, type_min, type_max;
unsigned min, first, last;
const char *tmp = argv[0]+12;
const char *sep1, *sep2, *sep3;
if (*tmp < '0' || *tmp > '9') {
if (hwloc_type_sscanf(tmp, &type, NULL, 0) < 0) {
fprintf(stderr, "Unsupported type `%s' passed to %s, ignoring.\n", tmp, argv[0]);
goto out_usagefailure;
}
type_min = type;
type_max = type+1;
sep1 = strchr(tmp, ',');
} else {
type_min = HWLOC_OBJ_TYPE_MIN;
type_max = HWLOC_OBJ_TYPE_MAX;
sep1 = tmp-1;
}
if (!sep1) {
min = FACTORIZE_MIN_DEFAULT;
lstopo_update_factorize_bounds(min, &first, &last);
} else {
min = atoi(sep1+1);
lstopo_update_factorize_bounds(min, &first, &last);
sep2 = strchr(sep1+1, ',');
if (sep2) {
first = atoi(sep2+1);
sep3 = strchr(sep2+1, ',');
if (sep3)
last = atoi(sep3+1);
}
}
for(i=type_min; i<(unsigned)type_max; i++) {
loutput.factorize_min[i] = min;
loutput.factorize_first[i] = first;
loutput.factorize_last[i] = last;
}
}
else if (!strcmp (argv[0], "--thissystem"))
flags |= HWLOC_TOPOLOGY_FLAG_IS_THISSYSTEM;
else if (!strcmp (argv[0], "--flags")) {
if (argc < 2)
goto out_usagefailure;
flags = hwloc_utils_parse_topology_flags(argv[1]);
if(flags == (unsigned long)-1)
goto out_with_topology;
opt = 1;
}
else if (!strcmp (argv[0], "--restrict")) {
if (argc < 2)
goto out_usagefailure;
if(strncmp(argv[1], "nodeset=", 8)) {
restrictstring = strdup(argv[1]);
} else {
restrictstring = strdup(argv[1]+8);
restrict_flags |= HWLOC_RESTRICT_FLAG_BYNODESET;
}
opt = 1;
}
else if (!strcmp (argv[0], "--restrict-flags")) {
if (argc < 2)
goto out_usagefailure;
restrict_flags = hwloc_utils_parse_restrict_flags(argv[1]);
if(restrict_flags == (unsigned long)-1)
goto out_with_topology;
opt = 1;
}
else if (!strcmp (argv[0], "--export-xml-flags")) {
if (argc < 2)
goto out_usagefailure;
loutput.export_xml_flags = hwloc_utils_parse_export_xml_flags(argv[1]);
if(loutput.export_xml_flags == (unsigned long)-1)
goto out_with_topology;
opt = 1;
}
else if (!strcmp (argv[0], "--export-synthetic-flags")) {
if (argc < 2)
goto out_usagefailure;
loutput.export_synthetic_flags = hwloc_utils_parse_export_synthetic_flags(argv[1]);
if(loutput.export_synthetic_flags == (unsigned long)-1)
goto out_with_topology;
opt = 1;
}
else if (!strcmp (argv[0], "--horiz"))
for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
loutput.force_orient[i] = LSTOPO_ORIENT_HORIZ;
else if (!strcmp (argv[0], "--vert"))
for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
loutput.force_orient[i] = LSTOPO_ORIENT_VERT;
else if (!strcmp (argv[0], "--rect"))
for(i=HWLOC_OBJ_TYPE_MIN; i<HWLOC_OBJ_TYPE_MAX; i++)
loutput.force_orient[i] = LSTOPO_ORIENT_RECT;
else if (!strncmp (argv[0], "--horiz=", 8)
|| !strncmp (argv[0], "--vert=", 7)
|| !strncmp (argv[0], "--rect=", 7)) {
enum lstopo_orient_e orient = (argv[0][2] == 'h') ? LSTOPO_ORIENT_HORIZ : (argv[0][2] == 'v') ? LSTOPO_ORIENT_VERT : LSTOPO_ORIENT_RECT;
char *tmp = argv[0] + ((argv[0][2] == 'h') ? 8 : 7);
while (tmp) {
char *end = strchr(tmp, ',');
hwloc_obj_type_t type;
if (end)
*end = '\0';
if (hwloc_type_sscanf(tmp, &type, NULL, 0) < 0)
fprintf(stderr, "Unsupported type `%s' passed to %s, ignoring.\n", tmp, argv[0]);
else
loutput.force_orient[type] = orient;
if (!end)
break;
tmp = end+1;
}
}
else if (!strcmp (argv[0], "--binding-color")) {
if (argc < 2)
goto out_usagefailure;
if (!strcmp(argv[1], "none"))
loutput.show_binding = 0;
else
fprintf(stderr, "Unsupported color `%s' passed to %s, ignoring.\n", argv[1], argv[0]);
opt = 1;
}
else if (!strcmp (argv[0], "--disallowed-color")) {
if (argc < 2)
goto out_usagefailure;
if (!strcmp(argv[1], "none"))
loutput.show_disallowed = 0;
else
fprintf(stderr, "Unsupported color `%s' passed to %s, ignoring.\n", argv[1], argv[0]);
opt = 1;
}
else if (!strcmp (argv[0], "--top-color")) {
if (argc < 2)