Commit 79bacb6a authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo

perf list: Add output file option

Add an option to write the 'perf list' output to a specific file. This
can avoid issues with debug output being written into the output stream.
Signed-off-by: default avatarIan Rogers <irogers@google.com>
Acked-by: default avatarNamhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Ross Zwisler <zwisler@chromium.org>
Cc: Shirisha G <shirisha@linux.ibm.com>
Link: https://lore.kernel.org/r/20240124043015.1388867-3-irogers@google.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 9d95c6be
...@@ -47,6 +47,10 @@ Print PMU events and metrics limited to the specific PMU name. ...@@ -47,6 +47,10 @@ Print PMU events and metrics limited to the specific PMU name.
--json:: --json::
Output in JSON format. Output in JSON format.
-o::
--output=::
Output file name. By default output is written to stdout.
[[EVENT_MODIFIERS]] [[EVENT_MODIFIERS]]
EVENT MODIFIERS EVENT MODIFIERS
--------------- ---------------
......
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
* functions. * functions.
*/ */
struct print_state { struct print_state {
/** @fp: File to write output to. */
FILE *fp;
/** /**
* @pmu_glob: Optionally restrict PMU and metric matching to PMU or * @pmu_glob: Optionally restrict PMU and metric matching to PMU or
* debugfs subsystem name. * debugfs subsystem name.
...@@ -66,13 +68,15 @@ static void default_print_start(void *ps) ...@@ -66,13 +68,15 @@ static void default_print_start(void *ps)
{ {
struct print_state *print_state = ps; struct print_state *print_state = ps;
if (!print_state->name_only && pager_in_use()) if (!print_state->name_only && pager_in_use()) {
printf("\nList of pre-defined events (to be used in -e or -M):\n\n"); fprintf(print_state->fp,
"\nList of pre-defined events (to be used in -e or -M):\n\n");
}
} }
static void default_print_end(void *print_state __maybe_unused) {} static void default_print_end(void *print_state __maybe_unused) {}
static void wordwrap(const char *s, int start, int max, int corr) static void wordwrap(FILE *fp, const char *s, int start, int max, int corr)
{ {
int column = start; int column = start;
int n; int n;
...@@ -82,10 +86,10 @@ static void wordwrap(const char *s, int start, int max, int corr) ...@@ -82,10 +86,10 @@ static void wordwrap(const char *s, int start, int max, int corr)
int wlen = strcspn(s, " \t\n"); int wlen = strcspn(s, " \t\n");
if ((column + wlen >= max && column > start) || saw_newline) { if ((column + wlen >= max && column > start) || saw_newline) {
printf("\n%*s", start, ""); fprintf(fp, "\n%*s", start, "");
column = start + corr; column = start + corr;
} }
n = printf("%s%.*s", column > start ? " " : "", wlen, s); n = fprintf(fp, "%s%.*s", column > start ? " " : "", wlen, s);
if (n <= 0) if (n <= 0)
break; break;
saw_newline = s[wlen] == '\n'; saw_newline = s[wlen] == '\n';
...@@ -104,6 +108,7 @@ static void default_print_event(void *ps, const char *pmu_name, const char *topi ...@@ -104,6 +108,7 @@ static void default_print_event(void *ps, const char *pmu_name, const char *topi
{ {
struct print_state *print_state = ps; struct print_state *print_state = ps;
int pos; int pos;
FILE *fp = print_state->fp;
if (deprecated && !print_state->deprecated) if (deprecated && !print_state->deprecated)
return; return;
...@@ -119,30 +124,30 @@ static void default_print_event(void *ps, const char *pmu_name, const char *topi ...@@ -119,30 +124,30 @@ static void default_print_event(void *ps, const char *pmu_name, const char *topi
if (print_state->name_only) { if (print_state->name_only) {
if (event_alias && strlen(event_alias)) if (event_alias && strlen(event_alias))
printf("%s ", event_alias); fprintf(fp, "%s ", event_alias);
else else
printf("%s ", event_name); fprintf(fp, "%s ", event_name);
return; return;
} }
if (strcmp(print_state->last_topic, topic ?: "")) { if (strcmp(print_state->last_topic, topic ?: "")) {
if (topic) if (topic)
printf("\n%s:\n", topic); fprintf(fp, "\n%s:\n", topic);
zfree(&print_state->last_topic); zfree(&print_state->last_topic);
print_state->last_topic = strdup(topic ?: ""); print_state->last_topic = strdup(topic ?: "");
} }
if (event_alias && strlen(event_alias)) if (event_alias && strlen(event_alias))
pos = printf(" %s OR %s", event_name, event_alias); pos = fprintf(fp, " %s OR %s", event_name, event_alias);
else else
pos = printf(" %s", event_name); pos = fprintf(fp, " %s", event_name);
if (!topic && event_type_desc) { if (!topic && event_type_desc) {
for (; pos < 53; pos++) for (; pos < 53; pos++)
putchar(' '); fputc(' ', fp);
printf("[%s]\n", event_type_desc); fprintf(fp, "[%s]\n", event_type_desc);
} else } else
putchar('\n'); fputc('\n', fp);
if (desc && print_state->desc) { if (desc && print_state->desc) {
char *desc_with_unit = NULL; char *desc_with_unit = NULL;
...@@ -155,22 +160,22 @@ static void default_print_event(void *ps, const char *pmu_name, const char *topi ...@@ -155,22 +160,22 @@ static void default_print_event(void *ps, const char *pmu_name, const char *topi
? "%s. Unit: %s" : "%s Unit: %s", ? "%s. Unit: %s" : "%s Unit: %s",
desc, pmu_name); desc, pmu_name);
} }
printf("%*s", 8, "["); fprintf(fp, "%*s", 8, "[");
wordwrap(desc_len > 0 ? desc_with_unit : desc, 8, pager_get_columns(), 0); wordwrap(fp, desc_len > 0 ? desc_with_unit : desc, 8, pager_get_columns(), 0);
printf("]\n"); fprintf(fp, "]\n");
free(desc_with_unit); free(desc_with_unit);
} }
long_desc = long_desc ?: desc; long_desc = long_desc ?: desc;
if (long_desc && print_state->long_desc) { if (long_desc && print_state->long_desc) {
printf("%*s", 8, "["); fprintf(fp, "%*s", 8, "[");
wordwrap(long_desc, 8, pager_get_columns(), 0); wordwrap(fp, long_desc, 8, pager_get_columns(), 0);
printf("]\n"); fprintf(fp, "]\n");
} }
if (print_state->detailed && encoding_desc) { if (print_state->detailed && encoding_desc) {
printf("%*s", 8, ""); fprintf(fp, "%*s", 8, "");
wordwrap(encoding_desc, 8, pager_get_columns(), 0); wordwrap(fp, encoding_desc, 8, pager_get_columns(), 0);
putchar('\n'); fputc('\n', fp);
} }
} }
...@@ -184,6 +189,7 @@ static void default_print_metric(void *ps, ...@@ -184,6 +189,7 @@ static void default_print_metric(void *ps,
const char *unit __maybe_unused) const char *unit __maybe_unused)
{ {
struct print_state *print_state = ps; struct print_state *print_state = ps;
FILE *fp = print_state->fp;
if (print_state->event_glob && if (print_state->event_glob &&
(!print_state->metrics || !name || !strglobmatch(name, print_state->event_glob)) && (!print_state->metrics || !name || !strglobmatch(name, print_state->event_glob)) &&
...@@ -192,27 +198,27 @@ static void default_print_metric(void *ps, ...@@ -192,27 +198,27 @@ static void default_print_metric(void *ps,
if (!print_state->name_only && !print_state->last_metricgroups) { if (!print_state->name_only && !print_state->last_metricgroups) {
if (print_state->metricgroups) { if (print_state->metricgroups) {
printf("\nMetric Groups:\n"); fprintf(fp, "\nMetric Groups:\n");
if (!print_state->metrics) if (!print_state->metrics)
putchar('\n'); fputc('\n', fp);
} else { } else {
printf("\nMetrics:\n\n"); fprintf(fp, "\nMetrics:\n\n");
} }
} }
if (!print_state->last_metricgroups || if (!print_state->last_metricgroups ||
strcmp(print_state->last_metricgroups, group ?: "")) { strcmp(print_state->last_metricgroups, group ?: "")) {
if (group && print_state->metricgroups) { if (group && print_state->metricgroups) {
if (print_state->name_only) if (print_state->name_only)
printf("%s ", group); fprintf(fp, "%s ", group);
else if (print_state->metrics) { else if (print_state->metrics) {
const char *gdesc = describe_metricgroup(group); const char *gdesc = describe_metricgroup(group);
if (gdesc) if (gdesc)
printf("\n%s: [%s]\n", group, gdesc); fprintf(fp, "\n%s: [%s]\n", group, gdesc);
else else
printf("\n%s:\n", group); fprintf(fp, "\n%s:\n", group);
} else } else
printf("%s\n", group); fprintf(fp, "%s\n", group);
} }
zfree(&print_state->last_metricgroups); zfree(&print_state->last_metricgroups);
print_state->last_metricgroups = strdup(group ?: ""); print_state->last_metricgroups = strdup(group ?: "");
...@@ -223,53 +229,59 @@ static void default_print_metric(void *ps, ...@@ -223,53 +229,59 @@ static void default_print_metric(void *ps,
if (print_state->name_only) { if (print_state->name_only) {
if (print_state->metrics && if (print_state->metrics &&
!strlist__has_entry(print_state->visited_metrics, name)) { !strlist__has_entry(print_state->visited_metrics, name)) {
printf("%s ", name); fprintf(fp, "%s ", name);
strlist__add(print_state->visited_metrics, name); strlist__add(print_state->visited_metrics, name);
} }
return; return;
} }
printf(" %s\n", name); fprintf(fp, " %s\n", name);
if (desc && print_state->desc) { if (desc && print_state->desc) {
printf("%*s", 8, "["); fprintf(fp, "%*s", 8, "[");
wordwrap(desc, 8, pager_get_columns(), 0); wordwrap(fp, desc, 8, pager_get_columns(), 0);
printf("]\n"); fprintf(fp, "]\n");
} }
if (long_desc && print_state->long_desc) { if (long_desc && print_state->long_desc) {
printf("%*s", 8, "["); fprintf(fp, "%*s", 8, "[");
wordwrap(long_desc, 8, pager_get_columns(), 0); wordwrap(fp, long_desc, 8, pager_get_columns(), 0);
printf("]\n"); fprintf(fp, "]\n");
} }
if (expr && print_state->detailed) { if (expr && print_state->detailed) {
printf("%*s", 8, "["); fprintf(fp, "%*s", 8, "[");
wordwrap(expr, 8, pager_get_columns(), 0); wordwrap(fp, expr, 8, pager_get_columns(), 0);
printf("]\n"); fprintf(fp, "]\n");
} }
if (threshold && print_state->detailed) { if (threshold && print_state->detailed) {
printf("%*s", 8, "["); fprintf(fp, "%*s", 8, "[");
wordwrap(threshold, 8, pager_get_columns(), 0); wordwrap(fp, threshold, 8, pager_get_columns(), 0);
printf("]\n"); fprintf(fp, "]\n");
} }
} }
struct json_print_state { struct json_print_state {
/** @fp: File to write output to. */
FILE *fp;
/** Should a separator be printed prior to the next item? */ /** Should a separator be printed prior to the next item? */
bool need_sep; bool need_sep;
}; };
static void json_print_start(void *print_state __maybe_unused) static void json_print_start(void *ps)
{ {
printf("[\n"); struct json_print_state *print_state = ps;
FILE *fp = print_state->fp;
fprintf(fp, "[\n");
} }
static void json_print_end(void *ps) static void json_print_end(void *ps)
{ {
struct json_print_state *print_state = ps; struct json_print_state *print_state = ps;
FILE *fp = print_state->fp;
printf("%s]\n", print_state->need_sep ? "\n" : ""); fprintf(fp, "%s]\n", print_state->need_sep ? "\n" : "");
} }
static void fix_escape_printf(struct strbuf *buf, const char *fmt, ...) static void fix_escape_fprintf(FILE *fp, struct strbuf *buf, const char *fmt, ...)
{ {
va_list args; va_list args;
...@@ -318,7 +330,7 @@ static void fix_escape_printf(struct strbuf *buf, const char *fmt, ...) ...@@ -318,7 +330,7 @@ static void fix_escape_printf(struct strbuf *buf, const char *fmt, ...)
} }
} }
va_end(args); va_end(args);
fputs(buf->buf, stdout); fputs(buf->buf, fp);
} }
static void json_print_event(void *ps, const char *pmu_name, const char *topic, static void json_print_event(void *ps, const char *pmu_name, const char *topic,
...@@ -330,60 +342,71 @@ static void json_print_event(void *ps, const char *pmu_name, const char *topic, ...@@ -330,60 +342,71 @@ static void json_print_event(void *ps, const char *pmu_name, const char *topic,
{ {
struct json_print_state *print_state = ps; struct json_print_state *print_state = ps;
bool need_sep = false; bool need_sep = false;
FILE *fp = print_state->fp;
struct strbuf buf; struct strbuf buf;
strbuf_init(&buf, 0); strbuf_init(&buf, 0);
printf("%s{\n", print_state->need_sep ? ",\n" : ""); fprintf(fp, "%s{\n", print_state->need_sep ? ",\n" : "");
print_state->need_sep = true; print_state->need_sep = true;
if (pmu_name) { if (pmu_name) {
fix_escape_printf(&buf, "\t\"Unit\": \"%S\"", pmu_name); fix_escape_fprintf(fp, &buf, "\t\"Unit\": \"%S\"", pmu_name);
need_sep = true; need_sep = true;
} }
if (topic) { if (topic) {
fix_escape_printf(&buf, "%s\t\"Topic\": \"%S\"", need_sep ? ",\n" : "", topic); fix_escape_fprintf(fp, &buf, "%s\t\"Topic\": \"%S\"",
need_sep ? ",\n" : "",
topic);
need_sep = true; need_sep = true;
} }
if (event_name) { if (event_name) {
fix_escape_printf(&buf, "%s\t\"EventName\": \"%S\"", need_sep ? ",\n" : "", fix_escape_fprintf(fp, &buf, "%s\t\"EventName\": \"%S\"",
event_name); need_sep ? ",\n" : "",
event_name);
need_sep = true; need_sep = true;
} }
if (event_alias && strlen(event_alias)) { if (event_alias && strlen(event_alias)) {
fix_escape_printf(&buf, "%s\t\"EventAlias\": \"%S\"", need_sep ? ",\n" : "", fix_escape_fprintf(fp, &buf, "%s\t\"EventAlias\": \"%S\"",
event_alias); need_sep ? ",\n" : "",
event_alias);
need_sep = true; need_sep = true;
} }
if (scale_unit && strlen(scale_unit)) { if (scale_unit && strlen(scale_unit)) {
fix_escape_printf(&buf, "%s\t\"ScaleUnit\": \"%S\"", need_sep ? ",\n" : "", fix_escape_fprintf(fp, &buf, "%s\t\"ScaleUnit\": \"%S\"",
scale_unit); need_sep ? ",\n" : "",
scale_unit);
need_sep = true; need_sep = true;
} }
if (event_type_desc) { if (event_type_desc) {
fix_escape_printf(&buf, "%s\t\"EventType\": \"%S\"", need_sep ? ",\n" : "", fix_escape_fprintf(fp, &buf, "%s\t\"EventType\": \"%S\"",
event_type_desc); need_sep ? ",\n" : "",
event_type_desc);
need_sep = true; need_sep = true;
} }
if (deprecated) { if (deprecated) {
fix_escape_printf(&buf, "%s\t\"Deprecated\": \"%S\"", need_sep ? ",\n" : "", fix_escape_fprintf(fp, &buf, "%s\t\"Deprecated\": \"%S\"",
deprecated ? "1" : "0"); need_sep ? ",\n" : "",
deprecated ? "1" : "0");
need_sep = true; need_sep = true;
} }
if (desc) { if (desc) {
fix_escape_printf(&buf, "%s\t\"BriefDescription\": \"%S\"", need_sep ? ",\n" : "", fix_escape_fprintf(fp, &buf, "%s\t\"BriefDescription\": \"%S\"",
desc); need_sep ? ",\n" : "",
desc);
need_sep = true; need_sep = true;
} }
if (long_desc) { if (long_desc) {
fix_escape_printf(&buf, "%s\t\"PublicDescription\": \"%S\"", need_sep ? ",\n" : "", fix_escape_fprintf(fp, &buf, "%s\t\"PublicDescription\": \"%S\"",
long_desc); need_sep ? ",\n" : "",
long_desc);
need_sep = true; need_sep = true;
} }
if (encoding_desc) { if (encoding_desc) {
fix_escape_printf(&buf, "%s\t\"Encoding\": \"%S\"", need_sep ? ",\n" : "", fix_escape_fprintf(fp, &buf, "%s\t\"Encoding\": \"%S\"",
encoding_desc); need_sep ? ",\n" : "",
encoding_desc);
need_sep = true; need_sep = true;
} }
printf("%s}", need_sep ? "\n" : ""); fprintf(fp, "%s}", need_sep ? "\n" : "");
strbuf_release(&buf); strbuf_release(&buf);
} }
...@@ -394,43 +417,53 @@ static void json_print_metric(void *ps __maybe_unused, const char *group, ...@@ -394,43 +417,53 @@ static void json_print_metric(void *ps __maybe_unused, const char *group,
{ {
struct json_print_state *print_state = ps; struct json_print_state *print_state = ps;
bool need_sep = false; bool need_sep = false;
FILE *fp = print_state->fp;
struct strbuf buf; struct strbuf buf;
strbuf_init(&buf, 0); strbuf_init(&buf, 0);
printf("%s{\n", print_state->need_sep ? ",\n" : ""); fprintf(fp, "%s{\n", print_state->need_sep ? ",\n" : "");
print_state->need_sep = true; print_state->need_sep = true;
if (group) { if (group) {
fix_escape_printf(&buf, "\t\"MetricGroup\": \"%S\"", group); fix_escape_fprintf(fp, &buf, "\t\"MetricGroup\": \"%S\"", group);
need_sep = true; need_sep = true;
} }
if (name) { if (name) {
fix_escape_printf(&buf, "%s\t\"MetricName\": \"%S\"", need_sep ? ",\n" : "", name); fix_escape_fprintf(fp, &buf, "%s\t\"MetricName\": \"%S\"",
need_sep ? ",\n" : "",
name);
need_sep = true; need_sep = true;
} }
if (expr) { if (expr) {
fix_escape_printf(&buf, "%s\t\"MetricExpr\": \"%S\"", need_sep ? ",\n" : "", expr); fix_escape_fprintf(fp, &buf, "%s\t\"MetricExpr\": \"%S\"",
need_sep ? ",\n" : "",
expr);
need_sep = true; need_sep = true;
} }
if (threshold) { if (threshold) {
fix_escape_printf(&buf, "%s\t\"MetricThreshold\": \"%S\"", need_sep ? ",\n" : "", fix_escape_fprintf(fp, &buf, "%s\t\"MetricThreshold\": \"%S\"",
threshold); need_sep ? ",\n" : "",
threshold);
need_sep = true; need_sep = true;
} }
if (unit) { if (unit) {
fix_escape_printf(&buf, "%s\t\"ScaleUnit\": \"%S\"", need_sep ? ",\n" : "", unit); fix_escape_fprintf(fp, &buf, "%s\t\"ScaleUnit\": \"%S\"",
need_sep ? ",\n" : "",
unit);
need_sep = true; need_sep = true;
} }
if (desc) { if (desc) {
fix_escape_printf(&buf, "%s\t\"BriefDescription\": \"%S\"", need_sep ? ",\n" : "", fix_escape_fprintf(fp, &buf, "%s\t\"BriefDescription\": \"%S\"",
desc); need_sep ? ",\n" : "",
desc);
need_sep = true; need_sep = true;
} }
if (long_desc) { if (long_desc) {
fix_escape_printf(&buf, "%s\t\"PublicDescription\": \"%S\"", need_sep ? ",\n" : "", fix_escape_fprintf(fp, &buf, "%s\t\"PublicDescription\": \"%S\"",
long_desc); need_sep ? ",\n" : "",
long_desc);
need_sep = true; need_sep = true;
} }
printf("%s}", need_sep ? "\n" : ""); fprintf(fp, "%s}", need_sep ? "\n" : "");
strbuf_release(&buf); strbuf_release(&buf);
} }
...@@ -449,8 +482,12 @@ static bool default_skip_duplicate_pmus(void *ps) ...@@ -449,8 +482,12 @@ static bool default_skip_duplicate_pmus(void *ps)
int cmd_list(int argc, const char **argv) int cmd_list(int argc, const char **argv)
{ {
int i, ret = 0; int i, ret = 0;
struct print_state default_ps = {}; struct print_state default_ps = {
struct print_state json_ps = {}; .fp = stdout,
};
struct print_state json_ps = {
.fp = stdout,
};
void *ps = &default_ps; void *ps = &default_ps;
struct print_callbacks print_cb = { struct print_callbacks print_cb = {
.print_start = default_print_start, .print_start = default_print_start,
...@@ -461,6 +498,7 @@ int cmd_list(int argc, const char **argv) ...@@ -461,6 +498,7 @@ int cmd_list(int argc, const char **argv)
}; };
const char *cputype = NULL; const char *cputype = NULL;
const char *unit_name = NULL; const char *unit_name = NULL;
const char *output_path = NULL;
bool json = false; bool json = false;
struct option list_options[] = { struct option list_options[] = {
OPT_BOOLEAN(0, "raw-dump", &default_ps.name_only, "Dump raw events"), OPT_BOOLEAN(0, "raw-dump", &default_ps.name_only, "Dump raw events"),
...@@ -471,6 +509,7 @@ int cmd_list(int argc, const char **argv) ...@@ -471,6 +509,7 @@ int cmd_list(int argc, const char **argv)
"Print longer event descriptions."), "Print longer event descriptions."),
OPT_BOOLEAN(0, "details", &default_ps.detailed, OPT_BOOLEAN(0, "details", &default_ps.detailed,
"Print information on the perf event names and expressions used internally by events."), "Print information on the perf event names and expressions used internally by events."),
OPT_STRING('o', "output", &output_path, "file", "output file name"),
OPT_BOOLEAN(0, "deprecated", &default_ps.deprecated, OPT_BOOLEAN(0, "deprecated", &default_ps.deprecated,
"Print deprecated events."), "Print deprecated events."),
OPT_STRING(0, "cputype", &cputype, "cpu type", OPT_STRING(0, "cputype", &cputype, "cpu type",
...@@ -497,6 +536,11 @@ int cmd_list(int argc, const char **argv) ...@@ -497,6 +536,11 @@ int cmd_list(int argc, const char **argv)
argc = parse_options(argc, argv, list_options, list_usage, argc = parse_options(argc, argv, list_options, list_usage,
PARSE_OPT_STOP_AT_NON_OPTION); PARSE_OPT_STOP_AT_NON_OPTION);
if (output_path) {
default_ps.fp = fopen(output_path, "w");
json_ps.fp = default_ps.fp;
}
setup_pager(); setup_pager();
if (!default_ps.name_only) if (!default_ps.name_only)
...@@ -618,5 +662,8 @@ int cmd_list(int argc, const char **argv) ...@@ -618,5 +662,8 @@ int cmd_list(int argc, const char **argv)
free(default_ps.last_topic); free(default_ps.last_topic);
free(default_ps.last_metricgroups); free(default_ps.last_metricgroups);
strlist__delete(default_ps.visited_metrics); strlist__delete(default_ps.visited_metrics);
if (output_path)
fclose(default_ps.fp);
return ret; return ret;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment