LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-trace-devel@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Tom Zanussi <zanussi@kernel.org>,
	Daniel Bristot de Oliveira <bristot@redhat.com>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	linux-rt-users <linux-rt-users@vger.kernel.org>,
	Clark Williams <williams@redhat.com>,
	"Steven Rostedt (VMware)" <rostedt@goodmis.org>
Subject: [PATCH 4/9] libtracefs: Add API tracefs_hist_data_read()
Date: Tue, 10 Aug 2021 16:48:13 -0400	[thread overview]
Message-ID: <20210810204818.880714-5-rostedt@goodmis.org> (raw)
In-Reply-To: <20210810204818.880714-1-rostedt@goodmis.org>

From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

Adds an API to read a "hist" file of a trace event and create a list of
tracefs_hist_data descriptors for every histogram that exists in the
"hist" file.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 include/tracefs.h       |  7 ++++
 src/tracefs-hist-data.c | 74 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)

diff --git a/include/tracefs.h b/include/tracefs.h
index 6bd40d72cb25..f1e4ffa0d65f 100644
--- a/include/tracefs.h
+++ b/include/tracefs.h
@@ -418,7 +418,14 @@ struct tracefs_hist_data;
 struct tracefs_hist_data *tracefs_hist_data_parse(const char *buffer,
 						  const char **next_buffer,
 						  char **err);
+
+struct tracefs_hist_data **tracefs_hist_data_read(struct tracefs_instance *instance,
+						  const char *system,
+						  const char *event,
+						  char **err);
+
 void tracefs_hist_data_free(struct tracefs_hist_data *hdata);
+void tracefs_hist_data_free_list(struct tracefs_hist_data **hdata_list);
 
 struct tracefs_synth;
 
diff --git a/src/tracefs-hist-data.c b/src/tracefs-hist-data.c
index c7e110559ee8..ab1ae824f59b 100644
--- a/src/tracefs-hist-data.c
+++ b/src/tracefs-hist-data.c
@@ -718,6 +718,24 @@ void tracefs_hist_data_free(struct tracefs_hist_data *hdata)
 	free(hdata);
 }
 
+/**
+ * tracefs_hist_data_free_list - frees a list of created hist data descriptors
+ * @hdata_list: The tracefs_hist_data descriptor list to free.
+ *
+ * Frees the data allocated by tracefs_hist_data_read().
+ */
+void tracefs_hist_data_free_list(struct tracefs_hist_data **hdata_list)
+{
+	int i;
+
+	if (!hdata_list)
+		return;
+
+	for (i = 0; hdata_list[i]; i++)
+		tracefs_hist_data_free(hdata_list[i]);
+	free(hdata_list);
+}
+
 /* Used for debugging in gdb */
 static void breakpoint(char *text)
 {
@@ -1019,3 +1037,59 @@ tracefs_hist_data_parse(const char *buffer, const char **next_buffer, char **err
 	tracefs_hist_data_free(hdata);
 	return NULL;
 }
+
+/**
+ * tracefs_hist_data_read - Reads and parses the trace event "hist" file
+ * @instance: The instance the trace event is in (NULL for top level)
+ * @system: The system of the @event (NULL to pick first event)
+ * @event: The trace event name to read the hist file from
+ * @err: On parsing errors, @err will be set to a message explaining what failed.
+ *
+ * Reads the content of a trace @event hist file and parses it.
+ *
+ * Returns an array of tracefs_hist_data descriptors, as a hist file
+ * may contain more than one histogram. Must be freed with
+ * tracefs_hist_data_free_list().
+ *
+ * Returns NULL on error, and if there was a parsing error, @err will contain
+ * a message explaining what failed.
+ */
+struct tracefs_hist_data **
+tracefs_hist_data_read(struct tracefs_instance *instance,
+		       const char *system, const char *event, char **err)
+{
+	struct tracefs_hist_data **tmp, **hdata_list = NULL;
+	const char *buffer;
+	char *content;
+	int cnt = 0;
+
+	if (err)
+		*err = NULL;
+
+	content = tracefs_event_file_read(instance, system, event, "hist", NULL);
+	if (!content)
+		return NULL;
+
+	buffer = content;
+	do {
+		tmp = realloc(hdata_list, sizeof(*tmp) * (cnt + 2));
+		if (!tmp)
+			goto error;
+		tmp[cnt + 1] = NULL;
+		tmp[cnt] = tracefs_hist_data_parse(buffer, &buffer, err);
+		if (!tmp[cnt])
+			goto error;
+		hdata_list = tmp;
+		if (buffer)
+			cnt++;
+	} while (buffer);
+
+	free(content);
+	return hdata_list;
+
+ error:
+	free(content);
+	tracefs_hist_data_free_list(hdata_list);
+	return NULL;
+}
+
-- 
2.30.2


  parent reply	other threads:[~2021-08-10 20:48 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-10 20:48 [PATCH 0/9] libtracefs: APIs to read a trace event hist file Steven Rostedt
2021-08-10 20:48 ` [PATCH 1/9] tracefs: Add API tracefs_hist_data_parse() Steven Rostedt
2021-09-10 16:54   ` Daniel Bristot de Oliveira
2021-08-10 20:48 ` [PATCH 2/9] libtracefs: Parse comment for hist data information Steven Rostedt
2021-08-10 20:48 ` [PATCH 3/9] libtracefs: Change hist_data_key type to flags Steven Rostedt
2021-08-10 20:48 ` Steven Rostedt [this message]
2021-08-10 20:48 ` [PATCH 5/9] libtracefs: Add API tracefs_list_dup() Steven Rostedt
2021-08-10 20:48 ` [PATCH 6/9] libtracefs: Add APIs tracefs_hist_data_keys/value_names() Steven Rostedt
2021-08-10 20:48 ` [PATCH 7/9] libtracefs: Add API tracefs_hist_data_keys/values() and next_bucket() Steven Rostedt
2021-08-10 20:48 ` [PATCH 8/9] libtracefs: Have tracefs_hist_bucket_key flags save the type Steven Rostedt
2021-08-10 20:48 ` [PATCH 9/9] libtracefs: Add man pages for tracefs_hist_data functions Steven Rostedt

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=20210810204818.880714-5-rostedt@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=bristot@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rt-users@vger.kernel.org \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=williams@redhat.com \
    --cc=zanussi@kernel.org \
    --subject='Re: [PATCH 4/9] libtracefs: Add API tracefs_hist_data_read()' \
    /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

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).