void output_xml(hwloc_topology_t topology, const char *filename, int logical __hwloc_attribute_unused, int legend __hwloc_attribute_unused, int verbose_mode __hwloc_attribute_unused) { if (!filename || !strcasecmp(filename, "-.xml")) filename = "-"; if (hwloc_topology_export_xml(topology, filename) < 0) { fprintf(stderr, "Failed to export XML to %s (%s)\n", filename, strerror(errno)); return; } }
void output_xml(struct lstopo_output *loutput, const char *filename) { struct stat st; if (!filename || !strcasecmp(filename, "-.xml")) filename = "-"; /* hwloc_topology_export_xml() writes to stdout if "-" is given */ if (strcmp(filename, "-") && !stat(filename, &st) && !loutput->overwrite) { fprintf(stderr, "Failed to export XML to %s (%s)\n", filename, strerror(EEXIST)); return; } if (hwloc_topology_export_xml(loutput->topology, filename) < 0) { fprintf(stderr, "Failed to export XML to %s (%s)\n", filename, strerror(errno)); return; } }
static int test(hwloc_topology_t orig, const char *callname) { unsigned long forced_addr; unsigned long fileoffset; size_t shmem_length; int synthetic_with_distances = (hwloc_obj_get_info_by_name(hwloc_get_root_obj(orig), "SyntheticDescription") != NULL); char tmpname[] = "/tmp/hwloc_test_shmem.XXXXXX"; char cmd[512]; struct stat st; int fd, err; int ret = EXIT_SKIP; printf("opening temporary file\n"); fd = mkstemp(tmpname); if (fd < 0) { perror("mkstemp"); goto out; } printf("opened %s\n", tmpname); printf("exporting XML\n"); err = hwloc_topology_export_xml(orig, tmpname, 0); assert(!err); err = stat(tmpname, &st); assert(!err); printf("exported %lu bytes\n", (unsigned long) st.st_size); fileoffset = st.st_size+1; /* skip a couple bytes to make sure the XML is don" */ fileoffset = (fileoffset + hwloc_getpagesize() - 1) &~(hwloc_getpagesize() - 1); printf("will mmap at file offset %lu\n", fileoffset); err = hwloc_shmem_topology_get_length(orig, &shmem_length, 0); assert(!err); printf("need mmap length %lu\n", (unsigned long) shmem_length); #if SIZEOF_VOID_P == 8 forced_addr = 0x300000000000UL; #else forced_addr = 0xb0000000UL; #endif printf("write to shmem at address %lx in file %s offset %lu\n", forced_addr, tmpname, fileoffset); err = hwloc_shmem_topology_write(orig, fd, fileoffset, (void*)(uintptr_t)forced_addr, shmem_length, 0); if (err == -1 && errno == EBUSY) { fprintf(stderr, "Failed to shmem write, requested mapping is busy\n"); goto out_with_fd; } assert(!err); printf("wrote length %lu\n", (unsigned long) shmem_length); printf("adopting locally\n"); ret = adopt(fd, fileoffset, forced_addr, shmem_length, synthetic_with_distances); assert(ret == EXIT_SUCCESS || ret == EXIT_SKIP); printf("adopting in other child process\n"); snprintf(cmd, sizeof(cmd), "%s %s %lu %lu %lu %d", callname, tmpname, fileoffset, forced_addr, (unsigned long) shmem_length, synthetic_with_distances); printf("running command %s\n", cmd); err = system(cmd); assert(WIFEXITED(err)); printf("child process returned %d\n", WEXITSTATUS(err)); assert(WEXITSTATUS(err) == EXIT_SUCCESS || WEXITSTATUS(err) == EXIT_SKIP); /* we caught errors above. * return SKIP if both returned SKIP. otherwise SUCCESS */ if (WEXITSTATUS(err) == EXIT_SKIP && ret == EXIT_SKIP) ret = EXIT_SKIP; else ret = EXIT_SUCCESS; out_with_fd: close(fd); unlink(tmpname); out: return ret; }
int main(void) { hwloc_topology_t topology1, topology2; char *xmlbuf; int xmlbuflen; char xmlfile[] = "hwloc_backends.tmpxml.XXXXXX"; int xmlbufok = 0, xmlfileok = 0, xmlfilefd; hwloc_obj_t sw; int err; printf("trying to export topology to XML buffer and file for later...\n"); hwloc_topology_init(&topology1); hwloc_topology_load(topology1); assert(hwloc_topology_is_thissystem(topology1)); if (hwloc_topology_export_xmlbuffer(topology1, &xmlbuf, &xmlbuflen) < 0) printf("XML buffer export failed (%s), ignoring\n", strerror(errno)); else xmlbufok = 1; xmlfilefd = mkstemp(xmlfile); if (xmlfilefd < 0 || hwloc_topology_export_xml(topology1, xmlfile) < 0) printf("XML file export failed (%s), ignoring\n", strerror(errno)); else xmlfileok = 1; printf("init...\n"); hwloc_topology_init(&topology2); if (xmlfileok) { printf("switching to xml...\n"); assert(!hwloc_topology_set_xml(topology2, xmlfile)); } if (xmlbufok) { printf("switching to xmlbuffer...\n"); assert(!hwloc_topology_set_xmlbuffer(topology2, xmlbuf, xmlbuflen)); } printf("switching to custom...\n"); hwloc_topology_set_custom(topology2); printf("switching to synthetic...\n"); hwloc_topology_set_synthetic(topology2, "machine:2 node:3 cache:2 pu:4"); printf("switching sysfs fsroot to // ...\n"); hwloc_topology_set_fsroot(topology2, "//"); /* valid path that won't be recognized as '/' */ printf("switching sysfs fsroot to / ...\n"); hwloc_topology_set_fsroot(topology2, "/"); hwloc_topology_destroy(topology2); if (xmlfileok) { printf("switching to xml and loading...\n"); hwloc_topology_init(&topology2); assert(!hwloc_topology_set_xml(topology2, xmlfile)); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); hwloc_topology_destroy(topology2); } if (xmlbufok) { printf("switching to xmlbuffer and loading...\n"); hwloc_topology_init(&topology2); assert(!hwloc_topology_set_xmlbuffer(topology2, xmlbuf, xmlbuflen)); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); hwloc_topology_destroy(topology2); } printf("switching to custom and loading...\n"); hwloc_topology_init(&topology2); hwloc_topology_set_custom(topology2); sw = hwloc_custom_insert_group_object_by_parent(topology2, hwloc_get_root_obj(topology2), 0); assert(sw); hwloc_custom_insert_topology(topology2, sw, topology1, NULL); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); hwloc_topology_destroy(topology2); printf("switching to synthetic and loading...\n"); hwloc_topology_init(&topology2); hwloc_topology_set_synthetic(topology2, "machine:2 node:3 cache:2 pu:4"); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); hwloc_topology_destroy(topology2); printf("switching sysfs fsroot to // and loading...\n"); hwloc_topology_init(&topology2); err = hwloc_topology_set_fsroot(topology2, "//"); /* '//' isn't recognized as the normal fsroot on Linux, and it fails and falls back to normal topology on !Linux */ hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2) == !err); hwloc_topology_destroy(topology2); printf("switching sysfs fsroot to / and loading...\n"); hwloc_topology_init(&topology2); err = hwloc_topology_set_fsroot(topology2, "/"); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(hwloc_topology_is_thissystem(topology2)); /* '/' is recognized as the normal fsroot on Linux, and it fails and falls back to normal topology on !Linux */ hwloc_topology_destroy(topology2); printf("switching to synthetic...\n"); hwloc_topology_init(&topology2); hwloc_topology_set_synthetic(topology2, "machine:2 node:3 cache:2 pu:4"); hwloc_topology_destroy(topology2); if (xmlbufok) hwloc_free_xmlbuffer(topology1, xmlbuf); if (xmlfilefd >= 0) { unlink(xmlfile); close(xmlfilefd); } hwloc_topology_destroy(topology1); return 0; }
int main(int argc, char *argv[]) { hwloc_obj_type_t type; unsigned old_index, new_index; const char *callname = argv[0]; hwloc_topology_t topology; int err; if (argc < 6) { usage(stderr, callname); exit(EXIT_FAILURE); } #ifdef HWLOC2 err = hwloc_type_sscanf(argv[3], &type, NULL, 0); #else err = hwloc_obj_type_sscanf(argv[3], &type, NULL, NULL, 0); #endif if (err < 0) { fprintf(stderr, "Failed to recognize type `%s'\n", argv[3]); usage(stderr, callname); exit(EXIT_FAILURE); } if (type != HWLOC_OBJ_PU && type != HWLOC_OBJ_NUMANODE) { fprintf(stderr, "Invalid type `%s', should be PU or NUMA node\n", argv[3]); usage(stderr, callname); exit(EXIT_FAILURE); } old_index = atoi(argv[4]); new_index = atoi(argv[5]); if (old_index == new_index) { fprintf(stderr, "Nothing to do\n"); exit(EXIT_SUCCESS); } err = hwloc_topology_init(&topology); if (err < 0) { fprintf(stderr, "hwloc_topology_init() failed (%s)\n", strerror(errno)); usage(stderr, callname); exit(EXIT_FAILURE); } err = hwloc_topology_set_xml(topology, argv[1]); if (err < 0) { fprintf(stderr, "hwloc_topology_set_xml() on file `%s' failed (%s)\n", argv[1], strerror(errno)); usage(stderr, callname); exit(EXIT_FAILURE); } #ifdef HWLOC2 err = hwloc_topology_set_flags(topology, HWLOC_TOPOLOGY_FLAG_WHOLE_SYSTEM); err = hwloc_topology_set_all_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_ALL); #else err = hwloc_topology_set_flags(topology, HWLOC_TOPOLOGY_FLAG_WHOLE_SYSTEM | HWLOC_TOPOLOGY_FLAG_WHOLE_IO | HWLOC_TOPOLOGY_FLAG_ICACHES); #endif err = hwloc_topology_load(topology); if (err < 0) { fprintf(stderr, "hwloc_topology_load() failed (%s)\n", strerror(errno)); usage(stderr, callname); exit(EXIT_FAILURE); } if (HWLOC_OBJ_PU == type) { hwloc_const_bitmap_t cpset = hwloc_topology_get_complete_cpuset(topology); if (!hwloc_bitmap_isset(cpset, old_index)) { fprintf(stderr, "Old PU os_index %u doesn't exist\n", old_index); usage(stderr, callname); exit(EXIT_FAILURE); } if (hwloc_bitmap_isset(cpset, new_index)) { fprintf(stderr, "New PU os_index %u already exists\n", new_index); usage(stderr, callname); exit(EXIT_FAILURE); } switch_pu_index(hwloc_get_root_obj(topology), old_index, new_index); } else if (HWLOC_OBJ_NUMANODE == type) { hwloc_const_bitmap_t cnset = hwloc_topology_get_complete_nodeset(topology); if (!cnset || hwloc_bitmap_isfull(cnset)) { fprintf(stderr, "Topology doesn't have NUMA nodes\n"); usage(stderr, callname); exit(EXIT_FAILURE); } if (!hwloc_bitmap_isset(cnset, old_index)) { fprintf(stderr, "Old NUMA node os_index %u doesn't exist\n", old_index); usage(stderr, callname); exit(EXIT_FAILURE); } if (hwloc_bitmap_isset(cnset, new_index)) { fprintf(stderr, "New NUMA node os_index %u already exists\n", new_index); usage(stderr, callname); exit(EXIT_FAILURE); } switch_numa_index(hwloc_get_root_obj(topology), old_index, new_index); } err = hwloc_topology_export_xml(topology, argv[2], 0); if (err < 0) { fprintf(stderr, "hwloc_topology_export_xml() on file `%s' failed (%s)\n", argv[2], strerror(errno)); usage(stderr, callname); exit(EXIT_FAILURE); } hwloc_topology_destroy(topology); printf("Beware that hwloc may warn about out-of-order objects when reloading %s\n", argv[2]); return 0; }
int main(void) { hwloc_topology_t topology1, topology2; char *xmlbuf; int xmlbuflen; char xmlfile[] = "hwloc_backends.tmpxml.XXXXXX"; int xmlbufok = 0, xmlfileok = 0; hwloc_obj_t sw; int err; printf("trying to export topology to XML buffer and file for later...\n"); hwloc_topology_init(&topology1); hwloc_topology_load(topology1); assert(hwloc_topology_is_thissystem(topology1)); if (hwloc_topology_export_xmlbuffer(topology1, &xmlbuf, &xmlbuflen) < 0) printf("XML buffer export failed (%s), ignoring\n", strerror(errno)); else xmlbufok = 1; mktemp(xmlfile); if (hwloc_topology_export_xml(topology1, xmlfile) < 0) printf("XML file export failed (%s), ignoring\n", strerror(errno)); else xmlfileok = 1; printf("init...\n"); hwloc_topology_init(&topology2); if (xmlfileok) { printf("switching to xml...\n"); assert(!hwloc_topology_set_xml(topology2, xmlfile)); } if (xmlbufok) { printf("switching to xmlbuffer...\n"); assert(!hwloc_topology_set_xmlbuffer(topology2, xmlbuf, xmlbuflen)); } printf("switching to custom...\n"); hwloc_topology_set_custom(topology2); printf("switching to synthetic...\n"); hwloc_topology_set_synthetic(topology2, "machine:2 node:3 cache:2 pu:4"); printf("switching sysfs fsroot to // ...\n"); hwloc_topology_set_fsroot(topology2, "//"); /* valid path that won't be recognized as '/' */ printf("switching sysfs fsroot to / ...\n"); hwloc_topology_set_fsroot(topology2, "/"); if (xmlfileok) { printf("switching to xml and loading...\n"); assert(!hwloc_topology_set_xml(topology2, xmlfile)); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); } if (xmlbufok) { printf("switching to xmlbuffer and loading...\n"); assert(!hwloc_topology_set_xmlbuffer(topology2, xmlbuf, xmlbuflen)); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); } printf("switching to custom and loading...\n"); hwloc_topology_set_custom(topology2); sw = hwloc_custom_insert_group_object_by_parent(topology2, hwloc_get_root_obj(topology2), 0); assert(sw); hwloc_custom_insert_topology(topology2, sw, topology1, NULL); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); /* don't try fsroot here because it fails on !linux, we would revert back to custom, which requires some insert to make the topology valid */ printf("switching to synthetic and loading...\n"); hwloc_topology_set_synthetic(topology2, "machine:2 node:3 cache:2 pu:4"); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); printf("switching sysfs fsroot to // and loading...\n"); hwloc_topology_set_fsroot(topology2, "//"); /* valid path that won't be recognized as '/' */ hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); /* earlier fsroot worked, or we're still synthetic */ printf("switching sysfs fsroot to / and loading...\n"); err = hwloc_topology_set_fsroot(topology2, "/"); hwloc_topology_load(topology2); hwloc_topology_check(topology2); assert(hwloc_topology_is_thissystem(topology2) == !err); /* on Linux, '/' is recognized as thissystem. on !Linux, set_fsroot() failed and we went back to synthetic */ printf("switching to synthetic...\n"); hwloc_topology_set_synthetic(topology2, "machine:2 node:3 cache:2 pu:4"); hwloc_topology_destroy(topology2); if (xmlbufok) hwloc_free_xmlbuffer(topology1, xmlbuf); if (xmlfileok) unlink(xmlfile); hwloc_topology_destroy(topology1); return 0; }
int main(void) { hwloc_topology_t topology1, topology2; char *xmlbuf; int xmlbuflen; char xmlfile[] = "hwloc_backends.tmpxml.XXXXXX"; char env[64]; int xmlbufok = 0, xmlfileok = 0, xmlfilefd; const char *orig_backend_name; putenv("HWLOC_LIBXML_CLEANUP=1"); printf("trying to export topology to XML buffer and file for later...\n"); hwloc_topology_init(&topology1); hwloc_topology_load(topology1); orig_backend_name = get_backend_name(topology1); hwloc_obj_add_info(hwloc_get_root_obj(topology1), "Foo", "Bar"); assert(hwloc_topology_is_thissystem(topology1)); if (hwloc_topology_export_xmlbuffer(topology1, &xmlbuf, &xmlbuflen) < 0) printf("XML buffer export failed (%s), ignoring\n", strerror(errno)); else xmlbufok = 1; xmlfilefd = mkstemp(xmlfile); if (xmlfilefd < 0 || hwloc_topology_export_xml(topology1, xmlfile) < 0) printf("XML file export failed (%s), ignoring\n", strerror(errno)); else xmlfileok = 1; /* init+config+destroy without loading */ printf("init...\n"); hwloc_topology_init(&topology2); if (xmlfileok) { printf("switching to xml...\n"); assert(!hwloc_topology_set_xml(topology2, xmlfile)); } if (xmlbufok) { printf("switching to xmlbuffer...\n"); assert(!hwloc_topology_set_xmlbuffer(topology2, xmlbuf, xmlbuflen)); } printf("switching to synthetic...\n"); hwloc_topology_set_synthetic(topology2, "machine:2 node:3 l1:2 pu:4"); hwloc_topology_destroy(topology2); /* init+xml+load+destroy */ if (xmlfileok) { printf("switching to xml and loading...\n"); hwloc_topology_init(&topology2); assert(!hwloc_topology_set_xml(topology2, xmlfile)); hwloc_topology_load(topology2); assert_backend_name(topology2, orig_backend_name); assert_foo_bar(topology2, 1); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); hwloc_topology_destroy(topology2); } /* init+xmlbuf+load+destroy */ if (xmlbufok) { printf("switching to xmlbuffer and loading...\n"); hwloc_topology_init(&topology2); assert(!hwloc_topology_set_xmlbuffer(topology2, xmlbuf, xmlbuflen)); hwloc_topology_load(topology2); assert_backend_name(topology2, orig_backend_name); assert_foo_bar(topology2, 1); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); hwloc_topology_destroy(topology2); } /* init+synthetic+load+destroy */ printf("switching to synthetic and loading...\n"); hwloc_topology_init(&topology2); hwloc_topology_set_synthetic(topology2, "machine:2 node:3 l3i:2 pu:4"); hwloc_topology_load(topology2); assert_backend_name(topology2, "Synthetic"); assert_foo_bar(topology2, 0); assert(hwloc_get_nbobjs_by_type(topology2, HWLOC_OBJ_PU) == 2*3*2*4); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); hwloc_topology_destroy(topology2); /* xmlenv+init+load+destroy */ if (xmlfileok) { printf("switching to xml by env and loading...\n"); snprintf(env, sizeof(env), "HWLOC_XMLFILE=%s", xmlfile); putenv(env); hwloc_topology_init(&topology2); hwloc_topology_load(topology2); assert_backend_name(topology2, orig_backend_name); assert_foo_bar(topology2, 1); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); hwloc_topology_destroy(topology2); } /* syntheticenv+init+load+destroy, synthetic env overrides xml */ printf("switching to synthetic by env and loading...\n"); putenv("HWLOC_SYNTHETIC=node:3 pu:3"); hwloc_topology_init(&topology2); hwloc_topology_load(topology2); assert_backend_name(topology2, "Synthetic"); assert_foo_bar(topology2, 0); assert(hwloc_get_nbobjs_by_type(topology2, HWLOC_OBJ_PU) == 3*3); hwloc_topology_check(topology2); assert(!hwloc_topology_is_thissystem(topology2)); hwloc_topology_destroy(topology2); /* componentsenv+init+load+destroy for testing defaults, overrides synthetic/xml/fsroot envs */ printf("switching to default components by env and loading...\n"); putenv("HWLOC_COMPONENTS=,"); /* don't set to empty since it means 'unset' on windows */ hwloc_topology_init(&topology2); hwloc_topology_load(topology2); assert_backend_name(topology2, orig_backend_name); assert_foo_bar(topology2, 0); hwloc_topology_check(topology2); assert(hwloc_topology_is_thissystem(topology2)); hwloc_topology_destroy(topology2); if (xmlbufok) hwloc_free_xmlbuffer(topology1, xmlbuf); if (xmlfilefd >= 0) { unlink(xmlfile); close(xmlfilefd); } hwloc_topology_destroy(topology1); return 0; }