| 1 |
|
| 2 |
. |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include "hwloc.h" |
| 12 |
|
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <errno.h> |
| 16 |
|
| 17 |
#if HWLOC_API_VERSION >= 0x20000 |
| 18 |
#define HWLOC2 |
| 19 |
#endif |
| 20 |
|
| 21 |
static void usage(FILE *output, const char *callname) |
| 22 |
{ |
| 23 |
fprintf(output, "%s <input.xml> <output.xml> <type> <old index> <new index>\n", callname); |
| 24 |
fprintf(output, " Replaces object type <type> os_index <old index> with <new index>\n"); |
| 25 |
fprintf(output, " <type> should be PU or NUMANode\n"); |
| 26 |
} |
| 27 |
|
| 28 |
static void switch_set_index(hwloc_bitmap_t set, unsigned old_index, unsigned new_index) |
| 29 |
{ |
| 30 |
if (hwloc_bitmap_isset(set, old_index)) { |
| 31 |
hwloc_bitmap_clr(set, old_index); |
| 32 |
hwloc_bitmap_set(set, new_index); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
static void switch_pu_index(hwloc_obj_t obj, unsigned old_index, unsigned new_index) |
| 37 |
{ |
| 38 |
hwloc_obj_t child; |
| 39 |
|
| 40 |
if (obj->type == HWLOC_OBJ_PU) { |
| 41 |
assert(obj->os_index == old_index); |
| 42 |
obj->os_index = new_index; |
| 43 |
} |
| 44 |
|
| 45 |
switch_set_index(obj->cpuset, old_index, new_index); |
| 46 |
#ifndef HWLOC2 |
| 47 |
switch_set_index(obj->online_cpuset, old_index, new_index); |
| 48 |
#endif |
| 49 |
switch_set_index(obj->complete_cpuset, old_index, new_index); |
| 50 |
|
| 51 |
for(child = obj->first_child; child; child = child->next_sibling) |
| 52 |
if (child->complete_cpuset && hwloc_bitmap_isset(child->complete_cpuset, old_index)) |
| 53 |
switch_pu_index(child, old_index, new_index); |
| 54 |
for(child = obj->memory_first_child; child; child = child->next_sibling) |
| 55 |
if (child->complete_cpuset && hwloc_bitmap_isset(child->complete_cpuset, old_index)) |
| 56 |
switch_pu_index(child, old_index, new_index); |
| 57 |
} |
| 58 |
|
| 59 |
static void switch_numa_index(hwloc_obj_t obj, unsigned old_index, unsigned new_index) |
| 60 |
{ |
| 61 |
hwloc_obj_t child; |
| 62 |
|
| 63 |
if (obj->type == HWLOC_OBJ_NUMANODE) { |
| 64 |
assert(obj->os_index == old_index); |
| 65 |
obj->os_index = new_index; |
| 66 |
} |
| 67 |
|
| 68 |
switch_set_index(obj->nodeset, old_index, new_index); |
| 69 |
switch_set_index(obj->complete_nodeset, old_index, new_index); |
| 70 |
|
| 71 |
for(child = obj->first_child; child; child = child->next_sibling) |
| 72 |
if (child->complete_nodeset && hwloc_bitmap_isset(child->complete_nodeset, old_index)) |
| 73 |
switch_numa_index(child, old_index, new_index); |
| 74 |
for(child = obj->memory_first_child; child; child = child->next_sibling) |
| 75 |
if (child->complete_nodeset && hwloc_bitmap_isset(child->complete_nodeset, old_index)) |
| 76 |
switch_numa_index(child, old_index, new_index); |
| 77 |
} |
| 78 |
|
| 79 |
int main(int argc, char *argv[]) |
| 80 |
{ |
| 81 |
hwloc_obj_type_t type; |
| 82 |
unsigned old_index, new_index; |
| 83 |
const char *callname = argv[0]; |
| 84 |
hwloc_topology_t topology; |
| 85 |
int err; |
| 86 |
|
| 87 |
if (argc < 6) { |
| 88 |
usage(stderr, callname); |
| 89 |
exit(EXIT_FAILURE); |
| 90 |
} |
| 91 |
|
| 92 |
#ifdef HWLOC2 |
| 93 |
err = hwloc_type_sscanf(argv[3], &type, NULL, 0); |
| 94 |
#else |
| 95 |
err = hwloc_obj_type_sscanf(argv[3], &type, NULL, NULL, 0); |
| 96 |
#endif |
| 97 |
if (err < 0) { |
| 98 |
fprintf(stderr, "Failed to recognize type `%s'\n", argv[3]); |
| 99 |
usage(stderr, callname); |
| 100 |
exit(EXIT_FAILURE); |
| 101 |
} |
| 102 |
if (type != HWLOC_OBJ_PU && type != HWLOC_OBJ_NUMANODE) { |
| 103 |
fprintf(stderr, "Invalid type `%s', should be PU or NUMA node\n", argv[3]); |
| 104 |
usage(stderr, callname); |
| 105 |
exit(EXIT_FAILURE); |
| 106 |
} |
| 107 |
|
| 108 |
old_index = atoi(argv[4]); |
| 109 |
new_index = atoi(argv[5]); |
| 110 |
if (old_index == new_index) { |
| 111 |
fprintf(stderr, "Nothing to do\n"); |
| 112 |
exit(EXIT_SUCCESS); |
| 113 |
} |
| 114 |
|
| 115 |
err = hwloc_topology_init(&topology); |
| 116 |
if (err < 0) { |
| 117 |
fprintf(stderr, "hwloc_topology_init() failed (%s)\n", strerror(errno)); |
| 118 |
usage(stderr, callname); |
| 119 |
exit(EXIT_FAILURE); |
| 120 |
} |
| 121 |
|
| 122 |
err = hwloc_topology_set_xml(topology, argv[1]); |
| 123 |
if (err < 0) { |
| 124 |
fprintf(stderr, "hwloc_topology_set_xml() on file `%s' failed (%s)\n", argv[1], strerror(errno)); |
| 125 |
usage(stderr, callname); |
| 126 |
exit(EXIT_FAILURE); |
| 127 |
} |
| 128 |
|
| 129 |
#ifdef HWLOC2 |
| 130 |
err = hwloc_topology_set_flags(topology, |
| 131 |
HWLOC_TOPOLOGY_FLAG_INCLUDE_DISALLOWED); |
| 132 |
err = hwloc_topology_set_all_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_ALL); |
| 133 |
#else |
| 134 |
err = hwloc_topology_set_flags(topology, |
| 135 |
HWLOC_TOPOLOGY_FLAG_INCLUDE_DISALLOWED |
| 136 |
| HWLOC_TOPOLOGY_FLAG_WHOLE_IO |
| 137 |
| HWLOC_TOPOLOGY_FLAG_ICACHES); |
| 138 |
#endif |
| 139 |
|
| 140 |
err = hwloc_topology_load(topology); |
| 141 |
if (err < 0) { |
| 142 |
fprintf(stderr, "hwloc_topology_load() failed (%s)\n", strerror(errno)); |
| 143 |
usage(stderr, callname); |
| 144 |
exit(EXIT_FAILURE); |
| 145 |
} |
| 146 |
|
| 147 |
if (HWLOC_OBJ_PU == type) { |
| 148 |
hwloc_const_bitmap_t cpset = hwloc_topology_get_complete_cpuset(topology); |
| 149 |
if (!hwloc_bitmap_isset(cpset, old_index)) { |
| 150 |
fprintf(stderr, "Old PU os_index %u doesn't exist\n", old_index); |
| 151 |
usage(stderr, callname); |
| 152 |
exit(EXIT_FAILURE); |
| 153 |
} |
| 154 |
if (hwloc_bitmap_isset(cpset, new_index)) { |
| 155 |
fprintf(stderr, "New PU os_index %u already exists\n", new_index); |
| 156 |
usage(stderr, callname); |
| 157 |
exit(EXIT_FAILURE); |
| 158 |
} |
| 159 |
|
| 160 |
switch_set_index((hwloc_bitmap_t)hwloc_topology_get_allowed_cpuset(topology), old_index, new_index); |
| 161 |
switch_pu_index(hwloc_get_root_obj(topology), old_index, new_index); |
| 162 |
|
| 163 |
} else if (HWLOC_OBJ_NUMANODE == type) { |
| 164 |
hwloc_const_bitmap_t cnset = hwloc_topology_get_complete_nodeset(topology); |
| 165 |
if (!cnset || hwloc_bitmap_isfull(cnset)) { |
| 166 |
fprintf(stderr, "Topology doesn't have NUMA nodes\n"); |
| 167 |
usage(stderr, callname); |
| 168 |
exit(EXIT_FAILURE); |
| 169 |
} |
| 170 |
if (!hwloc_bitmap_isset(cnset, old_index)) { |
| 171 |
fprintf(stderr, "Old NUMA node os_index %u doesn't exist\n", old_index); |
| 172 |
usage(stderr, callname); |
| 173 |
exit(EXIT_FAILURE); |
| 174 |
} |
| 175 |
if (hwloc_bitmap_isset(cnset, new_index)) { |
| 176 |
fprintf(stderr, "New NUMA node os_index %u already exists\n", new_index); |
| 177 |
usage(stderr, callname); |
| 178 |
exit(EXIT_FAILURE); |
| 179 |
} |
| 180 |
|
| 181 |
switch_set_index((hwloc_bitmap_t)hwloc_topology_get_allowed_nodeset(topology), old_index, new_index); |
| 182 |
switch_numa_index(hwloc_get_root_obj(topology), old_index, new_index); |
| 183 |
} |
| 184 |
|
| 185 |
err = hwloc_topology_export_xml(topology, argv[2], 0); |
| 186 |
if (err < 0) { |
| 187 |
fprintf(stderr, "hwloc_topology_export_xml() on file `%s' failed (%s)\n", argv[2], strerror(errno)); |
| 188 |
usage(stderr, callname); |
| 189 |
exit(EXIT_FAILURE); |
| 190 |
} |
| 191 |
|
| 192 |
hwloc_topology_destroy(topology); |
| 193 |
|
| 194 |
printf("Beware that hwloc may warn about out-of-order objects when reloading %s\n", argv[2]); |
| 195 |
return 0; |
| 196 |
} |