0

Is there any direct linux function call to get CPU usage in run time for a specific thread by passing thread ID? If not function call, is there any other possible way to achieve the same?

I have an application written in c which is running on a linux platform. And it has multi threads running in parallel. In which i need to identify the CPU usage of each thread. For that i need to enhance my code with a system call which gives me the CPU usage for a specific thread. Consider i have all the thread IDs in a structure ready in the run time.

Vinoth
  • 21
  • 3
  • I think this link will help you. http://stackoverflow.com/questions/3017162/how-to-get-total-cpu-usage-in-linux-c – someone Apr 02 '14 at 13:14

2 Answers2

1

You can get CPU usage of specific process as follows

 ps -p <pid> -o %cpu

You can descard %CPU word from output like

ps -p <pid> -o %cpu | tail -n +2

You can pass above command in system call as well.

Also look function for getting current process utilization named getusage

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
0

Without knowing the amount and depth of information you are looking for, if you want to make relatively simple estimates of what each thread is doing you can look at pthread_getcpulockid. You use the thread IDs you have saved to get the per thread clockid and then use the clockids to call clock_gettime for each thread. There is nice little example program of this right on the man page.

Otherwise I think you are headed into /proc territory.

Duck
  • 26,924
  • 5
  • 64
  • 92