Skip to content

Commit

Permalink
obj_snprintf(): add flags to tune type names and units
Browse files Browse the repository at this point in the history
Allow to switch to raw sizes without units, or actual KB/MB/GB/...

Use them instead of the old verbose flag for most tools and tests,
except the main lstopo where more tuning is coming.

Signed-off-by: Brice Goglin <Brice.Goglin@inria.fr>
  • Loading branch information
bgoglin committed Dec 13, 2022
1 parent 187cdc9 commit aeee9e3
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 27 deletions.
2 changes: 1 addition & 1 deletion hwloc/topology-synthetic.c
Expand Up @@ -1318,7 +1318,7 @@ hwloc__export_synthetic_obj(struct hwloc_topology * topology, unsigned long flag
res = hwloc_snprintf(tmp, tmplen, "%s%s", hwloc_obj_type_string(obj->type), aritys);
} else {
char types[64];
hwloc_obj_type_snprintf(types, sizeof(types), obj, HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_type_snprintf(types, sizeof(types), obj, HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);
res = hwloc_snprintf(tmp, tmplen, "%s%s", types, aritys);
}
if (hwloc__export_synthetic_update_status(&ret, &tmp, &tmplen, res) < 0)
Expand Down
4 changes: 2 additions & 2 deletions hwloc/topology.c
Expand Up @@ -404,12 +404,12 @@ hwloc_debug_print_object(int indent __hwloc_attribute_unused, hwloc_obj_t obj)
{
char type[64], idx[12], attr[1024], *cpuset = NULL;
hwloc_debug("%*s", 2*indent, "");
hwloc_obj_type_snprintf(type, sizeof(type), obj, HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_type_snprintf(type, sizeof(type), obj, HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);
if (obj->os_index != HWLOC_UNKNOWN_INDEX)
snprintf(idx, sizeof(idx), "#%u", obj->os_index);
else
*idx = '\0';
hwloc_obj_attr_snprintf(attr, sizeof(attr), obj, " ", HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_attr_snprintf(attr, sizeof(attr), obj, " ", HWLOC_OBJ_SNPRINTF_FLAG_MORE_ATTRS|HWLOC_OBJ_SNPRINTF_FLAG_NO_UNITS);
hwloc_debug("%s%s%s%s%s", type, idx, *attr ? "(" : "", attr, *attr ? ")" : "");
if (obj->name)
hwloc_debug(" name \"%s\"", obj->name);
Expand Down
4 changes: 2 additions & 2 deletions hwloc/traversal.c
Expand Up @@ -519,7 +519,7 @@ static const char* hwloc_obj_cache_type_letter(hwloc_obj_cache_type_t type)
int
hwloc_obj_type_snprintf(char * __hwloc_restrict string, size_t size, hwloc_obj_t obj, unsigned long flags)
{
int longnames = (flags & HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
int longnames = (flags & (HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE|HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES));
hwloc_obj_type_t type = obj->type;
switch (type) {
case HWLOC_OBJ_MISC:
Expand Down Expand Up @@ -578,7 +578,7 @@ hwloc_obj_type_snprintf(char * __hwloc_restrict string, size_t size, hwloc_obj_t
int
hwloc_obj_attr_snprintf(char * __hwloc_restrict string, size_t size, hwloc_obj_t obj, const char * separator, unsigned long flags)
{
int verbose = (flags & HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
int verbose = (flags & (HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE|HWLOC_OBJ_SNPRINTF_FLAG_MORE_ATTRS));
const char *prefix = "";
char *tmp = string;
ssize_t tmplen = size;
Expand Down
41 changes: 36 additions & 5 deletions include/hwloc.h
Expand Up @@ -979,8 +979,8 @@ HWLOC_DECLSPEC const char * hwloc_obj_type_string (hwloc_obj_type_t type) __hwlo
*
* If \p size is 0, \p string may safely be \c NULL.
*
* Flags \p flags is a OR'ed set of the following bits:
* HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE is for backward compatibility with 2.x, it shows longer type names, e.g. L1Cache instead of L1.
* Flags \p flags is a OR'ed set of ::hwloc_obj_snprintf_flag_e.
* By default, short names are used.
*
* \return the number of characters that were actually written if not truncating,
* or that would have been written (not including the ending \\0).
Expand All @@ -997,8 +997,9 @@ HWLOC_DECLSPEC int hwloc_obj_type_snprintf(char * __hwloc_restrict string, size_
*
* If \p size is 0, \p string may safely be \c NULL.
*
* Flags \p flags is a OR'ed set of the following bits:
* HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE is for backward compatibility with 2.x, it shows additional attributes and sizes with KB unit but KiB values.
* Flags \p flags is a OR'ed set of ::hwloc_obj_snprintf_flag_e.
* By default, only important attributes such as memory and cache sizes are shown.
* Sizes are reported in units such as GiB or KiB.
*
* \return the number of characters that were actually written if not truncating,
* or that would have been written (not including the ending \\0).
Expand All @@ -1007,7 +1008,37 @@ HWLOC_DECLSPEC int hwloc_obj_attr_snprintf(char * __hwloc_restrict string, size_
hwloc_obj_t obj, const char * __hwloc_restrict separator,
unsigned long flags);

#define HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE (1ULL<<0)
/** \brief Flags to be given to hwloc_obj_type_snprintf() and hwloc_obj_attr_snprintf(). */
enum hwloc_obj_snprintf_flag_e {
/** \brief Use long type names such as L2Cache instead of L2.
* \hideinitializer
*/
HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES = 1ULL<<1,

/** \brief Display additional attributes such as
* cache associativity, PCI link speed, and total memory.
* \hideinitializer
*/
HWLOC_OBJ_SNPRINTF_FLAG_MORE_ATTRS =1ULL<<2,

/** \brief Display memory sizes in bytes without units.
* \hideinitializer
*/
HWLOC_OBJ_SNPRINTF_FLAG_NO_UNITS = 1ULL<<3,

/** \brief Display memory sizes in KB, MB, GB, etc
* i.e. divide by 1000 instead of 1024 for KiB, MiB, GiB, etc.
* \hideinitializer
*/
HWLOC_OBJ_SNPRINTF_FLAG_UNITS_1000 = 1ULL<<4,

/** \brief Backward compatibility with hwloc 2.x verbose mode,
* shows additional attributes,
* and memory sizes with KB unit but KiB values.
* \hideinitializer
*/
HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE = 1ULL<<0
};

/** \brief Return an object type and attributes from a type string.
*
Expand Down
6 changes: 6 additions & 0 deletions include/hwloc/rename.h
Expand Up @@ -205,6 +205,12 @@ extern "C" {
#define hwloc_obj_type_string HWLOC_NAME(obj_type_string )
#define hwloc_obj_type_snprintf HWLOC_NAME(obj_type_snprintf )
#define hwloc_obj_attr_snprintf HWLOC_NAME(obj_attr_snprintf )
#define hwloc_obj_snprintf_flag_e HWLOC_NAME(obj_snprintf_flag_e)
#define HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES HWLOC_NAME_CAPS(OBJ_SNPRINTF_FLAG_LONG_NAMES)
#define HWLOC_OBJ_SNPRINTF_FLAG_MORE_ATTRS HWLOC_NAME_CAPS(OBJ_SNPRINTF_FLAG_MORE_ATTRS)
#define HWLOC_OBJ_SNPRINTF_FLAG_NO_UNITS HWLOC_NAME_CAPS(OBJ_SNPRINTF_FLAG_NO_UNITS)
#define HWLOC_OBJ_SNPRINTF_FLAG_UNITS_1000 HWLOC_NAME_CAPS(OBJ_SNPRINTF_FLAG_UNITS_1000)
#define HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE HWLOC_NAME_CAPS(OBJ_SNPRINTF_FLAG_OLD_VERBOSE)
#define hwloc_type_sscanf HWLOC_NAME(type_sscanf)

#define hwloc_obj_get_info_by_name HWLOC_NAME(obj_get_info_by_name)
Expand Down
18 changes: 18 additions & 0 deletions include/private/private.h
Expand Up @@ -464,11 +464,29 @@ extern int hwloc_snprintf(char *str, size_t size, const char *format, ...) __hwl
/* uses HWLOC_OBJ_SNPRINTF_FLAG_ flags */
static __hwloc_inline int hwloc_memory_size_snprintf(char *buffer, size_t bufsize, unsigned long long size, unsigned long flags)
{
/* no units */
if (flags & HWLOC_OBJ_SNPRINTF_FLAG_NO_UNITS) {
return snprintf(buffer, bufsize, "%llu", size);
}

/* old deprecated format (KiB value with KB units) */
if (flags & HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE) {
return snprintf(buffer, bufsize, "%llu%s", ((size>>9)+1)>>1, "KB");
}

/* units 1000 */
if (flags & HWLOC_OBJ_SNPRINTF_FLAG_UNITS_1000) {
if (size < 10000000ULL) {
return snprintf(buffer, bufsize, "%llu%s", ((size/500)+1)/2, "KB");
} else if (size < 10000000000ULL) {
return snprintf(buffer, bufsize, "%llu%s", ((size/500000)+1)/2, "MB");
} else if (size < 10000000000000ULL) {
return snprintf(buffer, bufsize, "%llu%s", ((size/500000000)+1)/2, "GB");
} else {
return snprintf(buffer, bufsize, "%llu%s", ((size/500000000000ULL)+1)/2, "TB");
}
}

/* units 1024 */
if (size < (10ULL<<20)) {
return snprintf(buffer, bufsize, "%llu%s", ((size>>9)+1)>>1, "KiB");
Expand Down
2 changes: 1 addition & 1 deletion tests/hwloc/hwloc_type_sscanf.c
Expand Up @@ -61,7 +61,7 @@ static void check(hwloc_topology_t topology, hwloc_obj_t obj)
printf(" parsing hwloc_obj_type_snprintf() normal output = %s\n", buffer);
_check(topology, obj, buffer, 1);

err = hwloc_obj_type_snprintf(buffer, sizeof(buffer), obj, HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
err = hwloc_obj_type_snprintf(buffer, sizeof(buffer), obj, HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);
assert(err > 0);
printf(" parsing hwloc_obj_type_snprintf() verbose output = %s\n", buffer);
_check(topology, obj, buffer, 1);
Expand Down
6 changes: 3 additions & 3 deletions utils/hwloc/hwloc-calc.c
Expand Up @@ -122,7 +122,7 @@ hwloc_calc_hierarch_output(hwloc_topology_t topology, const char *prefix, const
unsigned idx = logicalo ? logi : obj->os_index;
if (!hwloc_bitmap_intersects(set, obj->cpuset))
goto next;
hwloc_obj_type_snprintf(type, sizeof(type), obj, HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_type_snprintf(type, sizeof(type), obj, HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);
if (idx == (unsigned)-1)
snprintf(string, sizeof(string), "%s%s%s:-1", prefix, level ? "." : "", type);
else
Expand Down Expand Up @@ -176,7 +176,7 @@ hwloc_calc_output(hwloc_topology_t topology, const char *sep, hwloc_bitmap_t set
fprintf(stderr, "No object included in this cpuset\n");
return EXIT_FAILURE;
}
hwloc_obj_type_snprintf(type, sizeof(type), obj, HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_type_snprintf(type, sizeof(type), obj, HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);
idx = logicalo ? obj->logical_index : obj->os_index;
if (idx == (unsigned) -1)
printf("%s%s", first ? (const char *) "" : sep, type);
Expand Down Expand Up @@ -254,7 +254,7 @@ hwloc_calc_output(hwloc_topology_t topology, const char *sep, hwloc_bitmap_t set
for(i=0; i<nrnodes; i++) {
char type[64];
unsigned idx;
hwloc_obj_type_snprintf(type, sizeof(type), nodes[i], HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_type_snprintf(type, sizeof(type), nodes[i], HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);
idx = logicalo ? nodes[i]->logical_index : nodes[i]->os_index;
printf("%s%u", i==0 ? (const char *) "" : sep, idx);
}
Expand Down
16 changes: 8 additions & 8 deletions utils/hwloc/hwloc-info.c
Expand Up @@ -279,7 +279,7 @@ hwloc_info_show_obj(hwloc_topology_t topology, hwloc_obj_t obj, const char *type
hwloc_bitmap_asprintf(&inits, initiators[j].location.cpuset);
} else if (initiators[j].type == HWLOC_LOCATION_TYPE_OBJECT) {
char types[64];
hwloc_obj_type_snprintf(types, sizeof(types), initiators[j].location.object, HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_type_snprintf(types, sizeof(types), initiators[j].location.object, HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);
if (initiators[j].location.object->os_index != (unsigned)-1)
snprintf(_inits, sizeof(_inits), "%s L#%u P#%u", types, initiators[j].location.object->logical_index, initiators[j].location.object->os_index);
else
Expand Down Expand Up @@ -317,7 +317,7 @@ hwloc_calc_process_location_info_cb(struct hwloc_calc_location_context_s *lconte
if (show_index_prefix)
snprintf(prefix, sizeof(prefix), "%u: ", current_obj);

hwloc_obj_type_snprintf(objs, sizeof(objs), obj, HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_type_snprintf(objs, sizeof(objs), obj, HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);

if (show_ancestors) {
char parents[128];
Expand All @@ -326,7 +326,7 @@ hwloc_calc_process_location_info_cb(struct hwloc_calc_location_context_s *lconte
while (parent) {
if (show_index_prefix)
snprintf(prefix, sizeof(prefix), "%u.%u: ", current_obj, level);
hwloc_obj_type_snprintf(parents, sizeof(parents), parent, HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_type_snprintf(parents, sizeof(parents), parent, HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);
if (verbose < 0)
printf("%s%s:%u\n", prefix, parents, parent->logical_index);
else if (level)
Expand All @@ -343,7 +343,7 @@ hwloc_calc_process_location_info_cb(struct hwloc_calc_location_context_s *lconte
hwloc_obj_t parent = obj;
while (parent) {
if (parent->depth == show_ancestor_depth) {
hwloc_obj_type_snprintf(parents, sizeof(parents), parent, HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_type_snprintf(parents, sizeof(parents), parent, HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);
if (verbose < 0)
printf("%s%s:%u\n", prefix, parents, parent->logical_index);
else
Expand All @@ -361,7 +361,7 @@ hwloc_calc_process_location_info_cb(struct hwloc_calc_location_context_s *lconte
char childs[128];
if (show_index_prefix)
snprintf(prefix, sizeof(prefix), "%u.%u: ", current_obj, i);
hwloc_obj_type_snprintf(childs, sizeof(childs), child, HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_type_snprintf(childs, sizeof(childs), child, HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);
if (verbose < 0)
printf("%s%s:%u\n", prefix, childs, child->logical_index);
else
Expand All @@ -380,7 +380,7 @@ hwloc_calc_process_location_info_cb(struct hwloc_calc_location_context_s *lconte
char childs[128];
if (show_index_prefix)
snprintf(prefix, sizeof(prefix), "%u.%u: ", current_obj, i);
hwloc_obj_type_snprintf(childs, sizeof(childs), child, HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_type_snprintf(childs, sizeof(childs), child, HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);
if (verbose < 0)
printf("%s%s:%u\n", prefix, childs, child->logical_index);
else
Expand Down Expand Up @@ -411,7 +411,7 @@ hwloc_calc_process_location_info_cb(struct hwloc_calc_location_context_s *lconte
}
if (show_index_prefix)
snprintf(prefix, sizeof(prefix), "%u.%u: ", current_obj, i);
hwloc_obj_type_snprintf(childs, sizeof(childs), child, HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_type_snprintf(childs, sizeof(childs), child, HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);
if (verbose < 0)
printf("%s%s:%u\n", prefix, childs, child->logical_index);
else
Expand Down Expand Up @@ -461,7 +461,7 @@ hwloc_calc_process_location_info_cb(struct hwloc_calc_location_context_s *lconte
continue;
if (show_index_prefix)
snprintf(prefix, sizeof(prefix), "%u.%u: ", current_obj, i);
hwloc_obj_type_snprintf(nodestr, sizeof(nodestr), nodes[i], HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_type_snprintf(nodestr, sizeof(nodestr), nodes[i], HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);
if (verbose < 0)
printf("%s%s:%u\n", prefix, nodestr, nodes[i]->logical_index);
else
Expand Down
4 changes: 2 additions & 2 deletions utils/hwloc/hwloc-ps.c
Expand Up @@ -93,7 +93,7 @@ static void print_task(hwloc_topology_t topology,
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, HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_type_snprintf(type, sizeof(type), obj, HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);
idx = logical ? obj->logical_index : obj->os_index;
if (idx == (unsigned) -1)
printf("%s", type);
Expand All @@ -105,7 +105,7 @@ static void print_task(hwloc_topology_t topology,
/* 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;
hwloc_obj_type_snprintf(type, sizeof(type), obj, HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_type_snprintf(type, sizeof(type), obj, HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);
idx = logical ? obj->logical_index : obj->os_index;
if (idx == (unsigned) -1)
printf("%s%s", first ? "" : " ", type);
Expand Down
2 changes: 1 addition & 1 deletion utils/hwloc/misc.h
Expand Up @@ -500,7 +500,7 @@ hwloc_lstopo_show_summary_depth(FILE *output, size_t prefixmaxlen, hwloc_topolog
types = hwloc_obj_type_string(type);
} else {
/* use verbose type name, those are identical for all objects on normal levels */
hwloc_obj_type_snprintf(_types, sizeof(_types), hwloc_get_obj_by_depth(topology, depth, 0), HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_type_snprintf(_types, sizeof(_types), hwloc_get_obj_by_depth(topology, depth, 0), HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES);
types = _types;
}

Expand Down
2 changes: 1 addition & 1 deletion utils/hwloc/test-parsing-flags.sh.in
Expand Up @@ -11,7 +11,7 @@ include="$HWLOC_top_srcdir/include"
misch="$HWLOC_top_srcdir/utils/hwloc/misc.h"

flags_def=`grep -h _FLAG_ ${include}/hwloc.h ${include}/hwloc/*.h | grep '<<' | grep -v HWLOC_DISTRIB_FLAG \
| grep -v HWLOC_DISC_STATUS_FLAG | grep -v HWLOC_TOPOLOGY_COMPONENTS_FLAG | grep -v HWLOC_OBJ_SNPRINTF_FLAG | cut -d= -f1`
| grep -v HWLOC_DISC_STATUS_FLAG | grep -v HWLOC_TOPOLOGY_COMPONENTS_FLAG | cut -d= -f1`

IFS=' ' flags=${flags_def}
for flag in $flags
Expand Down
2 changes: 1 addition & 1 deletion utils/lstopo/lstopo-android.c
Expand Up @@ -32,7 +32,7 @@ static void native_android_box(struct lstopo_output *loutput, const struct lstop

if(obj){
gp_index = obj->gp_index;
hwloc_obj_attr_snprintf(info, 1096, obj, sep, HWLOC_OBJ_SNPRINTF_FLAG_OLD_VERBOSE);
hwloc_obj_attr_snprintf(info, 1096, obj, sep, HWLOC_OBJ_SNPRINTF_FLAG_LONG_NAMES|HWLOC_OBJ_SNPRINTF_FLAG_MORE_ATTRS);
}

if (cpukind_style)
Expand Down

0 comments on commit aeee9e3

Please sign in to comment.