Skip to contentSkip to conversation

perf hist: Fix width calculation in hpp__fmt()

PATCH Applied Dmitry Vyukov 3 participants 4 messages Jan 8, 2025 – Jan 17, 2025 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf hist: Fix width calculation in hpp__fmt()

hpp__width_fn() round up width to length of the field name, hpp__fmt() should do it too. Otherwise, the numbers may end up unaligned if the field name is long.

---
 tools/perf/ui/hist.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/ui/hist.c +1 -1
index e5491995adf08..34fda1d5eccb4 100644
@@ -121,7 +121,7 @@ int hpp__fmt(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
 	     const char *fmtstr, hpp_snprint_fn print_fn,
 	     enum perf_hpp_fmt_type fmtype)
 {
-	int len = fmt->user_len ?: fmt->len;
+	int len = max(fmt->user_len ?: fmt->len, (int)strlen(fmt->name));
 
 	if (symbol_conf.field_sep) {
 		return __hpp__fmt(hpp, he, get_field, fmtstr, 1,

base-commit: 09a0fa92e5b45e99cf435b2fbf5ebcf889cf8780

Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · Dmitry Vyukov

hpp__width_fn() round up width to length of the field name, hpp__fmt() should do it too. Otherwise, the numbers may end up unaligned if the field name is long.

---
  tools/perf/ui/hist.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/ui/hist.c +1 -1
index e5491995adf08..34fda1d5eccb4 100644
@@ -121,7 +121,7 @@ int hpp__fmt(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  	     const char *fmtstr, hpp_snprint_fn print_fn,
  	     enum perf_hpp_fmt_type fmtype)
  {
-	int len = fmt->user_len ?: fmt->len;
+	int len = max(fmt->user_len ?: fmt->len, (int)strlen(fmt->name));
 
  	if (symbol_conf.field_sep) {
  		return __hpp__fmt(hpp, he, get_field, fmtstr, 1,
Quoted 1 line

base-commit: 09a0fa92e5b45e99cf435b2fbf5ebcf889cf8780

This seems to right align the field correctly with a long name. Although it's worth noting I had to add a long name to make it obvious.

I saw some other existing issue with small column-width arguments which is changed slightly by this patch. But I don't think it's worse:

$ perf report --fields overhead,overhead_sys,overhead_us,overhead_guest_us,dso --column-widths=3,3,3,3,3

After:
------
Overhead sys usr guest usr long name Shared Object
63.78% 0.00% 63.78% 0.00% ld-linux-x86-64.so

After, width=3:
--------------
Overhead sys usr guest usr long name Sha
63.78% 0.00% 63.78% 0.00% ld-

Before:
---------------
Overhead sys usr guest usr long name Shared Object
63.78% 0.00% 63.78% 0.00% ld-linux-x86-64.so

Before, width=3:
---------------
Overhead sys usr guest usr long name Sha
63.78% 0.00% 63.78% 0.00% ld-

reviewer
#2 lore raw
Quoted 30 lines · James Clark
Quoted 27 lines · Dmitry Vyukov

hpp__width_fn() round up width to length of the field name, hpp__fmt() should do it too. Otherwise, the numbers may end up unaligned if the field name is long.

---
  tools/perf/ui/hist.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/ui/hist.c +1 -1
index e5491995adf08..34fda1d5eccb4 100644
@@ -121,7 +121,7 @@ int hpp__fmt(struct perf_hpp_fmt *fmt, struct 
 perf_hpp *hpp,
            const char *fmtstr, hpp_snprint_fn print_fn,
            enum perf_hpp_fmt_type fmtype)
   {
-    int len = fmt->user_len ?: fmt->len;
+    int len = max(fmt->user_len ?: fmt->len, (int)strlen(fmt->name));
       if (symbol_conf.field_sep) {
           return __hpp__fmt(hpp, he, get_field, fmtstr, 1,
Quoted 8 lines
Quoted 1 line

base-commit: 09a0fa92e5b45e99cf435b2fbf5ebcf889cf8780

This seems to right align the field correctly with a long name. Although it's worth noting I had to add a long name to make it obvious.

I saw some other existing issue with small column-width arguments which is changed slightly by this patch. But I don't think it's worse:

Ah maybe I was looking at it wrong, before and after are the same with width=3, apart from the intended fix. False alarm.

Quoted 22 lines
$ perf report --fields overhead,overhead_sys,overhead_us,overhead_guest_us,dso --column- widths=3,3,3,3,3

After:
------
Overhead       sys       usr  guest usr long name  Shared Object
  63.78%     0.00%    63.78%                0.00%  ld-linux-x86-64.so

After, width=3:
--------------
Overhead       sys       usr  guest usr long name  Sha
  63.78%   0.00%     63.78%                 0.00%  ld-

Before:
---------------
Overhead       sys       usr  guest usr long name  Shared Object
  63.78%     0.00%    63.78%      0.00%            ld-linux-x86-64.so

Before, width=3:
---------------
Overhead       sys       usr  guest usr long name  Sha
 63.78%    0.00%     63.78%    0.00%               ld-

maintainer Applied
#3 lore raw
In reply to Dmitry Vyukov
Quoted 5 lines · Dmitry Vyukov

hpp__width_fn() round up width to length of the field name, hpp__fmt() should do it too. Otherwise, the numbers may end up unaligned if the field name is long.

Applied to perf-tools-next, thanks!

Best regards,
Namhyung