mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jiri Olsa <jolsa@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: Jiri Olsa <jolsa@redhat.com>, Don Zickus <dzickus@redhat.com>,
	Corey Ashford <cjashfor@linux.vnet.ibm.com>,
	David Ahern <dsahern@gmail.com>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Ingo Molnar <mingo@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Paul Mackerras <paulus@samba.org>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>,
	Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Subject: [PATCH 5/6] perf tools: Share process map groups within process threads
Date: Tue, 18 Mar 2014 15:46:55 +0100	[thread overview]
Message-ID: <1395154016-26709-6-git-send-email-jolsa@redhat.com> (raw)
In-Reply-To: <1395154016-26709-1-git-send-email-jolsa@redhat.com>

Sharing map groups within all process threads. This way
there's only one copy of mmap info and it's reachable
from any thread within the process.

Adding reference counting for struct map_groups and
changing the thread map groups get/put interface.

Adding reference counter 'refcnt' into struct map_groups.

Function thread__map_groups_get now:
  - lookup (&create) thread leader and returns its mg,
    allocates one if needed
  - increments reference counter for each copy

Function thread__map_groups_put now:
  - decrease map groups reference counter and sets
    its pointer to NULL
  - frees map groups data if it was last copy

Signed-off-by: Jiri Olsa <jolsa@redhat.com>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Corey Ashford <cjashfor@linux.vnet.ibm.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
---
 tools/perf/util/map.h    |  2 ++
 tools/perf/util/thread.c | 40 +++++++++++++++++++++++++++++++++++-----
 2 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h
index 99a6488..472a686 100644
--- a/tools/perf/util/map.h
+++ b/tools/perf/util/map.h
@@ -59,6 +59,8 @@ struct map_groups {
 	struct rb_root	 maps[MAP__NR_TYPES];
 	struct list_head removed_maps[MAP__NR_TYPES];
 	struct machine	 *machine;
+	/* Used for thread sharing. */
+	int		  refcnt;
 };
 
 static inline struct kmap *map__kmap(struct map *map)
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index cbe451a..f34fda1 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -130,6 +130,13 @@ int thread__insert_map(struct thread *thread, struct map *map)
 	return 0;
 }
 
+static struct thread *thread__get_leader(struct thread *thread)
+{
+	pid_t pid = thread->pid_;
+
+	return machine__findnew_thread(thread->machine, pid, pid);
+}
+
 static struct map_groups *thread__map_groups_alloc(struct thread *thread)
 {
 	struct map_groups *mg = zalloc(sizeof(*mg));
@@ -137,6 +144,7 @@ static struct map_groups *thread__map_groups_alloc(struct thread *thread)
 	if (mg) {
 		map_groups__init(mg);
 		thread->mg = mg;
+		mg->refcnt = 1;
 	}
 
 	return mg;
@@ -146,17 +154,39 @@ struct map_groups *thread__map_groups_get(struct thread *thread)
 {
 	struct map_groups *mg = thread->mg;
 
-	if (!mg)
-		mg = thread__map_groups_alloc(thread);
+	if (!mg) {
+		struct thread *leader = thread__get_leader(thread);
+
+		if (!leader)
+			return NULL;
+
+		if (leader->mg)
+			mg = leader->mg;
+		else
+			mg = thread__map_groups_alloc(leader);
+
+		if (leader != thread) {
+			thread->mg = mg;
+			mg->refcnt++;
+		}
+	}
 
 	return mg;
 }
 
 void thread__map_groups_put(struct thread *thread)
 {
-	if (thread->mg) {
-		map_groups__exit(thread->mg);
-		zfree(&thread->mg);
+	struct map_groups *mg = thread->mg;
+
+	if (mg) {
+		BUG_ON(!mg->refcnt);
+
+		if (!--mg->refcnt) {
+			map_groups__exit(thread->mg);
+			free(thread->mg);
+		}
+
+		thread->mg = NULL;
 	}
 }
 
-- 
1.8.3.1


  parent reply	other threads:[~2014-03-18 15:56 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-18 14:46 [PATCH 0/6] perf tools: Share map groups within process Jiri Olsa
2014-03-18 14:46 ` [PATCH 1/6] perf tests: Add thread maps lookup automated tests Jiri Olsa
2014-03-18 14:46 ` [PATCH 2/6] perf tools: Remove thread__find_map function Jiri Olsa
2014-03-19 13:08   ` [tip:perf/core] " tip-bot for Jiri Olsa
2014-03-18 14:46 ` [PATCH 3/6] perf tools: Allocate thread map_groups dynamically Jiri Olsa
2014-03-18 14:46 ` [PATCH 4/6] perf tools: Add machine pointer into thread struct Jiri Olsa
2014-03-18 14:46 ` Jiri Olsa [this message]
2014-03-18 14:46 ` [PATCH 6/6] perf tests: Add map groups sharing with thread object test Jiri Olsa
2014-03-18 19:14 ` [PATCH 0/6] perf tools: Share map groups within process Don Zickus
2014-03-19  0:46 ` Namhyung Kim
2014-03-20  8:33 ` Jiri Olsa

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=1395154016-26709-6-git-send-email-jolsa@redhat.com \
    --to=jolsa@redhat.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@ghostprotocols.net \
    --cc=cjashfor@linux.vnet.ibm.com \
    --cc=dsahern@gmail.com \
    --cc=dzickus@redhat.com \
    --cc=fweisbec@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=paulus@samba.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

all inboxes | Powered by JetHome®