[PATCH 13/19] perf ftrace: add option '-b/--buffer-size' to set per-cpu buffer size

From: Changbin Du
Date: Sun May 10 2020 - 11:09:46 EST


This adds an option '-b/--buffer-size' to allow us set the size of per-cpu
tracing buffer.

Signed-off-by: Changbin Du <changbin.du@xxxxxxxxx>
---
tools/perf/builtin-ftrace.c | 45 ++++++++++++++++++++++++++++---------
1 file changed, 35 insertions(+), 10 deletions(-)

diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
index 8fd95c109fe8..a93fbdac6aa4 100644
--- a/tools/perf/builtin-ftrace.c
+++ b/tools/perf/builtin-ftrace.c
@@ -47,6 +47,7 @@ struct perf_ftrace {
bool long_info;
unsigned tracing_thresh;
bool trace_children;
+ unsigned buffer_size_kb;
};

struct filter_entry {
@@ -187,6 +188,17 @@ static int read_tracing_file_to_stdout(const char *name)
return ret;
}

+static int write_tracing_file_int(const char *name, int value)
+{
+ char buf[16];
+
+ snprintf(buf, sizeof(buf), "%d", value);
+ if (write_tracing_file(name, buf) < 0)
+ return -1;
+
+ return 0;
+}
+
static int reset_tracing_cpu(void);
static void reset_tracing_filters(void);

@@ -360,8 +372,6 @@ static void reset_tracing_filters(void)

static int set_tracing_depth(struct perf_ftrace *ftrace)
{
- char buf[16];
-
if (ftrace->graph_depth == 0)
return 0;

@@ -370,9 +380,7 @@ static int set_tracing_depth(struct perf_ftrace *ftrace)
return -1;
}

- snprintf(buf, sizeof(buf), "%d", ftrace->graph_depth);
-
- if (write_tracing_file("max_graph_depth", buf) < 0)
+ if (write_tracing_file_int("max_graph_depth", ftrace->graph_depth) < 0)
return -1;

return 0;
@@ -419,14 +427,10 @@ static int set_tracing_long_info(struct perf_ftrace *ftrace)

static int set_tracing_thresh(struct perf_ftrace *ftrace)
{
- char buf[16];
-
if (ftrace->tracing_thresh == 0)
return 0;

- snprintf(buf, sizeof(buf), "%d", ftrace->tracing_thresh);
-
- if (write_tracing_file("tracing_thresh", buf) < 0)
+ if (write_tracing_file_int("tracing_thresh", ftrace->tracing_thresh) < 0)
return -1;

return 0;
@@ -454,6 +458,20 @@ static int set_tracing_trace_children(struct perf_ftrace *ftrace)
return 0;
}

+static int set_tracing_buffer_size_kb(struct perf_ftrace *ftrace)
+{
+ int ret;
+
+ if (ftrace->buffer_size_kb == 0)
+ return 0;
+
+ ret = write_tracing_file_int("buffer_size_kb", ftrace->buffer_size_kb);
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
{
char *trace_file;
@@ -557,6 +575,11 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
goto out_reset;
}

+ if (set_tracing_buffer_size_kb(ftrace) < 0) {
+ pr_err("failed to set tracing per-cpu buffer size\n");
+ goto out_reset;
+ }
+
if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
goto out_reset;
@@ -706,6 +729,8 @@ int cmd_ftrace(int argc, const char **argv)
"Only show functions of which the duration is greater than <n>Âs"),
OPT_BOOLEAN(0, "trace-children", &ftrace.trace_children,
"Trace children processes"),
+ OPT_UINTEGER('b', "buffer-size", &ftrace.buffer_size_kb,
+ "size of per cpu buffer in kb"),
OPT_END()
};

--
2.25.1