2

I'm having a high CPU utilization, Is there any way to identify which part of my Program is using more CPU. Its a program written in C++, running on a CENTOS 5 box.

I'm suspecting the problem high usage is in shared libraries, but how can I monitor how much CPU all the shared libraries are using?

2 Answers 2

8

You can try using ltrace with the -c trace (very similar to strace but for library calls instead of system calls). This won't be complete as actually profiling the code and might not be the CPU time breakdown you are looking for, but it might just be the quick syadmin level tool you need.

kbrandt@kbrandt-acer:~$ ltrace -c xcalc
% time     seconds  usecs/call     calls      function
------ ----------- ----------- --------- --------------------
 66.83    0.222693        4453        50 XtCreateManagedWidget
 28.52    0.095048       95048         1 XtAppInitialize
  0.85    0.002837        2837         1 XtRealizeWidget
  0.83    0.002764        2764         1 XSetWMProtocols
  0.77    0.002581        2581         1 XtGetApplicationResources
  0.42    0.001383          53        26 XtWindow
  0.41    0.001371          54        25 XtDisplay
...
------ ----------- ----------- --------- --------------------
100.00    0.333219                   168 total

strace also with the -c switch will give you similar output but will show the system calls (the calls the libraries are using -- so sort of a level deeper).

The caveat with both of these break downs is these are wall clock time spent on each call and it doesn't show if this was active or idle time.

If you have the code and want to go all out you want code profiling. Stack Overflows "What can I use to profile my C++ code in Linux?" should get you started. I have used Valgrind with C code and liked it.

3
  • awesome, I didn't know about this. Let me try it and see what info I can get to identify where the issue is. Thanks!!
    – Manuel
    Nov 14, 2010 at 22:17
  • There's the better answer I was hoping for :) +1
    – James L
    Nov 14, 2010 at 22:19
  • I am such a fool. I have bashed Linux for 3+ years because Solaris could do this with truss and never once have I come across ltrace. This is a very handy thing the few times you need it and cannot begin to thank you enough for your answer. And not to make light, but I've worked with Linux for many, many years and I still learn something new all the time. Thanks again! Nov 15, 2010 at 2:03
0

I don't think you can, but the output of top -H might help (it shows CPU usage by threads). Hopefully someone will be able to give you a better answer. Also, this could be a simple misunderstanding, but aren't loaded shared libraries likely to cause excess RAM usage rather than CPU? Are you sure CPU is the problem?

1
  • Its a client-server app. This app consumes around 50% of memory, then when clients starts connecting the applications goes to 100-110% CPU utilization, SO i need to identify which portion of the code might need some enhancements. Sometimes with the same ammount of clients connected the CPU is looking good. It might be something that one of the clients its doing, but I don't know how to identify which part of the program is the one causing the high CPU ussage.
    – Manuel
    Nov 14, 2010 at 22:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .