I have a sample application which uses a  dynamically linked library library.so. I was measuring CPU usage of the sample application with the top command. But it shows CPU usage of both sample app and library.so per second. But I want to see the CPU usage of only the library.so. Is there anyway to do this? I heard its achievable with htop but could not find out how. I used the tree view but it shows several processes as the sample app process. I could not understand which one is library.so. I am using centos 5.11. Kernel version 3.2.63-1.el5.elrepo.
            Asked
            
        
        
            Active
            
        
            Viewed 459 times
        
    0
            
            
        
        Tahlil
        
- 2,680
 - 6
 - 43
 - 84
 
- 
                    1You seem to imply that a shared library is a separate process. It's not. – John Zwinck Oct 23 '14 at 04:40
 - 
                    Then how can I get the CPU usage for only the `library.so`? Isn't there any other way? I have tried using profilers. But they give so many infos which I don't need. Is there any profiler that will not give me a detailed report but only the CPU usage of the shared library? Thanks. – Tahlil Oct 23 '14 at 04:50
 - 
                    In HTOP I can see 10 PID's which has the same name as the sample app. If they are a single process why do they have different PIDs? Or are they thread ids? – Tahlil Oct 23 '14 at 04:53
 - 
                    2You can use `callgrind` (part of `valgrind`) but it's not going to straight away give you the exact number you're looking for--you will need to sum up the instructions taken by all functions in that library (write a script for this), and you'll end up with a cycle count rather than "CPU seconds." There are other profilers of course, maybe one of them is useful. You could look at `ltrace` too, it can filter by library. – John Zwinck Oct 23 '14 at 04:53
 - 
                    They are thread IDs. – John Zwinck Oct 23 '14 at 04:54
 - 
                    I have tried to use the `ltrace`. I tried to use the following command to profile the `library.so` file, `ltrace -c -T --library=library.so --output=out.txt ./SampleApp`. But it shows this error `ltrace: Couldn't find .dynsym or .dynstr in "library.so"`. But `library.so` is a debug build. So the symbol table should be there. I have tried to verify it with `objdump --source library.so | grep CreateSocket()`. It returns codes that uses that CreateSocket() function. Which means it contains a symbol table. Than why that error occurs? – Tahlil Oct 23 '14 at 07:02
 
1 Answers
0
            
            
        Given the library is considered part of your program, one way would be to implement the measurement within your code. The following minimal example is implemented on C++11 running only one function from a hypothetical library:
#include <chrono>
#include <iostream>
#include <hypothetical>
int main() {
  using namespace std::chrono;
  system_clock systemClock;
  system_clock::time_point startingTime{systemClock.now()};
  hypothetical::function();
  system_clock::duration libraryTime{systemClock.now() - startingTime};
  std::cout << "Hypothetical library took " << duration_cast<seconds>(libraryTime).count() << " seconds to run.\n";
  return 0;
}
You will need to extend this to all of the functions that your program invokes from your library.
        Fabio A. Correa
        
- 1,968
 - 1
 - 17
 - 26
 
- 
                    Thanks for the reply. But its not feasible as the library makes a lot of different function(blocking/non blocking) calls. – Tahlil Oct 24 '14 at 03:20