查找快速进程的CPU使用情况
Find CPU usage of a fast process
我正在寻找一种方法来测量1秒内完成的进程的CPU使用率。
由于速度太快,top
无法做到公正。据我所知,top
会拍摄快照,因此该过程可以在更新之间完成。
这个程序是用C++编写的,我在Linux上运行。若有一些简单的代码可以复制并粘贴到程序中,在main()
结束时打印出CPU使用情况,那个就可以了。或者,如果有一些评测工具,我也可以使用它。
编辑-人们似乎对我想要的东西有一些误解。
我不是在寻找持续时间。我知道持续时间。大约1秒。我想知道的是CPU的使用情况。如果它是100%,那么这意味着我的CPU运行了整整1秒。
如果是50%,则表示CPU有50%的时间处于空闲状态。它有可能在等待IO——另外50%。
如果我的程序运行很长一段时间,top
是好的,因为它显示了类似的东西
%Cpu(s): 0.6 us, 0.3 sy, 0.0 ni, 99.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
这意味着我的cpu有0.6%的时间处于用户空间,0.3%的时间处于内核空间,99.1%的时间处于空闲状态。
但是,正如我之前所说,top
不适用于快速流程。那么我该怎么办呢?
感谢
试试这个:
#include <unistd.h>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include <boost/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
//This function reads /proc/stat and returns the idle value for each cpu in a vector
std::vector<long long> get_idle() {
//Virtual file, created by the Linux kernel on demand
std::ifstream in( "/proc/stat" );
std::vector<long long> result;
//This might broke if there are not 8 columns in /proc/stat
boost::regex reg("cpu(\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+)");
std::string line;
while ( std::getline(in, line) ) {
boost::smatch match;
if ( boost::regex_match( line, match, reg ) ) {
long long idle_time = boost::lexical_cast<long long>(match[5]);
result.push_back( idle_time );
}
}
return result;
}
//This function returns the avarege load in the next interval_seconds for each cpu in a vector
//get_load() halts this thread for interval_seconds
std::vector<float> get_load(unsigned interval_seconds) {
boost::posix_time::ptime current_time_1 = boost::date_time::microsec_clock<boost::posix_time::ptime>::universal_time();
std::vector<long long> idle_time_1 = get_idle();
sleep(interval_seconds);
boost::posix_time::ptime current_time_2 = boost::date_time::microsec_clock<boost::posix_time::ptime>::universal_time();
std::vector<long long> idle_time_2 = get_idle();
//We have to measure the time, beacuse sleep is not accurate
const float total_seconds_elpased = float((current_time_2 - current_time_1).total_milliseconds()) / 1000.f;
std::vector<float> cpu_loads;
for ( unsigned i = 0; i < idle_time_1.size(); ++i ) {
//This might get slightly negative, because our time measurment is not accurate
const float load = 1.f - float(idle_time_2[i] - idle_time_1[i])/(100.f * total_seconds_elpased);
cpu_loads.push_back( load );
}
return cpu_loads;
}
int main() {
const unsigned measurement_count = 5;
const unsigned interval_seconds = 5;
for ( unsigned i = 0; i < measurement_count; ++i ) {
std::vector<float> cpu_loads = get_load(interval_seconds);
for ( unsigned i = 0; i < cpu_loads.size(); ++i ) {
std::cout << "cpu " << i << " : " << cpu_loads[i] * 100.f << "%" << std::endl;
}
}
return 0;
}
相关文章:
- 如何在没有死锁和/或争用的情况下正确使用 std::mutex C++?
- 在集合的情况下使用emplace_hint的用例是什么?
- 在没有堆的情况下用两种方法构造对象
- 如何在不中断的情况下正确使用C++中的切换用例语句中的Fallthrough
- 给定一个填充无符号字符**的 C 函数,如何在没有中间副本的情况下用数据填充 std::vector
- 在这种情况下,有没有办法用单个解决方案替换两个仅在类型上不同的相似函数?
- 为什么在一种情况下,我会收到带有字符串文字的已弃用转换警告,而在另一种情况下却没有?
- 不能在没有对象的情况下调用成员函数,但我用对象调用函数
- 在不先显式调用析构函数的情况下,在旧对象上使用placement new是否危险
- 无法在前面有多余空格的情况下获取要打印的数组.只有第一个值会用空格打印
- 在可疑的情况下发出叮当警告:函数'foo'可以用属性"noreturn"声明吗?
- 如何在没有任何JavaScript知识的情况下用PNaCl编写C++HTML5应用程序
- 如何在不使用开关大小写的情况下用C++编写菜单驱动的程序
- 在什么情况下,运算符=应该用左值/右值重载而不是复制和交换来实现
- 在没有测试用例的情况下提升单元测试框架
- 在这种情况下,为函数赋值有什么用
- 如何在没有_setmode的情况下用C++输出unicode
- 如何在没有STL的情况下用c++中的Key创建和访问内存块
- 如何在不锁定文件的情况下用C++编写文件
- clang与gcc运行时的区别:用clang构建的c++类模板在没有复制构造函数的情况下崩溃,用复制构造函数构建的gcc