rasd: Read the events data and print out the events fields

Read the mmapped buffers and dump out events in the following format:
 <sys>:<name>: <kernel format>\n

Signed-off-by: Jean Pihet <jean.pihet@linaro.org>
Link: http://lkml.kernel.org/r/1412933690-25576-11-git-send-email-jean.pihet@linaro.org
Signed-off-by: Borislav Petkov <bp@suse.de>
diff --git a/include/trace_event.h b/include/trace_event.h
index 83cefdb..c499bcc 100644
--- a/include/trace_event.h
+++ b/include/trace_event.h
@@ -8,3 +8,6 @@
 };
 
 struct event_format *trace_event__tp_format(const char *sys, const char *name);
+
+void event_format__print(struct event_format *event, int cpu, void *data,
+			 int size);
diff --git a/src/rasd.c b/src/rasd.c
index 5dce550..dd95440 100644
--- a/src/rasd.c
+++ b/src/rasd.c
@@ -3,9 +3,11 @@
 #include "evlist.h"
 #include "ras.h"
 #include "debugfs.h"
+#include "trace_event.h"
 
 #include <regex.h>
 
+#define POLL_TIMEOUT_MS		1000
 #define EVENT_STR_MAX_LENGTH	1024
 #define SYS_NAME_SEPARATOR	":"
 #define RASD_CFG_FILE		"rasd.cfg"
@@ -120,11 +122,51 @@
 	return ret;
 }
 
+static int mmap_read_idx(int idx)
+{
+	struct perf_sample sample;
+	struct perf_evsel *evsel;
+	union perf_event *event;
+	int ret, nr_samples = 0;
+
+	while ((event = perf_evlist__mmap_read(evlist, idx))) {
+		ret = perf_evlist__parse_sample(evlist, event, &sample);
+		if (ret) {
+			pr_err("Can't parse sample, err = %d\n", ret);
+			goto next_event;
+		}
+
+		evsel = perf_evlist__id2evsel(evlist, sample.id);
+		if (!evsel)
+			goto next_event;
+
+		if (event->header.type == PERF_RECORD_SAMPLE)
+			nr_samples++;
+		else
+			goto next_event;
+
+		if (!evsel->tp_format)
+			goto next_event;
+
+		/* Parse raw data and print out the tracepoint */
+		event_format__print(evsel->tp_format, sample.cpu,
+				    sample.raw_data, sample.raw_size);
+
+next_event:
+		perf_evlist__mmap_consume(evlist, idx);
+	}
+
+	pr_debug("read %d samples\n", nr_samples);
+
+	return nr_samples;
+}
+
 int main()
 {
 	struct perf_evsel *c;
 	struct thread_map *threads;
 	struct cpu_map *cpus;
+	int i;
 
 	page_size = sysconf(_SC_PAGE_SIZE);
 
@@ -162,6 +204,17 @@
 	/* Enable at kernel level */
 	perf_evlist__enable(evlist);
 
+	while (1) {
+		/*
+		 * Relax the CPUs by polling for events coming in, with a
+		 * timeout
+		 */
+		poll(evlist->pollfd, evlist->nr_fds, POLL_TIMEOUT_MS);
+		/* Read, parse and print out the events buffers */
+		for (i = 0; i < evlist->nr_mmaps; i++)
+			mmap_read_idx(i);
+	}
+
 	perf_evlist__delete(evlist);
 
 	free(rasd.name);
diff --git a/src/trace_event.c b/src/trace_event.c
index 5ea2556..ab06a82 100644
--- a/src/trace_event.c
+++ b/src/trace_event.c
@@ -82,3 +82,22 @@
 
 	return tp_format(sys, name);
 }
+
+void event_format__print(struct event_format *event,
+			 int cpu, void *data, int size)
+{
+	struct pevent_record record;
+	struct trace_seq s;
+
+	memset(&record, 0, sizeof(record));
+	record.cpu = cpu;
+	record.size = size;
+	record.data = data;
+
+	printf("%s:%s: ", event->system, event->name);
+	trace_seq_init(&s);
+	pevent_event_info(&s, event, &record);
+	trace_seq_do_printf(&s);
+	trace_seq_destroy(&s);
+	printf("\n");
+}