gpt4 book ai didi

c++ - 在 Linux C++ 上读取 CPU

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:57:36 24 4
gpt4 key购买 nike

我正在用 C++ 开发一个服务器审计器。我在这里遇到的大问题是,当我尝试读取 CPU 使用率时,第一个核心计算正确,但其余的看起来像这样:

Core 0 percentage: 3.48259%

Core 1 percentage: 8200%

Core 2 percentage: 2562.12%

Core 3 percentage: -905.97%

我使用的代码如下

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>


#include <boost/foreach.hpp>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif // win32

#include <vector>
using namespace std;


void sleepcp(int milliseconds);

void sleepcp(int milliseconds) // cross-platform sleep function
{
#ifdef WIN32
Sleep(milliseconds);
#else
usleep(milliseconds * 1000);
#endif // win32
}
string convertIntToString(int i){
string result;
ostringstream convert;
convert << i;
result = convert.str();
return result;
}
int convertStringToInt(string s){
return atoi(s.c_str());
}
bool is_digits(const std::string &str) {
return std::all_of(str.begin(), str.end(), ::isdigit); // C++11
}
int getNumberOfCores(){
ifstream cpuinfo;
cpuinfo.open("/proc/cpuinfo",ios::in);
string word;
int cpucount = 0;
if (cpuinfo.is_open()) {
while ( cpuinfo >> word ) {
if(word == "processor"){
cpucount++;
}
}
cpuinfo.close();
}

else cout << "Unable to open file";
return cpucount;
}
string splitLineAt(const std::string str, int position){
std::istringstream buf(str);
std::istream_iterator<std::string> beg(buf), end;

std::vector<std::string> tokens(beg, end);
return tokens[position];
}

int getTotalTicks(string line){
int total_ticks = convertStringToInt(splitLineAt(line,1)) + convertStringToInt(splitLineAt(line,2)) + convertStringToInt(splitLineAt(line,3)) + convertStringToInt(splitLineAt(line,4)) + convertStringToInt(splitLineAt(line,5)) + convertStringToInt(splitLineAt(line,6)) + convertStringToInt(splitLineAt(line,7));
return total_ticks;
}
int getWorkTicks(string line){
int work_ticks = convertStringToInt(splitLineAt(line,1)) + convertStringToInt(splitLineAt(line,2)) + convertStringToInt(splitLineAt(line,3));
return work_ticks;
}
int *prev_total_ticks;
int *prev_work_ticks;
double getCorePercentage(int i){

ifstream currentUsage;
currentUsage.open("/proc/stat",ios::in);
string line;
if(currentUsage.is_open()){

while(getline(currentUsage,line)){
if(line.at(0) == 'c' && line.at(1) == 'p' && line.at(2) == 'u'){
if(line.at(3) == convertIntToString(i).at(0)){
int total_ticks = getTotalTicks(line);
int work_ticks = getWorkTicks(line);
int total_over_period = total_ticks - (prev_total_ticks[i-1]);
int work_over_period = work_ticks - (prev_work_ticks[-1]);
cout << prev_total_ticks[i-1] << "\n";
cout << prev_work_ticks[i-1] << "\n";
prev_work_ticks[i-1] = work_ticks;
prev_total_ticks[i-1] = total_ticks;

return ((double)work_over_period / (double)total_over_period) * 100.0;
}
}
}
}
}


int main(){
prev_total_ticks = new int[getNumberOfCores()];
prev_work_ticks = new int[getNumberOfCores()];
for(int i = 0; i < getNumberOfCores(); i++){
prev_total_ticks[i] = 0;
prev_work_ticks[i] = 0;
}
while (true){
for(int i = 0; i < getNumberOfCores(); i++){
cout << "Core "<<i<<" percentage: "<< getCorePercentage(i) << "%\n";
}
sleepcp(2000);
}
return 0;
}

我相信 getCorePercentage 中的计算是罪魁祸首,但我不确定......以前我只是一次检查一个核心时它工作得很好......

关于如何解决这个问题有什么想法吗?

最佳答案

int work_over_period =   work_ticks - (prev_work_ticks[-1]);
^^

我确定你想在那里说 i-1

关于c++ - 在 Linux C++ 上读取 CPU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29647718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com