Skip to content

Commit

Permalink
utils: add --cpuset-output-format when we had --taskset
Browse files Browse the repository at this point in the history
hwloc-bind/calc/distrib and lstopo now support:
--cpuset-output-format hwloc (default)
--cpuset-output-format taskset (supersedes --taskset)
--cpuset-output-format list (new)

Or --cof for shorter

Refs #667

Signed-off-by: Brice Goglin <[email protected]>
  • Loading branch information
bgoglin committed Jun 7, 2024
1 parent 8c25f44 commit e25888c
Show file tree
Hide file tree
Showing 18 changed files with 193 additions and 79 deletions.
20 changes: 16 additions & 4 deletions contrib/completion/bash/hwloc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ _lstopo() {
--windows-processor-groups
-c --cpuset
-C --cpuset-only
--taskset
--cpuset-output-format --cof
--filter --ignore
--no-caches
--no-useless-caches
Expand Down Expand Up @@ -147,6 +147,9 @@ _lstopo() {
--distances-transform)
COMPREPLY=( `compgen -W "links merge-switch-ports transitive-closure" -- "$cur"` )
;;
--cpuset-output-format | --cof)
COMPREPLY=( "hwloc" "list" taskset" )
;;
esac
fi
}
Expand Down Expand Up @@ -251,7 +254,7 @@ _hwloc_bind(){
--nodeset
--pid
--tid
--taskset
--cpuset-output-format --cof
--restrict
--restrict-flags
--disallowed --whole-system
Expand Down Expand Up @@ -289,6 +292,9 @@ _hwloc_bind(){
--best-memattr)
COMPREPLY=( "<memattr>" "" )
;;
--cpuset-output-format | --cof)
COMPREPLY=( "hwloc" "list" taskset" )
;;
esac
fi
}
Expand All @@ -315,7 +321,7 @@ _hwloc_calc(){
--no --nodeset-output
--oo --object-output
--sep
--taskset
--cpuset-output-format --cof
--single
--restrict
--restrict-flags
Expand Down Expand Up @@ -366,6 +372,9 @@ _hwloc_calc(){
--cpukind)
COMPREPLY=( "<name>=<value>" "<n>" "" )
;;
--cpuset-output-format | --cof)
COMPREPLY=( "hwloc" "list" taskset" )
;;
esac
fi
}
Expand Down Expand Up @@ -451,7 +460,7 @@ _hwloc_distrib(){
--input -i
--input-format --if
--single
--taskset
--cpuset-output-format --cof
-v --verbose
--version
-h --help
Expand All @@ -478,6 +487,9 @@ _hwloc_distrib(){
--restrict-flags)
COMPREPLY=( "<integer>" "" )
;;
--cpuset-output-format | --cof)
COMPREPLY=( "hwloc" "list" taskset" )
;;
esac
fi
}
Expand Down
5 changes: 3 additions & 2 deletions doc/hwloc.doxy
Original file line number Diff line number Diff line change
Expand Up @@ -3058,8 +3058,9 @@ enabled for the current topology.
See for instance hwloc_bitmap_taskset_snprintf() in
\ref hwlocality_bitmap().

Most hwloc command-line tools also support the <tt>\--taskset</tt>
option to manipulate taskset-specific strings.
Most hwloc command-line tools also support the option
<tt>\--cpuset-output-format taskset</tt>
to manipulate taskset-specific strings.
</dd>

</dl>
Expand Down
13 changes: 9 additions & 4 deletions utils/hwloc/hwloc-bind.1in
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,16 @@ This option does not apply to the output, see \fB\-\-get\fR above.
Interpret input locations with logical indexes instead of physical/OS indexes (default).
This option does not apply to the output, see \fB\-\-get\fR above.
.TP
\fB\-\-taskset\fR
Display CPU set strings in the format recognized by the taskset command-line
program instead of hwloc-specific CPU set string format.
\fB\-\-cpuset\-output\-format\fR <hwloc|list|taskset> \fB\-\-cof\fR <hwloc|list|taskset>
Change the format of CPUset or nodeset strings displayed by \fB\-\-get\fR, \fB\-e\fR, etc.
By default, the hwloc-specific format is used.
If \fIlist\fR is given, the output is a comma-separated of numbers or ranges,
e.g. 2,4-5,8 .
If \fItaskset\fR is given, the output is compatible with the taskset program
(replaces the former \fB--taskset\fR option).

This option has no impact on the format of input CPU set strings,
both formats are always accepted.
both hwloc and taskset formats are always accepted.
.TP
\fB\-f\fR \fB\-\-force\fR
Launch the executable even if binding failed.
Expand Down
36 changes: 21 additions & 15 deletions utils/hwloc/hwloc-bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ void usage(const char *name, FILE *where)
#ifdef HWLOC_LINUX_SYS
fprintf(where, " --tid <tid> Operate on thread <tid>\n");
#endif
fprintf(where, " --taskset Use taskset-specific format when displaying cpuset strings\n");
fprintf(where, " --cpuset-output-format <hwloc|list|taskset>\n"
" --cof <hwloc|list|taskset>\n"
" Change the format of cpuset outputs\n");
fprintf(where, "Miscellaneous options:\n");
fprintf(where, " -f --force Launch the command even if binding failed\n");
fprintf(where, " -q --quiet Hide non-fatal error messages\n");
Expand All @@ -85,7 +87,7 @@ int main(int argc, char *argv[])
int no_smt = -1;
int only_hbm = -1;
int logical = 1;
int taskset = 0;
enum hwloc_utils_cpuset_format_e cpuset_output_format = HWLOC_UTILS_CPUSET_FORMAT_HWLOC;
unsigned cpubind_flags = 0;
hwloc_membind_policy_t membind_policy = HWLOC_MEMBIND_BIND;
int got_mempolicy = 0;
Expand Down Expand Up @@ -270,8 +272,21 @@ int main(int argc, char *argv[])
logical = 0;
goto next;
}
if (!strcmp(argv[0], "--cpuset-output-format") || !strcmp(argv[0], "--cof")) {
if (argc < 2) {
usage (callname, stderr);
exit(EXIT_FAILURE);
}
cpuset_output_format = hwloc_utils_parse_cpuset_format(argv[1]);
if (HWLOC_UTILS_CPUSET_FORMAT_UNKNOWN == cpuset_output_format) {
fprintf(stderr, "Unrecognized %s argument %s\n", argv[0], argv[1]);
exit(EXIT_FAILURE);
}
opt = 1;
goto next;
}
if (!strcmp(argv[0], "--taskset")) {
taskset = 1;
cpuset_output_format = HWLOC_UTILS_CPUSET_FORMAT_TASKSET;
goto next;
}
if (!strcmp (argv[0], "-e") || !strncmp (argv[0], "--get-last-cpu-location", 10)) {
Expand Down Expand Up @@ -417,16 +432,10 @@ int main(int argc, char *argv[])
if (use_nodeset) {
hwloc_bitmap_t nset = hwloc_bitmap_alloc();
hwloc_cpuset_to_nodeset(topology, cpubind_set, nset);
if (taskset)
hwloc_bitmap_taskset_asprintf(&s, nset);
else
hwloc_bitmap_asprintf(&s, nset);
hwloc_utils_cpuset_format_asprintf(&s, nset, cpuset_output_format);
hwloc_bitmap_free(nset);
} else {
if (taskset)
hwloc_bitmap_taskset_asprintf(&s, cpubind_set);
else
hwloc_bitmap_asprintf(&s, cpubind_set);
hwloc_utils_cpuset_format_asprintf(&s, cpubind_set, cpuset_output_format);
}

} else {
Expand All @@ -446,10 +455,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "hwloc_get_membind failed (errno %d %s)\n", errno, errmsg);
return EXIT_FAILURE;
}
if (taskset)
hwloc_bitmap_taskset_asprintf(&s, membind_set);
else
hwloc_bitmap_asprintf(&s, membind_set);
hwloc_utils_cpuset_format_asprintf(&s, membind_set, cpuset_output_format);
switch (policy) {
case HWLOC_MEMBIND_FIRSTTOUCH: policystr = "firsttouch"; break;
case HWLOC_MEMBIND_BIND: policystr = "bind"; break;
Expand Down
13 changes: 9 additions & 4 deletions utils/hwloc/hwloc-calc.1in
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,16 @@ while a comma is used to separate indexes
\fB\-\-single\fR
Singlify the output to a single CPU.
.TP
\fB\-\-taskset\fR
Display CPU set strings in the format recognized by the taskset command-line
program instead of hwloc-specific CPU set string format.
\fB\-\-cpuset\-output\-format\fR <hwloc|list|taskset> \fB\-\-cof\fR <hwloc|list|taskset>
Change the format of displayed CPU set strings.
By default, the hwloc-specific format is used.
If \fIlist\fR is given, the output is a comma-separated of numbers or ranges,
e.g. 2,4-5,8 .
If \fItaskset\fR is given, the output is compatible with the taskset program
(replaces the former \fB--taskset\fR option).

This option has no impact on the format of input CPU set strings,
both formats are always accepted.
both hwloc and taskset formats are always accepted.
.TP
\fB\-q\fR \fB\-\-quiet\fR
Hide non-fatal error messages.
Expand Down
28 changes: 20 additions & 8 deletions utils/hwloc/hwloc-calc.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ void usage(const char *callname __hwloc_attribute_unused, FILE *where)
fprintf(where, " --no --nodeset-output Manipulate nodesets instead of cpusets for outputs\n");
fprintf(where, " --oo --object-output Report objects instead of object indexes\n");
fprintf(where, " --sep <sep> Use separator <sep> in the output\n");
fprintf(where, " --taskset Use taskset-specific format when displaying cpuset strings\n");
fprintf(where, " --cpuset-output-format <hwloc|list|taskset>\n"
" --cof <hwloc|list|taskset>\n"
" Change the format of cpuset outputs\n");
fprintf(where, " --single Singlify the output to a single CPU\n");
fprintf(where, "Miscellaneous options:\n");
fprintf(where, " -q --quiet Hide non-fatal error messages\n");
Expand All @@ -83,7 +85,7 @@ static unsigned long best_node_flags = 0;
static int showobjs = 0;
static int no_smt = -1;
static int singlify = 0;
static int taskset = 0;
static enum hwloc_utils_cpuset_format_e cpuset_output_format = HWLOC_UTILS_CPUSET_FORMAT_HWLOC;
static hwloc_bitmap_t cpukind_cpuset = NULL;

static int
Expand Down Expand Up @@ -276,10 +278,7 @@ hwloc_calc_output(hwloc_topology_t topology, const char *sep, hwloc_bitmap_t set

} else {
char *string = NULL;
if (taskset)
hwloc_bitmap_taskset_asprintf(&string, set);
else
hwloc_bitmap_asprintf(&string, set);
hwloc_utils_cpuset_format_asprintf(&string, set, cpuset_output_format);
printf("%s\n", string);
free(string);
}
Expand Down Expand Up @@ -593,10 +592,23 @@ int main(int argc, char *argv[])
singlify = 1;
goto next;
}
if (!strcmp(argv[0], "--taskset")) {
taskset = 1;
if (!strcmp(argv[0], "--cpuset-output-format") || !strcmp(argv[0], "--cof")) {
if (argc < 2) {
usage (callname, stderr);
exit(EXIT_FAILURE);
}
cpuset_output_format = hwloc_utils_parse_cpuset_format(argv[1]);
if (HWLOC_UTILS_CPUSET_FORMAT_UNKNOWN == cpuset_output_format) {
fprintf(stderr, "Unrecognized %s argument %s\n", argv[0], argv[1]);
exit(EXIT_FAILURE);
}
opt = 1;
goto next;
}
if (!strcmp(argv[0], "--taskset")) {
cpuset_output_format = HWLOC_UTILS_CPUSET_FORMAT_TASKSET;
goto next;
}

fprintf (stderr, "Unrecognized option: %s\n", argv[0]);
usage(callname, stderr);
Expand Down
10 changes: 7 additions & 3 deletions utils/hwloc/hwloc-distrib.1in
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ hwloc-distrib \- Build a number of cpu masks distributed on the system
\fB\-\-single\fR
Singlify each output to a single CPU.
.TP
\fB\-\-taskset\fR
Show CPU set strings in the format recognized by the taskset command-line
program instead of hwloc-specific CPU set string format.
\fB\-\-cpuset\-output\-format\fR <hwloc|list|taskset> \fB\-\-cof\fR <hwloc|list|taskset>
Change the format of displayed CPU set strings.
By default, the hwloc-specific format is used.
If \fIlist\fR is given, the output is a comma-separated of numbers or ranges,
e.g. 2,4-5,8 .
If \fItaskset\fR is given, the output is compatible with the taskset program
(replaces the former \fB--taskset\fR option).
.TP
\fB\-v\fR \fB\-\-verbose\fR
Verbose messages.
Expand Down
27 changes: 20 additions & 7 deletions utils/hwloc/hwloc-distrib.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright © 2009 CNRS
* Copyright © 2009-2022 Inria. All rights reserved.
* Copyright © 2009-2024 Inria. All rights reserved.
* Copyright © 2009-2010 Université Bordeaux
* Copyright © 2009-2011 Cisco Systems, Inc. All rights reserved.
* Copyright © 2023 Université de Reims Champagne-Ardenne. All rights reserved.
Expand Down Expand Up @@ -33,6 +33,9 @@ void usage(const char *callname __hwloc_attribute_unused, FILE *where)
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, " --cpuset-output-format <hwloc|list|taskset>\n"
" --cof <hwloc|list|taskset>\n"
" Change the format of cpuset outputs\n");
fprintf(where, "Miscellaneous options:\n");
fprintf(where, " -v --verbose Show verbose messages\n");
fprintf(where, " --version Report version and exit\n");
Expand All @@ -45,7 +48,7 @@ int main(int argc, char *argv[])
char *callname;
char *input = NULL;
struct hwloc_utils_input_format_s input_format = HWLOC_UTILS_INPUT_FORMAT_DEFAULT;
int taskset = 0;
enum hwloc_utils_cpuset_format_e cpuset_output_format = HWLOC_UTILS_CPUSET_FORMAT_HWLOC;
int singlify = 0;
int verbose = 0;
char *restrictstring = NULL;
Expand Down Expand Up @@ -91,8 +94,21 @@ int main(int argc, char *argv[])
singlify = 1;
goto next;
}
if (!strcmp(argv[0], "--cpuset-output-format") || !strcmp(argv[0], "--cof")) {
if (argc < 2) {
usage (callname, stderr);
exit(EXIT_FAILURE);
}
cpuset_output_format = hwloc_utils_parse_cpuset_format(argv[1]);
if (HWLOC_UTILS_CPUSET_FORMAT_UNKNOWN == cpuset_output_format) {
fprintf(stderr, "Unrecognized %s argument %s\n", argv[0], argv[1]);
exit(EXIT_FAILURE);
}
opt = 1;
goto next;
}
if (!strcmp(argv[0], "--taskset")) {
taskset = 1;
cpuset_output_format = HWLOC_UTILS_CPUSET_FORMAT_TASKSET;
goto next;
}
if (!strcmp(argv[0], "-v") || !strcmp(argv[0], "--verbose")) {
Expand Down Expand Up @@ -291,10 +307,7 @@ int main(int argc, char *argv[])
hwloc_bitmap_singlify(cpuset[i]);
}
}
if (taskset)
hwloc_bitmap_taskset_asprintf(&str, cpuset[i]);
else
hwloc_bitmap_asprintf(&str, cpuset[i]);
hwloc_utils_cpuset_format_asprintf(&str, cpuset[i], cpuset_output_format);
printf("%s\n", str);
free(str);
hwloc_bitmap_free(cpuset[i]);
Expand Down
32 changes: 32 additions & 0 deletions utils/hwloc/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,38 @@ hwloc_utils_get_best_node_in_array_by_memattr(hwloc_topology_t topology, hwloc_m
return -1;
}

enum hwloc_utils_cpuset_format_e {
HWLOC_UTILS_CPUSET_FORMAT_UNKNOWN,
HWLOC_UTILS_CPUSET_FORMAT_HWLOC,
HWLOC_UTILS_CPUSET_FORMAT_TASKSET,
HWLOC_UTILS_CPUSET_FORMAT_LIST
};

static __hwloc_inline enum hwloc_utils_cpuset_format_e
hwloc_utils_parse_cpuset_format(const char *string)
{
if (!strcmp(string, "hwloc"))
return HWLOC_UTILS_CPUSET_FORMAT_HWLOC;
else if (!strcmp(string, "list"))
return HWLOC_UTILS_CPUSET_FORMAT_LIST;
else if (!strcmp(string, "taskset"))
return HWLOC_UTILS_CPUSET_FORMAT_TASKSET;
else
return HWLOC_UTILS_CPUSET_FORMAT_UNKNOWN;
}

static __hwloc_inline int
hwloc_utils_cpuset_format_asprintf(char **string, hwloc_const_bitmap_t set,
enum hwloc_utils_cpuset_format_e cpuset_output_format)
{
switch (cpuset_output_format) {
case HWLOC_UTILS_CPUSET_FORMAT_HWLOC: return hwloc_bitmap_asprintf(string, set);
case HWLOC_UTILS_CPUSET_FORMAT_TASKSET: return hwloc_bitmap_taskset_asprintf(string, set);
case HWLOC_UTILS_CPUSET_FORMAT_LIST: return hwloc_bitmap_list_asprintf(string, set);
default: abort();
}
}

static __hwloc_inline unsigned long
hwloc_utils_parse_restrict_flags(char * str){
struct hwloc_utils_parsing_flag possible_flags[] = {
Expand Down
7 changes: 5 additions & 2 deletions utils/hwloc/test-hwloc-calc.output
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# root
# root --cpuset-output-format hwloc
0xffffffff,0xffffffff

# all --taskset
# all --cof list
0-63

# all --cpuset-output-format taskset
0xffffffffffffffff

# hex
Expand Down
Loading

0 comments on commit e25888c

Please sign in to comment.