[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240919215107.2263552-4-stefan.ene@intel.com>
Date: Thu, 19 Sep 2024 14:51:03 -0700
From: Stefan@....codeaurora.org, Ene@....codeaurora.org
To: peterz@...radead.org,
mingo@...hat.com,
acme@...nel.org,
namhyung@...nel.org,
mark.rutland@....com,
alexander.shishkin@...ux.intel.com,
jolsa@...nel.org,
irogers@...gle.com,
adrian.hunter@...el.com,
kan.liang@...ux.intel.com,
linux-perf-users@...r.kernel.org,
linux-kernel@...r.kernel.org
Cc: vinicius.gomes@...el.com,
stefan.ene@...el.com,
stef_an_ene@...look.com
Subject: [RFC v1 2/3] C and Rust support for perf script
From: Stefan Ene <stefan.ene@...el.com>
[PATCH 2/3] added the C sample script
---
tools/perf/new_script_templates/README | 16 ++++
tools/perf/new_script_templates/script.c | 107 +++++++++++++++++++++++
2 files changed, 123 insertions(+)
create mode 100644 tools/perf/new_script_templates/README
create mode 100644 tools/perf/new_script_templates/script.c
diff --git a/tools/perf/new_script_templates/README b/tools/perf/new_script_templates/README
new file mode 100644
index 000000000000..c2a55a65d444
--- /dev/null
+++ b/tools/perf/new_script_templates/README
@@ -0,0 +1,16 @@
+Linux kernel additions for C and Rust script support inside the perf-script tool set.
+
+Steps to use new feature:
+
+ First, use the provided update_perf_tools.sh script to make sure your perf toolset is up to date with the latest implementation:
+ $ bash update_perf_tools.sh
+
+ a) For C scripts:
+ 1. Use the default C template test.c to write your own custom perf event processing
+
+ 2. Compile the C script into a dynamic library using the following two commands:
+ $ gcc -c -I ~/include -fpic new_script_templates/script.c
+ $ gcc -shared -o test.so new_script_templates/script.o
+
+ 3. Call the new perf script option to use the newly created .so file using the command:
+ $ sudo perf script --new_script new_script_templates/script.so
\ No newline at end of file
diff --git a/tools/perf/new_script_templates/script.c b/tools/perf/new_script_templates/script.c
new file mode 100644
index 000000000000..e4942ba689db
--- /dev/null
+++ b/tools/perf/new_script_templates/script.c
@@ -0,0 +1,107 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+// =================== Needed stuff begin, DO NOT CHANGE ===================
+
+#include <linux/types.h>
+
+struct new_script {
+ char *file;
+ void *handle;
+
+ void (*begin)(void *data, void *ctx);
+ void (*end)(void *data, void *ctx);
+
+ int (*process_file_header)(void *data, void *ctx);
+ int (*process_event_header)(void *data, void *ctx);
+ int (*process_event_raw_data)(void *data, const int size, void *ctx);
+
+};
+
+struct processed_file_header {
+ __u64 size;
+ __u64 data_size;
+ __u64 data_offset;
+};
+
+struct processed_event_header {
+ __u32 type;
+ __u16 misc;
+ __u16 size;
+};
+
+// =================== Editable funtions begin ===================
+
+void
+print_begin(void *data, void *ctx)
+{
+ printf(">> in trace_begin\n");
+}
+
+
+int
+process_file_header(void* data, void *ctx)
+{
+ if (!data) {
+ printf("> Error dynamically processing file header\n");
+ return -1;
+ }
+
+ struct processed_file_header *fh = (struct processed_file_header *)data;
+
+ printf("\nFile header: size=%lx, data.size=%u, data.offset=%u\n", fh->size, fh->data_size, fh->data_offset);
+
+ return 0;
+}
+
+
+int
+process_event_header(void* data, void *ctx)
+{
+ if (!data) {
+ printf("> Error dynamically processing event header\n");
+ return -1;
+ }
+
+ struct processed_event_header *evh = (struct processed_event_header *)data;
+
+ printf("\nEvent header: size=%u, type=%u, misc=%u\n", evh->size, evh->type, evh->misc);
+
+ return 0;
+}
+
+
+int
+process_event_raw_data(void* data, const int size, void *ctx)
+{
+ unsigned char *byte_data = (unsigned char *)data;
+ for (size_t i = 0; i < size; i++) {
+ // if (i >= 160) {
+ // printf("\n...");
+ // break;
+ // }
+ if ((i % 16) == 0)
+ printf("\n");
+ printf("%02x ", byte_data[i]);
+ }
+ printf("\n");
+}
+
+
+void
+print_end(void *data, void *ctx)
+{
+ printf("\n>> in trace_end\n");
+}
+
+// =================== Needed stuff begin, DO NOT CHANGE ===================
+
+void
+initialize_new_script(struct new_script* s)
+{
+ s->begin = print_begin;
+ s->end = print_end;
+ s->process_file_header = process_file_header;
+ s->process_event_header = process_event_header;
+ s->process_event_raw_data = process_event_raw_data;
+}
\ No newline at end of file
--
2.46.0
Powered by blists - more mailing lists