Use opaque record type in pevent accessor functions
PATCH 1/2 Use opaque record type in pevent accessor functions
Quoted 1 line
From
35c283e97c16525bbebae6f9937a1ccd0f3b8da9Mon Sep 17 00:00:00 2001
From: Darren Hart <[email protected]>
Date: Wed, 16 Dec 2009 15:40:31 -0800
Subject: [PATCH 1/2] Use opaque record type in pevent accessor functions
The caller of the pevent accessor functions:
pevent_data_pid(pevent, data) for example
Already have a struct record. Let them send that directly:
pevent_data_pid(pevent, record)
This decouples the API from the struct implementation and facilitates language bindings which use opaque pointers for passing the C structs around.
--- parse-events.c | 14 +++++++------- parse-events.h | 12 ++++++++++-- trace-cmd.h | 8 -------- 3 files changed, 17 insertions(+), 17 deletions(-)
parse-events.c +7 -7
@@ -3081,13 +3081,13 @@ void pevent_data_lat_fmt(struct pevent *pevent,/** * pevent_data_type - parse out the given event type * @pevent: a handle to the pevent -* @data: the raw data to read from +* @rec: the record to read from * -* This returns the event id from the raw @data. +* This returns the event id from the @rec. */ -int pevent_data_type (struct pevent *pevent,void *data) +int pevent_data_type (struct pevent *pevent,struct record *rec) { -return trace_parse_common_type (pevent, data); +return trace_parse_common_type (pevent, rec->data); }/**
@@ -3105,13 +3105,13 @@ struct event *pevent_data_event_from_type(struct pevent *pevent, int type)/** * pevent_data_pid - parse the PID from raw data * @pevent: a handle to the pevent -* @data: the raw data to parse +* @rec: the record to parse * * This returns the PID from a raw data. */ -int pevent_data_pid (struct pevent *pevent,void *data) +int pevent_data_pid (struct pevent *pevent,struct record *rec) { -return parse_common_pid (pevent, data); +return parse_common_pid (pevent, rec->data); }/**
parse-events.h +10 -2
@@ -14,6 +14,14 @@#define TRACE_SEQ_SIZE 4096#endif +struct record { +unsigned long long ts; +unsigned long long offset; +int record_size;/* size of binary record */ +int size;/* size of data */ +void *data; +}; +/* * Trace sequences are used to allow a function to call several other functions * to create a string of data to use (up to a max of PAGE_SIZE).
@@ -368,9 +376,9 @@ pevent_find_event_by_name(struct pevent *pevent, const char *sys, const char *navoid pevent_data_lat_fmt (struct pevent *pevent, struct trace_seq *s, void *data, int size __unused); -int pevent_data_type (struct pevent *pevent,void *data); +int pevent_data_type (struct pevent *pevent,struct record *rec);struct event *pevent_data_event_from_type (struct pevent *pevent,int type); -int pevent_data_pid (struct pevent *pevent,void *data); +int pevent_data_pid (struct pevent *pevent,struct record *rec);const char *pevent_data_comm_from_pid (struct pevent *pevent,int pid);void pevent_event_info (struct trace_seq *s,struct event *event,int cpu,void *data,int size,unsigned long long nsecs);
trace-cmd.h +0 -8
@@ -28,14 +28,6 @@ enum {#define TS_SHIFT 27#endif -struct record { -unsigned long long ts; -unsigned long long offset; -int record_size;/* size of binary record */ -int size;/* size of data */ -void *data; -}; -static inline void free_record (struct record *record) {free (record);
Signature · 5 lines
1.6.3.3 -- Darren Hart IBM Linux Technology Center Real-Time Linux Team
PATCH 2/2 RFC: Use opaque record type in pevent accessor functions
Quoted 1 line
From
1f76a9521a63e456a950f1a57e69d9dfc36ab001Mon Sep 17 00:00:00 2001
From: Darren Hart <[email protected]>
Date: Wed, 16 Dec 2009 15:25:30 -0800
Subject: [PATCH 2/2] RFC: Start of a tracecmd swig wrapper for python
Introduce a python tracecmd module for use in rapidly prototyping tracing applications. The interface description is provided in tracecmd.i, it identifies which functions are available from within python. A test python script is provided as tracecmd-test.py.
These bindings are expected to change significantly. Eventually I would like to wrap this automated binding with more pythonic objects, most likely including Trace and Event objects which merge the functionality of tracecmd-input, pevent, record, and event structures. This will make development of python apps much more accessible to many application developers.
For now, this is mostly a proof of concept and is no where near complete. It can however open a trace file and read all the events from it, displaying them by CPU in chronological order.
--- swig.sh | 18 ++++++++++++++++++ tracecmd-test.py | 35 +++++++++++++++++++++++++++++++++++ tracecmd.i | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 0 deletions(-) create mode 100755 swig.sh create mode 100755 tracecmd-test.py create mode 100644 tracecmd.i
swig.sh new file +18 -0
@@ -0,0 +1,18 @@ +#!/bin/sh + +# Temporary hack of a build script, eventually we'll incoroporate this +# into the Makefile. You may have to update the includes to point to your +# python installation. + +rm tracecmd_wrap.c tracecmd_wrap.o _tracecmd.so &> /dev/null + +swig -python tracecmd.i + +gcc -fpic -c -I/usr/include/python2.6/ -I/usr/lib/python2.6/config \ + trace-ftrace.c trace-seq.c trace-util.c \ + trace-input.c parse-events.c tracecmd_wrap.c + +gcc -shared trace-ftrace.o trace-seq.o trace-util.o \ + trace-input.o parse-events.o \ + tracecmd_wrap.o -o _tracecmd.so +
tracecmd-test.py new file +35 -0
@@ -0,0 +1,35 @@ +#!/usr/bin/env python + +from tracecmd import * + +# Let's move the following into a new Trace object constructor +filename = "trace.dat" +trace_file = open(filename) +handle = tracecmd_open(trace_file.fileno()) +tracecmd_read_headers(handle) +tracecmd_init_data(handle) + +# These should be members, i.e. Trace.cpus +pe = tracecmd_get_pevent(handle) +cpus = tracecmd_cpus(handle) +print "Trace %s contains data for %d cpus" % (filename, cpus) + +# FIXME: this doesn't print anything... +tracecmd_print_events(handle) + +print "Cycling through the events for each CPU" +for cpu in range(0,cpus): + print "CPU", cpu + rec = tracecmd_read_data(handle, cpu) + while True: + rec = tracecmd_read_data(handle, cpu) + if rec: + # these should be members of a Record object + pid = pevent_data_pid(pe, rec) + comm = pevent_data_comm_from_pid(pe, pid) + type = pevent_data_type(pe, rec) + event = pevent_data_event_from_type(pe, type) + print "\t%f %s: pid=%d comm=%s type=%d" % \ + (record_ts(rec), event_name(event), pid, comm, type) + else: + break
tracecmd.i new file +39 -0
@@ -0,0 +1,39 @@ +// tracecmd.i +%module tracecmd +%{ +/* The following functions provide accessors to the C struct members + * we want to eliminate these if at all possible with changes to the + * tracecmd API. + */ +#include "parse-events.h" +char *event_name(struct event *e) { + return e->name; +} + +/* Python can't handle long long, convert to double in seconds */ +double record_ts(struct record *rec) { + return (double)(rec->ts) / 1000000000; +} +%} + +/* tracecmd functions */ +struct tracecmd_input *tracecmd_open(int fd); +int tracecmd_read_headers(struct tracecmd_input *handle); +int tracecmd_init_data(struct tracecmd_input *handle); +int tracecmd_cpus(struct tracecmd_input *handle); +struct pevent *tracecmd_get_pevent(struct tracecmd_input *handle); +/* FIXME: this didn't print anything */ +void tracecmd_print_events(struct tracecmd_input *handle); +/* FIXME: need a way to free the record */ +struct record *tracecmd_read_data(struct tracecmd_input *handle, int cpu); + +/* pevent, record, event functions */ +int pevent_data_pid(struct pevent *pevent, struct record *rec); +int pevent_data_type(struct pevent *pevent, struct record *rec); +struct event *pevent_data_event_from_type(struct pevent *pevent, int type); +const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid); + + +/* custom wrappers to account for opaque pointers */ +char *event_name(struct event *e); +double record_ts(struct record *rec);
Signature · 6 lines
1.6.3.3 -- Darren Hart IBM Linux Technology Center Real-Time Linux Team
PATCH v2 2/2 tracecmd: Start of a tracecmd swig wrapper for python
Quoted 1 line
From
bae09bb458b68d152c5fd353fa0797a52207c2b1Mon Sep 17 00:00:00 2001
From: Darren Hart <[email protected]>
Date: Wed, 16 Dec 2009 15:25:30 -0800
Subject: [PATCH 2/2 V2] tracecmd: Start of a tracecmd swig wrapper for python
Introduce a python tracecmd module for use in rapidly prototyping tracing applications. The interface description is provided in tracecmd.i, it identifies which functions are available from within python. A test python script is provided as tracecmd-test.py.
These bindings are expected to change significantly. Eventually I would like to wrap this automated binding with more pythonic objects, most likely including Trace and Event objects which merge the functionality of tracecmd-input, pevent, record, and event structures. This will make development of python apps much more accessible to many application developers.
For now, this is mostly a proof of concept and is no where near complete. It can however open a trace file and read all the events from it, displaying them by CPU in chronological order.
V2: simplified interface file with some SWIG ifdefs in the header files
--- parse-events.h | 9 +++++++++ swig.sh | 19 +++++++++++++++++++ trace-cmd.h | 2 ++ tracecmd-test.py | 38 ++++++++++++++++++++++++++++++++++++++ tracecmd.i | 12 ++++++++++++ 5 files changed, 80 insertions(+), 0 deletions(-) create mode 100755 swig.sh create mode 100755 tracecmd-test.py create mode 100644 tracecmd.i
parse-events.h +9 -0
@@ -42,6 +42,8 @@ trace_seq_init(struct trace_seq *s) s->full =0 ; } +/* SWIG doesn't like the __attribute__, don't export trace_seq to python */ +#ifndef SWIG extern int trace_seq_printf (struct trace_seq *s,const char *fmt, ...) __attribute__ ((format (printf,2 ,3 )));extern int trace_seq_vprintf (struct trace_seq *s,const char *fmt,va_list args)
@@ -53,6 +55,7 @@ extern int trace_seq_putc(struct trace_seq *s, unsigned char c);extern void trace_seq_terminate (struct trace_seq *s);extern int trace_seq_do_printf (struct trace_seq *s); +#endif /* ----------------------- pevent ----------------------- */
@@ -275,7 +278,10 @@ struct pevent {struct format_field *bprint_buf_field; }; +/* this doesn't appear to be defined anywhere... */ +#ifndef SWIG void parse_set_info (struct pevent *pevent,int nr_cpus,int long_sz); +#endif void die (char *fmt, ...);void *malloc_or_die (unsigned int size);
@@ -374,8 +380,11 @@ struct event *pevent_find_event(struct pevent *pevent, int id);struct event *pevent_find_event_by_name (struct pevent *pevent,const char *sys,const char *name); +/* SWIG doesn't like __unused */ +#ifndef SWIG void pevent_data_lat_fmt (struct pevent *pevent, struct trace_seq *s, void *data, int size __unused); +#endif int pevent_data_type (struct pevent *pevent,struct record *rec);struct event *pevent_data_event_from_type (struct pevent *pevent,int type);int pevent_data_pid (struct pevent *pevent,struct record *rec);
swig.sh new file +19 -0
@@ -0,0 +1,19 @@ +#!/bin/sh + +# Temporary hack of a build script, eventually we'll incoroporate this +# into the Makefile. You may have to update the includes to point to your +# python installation. + +rm tracecmd_wrap.c tracecmd_wrap.o _tracecmd.so &> /dev/null + +swig -Wall -python tracecmd.i + +gcc -fpic -c -I/usr/include/python2.6/ -I/usr/lib/python2.6/config \ + parse_events.c trace-read.c trace-output.c trace-cmd.c \ + trace-record.c trace-input.c tracecmd_wrap.c + +gcc -shared trace-ftrace.o trace-seq.o trace-util.o \ + parse-events.o trace-read.o trace-output.o trace-cmd.o \ + trace-record.o trace-input.o tracecmd_wrap.o \ + -o _tracecmd.so +
trace-cmd.h +2 -0
@@ -83,8 +83,10 @@ int tracecmd_set_cpu_to_timestamp(struct tracecmd_input *handle,int tracecmd_ftrace_overrides (struct tracecmd_input *handle);struct pevent *tracecmd_get_pevent (struct tracecmd_input *handle); +#ifndef SWIG /* hack for function graph work around */ extern __threadstruct tracecmd_input *tracecmd_curr_thread_handle; +#endif /* --- Creating and Writing the trace.dat file --- */
tracecmd-test.py new file +38 -0
@@ -0,0 +1,38 @@ +#!/usr/bin/env python + +from tracecmd import * +# import the struct_member_get() wrappers +from _tracecmd import * + +# Let's move the following into a new Trace object constructor +filename = "trace.dat" +trace_file = open(filename) +handle = tracecmd_open(trace_file.fileno()) +tracecmd_read_headers(handle) +tracecmd_init_data(handle) + +# These should be members, i.e. Trace.cpus +pe = tracecmd_get_pevent(handle) +cpus = tracecmd_cpus(handle) +print "Trace %s contains data for %d cpus" % (filename, cpus) + +# FIXME: this doesn't print anything... +tracecmd_print_events(handle) + +print "Cycling through the events for each CPU" +for cpu in range(0,cpus): + print "CPU", cpu + rec = tracecmd_read_data(handle, cpu) + while True: + if rec: + # these should be members of a Record object + pid = pevent_data_pid(pe, rec) + comm = pevent_data_comm_from_pid(pe, pid) + type = pevent_data_type(pe, rec) + event = pevent_data_event_from_type(pe, type) + print "\t%f %s: pid=%d comm=%s type=%d" % \ + (record_ts_get(rec), event_name_get(event), pid, comm, type) + + rec = tracecmd_read_data(handle, cpu) + else: + break
tracecmd.i new file +12 -0
@@ -0,0 +1,12 @@ +// tracecmd.i +%module tracecmd + +%{ +#include "trace-cmd.h" +%} + +%inline %{ +%} + +%include "trace-cmd.h" +%include "parse-events.h"
Signature · 6 lines
1.6.3.3 -- Darren Hart IBM Linux Technology Center Real-Time Linux Team
Re: [PATCH 2/2 V2] tracecmd: Start of a tracecmd swig wrapper for python
Quoted 40 lines · Darren Hart
Quoted 1 line
From
bae09bb458b68d152c5fd353fa0797a52207c2b1Mon Sep 17 00:00:00 2001From: Darren Hart <[email protected]>
Date: Wed, 16 Dec 2009 15:25:30 -0800
Subject: [PATCH 2/2 V2] tracecmd: Start of a tracecmd swig wrapper for pythonIntroduce a python tracecmd module for use in rapidly prototyping tracing applications. The interface description is provided in tracecmd.i, it identifies which functions are available from within python. A test python script is provided as
tracecmd-test.py.These bindings are expected to change significantly. Eventually I would like to wrap this automated binding with more pythonic objects, most likely including Trace and Event objects which merge the functionality of tracecmd-input, pevent, record, and event structures. This will make development of python apps much more accessible to many application developers.
For now, this is mostly a proof of concept and is no where near complete. It can however open a trace file and read all the events from it, displaying them by CPU in chronological order.
V2: simplified interface file with some SWIG ifdefs in the header files
Signed-off-by Darren Hart <[email protected]>--- parse-events.h | 9 +++++++++ swig.sh | 19 +++++++++++++++++++ trace-cmd.h | 2 ++ tracecmd-test.py | 38 ++++++++++++++++++++++++++++++++++++++ tracecmd.i | 12 ++++++++++++ 5 files changed, 80 insertions(+), 0 deletions(-) create mode 100755 swig.sh create mode 100755 tracecmd-test.py create mode 100644 tracecmd.iparse-events.h +0
-0
parse-events.h trace_seq_init(struct trace_seq *s) · line 45
Quoted 4 lines
parse-events.h +1
-0@@ -42,6 +42,8 @@ trace_seq_init(struct trace_seq *s) s->full =0 ; } +/* SWIG doesn't like the __attribute__, don't export trace_seq to python */
Perhaps we should do something like:
#ifdef SWIG
# define __print_attribute(x) #else # define __print_attribute(x) __attribute__ ((x)) #endif
and here we can do:
extern int trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
__print_attribute(format (printf, 2, 3));
Quoted 13 lines
parse-events.h +2
-0+#ifndef SWIG extern int trace_seq_printf (struct trace_seq *s,const char *fmt, ...) __attribute__ ((format (printf,2 ,3 )));extern int trace_seq_vprintf (struct trace_seq *s,const char *fmt,va_list args)@@ -53,6 +55,7 @@ extern int trace_seq_putc(struct trace_seq *s, unsigned char c);extern void trace_seq_terminate (struct trace_seq *s);extern int trace_seq_do_printf (struct trace_seq *s); +#endif /* ----------------------- pevent ----------------------- */
parse-events.h struct pevent { · line 284
Quoted 7 lines
parse-events.h +3
-0@@ -275,7 +278,10 @@ struct pevent {struct format_field *bprint_buf_field; }; +/* this doesn't appear to be defined anywhere... */ +#ifndef SWIG void parse_set_info (struct pevent *pevent,int nr_cpus,int long_sz); +#endif
Ug, that's leftover. You can just delete it.
parse-events.h struct event *pevent_find_event(struct pevent *pevent, int id); · line 387
Quoted 12 lines
parse-events.h +3
-0void die (char *fmt, ...);void *malloc_or_die (unsigned int size);@@ -374,8 +380,11 @@ struct event *pevent_find_event(struct pevent *pevent, int id);struct event *pevent_find_event_by_name (struct pevent *pevent,const char *sys,const char *name); +/* SWIG doesn't like __unused */ +#ifndef SWIG void pevent_data_lat_fmt (struct pevent *pevent, struct trace_seq *s, void *data, int size __unused); +#endif
Again, we should put an #ifdef SWIG around the definition of __unused.
Quoted 33 lines
parse-events.h +0
-0int pevent_data_type (struct pevent *pevent,struct record *rec);struct event *pevent_data_event_from_type (struct pevent *pevent,int type);int pevent_data_pid (struct pevent *pevent,struct record *rec);swig.sh new file +19
-0@@ -0,0 +1,19 @@ +#!/bin/sh + +# Temporary hack of a build script, eventually we'll incoroporate this +# into the Makefile. You may have to update the includes to point to your +# python installation. + +rm tracecmd_wrap.c tracecmd_wrap.o _tracecmd.so &> /dev/null + +swig -Wall -python tracecmd.i + +gcc -fpic -c -I/usr/include/python2.6/ -I/usr/lib/python2.6/config \ + parse_events.c trace-read.c trace-output.c trace-cmd.c \ + trace-record.c trace-input.c tracecmd_wrap.c + +gcc -shared trace-ftrace.o trace-seq.o trace-util.o \ + parse-events.o trace-read.o trace-output.o trace-cmd.o \ + trace-record.o trace-input.o tracecmd_wrap.o \ + -o _tracecmd.so +trace-cmd.h +0
-0
trace-cmd.h int tracecmd_set_cpu_to_timestamp(struct tracecmd_input *handle, · line 88
Quoted 6 lines
trace-cmd.h +1
-0@@ -83,8 +83,10 @@ int tracecmd_set_cpu_to_timestamp(struct tracecmd_input *handle,int tracecmd_ftrace_overrides (struct tracecmd_input *handle);struct pevent *tracecmd_get_pevent (struct tracecmd_input *handle); +#ifndef SWIG /* hack for function graph work around */ extern __threadstruct tracecmd_input *tracecmd_curr_thread_handle;
Same for __thread
Quoted 56 lines
trace-cmd.h +1
-0+#endif /* --- Creating and Writing the trace.dat file --- */ tracecmd-test.py new file +38
-0@@ -0,0 +1,38 @@ +#!/usr/bin/env python + +from tracecmd import * +# import the struct_member_get() wrappers +from _tracecmd import * + +# Let's move the following into a new Trace object constructor +filename = "trace.dat" +trace_file = open(filename) +handle = tracecmd_open(trace_file.fileno()) +tracecmd_read_headers(handle) +tracecmd_init_data(handle) + +# These should be members, i.e. Trace.cpus +pe = tracecmd_get_pevent(handle) +cpus = tracecmd_cpus(handle) +print "Trace %s contains data for %d cpus" % (filename, cpus) + +# FIXME: this doesn't print anything... +tracecmd_print_events(handle) + +print "Cycling through the events for each CPU" +for cpu in range(0,cpus): + print "CPU", cpu + rec = tracecmd_read_data(handle, cpu) + while True: + if rec: + # these should be members of a Record object + pid = pevent_data_pid(pe, rec) + comm = pevent_data_comm_from_pid(pe, pid) + type = pevent_data_type(pe, rec) + event = pevent_data_event_from_type(pe, type) + print "\t%f %s: pid=%d comm=%s type=%d" % \ + (record_ts_get(rec), event_name_get(event), pid, comm, type) + + rec = tracecmd_read_data(handle, cpu) + else: + breaktracecmd.i new file +2
-0@@ -0,0 +1,12 @@ +// tracecmd.i +%module tracecmd
tracecmd.i line 12
Quoted 10 lines
tracecmd.i new file +10
-0+ +%{ +#include "trace-cmd.h" +%} + +%inline %{ +%} + +%include "trace-cmd.h" +%include "parse-events.h"
Otherwise, looks good!
-- Steve
Steven Rostedt
AppliedQuoted 11 lines · Darren Hart
Quoted 1 line
From
35c283e97c16525bbebae6f9937a1ccd0f3b8da9Mon Sep 17 00:00:00 2001From: Darren Hart <[email protected]>
Date: Wed, 16 Dec 2009 15:40:31 -0800
Subject: [PATCH 1/2] Use opaque record type in pevent accessor functionsThe caller of the pevent accessor functions:
pevent_data_pid(pevent, data)for exampleAlready have a struct record. Let them send that directly:
Quoted 7 lines
pevent_data_pid(pevent, record)This decouples the API from the struct implementation and facilitates language bindings which use opaque pointers for passing the C structs around.
Signed-off-by Darren Hart <[email protected]>
Applied, thanks!
-- Steve