public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jiri Olsa <jolsa@kernel.org>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: lkml <linux-kernel@vger.kernel.org>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Andi Kleen <ak@linux.intel.com>, David Ahern <dsahern@gmail.com>,
	Ingo Molnar <mingo@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>,
	Stephane Eranian <eranian@google.com>
Subject: [PATCH 02/22] perf tools: Add comm string into struct thread_map
Date: Fri, 26 Jun 2015 11:29:07 +0200	[thread overview]
Message-ID: <1435310967-14570-3-git-send-email-jolsa@kernel.org> (raw)
In-Reply-To: <1435310967-14570-1-git-send-email-jolsa@kernel.org>

Adding support to hold comm name together with pids in
'struct thread_map'. It will be useful for --per-thread
option to display task pid together with task name.

Adding thread_map__read_comms function that reads/set
comm string for the 'struct thread_map'.

Getting the task name from /proc/$pid/comm.

Link: http://lkml.kernel.org/n/tip-pf6bgmbujukce0sgliuhj2f4@git.kernel.org
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 tools/perf/util/python-ext-sources |  1 +
 tools/perf/util/thread_map.c       | 59 ++++++++++++++++++++++++++++++++++++++
 tools/perf/util/thread_map.h       |  8 ++++++
 3 files changed, 68 insertions(+)

diff --git a/tools/perf/util/python-ext-sources b/tools/perf/util/python-ext-sources
index 5925fec90562..e23ded40c79e 100644
--- a/tools/perf/util/python-ext-sources
+++ b/tools/perf/util/python-ext-sources
@@ -20,3 +20,4 @@ util/stat.c
 util/strlist.c
 util/trace-event.c
 ../../lib/rbtree.c
+util/string.c
diff --git a/tools/perf/util/thread_map.c b/tools/perf/util/thread_map.c
index 562e5e2c571c..75afb5be342b 100644
--- a/tools/perf/util/thread_map.c
+++ b/tools/perf/util/thread_map.c
@@ -8,9 +8,11 @@
 #include <unistd.h>
 #include "strlist.h"
 #include <string.h>
+#include <api/fs/fs.h>
 #include "asm/bug.h"
 #include "thread_map.h"
 #include "util.h"
+#include "debug.h"
 
 /* Skip "." and ".." directories */
 static int filter(const struct dirent *dir)
@@ -319,8 +321,12 @@ struct thread_map *thread_map__new_str(const char *pid, const char *tid,
 static void thread_map__delete(struct thread_map *threads)
 {
 	if (threads) {
+		int i;
+
 		WARN_ONCE(atomic_read(&threads->refcnt) != 0,
 			  "thread map refcnt unbalanced\n");
+		for (i = 0; i < threads->nr; i++)
+			free(thread_map__comm(threads, i));
 		free(threads);
 	}
 }
@@ -348,3 +354,56 @@ size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
 
 	return printed + fprintf(fp, "\n");
 }
+
+static int get_comm(char **comm, pid_t pid)
+{
+	char *path;
+	size_t size;
+	int err;
+
+	if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1)
+		return -ENOMEM;
+
+	err = filename__read_str(path, comm, &size);
+	if (!err) {
+		/*
+		 * We're reading 16 bytes, while filename__read_str
+		 * allocates data per BUFSIZ bytes, so we can safely
+		 * mark the end of the string.
+		 */
+		(*comm)[size] = 0;
+		rtrim(*comm);
+	}
+
+	free(path);
+	return err;
+}
+
+static void comm_init(struct thread_map *map, int i)
+{
+	pid_t pid = thread_map__pid(map, i);
+	char *comm = NULL;
+
+	/* dummy pid comm initialization */
+	if (pid == -1) {
+		map->map[i].comm = strdup("dummy");
+		return;
+	}
+
+	/*
+	 * The comm name is like extra bonus ;-),
+	 * so just warn if we fail for any reason.
+	 */
+	if (get_comm(&comm, pid))
+		pr_warning("Couldn't resolve comm name for pid %d\n", pid);
+
+	map->map[i].comm = comm;
+}
+
+void thread_map__read_comms(struct thread_map *threads)
+{
+	int i;
+
+	for (i = 0; i < threads->nr; ++i)
+		comm_init(threads, i);
+}
diff --git a/tools/perf/util/thread_map.h b/tools/perf/util/thread_map.h
index 6b0cd2dc006b..af679d8a50f8 100644
--- a/tools/perf/util/thread_map.h
+++ b/tools/perf/util/thread_map.h
@@ -7,6 +7,7 @@
 
 struct thread_map_data {
 	pid_t    pid;
+	char	*comm;
 };
 
 struct thread_map {
@@ -44,4 +45,11 @@ thread_map__set_pid(struct thread_map *map, int thread, pid_t pid)
 {
 	map->map[thread].pid = pid;
 }
+
+static inline char *thread_map__comm(struct thread_map *map, int thread)
+{
+	return map->map[thread].comm;
+}
+
+void thread_map__read_comms(struct thread_map *threads);
 #endif	/* __PERF_THREAD_MAP_H */
-- 
1.9.3


  parent reply	other threads:[~2015-06-26  9:35 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-26  9:29 [PATCHv5 00/22] perf stat: Introduce --per-thread option Jiri Olsa
2015-06-26  9:29 ` [PATCH 01/22] perf thread_map: Introduce thread_map__reset function Jiri Olsa
2015-06-26 14:06   ` Arnaldo Carvalho de Melo
2015-06-30  4:52   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26  9:29 ` Jiri Olsa [this message]
2015-06-30  4:52   ` [tip:perf/urgent] perf thrad_map: Add comm string into array tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 03/22] perf tests: Add thread_map object tests Jiri Olsa
2015-06-30  4:53   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 04/22] perf stat: Introduce perf_counts function Jiri Olsa
2015-06-30  4:53   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 05/22] perf stat: Use xyarray for cpu evsel counts Jiri Olsa
2015-06-30  4:53   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 06/22] perf stat: Make stats work over the thread dimension Jiri Olsa
2015-06-30  4:54   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 07/22] perf stat: Rename struct perf_counts::cpu member to values Jiri Olsa
2015-06-30  4:54   ` [tip:perf/urgent] perf stat: Rename struct perf_counts:: cpu " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 08/22] perf stat: Introduce perf_evlist__reset_stats Jiri Olsa
2015-06-30  4:54   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 09/22] perf stat: Move perf_evsel__(alloc|free|reset)_stat_priv into stat object Jiri Olsa
2015-06-30  4:54   ` [tip:perf/urgent] perf stat: Move perf_evsel__(alloc|free|reset) _stat_priv " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 10/22] perf stat: Move perf_evsel__(alloc|free)_prev_raw_counts " Jiri Olsa
2015-06-30  4:55   ` [tip:perf/urgent] perf stat: Move perf_evsel__(alloc|free) _prev_raw_counts " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 11/22] perf stat: Move perf_evlist__(alloc|free|reset)_stats " Jiri Olsa
2015-06-30  4:55   ` [tip:perf/urgent] perf stat: Move perf_evlist__(alloc|free|reset) _stats " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 12/22] perf stat: Introduce perf_evsel__alloc_stats function Jiri Olsa
2015-06-30  4:55   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 13/22] perf stat: Introduce perf_evsel__read function Jiri Olsa
2015-06-30  4:56   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 14/22] perf stat: Introduce read_counters function Jiri Olsa
2015-06-30  4:56   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 15/22] perf stat: Separate counters reading and processing Jiri Olsa
2015-06-30  4:56   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 16/22] perf stat: Move zero_per_pkg into counter process code Jiri Olsa
2015-06-30  4:57   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 17/22] perf stat: Move perf_stat initialization " Jiri Olsa
2015-06-30  4:57   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 18/22] perf stat: Remove perf_evsel__read_cb function Jiri Olsa
2015-06-30  4:57   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 19/22] perf stat: Rename print_interval to process_interval Jiri Olsa
2015-06-30  4:57   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 20/22] perf stat: Using init_stats instead of memset Jiri Olsa
2015-06-30  4:58   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 21/22] perf stat: Introduce print_counters function Jiri Olsa
2015-06-30  4:58   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26  9:29 ` [PATCH 22/22] perf stat: Introduce --per-thread option Jiri Olsa
2015-06-30  4:58   ` [tip:perf/urgent] " tip-bot for Jiri Olsa
2015-06-26 15:06 ` [PATCHv5 00/22] " 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=1435310967-14570-3-git-send-email-jolsa@kernel.org \
    --to=jolsa@kernel.org \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=ak@linux.intel.com \
    --cc=dsahern@gmail.com \
    --cc=eranian@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.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