测量动态链接库的每秒CPU使用率 [英] measure CPU usage per second of a dynamically linked library
问题描述
我有一个示例应用程序,它使用动态链接库 library.so
。我正在使用top命令测量示例应用程序的CPU使用率。但是它显示了示例应用程序和 library.so
的CPU使用率。但是我只想查看 library.so
的CPU使用率。反正有这样做吗?我听说可以用htop实现它,但不知道如何实现。我使用树状视图,但它显示了几个过程作为示例应用程序过程。我不明白哪个是 library.so
。我正在使用centos 5.11。内核版本3.2.63-1.el5.elrepo。
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.
推荐答案
鉴于该库被视为您程序的一部分,一种方法是在您的代码中实施度量。以下最小示例在C ++ 11上实现,该C ++ 11仅运行一个假设库中的一个函数:
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.
这篇关于测量动态链接库的每秒CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!