linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Ene
To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org,
	namhyung@kernel.org, mark.rutland@arm.com,
	alexander.shishkin@linux.intel.com, jolsa@kernel.org,
	irogers@google.com, adrian.hunter@intel.com,
	kan.liang@linux.intel.com, linux-perf-users@vger.kernel.org,
	linux-kernel@vger.kernel.org
Cc: vinicius.gomes@intel.com, stefan.ene@intel.com, stef_an_ene@outlook.com
Subject: [RFC v1 2/3] C and Rust support for perf script
Date: Thu, 19 Sep 2024 14:51:03 -0700	[thread overview]
Message-ID: <20240919215107.2263552-4-stefan.ene@intel.com> (raw)
In-Reply-To: <20240919215107.2263552-2-stefan.ene@intel.com>

From: Stefan Ene <stefan.ene@intel.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


  parent reply	other threads:[~2024-09-19 21:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-19 21:51 [RFC v1 0/3] C and Rust support for perf script Stefan
2024-09-19 21:51 ` [RFC v1 1/3] " Stefan
2024-09-19 21:51 ` Stefan [this message]
2024-09-19 21:51 ` [RFC v1 3/3] " Stefan
2024-09-26  0:03 ` [RFC v1 0/3] " Namhyung Kim

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240919215107.2263552-4-stefan.ene@intel.com \
    --to=peterz@infradead.org \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=stef_an_ene@outlook.com \
    --cc=stefan.ene@intel.com \
    --cc=vinicius.gomes@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).