mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Adrian Hunter <adrian.hunter@intel.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>,
	linux-kernel@vger.kernel.org, David Ahern <dsahern@gmail.com>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Jiri Olsa <jolsa@redhat.com>, Namhyung Kim <namhyung@gmail.com>,
	Paul Mackerras <paulus@samba.org>,
	Stephane Eranian <eranian@google.com>
Subject: [PATCH 15/25] perf itrace: Add a heap for sorting Instruction Tracing queues
Date: Tue, 11 Nov 2014 16:16:53 +0200	[thread overview]
Message-ID: <1415715423-15563-16-git-send-email-adrian.hunter@intel.com> (raw)
In-Reply-To: <1415715423-15563-1-git-send-email-adrian.hunter@intel.com>

In order to process Instruction Tracing
data in time order, the queue with data
with the lowest timestamp must be
processed first.  Provide a heap to
keep track of which queue that is.

As with the queues, a decoder does not have
to use the heap, but Intel BTS and Intel PT
will use it.

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
---
 tools/perf/util/itrace.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/itrace.h | 29 +++++++++++++++++
 2 files changed, 114 insertions(+)

diff --git a/tools/perf/util/itrace.c b/tools/perf/util/itrace.c
index c67bf10..b50d959 100644
--- a/tools/perf/util/itrace.c
+++ b/tools/perf/util/itrace.c
@@ -347,6 +347,91 @@ void itrace_queues__free(struct itrace_queues *queues)
 	queues->nr_queues = 0;
 }
 
+static void itrace_heapify(struct itrace_heap_item *heap_array,
+			   unsigned int pos, unsigned int queue_nr,
+			   u64 ordinal)
+{
+	unsigned int parent;
+
+	while (pos) {
+		parent = (pos - 1) >> 1;
+		if (heap_array[parent].ordinal <= ordinal)
+			break;
+		heap_array[pos] = heap_array[parent];
+		pos = parent;
+	}
+	heap_array[pos].queue_nr = queue_nr;
+	heap_array[pos].ordinal = ordinal;
+}
+
+int itrace_heap__add(struct itrace_heap *heap, unsigned int queue_nr,
+		     u64 ordinal)
+{
+	struct itrace_heap_item *heap_array;
+
+	if (queue_nr >= heap->heap_sz) {
+		unsigned int heap_sz = ITRACE_INIT_NR_QUEUES;
+
+		while (heap_sz <= queue_nr)
+			heap_sz <<= 1;
+		heap_array = realloc(heap->heap_array,
+				     heap_sz * sizeof(struct itrace_heap_item));
+		if (!heap_array)
+			return -ENOMEM;
+		heap->heap_array = heap_array;
+		heap->heap_sz = heap_sz;
+	}
+
+	itrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
+
+	return 0;
+}
+
+void itrace_heap__free(struct itrace_heap *heap)
+{
+	zfree(&heap->heap_array);
+	heap->heap_cnt = 0;
+	heap->heap_sz = 0;
+}
+
+void itrace_heap__pop(struct itrace_heap *heap)
+{
+	unsigned int pos, last, heap_cnt = heap->heap_cnt;
+	struct itrace_heap_item *heap_array;
+
+	if (!heap_cnt)
+		return;
+
+	heap->heap_cnt -= 1;
+
+	heap_array = heap->heap_array;
+
+	pos = 0;
+	while (1) {
+		unsigned int left, right;
+
+		left = (pos << 1) + 1;
+		if (left >= heap_cnt)
+			break;
+		right = left + 1;
+		if (right >= heap_cnt) {
+			heap_array[pos] = heap_array[left];
+			return;
+		}
+		if (heap_array[left].ordinal < heap_array[right].ordinal) {
+			heap_array[pos] = heap_array[left];
+			pos = left;
+		} else {
+			heap_array[pos] = heap_array[right];
+			pos = right;
+		}
+	}
+
+	last = heap_cnt - 1;
+	itrace_heapify(heap_array, pos, heap_array[last].queue_nr,
+		       heap_array[last].ordinal);
+}
+
 size_t itrace_record__info_priv_size(struct itrace_record *itr)
 {
 	if (itr)
diff --git a/tools/perf/util/itrace.h b/tools/perf/util/itrace.h
index c357893..a7d4ad2 100644
--- a/tools/perf/util/itrace.h
+++ b/tools/perf/util/itrace.h
@@ -172,6 +172,29 @@ struct itrace_queues {
 };
 
 /**
+ * struct itrace_heap_item - element of struct itrace_heap.
+ * @queue_nr: queue number
+ * @ordinal: value used for sorting (lowest ordinal is top of the heap) expected
+ *           to be a timestamp
+ */
+struct itrace_heap_item {
+	unsigned int		queue_nr;
+	u64			ordinal;
+};
+
+/**
+ * struct itrace_heap - a heap suitable for sorting Instruction Tracing queues.
+ * @heap_array: the heap
+ * @heap_cnt: the number of elements in the heap
+ * @heap_sz: maximum number of elements (grows as needed)
+ */
+struct itrace_heap {
+	struct itrace_heap_item	*heap_array;
+	unsigned int		heap_cnt;
+	unsigned int		heap_sz;
+};
+
+/**
  * struct itrace_mmap - records an mmap of the itrace buffer.
  * @base: address of mapped area
  * @userpg: pointer to buffer's perf_event_mmap_page
@@ -280,6 +303,12 @@ void *itrace_buffer__get_data(struct itrace_buffer *buffer, int fd);
 void itrace_buffer__put_data(struct itrace_buffer *buffer);
 void itrace_buffer__drop_data(struct itrace_buffer *buffer);
 void itrace_buffer__free(struct itrace_buffer *buffer);
+
+int itrace_heap__add(struct itrace_heap *heap, unsigned int queue_nr,
+		     u64 ordinal);
+void itrace_heap__pop(struct itrace_heap *heap);
+void itrace_heap__free(struct itrace_heap *heap);
+
 struct itrace_record *itrace_record__init(int *err);
 
 int itrace_record__options(struct itrace_record *itr,
-- 
1.9.1


  parent reply	other threads:[~2014-11-11 14:20 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-11 14:16 [PATCH 00/25] perf tools: Introduce an abstraction for Instruction Tracing Adrian Hunter
2014-11-11 14:16 ` [PATCH 01/25] perf tools: Do not poll events that use the system_wide flag Adrian Hunter
2014-11-20  7:40   ` [tip:perf/core] perf evlist: " tip-bot for Adrian Hunter
2014-11-11 14:16 ` [PATCH 02/25] perf tools: Add perf-read-vdso32 and perf-read-vdsox32 to .gitignore Adrian Hunter
2014-11-20  7:40   ` [tip:perf/core] " tip-bot for Adrian Hunter
2014-11-11 14:16 ` [PATCH 03/25] perf tools: Only override the default :tid comm entry Adrian Hunter
2014-11-20  7:40   ` [tip:perf/core] perf tools: Only override the default : tid " tip-bot for Adrian Hunter
2014-11-11 14:16 ` [PATCH 04/25] perf header: Add Instruction Tracing feature Adrian Hunter
2014-11-11 14:16 ` [PATCH 05/25] perf evlist: Add initial support for mmapping an Instruction Trace buffer Adrian Hunter
2014-11-11 14:16 ` [PATCH 06/25] perf tools: Add user events for Instruction Tracing Adrian Hunter
2014-11-11 14:16 ` [PATCH 07/25] perf tools: Add support for Instruction Trace recording Adrian Hunter
2014-11-11 14:16 ` [PATCH 08/25] perf record: Add basic Instruction Tracing support Adrian Hunter
2014-11-11 14:16 ` [PATCH 09/25] perf record: Extend -m option for Instruction Tracing mmap pages Adrian Hunter
2014-11-11 14:16 ` [PATCH 10/25] perf tools: Add a user event for Instruction Tracing errors Adrian Hunter
2014-11-11 14:16 ` [PATCH 11/25] perf session: Add hooks to allow transparent decoding of Instruction Tracing data Adrian Hunter
2014-11-11 14:16 ` [PATCH 12/25] perf session: Add Instruction Tracing options Adrian Hunter
2014-11-11 14:16 ` [PATCH 13/25] perf itrace: Add helpers for Instruction Tracing errors Adrian Hunter
2014-11-11 14:16 ` [PATCH 14/25] perf itrace: Add helpers for queuing Instruction Tracing data Adrian Hunter
2014-11-11 14:16 ` Adrian Hunter [this message]
2014-11-11 14:16 ` [PATCH 16/25] perf itrace: Add processing for Instruction Tracing events Adrian Hunter
2014-11-11 14:16 ` [PATCH 17/25] perf itrace: Add a hashtable for caching decoded instructions Adrian Hunter
2014-11-11 14:16 ` [PATCH 18/25] perf tools: Add member to struct dso for an instruction cache Adrian Hunter
2014-11-11 14:16 ` [PATCH 19/25] perf script: Add Instruction Tracing support Adrian Hunter
2014-11-11 14:16 ` [PATCH 20/25] perf script: Always allow fields 'addr' and 'cpu' for itrace Adrian Hunter
2014-11-11 14:16 ` [PATCH 21/25] perf report: Add Instruction Tracing support Adrian Hunter
2014-11-11 14:17 ` [PATCH 22/25] perf inject: Re-pipe Instruction Tracing events Adrian Hunter
2014-11-11 14:17 ` [PATCH 23/25] perf inject: Add Instruction Tracing support Adrian Hunter
2014-11-11 14:17 ` [PATCH 24/25] perf tools: Add Instruction Tracing index Adrian Hunter
2014-11-11 14:17 ` [PATCH 25/25] perf tools: Hit all build ids when Instruction Tracing Adrian Hunter

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=1415715423-15563-16-git-send-email-adrian.hunter@intel.com \
    --to=adrian.hunter@intel.com \
    --cc=acme@kernel.org \
    --cc=dsahern@gmail.com \
    --cc=eranian@google.com \
    --cc=fweisbec@gmail.com \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=namhyung@gmail.com \
    --cc=paulus@samba.org \
    --cc=peterz@infradead.org \
    /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

Powered by JetHome