measure CPU usage per second of a dynamically linked library

放肆的年华 提交于 2019-12-25 01:43:49

问题


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:


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.



来源:https://stackoverflow.com/questions/26520675/measure-cpu-usage-per-second-of-a-dynamically-linked-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!