linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ingo Molnar <mingo@kernel.org>, Thomas Gleixner <tglx@linutronix.de>
Cc: Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
	Clark Williams <williams@redhat.com>,
	linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
	Tzvetomir Stoyanov <tstoyanov@vmware.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Jiri Olsa <jolsa@redhat.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Arnaldo Carvalho de Melo <acme@redhat.com>
Subject: [PATCH 13/44] tools lib traceevent: Implement a new API, tep_list_events_copy()
Date: Tue,  2 Apr 2019 13:05:18 -0300	[thread overview]
Message-ID: <20190402160549.13544-14-acme@kernel.org> (raw)
In-Reply-To: <20190402160549.13544-1-acme@kernel.org>

From: Tzvetomir Stoyanov <tstoyanov@vmware.com>

Existing API tep_list_events() is not thread safe, it uses the internal
array sort_events to keep cache of the sorted events and reuses it. This
patch implements a new API, tep_list_events_copy(), which allocates new
sorted array each time it is called. It could be used when a sorted
events functionality is needed in thread safe use cases. It is up to the
caller to free the array.

Signed-off-by: Tzvetomir Stoyanov <tstoyanov@vmware.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: http://lore.kernel.org/linux-trace-devel/20181218133013.31094-1-tstoyanov@vmware.com
Link: http://lkml.kernel.org/r/20190401164343.117437443@goodmis.org
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/lib/traceevent/event-parse.c | 109 +++++++++++++++++++++++------
 tools/lib/traceevent/event-parse.h |   5 +-
 2 files changed, 91 insertions(+), 23 deletions(-)

diff --git a/tools/lib/traceevent/event-parse.c b/tools/lib/traceevent/event-parse.c
index 666e23ded6ed..c00aebab3c33 100644
--- a/tools/lib/traceevent/event-parse.c
+++ b/tools/lib/traceevent/event-parse.c
@@ -5651,32 +5651,26 @@ static int events_system_cmp(const void *a, const void *b)
 	return events_id_cmp(a, b);
 }
 
-struct tep_event **tep_list_events(struct tep_handle *pevent, enum tep_event_sort_type sort_type)
+static struct tep_event **list_events_copy(struct tep_handle *tep)
 {
 	struct tep_event **events;
-	int (*sort)(const void *a, const void *b);
-
-	events = pevent->sort_events;
-
-	if (events && pevent->last_type == sort_type)
-		return events;
 
-	if (!events) {
-		events = malloc(sizeof(*events) * (pevent->nr_events + 1));
-		if (!events)
-			return NULL;
+	if (!tep)
+		return NULL;
 
-		memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
-		events[pevent->nr_events] = NULL;
+	events = malloc(sizeof(*events) * (tep->nr_events + 1));
+	if (!events)
+		return NULL;
 
-		pevent->sort_events = events;
+	memcpy(events, tep->events, sizeof(*events) * tep->nr_events);
+	events[tep->nr_events] = NULL;
+	return events;
+}
 
-		/* the internal events are sorted by id */
-		if (sort_type == TEP_EVENT_SORT_ID) {
-			pevent->last_type = sort_type;
-			return events;
-		}
-	}
+static void list_events_sort(struct tep_event **events, int nr_events,
+			     enum tep_event_sort_type sort_type)
+{
+	int (*sort)(const void *a, const void *b);
 
 	switch (sort_type) {
 	case TEP_EVENT_SORT_ID:
@@ -5689,11 +5683,82 @@ struct tep_event **tep_list_events(struct tep_handle *pevent, enum tep_event_sor
 		sort = events_system_cmp;
 		break;
 	default:
+		sort = NULL;
+	}
+
+	if (sort)
+		qsort(events, nr_events, sizeof(*events), sort);
+}
+
+/**
+ * tep_list_events - Get events, sorted by given criteria.
+ * @tep: a handle to the tep context
+ * @sort_type: desired sort order of the events in the array
+ *
+ * Returns an array of pointers to all events, sorted by the given
+ * @sort_type criteria. The last element of the array is NULL. The returned
+ * memory must not be freed, it is managed by the library.
+ * The function is not thread safe.
+ */
+struct tep_event **tep_list_events(struct tep_handle *tep,
+				   enum tep_event_sort_type sort_type)
+{
+	struct tep_event **events;
+
+	if (!tep)
+		return NULL;
+
+	events = tep->sort_events;
+	if (events && tep->last_type == sort_type)
 		return events;
+
+	if (!events) {
+		events = list_events_copy(tep);
+		if (!events)
+			return NULL;
+
+		tep->sort_events = events;
+
+		/* the internal events are sorted by id */
+		if (sort_type == TEP_EVENT_SORT_ID) {
+			tep->last_type = sort_type;
+			return events;
+		}
 	}
 
-	qsort(events, pevent->nr_events, sizeof(*events), sort);
-	pevent->last_type = sort_type;
+	list_events_sort(events, tep->nr_events, sort_type);
+	tep->last_type = sort_type;
+
+	return events;
+}
+
+
+/**
+ * tep_list_events_copy - Thread safe version of tep_list_events()
+ * @tep: a handle to the tep context
+ * @sort_type: desired sort order of the events in the array
+ *
+ * Returns an array of pointers to all events, sorted by the given
+ * @sort_type criteria. The last element of the array is NULL. The returned
+ * array is newly allocated inside the function and must be freed by the caller
+ */
+struct tep_event **tep_list_events_copy(struct tep_handle *tep,
+					enum tep_event_sort_type sort_type)
+{
+	struct tep_event **events;
+
+	if (!tep)
+		return NULL;
+
+	events = list_events_copy(tep);
+	if (!events)
+		return NULL;
+
+	/* the internal events are sorted by id */
+	if (sort_type == TEP_EVENT_SORT_ID)
+		return events;
+
+	list_events_sort(events, tep->nr_events, sort_type);
 
 	return events;
 }
diff --git a/tools/lib/traceevent/event-parse.h b/tools/lib/traceevent/event-parse.h
index aec48f2aea8a..41159358abc2 100644
--- a/tools/lib/traceevent/event-parse.h
+++ b/tools/lib/traceevent/event-parse.h
@@ -544,7 +544,10 @@ void tep_event_info(struct trace_seq *s, struct tep_event *event,
 int tep_strerror(struct tep_handle *pevent, enum tep_errno errnum,
 		 char *buf, size_t buflen);
 
-struct tep_event **tep_list_events(struct tep_handle *pevent, enum tep_event_sort_type);
+struct tep_event **tep_list_events(struct tep_handle *tep,
+				   enum tep_event_sort_type);
+struct tep_event **tep_list_events_copy(struct tep_handle *tep,
+					enum tep_event_sort_type);
 struct tep_format_field **tep_event_common_fields(struct tep_event *event);
 struct tep_format_field **tep_event_fields(struct tep_event *event);
 
-- 
2.20.1

  parent reply	other threads:[~2019-04-02 16:05 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-02 16:05 [GIT PULL 00/44] perf/core improvements and fixes for 5.2 Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 01/44] perf trace: Add 'string' event alias to select syscalls with string args Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 02/44] perf augmented_raw_syscalls: Copy strings from all syscalls with 1st or 2nd string arg Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 03/44] perf augmented_raw_syscalls: Use a PERCPU_ARRAY map to copy more string bytes Arnaldo Carvalho de Melo
2019-04-02 16:44   ` Andrii Nakryiko
2019-04-02 16:05 ` [PATCH 04/44] perf trace beauty renameat: No need to include linux/fs.h Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 05/44] perf tools: Add header defining used namespace struct to event.h Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 06/44] perf list: Fix s390 counter long description for L1D_RO_EXCL_WRITES Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 07/44] perf stat: Revert checks for duration_time Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 08/44] perf stat: Implement duration_time as a proper event Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 09/44] perf evsel: Support printing evsel name for 'duration_time' Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 10/44] perf list: Output tool events Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 11/44] tools lib traceevent: Handle trace_printk() "%px" Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 12/44] tools lib traceevent: Add mono clocks to be parsed in seconds Arnaldo Carvalho de Melo
2019-04-02 16:05 ` Arnaldo Carvalho de Melo [this message]
2019-04-02 16:05 ` [PATCH 14/44] tools lib traceevent: Add more debugging to see various internal ring buffer entries Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 15/44] tools lib traceevent: Change description of few APIs Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 16/44] tools lib traceevent: Coding style fixes Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 17/44] tools lib traceevent: Implement new traceevent APIs for accessing struct tep_handler fields Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 18/44] tools lib traceevent: Removed unneeded !! and return parenthesis Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 19/44] tools lib traceevent: Remove tep filter trivial APIs Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 20/44] tools lib traceevent: Remove call to exit() from tep_filter_add_filter_str() Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 21/44] tools tools, tools lib traceevent: Make traceevent APIs more consistent Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 22/44] tools lib traceevent: Rename input arguments of libtraceevent APIs from pevent to tep Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 23/44] perf tools, tools lib traceevent: Rename "pevent" member of struct tep_event to "tep" Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 24/44] perf tools, tools lib traceevent: Rename "pevent" member of struct tep_event_filter " Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 25/44] tools lib traceevent: Rename input arguments and local variables of libtraceevent from pevent to tep Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 26/44] tools build: Implement libzstd feature check, LIBZSTD_DIR and NO_LIBZSTD defines Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 27/44] perf record: Implement --mmap-flush=<number> option Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 28/44] perf vendor events intel: Update metrics from TMAM 3.5 Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 29/44] perf vendor events intel: Update Broadwell events to v23 Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 30/44] perf vendor events intel: Update Broadwell-DE events to v7 Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 31/44] perf vendor events intel: Update Skylake events to v42 Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 32/44] perf vendor events intel: Update SkylakeX events to v1.12 Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 33/44] perf vendor events intel: Update BroadwellX events to v14 Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 34/44] perf vendor events intel: Update HaswellX events to v20 Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 35/44] perf vendor events intel: Update IvyTown " Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 36/44] perf vendor events intel: Update JakeTown " Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 37/44] perf vendor events intel: Update SandyBridge events to v16 Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 38/44] perf vendor events intel: Update IvyBridge events to v21 Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 39/44] perf vendor events intel: Update Haswell events to v28 Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 40/44] perf vendor events intel: Update KnightsLanding events to v9 Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 41/44] perf vendor events intel: Update Bonnell to V4 Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 42/44] perf vendor events intel: Update Goldmont to v13 Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 43/44] perf vendor events intel: Update GoldmontPlus to v1.01 Arnaldo Carvalho de Melo
2019-04-02 16:05 ` [PATCH 44/44] perf vendor events intel: Update Silvermont to v14 Arnaldo Carvalho de Melo

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=20190402160549.13544-14-acme@kernel.org \
    --to=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=jolsa@kernel.org \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    --cc=tstoyanov@vmware.com \
    --cc=williams@redhat.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).