mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 10:40:15 +02:00 
			
		
		
		
	perf thrad_map: Add comm string into array
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. Signed-off-by: Jiri Olsa <jolsa@kernel.org> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Stephane Eranian <eranian@google.com> Link: http://lkml.kernel.org/r/1435310967-14570-3-git-send-email-jolsa@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
		
							parent
							
								
									62eea46438
								
							
						
					
					
						commit
						792402fd5c
					
				
					 3 changed files with 68 additions and 0 deletions
				
			
		| 
						 | 
					@ -20,3 +20,4 @@ util/stat.c
 | 
				
			||||||
util/strlist.c
 | 
					util/strlist.c
 | 
				
			||||||
util/trace-event.c
 | 
					util/trace-event.c
 | 
				
			||||||
../../lib/rbtree.c
 | 
					../../lib/rbtree.c
 | 
				
			||||||
 | 
					util/string.c
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,9 +8,11 @@
 | 
				
			||||||
#include <unistd.h>
 | 
					#include <unistd.h>
 | 
				
			||||||
#include "strlist.h"
 | 
					#include "strlist.h"
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					#include <api/fs/fs.h>
 | 
				
			||||||
#include "asm/bug.h"
 | 
					#include "asm/bug.h"
 | 
				
			||||||
#include "thread_map.h"
 | 
					#include "thread_map.h"
 | 
				
			||||||
#include "util.h"
 | 
					#include "util.h"
 | 
				
			||||||
 | 
					#include "debug.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Skip "." and ".." directories */
 | 
					/* Skip "." and ".." directories */
 | 
				
			||||||
static int filter(const struct dirent *dir)
 | 
					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)
 | 
					static void thread_map__delete(struct thread_map *threads)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (threads) {
 | 
						if (threads) {
 | 
				
			||||||
 | 
							int i;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		WARN_ONCE(atomic_read(&threads->refcnt) != 0,
 | 
							WARN_ONCE(atomic_read(&threads->refcnt) != 0,
 | 
				
			||||||
			  "thread map refcnt unbalanced\n");
 | 
								  "thread map refcnt unbalanced\n");
 | 
				
			||||||
 | 
							for (i = 0; i < threads->nr; i++)
 | 
				
			||||||
 | 
								free(thread_map__comm(threads, i));
 | 
				
			||||||
		free(threads);
 | 
							free(threads);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -348,3 +354,56 @@ size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return printed + fprintf(fp, "\n");
 | 
						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);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,6 +7,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct thread_map_data {
 | 
					struct thread_map_data {
 | 
				
			||||||
	pid_t    pid;
 | 
						pid_t    pid;
 | 
				
			||||||
 | 
						char	*comm;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct thread_map {
 | 
					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;
 | 
						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 */
 | 
					#endif	/* __PERF_THREAD_MAP_H */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue