/
hwloc-distrib.c
290 lines (260 loc) · 7.52 KB
/
hwloc-distrib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright © 2009 CNRS
* Copyright © 2009-2020 Inria. All rights reserved.
* Copyright © 2009-2010 Université Bordeaux
* Copyright © 2009-2011 Cisco Systems, Inc. All rights reserved.
* See COPYING in top-level directory.
*/
#include "private/autogen/config.h"
#include "hwloc.h"
#include "misc.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
void usage(const char *callname __hwloc_attribute_unused, FILE *where)
{
fprintf(where, "Usage: hwloc-distrib [options] number\n");
fprintf(where, "Distribution options:\n");
fprintf(where, " --ignore <type> Ignore objects of the given type\n");
fprintf(where, " --from <type> Distribute starting from objects of the given type\n");
fprintf(where, " --to <type> Distribute down to objects of the given type\n");
fprintf(where, " --at <type> Distribute among objects of the given type\n");
fprintf(where, " --reverse Distribute by starting from last objects\n");
fprintf(where, "Input topology options:\n");
fprintf(where, " --restrict [nodeset=]<bitmap>\n");
fprintf(where, " Restrict the topology to some processors or NUMA nodes.\n");
fprintf(where, " --disallowed Include objects disallowed by administrative limitations\n");
hwloc_utils_input_format_usage(where, 0);
fprintf(where, "Formatting options:\n");
fprintf(where, " --single Singlify each output to a single CPU\n");
fprintf(where, " --taskset Show taskset-specific cpuset strings\n");
fprintf(where, "Miscellaneous options:\n");
fprintf(where, " -v --verbose Show verbose messages\n");
fprintf(where, " --version Report version and exit\n");
}
int main(int argc, char *argv[])
{
long n = -1;
char *callname;
char *input = NULL;
enum hwloc_utils_input_format input_format = HWLOC_UTILS_INPUT_DEFAULT;
int taskset = 0;
int singlify = 0;
int verbose = 0;
char *restrictstring = NULL;
const char *from_type = NULL, *to_type = NULL;
hwloc_topology_t topology;
unsigned long flags = 0;
unsigned long restrict_flags = 0;
unsigned long dflags = 0;
int opt;
int err;
callname = argv[0];
/* skip argv[0], handle options */
argv++;
argc--;
hwloc_utils_check_api_version(callname);
/* enable verbose backends */
if (!getenv("HWLOC_XML_VERBOSE"))
putenv((char *) "HWLOC_XML_VERBOSE=1");
if (!getenv("HWLOC_SYNTHETIC_VERBOSE"))
putenv((char *) "HWLOC_SYNTHETIC_VERBOSE=1");
hwloc_topology_init(&topology);
while (argc >= 1) {
if (!strcmp(argv[0], "--")) {
argc--;
argv++;
break;
}
if (*argv[0] == '-') {
if (!strcmp(argv[0], "--single")) {
singlify = 1;
goto next;
}
if (!strcmp(argv[0], "--taskset")) {
taskset = 1;
goto next;
}
if (!strcmp(argv[0], "-v") || !strcmp(argv[0], "--verbose")) {
verbose = 1;
goto next;
}
if (!strcmp (argv[0], "--disallowed") || !strcmp (argv[0], "--whole-system")) {
flags |= HWLOC_TOPOLOGY_FLAG_INCLUDE_DISALLOWED;
goto next;
}
if (!strcmp(argv[0], "-h") || !strcmp(argv[0], "--help")) {
usage(callname, stdout);
return EXIT_SUCCESS;
}
if (hwloc_utils_lookup_input_option(argv, argc, &opt,
&input, &input_format,
callname)) {
argv += opt;
argc -= opt;
goto next;
}
else if (!strcmp (argv[0], "--ignore")) {
hwloc_obj_type_t type;
if (argc < 2) {
usage(callname, stdout);
exit(EXIT_FAILURE);
}
if (hwloc_type_sscanf(argv[1], &type, NULL, 0) < 0)
fprintf(stderr, "Unsupported type `%s' passed to --ignore, ignoring.\n", argv[1]);
else
hwloc_topology_set_type_filter(topology, type, HWLOC_TYPE_FILTER_KEEP_NONE);
argc--;
argv++;
goto next;
}
else if (!strcmp (argv[0], "--from")) {
if (argc < 2) {
usage(callname, stdout);
exit(EXIT_FAILURE);
}
from_type = argv[1];
argc--;
argv++;
goto next;
}
else if (!strcmp (argv[0], "--to")) {
if (argc < 2) {
usage(callname, stdout);
exit(EXIT_FAILURE);
}
to_type = argv[1];
argc--;
argv++;
goto next;
}
else if (!strcmp (argv[0], "--at")) {
if (argc < 2) {
usage(callname, stdout);
exit(EXIT_FAILURE);
}
from_type = to_type = argv[1];
argc--;
argv++;
goto next;
}
else if (!strcmp (argv[0], "--reverse")) {
dflags |= HWLOC_DISTRIB_FLAG_REVERSE;
goto next;
}
else if (!strcmp (argv[0], "--restrict")) {
if (argc < 2) {
usage (callname, stdout);
exit(EXIT_FAILURE);
}
if(strncmp(argv[1], "nodeset=", 8)) {
restrictstring = strdup(argv[1]);
} else {
restrictstring = strdup(argv[1]+8);
restrict_flags |= HWLOC_RESTRICT_FLAG_BYNODESET;
}
argc--;
argv++;
goto next;
}
else if (!strcmp (argv[0], "--version")) {
printf("%s %s\n", callname, HWLOC_VERSION);
exit(EXIT_SUCCESS);
}
fprintf (stderr, "Unrecognized option: %s\n", argv[0]);
usage(callname, stderr);
return EXIT_FAILURE;
}
if (n != -1) {
fprintf(stderr,"duplicate number\n");
usage(callname, stderr);
return EXIT_FAILURE;
}
n = atol(argv[0]);
next:
argc--;
argv++;
}
if (n == -1) {
fprintf(stderr,"need a number\n");
usage(callname, stderr);
return EXIT_FAILURE;
}
if (verbose)
fprintf(stderr, "distributing %ld\n", n);
{
unsigned i;
int from_depth, to_depth;
unsigned chunks;
hwloc_bitmap_t *cpuset;
cpuset = malloc(n * sizeof(hwloc_bitmap_t));
if (input) {
err = hwloc_utils_enable_input_format(topology, flags, input, &input_format, verbose, callname);
if (err) {
free(cpuset);
return EXIT_FAILURE;
}
}
hwloc_topology_set_flags(topology, flags);
err = hwloc_topology_load(topology);
if (err < 0) {
free(cpuset);
return EXIT_FAILURE;
}
if (restrictstring) {
hwloc_bitmap_t restrictset = hwloc_bitmap_alloc();
hwloc_bitmap_sscanf(restrictset, restrictstring);
err = hwloc_topology_restrict (topology, restrictset, restrict_flags);
if (err) {
perror("Restricting the topology");
/* FALLTHRU */
}
hwloc_bitmap_free(restrictset);
free(restrictstring);
}
from_depth = 0;
if (from_type) {
if (hwloc_type_sscanf_as_depth(from_type, NULL, topology, &from_depth) < 0 || from_depth < 0) {
fprintf(stderr, "Unsupported or unavailable type `%s' passed to --from, ignoring.\n", from_type);
return EXIT_FAILURE;
}
}
to_depth = INT_MAX;
if (to_type) {
if (hwloc_type_sscanf_as_depth(to_type, NULL, topology, &to_depth) < 0 || to_depth < 0) {
fprintf(stderr, "Unsupported or unavailable type `%s' passed to --to, ignoring.\n", to_type);
return EXIT_FAILURE;
}
}
chunks = hwloc_get_nbobjs_by_depth(topology, from_depth);
{
hwloc_obj_t *roots;
roots = malloc(chunks * sizeof(hwloc_obj_t));
for (i = 0; i < chunks; i++)
roots[i] = hwloc_get_obj_by_depth(topology, from_depth, i);
hwloc_distrib(topology, roots, chunks, cpuset, n, to_depth, dflags);
for (i = 0; (long) i < n; i++) {
char *str = NULL;
if (singlify) {
if (dflags & HWLOC_DISTRIB_FLAG_REVERSE) {
unsigned last = hwloc_bitmap_last(cpuset[i]);
hwloc_bitmap_only(cpuset[i], last);
} else {
hwloc_bitmap_singlify(cpuset[i]);
}
}
if (taskset)
hwloc_bitmap_taskset_asprintf(&str, cpuset[i]);
else
hwloc_bitmap_asprintf(&str, cpuset[i]);
printf("%s\n", str);
free(str);
hwloc_bitmap_free(cpuset[i]);
}
free(roots);
}
free(cpuset);
}
hwloc_topology_destroy(topology);
return EXIT_SUCCESS;
}