Debian Codegraph @

utils/lstopo/lstopo-draw.c

raw
1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * Copyright © 2009 CNRS
4 * Copyright © 2009-2022 Inria. All rights reserved.
5 * Copyright © 2009-2013, 2015 Université Bordeaux
6 * Copyright © 2009-2011 Cisco Systems, Inc. All rights reserved.
7 * See COPYING in top-level directory.
8 */
9
10 #include "private/autogen/config.h"
11 #include "hwloc.h"
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <math.h>
17 #include <time.h>
18
19 #include "lstopo.h"
20
21 #define LSTOPO_COLOR(r,g,b) (struct lstopo_color) { r, g, b, 0 }
22 #define LSTOPO_COLOR_GREY(x) (struct lstopo_color) { x, x, x, 0 }
23 #define LSTOPO_COLOR_WHITE LSTOPO_COLOR_GREY(0xff)
24 #define LSTOPO_COLOR_BLACK LSTOPO_COLOR_GREY(0)
25
26 #define EPOXY_R_COLOR 0xe7
27 #define EPOXY_G_COLOR 0xff
28 #define EPOXY_B_COLOR 0xb5
29 #define LSTOPO_COLOR_EPOXY LSTOPO_COLOR(EPOXY_R_COLOR, EPOXY_G_COLOR, EPOXY_B_COLOR)
30
31 #define EPOXY_GREY_COLOR ((EPOXY_R_COLOR+EPOXY_G_COLOR+EPOXY_B_COLOR)/3)
32 #define LSTOPO_COLOR_GREY_EPOXY LSTOPO_COLOR_GREY(EPOXY_GREY_COLOR)
33
34 #define DARK_EPOXY_R_COLOR ((EPOXY_R_COLOR * 100) / 110)
35 #define DARK_EPOXY_G_COLOR ((EPOXY_G_COLOR * 100) / 110)
36 #define DARK_EPOXY_B_COLOR ((EPOXY_B_COLOR * 100) / 110)
37 #define LSTOPO_COLOR_DARK_EPOXY LSTOPO_COLOR(DARK_EPOXY_R_COLOR, DARK_EPOXY_G_COLOR, DARK_EPOXY_B_COLOR)
38
39 #define DARK_EPOXY_GREY_COLOR ((EPOXY_GREY_COLOR * 100) / 110)
40 #define LSTOPO_COLOR_GREY_DARK_EPOXY LSTOPO_COLOR_GREY(DARK_EPOXY_GREY_COLOR)
41
42 #define DARKER_EPOXY_R_COLOR ((DARK_EPOXY_R_COLOR * 100) / 110)
43 #define DARKER_EPOXY_G_COLOR ((DARK_EPOXY_G_COLOR * 100) / 110)
44 #define DARKER_EPOXY_B_COLOR ((DARK_EPOXY_B_COLOR * 100) / 110)
45 #define LSTOPO_COLOR_DARKER_EPOXY LSTOPO_COLOR(DARKER_EPOXY_R_COLOR, DARKER_EPOXY_G_COLOR, DARKER_EPOXY_B_COLOR)
46
47 #define DARKER_EPOXY_GREY_COLOR ((DARK_EPOXY_GREY_COLOR * 100) / 110)
48 #define LSTOPO_COLOR_GREY_DARKER_EPOXY LSTOPO_COLOR_GREY(DARKER_EPOXY_GREY_COLOR)
49
50 struct lstopo_color_palette lstopo_main_palette, lstopo_grey_palette, lstopo_white_palette;
51
52 void
53 lstopo_palette_init(struct lstopo_output *loutput)
54 {
55 #ifdef HWLOC_HAVE_GCC_W_MISSING_FIELD_INITIALIZERS
56 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
57 #endif
58 /* each of these colors must be declared in declare_colors() */
59 lstopo_main_palette.white = LSTOPO_COLOR_WHITE;
60 lstopo_main_palette.black = LSTOPO_COLOR_BLACK;
61 lstopo_main_palette.machine = LSTOPO_COLOR_WHITE;
62 lstopo_main_palette.group = LSTOPO_COLOR_WHITE;
63 lstopo_main_palette.package = LSTOPO_COLOR_DARK_EPOXY;
64 lstopo_main_palette.group_in_package = LSTOPO_COLOR_EPOXY;
65 lstopo_main_palette.die = LSTOPO_COLOR_EPOXY;
66 lstopo_main_palette.core = LSTOPO_COLOR_GREY(0xbe);
67 lstopo_main_palette.pu = LSTOPO_COLOR_WHITE;
68 lstopo_main_palette.numanode = LSTOPO_COLOR(0xef, 0xdf, 0xde);
69 lstopo_main_palette.memories = LSTOPO_COLOR(0xf2, 0xe8, 0xe8); /* slightly lighter than numanode */
70 lstopo_main_palette.cache = LSTOPO_COLOR_WHITE;
71 lstopo_main_palette.pcidev = LSTOPO_COLOR_DARKER_EPOXY;
72 lstopo_main_palette.osdev = LSTOPO_COLOR_GREY(0xde);
73 lstopo_main_palette.bridge = LSTOPO_COLOR_WHITE;
74 lstopo_main_palette.misc = LSTOPO_COLOR_WHITE;
75 lstopo_main_palette.binding = LSTOPO_COLOR(0, 0xff, 0); /* green */
76 lstopo_main_palette.disallowed = LSTOPO_COLOR(0xff, 0, 0); /* red */
77 lstopo_main_palette.process = LSTOPO_COLOR(0xff, 0xff, 0); /* yellow */
78
79 memcpy(&lstopo_grey_palette, &lstopo_main_palette, sizeof(lstopo_main_palette));
80 /* replace non-grey colors by some grey */
81 lstopo_grey_palette.package = LSTOPO_COLOR_GREY_DARK_EPOXY;
82 lstopo_grey_palette.group_in_package = LSTOPO_COLOR_GREY_EPOXY;
83 lstopo_grey_palette.die = LSTOPO_COLOR_GREY_EPOXY;
84 lstopo_grey_palette.numanode = LSTOPO_COLOR_GREY(0xe4);
85 lstopo_grey_palette.memories = LSTOPO_COLOR_GREY(0xe8); /* slightly lighter than numanode */
86 lstopo_grey_palette.pcidev = LSTOPO_COLOR_GREY_DARKER_EPOXY;
87 lstopo_grey_palette.binding = LSTOPO_COLOR_GREY(0xbb);
88 lstopo_grey_palette.disallowed = LSTOPO_COLOR_GREY(0x77);
89 lstopo_grey_palette.process = LSTOPO_COLOR_GREY(0x99);
90
91 memcpy(&lstopo_white_palette, &lstopo_main_palette, sizeof(lstopo_main_palette));
92 /* replace everything but white/black with white */
93 lstopo_white_palette.machine = LSTOPO_COLOR_WHITE;
94 lstopo_white_palette.group = LSTOPO_COLOR_WHITE;
95 lstopo_white_palette.package = LSTOPO_COLOR_WHITE;
96 lstopo_white_palette.group_in_package = LSTOPO_COLOR_WHITE;
97 lstopo_white_palette.die = LSTOPO_COLOR_WHITE;
98 lstopo_white_palette.core = LSTOPO_COLOR_WHITE;
99 lstopo_white_palette.pu = LSTOPO_COLOR_WHITE;
100 lstopo_white_palette.numanode = LSTOPO_COLOR_WHITE;
101 lstopo_white_palette.memories = LSTOPO_COLOR_WHITE;
102 lstopo_white_palette.cache = LSTOPO_COLOR_WHITE;
103 lstopo_white_palette.pcidev = LSTOPO_COLOR_WHITE;
104 lstopo_white_palette.osdev = LSTOPO_COLOR_WHITE;
105 lstopo_white_palette.bridge = LSTOPO_COLOR_WHITE;
106 lstopo_white_palette.misc = LSTOPO_COLOR_WHITE;
107 lstopo_white_palette.binding = LSTOPO_COLOR_WHITE;
108 lstopo_white_palette.disallowed = LSTOPO_COLOR_WHITE;
109 lstopo_white_palette.process = LSTOPO_COLOR_WHITE;
110
111 #ifdef HWLOC_HAVE_GCC_W_MISSING_FIELD_INITIALIZERS
112 #pragma GCC diagnostic warning "-Wmissing-field-initializers"
113 #endif
114
115 /* use the color palette by default */
116 loutput->palette = &lstopo_main_palette;
117 }
118
119 void
120 lstopo_palette_select(struct lstopo_output *loutput, const char *name)
121 {
122 if (!strcmp(name, "grey") || !strcmp(name, "greyscale"))
123 loutput->palette = &lstopo_grey_palette;
124 else if (!strcmp(name, "colors") || !strcmp(name, "default"))
125 loutput->palette = &lstopo_main_palette;
126 else if (!strcmp(name, "white") || !strcmp(name, "none"))
127 loutput->palette = &lstopo_white_palette;
128 else
129 fprintf(stderr, "Unrecognized palette name `%s', ignoring\n", name);
130 }
131
132 void
133 lstopo_palette_set_color(struct lstopo_color *color, unsigned rrggbb)
134 {
135 color->r = (rrggbb >> 16) & 0xff;
136 color->g = (rrggbb >> 8) & 0xff;
137 color->b = (rrggbb >> 0) & 0xff;
138 }
139
140 void
141 lstopo_palette_set_color_by_name(struct lstopo_output *loutput, const char *name, unsigned rrggbb)
142 {
143 if (!strcasecmp(name, "machine"))
144 lstopo_palette_set_color(&loutput->palette->machine, rrggbb);
145 else if (!strcasecmp(name, "group"))
146 lstopo_palette_set_color(&loutput->palette->group, rrggbb);
147 else if (!strcasecmp(name, "package"))
148 lstopo_palette_set_color(&loutput->palette->package, rrggbb);
149 else if (!strcasecmp(name, "group_in_package"))
150 lstopo_palette_set_color(&loutput->palette->group_in_package, rrggbb);
151 else if (!strcasecmp(name, "die"))
152 lstopo_palette_set_color(&loutput->palette->die, rrggbb);
153 else if (!strcasecmp(name, "core"))
154 lstopo_palette_set_color(&loutput->palette->core, rrggbb);
155 else if (!strcasecmp(name, "pu"))
156 lstopo_palette_set_color(&loutput->palette->pu, rrggbb);
157 else if (!strcasecmp(name, "numanode"))
158 lstopo_palette_set_color(&loutput->palette->numanode, rrggbb);
159 else if (!strcasecmp(name, "memories"))
160 lstopo_palette_set_color(&loutput->palette->memories, rrggbb);
161 else if (!strcasecmp(name, "cache"))
162 lstopo_palette_set_color(&loutput->palette->cache, rrggbb);
163 else if (!strcasecmp(name, "pcidev"))
164 lstopo_palette_set_color(&loutput->palette->pcidev, rrggbb);
165 else if (!strcasecmp(name, "osdev"))
166 lstopo_palette_set_color(&loutput->palette->osdev, rrggbb);
167 else if (!strcasecmp(name, "bridge"))
168 lstopo_palette_set_color(&loutput->palette->bridge, rrggbb);
169 else if (!strcasecmp(name, "misc"))
170 lstopo_palette_set_color(&loutput->palette->misc, rrggbb);
171 else
172 fprintf(stderr, "Unrecognized palette color name `%s', ignoring\n", name);
173 /* binding/disallowed/process are handled by --binding/disallowed/top-color */
174 }
175
176 static struct lstopo_color *color_list = NULL;
177
178 static struct lstopo_color *
179 declare_color(struct lstopo_output *loutput, struct lstopo_color *color)
180 {
181 memset(&color->private, 0, sizeof(color->private));
182
183 /* call the backend callback if any */
184 if (loutput->methods->declare_color) {
185 int ret = loutput->methods->declare_color(loutput, color);
186 if (ret < 0)
187 return NULL;
188 }
189
190 /* insert */
191 color->next = color_list;
192 color_list = color;
193
194 return color;
195 }
196
197 void
198 declare_colors(struct lstopo_output *output)
199 {
200 /* don't bother looking for duplicate colors here,
201 * we want to be able to use those structs so always queue them
202 */
203 declare_color(output, &output->palette->white);
204 declare_color(output, &output->palette->black);
205 declare_color(output, &output->palette->machine);
206 declare_color(output, &output->palette->group);
207 declare_color(output, &output->palette->package);
208 declare_color(output, &output->palette->group_in_package);
209 declare_color(output, &output->palette->die);
210 declare_color(output, &output->palette->core);
211 declare_color(output, &output->palette->pu);
212 declare_color(output, &output->palette->numanode);
213 declare_color(output, &output->palette->memories);
214 declare_color(output, &output->palette->cache);
215 declare_color(output, &output->palette->pcidev);
216 declare_color(output, &output->palette->osdev);
217 declare_color(output, &output->palette->bridge);
218 declare_color(output, &output->palette->misc);
219 declare_color(output, &output->palette->binding);
220 declare_color(output, &output->palette->disallowed);
221 declare_color(output, &output->palette->process);
222 }
223
224 void
225 destroy_colors(struct lstopo_output *loutput)
226 {
227 struct lstopo_color *tmp = color_list;
228
229 while (tmp) {
230 struct lstopo_color *next = tmp->next;
231
232 if (loutput->methods->destroy_color)
233 loutput->methods->destroy_color(loutput, tmp);
234 if (tmp->free)
235 free(tmp);
236 tmp = next;
237 }
238
239 color_list = NULL; /* so that it works after refresh */
240 }
241
242 static struct lstopo_color *
243 find_or_declare_rgb_color(struct lstopo_output *loutput, int r, int g, int b)
244 {
245 struct lstopo_color *color, *tmp;
246
247 for(tmp = color_list; tmp; tmp = tmp->next)
248 if (tmp->r == r && tmp->g == g && tmp->b == b)
249 return tmp;
250
251 color = malloc(sizeof(*color));
252 if (!color)
253 return NULL;
254
255 color->r = r & 255;
256 color->g = g & 255;
257 color->b = b & 255;
258 color->free = 1;
259 tmp = declare_color(loutput, color);
260 if (!tmp)
261 free(color);
262 return tmp;
263 }
264
265 static unsigned
266 get_textwidth(void *output,
267 const char *text, unsigned length,
268 unsigned fontsize)
269 {
270 struct lstopo_output *loutput = output;
271 unsigned width;
272
273 #ifdef HWLOC_DEBUG
274 assert(loutput->methods->textsize);
275 #endif
276 loutput->methods->textsize(output, text, length, fontsize, &width);
277 width = (unsigned)(loutput->text_xscale * ((float)width));
278 return width;
279 }
280
281 /*
282 * foo_draw functions take a OBJ, computes which size it needs, recurse into
283 * sublevels with drawing=PREPARE to recursively compute the needed size
284 * without actually drawing anything, then draw things about OBJ (chip draw,
285 * cache size information etc) at (X,Y), recurse into sublevels again to
286 * actually draw things, and return in RETWIDTH and RETHEIGHT the amount of
287 * space that the drawing took.
288 *
289 * For generic detailed comments, see the node_draw function.
290 *
291 * border is added around the objects
292 * separator is added between objects
293 */
294
295 typedef void (*foo_draw)(struct lstopo_output *loutput, hwloc_obj_t obj, unsigned depth, unsigned x, unsigned y);
296
297 static foo_draw get_type_fun(hwloc_obj_type_t type);
298
299 /* next child, in all children list, with memory before CPU, ignoring PU if needed.
300 * similar to hwloc_get_next_child() but returns memory children first.
301 */
302 #define NEXT_CHILD_INIT_STATE -1
303 static hwloc_obj_t next_child(struct lstopo_output *loutput, hwloc_obj_t parent, unsigned kind, hwloc_obj_t prev, int *statep)
304 {
305 int state;
306 hwloc_obj_t obj;
307 if (prev) {
308 obj = prev->next_sibling;
309 state = *statep;
310 } else {
311 obj = NULL;
312 state = NEXT_CHILD_INIT_STATE;
313 }
314
315 again:
316 if (!obj && state <= -1 && (kind & LSTOPO_CHILD_KIND_MEMORY)) {
317 obj = parent->memory_first_child;
318 state = 0;
319 }
320 if (!obj && state <= 0 && (kind & LSTOPO_CHILD_KIND_NORMAL)) {
321 obj = parent->first_child;
322 state = 1;
323 }
324 if (!obj && state <= 1 && (kind & LSTOPO_CHILD_KIND_IO)) {
325 obj = parent->io_first_child;
326 state = 2;
327 }
328 if (!obj && state <= 2 && (kind & LSTOPO_CHILD_KIND_MISC)) {
329 obj = parent->misc_first_child;
330 state = 3;
331 }
332 if (!obj)
333 return NULL;
334
335 if (loutput->factorize_enabled && parent->arity > loutput->factorize_min[obj->type]) {
336 if (((struct lstopo_obj_userdata *)obj->userdata)->factorized < 0) {
337 obj = obj->next_sibling;
338 goto again;
339 }
340 }
341
342 if (obj->type == HWLOC_OBJ_PU && loutput->ignore_pus) {
343 obj = obj->next_sibling;
344 goto again;
345 }
346 if (obj->type == HWLOC_OBJ_NUMANODE && loutput->ignore_numanodes) {
347 obj = obj->next_sibling;
348 goto again;
349 }
350 if (loutput->pci_collapse_enabled && obj->type == HWLOC_OBJ_PCI_DEVICE) {
351 struct lstopo_obj_userdata *lud = obj->userdata;
352 if (lud->pci_collapsed == -1) {
353 obj = obj->next_sibling;
354 goto again;
355 }
356 }
357 *statep = state;
358 return obj;
359 }
360
361 static float pci_link_speed(hwloc_obj_t obj)
362 {
363 if (obj->type == HWLOC_OBJ_PCI_DEVICE)
364 return obj->attr->pcidev.linkspeed;
365 if (obj->type == HWLOC_OBJ_BRIDGE && obj->attr->bridge.upstream_type == HWLOC_OBJ_BRIDGE_PCI)
366 return obj->attr->bridge.upstream.pci.linkspeed;
367 return 0.;
368 }
369
370 /********************************
371 * Placing children in rectangle
372 */
373
374 /* returns a score <= 1. close to 1 is better */
375 static __hwloc_inline
376 float rectangle_score(unsigned width, unsigned height, float ratio)
377 {
378 float score = ((float) width)/height/ratio;
379 if (score > 1)
380 score = 1/score;
381 return score;
382 }
383
384 static void find_children_rectangle(struct lstopo_output *loutput, hwloc_obj_t parent,
385 unsigned kind, unsigned separator,
386 unsigned *rowsp, unsigned *columnsp,
387 float ratio)
388 {
389 unsigned rows = 0, columns = 0, n;
390 unsigned numsubobjs = 0, obj_totwidth = 0, obj_totheight = 0;
391 unsigned obj_avgwidth, obj_avgheight;
392 unsigned area = 0;
393 float idealtotheight, under_score, over_score, best_score = 0.f;
394 hwloc_obj_t child;
395 int ncstate;
396
397 /* Total area for subobjects */
398 child = NULL;
399 while ((child=next_child(loutput, parent, kind, child, &ncstate)) != NULL) {
400 struct lstopo_obj_userdata *clud = child->userdata;
401 numsubobjs++;
402 obj_totwidth += clud->width + separator;
403 obj_totheight += clud->height + separator;
404 area += (clud->width + separator) * (clud->height + separator);
405 }
406
407 /* Average object size */
408 obj_avgwidth = obj_totwidth / numsubobjs;
409 obj_avgheight = obj_totheight / numsubobjs;
410
411 /* Try to find a rectangle fitting exactly */
412 for (n = (unsigned) (float) floor(sqrt(numsubobjs));
413 rows >= (unsigned) (float) ceil(pow(numsubobjs, 0.33)) && n > 1;
414 n--) {
415 float old_best_score = best_score;
416 unsigned p = numsubobjs / n;
417 float np_score; /* n rows x p columns */
418 float pn_score; /* p rows x n columns */
419 if (p <= 1 || p * n != numsubobjs)
420 continue;
421 /* Try both n*p and n*p rectangles */
422 np_score = rectangle_score(p * obj_avgwidth, obj_avgheight * n, ratio);
423 pn_score = rectangle_score(n * obj_avgwidth, obj_avgheight * p, ratio);
424 if (np_score > pn_score) {
425 if (np_score > best_score) {
426 rows = n;
427 columns = p;
428 best_score = np_score;
429 }
430 } else {
431 if (pn_score > best_score) {
432 rows = p;
433 columns = n;
434 best_score = pn_score;
435 }
436 }
437 if (old_best_score == best_score)
438 /* Score didn't improve, no need to try further.
439 * The graph score(n) is an inverted U, with np_score on the left and pn_score on the right.
440 * Se start somewhere in the middle and walk to both ends simultaneously,
441 * Only one side can improve towards the maximum, we stop once once it stops improving.
442 * The other side can just decrease for ever.
443 */
444 goto done;
445 }
446 if (best_score)
447 /* We got a rectangle fitting exactly, it may not be perfect but it's good looking */
448 goto done;
449
450 /* Try to find a rectangle with an incomplete last row */
451
452 /* Ideal total height for spreading that area with ratio */
453 idealtotheight = (float) sqrt(area/ratio);
454 /* approximation of number of rows */
455 rows = (unsigned) (idealtotheight / obj_avgheight);
456 columns = rows ? (numsubobjs + rows - 1) / rows : 1;
457 /* Score obtained by underestimation */
458 under_score = rectangle_score(columns * obj_avgwidth, rows * obj_avgheight, ratio);
459 /* try to overestimate too */
460 rows++;
461 columns = (numsubobjs + rows - 1) / rows;
462 /* Score obtained by overestimation */
463 over_score = rectangle_score(columns * obj_avgwidth, rows * obj_avgheight, ratio);
464 /* Revert back to under estimation if it was better */
465 if (rows > 1 && under_score > over_score) {
466 rows--;
467 columns = (numsubobjs + rows - 1) / rows;
468 }
469
470 done:
471 *rowsp = rows;
472 *columnsp = columns;
473 }
474
475 /**************************
476 * Placing children
477 */
478
479 static void
480 place_children_horiz(struct lstopo_output *loutput, hwloc_obj_t parent,
481 unsigned kind, unsigned border, unsigned separator,
482 unsigned *width, unsigned *height)
483 {
484 unsigned curx = 0;
485 unsigned maxh = 0;
486 hwloc_obj_t child;
487 int ncstate;
488 for(child = next_child(loutput, parent, kind, NULL, &ncstate);
489 child;
490 child = next_child(loutput, parent, kind, child, &ncstate)) {
491 struct lstopo_obj_userdata *clud = child->userdata;
492 clud->xrel = curx + border;
493 clud->yrel = border;
494 if (clud->height > maxh)
495 maxh = clud->height;
496 curx += separator + clud->width;
497 }
498 *width = curx - separator + 2*border;
499 *height = maxh + 2*border;
500 }
501
502 /* bridge object height: small empty box */
503 #define BRIDGE_HEIGHT (gridsize)
504
505 static void
506 place_children_vert(struct lstopo_output *loutput, hwloc_obj_t parent,
507 unsigned kind, unsigned border, unsigned separator,
508 unsigned *width, unsigned *height)
509 {
510 unsigned cury = 0;
511 unsigned maxw = 0;
512 unsigned gridsize = loutput->gridsize;
513 unsigned fontsize = loutput->fontsize;
514 int bridge_parent_with_pcilinkspeed = parent->type == HWLOC_OBJ_BRIDGE
515 && loutput->show_text_enabled && loutput->show_text[HWLOC_OBJ_BRIDGE];
516 hwloc_obj_t child;
517 int ncstate;
518 for(child = next_child(loutput, parent, kind, NULL, &ncstate);
519 child;
520 child = next_child(loutput, parent, kind, child, &ncstate)) {
521 struct lstopo_obj_userdata *clud = child->userdata;
522 unsigned child_height = clud->height;
523 clud->xrel = border;
524 clud->yrel = cury + border;
525 if (clud->width > maxw)
526 maxw = clud->width;
527
528 if (bridge_parent_with_pcilinkspeed && pci_link_speed(child) != 0.)
529 /* make sure padding between children is enough to display pci link speed */
530 if (child_height <= BRIDGE_HEIGHT + fontsize)
531 child_height = BRIDGE_HEIGHT + fontsize;
532
533 cury += separator + child_height;
534 }
535 *width = maxw + 2*border;
536 *height = cury - separator + 2*border;
537 }
538
539 static void
540 place_children_rect(struct lstopo_output *loutput, hwloc_obj_t parent,
541 unsigned kind, unsigned border, unsigned separator,
542 unsigned *width, unsigned *height)
543 {
544 unsigned rows, columns;
545 unsigned totwidth, totheight; /* total children array size, without borders */
546 unsigned rowwidth; /* current row width */
547 unsigned maxheight; /* max height for current row */
548 hwloc_obj_t child;
549 int ncstate;
550 float ratio;
551 int i;
552
553 /* preferred width/height compromise */
554 if (kind == LSTOPO_CHILD_KIND_MEMORY)
555 ratio = 8.f; /* very large for memory above objects since the parent is usually very large */
556 else if (parent->type == HWLOC_OBJ_CORE)
557 ratio = 3.f/4.f; /* rather high Core objects since they often contain 2 PUs that we don't want horizontal */
558 else
559 ratio = 4.f/3.f; /* rather largeother objects */
560 find_children_rectangle(loutput, parent, kind, separator, &rows, &columns, ratio);
561
562 rowwidth = 0;
563 maxheight = 0;
564 totwidth = 0;
565 totheight = 0;
566 for(i = 0, child = next_child(loutput, parent, kind, NULL, &ncstate);
567 child;
568 i++, child = next_child(loutput, parent, kind, child, &ncstate)) {
569 struct lstopo_obj_userdata *clud = child->userdata;
570 /* Newline? */
571 if (i && i%columns == 0) {
572 /* Update total width using new row */
573 if (rowwidth > totwidth)
574 totwidth = rowwidth;
575 rowwidth = 0;
576 /* Update total height */
577 totheight += maxheight + separator;
578 maxheight = 0;
579 }
580 /* Add new child */
581 clud->xrel = rowwidth + border;
582 clud->yrel = totheight + border;
583 rowwidth += clud->width + separator;
584 if (clud->height > maxheight)
585 maxheight = clud->height;
586 }
587 /* Update total width using last row */
588 if (rowwidth > totwidth)
589 totwidth = rowwidth;
590 /* Remove spurious separator on the right */
591 totwidth -= separator;
592 /* Update total height using last row */
593 totheight += maxheight; /* no separator */
594
595 *width = totwidth + 2*border;
596 *height = totheight + 2*border;
597 }
598
599 static void
600 place__children(struct lstopo_output *loutput, hwloc_obj_t parent,
601 unsigned kind,
602 enum lstopo_orient_e *orientp,
603 unsigned border, unsigned separator,
604 unsigned *widthp, unsigned *heightp)
605 {
606 if (*orientp == LSTOPO_ORIENT_HORIZ) {
607 /* force horizontal */
608 place_children_horiz(loutput, parent, kind, border, separator, widthp, heightp);
609
610 } else if (*orientp == LSTOPO_ORIENT_VERT) {
611 /* force vertical */
612 place_children_vert(loutput, parent, kind, border, separator, widthp, heightp);
613
614 } else {
615 /* NONE or forced RECT, do a rectangular placement */
616 place_children_rect(loutput, parent, kind, border, separator, widthp, heightp);
617 }
618 }
619
620 /* Recurse into children to get their size.
621 * Place them.
622 * Save their position and the parent total size for later.
623 */
624 static void
625 place_children(struct lstopo_output *loutput, hwloc_obj_t parent,
626 unsigned xrel, unsigned yrel /* position of children within parent */)
627 {
628 struct lstopo_obj_userdata *plud = parent->userdata;
629 enum lstopo_orient_e main_orient, right_orient, below_orient, above_orient;
630 unsigned border = loutput->gridsize;
631 unsigned separator = loutput->gridsize;
632 unsigned separator_below_cache = loutput->gridsize;
633 unsigned normal_children_separator = loutput->gridsize;
634 unsigned totwidth = plud->width, totheight = plud->height;
635
636 /* Children placement is divided in 4 zones:
637 *
638 * +-------------------------------------------------------------------+
639 * | Above = Memory Children (by default) |
640 * +-----------------------------------+-------------------------------+
641 * | Main = CPU (always), | Right = I/O+Misc (by default) |
642 * | Memory+I/O+Misc (optional) | |
643 * +-----------------------------------+-------------------------------+
644 * | Below = I/O+Misc (optional) |
645 * +-----------------------------------+
646 *
647 * All these children are placed inside the parent box, except:
648 * - Cache parent are between Above and Main.
649 * - Bridges have a special drawing for PCI buses and links
650 *
651 * I/O and Misc parents only have the Main section because
652 * their children are either I/O or Misc, no CPU or Memory.
653 *
654 * Memory parents may have Main (for Memory children)
655 * and below/right for Misc.
656 */
657 unsigned children_width = 0, children_height = 0; /* Main children size */
658 unsigned above_children_width = 0, above_children_height = 0; /* Above children size */
659 unsigned right_children_width = 0, right_children_height = 0; /* Right children size */
660 unsigned below_children_width = 0, below_children_height = 0; /* Below children size */
661 unsigned mrb_children_width = 0, mrb_children_height = 0; /* sum of Main+Right+Below sizes */
662 unsigned existing_kinds;
663 int normal_children_are_PUs;
664 hwloc_obj_t child;
665 int ncstate;
666 unsigned i;
667
668 /* place main children according to the parent type if specified, or to the main layout */
669 main_orient = loutput->force_orient[parent->type];
670 /* place right/below children according to right/below first, or fallback to the parent type */
671 right_orient = loutput->right_force_orient;
672 if (right_orient == LSTOPO_ORIENT_NONE)
673 right_orient = loutput->force_orient[parent->type];
674 below_orient = loutput->below_force_orient;
675 if (below_orient == LSTOPO_ORIENT_NONE)
676 below_orient = loutput->force_orient[parent->type];
677 /* place above children in rectangle by default */
678 above_orient = loutput->above_force_orient;
679 if (above_orient == LSTOPO_ORIENT_NONE)
680 above_orient = LSTOPO_ORIENT_RECT;
681
682 /* defaults */
683 plud->children.box = 0;
684 plud->above_children.box = 0;
685 plud->right_children.box = 0;
686 plud->below_children.box = 0;
687
688 /* list the kinds of children that exist in that parent */
689 existing_kinds = (parent->arity ? LSTOPO_CHILD_KIND_NORMAL : 0)
690 | (parent->memory_arity ? LSTOPO_CHILD_KIND_MEMORY : 0)
691 | (parent->io_arity ? LSTOPO_CHILD_KIND_IO : 0)
692 | (parent->misc_arity ? LSTOPO_CHILD_KIND_MISC : 0);
693 /* all children together by default */
694 plud->children.kinds = existing_kinds;
695 plud->above_children.kinds = 0;
696 plud->right_children.kinds = 0;
697 plud->below_children.kinds = 0;
698 /* if we're not inside a memory object, put memory children above if requested */
699 if (!hwloc_obj_type_is_memory(parent->type)
700 && (loutput->children_order & LSTOPO_ORDER_MEMORY_ABOVE)) {
701 plud->children.kinds &= ~LSTOPO_CHILD_KIND_MEMORY;
702 plud->above_children.kinds |= existing_kinds & LSTOPO_CHILD_KIND_MEMORY;
703 }
704 /* if we're not inside a I/O, put I/O on the right if requested */
705 if (!hwloc_obj_type_is_io(parent->type)
706 && (loutput->children_order & LSTOPO_ORDER_IO_RIGHT)) {
707 plud->children.kinds &= ~LSTOPO_CHILD_KIND_IO;
708 plud->right_children.kinds |= existing_kinds & LSTOPO_CHILD_KIND_IO;
709 }
710 /* if we're not inside a I/O, put I/O below if requested */
711 if (!hwloc_obj_type_is_io(parent->type)
712 && (loutput->children_order & LSTOPO_ORDER_IO_BELOW)) {
713 plud->children.kinds &= ~LSTOPO_CHILD_KIND_IO;
714 plud->below_children.kinds |= existing_kinds & LSTOPO_CHILD_KIND_IO;
715 }
716 /* if we're not inside a Misc, put Misc on the right if requested */
717 if (parent->type != HWLOC_OBJ_MISC
718 && (loutput->children_order & LSTOPO_ORDER_MISC_RIGHT)) {
719 plud->children.kinds &= ~LSTOPO_CHILD_KIND_MISC;
720 plud->right_children.kinds |= existing_kinds & LSTOPO_CHILD_KIND_MISC;
721 }
722 /* if we're not inside a Misc, put Misc below if requested */
723 if (parent->type != HWLOC_OBJ_MISC
724 && (loutput->children_order & LSTOPO_ORDER_MISC_BELOW)) {
725 plud->children.kinds &= ~LSTOPO_CHILD_KIND_MISC;
726 plud->below_children.kinds |= existing_kinds & LSTOPO_CHILD_KIND_MISC;
727 }
728
729 /* bridge children always vertical */
730 if (parent->type == HWLOC_OBJ_BRIDGE)
731 main_orient = LSTOPO_ORIENT_VERT;
732
733 /* if factorizing children, use horizontal by default */
734 if (main_orient == LSTOPO_ORIENT_NONE
735 && parent->symmetric_subtree
736 && parent->first_child
737 && loutput->factorize_enabled
738 && parent->arity > loutput->factorize_min[parent->first_child->type]) {
739 main_orient = LSTOPO_ORIENT_HORIZ;
740 }
741
742 /* if there are memory children and using plain children layout, use horizontal by default */
743 if (main_orient == LSTOPO_ORIENT_NONE
744 && parent->memory_arity
745 && !(loutput->children_order & LSTOPO_ORDER_MEMORY_ABOVE))
746 main_orient = LSTOPO_ORIENT_HORIZ;
747
748 /* recurse into children to prepare their sizes,
749 * and check whether all normal children are PUs. */
750 normal_children_are_PUs = (parent->arity > 0);
751 for(i = 0, child = next_child(loutput, parent, LSTOPO_CHILD_KIND_ALL, NULL, &ncstate);
752 child;
753 i++, child = next_child(loutput, parent, LSTOPO_CHILD_KIND_ALL, child, &ncstate)) {
754 get_type_fun(child->type)(loutput, child, 0, 0, 0);
755 if (hwloc_obj_type_is_normal(child->type) && child->type != HWLOC_OBJ_PU)
756 normal_children_are_PUs = 0;
757 }
758 if (!i)
759 return;
760
761
762 /* no separator between PUs */
763 if (normal_children_are_PUs)
764 normal_children_separator = 0;
765
766 /* add separator between a cache parent and its children */
767 if (hwloc_obj_type_is_cache(parent->type) || parent->type == HWLOC_OBJ_MEMCACHE) {
768 if (normal_children_are_PUs || parent->type == HWLOC_OBJ_MEMCACHE)
769 /* except between cache parent and PU children */
770 separator_below_cache = 0;
771 /* update children placement */
772 yrel += separator_below_cache;
773 }
774
775 /* compute the size of the main children section */
776 if (plud->children.kinds)
777 place__children(loutput, parent, plud->children.kinds, &main_orient, 0, normal_children_separator, &children_width, &children_height);
778
779 /* compute the size of the right children section (I/O and Misc), if any */
780 if (plud->right_children.kinds) {
781 place__children(loutput, parent, plud->right_children.kinds, &right_orient, 0, separator, &right_children_width, &right_children_height);
782 }
783
784 /* compute the size of the below children section (I/O and Misc), if any */
785 if (plud->below_children.kinds) {
786 place__children(loutput, parent, plud->below_children.kinds, &below_orient, 0, separator, &below_children_width, &below_children_height);
787 }
788
789 /* compute the width of the MRB children sections, it may be need for the above children section below */
790 mrb_children_width = children_width + right_children_width + (children_width && right_children_width ? separator : 0);
791 if (mrb_children_width < below_children_width)
792 mrb_children_width = below_children_width;
793 /* MRB height will be computed later, it's more difficult because of possible overlaps */
794
795 /* compute the size of the above children section (Memory), if any */
796 if (plud->above_children.kinds) {
797 int need_box;
798
799 assert(plud->above_children.kinds == LSTOPO_CHILD_KIND_MEMORY);
800
801 /* we need a memory children box if parent isn't a memory object
802 * and if there are multiple objects in the box
803 */
804 need_box = !hwloc_obj_type_is_memory(parent->type)
805 && (parent->memory_arity + parent->memory_first_child->memory_arity > 1);
806
807 place__children(loutput, parent, plud->above_children.kinds, &above_orient, need_box ? border : 0, separator, &above_children_width, &above_children_height);
808 if (parent->type == HWLOC_OBJ_MEMCACHE)
809 above_children_height -= separator;
810
811 if (need_box) {
812 /* if there are multiple memory children, add a box, as large as the parent */
813 if (above_children_width < children_width) {
814 above_children_width = mrb_children_width;
815 }
816 plud->above_children.boxcolor = &loutput->palette->memories;
817 plud->above_children.box = 1;
818
819 } else {
820 /* if there's a single memory child without wide memory box, enlarge that child */
821 struct lstopo_obj_userdata *clud = parent->memory_first_child->userdata;
822 if (clud->width < children_width) {
823 clud->width = mrb_children_width;
824 above_children_width = mrb_children_width;
825 }
826 }
827 }
828
829 /* place the main section, assuming there's no above yet */
830 plud->children.width = children_width;
831 plud->children.height = children_height;
832 plud->children.xrel = xrel;
833 plud->children.yrel = yrel;
834 /* now place the above section and update main */
835 if (plud->above_children.kinds) {
836 plud->above_children.width = above_children_width;
837 plud->above_children.height = above_children_height;
838 plud->above_children.xrel = xrel;
839 plud->above_children.yrel = yrel;
840 plud->children.yrel += above_children_height + separator;
841 }
842 /* place the right section */
843 if (plud->right_children.kinds) {
844 plud->right_children.width = right_children_width;
845 plud->right_children.height = right_children_height;
846 plud->right_children.xrel = plud->children.xrel + children_width + (children_width ? separator : 0);
847 plud->right_children.yrel = plud->children.yrel;
848 }
849 /* place the below section */
850 if (plud->below_children.kinds) {
851 plud->below_children.width = below_children_width;
852 plud->below_children.height = below_children_height;
853 plud->below_children.xrel = plud->children.xrel;
854
855 if (plud->right_children.kinds
856 && below_children_width > children_width
857 && right_children_height > children_height) {
858 /* below section is larger than CPU section, and right section is higher than CPU section.
859 * right and below would overlap.
860 * move the below section below right instead of below main
861 */
862 plud->below_children.yrel = plud->children.yrel + right_children_height + separator;
863 mrb_children_height = right_children_height + below_children_height + separator;
864 } else {
865 plud->below_children.yrel = plud->children.yrel + children_height + (children_height ? separator : 0);
866 mrb_children_height = children_height + below_children_height + (children_height ? separator : 0);
867 }
868 } else {
869 mrb_children_height = children_height > right_children_height ? children_height : right_children_height;
870 }
871
872 /* adjust parent size */
873 if (hwloc_obj_type_is_cache(parent->type) || parent->type == HWLOC_OBJ_MEMCACHE) {
874 /* cache children are below */
875 if (mrb_children_width > totwidth)
876 totwidth = mrb_children_width;
877 if (mrb_children_height)
878 totheight += mrb_children_height + separator_below_cache;
879 if (plud->above_children.kinds) {
880 totheight += above_children_height + separator;
881 if (above_children_width > totwidth)
882 totwidth = above_children_width;
883 }
884
885 } else if (parent->type == HWLOC_OBJ_BRIDGE) {
886 /* bridge children are on the right, within any space between bridge and children */
887 if (children_width)
888 totwidth += children_width;
889 if (children_height > totheight)
890 totheight = children_height;
891 /* no right or below sections here */
892
893 } else {
894 /* normal objects have children inside their box, with space around them */
895 if (mrb_children_width + 2*border > totwidth)
896 totwidth = mrb_children_width + 2*border;
897 if (mrb_children_height)
898 totheight += mrb_children_height + border;
899 if (plud->above_children.kinds) {
900 totheight += above_children_height + separator;
901 if (above_children_width + 2*border > totwidth)
902 totwidth = above_children_width + 2*border;
903 }
904 }
905
906 /* save config for draw_children() later */
907 plud->width = totwidth;
908 plud->height = totheight;
909 }
910
911 /***********************
912 * Drawing children
913 */
914
915 static void
916 draw__children(struct lstopo_output *loutput, hwloc_obj_t parent,
917 struct lstopo_children_position *children,
918 unsigned depth,
919 unsigned x, unsigned y)
920 {
921 hwloc_obj_t child;
922 int ncstate;
923
924 if (children->box)
925 loutput->methods->box(loutput, children->boxcolor, depth, x, children->width, y, children->height, parent, 1);
926
927 for(child = next_child(loutput, parent, children->kinds, NULL, &ncstate);
928 child;
929 child = next_child(loutput, parent, children->kinds, child, &ncstate)) {
930 struct lstopo_obj_userdata *clud = child->userdata;
931 get_type_fun(child->type)(loutput, child, depth-1, x + clud->xrel, y + clud->yrel);
932 }
933 }
934
935 static void
936 draw_children(struct lstopo_output *loutput, hwloc_obj_t parent, unsigned depth,
937 unsigned x, unsigned y)
938 {
939 struct lstopo_obj_userdata *plud = parent->userdata;
940
941 if (plud->children.kinds)
942 draw__children(loutput, parent, &plud->children, depth, x + plud->children.xrel, y + plud->children.yrel);
943
944 if (plud->above_children.kinds)
945 draw__children(loutput, parent, &plud->above_children, depth, x + plud->above_children.xrel, y + plud->above_children.yrel);
946
947 if (plud->right_children.kinds)
948 draw__children(loutput, parent, &plud->right_children, depth, x + plud->right_children.xrel, y + plud->right_children.yrel);
949
950 if (plud->below_children.kinds)
951 draw__children(loutput, parent, &plud->below_children, depth, x + plud->below_children.xrel, y + plud->below_children.yrel);
952 }
953
954 /*******
955 * Misc
956 */
957
958 static int
959 lstopo_obj_snprintf(struct lstopo_output *loutput, char *text, size_t textlen, hwloc_obj_t obj)
960 {
961 enum lstopo_index_type_e index_type = loutput->index_type;
962 unsigned idx;
963 const char *indexprefix;
964 char typestr[32];
965 char indexstr[32]= "";
966 char index2str[32] = "";
967 char attrstr[256];
968 char totmemstr[64] = "";
969 int attrlen;
970
971 /* For Misc and Group, name replaces type+index+attrs */
972 if (obj->name && (obj->type == HWLOC_OBJ_MISC || obj->type == HWLOC_OBJ_GROUP)) {
973 return snprintf(text, textlen, "%s", obj->name);
974 }
975 /* For OSDev, OSDev-type+name replaces type+index+attrs */
976 if (obj->type == HWLOC_OBJ_OS_DEVICE) {
977 /* consider the name as an index and remove it if LSTOPO_INDEX_TYPE_NONE */
978 if (index_type != LSTOPO_INDEX_TYPE_NONE) {
979 hwloc_obj_type_snprintf(typestr, sizeof(typestr), obj, 0);
980 return snprintf(text, textlen, "%s %s", typestr, obj->name);
981 } else {
982 return hwloc_obj_type_snprintf(text, textlen, obj, 0);
983 }
984 }
985
986 /* subtype replaces the basic type name */
987 if (obj->subtype) {
988 snprintf(typestr, sizeof(typestr), "%s", obj->subtype);
989 } else {
990 hwloc_obj_type_snprintf(typestr, sizeof(typestr), obj, 0);
991 }
992
993 if (index_type == LSTOPO_INDEX_TYPE_DEFAULT) {
994 if (obj->type == HWLOC_OBJ_PU || obj->type == HWLOC_OBJ_NUMANODE) {
995 /* by default we show logical+physical for PU/NUMA */
996 idx = obj->logical_index;
997 indexprefix = loutput->logical_index_prefix;
998 } else if (obj->type == HWLOC_OBJ_PACKAGE || obj->type == HWLOC_OBJ_DIE || obj->type == HWLOC_OBJ_CORE) {
999 /* logical only for package+core (so that we see easily how many packages/cores there are */
1000 idx = obj->logical_index;
1001 indexprefix = loutput->logical_index_prefix;
1002 } else {
1003 /* nothing for others */
1004 idx = HWLOC_UNKNOWN_INDEX;
1005 indexprefix = "";
1006 }
1007 } else if (index_type == LSTOPO_INDEX_TYPE_LOGICAL) {
1008 idx = obj->logical_index;
1009 indexprefix = loutput->logical_index_prefix;
1010 } else if (index_type == LSTOPO_INDEX_TYPE_PHYSICAL) {
1011 idx = obj->os_index;
1012 indexprefix = loutput->os_index_prefix;
1013 } else {
1014 /* shutup the compiler */
1015 idx = 0;
1016 indexprefix = "";
1017 }
1018
1019 if (loutput->show_indexes[obj->type]
1020 && index_type != LSTOPO_INDEX_TYPE_NONE
1021 && idx != (unsigned)-1 && obj->depth != 0
1022 && obj->type != HWLOC_OBJ_PCI_DEVICE
1023 && (obj->type != HWLOC_OBJ_BRIDGE || obj->attr->bridge.upstream_type == HWLOC_OBJ_BRIDGE_HOST))
1024 snprintf(indexstr, sizeof(indexstr), "%s%u", indexprefix, idx);
1025
1026 if (index_type == LSTOPO_INDEX_TYPE_DEFAULT && obj->type == HWLOC_OBJ_NUMANODE && loutput->show_indexes[obj->type])
1027 /* NUMA have both P# and L# on the same line (PU is split on 2 lines) */
1028 snprintf(index2str, sizeof(index2str), "%s%u", loutput->os_index_prefix, obj->os_index);
1029
1030 if (loutput->show_attrs_enabled && loutput->show_attrs[obj->type]) {
1031 attrlen = hwloc_obj_attr_snprintf(attrstr, sizeof(attrstr), obj, " ", 0);
1032 /* display the root total_memory (cannot be local_memory since root cannot be a NUMA node) */
1033 if (!obj->parent && obj->total_memory)
1034 snprintf(totmemstr, sizeof(totmemstr), " (%lu%s total)",
1035 (unsigned long) hwloc_memory_size_printf_value(obj->total_memory, 0),
1036 hwloc_memory_size_printf_unit(obj->total_memory, 0));
1037 } else
1038 attrlen = 0;
1039
1040 if (attrlen > 0)
1041 return snprintf(text, textlen, "%s%s%s (%s)%s", typestr, indexstr, index2str, attrstr, totmemstr);
1042 else
1043 return snprintf(text, textlen, "%s%s%s%s", typestr, indexstr, index2str, totmemstr);
1044 }
1045
1046 static void
1047 lstopo__prepare_custom_styles(struct lstopo_output *loutput, hwloc_obj_t obj)
1048 {
1049 struct lstopo_obj_userdata *lud = obj->userdata;
1050 struct lstopo_style *s = &lud->style;
1051 hwloc_obj_t child;
1052 unsigned forcer, forceg, forceb;
1053 const char *stylestr;
1054 struct lstopo_color *lcolor;
1055
1056 lud->style_set = 0;
1057
1058 stylestr = hwloc_obj_get_info_by_name(obj, "lstopoStyle");
1059 if (stylestr) {
1060 while (*stylestr != '\0') {
1061 if (sscanf(stylestr, "%02x%02x%02x", &forcer, &forceg, &forceb) == 3
1062 || sscanf(stylestr, "Background=#%02x%02x%02x", &forcer, &forceg, &forceb) == 3) {
1063 lcolor = find_or_declare_rgb_color(loutput, forcer, forceg, forceb);
1064 if (lcolor) {
1065 s->bg = lcolor;
1066 lud->style_set |= LSTOPO_STYLE_BG;
1067 if (!(lud->style_set & LSTOPO_STYLE_T)) {
1068 s->t = (lcolor->r + lcolor->g + lcolor->b < 0xff) ? &loutput->palette->white : &loutput->palette->black;
1069 lud->style_set |= LSTOPO_STYLE_T;
1070 }
1071 }
1072 } else if (sscanf(stylestr, "Text=#%02x%02x%02x", &forcer, &forceg, &forceb) == 3) {
1073 lcolor = find_or_declare_rgb_color(loutput, forcer, forceg, forceb);
1074 if (lcolor) {
1075 s->t = lcolor;
1076 lud->style_set |= LSTOPO_STYLE_T;
1077 }
1078 } else if (sscanf(stylestr, "Text2=#%02x%02x%02x", &forcer, &forceg, &forceb) == 3) {
1079 lcolor = find_or_declare_rgb_color(loutput, forcer, forceg, forceb);
1080 if (lcolor) {
1081 s->t2 = lcolor;
1082 lud->style_set |= LSTOPO_STYLE_T2;
1083 }
1084 }
1085 stylestr = strchr(stylestr, ';');
1086 if (!stylestr)
1087 break;
1088 stylestr++;
1089 }
1090 }
1091
1092 for_each_child(child, obj)
1093 lstopo__prepare_custom_styles(loutput, child);
1094 for_each_memory_child(child, obj)
1095 lstopo__prepare_custom_styles(loutput, child);
1096 for_each_io_child(child, obj)
1097 lstopo__prepare_custom_styles(loutput, child);
1098 for_each_misc_child(child, obj)
1099 lstopo__prepare_custom_styles(loutput, child);
1100 }
1101
1102 void
1103 lstopo_prepare_custom_styles(struct lstopo_output *loutput)
1104 {
1105 lstopo__prepare_custom_styles(loutput, hwloc_get_root_obj(loutput->topology));
1106 }
1107
1108 static void
1109 lstopo_set_object_color(struct lstopo_output *loutput,
1110 hwloc_obj_t obj,
1111 struct lstopo_style *s)
1112 {
1113 struct lstopo_obj_userdata *lud = obj->userdata;
1114
1115 /* defaults */
1116 s->bg = &loutput->palette->white; /* always overwritten below */
1117 s->t = &loutput->palette->black;
1118 s->t2 = &loutput->palette->black;
1119
1120 switch (obj->type) {
1121
1122 case HWLOC_OBJ_MACHINE:
1123 s->bg = &loutput->palette->machine;
1124 break;
1125
1126 case HWLOC_OBJ_GROUP: {
1127 hwloc_obj_t parent;
1128 s->bg = &loutput->palette->group;
1129 parent = obj->parent;
1130 while (parent) {
1131 if (parent->type == HWLOC_OBJ_PACKAGE) {
1132 s->bg = &loutput->palette->group_in_package;
1133 break;
1134 }
1135 parent = parent->parent;
1136 }
1137 break;
1138 }
1139
1140 case HWLOC_OBJ_MISC:
1141 if (loutput->show_process_color && obj->subtype &&
1142 (!strcmp(obj->subtype, "Process") || !strcmp(obj->subtype, "Thread")))
1143 s->bg = &loutput->palette->process;
1144 else
1145 s->bg = &loutput->palette->misc;
1146 break;
1147
1148 case HWLOC_OBJ_NUMANODE:
1149 if (loutput->show_disallowed && lstopo_numa_disallowed(loutput, obj)) {
1150 s->bg = &loutput->palette->disallowed;
1151 } else if (loutput->show_binding && lstopo_numa_binding(loutput, obj)) {
1152 s->bg = &loutput->palette->binding;
1153 } else {
1154 s->bg = &loutput->palette->numanode;
1155 }
1156 break;
1157
1158 case HWLOC_OBJ_PACKAGE:
1159 s->bg = &loutput->palette->package;
1160 break;
1161
1162 case HWLOC_OBJ_DIE:
1163 s->bg = &loutput->palette->die;
1164 break;
1165
1166 case HWLOC_OBJ_CORE:
1167 s->bg = &loutput->palette->core;
1168 break;
1169
1170 case HWLOC_OBJ_L1CACHE:
1171 case HWLOC_OBJ_L2CACHE:
1172 case HWLOC_OBJ_L3CACHE:
1173 case HWLOC_OBJ_L4CACHE:
1174 case HWLOC_OBJ_L5CACHE:
1175 case HWLOC_OBJ_L1ICACHE:
1176 case HWLOC_OBJ_L2ICACHE:
1177 case HWLOC_OBJ_L3ICACHE:
1178 case HWLOC_OBJ_MEMCACHE:
1179 s->bg = &loutput->palette->cache;
1180 break;
1181
1182 case HWLOC_OBJ_PU:
1183 if (loutput->show_disallowed && lstopo_pu_disallowed(loutput, obj)) {
1184 s->bg = &loutput->palette->disallowed;
1185 } else if (loutput->show_binding && lstopo_pu_binding(loutput, obj)) {
1186 s->bg = &loutput->palette->binding;
1187 } else {
1188 s->bg = &loutput->palette->pu;
1189 }
1190 break;
1191
1192 case HWLOC_OBJ_BRIDGE:
1193 s->bg = &loutput->palette->bridge;
1194 break;
1195
1196 case HWLOC_OBJ_PCI_DEVICE:
1197 s->bg = &loutput->palette->pcidev;
1198 break;
1199
1200 case HWLOC_OBJ_OS_DEVICE:
1201 s->bg = &loutput->palette->osdev;
1202 break;
1203
1204 default:
1205 assert(0);
1206 }
1207
1208 if (lud->style_set & LSTOPO_STYLE_BG)
1209 s->bg = lud->style.bg;
1210 if (lud->style_set & LSTOPO_STYLE_T)
1211 s->t = lud->style.t;
1212 if (lud->style_set & LSTOPO_STYLE_T2)
1213 s->t2 = lud->style.t2;
1214 }
1215
1216 static void
1217 prepare_text(struct lstopo_output *loutput, hwloc_obj_t obj)
1218 {
1219 struct lstopo_obj_userdata *lud = obj->userdata;
1220 unsigned fontsize = loutput->fontsize;
1221 unsigned i;
1222 int n;
1223
1224 /* sane defaults */
1225 lud->ntext = 0;
1226 lud->textwidth = 0;
1227
1228 if (!loutput->show_text_enabled || !loutput->show_text[obj->type])
1229 return;
1230
1231 /* main object identifier line */
1232 if (obj->type == HWLOC_OBJ_PCI_DEVICE && loutput->show_attrs_enabled && loutput->show_attrs[HWLOC_OBJ_PCI_DEVICE]) {
1233 /* PCI text collapsing */
1234 char busid[32];
1235 char _text[64];
1236 lstopo_obj_snprintf(loutput, _text, sizeof(_text), obj);
1237 lstopo_busid_snprintf(loutput, busid, sizeof(busid), obj, lud->pci_collapsed, loutput->need_pci_domain);
1238 if (loutput->pci_collapse_enabled && lud->pci_collapsed > 1) {
1239 n = snprintf(lud->text[0].text, sizeof(lud->text[0].text), "%d x { %s %s }", lud->pci_collapsed, _text, busid);
1240 } else {
1241 n = snprintf(lud->text[0].text, sizeof(lud->text[0].text), "%s %s", _text, busid);
1242 }
1243 } else {
1244 /* normal object text */
1245 n = lstopo_obj_snprintf(loutput, lud->text[0].text, sizeof(lud->text[0].text), obj);
1246 }
1247 lud->ntext = 1;
1248
1249 /* additional lines of text */
1250
1251 if (HWLOC_OBJ_PU == obj->type && loutput->index_type == LSTOPO_INDEX_TYPE_DEFAULT && loutput->show_indexes[obj->type]) {
1252 /* PU P# is on second line */
1253 snprintf(lud->text[lud->ntext++].text, sizeof(lud->text[0].text),
1254 "%s%u",
1255 loutput->os_index_prefix[0] == ' ' ? loutput->os_index_prefix+1 : loutput->os_index_prefix, /* skip the starting space if any */
1256 obj->os_index);
1257 }
1258
1259 if (loutput->show_attrs_enabled && loutput->show_attrs[obj->type]) {
1260 if (HWLOC_OBJ_OS_DEVICE == obj->type) {
1261 if (HWLOC_OBJ_OSDEV_COPROC == obj->attr->osdev.type && obj->subtype) {
1262 /* Coprocessor */
1263 if (!strcmp(obj->subtype, "CUDA")) {
1264 /* CUDA */
1265 const char *value, *value2, *value3;
1266 value = hwloc_obj_get_info_by_name(obj, "CUDAGlobalMemorySize");
1267 if (value) {
1268 unsigned long long mb = strtoull(value, NULL, 10) / 1024;
1269 snprintf(lud->text[lud->ntext++].text, sizeof(lud->text[0].text),
1270 mb >= 10240 ? "%llu GB" : "%llu MB",
1271 mb >= 10240 ? mb/1024 : mb);
1272 }
1273 value = hwloc_obj_get_info_by_name(obj, "CUDAL2CacheSize");
1274 if (value) {
1275 unsigned long long kb = strtoull(value, NULL, 10);
1276 snprintf(lud->text[lud->ntext++].text, sizeof(lud->text[0].text),
1277 kb >= 10240 ? "L2 (%llu MB)" : "L2 (%llu kB)",
1278 kb >= 10240 ? kb/1024 : kb);
1279 }
1280 value = hwloc_obj_get_info_by_name(obj, "CUDAMultiProcessors");
1281 value2 = hwloc_obj_get_info_by_name(obj, "CUDACoresPerMP");
1282 value3 = hwloc_obj_get_info_by_name(obj, "CUDASharedMemorySizePerMP");
1283 if (value && value2 && value3) {
1284 snprintf(lud->text[lud->ntext++].text, sizeof(lud->text[0].text),
1285 "%s MP x (%s cores + %s kB)", value, value2, value3);
1286 }
1287
1288 } else if (!strcmp(obj->subtype, "OpenCL")) {
1289 /* OpenCL */
1290 const char *value;
1291 value = hwloc_obj_get_info_by_name(obj, "OpenCLComputeUnits");
1292 if (value) {
1293 unsigned long long cu = strtoull(value, NULL, 10);
1294 snprintf(lud->text[lud->ntext++].text, sizeof(lud->text[0].text),
1295 "%llu compute units", cu);
1296 }
1297 value = hwloc_obj_get_info_by_name(obj, "OpenCLGlobalMemorySize");
1298 if (value) {
1299 unsigned long long mb = strtoull(value, NULL, 10) / 1024;
1300 snprintf(lud->text[lud->ntext++].text, sizeof(lud->text[0].text),
1301 mb >= 10240 ? "%llu GB" : "%llu MB",
1302 mb >= 10240 ? mb/1024 : mb);
1303 }
1304 } else if (!strcmp(obj->subtype, "VectorEngine")) {
1305 /* NEC Vector Engine */
1306 const char *value;
1307 value = hwloc_obj_get_info_by_name(obj, "VectorEngineCores");
1308 if (value) {
1309 unsigned long long cu = strtoull(value, NULL, 10);
1310 snprintf(lud->text[lud->ntext++].text, sizeof(lud->text[0].text),
1311 "%llu cores", cu);
1312 }
1313 value = hwloc_obj_get_info_by_name(obj, "VectorEngineMemorySize");
1314 if (value) {
1315 unsigned long long mb = strtoull(value, NULL, 10) / 1024;
1316 snprintf(lud->text[lud->ntext++].text, sizeof(lud->text[0].text),
1317 mb >= 10240 ? "%llu GB" : "%llu MB",
1318 mb >= 10240 ? mb/1024 : mb);
1319 }
1320 } else if (!strcmp(obj->subtype, "LevelZero")) {
1321 /* LevelZero */
1322 const char *valueSl, *valueSS, *valueEU, *valueTh, *valueHBM, *valueMem;
1323 valueHBM = hwloc_obj_get_info_by_name(obj, "LevelZeroHBMSize");
1324 if (valueHBM) {
1325 unsigned long long mb = strtoull(valueHBM, NULL, 10) / 1024;
1326 snprintf(lud->text[lud->ntext++].text, sizeof(lud->text[0].text),
1327 mb >= 10240 ? "%llu GB (HBM)" : "%llu MB (HBM)",
1328 mb >= 10240 ? mb/1024 : mb);
1329 }
1330 valueMem = hwloc_obj_get_info_by_name(obj, "LevelZeroDDRSize");
1331 if (!valueMem)
1332 valueMem = hwloc_obj_get_info_by_name(obj, "LevelZeroMemorySize");
1333 if (valueMem) {
1334 unsigned long long mb = strtoull(valueMem, NULL, 10) / 1024;
1335 snprintf(lud->text[lud->ntext++].text, sizeof(lud->text[0].text),
1336 mb >= 10240 ? "%llu GB" : "%llu MB",
1337 mb >= 10240 ? mb/1024 : mb);
1338 }
1339 valueSl = hwloc_obj_get_info_by_name(obj, "LevelZeroNumSlices");
1340 valueSS = hwloc_obj_get_info_by_name(obj, "LevelZeroNumSubslicesPerSlice");
1341 valueEU = hwloc_obj_get_info_by_name(obj, "LevelZeroNumEUsPerSubslice");
1342 valueTh = hwloc_obj_get_info_by_name(obj, "LevelZeroNumThreadsPerEU");
1343 if (valueSl && valueSS && valueEU && valueTh) {
1344 snprintf(lud->text[lud->ntext++].text, sizeof(lud->text[0].text),
1345 "%s Slice%s x %s x %s x %s Threads",
1346 valueSl, atoi(valueSl) > 1 ? "s" : "", valueSS, valueEU, valueTh);
1347 }
1348 }
1349
1350 } else if (HWLOC_OBJ_OSDEV_BLOCK == obj->attr->osdev.type) {
1351 /* Block */
1352 const char *value;
1353 value = hwloc_obj_get_info_by_name(obj, "Size");
1354 if (value) {
1355 unsigned long long mb = strtoull(value, NULL, 10) / 1024;
1356 snprintf(lud->text[lud->ntext++].text, sizeof(lud->text[0].text),
1357 mb >= 10485760 ? "%llu TB" : mb >= 10240 ? "%llu GB" : "%llu MB",
1358 mb >= 10485760 ? mb/1048576 : mb >= 10240 ? mb/1024 : mb);
1359 }
1360 value = hwloc_obj_get_info_by_name(obj, "CXLRAMSize");
1361 if (value) {
1362 unsigned long long mb = strtoull(value, NULL, 10) / 1024;
1363 snprintf(lud->text[lud->ntext++].text, sizeof(lud->text[0].text),
1364 mb >= 10485760 ? "%llu TB (RAM)" : mb >= 10240 ? "%llu GB (RAM)" : "%llu MB (RAM)",
1365 mb >= 10485760 ? mb/1048576 : mb >= 10240 ? mb/1024 : mb);
1366 }
1367 value = hwloc_obj_get_info_by_name(obj, "CXLPMEMSize");
1368 if (value) {
1369 unsigned long long mb = strtoull(value, NULL, 10) / 1024;
1370 snprintf(lud->text[lud->ntext++].text, sizeof(lud->text[0].text),
1371 mb >= 10485760 ? "%llu TB (PMEM)" : mb >= 10240 ? "%llu GB (PMEM)" : "%llu MB (PMEM)",
1372 mb >= 10485760 ? mb/1048576 : mb >= 10240 ? mb/1024 : mb);
1373 }
1374 }
1375 }
1376 }
1377
1378 lud->textwidth = 0;
1379 for(i=0; i<lud->ntext; i++) {
1380 unsigned textwidth;
1381 if (i) /* already computed above for n=0 */
1382 n = (unsigned)strlen(lud->text[i].text);
1383 textwidth = get_textwidth(loutput, lud->text[i].text, n, fontsize);
1384 lud->text[i].width = textwidth;
1385 lud->text[i].xoffset = 0; /* only used for PU in output_compute_pu_min_textwidth() */
1386 if (textwidth > lud->textwidth)
1387 lud->textwidth = textwidth;
1388 }
1389 }
1390
1391 static void
1392 draw_text(struct lstopo_output *loutput, hwloc_obj_t obj, struct lstopo_color *lcolor, unsigned depth, unsigned x, unsigned y)
1393 {
1394 struct draw_methods *methods = loutput->methods;
1395 struct lstopo_obj_userdata *lud = obj->userdata;
1396 unsigned fontsize = loutput->fontsize;
1397 unsigned linespacing = loutput->linespacing;
1398 unsigned i;
1399
1400 if (!loutput->show_text_enabled || !loutput->show_text[obj->type])
1401 return;
1402
1403 for(i=0; i<lud->ntext; i++)
1404 methods->text(loutput, lcolor, fontsize, depth, x + lud->text[i].xoffset, y + i*(linespacing + fontsize), lud->text[i].text, obj, i);
1405 }
1406
1407 static void
1408 pci_device_draw(struct lstopo_output *loutput, hwloc_obj_t level, unsigned depth, unsigned x, unsigned y)
1409 {
1410 struct lstopo_obj_userdata *lud = level->userdata;
1411 unsigned gridsize = loutput->gridsize;
1412 unsigned fontsize = loutput->fontsize;
1413 unsigned overlaidoffset = 0;
1414
1415 if (loutput->pci_collapse_enabled && lud->pci_collapsed > 1) {
1416 /* additional depths and height for overlaid boxes */
1417 depth -= 2;
1418 if (lud->pci_collapsed > 2) {
1419 overlaidoffset = gridsize;
1420 } else {
1421 overlaidoffset = gridsize/2;
1422 }
1423 if (loutput->backend_flags & LSTOPO_BACKEND_FLAG_NO_HALF_LINES)
1424 overlaidoffset *= 2;
1425 }
1426
1427 if (loutput->drawing == LSTOPO_DRAWING_PREPARE) {
1428 /* compute children size and position, our size, and save it */
1429 prepare_text(loutput, level);
1430 lud->width = gridsize + overlaidoffset;
1431 lud->height = gridsize + overlaidoffset;
1432 if (lud->ntext > 0) {
1433 lud->width += lud->textwidth + gridsize;
1434 lud->height += fontsize + gridsize;
1435 }
1436 place_children(loutput, level,
1437 gridsize, lud->height);
1438
1439 } else { /* LSTOPO_DRAWING_DRAW */
1440 struct draw_methods *methods = loutput->methods;
1441 struct lstopo_style style;
1442 unsigned totwidth, totheight;
1443
1444 /* restore our size that was computed during prepare */
1445 totwidth = lud->width;
1446 totheight = lud->height;
1447
1448 lstopo_set_object_color(loutput, level, &style);
1449
1450 if (loutput->pci_collapse_enabled && lud->pci_collapsed > 1) {
1451 methods->box(loutput, style.bg, depth+2, x + overlaidoffset, totwidth - overlaidoffset, y + overlaidoffset, totheight - overlaidoffset, level, 2);
1452 if (lud->pci_collapsed > 2)
1453 methods->box(loutput, style.bg, depth+1, x + overlaidoffset/2, totwidth - overlaidoffset, y + overlaidoffset/2, totheight - overlaidoffset, level, 1);
1454 methods->box(loutput, style.bg, depth, x, totwidth - overlaidoffset, y, totheight - overlaidoffset, level, 0);
1455 } else {
1456 methods->box(loutput, style.bg, depth, x, totwidth, y, totheight, level, 0);
1457 }
1458
1459 draw_text(loutput, level, style.t, depth-1, x + gridsize, y + gridsize);
1460
1461 /* Draw sublevels for real */
1462 draw_children(loutput, level, depth-1, x, y);
1463 }
1464 }
1465
1466 static void
1467 bridge_draw(struct lstopo_output *loutput, hwloc_obj_t level, unsigned depth, unsigned x, unsigned y)
1468 {
1469 struct lstopo_obj_userdata *lud = level->userdata;
1470 unsigned gridsize = loutput->gridsize;
1471 unsigned fontsize = loutput->fontsize;
1472 /* align all children even if only some of them have a linkspeed */
1473 unsigned speedwidth = (loutput->show_text_enabled && loutput->show_text[HWLOC_OBJ_BRIDGE]) ? fontsize + gridsize : 0;
1474
1475 if (loutput->drawing == LSTOPO_DRAWING_PREPARE) {
1476 /* compute children size and position, our size, and save it */
1477 lud->width = 2*gridsize + gridsize + speedwidth;
1478 lud->height = BRIDGE_HEIGHT;
1479 place_children(loutput, level,
1480 3*gridsize + speedwidth, 0);
1481
1482 } else { /* LSTOPO_DRAWING_DRAW */
1483 struct draw_methods *methods = loutput->methods;
1484 struct lstopo_style style;
1485
1486 /* Square and left link */
1487 lstopo_set_object_color(loutput, level, &style);
1488 methods->box(loutput, style.bg, depth, x, gridsize, y + BRIDGE_HEIGHT/2 - gridsize/2, gridsize, level, 0);
1489 methods->line(loutput, depth, x + gridsize, y + BRIDGE_HEIGHT/2, x + 2*gridsize, y + BRIDGE_HEIGHT/2, level, 0);
1490
1491 if (level->io_arity > 0) {
1492 hwloc_obj_t child = NULL;
1493 unsigned ymax = -1;
1494 unsigned ymin = (unsigned) -1;
1495 int ncstate;
1496 int i = 0;
1497 while ((child=next_child(loutput, level, LSTOPO_CHILD_KIND_ALL, child, &ncstate)) != NULL) {
1498 struct lstopo_obj_userdata *clud = child->userdata;
1499 unsigned ymid = y + clud->yrel + BRIDGE_HEIGHT/2;
1500 /* Line to PCI device */
1501 methods->line(loutput, depth-1, x+2*gridsize, ymid, x+3*gridsize+speedwidth, ymid, level, i+2);
1502 if (ymin == (unsigned) -1)
1503 ymin = ymid;
1504 ymax = ymid;
1505 /* Negotiated link speed */
1506 if (loutput->show_text_enabled && loutput->show_text[HWLOC_OBJ_BRIDGE]) {
1507 float speed = pci_link_speed(child);
1508 if (loutput->show_attrs_enabled && loutput->show_attrs[HWLOC_OBJ_BRIDGE] && speed != 0.) {
1509 char text[4];
1510 if (speed >= 10.)
1511 snprintf(text, sizeof(text), "%.0f", child->attr->pcidev.linkspeed);
1512 else
1513 snprintf(text, sizeof(text), "%0.1f", child->attr->pcidev.linkspeed);
1514 methods->text(loutput, style.t2, fontsize, depth-1, x + (5*gridsize/2), ymid + BRIDGE_HEIGHT/2, text, level, i+2);
1515 }
1516 }
1517 i++;
1518 }
1519 methods->line(loutput, depth-1, x+2*gridsize, ymin, x+2*gridsize, ymax, level, 1);
1520
1521 /* Draw sublevels for real */
1522 draw_children(loutput, level, depth-1, x, y);
1523 }
1524 }
1525 }
1526
1527 static void
1528 factorized_draw(struct lstopo_output *loutput, hwloc_obj_t level, unsigned depth, unsigned x, unsigned y)
1529 {
1530 struct lstopo_obj_userdata *lud = level->userdata;
1531 unsigned gridsize = loutput->gridsize;
1532 unsigned fontsize = loutput->fontsize;
1533 unsigned linespacing = loutput->linespacing;
1534 unsigned missingseparator;
1535
1536 /* we need a separator even between PUs */
1537 if ((unsigned)level->depth == loutput->depth-1)
1538 missingseparator = gridsize;
1539 else
1540 missingseparator = 0;
1541
1542 if (loutput->drawing == LSTOPO_DRAWING_PREPARE) {
1543 /* compute children size and position, our size, and save it */
1544 unsigned n, textwidth;
1545 lud->width = gridsize*5; /* space, box, space, box, space */
1546 lud->height = gridsize*2 + linespacing + fontsize + gridsize; /* space, box, linespace, text, gridsize */
1547 sprintf(lud->text[0].text, "%ux total", level->parent->arity);
1548 n = (unsigned)strlen(lud->text[0].text);
1549 textwidth = get_textwidth(loutput, lud->text[0].text, n, fontsize);
1550 lud->text[0].width = textwidth;
1551 if (textwidth > lud->width)
1552 lud->width = textwidth;
1553
1554 lud->text[0].xoffset = 0;
1555 lud->internal_xoffset = 0;
1556 /* We want to center-align the 3 little boxes and the text.
1557 * But they can get severely misplaced if the backend cannot tell us the textwidth precisely.
1558 * Only left-align them by default.
1559 */
1560 if (!(loutput->backend_flags & LSTOPO_BACKEND_FLAG_APPROXIMATIVE_TEXTWIDTH)) {
1561 /* Backends can give the textwidth precisely, center-align them */
1562 if (lud->width > textwidth) {
1563 lud->text[0].xoffset = (lud->width - textwidth)/2;
1564 } else {
1565 lud->internal_xoffset = (textwidth - lud->width)/2;
1566 }
1567 }
1568
1569 lud->ntext = 1;
1570
1571 lud->width += 2*missingseparator;
1572
1573 } else { /* LSTOPO_DRAWING_DRAW */
1574 struct draw_methods *methods = loutput->methods;
1575 struct lstopo_style boxstyle, textstyle;
1576 unsigned boxoffset = lud->internal_xoffset;
1577 unsigned textoffset = lud->text[0].xoffset;
1578
1579 /* boxes use object style, but the text outside uses the parent style */
1580 lstopo_set_object_color(loutput, level, &boxstyle);
1581 lstopo_set_object_color(loutput, level->parent, &textstyle);
1582 methods->box(loutput, boxstyle.bg, depth, x + missingseparator + boxoffset, gridsize, y + gridsize, gridsize, level, 0);
1583 methods->box(loutput, boxstyle.bg, depth, x + missingseparator + boxoffset + 2*gridsize, gridsize, y + gridsize, gridsize, level, 0);
1584 methods->box(loutput, boxstyle.bg, depth, x + missingseparator + boxoffset + 4*gridsize, gridsize, y + gridsize, gridsize, level, 0);
1585 methods->text(loutput, textstyle.t, fontsize, depth, x + missingseparator + textoffset, y + 2 * gridsize + linespacing, lud->text[0].text, level, 0);
1586 }
1587 }
1588
1589 static void
1590 cache_draw(struct lstopo_output *loutput, hwloc_obj_t level, unsigned depth, unsigned x, unsigned y)
1591 {
1592 struct lstopo_obj_userdata *lud = level->userdata;
1593 unsigned gridsize = loutput->gridsize;
1594 unsigned fontsize = loutput->fontsize;
1595
1596 if (loutput->factorize_enabled
1597 && lud->factorized == 1
1598 && level->parent->arity > loutput->factorize_min[level->type]) {
1599 factorized_draw(loutput, level, depth, x, y);
1600 return;
1601 }
1602
1603 if (loutput->drawing == LSTOPO_DRAWING_PREPARE) {
1604 /* compute children size and position, our size, and save it */
1605 prepare_text(loutput, level);
1606 lud->width = gridsize;
1607 lud->height = gridsize;
1608 if (lud->ntext > 0) {
1609 lud->width += lud->textwidth + gridsize;
1610 lud->height += fontsize + gridsize;
1611 }
1612 place_children(loutput, level,
1613 0, lud->height /* the callee with add vertical space if needed */);
1614
1615 } else { /* LSTOPO_DRAWING_DRAW */
1616 struct draw_methods *methods = loutput->methods;
1617 struct lstopo_style style;
1618 unsigned totwidth;
1619 unsigned myoff = 0;
1620 unsigned myheight;
1621
1622 /* restore our size that was computed during prepare */
1623 totwidth = lud->width;
1624
1625 /* totheight also contains children outside of this actual cache box,
1626 * recompute our height without outside children (just like above)
1627 */
1628 myheight = gridsize;
1629 if (lud->ntext > 0)
1630 myheight += fontsize + gridsize;
1631
1632 if (lud->above_children.kinds) {
1633 /* display above_children even above the cache itself */
1634 myoff = lud->above_children.height + gridsize;
1635 lud->above_children.yrel = 0;
1636 }
1637
1638 lstopo_set_object_color(loutput, level, &style);
1639 methods->box(loutput, style.bg, depth, x, totwidth, y + myoff, myheight, level, 0);
1640
1641 draw_text(loutput, level, style.t, depth-1, x + gridsize, y + gridsize + myoff);
1642
1643 /* Draw sublevels for real */
1644 draw_children(loutput, level, depth-1, x, y);
1645 }
1646 }
1647
1648 static void
1649 normal_draw(struct lstopo_output *loutput, hwloc_obj_t level, unsigned depth, unsigned x, unsigned y)
1650 {
1651 struct lstopo_obj_userdata *lud = level->userdata;
1652 unsigned gridsize = loutput->gridsize;
1653 unsigned fontsize = loutput->fontsize;
1654 unsigned linespacing = loutput->linespacing;
1655
1656 if (loutput->factorize_enabled
1657 && lud->factorized == 1
1658 && level->parent->arity > loutput->factorize_min[level->type]) {
1659 factorized_draw(loutput, level, depth, x, y);
1660 return;
1661 }
1662
1663 if (loutput->drawing == LSTOPO_DRAWING_PREPARE) {
1664 /* compute children size and position, our size, and save it */
1665 if (level->type != HWLOC_OBJ_PU) /* PU already computed in output_compute_pu_min_textwidth() earlier */
1666 prepare_text(loutput, level);
1667 lud->width = gridsize;
1668 lud->height = gridsize;
1669 if (lud->ntext > 0) {
1670 lud->width += lud->textwidth + gridsize;
1671 lud->height += fontsize + (fontsize + linespacing) * (lud->ntext - 1) + gridsize;
1672 }
1673 place_children(loutput, level,
1674 gridsize, lud->height);
1675
1676 } else { /* LSTOPO_DRAWING_DRAW */
1677 struct draw_methods *methods = loutput->methods;
1678 struct lstopo_style style;
1679 unsigned totwidth, totheight;
1680
1681 /* restore our size that was computed during prepare */
1682 totwidth = lud->width;
1683 totheight = lud->height;
1684
1685 lstopo_set_object_color(loutput, level, &style);
1686 methods->box(loutput, style.bg, depth, x, totwidth, y, totheight, level, 0);
1687 draw_text(loutput, level, style.t, depth-1, x + gridsize, y + gridsize);
1688
1689 /* Draw sublevels for real */
1690 draw_children(loutput, level, depth-1, x, y);
1691 }
1692 }
1693
1694 static void
1695 output_align_PU_textwidth(struct lstopo_output *loutput)
1696 {
1697 hwloc_topology_t topology = loutput->topology;
1698 unsigned textwidth_max = 0;
1699 hwloc_obj_t pu;
1700 unsigned i;
1701
1702 /* compute the max PU textwidth */
1703 pu = NULL;
1704 while ((pu = hwloc_get_next_obj_by_type(topology, HWLOC_OBJ_PU, pu)) != NULL) {
1705 struct lstopo_obj_userdata *lud = pu->userdata;
1706 prepare_text(loutput, pu);
1707 if (lud->textwidth > textwidth_max)
1708 textwidth_max = lud->textwidth;
1709 }
1710
1711 /* update text placement to match the max textwidth */
1712 pu = NULL;
1713 while ((pu = hwloc_get_next_obj_by_type(topology, HWLOC_OBJ_PU, pu)) != NULL) {
1714 struct lstopo_obj_userdata *lud = pu->userdata;
1715 for(i=0; i<lud->ntext; i++)
1716 if (lud->text[i].width < textwidth_max) {
1717 lud->text[i].xoffset = (textwidth_max-lud->text[i].width)/2;
1718 lud->text[i].width = textwidth_max;
1719 }
1720 lud->textwidth = textwidth_max;
1721 }
1722 }
1723
1724 void
1725 output_draw(struct lstopo_output *loutput)
1726 {
1727 hwloc_topology_t topology = loutput->topology;
1728 struct draw_methods *methods = loutput->methods;
1729 unsigned gridsize = loutput->gridsize;
1730 unsigned fontsize = loutput->fontsize;
1731 unsigned linespacing = loutput->linespacing;
1732 hwloc_obj_t root = hwloc_get_root_obj(topology);
1733 struct lstopo_obj_userdata *rlud = root->userdata;
1734 unsigned depth = 100;
1735 unsigned totwidth, totheight, offset, i, j;
1736
1737 if (loutput->drawing == LSTOPO_DRAWING_PREPARE) {
1738 unsigned maxtextwidth = 0, textwidth;
1739 unsigned ndl = 0;
1740 char hostname[122] = "";
1741 #if defined(HWLOC_WIN_SYS) && !defined(__CYGWIN__)
1742 DWORD hostname_size = sizeof(hostname);
1743 #else
1744 size_t hostname_size = sizeof(hostname);
1745 #endif
1746 unsigned infocount = 0;
1747
1748 /* prepare legend lines and compute the width */
1749 if (loutput->show_legend == LSTOPO_SHOW_LEGEND_ALL) {
1750 time_t t;
1751 const char *forcedhostname = NULL;
1752
1753 /* build the default legend lines */
1754 forcedhostname = hwloc_obj_get_info_by_name(hwloc_get_root_obj(topology), "HostName");
1755 if (!forcedhostname && hwloc_topology_is_thissystem(topology)) {
1756 #if defined(HWLOC_WIN_SYS) && !defined(__CYGWIN__)
1757 GetComputerName(hostname, &hostname_size);
1758 #else
1759 gethostname(hostname, hostname_size);
1760 #endif
1761 }
1762 if (forcedhostname || *hostname) {
1763 snprintf(loutput->legend_default_lines[ndl],
1764 sizeof(loutput->legend_default_lines[ndl]),
1765 "Host: %s",
1766 forcedhostname ? forcedhostname : hostname);
1767 textwidth = get_textwidth(loutput,
1768 loutput->legend_default_lines[ndl],
1769 (unsigned) strlen(loutput->legend_default_lines[ndl]),
1770 fontsize);
1771 if (textwidth > maxtextwidth)
1772 maxtextwidth = textwidth;
1773 ndl++;
1774 }
1775
1776 /* Display whether we're showing physical or logical IDs */
1777 if (loutput->index_type != LSTOPO_INDEX_TYPE_DEFAULT) {
1778 snprintf(loutput->legend_default_lines[ndl],
1779 sizeof(loutput->legend_default_lines[ndl]),
1780 "Indexes: %s",
1781 (loutput->index_type == LSTOPO_INDEX_TYPE_LOGICAL ? "logical" : "physical"));
1782 textwidth = get_textwidth(loutput,
1783 loutput->legend_default_lines[ndl],
1784 (unsigned) strlen(loutput->legend_default_lines[ndl]),
1785 fontsize);
1786 if (textwidth > maxtextwidth)
1787 maxtextwidth = textwidth;
1788 ndl++;
1789 }
1790
1791 /* Display timestamp */
1792 t = time(NULL);
1793 #ifdef HAVE_STRFTIME
1794 {
1795 struct tm *tmp;
1796 tmp = localtime(&t);
1797 strftime(loutput->legend_default_lines[ndl],
1798 sizeof(loutput->legend_default_lines[ndl]),
1799 "Date: %c",
1800 tmp);
1801 }
1802 #else /* HAVE_STRFTIME */
1803 {
1804 char *date;
1805 unsigned n;
1806 date = ctime(&t);
1807 n = (unsigned) strlen(date);
1808 if (n && date[n-1] == '\n') {
1809 date[n-1] = 0;
1810 }
1811 snprintf(loutput->legend_default_lines[ndl],
1812 sizeof(loutput->legend_default_lines[ndl]),
1813 "Date: %s",
1814 date);
1815 }
1816 #endif /* HAVE_STRFTIME */
1817 textwidth = get_textwidth(loutput,
1818 loutput->legend_default_lines[ndl],
1819 (unsigned) strlen(loutput->legend_default_lines[ndl]),
1820 fontsize);
1821 if (textwidth > maxtextwidth)
1822 maxtextwidth = textwidth;
1823 ndl++;
1824 }
1825
1826 if (loutput->show_legend != LSTOPO_SHOW_LEGEND_NONE) {
1827 /* look at custom legend lines in root info attr and --append-legend */
1828 for(i=0; i<root->infos_count; i++) {
1829 if (!strcmp(root->infos[i].name, "lstopoLegend")) {
1830 infocount++;
1831 textwidth = get_textwidth(loutput, root->infos[i].value, (unsigned) strlen(root->infos[i].value), fontsize);
1832 if (textwidth > maxtextwidth)
1833 maxtextwidth = textwidth;
1834 }
1835 }
1836 for(i=0; i<loutput->legend_append_nr; i++) {
1837 textwidth = get_textwidth(loutput, loutput->legend_append[i], (unsigned) strlen(loutput->legend_append[i]), fontsize);
1838 if (textwidth > maxtextwidth)
1839 maxtextwidth = textwidth;
1840 }
1841 }
1842
1843 /* save legend info for later */
1844 loutput->legend_maxtextwidth = maxtextwidth;
1845 loutput->legend_default_lines_nr = ndl;
1846 loutput->legend_info_lines_nr = infocount;
1847
1848 /* compute root size, our size, and save it */
1849
1850 output_align_PU_textwidth(loutput);
1851
1852 get_type_fun(root->type)(loutput, root, depth, 0, 0);
1853
1854 /* loutput width is max(root, legend) */
1855 totwidth = rlud->width;
1856 if (loutput->legend_maxtextwidth + 2*gridsize > totwidth)
1857 totwidth = loutput->legend_maxtextwidth + 2*gridsize;
1858 loutput->width = totwidth;
1859
1860 /* loutput height is sum(root, legend) */
1861 totheight = rlud->height;
1862 if (loutput->show_legend != LSTOPO_SHOW_LEGEND_NONE
1863 && (ndl + infocount + loutput->legend_append_nr))
1864 totheight += gridsize
1865 + (ndl + infocount + loutput->legend_append_nr - 1) * (linespacing + fontsize)
1866 + fontsize + gridsize;
1867 loutput->height = totheight;
1868
1869 } else { /* LSTOPO_DRAWING_DRAW */
1870 /* restore our size that was computed during prepare */
1871 totwidth = rlud->width;
1872 totheight = rlud->height;
1873
1874 /* Draw root for real */
1875 get_type_fun(root->type)(loutput, root, depth, 0, 0);
1876
1877 /* Draw legend */
1878 if (loutput->show_legend != LSTOPO_SHOW_LEGEND_NONE
1879 && (loutput->legend_default_lines_nr + loutput->legend_info_lines_nr + loutput->legend_append_nr)) {
1880 offset = rlud->height + gridsize;
1881 methods->box(loutput, &loutput->palette->white, depth,
1882 0,
1883 loutput->width,
1884 totheight,
1885 gridsize
1886 + (loutput->legend_default_lines_nr + loutput->legend_info_lines_nr + loutput->legend_append_nr - 1) * (linespacing + fontsize)
1887 + fontsize + gridsize,
1888 NULL, 0);
1889 for(i=0; i<loutput->legend_default_lines_nr; i++, offset += linespacing + fontsize)
1890 methods->text(loutput, &loutput->palette->black, fontsize, depth, gridsize, offset, loutput->legend_default_lines[i], NULL, i);
1891 for(i=0, j=0; i<root->infos_count; i++) {
1892 if (!strcmp(root->infos[i].name, "lstopoLegend")) {
1893 methods->text(loutput, &loutput->palette->black, fontsize, depth, gridsize, offset, root->infos[i].value, NULL, j+loutput->legend_default_lines_nr);
1894 j++;
1895 offset += linespacing + fontsize;
1896 }
1897 }
1898 for(i=0; i<loutput->legend_append_nr; i++, offset += linespacing + fontsize)
1899 methods->text(loutput, &loutput->palette->black, fontsize, depth, gridsize, offset, loutput->legend_append[i], NULL, i+loutput->legend_default_lines_nr+loutput->legend_info_lines_nr);
1900 }
1901 }
1902 }
1903
1904 /*
1905 * given a type, return a pointer FUN to the function that draws it.
1906 */
1907 static foo_draw
1908 get_type_fun(hwloc_obj_type_t type)
1909 {
1910 switch (type) {
1911 case HWLOC_OBJ_MACHINE:
1912 case HWLOC_OBJ_NUMANODE:
1913 case HWLOC_OBJ_PACKAGE:
1914 case HWLOC_OBJ_DIE:
1915 case HWLOC_OBJ_CORE:
1916 case HWLOC_OBJ_PU:
1917 case HWLOC_OBJ_GROUP:
1918 case HWLOC_OBJ_OS_DEVICE:
1919 case HWLOC_OBJ_MISC: return normal_draw;
1920 case HWLOC_OBJ_L1CACHE: return cache_draw;
1921 case HWLOC_OBJ_L2CACHE: return cache_draw;
1922 case HWLOC_OBJ_L3CACHE: return cache_draw;
1923 case HWLOC_OBJ_L4CACHE: return cache_draw;
1924 case HWLOC_OBJ_L5CACHE: return cache_draw;
1925 case HWLOC_OBJ_L1ICACHE: return cache_draw;
1926 case HWLOC_OBJ_L2ICACHE: return cache_draw;
1927 case HWLOC_OBJ_L3ICACHE: return cache_draw;
1928 case HWLOC_OBJ_MEMCACHE: return cache_draw;
1929 case HWLOC_OBJ_PCI_DEVICE: return pci_device_draw;
1930 case HWLOC_OBJ_BRIDGE: return bridge_draw;
1931 default:
1932 case HWLOC_OBJ_TYPE_MAX: assert(0);
1933 }
1934 /* for dumb compilers */
1935 return normal_draw;
1936 }