linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Adrien BAK <adrien.bak@numscale.com>
To: a.p.zijlstra@chello.nl, paulus@samba.org, mingo@redhat.com,
	acme@kernel.org
Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org
Subject: [PATCH] perf tools : adds support for native scripting
Date: Thu, 22 May 2014 11:53:02 +0900	[thread overview]
Message-ID: <1400727182.19852.0.camel@desktop> (raw)

As it is perf-script allows one to use perl or python scripts to parse 
perf events.
The following proposal aimed to introduce support of .so files as scripts.
This support allows for better performance when parsing perf's data 
files and a complete access to the raw data.

This support is implemented analogously to the one in place for 
perl/python script.
More precisely, when passing a .so file as a script argument, the 
following symbols
are loaded :
	- start_script
	- stop_script
	- process_event
These symbols will be called instead of the corresponding :
	- default_start_script
	- default_stop_script
	- process_event

There currently is a limitation regarding the generate_script function 
which cannot be generalized to native scripts. Is it better to leave
 things as they are (die gracefully when the user asks for 
	native_generate_script) or to do one of the following :
	- print a usage message specifying the symbols a .so script should 
expose
	- create a .c file complying with said interface

Regarding style compliance, the following issues remains :
	- 3 'fprintf' lines slightly over 80 chars in trace-event-native.
	Is it better to introduce a line break than to have a 83-chars 
	long line?
	- in trace-event-scripting.c a struct is declared as extern. Here, 
I followed what was already done for perl/python scripting.


Signed-off-by: Adrien BAK <adrien.bak@numscale.com>

---

diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index 895edd3..1342a2e 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -543,6 +543,10 @@ ifndef NO_LIBPYTHON
   LIB_OBJS += $(OUTPUT)scripts/python/Perf-Trace-Util/Context.o
 endif
 
+ifndef NO_NATIVE_SCRIPTING
+  LIB_OBJS += $(OUTPUT)util/scripting-engines/trace-event-native.o
+endif
+
 ifeq ($(NO_PERF_REGS),0)
   ifeq ($(ARCH),x86)
     LIB_H += arch/x86/include/perf_regs.h
@@ -715,6 +719,9 @@ $(OUTPUT)util/scripting-engines/trace-event-python.o: util/scripting-engines/tra
 $(OUTPUT)scripts/python/Perf-Trace-Util/Context.o: scripts/python/Perf-Trace-Util/Context.c $(OUTPUT)PERF-CFLAGS
 	$(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) $(PYTHON_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-nested-externs $<
 
+$(OUTPUT)util/scripting-engines/trace-event-native.o: util/scripting-engines/trace-event-native.c $(OUTPUT)PERF-CFLAGS
+	$(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-nested-externs $<
+
 $(OUTPUT)perf-%: %.o $(PERFLIBS)
 	$(QUIET_LINK)$(CC) $(CFLAGS) -o $@ $(LDFLAGS) $(filter %.o,$^) $(LIBS)
 
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 9e9c91f..7241e30 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -522,6 +522,7 @@ static void setup_scripting(void)
 {
 	setup_perl_scripting();
 	setup_python_scripting();
+	setup_native_scripting();
 
 	scripting_ops = &default_scripting_ops;
 }
diff --git a/tools/perf/util/scripting-engines/trace-event-native.c b/tools/perf/util/scripting-engines/trace-event-native.c
new file mode 100644
index 0000000..4349bf1
--- /dev/null
+++ b/tools/perf/util/scripting-engines/trace-event-native.c
@@ -0,0 +1,125 @@
+#include <dlfcn.h>
+#include <stdio.h>
+
+#include "../../perf.h"
+#include "../evsel.h"
+#include "../util.h"
+#include "../event.h"
+#include "../thread.h"
+#include "../trace-event.h"
+
+static void *handle;
+
+enum syms {
+START_SCRIPT = 0,
+STOP_SCRIPT,
+PROCESS_EVENT,
+GENERATE_SCRIPT,
+};
+
+const char *sym_names[4] = {"start_script",
+			    "stop_script",
+			    "process_event",
+			    "generate_script"};
+
+int  (*so_start_script)(const char*, int, const char**);
+int  (*so_stop_script)(void);
+void (*so_process_event)(union perf_event *, struct perf_sample *,
+			 struct perf_evsel *, struct thread *,
+			 struct addr_location *);
+
+static void native_process_event(union perf_event *event __maybe_unused,
+				 struct perf_sample *sample,
+				 struct perf_evsel *evsel,
+				 struct thread *thread,
+				 struct addr_location *al)
+{
+	(so_process_event)(event, sample, evsel, thread, al);
+}
+
+static int native_start_script(const char *script, int argc, const char **argv)
+{
+	int err = 0;
+
+	char *local_error;
+
+	handle = dlopen(script, RTLD_NOW);
+
+	if (!handle) {
+		fprintf(stderr, "an error occurred while loading the .so file\n");
+		fprintf(stderr, "%s\n", dlerror());
+		return -1;
+	}
+
+	*(void **)(&so_start_script) = dlsym(handle, sym_names[START_SCRIPT]);
+
+	local_error = dlerror();
+	if (local_error) {
+		fprintf(stderr, "an error occured while loading start_script\n");
+		fprintf(stderr, "%s\n", local_error);
+		goto cleanup;
+	}
+
+	*(void **)(&so_process_event) = dlsym(handle, sym_names[PROCESS_EVENT]);
+
+	local_error = dlerror();
+	if (local_error) {
+		fprintf(stderr, "an error occured while loading process_event\n");
+		fprintf(stderr, "%s\n", local_error);
+		goto cleanup;
+	}
+
+	*(void **)(&so_stop_script) = dlsym(handle, sym_names[STOP_SCRIPT]);
+
+	local_error = dlerror();
+	if (local_error) {
+		fprintf(stderr, "an error occured while loading stop_script\n");
+		fprintf(stderr, "%s\n", local_error);
+		goto cleanup;
+	}
+
+
+	err = (so_start_script)(script, argc, argv);
+	return err;
+
+cleanup:
+	dlclose(handle);
+	return -1;
+
+}
+
+static int native_stop_script(void)
+{
+	int err = 0;
+
+	err = (so_stop_script)();
+
+	if (err)
+		fprintf(stderr,
+			"an error occurred during the execution of stop_script");
+
+	if (handle)
+		err = dlclose(handle);
+
+	if (err) {
+		fprintf(stderr, "an error occurred while closing the .so file");
+		fprintf(stderr, "%s\n", dlerror());
+	}
+
+	return err;
+}
+
+static int native_generate_script(struct pevent *pevent, const char *outfile)
+{
+	fprintf(stderr, "unsupported operation : native script generation");
+
+	return -1;
+}
+
+struct scripting_ops native_scripting_ops = {
+	.name = "Native",
+	.start_script = native_start_script,
+	.stop_script = native_stop_script,
+	.process_event = native_process_event,
+	.generate_script = native_generate_script,
+};
diff --git a/tools/perf/util/trace-event-scripting.c b/tools/perf/util/trace-event-scripting.c
index 57aaccc..c6fa13f 100644
--- a/tools/perf/util/trace-event-scripting.c
+++ b/tools/perf/util/trace-event-scripting.c
@@ -169,3 +169,25 @@ void setup_perl_scripting(void)
 	register_perl_scripting(&perl_scripting_ops);
 }
 #endif
+
+
+
+extern struct scripting_ops native_scripting_ops;
+
+void setup_native_scripting(void)
+{
+	int err;
+	err = script_spec_register("Native", &native_scripting_ops);
+	if (err)
+		die("error registering Native script extension");
+
+	err = script_spec_register("so", &native_scripting_ops);
+	if (err)
+		die("error registering so script extension");
+
+	scripting_context = malloc(sizeof(struct scripting_context));
+}
+
+
+
+
diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h
index 7b6d686..e0b524a 100644
--- a/tools/perf/util/trace-event.h
+++ b/tools/perf/util/trace-event.h
@@ -77,6 +77,7 @@ int script_spec_register(const char *spec, struct scripting_ops *ops);
 
 void setup_perl_scripting(void);
 void setup_python_scripting(void);
+void setup_native_scripting(void);
 
 struct scripting_context {
 	struct pevent *pevent;

             reply	other threads:[~2014-05-22  4:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-22  2:53 Adrien BAK [this message]
2014-05-22 11:24 ` [PATCH] perf tools : adds support for native scripting Andi Kleen
2014-09-06 15:06   ` Milian Wolff

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=1400727182.19852.0.camel@desktop \
    --to=adrien.bak@numscale.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=paulus@samba.org \
    /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).