From 5291012ef93d2f64bf749866c19ad6e5b65997ba Mon Sep 17 00:00:00 2001 From: Nan Xiao Date: Thu, 21 Nov 2019 17:20:42 +0800 Subject: [PATCH] Add record-events-per-thread.md --- SUMMARY.md | 3 +- posts/record-event-counts-per-thread.md | 57 +++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 posts/record-event-counts-per-thread.md diff --git a/SUMMARY.md b/SUMMARY.md index 6a91042..db79b0e 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -5,6 +5,7 @@ * [How to specify monitoring events](posts/how-to-specify-monitoring-events.md) * [What is skid](posts/what-is-skid.md) * [Count events](posts/count-events.md) +* [Record event counts per thread](posts/record-event-counts-per-thread.md) * [Profile memory access](posts/profile-memory-access.md) * [Check cache false sharing](posts/check-cache-false-sharing.md) * [Profile system in real time](posts/profile-system-in-real-time.md) @@ -12,4 +13,4 @@ * [Measure scheduler latency](posts/measure-scheduler-latency.md) * [Use ftrace](posts/use-ftrace.md) * [Show performance difference](posts/show-performance-difference.md) -* [The end](posts/the-end.md) \ No newline at end of file +* [The end](posts/the-end.md) diff --git a/posts/record-event-counts-per-thread.md b/posts/record-event-counts-per-thread.md new file mode 100644 index 0000000..ade0f25 --- /dev/null +++ b/posts/record-event-counts-per-thread.md @@ -0,0 +1,57 @@ +# Record event counts per thread + +`perf record` can be used to record event counts for every thread when using `-s` option. Check following multi-thread program: + +``` +$ cat sum.c +#include + +#define N 100000000 +#define THRAED_NUM 8 + +int values[N]; + +int main(void) +{ + int sum[THRAED_NUM]; + + #pragma omp parallel for + for (int i = 0; i < THRAED_NUM; i++) + { + int local_sum; + for (int j = 0; j < N; j++) + { + local_sum += values[j] >> i; + } + sum[i] = local_sum; + } + + return 0; +} +``` + +Build and use `perf record` to note down event counts for every thread: + +``` +# gcc -fopenmp -g sum.c -o sum +# perf record -s ./sum +[ perf record: Woken up 1 times to write data ] +[ perf record: Captured and wrote 0.404 MB perf.data (8757 samples) ] +``` + +Use `perf report` to analyze it (`-T` option means displaying per-thread event counts): + +``` +# perf report -T +...... +# PID TID cycles:uppp + 9960 9963 751824252 + 9960 9961 750625307 + 9960 9965 749742594 + 9960 9967 749228142 + 9960 9968 0 + 9960 9970 9857914 + 9960 9974 9150516 + 9960 9977 8928628 +...... +```