Browse Source

perf thread_map: Don't access the array entries directly

Instead provide a method to set the array entries, and another to access
the contents.

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/1435012588-9007-2-git-send-email-jolsa@kernel.org
[ Split providing the set/get accessors from transforming the entries structs ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Jiri Olsa 10 years ago
parent
commit
e13798c77b

+ 2 - 2
tools/perf/builtin-trace.c

@@ -2325,7 +2325,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
 	 */
 	 */
 	if (trace->filter_pids.nr > 0)
 	if (trace->filter_pids.nr > 0)
 		err = perf_evlist__set_filter_pids(evlist, trace->filter_pids.nr, trace->filter_pids.entries);
 		err = perf_evlist__set_filter_pids(evlist, trace->filter_pids.nr, trace->filter_pids.entries);
-	else if (evlist->threads->map[0] == -1)
+	else if (thread_map__pid(evlist->threads, 0) == -1)
 		err = perf_evlist__set_filter_pid(evlist, getpid());
 		err = perf_evlist__set_filter_pid(evlist, getpid());
 
 
 	if (err < 0) {
 	if (err < 0) {
@@ -2343,7 +2343,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
 	if (forks)
 	if (forks)
 		perf_evlist__start_workload(evlist);
 		perf_evlist__start_workload(evlist);
 
 
-	trace->multiple_threads = evlist->threads->map[0] == -1 ||
+	trace->multiple_threads = thread_map__pid(evlist->threads, 0) == -1 ||
 				  evlist->threads->nr > 1 ||
 				  evlist->threads->nr > 1 ||
 				  perf_evlist__first(evlist)->attr.inherit;
 				  perf_evlist__first(evlist)->attr.inherit;
 again:
 again:

+ 1 - 1
tools/perf/tests/openat-syscall-tp-fields.c

@@ -45,7 +45,7 @@ int test__syscall_openat_tp_fields(void)
 
 
 	perf_evsel__config(evsel, &opts);
 	perf_evsel__config(evsel, &opts);
 
 
-	evlist->threads->map[0] = getpid();
+	thread_map__set_pid(evlist->threads, 0, getpid());
 
 
 	err = perf_evlist__open(evlist);
 	err = perf_evlist__open(evlist);
 	if (err < 0) {
 	if (err < 0) {

+ 2 - 2
tools/perf/util/auxtrace.c

@@ -119,12 +119,12 @@ void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
 	if (per_cpu) {
 	if (per_cpu) {
 		mp->cpu = evlist->cpus->map[idx];
 		mp->cpu = evlist->cpus->map[idx];
 		if (evlist->threads)
 		if (evlist->threads)
-			mp->tid = evlist->threads->map[0];
+			mp->tid = thread_map__pid(evlist->threads, 0);
 		else
 		else
 			mp->tid = -1;
 			mp->tid = -1;
 	} else {
 	} else {
 		mp->cpu = -1;
 		mp->cpu = -1;
-		mp->tid = evlist->threads->map[idx];
+		mp->tid = thread_map__pid(evlist->threads, idx);
 	}
 	}
 }
 }
 
 

+ 3 - 3
tools/perf/util/event.c

@@ -504,7 +504,7 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,
 	for (thread = 0; thread < threads->nr; ++thread) {
 	for (thread = 0; thread < threads->nr; ++thread) {
 		if (__event__synthesize_thread(comm_event, mmap_event,
 		if (__event__synthesize_thread(comm_event, mmap_event,
 					       fork_event,
 					       fork_event,
-					       threads->map[thread], 0,
+					       thread_map__pid(threads, thread), 0,
 					       process, tool, machine,
 					       process, tool, machine,
 					       mmap_data, proc_map_timeout)) {
 					       mmap_data, proc_map_timeout)) {
 			err = -1;
 			err = -1;
@@ -515,12 +515,12 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,
 		 * comm.pid is set to thread group id by
 		 * comm.pid is set to thread group id by
 		 * perf_event__synthesize_comm
 		 * perf_event__synthesize_comm
 		 */
 		 */
-		if ((int) comm_event->comm.pid != threads->map[thread]) {
+		if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
 			bool need_leader = true;
 			bool need_leader = true;
 
 
 			/* is thread group leader in thread_map? */
 			/* is thread group leader in thread_map? */
 			for (j = 0; j < threads->nr; ++j) {
 			for (j = 0; j < threads->nr; ++j) {
-				if ((int) comm_event->comm.pid == threads->map[j]) {
+				if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
 					need_leader = false;
 					need_leader = false;
 					break;
 					break;
 				}
 				}

+ 2 - 2
tools/perf/util/evlist.c

@@ -548,7 +548,7 @@ static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
 	else
 	else
 		sid->cpu = -1;
 		sid->cpu = -1;
 	if (!evsel->system_wide && evlist->threads && thread >= 0)
 	if (!evsel->system_wide && evlist->threads && thread >= 0)
-		sid->tid = evlist->threads->map[thread];
+		sid->tid = thread_map__pid(evlist->threads, thread);
 	else
 	else
 		sid->tid = -1;
 		sid->tid = -1;
 }
 }
@@ -1475,7 +1475,7 @@ int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *tar
 				__func__, __LINE__);
 				__func__, __LINE__);
 			goto out_close_pipes;
 			goto out_close_pipes;
 		}
 		}
-		evlist->threads->map[0] = evlist->workload.pid;
+		thread_map__set_pid(evlist->threads, 0, evlist->workload.pid);
 	}
 	}
 
 
 	close(child_ready_pipe[1]);
 	close(child_ready_pipe[1]);

+ 1 - 1
tools/perf/util/evsel.c

@@ -1167,7 +1167,7 @@ retry_sample_id:
 			int group_fd;
 			int group_fd;
 
 
 			if (!evsel->cgrp && !evsel->system_wide)
 			if (!evsel->cgrp && !evsel->system_wide)
-				pid = threads->map[thread];
+				pid = thread_map__pid(threads, thread);
 
 
 			group_fd = get_group_fd(evsel, cpu, thread);
 			group_fd = get_group_fd(evsel, cpu, thread);
 retry_open:
 retry_open:

+ 13 - 11
tools/perf/util/thread_map.c

@@ -45,7 +45,7 @@ struct thread_map *thread_map__new_by_pid(pid_t pid)
 	threads = thread_map__alloc(items);
 	threads = thread_map__alloc(items);
 	if (threads != NULL) {
 	if (threads != NULL) {
 		for (i = 0; i < items; i++)
 		for (i = 0; i < items; i++)
-			threads->map[i] = atoi(namelist[i]->d_name);
+			thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
 		threads->nr = items;
 		threads->nr = items;
 	}
 	}
 
 
@@ -61,8 +61,8 @@ struct thread_map *thread_map__new_by_tid(pid_t tid)
 	struct thread_map *threads = thread_map__alloc(1);
 	struct thread_map *threads = thread_map__alloc(1);
 
 
 	if (threads != NULL) {
 	if (threads != NULL) {
-		threads->map[0] = tid;
-		threads->nr	= 1;
+		thread_map__set_pid(threads, 0, tid);
+		threads->nr = 1;
 	}
 	}
 
 
 	return threads;
 	return threads;
@@ -123,8 +123,10 @@ struct thread_map *thread_map__new_by_uid(uid_t uid)
 			threads = tmp;
 			threads = tmp;
 		}
 		}
 
 
-		for (i = 0; i < items; i++)
-			threads->map[threads->nr + i] = atoi(namelist[i]->d_name);
+		for (i = 0; i < items; i++) {
+			thread_map__set_pid(threads, threads->nr + i,
+					    atoi(namelist[i]->d_name));
+		}
 
 
 		for (i = 0; i < items; i++)
 		for (i = 0; i < items; i++)
 			zfree(&namelist[i]);
 			zfree(&namelist[i]);
@@ -201,7 +203,7 @@ static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
 		threads = nt;
 		threads = nt;
 
 
 		for (i = 0; i < items; i++) {
 		for (i = 0; i < items; i++) {
-			threads->map[j++] = atoi(namelist[i]->d_name);
+			thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
 			zfree(&namelist[i]);
 			zfree(&namelist[i]);
 		}
 		}
 		threads->nr = total_tasks;
 		threads->nr = total_tasks;
@@ -227,8 +229,8 @@ struct thread_map *thread_map__new_dummy(void)
 	struct thread_map *threads = thread_map__alloc(1);
 	struct thread_map *threads = thread_map__alloc(1);
 
 
 	if (threads != NULL) {
 	if (threads != NULL) {
-		threads->map[0]	= -1;
-		threads->nr	= 1;
+		thread_map__set_pid(threads, 0, -1);
+		threads->nr = 1;
 	}
 	}
 	return threads;
 	return threads;
 }
 }
@@ -267,8 +269,8 @@ static struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
 			goto out_free_threads;
 			goto out_free_threads;
 
 
 		threads = nt;
 		threads = nt;
-		threads->map[ntasks - 1] = tid;
-		threads->nr		 = ntasks;
+		thread_map__set_pid(threads, ntasks - 1, tid);
+		threads->nr = ntasks;
 	}
 	}
 out:
 out:
 	return threads;
 	return threads;
@@ -301,7 +303,7 @@ size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
 	size_t printed = fprintf(fp, "%d thread%s: ",
 	size_t printed = fprintf(fp, "%d thread%s: ",
 				 threads->nr, threads->nr > 1 ? "s" : "");
 				 threads->nr, threads->nr > 1 ? "s" : "");
 	for (i = 0; i < threads->nr; ++i)
 	for (i = 0; i < threads->nr; ++i)
-		printed += fprintf(fp, "%s%d", i ? ", " : "", threads->map[i]);
+		printed += fprintf(fp, "%s%d", i ? ", " : "", thread_map__pid(threads, i));
 
 
 	return printed + fprintf(fp, "\n");
 	return printed + fprintf(fp, "\n");
 }
 }

+ 10 - 0
tools/perf/util/thread_map.h

@@ -27,4 +27,14 @@ static inline int thread_map__nr(struct thread_map *threads)
 	return threads ? threads->nr : 1;
 	return threads ? threads->nr : 1;
 }
 }
 
 
+static inline pid_t thread_map__pid(struct thread_map *map, int thread)
+{
+	return map->map[thread];
+}
+
+static inline void
+thread_map__set_pid(struct thread_map *map, int thread, pid_t pid)
+{
+	map->map[thread] = pid;
+}
 #endif	/* __PERF_THREAD_MAP_H */
 #endif	/* __PERF_THREAD_MAP_H */