Skip to content

Commit

Permalink
hwloc.ps: add --single-ancestor option
Browse files Browse the repository at this point in the history
Signed-off-by: Valentin Hoyet <valentin.hoyet@inria.fr>
Signed-off-by: Brice Goglin <Brice.Goglin@inria.fr>
  • Loading branch information
Valentin Hoyet authored and bgoglin committed Mar 15, 2021
1 parent 7ad9a4e commit 3e0137c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
2 changes: 2 additions & 0 deletions NEWS
Expand Up @@ -34,6 +34,8 @@ Version 2.5.0
+ lstopo now has a --windows-processor-groups option on Windows.
+ hwloc-ps now has a --short-name option to avoid long/truncated
command path.
+ hwloc-ps now has a --single-ancestor option to return a single
(possibly too large) object where a process is bound.


Version 2.4.1
Expand Down
3 changes: 2 additions & 1 deletion contrib/completion/bash/hwloc
@@ -1,5 +1,5 @@
#
# Copyright © 2018-2020 Inria. All rights reserved.
# Copyright © 2018-2021 Inria. All rights reserved.
# See COPYING in top-level directory.
#

Expand Down Expand Up @@ -468,6 +468,7 @@ _hwloc_ps(){
-l --logical
-p --physical
-c --cpuset
--single-ancestor
-t --threads
-e --get-last-cpu-location
--pid-cmd
Expand Down
15 changes: 15 additions & 0 deletions utils/hwloc/hwloc-ps.1in
Expand Up @@ -55,6 +55,10 @@ Otherwise, show all threads inside each process where at least one
thread is bound.
This is currently only supported on Linux.
.TP
\fB\-\-single\-ancestor\fR
When the object is bound to different objects,
report their common ancestor (even if it may be larger than the actual binding).
.TP
\fB\-e\fR \fB\-\-get\-last\-cpu\-location\fR
Report the last processors where the process/thread ran.
Note that the result may already be outdated when reported
Expand Down Expand Up @@ -129,6 +133,17 @@ If a process is bound, it appears in the default output:
$ hwloc-ps
4759 Core:0 myprogram

If a process is bound on two cores of a larger package,
the output will show these cores.
Option \-\-single\-ancestor will rather return the package
even if it is actually larger than the binding here
(the process is not bound to Core:0 of Package:0):

$ hwloc-ps
4863 Core:1 Core:2 myprogram
$ hwloc-ps --single-ancestor
4863 Package:0 myprogram

If a process is not bound but 3 of his 4 threads are bound,
it only appears in the thread-aware output (or if explicitly selected):

Expand Down
25 changes: 21 additions & 4 deletions utils/hwloc/hwloc-ps.c
Expand Up @@ -32,6 +32,7 @@ static int show_threads = 0;
static char *only_name = NULL;
static int show_cpuset = 0;
static int logical = 1;
static int single_ancestor = 0;
#define NO_ONLY_PID -1
static long only_pid = NO_ONLY_PID;
static long only_uid;
Expand All @@ -54,6 +55,7 @@ void usage(const char *name, FILE *where)
fprintf (where, " -l --logical Use logical object indexes (default)\n");
fprintf (where, " -p --physical Use physical object indexes\n");
fprintf (where, " -c --cpuset Show cpuset instead of objects\n");
fprintf (where, " --single-ancestor Show a single ancestor containing the binding\n");
#ifdef HWLOC_LINUX_SYS
fprintf (where, " -t --threads Show threads\n");
#endif
Expand Down Expand Up @@ -82,13 +84,25 @@ static void print_task(hwloc_topology_t topology,
} else {
hwloc_bitmap_t remaining = hwloc_bitmap_dup(cpuset);
int first = 1;
while (!hwloc_bitmap_iszero(remaining)) {
char type[64];
unsigned idx;
char type[64];
unsigned idx;
if (single_ancestor) {
hwloc_obj_t obj = hwloc_get_obj_covering_cpuset(topology, cpuset);
while (obj->parent && hwloc_bitmap_isequal(obj->cpuset, obj->parent->cpuset) && !hwloc_obj_type_is_cache(obj->parent->type) )
obj = obj->parent;

hwloc_obj_type_snprintf(type, sizeof(type), obj, 1);
idx = logical ? obj->logical_index : obj->os_index;
if (idx == (unsigned) -1)
printf("%s", type);
else
printf("%s:%u", type, idx);
} else {
while (!hwloc_bitmap_iszero(remaining)) {
hwloc_obj_t obj = hwloc_get_first_largest_obj_inside_cpuset(topology, remaining);
/* don't show a cache if there's something equivalent and nicer */
while (hwloc_obj_type_is_cache(obj->type) && obj->arity == 1)
obj = obj->first_child;
obj = obj->first_child;
hwloc_obj_type_snprintf(type, sizeof(type), obj, 1);
idx = logical ? obj->logical_index : obj->os_index;
if (idx == (unsigned) -1)
Expand All @@ -97,6 +111,7 @@ static void print_task(hwloc_topology_t topology,
printf("%s%s:%u", first ? "" : " ", type, idx);
hwloc_bitmap_andnot(remaining, remaining, obj->cpuset);
first = 0;
}
}
hwloc_bitmap_free(remaining);
}
Expand Down Expand Up @@ -392,6 +407,8 @@ int main(int argc, char *argv[])
#else
fprintf (stderr, "Listing threads is currently only supported on Linux\n");
#endif
} else if (!strcmp(argv[0], "--single-ancestor")) {
single_ancestor = 1;
} else if (!strcmp(argv[0], "--pid")) {
if (argc < 2) {
usage(callname, stderr);
Expand Down

0 comments on commit 3e0137c

Please sign in to comment.