From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755972AbbFOTRi (ORCPT ); Mon, 15 Jun 2015 15:17:38 -0400 Received: from mail.kernel.org ([198.145.29.136]:52573 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751948AbbFOTRc (ORCPT ); Mon, 15 Jun 2015 15:17:32 -0400 Date: Mon, 15 Jun 2015 16:17:07 -0300 From: Arnaldo Carvalho de Melo To: Jiri Olsa Cc: lkml , Adrian Hunter , Andi Kleen , David Ahern , Ingo Molnar , Namhyung Kim , Peter Zijlstra , Stephane Eranian Subject: Re: [PATCH 04/30] perf tools: Add comm string into struct thread_map Message-ID: <20150615191707.GD5845@kernel.org> References: <1434269985-521-1-git-send-email-jolsa@kernel.org> <1434269985-521-5-git-send-email-jolsa@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1434269985-521-5-git-send-email-jolsa@kernel.org> X-Url: http://acmel.wordpress.com User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Em Sun, Jun 14, 2015 at 10:19:19AM +0200, Jiri Olsa escreveu: > Adding support to hold comm name together with pids in > 'struct thread_map'. It will be useful for --per-task > option to display task pid together with task name. > > Getting the task name from /proc/$pid/comm. Humm, so you want to extend the thread_map, that was just for pids, to have some more info about the thread, if that is the case, why not synthesize the whole thread, and have a pointer to 'struct thread', that is the representation for a thread in perf instead of creating a new, partial thing like 'thread_map_data'? You wouldn't have to use the existing synthesizing code if that seems to overkill, i.e. instead of having 'pid_t' as the array element in a thread_map, you would have 'struct thread', i.e. the whole shebang, but would just set what you need now, i.e. thread->pid and thread->comm, but later could do the rest instead of recreating it in that thread_map_data :-) I.e. besides --per-task, don't you want --per-pid, --per-thread, or other stuff that will use other thread characteristics? That or rename that 'thread_map_data' to something like mini_thread, ...}. Got carried away, but that was the reaction to that vague name 'thread_map_data' 8-) Anyway, applied the first two, looking at the rest... - Arnaldo > Link: http://lkml.kernel.org/n/tip-pf6bgmbujukce0sgliuhj2f4@git.kernel.org > Signed-off-by: Jiri Olsa > --- > tools/perf/util/python-ext-sources | 1 + > tools/perf/util/thread_map.c | 63 +++++++++++++++++++++++++++++++++++--- > tools/perf/util/thread_map.h | 2 ++ > 3 files changed, 62 insertions(+), 4 deletions(-) > > diff --git a/tools/perf/util/python-ext-sources b/tools/perf/util/python-ext-sources > index 4d28624a1eca..55ba8968a263 100644 > --- a/tools/perf/util/python-ext-sources > +++ b/tools/perf/util/python-ext-sources > @@ -19,3 +19,4 @@ util/rblist.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 7f03f9facfdd..f7a49a693d27 100644 > --- a/tools/perf/util/thread_map.c > +++ b/tools/perf/util/thread_map.c > @@ -8,8 +8,10 @@ > #include > #include "strlist.h" > #include > +#include > #include "thread_map.h" > #include "util.h" > +#include "debug.h" > > /* Skip "." and ".." directories */ > static int filter(const struct dirent *dir) > @@ -29,6 +31,44 @@ static struct thread_map *thread_map__realloc(struct thread_map *map, int nr) > > #define thread_map__alloc(__nr) thread_map__realloc(NULL, __nr) > > +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) > + rtrim(*comm); > + > + free(path); > + return err; > +} > + > +static void comm_init(struct thread_map *map, int i) > +{ > + struct thread_map_data *data = &map->map[i]; > + > + /* > + * The comm name is like extra bonus ;-), > + * so just warn if we fail for any reason. > + */ > + data->comm = NULL; > + > + /* dummy pid comm initialization */ > + if (data->pid == -1) { > + data->comm = strdup("dummy"); > + return; > + } > + > + /* try to get pid's comm string */ > + if (get_comm(&data->comm, data->pid)) > + pr_warning("Couldn't resolve comm name for pid %d\n", data->pid); > +} > + > struct thread_map *thread_map__new_by_pid(pid_t pid) > { > struct thread_map *threads; > @@ -44,8 +84,10 @@ struct thread_map *thread_map__new_by_pid(pid_t pid) > > threads = thread_map__alloc(items); > if (threads != NULL) { > - for (i = 0; i < items; i++) > + for (i = 0; i < items; i++) { > thread_map__pid(threads, i) = atoi(namelist[i]->d_name); > + comm_init(threads, i); > + } > threads->nr = items; > } > > @@ -63,6 +105,7 @@ struct thread_map *thread_map__new_by_tid(pid_t tid) > if (threads != NULL) { > thread_map__pid(threads, 0) = tid; > threads->nr = 1; > + comm_init(threads, 0); > } > > return threads; > @@ -123,8 +166,10 @@ struct thread_map *thread_map__new_by_uid(uid_t uid) > threads = tmp; > } > > - for (i = 0; i < items; i++) > + for (i = 0; i < items; i++) { > thread_map__pid(threads, threads->nr + i) = atoi(namelist[i]->d_name); > + comm_init(threads, threads->nr + i); > + } > > for (i = 0; i < items; i++) > zfree(&namelist[i]); > @@ -200,8 +245,9 @@ static struct thread_map *thread_map__new_by_pid_str(const char *pid_str) > > threads = nt; > > - for (i = 0; i < items; i++) { > - thread_map__pid(threads, j++) = atoi(namelist[i]->d_name); > + for (i = 0; i < items; i++, j++) { > + thread_map__pid(threads, j) = atoi(namelist[i]->d_name); > + comm_init(threads, j); > zfree(&namelist[i]); > } > threads->nr = total_tasks; > @@ -229,6 +275,7 @@ struct thread_map *thread_map__new_dummy(void) > if (threads != NULL) { > thread_map__pid(threads, 0) = -1; > threads->nr = 1; > + comm_init(threads, 0); > } > return threads; > } > @@ -269,6 +316,7 @@ static struct thread_map *thread_map__new_by_tid_str(const char *tid_str) > threads = nt; > thread_map__pid(threads, ntasks - 1) = tid; > threads->nr = ntasks; > + comm_init(threads, ntasks - 1); > } > out: > return threads; > @@ -292,6 +340,13 @@ struct thread_map *thread_map__new_str(const char *pid, const char *tid, > > void thread_map__delete(struct thread_map *threads) > { > + int i; > + > + if (threads) { > + for (i = 0; i < threads->nr; i++) > + free(thread_map__comm(threads, i)); > + } > + > free(threads); > } > > diff --git a/tools/perf/util/thread_map.h b/tools/perf/util/thread_map.h > index 9377850c7b71..6933f9d316d9 100644 > --- a/tools/perf/util/thread_map.h > +++ b/tools/perf/util/thread_map.h > @@ -6,6 +6,7 @@ > > struct thread_map_data { > pid_t pid; > + char *comm; > }; > > struct thread_map { > @@ -14,6 +15,7 @@ struct thread_map { > }; > > #define thread_map__pid(__m, __t) __m->map[__t].pid > +#define thread_map__comm(__m, __t) __m->map[__t].comm > > struct thread_map *thread_map__new_dummy(void); > struct thread_map *thread_map__new_by_pid(pid_t pid); > -- > 1.9.3