Skip to contentSkip to conversation

Perf top: Add ability to detect new threads dynamically during 'perf top -p 'pid'' is running

PATCH In review chenggang qin 2 participants 2 messages Aug 22, 2012 – Sep 4, 2012 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Perf top: Add ability to detect new threads dynamically during 'perf top -p 'pid'' is running

From: Chenggang Qin <[email protected]>

While we use "perf top -p 'pid'" to monitor the symbols of specified processes, some new threads would be created by the monitored processes during "perf top" is running. In current version, these new threads and their symbols cannot be shown.
This patch add ability to show these new threads.

---
 tools/perf/builtin-top.c |   86 ++++++++++++++++++++++++++++++++++++++++++++--
 tools/perf/util/evlist.c |    2 ++
 2 files changed, 85 insertions(+), 3 deletions(-)
tools/perf/builtin-top.c +83 -3
index 68cd61e..54c9cc1 100644
@@ -882,7 +882,7 @@ static void perf_top__mmap_read(struct perf_top *top)
 		perf_top__mmap_read_idx(top, i);
 }
 
-static void perf_top__start_counters(struct perf_top *top)
+static int perf_top__start_counters(struct perf_top *top)
 {
 	struct perf_evsel *counter, *first;
 	struct perf_evlist *evlist = top->evlist;
@@ -929,6 +929,10 @@ try_again:
 				     group_fd) < 0) {
 			int err = errno;
 
+			if (err == ESRCH) {
+				return err;
+			}
+
 			if (err == EPERM || err == EACCES) {
 				ui__error_paranoid();
 				goto out_err;
@@ -994,7 +998,7 @@ try_again:
 		goto out_err;
 	}
 
-	return;
+	return 0;
 
 out_err:
 	exit_browser(0);
@@ -1018,6 +1022,77 @@ static int perf_top__setup_sample_type(struct perf_top *top)
 	return 0;
 }
 
+static int thread_map_cmp(struct thread_map *threads_a,
+			  struct thread_map *threads_b)
+{
+	int i, j;
+
+	if (threads_a->nr != threads_b->nr) {
+		return 1;
+	} else {
+		for (i = 0; i < threads_b->nr; i++) {
+			for (j = 0; j < threads_a->nr; j++)
+				if (threads_b->map[i] == threads_a->map[j])
+					break;
+
+			if (j == threads_a->nr)
+				return 1;
+		}
+
+		return 0;
+	}
+}
+
+static void check_new_threads(struct perf_top *top)
+{
+	struct thread_map *new_thread_map;
+	struct perf_evsel *counter;
+	struct perf_evlist *evlist = top->evlist;
+
+retry:
+	new_thread_map = thread_map__new_str(top->target.pid, top->target.tid,
+					     top->target.uid);
+	if (!new_thread_map)
+		return;
+
+	if (thread_map_cmp(top->evlist->threads, new_thread_map) == 0) {
+		free(new_thread_map);
+		return;
+	} else {
+		list_for_each_entry(counter, &evlist->entries, node) {
+			perf_evsel__close(counter, top->evlist->cpus->nr,
+					  top->evlist->threads->nr);
+		}
+
+		if (top->evlist->mmap)
+			perf_evlist__munmap(top->evlist);
+
+		if (top->evlist->pollfd) {
+			free(top->evlist->pollfd);
+			top->evlist->pollfd = NULL;
+		}
+
+		top->evlist->nr_fds = 0;
+
+		thread_map__delete(top->evlist->threads);
+		top->evlist->threads = new_thread_map;
+
+		if (perf_top__start_counters(top) == ESRCH) {
+			while (thread_map_cmp(top->evlist->threads,
+					      new_thread_map) == 0) {
+				new_thread_map = thread_map__new_str(top->target.pid,
+								     top->target.tid,
+								     top->target.uid);
+				if (!new_thread_map)
+					return;
+			}
+			goto retry;
+		}
+
+		return;
+	}
+}
+
 static int __cmd_top(struct perf_top *top)
 {
 	pthread_t thread;
@@ -1067,7 +1142,12 @@ static int __cmd_top(struct perf_top *top)
 	}
 
 	while (1) {
-		u64 hits = top->samples;
+		u64 hits;
+
+		if (perf_target__has_task(&top->target))
+			check_new_threads(top);
+
+		hits = top->samples;
 
 		perf_top__mmap_read(top);
 
tools/perf/util/evlist.c +2 -0
index 9b38681..293eca7 100644
@@ -452,6 +452,8 @@ void perf_evlist__munmap(struct perf_evlist *evlist)
 
 	free(evlist->mmap);
 	evlist->mmap = NULL;
+
+	evlist->nr_mmaps = 0;
 }
 
 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Signature · 1 line
1.7.9.7
#1 lore raw
Quoted 113 lines · chenggang qin

From: Chenggang Qin <[email protected]>

While we use "perf top -p 'pid'" to monitor the symbols of specified processes, some new threads would be created by the monitored processes during "perf top" is running. In current version, these new threads and their symbols cannot be shown.
This patch add ability to show these new threads.

---
  tools/perf/builtin-top.c |   86 ++++++++++++++++++++++++++++++++++++++++++++--
  tools/perf/util/evlist.c |    2 ++
  2 files changed, 85 insertions(+), 3 deletions(-)
tools/perf/builtin-top.c +67 -2
index 68cd61e..54c9cc1 100644
@@ -882,7 +882,7 @@ static void perf_top__mmap_read(struct perf_top *top)
  		perf_top__mmap_read_idx(top, i);
  }
 
-static void perf_top__start_counters(struct perf_top *top)
+static int perf_top__start_counters(struct perf_top *top)
  {
  	struct perf_evsel *counter, *first;
  	struct perf_evlist *evlist = top->evlist;
@@ -929,6 +929,10 @@ try_again:
  				     group_fd) < 0) {
  			int err = errno;
 
+			if (err == ESRCH) {
+				return err;
+			}
+
  			if (err == EPERM || err == EACCES) {
  				ui__error_paranoid();
  				goto out_err;
@@ -994,7 +998,7 @@ try_again:
  		goto out_err;
  	}
 
-	return;
+	return 0;
 
  out_err:
  	exit_browser(0);
@@ -1018,6 +1022,77 @@ static int perf_top__setup_sample_type(struct perf_top *top)
  	return 0;
  }
 
+static int thread_map_cmp(struct thread_map *threads_a,
+			  struct thread_map *threads_b)
+{
+	int i, j;
+
+	if (threads_a->nr != threads_b->nr) {
+		return 1;
+	} else {
+		for (i = 0; i < threads_b->nr; i++) {
+			for (j = 0; j < threads_a->nr; j++)
+				if (threads_b->map[i] == threads_a->map[j])
+					break;
+
+			if (j == threads_a->nr)
+				return 1;
+		}
+
+		return 0;
+	}
+}
+
+static void check_new_threads(struct perf_top *top)
+{
+	struct thread_map *new_thread_map;
+	struct perf_evsel *counter;
+	struct perf_evlist *evlist = top->evlist;
+
+retry:
+	new_thread_map = thread_map__new_str(top->target.pid, top->target.tid,
+					     top->target.uid);
+	if (!new_thread_map)
+		return;
+
+	if (thread_map_cmp(top->evlist->threads, new_thread_map) == 0) {
+		free(new_thread_map);
+		return;
+	} else {
+		list_for_each_entry(counter, &evlist->entries, node) {
+			perf_evsel__close(counter, top->evlist->cpus->nr,
+					  top->evlist->threads->nr);
+		}
+
+		if (top->evlist->mmap)
+			perf_evlist__munmap(top->evlist);
+
+		if (top->evlist->pollfd) {
+			free(top->evlist->pollfd);
+			top->evlist->pollfd = NULL;
+		}
+
+		top->evlist->nr_fds = 0;
+
+		thread_map__delete(top->evlist->threads);
+		top->evlist->threads = new_thread_map;
+
+		if (perf_top__start_counters(top) == ESRCH) {
+			while (thread_map_cmp(top->evlist->threads,
+					      new_thread_map) == 0) {
+				new_thread_map = thread_map__new_str(top->target.pid,
+								     top->target.tid,
+								     top->target.uid);

tools/perf/builtin-top.c static int perf_top__setup_sample_type(struct perf_top *top) · line 1095

Quoted 10 lines
tools/perf/builtin-top.c +10 -0
+				if (!new_thread_map)
+					return;
+			}
+			goto retry;
+		}
+
+		return;
+	}
+}
+

I think it would be better to respond to FORK (and EXIT) events and open (close) counters accordingly. perf-top gets events when a thread is created or terminated; it might as well use the events to react than to poll every trip through the loop.

David