Skip to content

Commit 323bdba

Browse files
committed
utils: factorize the computing of the location prefix length
pci[vendor:device] needs a temporary hack because it's not handled in hwloc_calc_parse_level() yet while typelen includes it. Will be removed in next commit. Signed-off-by: Brice Goglin <[email protected]>
1 parent e9c4e82 commit 323bdba

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

utils/hwloc/hwloc-annotate.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ get_unique_obj(hwloc_topology_t topology, int topodepth, char *str,
171171
size_t typelen;
172172
int err;
173173

174-
typelen = strspn(str, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
174+
typelen = hwloc_calc_parse_level_size(str);
175175
if (!typelen || (str[typelen] != ':' && str[typelen] != '=' && str[typelen] != '['))
176176
return NULL;
177177

@@ -809,7 +809,7 @@ int main(int argc, char *argv[])
809809
apply(topology, hwloc_get_root_obj(topology));
810810
} else {
811811
size_t typelen;
812-
typelen = strspn(location, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
812+
typelen = hwloc_calc_parse_level_size(location);
813813
if (typelen && (location[typelen] == ':' || location[typelen] == '=' || location[typelen] == '[')) {
814814
struct hwloc_calc_location_context_s lcontext;
815815
lcontext.topology = topology;

utils/hwloc/hwloc-calc.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,18 @@ hwloc_calc_get_obj_inside_sets_by_depth(struct hwloc_calc_location_context_s *lc
151151
return NULL;
152152
}
153153

154+
/* return the length of the type/depth prefix
155+
* 0 if not found or invalid.
156+
*/
157+
static __hwloc_inline size_t
158+
hwloc_calc_parse_level_size(const char *string)
159+
{
160+
/* type/depth prefix ends with either '.' (for child), "=" (for name of osdev),
161+
* ':' (for index).
162+
*/
163+
return strcspn(string, ":=.");
164+
}
165+
154166
static __hwloc_inline int
155167
hwloc_calc_parse_level(struct hwloc_calc_location_context_s *lcontext,
156168
hwloc_topology_t topology,
@@ -328,7 +340,7 @@ hwloc_calc_append_object_range(struct hwloc_calc_location_context_s *lcontext,
328340
/* parse the next string before calling ourself recursively */
329341
size_t typelen;
330342
const char *nextstring = dot+1;
331-
typelen = strspn(nextstring, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
343+
typelen = hwloc_calc_parse_level_size(nextstring);
332344
if (!typelen || nextstring[typelen] != ':') {
333345
if (verbose >= 0)
334346
fprintf(stderr, "hierarchical sublocation %s contains types not followed by colon and index range\n", nextstring);
@@ -592,6 +604,14 @@ hwloc_calc_process_location(struct hwloc_calc_location_context_s *lcontext,
592604
hwloc_obj_t obj = NULL;
593605

594606
if (*sep == ':' || *sep == '[') {
607+
if (level.type == HWLOC_OBJ_PCI_DEVICE) {
608+
/* FIXME: temporary hack because typelen includes pci and osdev filters
609+
* but only osdev types are handled in hwloc_calc_parse_level()
610+
*/
611+
const char *bracket = strchr(arg, '[');
612+
if (bracket && bracket-sep < 0)
613+
sep = bracket;
614+
}
595615
return hwloc_calc_append_iodev_by_index(lcontext, level.type, level.depth, sep, cbfunc, cbdata);
596616

597617
} else if (*sep == '=' && level.type == HWLOC_OBJ_PCI_DEVICE) {
@@ -696,7 +716,7 @@ hwloc_calc_process_location_as_set(struct hwloc_calc_location_context_s *lcontex
696716
mode, verbose);
697717

698718
/* try to match a type/depth followed by a special character */
699-
typelen = strspn(arg, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
719+
typelen = hwloc_calc_parse_level_size(arg);
700720
if (typelen && (arg[typelen] == ':' || arg[typelen] == '=' || arg[typelen] == '[')) {
701721
/* process type/depth */
702722
struct hwloc_calc_process_location_set_cbdata_s cbdata;

utils/hwloc/hwloc-info.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ main (int argc, char *argv[])
10511051
return EXIT_FAILURE;
10521052
} else {
10531053
/* try to match a type/depth followed by a special character */
1054-
typelen = strspn(argv[0], "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
1054+
typelen = hwloc_calc_parse_level_size(argv[0]);
10551055
if (typelen && (argv[0][typelen] == ':' || argv[0][typelen] == '=' || argv[0][typelen] == '[')) {
10561056
err = hwloc_calc_process_location(&lcontext, argv[0], typelen,
10571057
hwloc_calc_process_location_info_cb, NULL);

0 commit comments

Comments
 (0)