measure CPU usage per second of a dynamically link

2019-09-02 20:35发布

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.

1条回答
狗以群分
2楼-- · 2019-09-02 20:49

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.

查看更多
登录 后发表回答