static void output_compute_pu_min_textwidth(struct lstopo_output *output) { unsigned fontsize = output->fontsize; char text[64]; int n; hwloc_topology_t topology = output->topology; hwloc_obj_t lastpu; if (!output->methods->textsize) { output->min_pu_textwidth = 0; return; } if (output->logical) { int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_PU); lastpu = hwloc_get_obj_by_depth(topology, depth, hwloc_get_nbobjs_by_depth(topology, depth)-1); } else { unsigned lastidx = hwloc_bitmap_last(hwloc_topology_get_topology_cpuset(topology)); lastpu = hwloc_get_pu_obj_by_os_index(topology, lastidx); } n = lstopo_obj_snprintf(output, text, sizeof(text), lastpu); output->min_pu_textwidth = get_textwidth(output, text, n, fontsize); }
bool Hwloc::isCpuAvailable( unsigned int cpu ) const { #ifndef HWLOC return true; #else return hwloc_get_pu_obj_by_os_index( _hwlocTopology, cpu ) != NULL; #endif }
unsigned int Hwloc::getNumaNodeOfCpu ( unsigned int cpu ) { int numaNodeId = 0; #ifdef HWLOC hwloc_obj_t pu = hwloc_get_pu_obj_by_os_index( _hwlocTopology, cpu ); // Now we have the PU object, go find its parent numa node hwloc_obj_t numaNode = hwloc_get_ancestor_obj_by_type( _hwlocTopology, HWLOC_OBJ_NODE, pu ); // If the machine is not NUMA if ( numaNode != NULL ) { numaNodeId = numaNode->os_index; } return numaNodeId; #else return numaNodeId; #endif }
int main(int argc, const char * const argv[]) { hwloc_topology_t topo; hwloc_obj_t pu; const char *basedir; const char *callname; char *path; size_t pathlen; unsigned idx = (unsigned) -1; int err; int ret = EXIT_SUCCESS; callname = argv[0]; argc--; argv++; hwloc_utils_check_api_version(callname); if (!hwloc_have_x86_cpuid()) { fprintf(stderr, "CPUID not supported.\n"); ret = EXIT_FAILURE; goto out; } while (argc > 0 && argv[0][0] == '-' && argv[0][1] != '\0') { if (argc >= 2 && !strcmp(argv[0], "-c")) { idx = atoi(argv[1]); argc -= 2; argv += 2; } else if (argc >= 1 && (!strcmp(argv[0], "-s") || !strcmp(argv[0], "--silent"))) { verbose--; argc--; argv++; } else if (!strcmp(argv[0], "-h") || !strcmp(argv[0], "--help")) { usage(callname, stdout); goto out; } else { usage(callname, stderr); ret = EXIT_FAILURE; goto out; } } basedir = "./cpuid"; if (argc >= 1) basedir = argv[0]; if (!getenv("HWLOC_COMPONENTS")) putenv((char *) "HWLOC_COMPONENTS=no_os,stop"); hwloc_topology_init(&topo); hwloc_topology_set_all_types_filter(topo, HWLOC_TYPE_FILTER_KEEP_NONE); err = hwloc_topology_load(topo); if (err < 0) { fprintf(stderr, "Failed to load topology\n"); ret = EXIT_FAILURE; goto out; } if (!hwloc_topology_is_thissystem(topo)) { fprintf(stderr, "%s must run on the current system topology, while this topology doesn't come from this system.\n", callname); ret = EXIT_FAILURE; goto out; } if (!strcmp(basedir, "-")) { if (verbose) printf("Gathering on stdout ...\n"); if (idx == (unsigned) -1) { fprintf(stderr, "Cannot gather multiple PUs on stdout.\n"); ret = EXIT_FAILURE; goto out; } path = NULL; pathlen = 0; } else { err = mkdir(basedir, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); if (err < 0) { if (access(basedir, X_OK|W_OK) < 0) { fprintf(stderr, "Could not create/open destination directory %s\n", basedir); ret = EXIT_FAILURE; goto out_with_topo; } } if (verbose) printf("Gathering in directory %s ...\n", basedir); pathlen = strlen(basedir) + 20; /* for '/pu%u' or '/hwloc-cpuid-info' */ path = malloc(pathlen); } if (idx == (unsigned) -1) { FILE *file; pu = NULL; while ((pu = hwloc_get_next_obj_by_type(topo, HWLOC_OBJ_PU, pu)) != NULL) { idx = pu->os_index; if (path) snprintf(path, pathlen, "%s/pu%u", basedir, idx); dump_one_proc(topo, pu, path); } snprintf(path, pathlen, "%s/hwloc-cpuid-info", basedir); file = fopen(path, "w"); if (file) { fprintf(file, "Architecture: x86\n"); fclose(file); if (verbose) printf("Summary written to %s\n", path); } else { fprintf(stderr, "Failed to open summary file '%s' for writing: %s\n", path, strerror(errno)); } } else { pu = hwloc_get_pu_obj_by_os_index(topo, idx); if (!pu) { fprintf(stderr, "Cannot find PU P#%u\n", idx); ret = EXIT_FAILURE; goto out_with_path; } else { if (path) snprintf(path, pathlen, "%s/pu%u", basedir, idx); dump_one_proc(topo, pu, path); } } if (verbose) printf("\n" "WARNING: Do not post these files on a public list or website unless you\n" "WARNING: are sure that no information about this platform is sensitive.\n"); out_with_path: free(path); out_with_topo: hwloc_topology_destroy(topo); out: return ret; }
int main(void) { hwloc_topology_t topology; hwloc_bitmap_t set, set2; hwloc_const_bitmap_t cset_available, cset_all; hwloc_obj_t obj; char *buffer; char type[64]; unsigned i; int err; /* create a topology */ err = hwloc_topology_init(&topology); if (err < 0) { fprintf(stderr, "failed to initialize the topology\n"); return EXIT_FAILURE; } err = hwloc_topology_load(topology); if (err < 0) { fprintf(stderr, "failed to load the topology\n"); hwloc_topology_destroy(topology); return EXIT_FAILURE; } /* retrieve the entire set of available PUs */ cset_available = hwloc_topology_get_topology_cpuset(topology); /* retrieve the CPU binding of the current entire process */ set = hwloc_bitmap_alloc(); if (!set) { fprintf(stderr, "failed to allocate a bitmap\n"); hwloc_topology_destroy(topology); return EXIT_FAILURE; } err = hwloc_get_cpubind(topology, set, HWLOC_CPUBIND_PROCESS); if (err < 0) { fprintf(stderr, "failed to get cpu binding\n"); hwloc_bitmap_free(set); hwloc_topology_destroy(topology); } /* display the processing units that cannot be used by this process */ if (hwloc_bitmap_isequal(set, cset_available)) { printf("this process can use all available processing units in the system\n"); } else { /* compute the set where we currently cannot run. * we can't modify cset_available because it's a system read-only one, * so we do set = available &~ set */ hwloc_bitmap_andnot(set, cset_available, set); hwloc_bitmap_asprintf(&buffer, set); printf("process cannot use %d process units (%s) among %u in the system\n", hwloc_bitmap_weight(set), buffer, hwloc_bitmap_weight(cset_available)); free(buffer); /* restore set where it was before the &~ operation above */ hwloc_bitmap_andnot(set, cset_available, set); } /* print the smallest object covering the current process binding */ obj = hwloc_get_obj_covering_cpuset(topology, set); hwloc_obj_type_snprintf(type, sizeof(type), obj, 0); printf("process is bound within object %s logical index %u\n", type, obj->logical_index); /* retrieve the single PU where the current thread actually runs within this process binding */ set2 = hwloc_bitmap_alloc(); if (!set2) { fprintf(stderr, "failed to allocate a bitmap\n"); hwloc_bitmap_free(set); hwloc_topology_destroy(topology); return EXIT_FAILURE; } err = hwloc_get_last_cpu_location(topology, set2, HWLOC_CPUBIND_THREAD); if (err < 0) { fprintf(stderr, "failed to get last cpu location\n"); hwloc_bitmap_free(set); hwloc_bitmap_free(set2); hwloc_topology_destroy(topology); } /* sanity checks that are not actually needed but help the reader */ /* this thread runs within the process binding */ assert(hwloc_bitmap_isincluded(set2, set)); /* this thread runs on a single PU at a time */ assert(hwloc_bitmap_weight(set2) == 1); /* print the logical number of the PU where that thread runs */ /* extract the PU OS index from the bitmap */ i = hwloc_bitmap_first(set2); obj = hwloc_get_pu_obj_by_os_index(topology, i); printf("thread is now running on PU logical index %u (OS/physical index %u)\n", obj->logical_index, i); /* migrate this single thread to where other PUs within the current binding */ hwloc_bitmap_andnot(set2, set, set2); err = hwloc_set_cpubind(topology, set2, HWLOC_CPUBIND_THREAD); if (err < 0) { fprintf(stderr, "failed to set thread binding\n"); hwloc_bitmap_free(set); hwloc_bitmap_free(set2); hwloc_topology_destroy(topology); } /* reprint the PU where that thread runs */ err = hwloc_get_last_cpu_location(topology, set2, HWLOC_CPUBIND_THREAD); if (err < 0) { fprintf(stderr, "failed to get last cpu location\n"); hwloc_bitmap_free(set); hwloc_bitmap_free(set2); hwloc_topology_destroy(topology); } /* print the logical number of the PU where that thread runs */ /* extract the PU OS index from the bitmap */ i = hwloc_bitmap_first(set2); obj = hwloc_get_pu_obj_by_os_index(topology, i); printf("thread is running on PU logical index %u (OS/physical index %u)\n", obj->logical_index, i); hwloc_bitmap_free(set); hwloc_bitmap_free(set2); /* retrieve the entire set of all PUs */ cset_all = hwloc_topology_get_complete_cpuset(topology); if (hwloc_bitmap_isequal(cset_all, cset_available)) { printf("all hardware PUs are available\n"); } else { printf("only %d hardware PUs are available in the machine among %d\n", hwloc_bitmap_weight(cset_available), hwloc_bitmap_weight(cset_all)); } hwloc_topology_destroy(topology); return EXIT_SUCCESS; }