| 1 |
/* |
| 2 |
* SPDX-License-Identifier: BSD-3-Clause |
| 3 |
* Copyright © 2009 CNRS |
| 4 |
* Copyright © 2009-2024 Inria. All rights reserved. |
| 5 |
* Copyright © 2009-2010, 2012, 2015 Université Bordeaux |
| 6 |
* Copyright © 2011 Cisco Systems, Inc. All rights reserved. |
| 7 |
* Copyright © 2020 Hewlett Packard Enterprise. All rights reserved. |
| 8 |
* See COPYING in top-level directory. |
| 9 |
*/ |
| 10 |
|
| 11 |
#ifndef UTILS_LSTOPO_H |
| 12 |
#define UTILS_LSTOPO_H |
| 13 |
|
| 14 |
#include "private/autogen/config.h" |
| 15 |
#include "hwloc.h" |
| 16 |
#include "misc.h" |
| 17 |
#include "hwloc-calc.h" |
| 18 |
|
| 19 |
enum lstopo_drawing_e { |
| 20 |
LSTOPO_DRAWING_PREPARE, |
| 21 |
LSTOPO_DRAWING_DRAW |
| 22 |
}; |
| 23 |
|
| 24 |
enum lstopo_orient_e { |
| 25 |
LSTOPO_ORIENT_NONE = 0, |
| 26 |
LSTOPO_ORIENT_HORIZ, |
| 27 |
LSTOPO_ORIENT_VERT, |
| 28 |
LSTOPO_ORIENT_RECT |
| 29 |
}; |
| 30 |
|
| 31 |
enum lstopo_index_type_e { |
| 32 |
LSTOPO_INDEX_TYPE_DEFAULT, |
| 33 |
LSTOPO_INDEX_TYPE_PHYSICAL, |
| 34 |
LSTOPO_INDEX_TYPE_LOGICAL, |
| 35 |
LSTOPO_INDEX_TYPE_NONE /* only used during the interactive display */ |
| 36 |
}; |
| 37 |
|
| 38 |
enum lstopo_show_legend_e { |
| 39 |
LSTOPO_SHOW_LEGEND_ALL, |
| 40 |
LSTOPO_SHOW_LEGEND_NONE, |
| 41 |
LSTOPO_SHOW_LEGEND_NO_DEFAULT |
| 42 |
}; |
| 43 |
|
| 44 |
enum lstopo_order_e { |
| 45 |
LSTOPO_ORDER_PLAIN = 0, |
| 46 |
LSTOPO_ORDER_MEMORY_ABOVE = (1<<0), |
| 47 |
LSTOPO_ORDER_IO_RIGHT = (1<<1), |
| 48 |
LSTOPO_ORDER_IO_BELOW = (1<<2), |
| 49 |
LSTOPO_ORDER_MISC_RIGHT = (1<<3), |
| 50 |
LSTOPO_ORDER_MISC_BELOW = (1<<4) |
| 51 |
}; |
| 52 |
|
| 53 |
FILE *open_output(const char *filename, int overwrite) __hwloc_attribute_malloc; |
| 54 |
|
| 55 |
struct draw_methods; |
| 56 |
|
| 57 |
/* if embedded in backend-specific output structure, must be at the beginning */ |
| 58 |
struct lstopo_output { |
| 59 |
hwloc_topology_t topology; |
| 60 |
unsigned depth; |
| 61 |
|
| 62 |
/* when an interactive backends want a refresh of the topology */ |
| 63 |
int needs_topology_refresh; |
| 64 |
/* when actually doing a refresh */ |
| 65 |
int refreshing; |
| 66 |
|
| 67 |
/* file config */ |
| 68 |
FILE *file; |
| 69 |
int overwrite; |
| 70 |
|
| 71 |
/* misc config */ |
| 72 |
enum lstopo_index_type_e index_type; |
| 73 |
int verbose_mode; |
| 74 |
int ignore_pus; |
| 75 |
int ignore_numanodes; |
| 76 |
int pci_collapse_enabled; /* global toggle for PCI collapsing */ |
| 77 |
int pid_number; |
| 78 |
hwloc_pid_t pid; |
| 79 |
hwloc_bitmap_t cpubind_set, membind_set; |
| 80 |
|
| 81 |
/* misc cached data */ |
| 82 |
int need_pci_domain; |
| 83 |
unsigned nr_cpukind_styles; |
| 84 |
|
| 85 |
/* export config */ |
| 86 |
unsigned long export_synthetic_flags; |
| 87 |
unsigned long export_xml_flags; |
| 88 |
uint64_t shmem_output_addr; |
| 89 |
|
| 90 |
/* legend */ |
| 91 |
enum lstopo_show_legend_e show_legend; |
| 92 |
#define LSTOPO_LEGEND_DEFAULT_LINES 3 /* hostname + index + date */ |
| 93 |
char legend_default_lines[LSTOPO_LEGEND_DEFAULT_LINES][128]; |
| 94 |
unsigned legend_default_lines_nr; |
| 95 |
unsigned legend_info_lines_nr; |
| 96 |
char ** legend_append; |
| 97 |
unsigned legend_append_nr; |
| 98 |
unsigned legend_maxtextwidth; |
| 99 |
|
| 100 |
/* text config */ |
| 101 |
int show_distances_only; |
| 102 |
int show_memattrs_only; |
| 103 |
int show_cpukinds_only; |
| 104 |
int show_windows_processor_groups_only; |
| 105 |
struct hwloc_calc_level show_only; |
| 106 |
int show_cpuset; |
| 107 |
enum hwloc_utils_cpuset_format_e cpuset_output_format; |
| 108 |
int transform_distances; |
| 109 |
|
| 110 |
/* draw config */ |
| 111 |
char title[256]; |
| 112 |
unsigned children_order; /* OR'd set of enum lstopo_order_e */ |
| 113 |
unsigned int gridsize, fontsize, linespacing, thickness; |
| 114 |
float text_xscale; |
| 115 |
enum lstopo_orient_e force_orient[HWLOC_OBJ_TYPE_MAX]; /* orientation of children within an object of the given type */ |
| 116 |
enum lstopo_orient_e above_force_orient; |
| 117 |
enum lstopo_orient_e right_force_orient; |
| 118 |
enum lstopo_orient_e below_force_orient; |
| 119 |
int show_indexes[HWLOC_OBJ_TYPE_MAX]; /* enabled by global toggle index_type */ |
| 120 |
int show_text_enabled; /* global toggle for interactive keyboard shortcuts */ |
| 121 |
int show_text[HWLOC_OBJ_TYPE_MAX]; |
| 122 |
int show_attrs_enabled; /* global toggle for interactive keyboard shortcuts */ |
| 123 |
int show_attrs[HWLOC_OBJ_TYPE_MAX]; |
| 124 |
int show_binding; |
| 125 |
int show_disallowed; |
| 126 |
int show_process_color; |
| 127 |
int show_cpukinds; |
| 128 |
char *logical_index_prefix; |
| 129 |
char *os_index_prefix; |
| 130 |
int factorize_enabled; /* global toggle for interactive keyboard shortcuts */ |
| 131 |
unsigned factorize_min[HWLOC_OBJ_TYPE_MAX]; /* minimum number of object before factorizing (parent->arity must be strictly higher) */ |
| 132 |
#define FACTORIZE_MIN_DEFAULT 4 |
| 133 |
#define FACTORIZE_MIN_DISABLED UINT_MAX |
| 134 |
unsigned factorize_first[HWLOC_OBJ_TYPE_MAX]; /* number of first children to keep before factorizing */ |
| 135 |
unsigned factorize_last[HWLOC_OBJ_TYPE_MAX]; /* number of last children to keep after factorizing */ |
| 136 |
struct lstopo_color_palette *palette; |
| 137 |
|
| 138 |
/* draw internal data */ |
| 139 |
void *backend_data; |
| 140 |
#define LSTOPO_BACKEND_FLAG_NO_HALF_LINES (1UL<<0) /* ASCII backend cannot draw lines between grid lines */ |
| 141 |
#define LSTOPO_BACKEND_FLAG_APPROXIMATIVE_TEXTWIDTH (1UL<<1) /* Tikz and fig backends cannot measure text width precisely */ |
| 142 |
unsigned long backend_flags; |
| 143 |
struct draw_methods *methods; |
| 144 |
enum lstopo_drawing_e drawing; |
| 145 |
unsigned width, height; /* total output size */ |
| 146 |
}; |
| 147 |
|
| 148 |
struct lstopo_color { |
| 149 |
/* these variables must be initialized before passing the structure to declare_color() */ |
| 150 |
int r, g, b; |
| 151 |
int free; /* 1 if lstopo should free() this structure at exit */ |
| 152 |
|
| 153 |
/* these variable are initialized by declare_color() */ |
| 154 |
/* backend specific private data */ |
| 155 |
union lstopo_color_private_u { |
| 156 |
struct lstopo_color_private_ascii_s { |
| 157 |
int color; |
| 158 |
} ascii; |
| 159 |
struct lstopo_color_private_fig_s { |
| 160 |
int color; |
| 161 |
} fig; |
| 162 |
#ifdef HWLOC_WIN_SYS |
| 163 |
struct lstopo_color_private_windows_s { |
| 164 |
HGDIOBJ brush; |
| 165 |
COLORREF color; |
| 166 |
} windows; |
| 167 |
#endif |
| 168 |
} private; |
| 169 |
/* list of colors */ |
| 170 |
struct lstopo_color *next; |
| 171 |
}; |
| 172 |
|
| 173 |
struct lstopo_color_palette { |
| 174 |
struct lstopo_color |
| 175 |
white, /* used for legend background, and text on dark background */ |
| 176 |
black, /* used for text on light background, and legend text */ |
| 177 |
/* all colors below are box backgrounds */ |
| 178 |
machine, |
| 179 |
group, |
| 180 |
package, |
| 181 |
group_in_package, |
| 182 |
die, |
| 183 |
core, |
| 184 |
pu, |
| 185 |
numanode, |
| 186 |
memories, |
| 187 |
cache, |
| 188 |
pcidev, |
| 189 |
osdev, |
| 190 |
bridge, |
| 191 |
misc, |
| 192 |
binding, |
| 193 |
disallowed, |
| 194 |
process; |
| 195 |
}; |
| 196 |
|
| 197 |
extern void lstopo_palette_init(struct lstopo_output *loutput); |
| 198 |
extern void lstopo_palette_select(struct lstopo_output *loutput, const char *name); |
| 199 |
extern void lstopo_palette_set_color(struct lstopo_color *color, unsigned rrggbb); |
| 200 |
extern void lstopo_palette_set_color_by_name(struct lstopo_output *output, const char *name, unsigned rrggbb); |
| 201 |
|
| 202 |
struct lstopo_style { |
| 203 |
struct lstopo_color |
| 204 |
*bg, /* main box background color */ |
| 205 |
*t, /* main text color */ |
| 206 |
*t2; /* other text color */ |
| 207 |
}; |
| 208 |
|
| 209 |
#define LSTOPO_CHILD_KIND_NORMAL 0x1 |
| 210 |
#define LSTOPO_CHILD_KIND_MEMORY 0x2 |
| 211 |
#define LSTOPO_CHILD_KIND_IO 0x4 |
| 212 |
#define LSTOPO_CHILD_KIND_MISC 0x8 |
| 213 |
#define LSTOPO_CHILD_KIND_ALL 0xf |
| 214 |
|
| 215 |
struct lstopo_obj_userdata { |
| 216 |
/* original common userdata (we replace the first one with this extended structure) */ |
| 217 |
struct hwloc_utils_userdata common; |
| 218 |
|
| 219 |
/* PCI collapsing */ |
| 220 |
int pci_collapsed; /* 0 if no collapsing, -1 if collapsed with a previous one, >1 if collapsed with several next */ |
| 221 |
int factorized; /* 0 if no factorizing, -1 if hidden, 1 if replaced with dots */ |
| 222 |
|
| 223 |
/* custom style */ |
| 224 |
struct lstopo_style style; |
| 225 |
#define LSTOPO_STYLE_BG 0x1 |
| 226 |
#define LSTOPO_STYLE_T 0x2 |
| 227 |
#define LSTOPO_STYLE_T2 0x4 |
| 228 |
unsigned style_set; /* OR'ed LSTOPO_STYLE_* */ |
| 229 |
|
| 230 |
/* PU style for CPU kind, 0 for normal style */ |
| 231 |
unsigned cpukind_style; |
| 232 |
|
| 233 |
/* object size (including children if they are outside of it, not including borders) */ |
| 234 |
unsigned width; |
| 235 |
unsigned height; |
| 236 |
|
| 237 |
/* a child position is: its parent position + parent->children_*rel + child->*rel */ |
| 238 |
/* relative position of first child with respect to top-left corner of this object */ |
| 239 |
struct lstopo_children_position { |
| 240 |
unsigned kinds; |
| 241 |
unsigned width; |
| 242 |
unsigned height; |
| 243 |
unsigned xrel; |
| 244 |
unsigned yrel; |
| 245 |
unsigned box; |
| 246 |
struct lstopo_color *boxcolor; |
| 247 |
} children; |
| 248 |
/* if memory is displayed separately above normal children */ |
| 249 |
struct lstopo_children_position above_children; |
| 250 |
/* if I/O and/or Misc are displayed separately on the right */ |
| 251 |
struct lstopo_children_position right_children; |
| 252 |
/* if I/O and/or Misc are displayed separately below */ |
| 253 |
struct lstopo_children_position below_children; |
| 254 |
|
| 255 |
/* relative position of this object within its parent children zone */ |
| 256 |
unsigned xrel; |
| 257 |
unsigned yrel; |
| 258 |
|
| 259 |
/* internal offset for factorized_draw */ |
| 260 |
unsigned internal_xoffset; |
| 261 |
|
| 262 |
/* text lines within object */ |
| 263 |
#define LSTOPO_OBJ_TEXT_MAX 4 /* current max number of lines is osdev name + 3 cuda attributes */ |
| 264 |
struct lstopo_text_line { |
| 265 |
char text[128]; |
| 266 |
unsigned width; |
| 267 |
unsigned xoffset; |
| 268 |
} text[LSTOPO_OBJ_TEXT_MAX]; |
| 269 |
unsigned ntext; |
| 270 |
unsigned textwidth; /* required width for all lines of text, |
| 271 |
* max of above text[].width + optional padding |
| 272 |
*/ |
| 273 |
}; |
| 274 |
|
| 275 |
static __hwloc_inline unsigned |
| 276 |
lstopo_obj_cpukind_style(struct lstopo_output *loutput, hwloc_obj_t obj) |
| 277 |
{ |
| 278 |
if (loutput->show_cpukinds) { |
| 279 |
if (obj && obj->userdata) { |
| 280 |
struct lstopo_obj_userdata *ou = obj->userdata; |
| 281 |
return ou->cpukind_style; |
| 282 |
} |
| 283 |
} |
| 284 |
return 0; |
| 285 |
} |
| 286 |
|
| 287 |
#define CPUKIND_STYLE(_loutput, _obj) (((_loutput)->show_cpukinds && (_obj != NULL)) ? (_obj)->cpukind_style : 0) |
| 288 |
|
| 289 |
typedef int output_method (struct lstopo_output *output, const char *filename); |
| 290 |
extern output_method output_console, output_synthetic, output_ascii, output_tikz, output_fig, output_png, output_pdf, output_ps, output_nativesvg, output_cairosvg, output_x11, output_windows, output_xml, output_android, output_shmem; |
| 291 |
|
| 292 |
extern int lstopo_shmem_adopt(const char *input, hwloc_topology_t *topologyp); |
| 293 |
|
| 294 |
struct draw_methods { |
| 295 |
int (*declare_color) (struct lstopo_output *loutput, struct lstopo_color *lcolor); |
| 296 |
void (*destroy_color) (struct lstopo_output *loutput, struct lstopo_color *lcolor); |
| 297 |
/* only called when loutput->draw_methods == LSTOPO_DRAWING_DRAW */ |
| 298 |
void (*box) (struct lstopo_output *loutput, const struct lstopo_color *lcolor, unsigned depth, unsigned x, unsigned width, unsigned y, unsigned height, hwloc_obj_t obj, unsigned box_id); |
| 299 |
void (*line) (struct lstopo_output *loutput, unsigned depth, unsigned x1, unsigned y1, unsigned x2, unsigned y2, hwloc_obj_t obj, unsigned line_id); |
| 300 |
void (*text) (struct lstopo_output *loutput, const struct lstopo_color *lcolor, int size, unsigned depth, unsigned x, unsigned y, const char *text, hwloc_obj_t obj, unsigned text_id); |
| 301 |
/* may be called when loutput->drawing == LSTOPO_DRAWING_PREPARE */ |
| 302 |
void (*textsize) (struct lstopo_output *loutput, const char *text, unsigned textlength, unsigned fontsize, unsigned *width); |
| 303 |
}; |
| 304 |
|
| 305 |
extern void output_draw(struct lstopo_output *output); |
| 306 |
|
| 307 |
extern void lstopo_prepare_custom_styles(struct lstopo_output *loutput); |
| 308 |
extern void declare_colors(struct lstopo_output *output); |
| 309 |
extern void destroy_colors(struct lstopo_output *output); |
| 310 |
|
| 311 |
static __hwloc_inline int lstopo_pu_disallowed(struct lstopo_output *loutput, hwloc_obj_t l) |
| 312 |
{ |
| 313 |
hwloc_topology_t topology = loutput->topology; |
| 314 |
return !hwloc_bitmap_isset(hwloc_topology_get_allowed_cpuset(topology), l->os_index); |
| 315 |
} |
| 316 |
|
| 317 |
static __hwloc_inline int lstopo_pu_binding(struct lstopo_output *loutput, hwloc_obj_t l) |
| 318 |
{ |
| 319 |
return loutput->pid_number != -1 && hwloc_bitmap_isset(loutput->cpubind_set, l->os_index); |
| 320 |
} |
| 321 |
|
| 322 |
static __hwloc_inline int lstopo_numa_disallowed(struct lstopo_output *loutput, hwloc_obj_t l) |
| 323 |
{ |
| 324 |
hwloc_topology_t topology = loutput->topology; |
| 325 |
return !hwloc_bitmap_isset(hwloc_topology_get_allowed_nodeset(topology), l->os_index); |
| 326 |
} |
| 327 |
|
| 328 |
static __hwloc_inline int lstopo_numa_binding(struct lstopo_output *loutput, hwloc_obj_t l) |
| 329 |
{ |
| 330 |
return loutput->pid_number != -1 && hwloc_bitmap_isset(loutput->membind_set, l->os_index); |
| 331 |
} |
| 332 |
|
| 333 |
static __hwloc_inline int lstopo_busid_snprintf(struct lstopo_output *loutput, char *text, size_t textlen, hwloc_obj_t firstobj, int collapse, unsigned needdomain) |
| 334 |
{ |
| 335 |
hwloc_obj_t lastobj; |
| 336 |
char domain[10] = ""; |
| 337 |
unsigned i; |
| 338 |
|
| 339 |
if (needdomain) |
| 340 |
snprintf(domain, sizeof(domain), "%04x:", firstobj->attr->pcidev.domain); |
| 341 |
|
| 342 |
/* single busid */ |
| 343 |
if (!loutput->pci_collapse_enabled || collapse <= 1) { |
| 344 |
return snprintf(text, textlen, "%s%02x:%02x.%01x", |
| 345 |
domain, |
| 346 |
firstobj->attr->pcidev.bus, |
| 347 |
firstobj->attr->pcidev.dev, |
| 348 |
firstobj->attr->pcidev.func); |
| 349 |
} |
| 350 |
|
| 351 |
assert(collapse >= 0); /* should be called on the first object of a collapsed range */ |
| 352 |
|
| 353 |
for(lastobj=firstobj, i=1; i<(unsigned)collapse; i++) |
| 354 |
lastobj = lastobj->next_cousin; |
| 355 |
|
| 356 |
/* multiple busid functions for same busid device */ |
| 357 |
if (firstobj->attr->pcidev.dev == lastobj->attr->pcidev.dev) |
| 358 |
return snprintf(text, textlen, "%s%02x:%02x.%01x-%01x", |
| 359 |
domain, |
| 360 |
firstobj->attr->pcidev.bus, |
| 361 |
firstobj->attr->pcidev.dev, |
| 362 |
firstobj->attr->pcidev.func, |
| 363 |
lastobj->attr->pcidev.func); |
| 364 |
|
| 365 |
/* multiple busid devices */ |
| 366 |
return snprintf(text, textlen, "%s%02x:%02x.%01x-%02x.%01x", |
| 367 |
domain, |
| 368 |
firstobj->attr->pcidev.bus, |
| 369 |
firstobj->attr->pcidev.dev, |
| 370 |
firstobj->attr->pcidev.func, |
| 371 |
lastobj->attr->pcidev.dev, |
| 372 |
lastobj->attr->pcidev.func); |
| 373 |
} |
| 374 |
|
| 375 |
extern void lstopo_show_interactive_cli_options(const struct lstopo_output *loutput); |
| 376 |
extern void lstopo_show_interactive_help(void); |
| 377 |
|
| 378 |
#endif /* UTILS_LSTOPO_H */ |