perf trace: Correct cpumask formatting and add --bitmask-list
v4 cover perf trace: Correct cpumask formatting and add --bitmask-list
This series corrects cpumask formatting inside 'perf trace' and introduces a new command-line option to display these masks as human-readable CPU lists.
Currently, when parsing tracepoints containing 'cpumask_t' fields (which are represented as dynamic non-array fields in libtraceevent), 'perf trace' incorrectly interprets and prints the raw length and offset descriptor as a decimal integer (i.e., "cpumask: 524320") rather than formatting the actual mask data.
The first patch addresses this anomaly by introducing helper functions to correctly parse and extract raw bits from dynamic fields that do not have the TEP_FIELD_IS_ARRAY flag set. Once the raw bytes are resolved, 'perf trace' displays them as a zero-padded hexadecimal string by default.
The second patch introduces a '--bitmask-list' option. When specified, formatting is delegated to bitmap_scnprintf(), allowing the resolved cpumask to be rendered as a condensed, human-readable list of CPUs (e.g., "0,2-5,7").
Changes since v3:
- Added a blank line in the code block for bitmap_byte() to improve readability (Namhyung Kim)
- Refactored the bit iteration loop to use an early break, reducing indentation (Namhyung Kim)
- Linked to v3: https://lore.kernel.org/lkml/[email protected]/
Changes since v2:
- Provided an example output of the newly introduced '--bitmask-list' option (Namhyung Kim)
- Updated the documentation to include the new option (Namhyung Kim)
- Linked to v2: https://lore.kernel.org/lkml/[email protected]/
Changes since v1:
- Refactored cpumask parsing by moving the dynamic bitmask reconstruction logic into a new helper function, format_field__get_cpumask()
- Made bitmask parsing cross-platform, word-size, and endianness-safe
- Added a new helper function, bitmap_byte() to extract bytes from the host-native bitmask in a host-endianness-independent manner, simplifying hexadecimal cpumask formatting
- Cleaned up trace__fprintf_tp_fields() to remove complex inline parsing logic
- Linked to v1: https://lore.kernel.org/lkml/[email protected]/
Aaron Tomlin (2):
perf trace: Correct default cpumask formatting to hexadecimal perf trace: Add --bitmask-list command-line option
tools/perf/Documentation/perf-trace.txt | 4 ++ tools/perf/builtin-trace.c | 76 ++++++++++++++++++--- tools/perf/util/evsel.c | 91 +++++++++++++++++++++++++ tools/perf/util/evsel.h | 6 ++ 4 files changed, 168 insertions(+), 9 deletions(-)
Signature · 1 line
2.54.0
PATCH v4 1/2 perf trace: Correct default cpumask formatting to hexadecimal
Currently, dynamic non-array fields such as 'cpumask_t' are mishandled in 'perf trace', causing the raw length and offset descriptors to be interpreted and displayed as a literal integer (e.g., "cpumask: 524320" instead of the actual mask data).
Correct the parsing of dynamic fields that do not have the TEP_FIELD_IS_ARRAY flag set by introducing helper functions format_field__get_raw_data() and format_field__get_cpumask(). Using these helpers, resolve the pointer to the raw bits within the payload and format the cpumask as a zero-padded hexadecimal string by default.
--- tools/perf/builtin-trace.c | 71 +++++++++++++++++++++++++---- tools/perf/util/evsel.c | 91 ++++++++++++++++++++++++++++++++++++++ tools/perf/util/evsel.h | 6 +++ 3 files changed, 159 insertions(+), 9 deletions(-)
tools/perf/builtin-trace.c +62 -9
@@ -3207,6 +3207,22 @@ static void bpf_output__fprintf(struct trace *trace, ++trace->nr_events_printed; } +static unsigned char bitmap_byte (const unsigned long *mask,int byte_idx) +{ +unsigned char b_val =0 ; +int bit_in_byte; + +for (bit_in_byte =0 ; bit_in_byte <8 ; bit_in_byte++) { +int b_idx = byte_idx *8 + bit_in_byte; +int host_w_idx = b_idx /BITS_PER_LONG ; +int host_bit_in_word = b_idx %BITS_PER_LONG ; + +if (mask[host_w_idx] & (1UL << host_bit_in_word)) + b_val |= (1 << bit_in_byte); + } +return b_val; +} +static size_t trace__fprintf_tp_fields (struct trace *trace,struct perf_sample *sample,struct thread *thread,void *augmented_args,int augmented_args_size) {
@@ -3238,17 +3254,54 @@ static size_t trace__fprintf_tp_fields(struct trace *trace, struct perf_sample * syscall_arg.len =0 ; syscall_arg.fmt = arg;if (field->flags &TEP_FIELD_IS_ARRAY ) { -int offset = field->offset; - -if (field->flags &TEP_FIELD_IS_DYNAMIC ) { - offset =format_field__intval (field, sample, evsel->needs_swap); - syscall_arg.len = offset >>16 ; - offset &=0xffff ; -if (tep_field_is_relative (field->flags)) - offset += field->offset + field->size; +void *ptr =format_field__get_raw_data (field, sample, + evsel->needs_swap, + &syscall_arg.len); + +if (!ptr) { +pr_err ("Problem processing %s field, skipping...\n" , field->name); +continue ; + } + val = (uintptr_t )ptr; + }else if ((field->flags &TEP_FIELD_IS_DYNAMIC ) && +strstr (field->type,"cpumask" )) { +unsigned long *mask =format_field__get_cpumask (field, sample, + evsel->needs_swap, + &syscall_arg.len); + +if (!mask) { +pr_err ("Problem processing %s field, skipping...\n" , field->name); +continue ; } - val = (uintptr_t )(sample->raw_data + offset); + printed +=scnprintf (bf + printed, size - printed,"%s" , printed ?", " :"" ); +if (trace->show_arg_names) + printed +=scnprintf (bf + printed, size - printed,"%s: " , field->name); + +if (syscall_arg.len ==0 ) { + printed +=scnprintf (bf + printed, size - printed,"0" ); + }else { +int i; +bool skip_zero = true; + + printed +=scnprintf (bf + printed, size - printed,"0x" ); +/* Print bytes from most significant to least significant */ +for (i = syscall_arg.len -1 ; i >=0 ; i--) { +unsigned char b_val =bitmap_byte (mask, i); + +if (skip_zero && b_val ==0 && i >0 ) +continue ; + +if (skip_zero) { + printed +=scnprintf (bf + printed, size - printed,"%x" , b_val); + skip_zero = false; + }else { + printed +=scnprintf (bf + printed, size - printed,"%02x" , b_val); + } + } + } +free (mask); +continue ; }else val =format_field__intval (field, sample, evsel->needs_swap);/*
tools/perf/util/evsel.c +91 -0
106 lines not on this page · raw message
tools/perf/util/evsel.h +6 -0
@@ -400,6 +400,12 @@ static inline char *perf_sample__strval(struct perf_sample *sample, const char *struct tep_format_field ; +void *format_field__get_raw_data (struct tep_format_field *field, +struct perf_sample *sample, +bool needs_swap,u16 *len_out); +unsigned long *format_field__get_cpumask (struct tep_format_field *field, +struct perf_sample *sample, +bool needs_swap,u16 *len_out);u64 format_field__intval (struct tep_format_field *field,struct perf_sample *sample,bool needs_swap);#ifdef HAVE_LIBTRACEEVENT
Signature · 1 line
2.54.0
PATCH v4 2/2 perf trace: Add --bitmask-list command-line option
In reply to Aaron TomlinIntroduce a new '--bitmask-list' command-line option for 'perf trace'. When this option is specified, the formatting of cpumasks is delegated to bitmap_scnprintf(), enabling cpumasks to be displayed as a condensed, human-readable list (e.g., "0,2-5,7") instead of the default hexadecimal representation. An example is provided below:
❯ sudo ./perf trace --show-cpu --bitmask-list --event ipi:ipi_send_cpumask --max-event 5
0.000 [000] Xorg/1434 ipi:ipi_send_cpumask(cpumask: 2-3,6, callsite: 0xffffffff9994f8e4, callback: 0xffffffff9994fdd0)
694.527 [002] chrome/2894 ipi:ipi_send_cpumask(cpumask: 1,3-5, callsite: 0xffffffff9994f8e4, callback: 0xffffffff9994fdd0)
2666.608 [003] Chrome_ChildIO/2948 ipi:ipi_send_cpumask(cpumask: 4,7, callsite: 0xffffffff9994f8e4, callback: 0xffffffff9994fdd0) 2673.638 [000] Chrome_IOThrea/2920 ipi:ipi_send_cpumask(cpumask: 2-5, callsite: 0xffffffff9994f8e4, callback: 0xffffffff9994fdd0) 2714.228 [005] chrome/3375 ipi:ipi_send_cpumask(cpumask: 0-4,6-7, callsite: 0xffffffff9994f8e4, callback: 0xffffffff9994fdd0)
--- tools/perf/Documentation/perf-trace.txt | 4 ++++ tools/perf/builtin-trace.c | 5 +++++ 2 files changed, 9 insertions(+)
tools/perf/Documentation/perf-trace.txt +4 -0
@@ -247,6 +247,10 @@ the thread executes on the designated CPUs. Default is to monitor all CPUs. pretty-printing serves as a fallback to hand-crafted pretty printers, as the latter can better pretty-print integer flags and struct pointers. +--bitmask-list:: + Show bitmasks as a human-readable, condensed list (e.g. "0,2-5,7") + instead of the default hexadecimal representation. + --bpf-summary:: Collect system call statistics in BPF. This is only for live mode and works well with -s/--summary option where no argument information is
tools/perf/builtin-trace.c +5 -0
@@ -226,6 +226,7 @@ struct trace {bool force;bool vfs_getname;bool force_btf; +bool bitmask_list;bool summary_bpf;int trace_pgfaults;char *perfconfig_events;
@@ -3280,6 +3281,9 @@ static size_t trace__fprintf_tp_fields(struct trace *trace, struct perf_sample *if (syscall_arg.len ==0 ) { printed +=scnprintf (bf + printed, size - printed,"0" ); + }else if (trace->bitmask_list) { + printed +=bitmap_scnprintf (mask, syscall_arg.len *8 , + bf + printed, size - printed); }else {int i;bool skip_zero = true;
@@ -5590,6 +5594,7 @@ int cmd_trace(int argc, const char **argv) "start"),OPT_BOOLEAN (0 ,"force-btf" , &trace.force_btf,"Prefer btf_dump general pretty printer" "to customized ones" ), +OPT_BOOLEAN (0 ,"bitmask-list" , &trace.bitmask_list,"Show bitmask as a human-readable list" ),OPT_BOOLEAN (0 ,"bpf-summary" , &trace.summary_bpf,"Summary syscall stats in BPF" ),OPT_INTEGER (0 ,"max-summary" , &trace.max_summary, "Max number of entries in the summary."),
Signature · 1 line
2.54.0
Namhyung Kim
maintainerRe: [PATCH v4 2/2] perf trace: Add --bitmask-list command-line option
Quoted 5 lines · Aaron Tomlin
Introduce a new '--bitmask-list' command-line option for 'perf trace'. When this option is specified, the formatting of cpumasks is delegated to
bitmap_scnprintf(), enabling cpumasks to be displayed as a condensed, human-readable list (e.g., "0,2-5,7") instead of the default hexadecimal representation. An example is provided below:
Do you know any other places than cpumask use bitmask too? I'm just curious if it needs to handle other cases or not.
Thanks,
Namhyung
Quoted 62 lines
❯ sudo ./perf trace --show-cpu --bitmask-list --event ipi:ipi_send_cpumask --max-event 5
0.000 [000] Xorg/1434 ipi:ipi_send_cpumask(cpumask: 2-3,6, callsite: 0xffffffff9994f8e4, callback: 0xffffffff9994fdd0)694.527 [002] chrome/2894 ipi:ipi_send_cpumask(cpumask: 1,3-5, callsite:
0xffffffff9994f8e4, callback:0xffffffff9994fdd0)
2666.608 [003] Chrome_ChildIO/2948 ipi:ipi_send_cpumask(cpumask: 4,7, callsite:0xffffffff9994f8e4, callback:0xffffffff9994fdd0) 2673.638 [000] Chrome_IOThrea/2920 ipi:ipi_send_cpumask(cpumask: 2-5, callsite:0xffffffff9994f8e4, callback:0xffffffff9994fdd0) 2714.228 [005] chrome/3375 ipi:ipi_send_cpumask(cpumask: 0-4,6-7, callsite:0xffffffff9994f8e4, callback:0xffffffff9994fdd0)Signed-off-by Aaron Tomlin <[email protected]>--- tools/perf/Documentation/perf-trace.txt | 4 ++++ tools/perf/builtin-trace.c | 5 +++++ 2 files changed, 9 insertions(+)tools/perf/Documentation/perf-trace.txt +4
-0@@ -247,6 +247,10 @@ the thread executes on the designated CPUs. Default is to monitor all CPUs. pretty-printing serves as a fallback to hand-crafted pretty printers, as the latter can better pretty-print integer flags and struct pointers. +--bitmask-list:: + Show bitmasks as a human-readable, condensed list (e.g. "0,2-5,7") + instead of the default hexadecimal representation. + --bpf-summary:: Collect system call statistics in BPF. This is only for live mode and works well with -s/--summary option where no argument information istools/perf/builtin-trace.c +5
-0@@ -226,6 +226,7 @@ struct trace {bool force;bool vfs_getname;bool force_btf; +bool bitmask_list;bool summary_bpf;int trace_pgfaults;char *perfconfig_events;@@ -3280,6 +3281,9 @@ static size_t trace__fprintf_tp_fields(struct trace *trace, struct perf_sample *if (syscall_arg.len ==0 ) { printed +=scnprintf (bf + printed, size - printed,"0" ); + }else if (trace->bitmask_list) { + printed +=bitmap_scnprintf (mask, syscall_arg.len *8 , + bf + printed, size - printed); }else {int i;bool skip_zero = true;@@ -5590,6 +5594,7 @@ int cmd_trace(int argc, const char **argv) "start"),OPT_BOOLEAN (0 ,"force-btf" , &trace.force_btf,"Prefer btf_dump general pretty printer" "to customized ones" ), +OPT_BOOLEAN (0 ,"bitmask-list" , &trace.bitmask_list,"Show bitmask as a human-readable list" ),OPT_BOOLEAN (0 ,"bpf-summary" , &trace.summary_bpf,"Summary syscall stats in BPF" ),OPT_INTEGER (0 ,"max-summary" , &trace.max_summary, "Max number of entries in the summary."),Signature · 1 line
2.54.0
Re: [PATCH v4 2/2] perf trace: Add --bitmask-list command-line option
Quoted 9 lines · Namhyung Kim
Quoted 5 lines · Aaron Tomlin
Introduce a new '--bitmask-list' command-line option for 'perf trace'. When this option is specified, the formatting of cpumasks is delegated to
bitmap_scnprintf(), enabling cpumasks to be displayed as a condensed, human-readable list (e.g., "0,2-5,7") instead of the default hexadecimal representation. An example is provided below:Do you know any other places than cpumask use bitmask too? I'm just curious if it needs to handle other cases or not.
Hi Namhyung,
As far as I can tell, currently no other users.
Kind regards,
Signature · 1 line
Aaron Tomlin
Namhyung Kim
maintainerRe: [PATCH v4 2/2] perf trace: Add --bitmask-list command-line option
Quoted 8 lines · Aaron Tomlin
Quoted 7 lines · Namhyung Kim
Quoted 5 lines · Aaron Tomlin
Introduce a new '--bitmask-list' command-line option for 'perf trace'. When this option is specified, the formatting of cpumasks is delegated to
bitmap_scnprintf(), enabling cpumasks to be displayed as a condensed, human-readable list (e.g., "0,2-5,7") instead of the default hexadecimal representation. An example is provided below:
Quoted 6 lines
Quoted 2 lines
Do you know any other places than cpumask use bitmask too? I'm just curious if it needs to handle other cases or not.
Hi Namhyung,
As far as I can tell, currently no other users.
Ok, I was thinking about --cpumask-list or --cpulist. But I'm fine with the current name as long as we can update other use cases in the future. I'll process this unless others have different opinions.
Thanks,
Namhyung
Re: [PATCH v4 2/2] perf trace: Add --bitmask-list command-line option
Quoted 12 lines · Namhyung Kim
Quoted 11 lines · Aaron Tomlin
Quoted 9 lines · Namhyung Kim
Quoted 5 lines · Aaron Tomlin
Introduce a new '--bitmask-list' command-line option for 'perf trace'. When this option is specified, the formatting of cpumasks is delegated to
bitmap_scnprintf(), enabling cpumasks to be displayed as a condensed, human-readable list (e.g., "0,2-5,7") instead of the default hexadecimal representation. An example is provided below:Do you know any other places than cpumask use bitmask too? I'm just curious if it needs to handle other cases or not.
Quoted 7 lines
Quoted 3 lines
Hi Namhyung,
As far as I can tell, currently no other users.
Ok, I was thinking about --cpumask-list or --cpulist. But I'm fine with the current name as long as we can update other use cases in the future. I'll process this unless others have different opinions.
Hi Namhyung,
Thank you.
I decided to be somewhat analogous to commit 2cddfc2 ("tracing: Add bitmask-list option for human-readable bitmask display") [1].
[1]: https://github.com/torvalds/linux/commit/2cddfc2e8fc78c13b0f5286ea5dd48cdf527ad41
Kind regards,
Signature · 1 line
Aaron Tomlin
Quoted 11 lines · Aaron Tomlin
This series corrects cpumask formatting inside 'perf trace' and introduces a new command-line option to display these masks as human-readable CPU lists.
Currently, when parsing tracepoints containing '
cpumask_t' fields (which are represented as dynamic non-array fields in libtraceevent), 'perf trace' incorrectly interprets and prints the raw length and offset descriptor as a decimal integer (i.e., "cpumask: 524320") rather than formatting the actual mask data.[...]
Applied to perf-tools-next, thanks!
Best regards,
Namhyung