Skip to content

Commit 37a058e

Browse files
Robert Richteracmel
authored andcommitted
perf script: Add generic perl handler to process events
The current perf scripting facility only supports tracepoints. This patch implements a generic perl handler to support other events than tracepoints too. This patch introduces a function process_event() that is called by perf for each sample. The function is called with byte streams as arguments containing information about the event, its attributes, the sample and raw data. Perl's unpack() function can easily be used for byte decoding. The following is the default implementation for process_event() that can also be generated with perf script: # Packed byte string args of process_event(): # # $event: union perf_event util/event.h # $attr: struct perf_event_attr linux/perf_event.h # $sample: struct perf_sample util/event.h # $raw_data: perf_sample->raw_data util/event.h sub process_event { my ($event, $attr, $sample, $raw_data) = @_; my @event = unpack("LSS", $event); my @attr = unpack("LLQQQQQLLQQ", $attr); my @sample = unpack("QLLQQQQQLL", $sample); my @raw_data = unpack("C*", $raw_data); use Data::Dumper; print Dumper \@event, \@attr, \@sample, \@raw_data; } Cc: Ingo Molnar <mingo@elte.hu> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stephane Eranian <eranian@google.com> Link: http://lkml.kernel.org/r/1323969824-9711-4-git-send-email-robert.richter@amd.com Signed-off-by: Robert Richter <robert.richter@amd.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent b1e5a9b commit 37a058e

1 file changed

Lines changed: 67 additions & 6 deletions

File tree

‎tools/perf/util/scripting-engines/trace-event-perl.c‎

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "../thread.h"
3131
#include "../event.h"
3232
#include "../trace-event.h"
33+
#include "../evsel.h"
3334

3435
#include <EXTERN.h>
3536
#include <perl.h>
@@ -247,11 +248,11 @@ static inline struct event *find_cache_event(int type)
247248
return event;
248249
}
249250

250-
static void perl_process_event(union perf_event *pevent __unused,
251-
struct perf_sample *sample,
252-
struct perf_evsel *evsel,
253-
struct machine *machine __unused,
254-
struct thread *thread)
251+
static void perl_process_tracepoint(union perf_event *pevent __unused,
252+
struct perf_sample *sample,
253+
struct perf_evsel *evsel,
254+
struct machine *machine __unused,
255+
struct thread *thread)
255256
{
256257
struct format_field *field;
257258
static char handler[256];
@@ -267,6 +268,9 @@ static void perl_process_event(union perf_event *pevent __unused,
267268

268269
dSP;
269270

271+
if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
272+
return;
273+
270274
type = trace_parse_common_type(data);
271275

272276
event = find_cache_event(type);
@@ -334,6 +338,42 @@ static void perl_process_event(union perf_event *pevent __unused,
334338
LEAVE;
335339
}
336340

341+
static void perl_process_event_generic(union perf_event *pevent __unused,
342+
struct perf_sample *sample,
343+
struct perf_evsel *evsel __unused,
344+
struct machine *machine __unused,
345+
struct thread *thread __unused)
346+
{
347+
dSP;
348+
349+
if (!get_cv("process_event", 0))
350+
return;
351+
352+
ENTER;
353+
SAVETMPS;
354+
PUSHMARK(SP);
355+
XPUSHs(sv_2mortal(newSVpvn((const char *)pevent, pevent->header.size)));
356+
XPUSHs(sv_2mortal(newSVpvn((const char *)&evsel->attr, sizeof(evsel->attr))));
357+
XPUSHs(sv_2mortal(newSVpvn((const char *)sample, sizeof(*sample))));
358+
XPUSHs(sv_2mortal(newSVpvn((const char *)sample->raw_data, sample->raw_size)));
359+
PUTBACK;
360+
call_pv("process_event", G_SCALAR);
361+
SPAGAIN;
362+
PUTBACK;
363+
FREETMPS;
364+
LEAVE;
365+
}
366+
367+
static void perl_process_event(union perf_event *pevent,
368+
struct perf_sample *sample,
369+
struct perf_evsel *evsel,
370+
struct machine *machine,
371+
struct thread *thread)
372+
{
373+
perl_process_tracepoint(pevent, sample, evsel, machine, thread);
374+
perl_process_event_generic(pevent, sample, evsel, machine, thread);
375+
}
376+
337377
static void run_start_sub(void)
338378
{
339379
dSP; /* access to Perl stack */
@@ -555,7 +595,28 @@ static int perl_generate_script(const char *outfile)
555595
fprintf(ofp, "sub print_header\n{\n"
556596
"\tmy ($event_name, $cpu, $secs, $nsecs, $pid, $comm) = @_;\n\n"
557597
"\tprintf(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \",\n\t "
558-
"$event_name, $cpu, $secs, $nsecs, $pid, $comm);\n}");
598+
"$event_name, $cpu, $secs, $nsecs, $pid, $comm);\n}\n");
599+
600+
fprintf(ofp,
601+
"\n# Packed byte string args of process_event():\n"
602+
"#\n"
603+
"# $event:\tunion perf_event\tutil/event.h\n"
604+
"# $attr:\tstruct perf_event_attr\tlinux/perf_event.h\n"
605+
"# $sample:\tstruct perf_sample\tutil/event.h\n"
606+
"# $raw_data:\tperf_sample->raw_data\tutil/event.h\n"
607+
"\n"
608+
"sub process_event\n"
609+
"{\n"
610+
"\tmy ($event, $attr, $sample, $raw_data) = @_;\n"
611+
"\n"
612+
"\tmy @event\t= unpack(\"LSS\", $event);\n"
613+
"\tmy @attr\t= unpack(\"LLQQQQQLLQQ\", $attr);\n"
614+
"\tmy @sample\t= unpack(\"QLLQQQQQLL\", $sample);\n"
615+
"\tmy @raw_data\t= unpack(\"C*\", $raw_data);\n"
616+
"\n"
617+
"\tuse Data::Dumper;\n"
618+
"\tprint Dumper \\@event, \\@attr, \\@sample, \\@raw_data;\n"
619+
"}\n");
559620

560621
fclose(ofp);
561622

0 commit comments

Comments
 (0)