perf list libpfm support and other fixes
Ian Rogers
reviewercover perf list libpfm support and other fixes
Fix an asan issue and a a lack of libpfm support with the refactored perf list code. Add some improvements to wordwrap and the escape printing for json to aid this.
Ian Rogers (4):
perf list: Fix asan issue
perf list: Support newlines in wordwrap
perf list: Json escape encoding improvements
perf list: List callback support for libpfm
tools/perf/builtin-list.c | 122 ++++++++++++++++++------------ tools/perf/util/pfm.c | 154 +++++++++++++++++--------------------- tools/perf/util/pfm.h | 6 +- 3 files changed, 145 insertions(+), 137 deletions(-)
Signature · 1 line
2.38.1.431.g37b22c650d-goog
Ian Rogers
reviewerPATCH 1/4 perf list: Fix asan issue
Missed due to the void* being a valid cast.
--- tools/perf/builtin-list.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-list.c +1 -1
@@ -466,7 +466,7 @@ int cmd_list(int argc, const char **argv)pr_warning ("WARNING: hybrid cputype is not supported!\n" ); } } - print_cb.print_start (&ps); + print_cb.print_start (ps);if (argc ==0 ) { default_ps.metrics = true;
Signature · 1 line
2.38.1.431.g37b22c650d-goog
Ian Rogers
reviewerPATCH 2/4 perf list: Support newlines in wordwrap
In reply to Ian RogersRather than a newline starting from column 0, record a newline was seen and then add the newline and space before the next word.
--- tools/perf/builtin-list.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
tools/perf/builtin-list.c +7 -4
@@ -73,17 +73,19 @@ static void wordwrap(const char *s, int start, int max, int corr) {int column = start;int n; +bool saw_newline = false;while (*s) { -int wlen =strcspn (s," \t" ); +int wlen =strcspn (s," \t\n" ); -if (column + wlen >= max && column > start) { +if ((column + wlen >= max && column > start) || saw_newline) {printf ("\n%*s" , start,"" ); column = start + corr; } n =printf ("%s%.*s" , column > start ?" " :"" , wlen, s);if (n <=0 )break ; + saw_newline = s[wlen] =='\n' ; s += wlen; column += n; s =skip_spaces (s);
@@ -145,7 +147,7 @@ static void default_print_event(void *ps, const char *pmu_name, const char *topiwordwrap (desc,8 ,pager_get_columns (),0 );printf ("]\n" ); } - + long_desc = long_desc ?: desc;if (long_desc && print_state->long_desc) {printf ("%*s" ,8 ,"[" );wordwrap (long_desc,8 ,pager_get_columns (),0 );
@@ -153,7 +155,8 @@ static void default_print_event(void *ps, const char *pmu_name, const char *topi }if (print_state->detailed && encoding_desc) { -printf ("%*s%s" ,8 ,"" , encoding_desc); +printf ("%*s" ,8 ,"" ); +wordwrap (encoding_desc,8 ,pager_get_columns (),0 );if (metric_name)printf (" MetricName: %s" , metric_name);if (metric_expr)
Signature · 1 line
2.38.1.431.g37b22c650d-goog
Ian Rogers
reviewerPATCH 3/4 perf list: Json escape encoding improvements
In reply to Ian RogersUse strbuf to make the string under construction's length unlimited. Use the format %s to mean a literal string copy and %S to signify a need to escape the string. Add supported for escaping a newline character.
--- tools/perf/builtin-list.c | 109 +++++++++++++++++++++++--------------- 1 file changed, 67 insertions(+), 42 deletions(-)
tools/perf/builtin-list.c +67 -42
@@ -17,6 +17,7 @@#include "util/metricgroup.h" #include "util/string2.h" #include "util/strlist.h" +#include "util/strbuf.h" #include <subcmd/pager.h> #include <subcmd/parse-options.h> #include <stdarg.h>
@@ -249,45 +250,56 @@ static void json_print_end(void *ps)printf ("%s]\n" , print_state->need_sep ?"\n" :"" ); } -static void fix_escape_printf (const char *fmt, ...) +static void fix_escape_printf (struct strbuf *buf,const char *fmt, ...) {va_list args; -char buf[2048 ]; -size_t buf_pos =0 ;va_start (args, fmt); +strbuf_setlen (buf,0 );for (size_t fmt_pos =0 ; fmt_pos <strlen (fmt); fmt_pos++) {switch (fmt[fmt_pos]) { -case '%' : { - const char *s = va_arg(args, const char*); - +case '%' : fmt_pos++; -assert (fmt[fmt_pos] =='s' ); -for (size_t s_pos =0 ; s_pos <strlen (s); s_pos++) { -switch (s[s_pos]) { -case '\\' : - __fallthrough; -case '\"' : - buf[buf_pos++] ='\\' ; -assert (buf_pos <sizeof (buf)); - __fallthrough; -default : - buf[buf_pos++] = s[s_pos]; -assert (buf_pos <sizeof (buf)); -break ; +switch (fmt[fmt_pos]) { +case 's' : { + const char *s = va_arg(args, const char*); + +strbuf_addstr (buf, s); +break ; + } +case 'S' : { + const char *s = va_arg(args, const char*); + +for (size_t s_pos =0 ; s_pos <strlen (s); s_pos++) { +switch (s[s_pos]) { +case '\n' : +strbuf_addstr (buf,"\\n" ); +break ; +case '\\' : + __fallthrough; +case '\"' : +strbuf_addch (buf,'\\' ); + __fallthrough; +default : +strbuf_addch (buf, s[s_pos]); +break ; + } } +break ; + } +default : +pr_err ("Unexpected format character '%c'\n" , fmt[fmt_pos]); +strbuf_addch (buf,'%' ); +strbuf_addch (buf, fmt[fmt_pos]); }break ; - }default : - buf[buf_pos++] = fmt[fmt_pos]; -assert (buf_pos <sizeof (buf)); +strbuf_addch (buf, fmt[fmt_pos]);break ; } }va_end (args); - buf[buf_pos] ='\0' ; -fputs (buf, stdout); +fputs (buf->buf, stdout); } static void json_print_event(void *ps, const char *pmu_name, const char *topic,
@@ -300,62 +312,71 @@ static void json_print_event(void *ps, const char *pmu_name, const char *topic, {struct json_print_state *print_state = ps;bool need_sep = false; +struct strbuf buf; +strbuf_init (&buf,0 );printf ("%s{\n" , print_state->need_sep ?",\n" :"" ); print_state->need_sep = true;if (pmu_name) { -fix_escape_printf ("\t\"Unit\": \"%s\"" , pmu_name); +fix_escape_printf (&buf,"\t\"Unit\": \"%S\"" , pmu_name); need_sep = true; }if (topic) { -fix_escape_printf ("%s\t\"Topic\": \"%s\"" , need_sep ?",\n" :"" , topic); +fix_escape_printf (&buf,"%s\t\"Topic\": \"%S\"" , need_sep ?",\n" :"" , topic); need_sep = true; }if (event_name) { -fix_escape_printf ("%s\t\"EventName\": \"%s\"" , need_sep ?",\n" :"" , event_name); +fix_escape_printf (&buf,"%s\t\"EventName\": \"%S\"" , need_sep ?",\n" :"" , + event_name); need_sep = true; }if (event_alias &&strlen (event_alias)) { -fix_escape_printf ("%s\t\"EventAlias\": \"%s\"" , need_sep ?",\n" :"" , event_alias); +fix_escape_printf (&buf,"%s\t\"EventAlias\": \"%S\"" , need_sep ?",\n" :"" , + event_alias); need_sep = true; }if (scale_unit &&strlen (scale_unit)) { -fix_escape_printf ("%s\t\"ScaleUnit\": \"%s\"" , need_sep ?",\n" :"" , +fix_escape_printf (&buf,"%s\t\"ScaleUnit\": \"%S\"" , need_sep ?",\n" :"" , scale_unit); need_sep = true; }if (event_type_desc) { -fix_escape_printf ("%s\t\"EventType\": \"%s\"" , need_sep ?",\n" :"" , +fix_escape_printf (&buf,"%s\t\"EventType\": \"%S\"" , need_sep ?",\n" :"" , event_type_desc); need_sep = true; }if (deprecated) { -fix_escape_printf ("%s\t\"Deprecated\": \"%s\"" , need_sep ?",\n" :"" , +fix_escape_printf (&buf,"%s\t\"Deprecated\": \"%S\"" , need_sep ?",\n" :"" , deprecated ?"1" :"0" ); need_sep = true; }if (desc) { -fix_escape_printf ("%s\t\"BriefDescription\": \"%s\"" , need_sep ?",\n" :"" , desc); +fix_escape_printf (&buf,"%s\t\"BriefDescription\": \"%S\"" , need_sep ?",\n" :"" , + desc); need_sep = true; }if (long_desc) { -fix_escape_printf ("%s\t\"PublicDescription\": \"%s\"" , need_sep ?",\n" :"" , +fix_escape_printf (&buf,"%s\t\"PublicDescription\": \"%S\"" , need_sep ?",\n" :"" , long_desc); need_sep = true; }if (encoding_desc) { -fix_escape_printf ("%s\t\"Encoding\": \"%s\"" , need_sep ?",\n" :"" , encoding_desc); +fix_escape_printf (&buf,"%s\t\"Encoding\": \"%S\"" , need_sep ?",\n" :"" , + encoding_desc); need_sep = true; }if (metric_name) { -fix_escape_printf ("%s\t\"MetricName\": \"%s\"" , need_sep ?",\n" :"" , metric_name); +fix_escape_printf (&buf,"%s\t\"MetricName\": \"%S\"" , need_sep ?",\n" :"" , + metric_name); need_sep = true; }if (metric_expr) { -fix_escape_printf ("%s\t\"MetricExpr\": \"%s\"" , need_sep ?",\n" :"" , metric_expr); +fix_escape_printf (&buf,"%s\t\"MetricExpr\": \"%S\"" , need_sep ?",\n" :"" , + metric_expr); need_sep = true; }printf ("%s}" , need_sep ?"\n" :"" ); +strbuf_release (&buf); } static void json_print_metric(void *ps __maybe_unused, const char *group,
@@ -365,35 +386,39 @@ static void json_print_metric(void *ps __maybe_unused, const char *group, {struct json_print_state *print_state = ps;bool need_sep = false; +struct strbuf buf; +strbuf_init (&buf,0 );printf ("%s{\n" , print_state->need_sep ?",\n" :"" ); print_state->need_sep = true;if (group) { -fix_escape_printf ("\t\"MetricGroup\": \"%s\"" , group); +fix_escape_printf (&buf,"\t\"MetricGroup\": \"%S\"" , group); need_sep = true; }if (name) { -fix_escape_printf ("%s\t\"MetricName\": \"%s\"" , need_sep ?",\n" :"" , name); +fix_escape_printf (&buf,"%s\t\"MetricName\": \"%S\"" , need_sep ?",\n" :"" , name); need_sep = true; }if (expr) { -fix_escape_printf ("%s\t\"MetricExpr\": \"%s\"" , need_sep ?",\n" :"" , expr); +fix_escape_printf (&buf,"%s\t\"MetricExpr\": \"%S\"" , need_sep ?",\n" :"" , expr); need_sep = true; }if (unit) { -fix_escape_printf ("%s\t\"ScaleUnit\": \"%s\"" , need_sep ?",\n" :"" , unit); +fix_escape_printf (&buf,"%s\t\"ScaleUnit\": \"%S\"" , need_sep ?",\n" :"" , unit); need_sep = true; }if (desc) { -fix_escape_printf ("%s\t\"BriefDescription\": \"%s\"" , need_sep ?",\n" :"" , desc); +fix_escape_printf (&buf,"%s\t\"BriefDescription\": \"%S\"" , need_sep ?",\n" :"" , + desc); need_sep = true; }if (long_desc) { -fix_escape_printf ("%s\t\"PublicDescription\": \"%s\"" , need_sep ?",\n" :"" , +fix_escape_printf (&buf,"%s\t\"PublicDescription\": \"%S\"" , need_sep ?",\n" :"" , long_desc); need_sep = true; }printf ("%s}" , need_sep ?"\n" :"" ); +strbuf_release (&buf); }int cmd_list (int argc,const char **argv)
Signature · 1 line
2.38.1.431.g37b22c650d-goog
Ian Rogers
reviewerPATCH 4/4 perf list: List callback support for libpfm
In reply to Ian RogersMissed previously, add libpfm support for 'perf list' callbacks and thereby json support.
--- tools/perf/util/pfm.c | 154 ++++++++++++++++++------------------------ tools/perf/util/pfm.h | 6 +- 2 files changed, 70 insertions(+), 90 deletions(-)
tools/perf/util/pfm.c +67 -87
@@ -12,6 +12,7 @@#include "util/parse-events.h" #include "util/pmu.h" #include "util/pfm.h" +#include "util/strbuf.h" #include <string.h> #include <linux/kernel.h>
@@ -130,53 +131,36 @@ static const char *srcs[PFM_ATTR_CTRL_MAX] = { };static void -print_attr_flags (pfm_event_attr_info_t *info) +print_attr_flags (struct strbuf *buf,const pfm_event_attr_info_t *info) { -int n =0 ; +if (info->is_dfl) +strbuf_addf (buf,"[default] " ); -if (info->is_dfl) { -printf ("[default] " ); - n++; - } - -if (info->is_precise) { -printf ("[precise] " ); - n++; - } - -if (!n) -printf ("- " ); +if (info->is_precise) +strbuf_addf (buf,"[precise] " ); }static void -print_libpfm_events_detailed (pfm_event_info_t *info,bool long_desc) +print_libpfm_event (const struct print_callbacks *print_cb,void *print_state, +const pfm_pmu_info_t *pinfo,const pfm_event_info_t *info, +struct strbuf *buf) { -pfm_event_attr_info_t ainfo; -const char *src;int j, ret; +char topic[80 ], name[80 ]; - ainfo.size =sizeof (ainfo); +strbuf_setlen (buf,0 ); +snprintf (topic,sizeof (topic),"pfm %s" , pinfo->name); -printf (" %s\n" , info->name); -printf (" [%s]\n" , info->desc); -if (long_desc) { -if (info->equiv) -printf (" Equiv: %s\n" , info->equiv); +snprintf (name,sizeof (name),"%s::%s" , pinfo->name, info->name); +strbuf_addf (buf,"Code: 0x%" PRIx64"\n" , info->code); -printf (" Code : 0x%" PRIx64"\n" , info->code); - }pfm_for_each_event_attr (j, info) { - ret =pfm_get_event_attr_info (info->idx, j, -PFM_OS_PERF_EVENT_EXT , &ainfo); -if (ret !=PFM_SUCCESS ) -continue ; - -if (ainfo.type ==PFM_ATTR_UMASK ) { -printf (" %s:%s\n" , info->name, ainfo.name); -printf (" [%s]\n" , ainfo.desc); - } +pfm_event_attr_info_t ainfo; +const char *src; -if (!long_desc) + ainfo.size =sizeof (ainfo); + ret =pfm_get_event_attr_info (info->idx, j,PFM_OS_PERF_EVENT_EXT , &ainfo); +if (ret !=PFM_SUCCESS )continue ;if (ainfo.ctrl >=PFM_ATTR_CTRL_MAX )
@@ -184,64 +168,74 @@ print_libpfm_events_detailed(pfm_event_info_t *info, bool long_desc) src = srcs[ainfo.ctrl];switch (ainfo.type) { -case PFM_ATTR_UMASK : -printf (" Umask : 0x%02" PRIx64" : %s: " , - ainfo.code, src); -print_attr_flags (&ainfo); -putchar ('\n' ); +case PFM_ATTR_UMASK :/* Ignore for now */ break ;case PFM_ATTR_MOD_BOOL : -printf (" Modif : %s: [%s] : %s (boolean)\n" , src, - ainfo.name, ainfo.desc); +strbuf_addf (buf," Modif: %s: [%s] : %s (boolean)\n" , src, + ainfo.name, ainfo.desc);break ;case PFM_ATTR_MOD_INTEGER : -printf (" Modif : %s: [%s] : %s (integer)\n" , src, - ainfo.name, ainfo.desc); +strbuf_addf (buf," Modif: %s: [%s] : %s (integer)\n" , src, + ainfo.name, ainfo.desc);break ;case PFM_ATTR_NONE :case PFM_ATTR_RAW_UMASK :case PFM_ATTR_MAX :default : -printf (" Attr : %s: [%s] : %s\n" , src, - ainfo.name, ainfo.desc); +strbuf_addf (buf," Attr: %s: [%s] : %s\n" , src, + ainfo.name, ainfo.desc); } } -} + print_cb->print_event (print_state, + pinfo->name, + topic, + name, info->equiv, +/*scale_unit=*/ NULL , +/*deprecated=*/ NULL ,"PFM event" , + info->desc,/*long_desc=*/ NULL , +/*encoding_desc=*/ buf->buf, +/*metric_name=*/ NULL ,/*metric_expr=*/ NULL ); -/* -* list all pmu::event:umask, pmu::event -* printed events may not be all valid combinations of umask for an event -*/ -static void -print_libpfm_events_raw (pfm_pmu_info_t *pinfo,pfm_event_info_t *info) -{ -pfm_event_attr_info_t ainfo; -int j, ret; -bool has_umask = false; +pfm_for_each_event_attr (j, info) { +pfm_event_attr_info_t ainfo; +const char *src; - ainfo.size =sizeof (ainfo); +strbuf_setlen (buf,0 ); -pfm_for_each_event_attr (j, info) { - ret =pfm_get_event_attr_info (info->idx, j, -PFM_OS_PERF_EVENT_EXT , &ainfo); + ainfo.size =sizeof (ainfo); + ret =pfm_get_event_attr_info (info->idx, j,PFM_OS_PERF_EVENT_EXT , &ainfo);if (ret !=PFM_SUCCESS )continue ; -if (ainfo.type !=PFM_ATTR_UMASK ) -continue ; +if (ainfo.ctrl >=PFM_ATTR_CTRL_MAX ) + ainfo.ctrl =PFM_ATTR_CTRL_UNKNOWN ; -printf ("%s::%s:%s\n" , pinfo->name, info->name, ainfo.name); - has_umask = true; + src = srcs[ainfo.ctrl]; +if (ainfo.type ==PFM_ATTR_UMASK ) { +strbuf_addf (buf,"Umask: 0x%02" PRIx64" : %s: " , + ainfo.code, src); +print_attr_flags (buf, &ainfo); +snprintf (name,sizeof (name),"%s::%s:%s" , + pinfo->name, info->name, ainfo.name); + print_cb->print_event (print_state, + pinfo->name, + topic, + name,/*alias=*/ NULL , +/*scale_unit=*/ NULL , +/*deprecated=*/ NULL ,"PFM event" , + ainfo.desc,/*long_desc=*/ NULL , +/*encoding_desc=*/ buf->buf, +/*metric_name=*/ NULL ,/*metric_expr=*/ NULL ); + } } -if (!has_umask) -printf ("%s::%s\n" , pinfo->name, info->name); } -void print_libpfm_events (bool name_only,bool long_desc) +void print_libpfm_events (const struct print_callbacks *print_cb,void *print_state) {pfm_event_info_t info;pfm_pmu_info_t pinfo; -int i, p, ret; +int p, ret; +struct strbuf storage;libpfm_initialize ();
@@ -249,12 +243,9 @@ void print_libpfm_events(bool name_only, bool long_desc) info.size =sizeof (info); pinfo.size =sizeof (pinfo); -if (!name_only) -puts ("\nList of pre-defined events (to be used in --pfm-events):\n" ); +strbuf_init (&storage,2048 );pfm_for_all_pmus (p) { -bool printed_pmu = false; - ret =pfm_get_pmu_info (p, &pinfo);if (ret !=PFM_SUCCESS )continue ;
@@ -267,25 +258,14 @@ void print_libpfm_events(bool name_only, bool long_desc)if (pinfo.pmu ==PFM_PMU_PERF_EVENT )continue ; -for (i = pinfo.first_event; i !=-1 ; - i =pfm_get_event_next (i)) { - +for (int i = pinfo.first_event; i !=-1 ; i =pfm_get_event_next (i)) { ret =pfm_get_event_info (i,PFM_OS_PERF_EVENT_EXT , &info);if (ret !=PFM_SUCCESS )continue ; -if (!name_only && !printed_pmu) { -printf ("%s:\n" , pinfo.name); - printed_pmu = true; - } - -if (!name_only) -print_libpfm_events_detailed (&info, long_desc); -else -print_libpfm_events_raw (&pinfo, &info); +print_libpfm_event (print_cb, print_state, &pinfo, &info, &storage); } -if (!name_only && printed_pmu) -putchar ('\n' ); } +strbuf_release (&storage); }
tools/perf/util/pfm.h +3 -3
@@ -7,13 +7,14 @@#ifndef __PERF_PFM_H#define __PERF_PFM_H +#include "print-events.h" #include <subcmd/parse-options.h> #ifdef HAVE_LIBPFM int parse_libpfm_events_option (const struct option *opt,const char *str,int unset); -void print_libpfm_events (bool name_only,bool long_desc); +void print_libpfm_events (const struct print_callbacks *print_cb,void *print_state);#else #include <linux/compiler.h>
@@ -26,8 +27,7 @@ static inline int parse_libpfm_events_option(return 0 ; } -static inline void print_libpfm_events (bool name_only__maybe_unused , -bool long_desc__maybe_unused ) +static inline void print_libpfm_events (const struct print_callbacks *print_cb,void *print_state) { }
Signature · 1 line
2.38.1.431.g37b22c650d-goog
Arnaldo Carvalho de Melo
maintainerEm Tue, Nov 15, 2022 at 11:12:55PM -0800, Ian Rogers escreveu:
Quoted 9 lines
Fix an asan issue and a a lack of libpfm support with the refactored perf list code. Add some improvements to wordwrap and the escape printing for json to aid this.
Ian Rogers (4):
perf list: Fix asan issue
perf list: Support newlines in wordwrap
perf list: Json escape encoding improvements
perf list: List callback support for libpfm
Please take a look at my reports, the patch just before the one having fixes in this series has some problems, so please check and fold the fixes for the JSON one.
I thought I hadn't published that part, but realized I did and force pushed without the last two patches, to avoid breaking bisection before it hits upstream.
- Arnaldo