diff options
author | Jiri Olsa <jolsa@kernel.org> | 2018-11-05 15:23:40 -0500 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2018-12-17 12:58:03 -0500 |
commit | 94ad6e7e3606454498aeac1fdd1b9de5c1e6735a (patch) | |
tree | 00954eb7ea985e5053f7c1b53fc9c419a1316d4e | |
parent | 16c66bc167cc52992f66748aed7ac21396189457 (diff) |
perf top: Use cond variable instead of a lock
Use conditional variable logic to synchronize between the reading and
processing threads. Currently it's done by having mutex around rotation
code.
Using a POSIX cond variable to sync both threads after queues rotation:
Process thread:
- Detects data
- Switches queues
- Sets rotate variable
- Waits in pthread_cond_wait()
Read thread:
- Detects rotate is set
- Kicks the process thread with a pthread_cond_signal()
After this rotation is safely completed and both threads can continue
with the new queue.
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Acked-by: David S. Miller <davem@davemloft.net>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/n/tip-3rdeg23rv3brvy1pwt3igvyw@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
-rw-r--r-- | tools/perf/builtin-top.c | 24 | ||||
-rw-r--r-- | tools/perf/util/top.h | 4 |
2 files changed, 20 insertions, 8 deletions
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 75afeae7f04d..aad58643102e 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c | |||
@@ -846,13 +846,18 @@ static void perf_top__mmap_read_idx(struct perf_top *top, int idx) | |||
846 | if (ret && ret != -1) | 846 | if (ret && ret != -1) |
847 | break; | 847 | break; |
848 | 848 | ||
849 | pthread_mutex_lock(&top->qe.lock); | ||
850 | ret = ordered_events__queue(top->qe.in, event, timestamp, 0); | 849 | ret = ordered_events__queue(top->qe.in, event, timestamp, 0); |
851 | pthread_mutex_unlock(&top->qe.lock); | ||
852 | |||
853 | perf_mmap__consume(md); | ||
854 | if (ret) | 850 | if (ret) |
855 | break; | 851 | break; |
852 | |||
853 | perf_mmap__consume(md); | ||
854 | |||
855 | if (top->qe.rotate) { | ||
856 | pthread_mutex_lock(&top->qe.mutex); | ||
857 | top->qe.rotate = false; | ||
858 | pthread_cond_signal(&top->qe.cond); | ||
859 | pthread_mutex_unlock(&top->qe.mutex); | ||
860 | } | ||
856 | } | 861 | } |
857 | 862 | ||
858 | perf_mmap__read_done(md); | 863 | perf_mmap__read_done(md); |
@@ -1059,9 +1064,12 @@ static void *process_thread(void *arg) | |||
1059 | continue; | 1064 | continue; |
1060 | } | 1065 | } |
1061 | 1066 | ||
1062 | pthread_mutex_lock(&top->qe.lock); | ||
1063 | out = rotate_queues(top); | 1067 | out = rotate_queues(top); |
1064 | pthread_mutex_unlock(&top->qe.lock); | 1068 | |
1069 | pthread_mutex_lock(&top->qe.mutex); | ||
1070 | top->qe.rotate = true; | ||
1071 | pthread_cond_wait(&top->qe.cond, &top->qe.mutex); | ||
1072 | pthread_mutex_unlock(&top->qe.mutex); | ||
1065 | 1073 | ||
1066 | if (ordered_events__flush(out, OE_FLUSH__TOP)) | 1074 | if (ordered_events__flush(out, OE_FLUSH__TOP)) |
1067 | pr_err("failed to process events\n"); | 1075 | pr_err("failed to process events\n"); |
@@ -1151,7 +1159,8 @@ static void init_process_thread(struct perf_top *top) | |||
1151 | ordered_events__set_copy_on_queue(&top->qe.data[0], true); | 1159 | ordered_events__set_copy_on_queue(&top->qe.data[0], true); |
1152 | ordered_events__set_copy_on_queue(&top->qe.data[1], true); | 1160 | ordered_events__set_copy_on_queue(&top->qe.data[1], true); |
1153 | top->qe.in = &top->qe.data[0]; | 1161 | top->qe.in = &top->qe.data[0]; |
1154 | pthread_mutex_init(&top->qe.lock, NULL); | 1162 | pthread_mutex_init(&top->qe.mutex, NULL); |
1163 | pthread_cond_init(&top->qe.cond, NULL); | ||
1155 | } | 1164 | } |
1156 | 1165 | ||
1157 | static int __cmd_top(struct perf_top *top) | 1166 | static int __cmd_top(struct perf_top *top) |
@@ -1271,6 +1280,7 @@ static int __cmd_top(struct perf_top *top) | |||
1271 | out_join: | 1280 | out_join: |
1272 | pthread_join(thread, NULL); | 1281 | pthread_join(thread, NULL); |
1273 | out_join_thread: | 1282 | out_join_thread: |
1283 | pthread_cond_signal(&top->qe.cond); | ||
1274 | pthread_join(thread_process, NULL); | 1284 | pthread_join(thread_process, NULL); |
1275 | out_delete: | 1285 | out_delete: |
1276 | perf_session__delete(top->session); | 1286 | perf_session__delete(top->session); |
diff --git a/tools/perf/util/top.h b/tools/perf/util/top.h index 5f503293cfd8..5bce62ebcf14 100644 --- a/tools/perf/util/top.h +++ b/tools/perf/util/top.h | |||
@@ -44,7 +44,9 @@ struct perf_top { | |||
44 | struct { | 44 | struct { |
45 | struct ordered_events *in; | 45 | struct ordered_events *in; |
46 | struct ordered_events data[2]; | 46 | struct ordered_events data[2]; |
47 | pthread_mutex_t lock; | 47 | bool rotate; |
48 | pthread_mutex_t mutex; | ||
49 | pthread_cond_t cond; | ||
48 | } qe; | 50 | } qe; |
49 | }; | 51 | }; |
50 | 52 | ||