public inbox for linux-kernel@vger.kernel.org
 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 V4 14/23] perf itrace: Add a hashtable for caching decoded instructions
Date: Thu,  8 Jan 2015 14:52:18 +0200	[thread overview]
Message-ID: <1420721547-26470-15-git-send-email-adrian.hunter@intel.com> (raw)
In-Reply-To: <1420721547-26470-1-git-send-email-adrian.hunter@intel.com>

Decoding Instruction Trace data may involve walking object
code.  Rather than repetitively decoding the same instructions,
a cache can be used to cache the results.

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

diff --git a/tools/perf/util/itrace.c b/tools/perf/util/itrace.c
index c3b0993..bdf19d2 100644
--- a/tools/perf/util/itrace.c
+++ b/tools/perf/util/itrace.c
@@ -39,6 +39,8 @@
 #include "thread_map.h"
 #include "itrace.h"
 
+#include <linux/hash.h>
+
 #include "event.h"
 #include "session.h"
 #include "debug.h"
@@ -919,3 +921,124 @@ int itrace_mmap__read(struct itrace_mmap *mm, struct itrace_record *itr,
 
 	return 1;
 }
+
+/**
+ * struct itrace_cache - hash table to cache decoded instruction blocks
+ * @hashtable: the hashtable
+ * @sz: hashtable size (number of hlists)
+ * @entry_size: size of an entry
+ * @limit: limit the number of entries to this maximum, when reached the cache
+ *         is dropped and caching begins again with an empty cache
+ * @cnt: current number of entries
+ * @bits: hashtable size (@sz = 2^@bits)
+ */
+struct itrace_cache {
+	struct hlist_head *hashtable;
+	size_t sz;
+	size_t entry_size;
+	size_t limit;
+	size_t cnt;
+	unsigned int bits;
+};
+
+struct itrace_cache *itrace_cache__new(unsigned int bits, size_t entry_size,
+				       unsigned int limit_percent)
+{
+	struct itrace_cache *c;
+	struct hlist_head *ht;
+	size_t sz, i;
+
+	c = zalloc(sizeof(struct itrace_cache));
+	if (!c)
+		return NULL;
+
+	sz = 1UL << bits;
+
+	ht = calloc(sz, sizeof(struct hlist_head));
+	if (!ht)
+		goto out_free;
+
+	for (i = 0; i < sz; i++)
+		INIT_HLIST_HEAD(&ht[i]);
+
+	c->hashtable = ht;
+	c->sz = sz;
+	c->entry_size = entry_size;
+	c->limit = (c->sz * limit_percent) / 100;
+	c->bits = bits;
+
+	return c;
+
+out_free:
+	free(c);
+	return NULL;
+}
+
+static void itrace_cache__drop(struct itrace_cache *c)
+{
+	struct itrace_cache_entry *entry;
+	struct hlist_node *tmp;
+	size_t i;
+
+	if (!c)
+		return;
+
+	for (i = 0; i < c->sz; i++) {
+		hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
+			hlist_del(&entry->hash);
+			itrace_cache__free_entry(c, entry);
+		}
+	}
+
+	c->cnt = 0;
+}
+
+void itrace_cache__free(struct itrace_cache *c)
+{
+	if (!c)
+		return;
+
+	itrace_cache__drop(c);
+	free(c->hashtable);
+	free(c);
+}
+
+void *itrace_cache__alloc_entry(struct itrace_cache *c)
+{
+	return malloc(c->entry_size);
+}
+
+void itrace_cache__free_entry(struct itrace_cache *c __maybe_unused,
+			      void *entry)
+{
+	free(entry);
+}
+
+int itrace_cache__add(struct itrace_cache *c, u32 key,
+		      struct itrace_cache_entry *entry)
+{
+	if (c->limit && ++c->cnt > c->limit)
+		itrace_cache__drop(c);
+
+	entry->key = key;
+	hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
+
+	return 0;
+}
+
+void *itrace_cache__lookup(struct itrace_cache *c, u32 key)
+{
+	struct itrace_cache_entry *entry;
+	struct hlist_head *hlist;
+
+	if (!c)
+		return NULL;
+
+	hlist = &c->hashtable[hash_32(key, c->bits)];
+	hlist_for_each_entry(entry, hlist, hash) {
+		if (entry->key == key)
+			return entry;
+	}
+
+	return NULL;
+}
diff --git a/tools/perf/util/itrace.h b/tools/perf/util/itrace.h
index 08247d5..3bd899e 100644
--- a/tools/perf/util/itrace.h
+++ b/tools/perf/util/itrace.h
@@ -316,6 +316,20 @@ int itrace_heap__add(struct itrace_heap *heap, unsigned int queue_nr,
 void itrace_heap__pop(struct itrace_heap *heap);
 void itrace_heap__free(struct itrace_heap *heap);
 
+struct itrace_cache_entry {
+	struct hlist_node hash;
+	u32 key;
+};
+
+struct itrace_cache *itrace_cache__new(unsigned int bits, size_t entry_size,
+				       unsigned int limit_percent);
+void itrace_cache__free(struct itrace_cache *itrace_cache);
+void *itrace_cache__alloc_entry(struct itrace_cache *c);
+void itrace_cache__free_entry(struct itrace_cache *c, void *entry);
+int itrace_cache__add(struct itrace_cache *c, u32 key,
+		      struct itrace_cache_entry *entry);
+void *itrace_cache__lookup(struct itrace_cache *c, u32 key);
+
 struct itrace_record *itrace_record__init(struct perf_evlist *evlist, int *err);
 
 int itrace_record__options(struct itrace_record *itr,
-- 
1.9.1


  parent reply	other threads:[~2015-01-08 12:55 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-08 12:52 [PATCH V4 00/23] perf tools: Introduce an abstraction for Instruction Tracing Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 01/23] perf header: Add Instruction Tracing feature Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 02/23] perf evlist: Add initial support for mmapping an Instruction Trace buffer Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 03/23] perf tools: Add user events for Instruction Tracing Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 04/23] perf tools: Add support for Instruction Trace recording Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 05/23] perf record: Add basic Instruction Tracing support Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 06/23] perf record: Extend -m option for Instruction Tracing mmap pages Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 07/23] perf tools: Add a user event for Instruction Tracing errors Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 08/23] perf session: Add hooks to allow transparent decoding of Instruction Tracing data Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 09/23] perf session: Add Instruction Tracing options Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 10/23] perf itrace: Add helpers for Instruction Tracing errors Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 11/23] perf itrace: Add helpers for queuing Instruction Tracing data Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 12/23] perf itrace: Add a heap for sorting Instruction Tracing queues Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 13/23] perf itrace: Add processing for Instruction Tracing events Adrian Hunter
2015-01-08 12:52 ` Adrian Hunter [this message]
2015-01-08 12:52 ` [PATCH V4 15/23] perf tools: Add member to struct dso for an instruction cache Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 16/23] perf script: Add Instruction Tracing support Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 17/23] perf script: Always allow fields 'addr' and 'cpu' for itrace Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 18/23] perf report: Add Instruction Tracing support Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 19/23] perf inject: Re-pipe Instruction Tracing events Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 20/23] perf inject: Add Instruction Tracing support Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 21/23] perf tools: Add Instruction Tracing index Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 22/23] perf tools: Hit all build ids when Instruction Tracing Adrian Hunter
2015-01-08 12:52 ` [PATCH V4 23/23] perf tools: Add build option NO_ITRACE to exclude " Adrian Hunter
2015-02-10 12:31 ` [PATCH V4 00/23] perf tools: Introduce an abstraction for " Adrian Hunter
2015-02-11 20:40   ` 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=1420721547-26470-15-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