Skip to content

Commit

Permalink
utils/info: add --get-attr to output a single attribute without prefi…
Browse files Browse the repository at this point in the history
…x etc

Refs #661

Signed-off-by: Brice Goglin <Brice.Goglin@inria.fr>
(cherry picked from commit 665a51a)
  • Loading branch information
bgoglin committed Apr 29, 2024
1 parent 9fac8a4 commit 49886d7
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 33 deletions.
1 change: 1 addition & 0 deletions NEWS
Expand Up @@ -24,6 +24,7 @@ Version 2.11.0
* Tools/
+ Option --best-memattr may now return multiple nodes. Additional
configuration flags may be given to tweak its behavior.
+ hwloc-info has a new --get-attr option to get a single attribute.
+ hwloc-info now supports "levels", "support" and "topology"
special keywords for backward compatibility for hwloc 3.0.

Expand Down
6 changes: 5 additions & 1 deletion contrib/completion/bash/hwloc
@@ -1,5 +1,5 @@
#
# Copyright © 2018-2023 Inria. All rights reserved.
# Copyright © 2018-2024 Inria. All rights reserved.
# See COPYING in top-level directory.
#

Expand Down Expand Up @@ -162,6 +162,7 @@ _hwloc_info(){
--support
-v --verbose
-q --quiet -s --silent
--get-attr
--ancestors
--ancestor
--children
Expand Down Expand Up @@ -226,6 +227,9 @@ _hwloc_info(){
--best-memattr)
COMPREPLY=( "<memattr>" "" )
;;
--get-attr)
COMPREPLY=( "<name>" "" )
;;
esac
fi
}
Expand Down
17 changes: 17 additions & 0 deletions utils/hwloc/hwloc-info.1in
Expand Up @@ -111,6 +111,17 @@ Include additional detail.
Reduce the amount of details to show.
A single summary line per object is displayed.
.TP
\fB\-\-get\-attr\fR <name>
Only report the attribute of name <name> for each object (instead of all attributes).
The name must exactly match what is usually reported by the program,
for instance "complete cpuset" in "0.1: complete cpuset = %0x00ffff00".

Only the value is reported, any other prefix or object name is ignored,
so that the output may easily be used by other tools.

This option also works on topology information but it is ignored for
\fBlevels\fR and \fBsupport\fR keywords.
.TP
\fB\-\-ancestors\fR
Display information about the object as well as
about all its ancestors up to the root of the topology.
Expand Down Expand Up @@ -343,6 +354,12 @@ To list the OS devices that are of subtype OpenCL:
CoProc:6
CoProc:8

To find the PCI bus ID of PCI devices containing OpenCL devices:

$ hwloc-info --ancestor PCI --get-attr "attr PCI bus id" 'os[opencl]:all'
0000:05:00.0
0000:42:00.0

To list the NUMA nodes that are local a PU:

$ hwloc-info --local-memory pu:25
Expand Down
96 changes: 64 additions & 32 deletions utils/hwloc/hwloc-info.c
Expand Up @@ -86,6 +86,7 @@ static int show_local_memory_flags = HWLOC_LOCAL_NUMANODE_FLAG_SMALLER_LOCALITY
static hwloc_memattr_id_t best_memattr_id = (hwloc_memattr_id_t) -1;
static unsigned long best_node_flags = 0;
static unsigned current_obj;
static const char *only_attr_name = NULL;

void usage(const char *name, FILE *where)
{
Expand All @@ -96,6 +97,7 @@ void usage(const char *name, FILE *where)
fprintf (where, " --support Report information about supported features\n");
fprintf (where, " -v --verbose Include additional details\n");
fprintf (where, " -q --quiet -s Reduce the amount of details to show\n");
fprintf (where, " --get-attr <name> Only show the attribute line with name <name>\n");
fprintf (where, " --ancestors Display the chain of ancestor objects up to the root\n");
fprintf (where, " --ancestor <type> Only display the ancestor of the given type\n");
fprintf (where, " --children Display all children\n");
Expand Down Expand Up @@ -132,7 +134,12 @@ void usage(const char *name, FILE *where)
static void
hwloc_info_show_attr(const char *prefix, const char *name, const char *value)
{
printf("%s %s = %s\n", prefix, name, value);
if (only_attr_name) {
if (!strcmp(only_attr_name, name))
printf("%s\n", value);
} else {
printf("%s %s = %s\n", prefix, name, value);
}
}

static void
Expand Down Expand Up @@ -407,17 +414,19 @@ hwloc_info_show_ancestor(hwloc_topology_t topology, hwloc_obj_t ancestor,
{
char ancestors[128];
hwloc_obj_type_snprintf(ancestors, sizeof(ancestors), ancestor, 1);
if (verbose < 0)
printf("%s%s:%u\n", prefix, ancestors, ancestor->logical_index);
else if (level > 0)
printf("%s%s L#%u = parent #%u of %s L#%u\n",
prefix, ancestors, ancestor->logical_index, level, objs, obj->logical_index);
else if (level == 0) /* the object itself, don't show it twice */
printf("%s%s L#%u\n",
prefix, ancestors, ancestor->logical_index);
else /* single ancestor */
printf("%s%s L#%u = parent of %s L#%u\n",
prefix, ancestors, ancestor->logical_index, objs, obj->logical_index);
if (!only_attr_name) {
if (verbose < 0)
printf("%s%s:%u\n", prefix, ancestors, ancestor->logical_index);
else if (level > 0)
printf("%s%s L#%u = parent #%u of %s L#%u\n",
prefix, ancestors, ancestor->logical_index, level, objs, obj->logical_index);
else if (level == 0) /* the object itself, don't show it twice */
printf("%s%s L#%u\n",
prefix, ancestors, ancestor->logical_index);
else /* single ancestor */
printf("%s%s L#%u = parent of %s L#%u\n",
prefix, ancestors, ancestor->logical_index, objs, obj->logical_index);
}
hwloc_info_show_obj(topology, ancestor, ancestors, prefix, verbose);
}

Expand All @@ -428,11 +437,13 @@ hwloc_info_show_descendant(hwloc_topology_t topology, hwloc_obj_t descendant,
{
char descendants[128];
hwloc_obj_type_snprintf(descendants, sizeof(descendants), descendant, 1);
if (verbose < 0)
printf("%s%s:%u\n", prefix, descendants, descendant->logical_index);
else
printf("%s%s L#%u = descendant #%u of %s L#%u\n",
prefix, descendants, descendant->logical_index, number, objs, obj->logical_index);
if (!only_attr_name) {
if (verbose < 0)
printf("%s%s:%u\n", prefix, descendants, descendant->logical_index);
else
printf("%s%s L#%u = descendant #%u of %s L#%u\n",
prefix, descendants, descendant->logical_index, number, objs, obj->logical_index);
}
hwloc_info_show_obj(topology, descendant, descendants, prefix, verbose);
}

Expand All @@ -443,11 +454,13 @@ hwloc_info_show_child(hwloc_topology_t topology, hwloc_obj_t child,
{
char childs[128];
hwloc_obj_type_snprintf(childs, sizeof(childs), child, 1);
if (verbose < 0)
printf("%s%s:%u\n", prefix, childs, child->logical_index);
else
printf("%s%s L#%u = child #%u of %s L#%u\n",
prefix, childs, child->logical_index, number, objs, obj->logical_index);
if (!only_attr_name) {
if (verbose < 0)
printf("%s%s:%u\n", prefix, childs, child->logical_index);
else
printf("%s%s L#%u = child #%u of %s L#%u\n",
prefix, childs, child->logical_index, number, objs, obj->logical_index);
}
hwloc_info_show_obj(topology, child, childs, prefix, verbose);
}

Expand All @@ -458,11 +471,13 @@ hwloc_info_show_local_memory(hwloc_topology_t topology, hwloc_obj_t node,
{
char nodes[128];
hwloc_obj_type_snprintf(nodes, sizeof(nodes), node, 1);
if (verbose < 0)
printf("%s%s:%u\n", prefix, nodes, node->logical_index);
else
printf("%s%s L#%u = local memory #%u of %s L#%u\n",
prefix, nodes, node->logical_index, number, objs, obj->logical_index);
if (!only_attr_name) {
if (verbose < 0)
printf("%s%s:%u\n", prefix, nodes, node->logical_index);
else
printf("%s%s L#%u = local memory #%u of %s L#%u\n",
prefix, nodes, node->logical_index, number, objs, obj->logical_index);
}
hwloc_info_show_obj(topology, node, nodes, prefix, verbose);
}

Expand All @@ -471,10 +486,12 @@ hwloc_info_show_single_obj(hwloc_topology_t topology,
hwloc_obj_t obj, const char *objs,
const char *prefix, int verbose)
{
if (verbose < 0)
printf("%s%s:%u\n", prefix, objs, obj->logical_index);
else
printf("%s%s L#%u\n", prefix, objs, obj->logical_index);
if (!only_attr_name) {
if (verbose < 0)
printf("%s%s:%u\n", prefix, objs, obj->logical_index);
else
printf("%s%s L#%u\n", prefix, objs, obj->logical_index);
}
hwloc_info_show_obj(topology, obj, objs, prefix, verbose);
}

Expand Down Expand Up @@ -697,7 +714,14 @@ hwloc_info_show_topology_infos(hwloc_topology_t topology)
|| !strcmp(infoname, "Architecture")
|| !strcmp(infoname, "hwlocVersion")
|| !strcmp(infoname, "ProcessName")) {
printf("info %s = %s\n", infoname, root->infos[i].value);
if (!only_attr_name)
printf("info %s = %s\n", infoname, root->infos[i].value);
else {
char name[256];
snprintf(name, sizeof(name), "info %s", infoname);
if (!strcmp(only_attr_name, name))
printf("%s\n", root->infos[i].value);
}
}
}
}
Expand Down Expand Up @@ -816,6 +840,14 @@ main (int argc, char *argv[])
usage(callname, stdout);
exit(EXIT_SUCCESS);
}
else if (!strcmp (argv[0], "--get-attr")) {
if (argc < 2) {
usage (callname, stderr);
exit(EXIT_FAILURE);
}
only_attr_name = argv[1];
opt = 1;
}
else if (!strcmp (argv[0], "-n"))
show_index_prefix = 1;
else if (!strcmp (argv[0], "--ancestors"))
Expand Down
7 changes: 7 additions & 0 deletions utils/hwloc/test-hwloc-info.output
Expand Up @@ -9,6 +9,9 @@ Special depth -3: 2 NUMANode (type #13)
info Backend = Synthetic
info SyntheticDescription = node:2 core:3 pu:4

# topology infos --get-attr
node:2 core:3 pu:4

# levels support
depth 0: 1 Machine (type #0)
depth 1: 2 Group0 (type #12)
Expand Down Expand Up @@ -403,6 +406,10 @@ L1dCache:5
L3Cache:1
Package:2

# Allowed cpuset of where NUMA is attached
0x0000ff00
0x0000ffff,0x0

# Children of L2 and Core of Node, silent
L1dCache:2
L1dCache:3
Expand Down
6 changes: 6 additions & 0 deletions utils/hwloc/test-hwloc-info.sh.in
Expand Up @@ -46,6 +46,9 @@ set -e
echo "# topology infos"
$info --if synthetic --input "node:2 core:3 pu:4" topology | grep -v hwlocVersion | grep -v ProcessName
echo
echo "# topology infos --get-attr"
$info --if synthetic --input "node:2 core:3 pu:4" --get-attr "info SyntheticDescription" topology
echo
echo "# levels support"
$info --if synthetic --input "node:2 core:3 pu:4" levels support
echo
Expand Down Expand Up @@ -78,6 +81,9 @@ set -e
echo "# Where a NUMA is attached"
$info --if synthetic --input "pack:4 [numa] l3:2 [numa] [numa] core:4 pu:2" --ancestor kind=normal --first -s numa:3 numa:14
echo
echo "# Allowed cpuset of where NUMA is attached"
$info --if synthetic --input "pack:4 [numa] l3:2 [numa] [numa] core:4 pu:2" --ancestor kind=normal --first --get-attr "allowed cpuset" numa:3 numa:14
echo

echo "# Children of L2 and Core of Node, silent"
$info --if synthetic --input "node:2 core:2 l2:2 l1d:2 pu:2" --children -s l2:1 node:1.core:1
Expand Down

0 comments on commit 49886d7

Please sign in to comment.