21

I'm interested in the current usage of cpu - precisely cpu% and wait% - for each thread in a specific application. Is it possible to get that information from somewhere?

I know that top can split information per real thread (ones with pid), but it doesn't show the system/user/wait cpu usage split for each of them. I would also like some way to log that info. Do you know any apps (or apis) that can do that?

viraptor
  • 1,296
  • 6
  • 21
  • 41

7 Answers7

23
top -H -p pid 

hope it can help

Jenny D
  • 27,780
  • 21
  • 75
  • 114
liu bluse
  • 331
  • 2
  • 2
14

Percent of cpu usage per thread you can get with ps command:

 ps -emo %cpu,pid,user,args

The way it is calculated is described in ps manpage:

Currently, it is the CPU time used divided by the time the process has been running (cputime/realtime ratio), expressed as a percentage.

EM0
  • 370
  • 9
  • 24
lexsys
  • 2,913
  • 6
  • 31
  • 34
  • I'm really interested in both cpu time and io-wait time. ps can't handle the second one unfortunately. – viraptor Jul 09 '09 at 12:41
  • note to self: replace -e by -C java and pid by spid for thread Ids in java app – kellogs Jan 25 '15 at 11:12
  • 4
    greater note to self: `ps -To pcpu,tid -C java | sort -r -k1 | more` for hog threads in a java app. `ps --sort=pcpu` achieves nothing; better rely on shell for the sorting part. – kellogs Jan 25 '15 at 11:35
  • 1
    Very useful @kellogs, thanks. I would add `-n` or `-g` to sort, so that "10" shows up above "2", for example. – EM0 Nov 08 '19 at 10:19
5

I'd look into SystemTap. This tool will certainly give you what you want. There is this example of profiling threads; don't know if it has all you want, but you could modify it so that it does.

Mei
  • 4,590
  • 8
  • 45
  • 53
1

Maybe have a look at htop, you can configure quite a lot with it.

Sven
  • 98,649
  • 14
  • 180
  • 226
0

Did you tried sar? It can fetch a lot of information even on pid level.

0

Nagios and PNP http://docs.pnp4nagios.org/pnp-0.4/start

Works awesome... little configuration.

fsckin
  • 573
  • 4
  • 9
0

I have stumbled on this on a embedded system where we don't have the fancy tools to look in to this stuff.

For this, I have used cpuacct cgroup and placed each thread into its own folder. This way I was able to measure their usage.

Hierarchy was looking like this:

my-app.service
|-tid1
|-tid2
|-tid3

Then I changed it to

my-app.service
|-dir1
|  -tid1
|-dir2
|  -tid2
|-dir3
|  -tid3
Umut
  • 421
  • 3
  • 3