convert traceevent into a thread safe library
cover convert traceevent into a thread safe library
thread safe library. It implements per thread local storage for tep->last_event cache, and converts input_buf, input_buf_ptr and input_buf_siz internal variables to be thread specific.
[backported from Linux kernel tree]
Tzvetomir Stoyanov (2):
tools/lib/traceevent: make libtraceevent thread safe tools/lib/traceevent: make few libtraceevent internal variables to be
per thread
lib/traceevent/Makefile | 1 + lib/traceevent/event-parse-local.h | 15 +++++-- lib/traceevent/event-parse-thread.c | 63 +++++++++++++++++++++++++++++ lib/traceevent/event-parse.c | 45 +++++++++++---------- 4 files changed, 100 insertions(+), 24 deletions(-) create mode 100644 lib/traceevent/event-parse-thread.c
Signature · 1 line
2.19.2
PATCH 1/2 tools/lib/traceevent: make libtraceevent thread safe
This patch begins tranformation of libtraceevent into a thread safe library. It implements per thread local storage of tep hadler thread sensitive data and internal APIs to access it. It covers tep->last_event cache, more library's thread sensitive data will be added with additional patches.
--- lib/traceevent/Makefile | 1 + lib/traceevent/event-parse-local.h | 15 +++++-- lib/traceevent/event-parse-thread.c | 63 +++++++++++++++++++++++++++++ lib/traceevent/event-parse.c | 39 +++++++++--------- 4 files changed, 97 insertions(+), 21 deletions(-) create mode 100644 lib/traceevent/event-parse-thread.c
lib/traceevent/Makefile +1 -0
@@ -14,6 +14,7 @@ OBJS += trace-seq.o OBJS += parse-filter.o OBJS += parse-utils.o OBJS += event-parse-api.o +OBJS += event-parse-thread.o # Additional util objects OBJS += str_error_r.o
lib/traceevent/event-parse-local.h +12 -3
@@ -14,6 +14,12 @@ struct func_list;struct event_handler ;struct func_resolver ; +/* cache */ +struct tep_thread_data { +struct tep_handle *tep; +struct tep_event *last_event; +}; +struct tep_handle {int ref_count;
@@ -85,9 +91,6 @@ struct tep_handle {int parsing_failures; -/* cache */ -struct tep_event *last_event; -char *trace_clock; };
@@ -98,4 +101,10 @@ unsigned short tep_data2host2(struct tep_handle *pevent, unsigned short data);unsigned int tep_data2host4 (struct tep_handle *pevent,unsigned int data);unsigned long long tep_data2host8 (struct tep_handle *pevent,unsigned long long data); +/* tep cache per thread */ +struct tep_event *get_thread_local_event_by_id (struct tep_handle *tep,int id); +struct tep_event * +get_thread_local_event_by_name (struct tep_handle *tep,const char *name,const char *sys); +void set_thread_local_event_cache (struct tep_handle *tep,struct tep_event *event); +#endif /* _PARSE_EVENTS_INT_H */
lib/traceevent/event-parse-thread.c new file +63 -0
63 lines not on this page · raw message
lib/traceevent/event-parse.c +21 -18
@@ -3461,28 +3461,29 @@ static int events_id_cmp(const void *a, const void *b);/** * tep_find_event - find an event by given id -* @pevent: a handle to the pevent +* @tep: a handle to the tep context * @id: the id of the event * * Returns an event that has a given @id. */ -struct tep_event *tep_find_event (struct tep_handle *pevent,int id) +struct tep_event *tep_find_event (struct tep_handle *tep,int id) {struct tep_event **eventptr;struct tep_event key;struct tep_event *pkey = &key; +struct tep_event *event_cache =get_thread_local_event_by_id (tep, id);/* Check cache first */ -if (pevent->last_event && pevent->last_event->id == id) -return pevent->last_event; +if (event_cache) +return event_cache; key.id = id; - eventptr =bsearch (&pkey, pevent->events, pevent->nr_events, -sizeof (*pevent->events), events_id_cmp); + eventptr =bsearch (&pkey, tep->events, tep->nr_events, +sizeof (*tep->events), events_id_cmp);if (eventptr) { - pevent->last_event = *eventptr; +set_thread_local_event_cache (tep, *eventptr);return *eventptr; }
@@ -3491,7 +3492,7 @@ struct tep_event *tep_find_event(struct tep_handle *pevent, int id)/** * tep_find_event_by_name - find an event by given name -* @pevent: a handle to the pevent +* @tep: a handle to the tep context * @sys: the system name to search for * @name: the name of the event to search for *
@@ -3499,19 +3500,19 @@ struct tep_event *tep_find_event(struct tep_handle *pevent, int id)* @sys. If @sys is NULL the first event with @name is returned. */ struct tep_event * -tep_find_event_by_name (struct tep_handle *pevent, +tep_find_event_by_name (struct tep_handle *tep,const char *sys,const char *name) { -struct tep_event *event;int i; +struct tep_event *event =NULL ; +struct tep_event *event_cache =get_thread_local_event_by_name (tep, name, sys); -if (pevent->last_event && -strcmp (pevent->last_event->name, name) ==0 && - (!sys ||strcmp (pevent->last_event->system, sys) ==0 )) -return pevent->last_event; +/* Check cache first */ +if (event_cache) +return event_cache; -for (i =0 ; i < pevent->nr_events; i++) { - event = pevent->events[i]; +for (i =0 ; i < tep->nr_events; i++) { + event = tep->events[i];if (strcmp (event->name, name) ==0 ) {if (!sys)break ;
@@ -3519,10 +3520,12 @@ tep_find_event_by_name(struct tep_handle *pevent,break ; } } -if (i == pevent->nr_events) + +if (i == tep->nr_events) event =NULL ; +if (event) +set_thread_local_event_cache (tep, event); - pevent->last_event = event;return event; }
Signature · 1 line
2.19.2
Re: [PATCH 1/2] tools/lib/traceevent: make libtraceevent thread safe
lib/traceevent/event-parse-thread.c line 63
Quoted 7 lines · On Wed, 5 Dec 2018 09:22:12 +0000 Tzvetomir Stoyanov <[email protected]>…
lib/traceevent/event-parse-thread.c new file +7
-0+void set_thread_local_event_cache (struct tep_handle *tep,struct tep_event *event) +{ + tep_thread_local.tep = tep; + tep_thread_local.last_event = event; +} + +
FYI,
Your file ended up with an extra blank line at the end. I deleted it, but that does make git complain when applying this patch.
-- Steve
Quoted 2 lines
lib/traceevent/event-parse.c +0
-0
Re: [PATCH 1/2] tools/lib/traceevent: make libtraceevent thread safe
In reply to Tzvetomir Stoyanovlib/traceevent/event-parse.c struct tep_event *tep_find_event(struct tep_handle *pevent, int id) · line 3508
Quoted 12 lines · On Wed, 5 Dec 2018 09:22:12 +0000 Tzvetomir Stoyanov <[email protected]>…
lib/traceevent/event-parse.c +3
-2@@ -3499,19 +3500,19 @@ struct tep_event *tep_find_event(struct tep_handle *pevent, int id)* @sys. If @sys is NULL the first event with @name is returned. */ struct tep_event * -tep_find_event_by_name (struct tep_handle *pevent, +tep_find_event_by_name (struct tep_handle *tep,const char *sys,const char *name) { -struct tep_event *event;int i; +struct tep_event *event =NULL ; +struct tep_event *event_cache =get_thread_local_event_by_name (tep, name, sys);
BTW, I modified this to keep with the "upside down christmas tree" method. That is, instead of:
int i; struct tep_event *event = NULL; struct tep_event *event_cache = get_thread_local_event_by_name(tep, name, sys);
I made it:
struct tep_event *event_cache = get_thread_local_event_by_name(tep, name, sys); struct tep_event *event = NULL; int i;
to make it look better, and some kernel developers (as well as the perf developers) require that for declarations.
-- Steve
Quoted 16 lines
lib/traceevent/event-parse.c +5
-6-if (pevent->last_event && -strcmp (pevent->last_event->name, name) ==0 && - (!sys ||strcmp (pevent->last_event->system, sys) ==0 )) -return pevent->last_event; +/* Check cache first */ +if (event_cache) +return event_cache; -for (i =0 ; i < pevent->nr_events; i++) { - event = pevent->events[i]; +for (i =0 ; i < tep->nr_events; i++) { + event = tep->events[i];if (strcmp (event->name, name) ==0 ) {if (!sys)break ;
PATCH 2/2 tools/lib/traceevent: make few libtraceevent internal variables to be per thread
In reply to Tzvetomir StoyanovThis patch continues the effort to transform libtraceevent into a thread safe library. It converts input_buf, input_buf_ptr and input_buf_siz internal variables to be thread specific. This buffer is not related to a specific tep context. It is used internally by the library during the parsing of various strings.
--- lib/traceevent/event-parse.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
lib/traceevent/event-parse.c +3 -3
@@ -28,9 +28,9 @@#include "event-utils.h" #include "trace-seq.h" -static const char *input_buf; -static unsigned long long input_buf_ptr; -static unsigned long long input_buf_siz; +static __threadconst char *input_buf; +static __threadunsigned long long input_buf_ptr; +static __threadunsigned long long input_buf_siz;static int is_flag_field;static int is_symbolic_field;
Signature · 1 line
2.19.2