5

I know perf can profile single progress or single thread use perf stat -p tid/pid or perf top -p tid/pid.

But I want to profile per-thread in a progress, and compare event, get which thread is high consumption, then to optimize it. Can perf do this ? if not, which tools can do that ?

thanks.

Jerry Zhang
  • 1,198
  • 1
  • 17
  • 36

3 Answers3

3

There was proposed patch to add --per-thread option to perf stat (and with interval mode -I 1000 it is possible to see current counters every second for every thread ): https://lwn.net/Articles/649001/ "perf stat: Introduce --per-thread option" From: Jiri Olsa, Date: Tue, 23 Jun 2015

adding the possibility to display stat data per thread.

Allowing following commands and output:

$ perf stat -e cycles,instructions --per-thread -p 30190,30242 ^C Performance counter stats for process id '30190,30242':

           cat-30190                     0      cycles
           yes-30242         3,842,525,421      cycles
           cat-30190                     0      instructions
           yes-30242        10,370,817,010      instructions

     1.143155657 seconds time elapsed

Also works under interval mode:

$ perf stat -e cycles,instructions --per-thread -p 30190,30242 -I 1000

#           time             comm-pid                  counts   unit events
   1.000073435              cat-30190                89,058      cycles
   1.000073435              yes-30242         3,360,786,902      cycles                     (100.00%)
   1.000073435              cat-30190                14,066      instructions
   1.000073435              yes-30242         9,069,937,462      instructions
   2.000204830              cat-30190                     0      cycles
   2.000204830              yes-30242         3,351,667,626      cycles
   2.000204830              cat-30190                     0      instructions
   2.000204830              yes-30242         9,045,796,885      instructions   
  ^C
   2.771286639              cat-30190                     0      cycles
   2.771286639              yes-30242         2,593,884,166      cycles
   2.771286639              cat-30190                     0      instructions
   2.771286639              yes-30242         7,001,171,191      instructions

Available in here:
git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
perf/per_thread

osgx
  • 90,338
  • 53
  • 357
  • 513
1

Yes. Of course.

You could use perf_event_open() system call to open performance counters. and then use proctl/ioctl to read counters.

You could check linux man pages for all the details.

Serjik
  • 10,543
  • 8
  • 61
  • 70
L.Y.
  • 21
  • 1
  • 3
-2

Do you see this question?

How to profile multi-threaded C++ application on Linux?

I think you could start with valgrind:

http://valgrind.org/docs/manual/cl-manual.html

Community
  • 1
  • 1
Klaus
  • 24,205
  • 7
  • 58
  • 113