Abrufen des Gesamtprozentsatzes der CPU-Auslastung in Windows mit C++C++

Programme in C++. Entwicklerforum
Anonymous
 Abrufen des Gesamtprozentsatzes der CPU-Auslastung in Windows mit C++

Post by Anonymous »

Ich habe an diesem Tool gearbeitet, um schnell einige Systemstatistiken zu protokollieren, z. B. Speicherinformationen und den Prozentsatz der CPU-Auslastung (wie die Anzeige im Task-Manager). Ich scheine mich um den Speicher- und Protokollierungsteil gekümmert zu haben, aber es war sehr schwierig, den CPU-Prozentsatz herauszufinden :( Ich habe viele Informationen zu Methoden zum Überprüfen von CPU-Informationen gefunden, aber außerhalb der Zusammenfassungen ist so gut wie keines der Codebeispiele, die ich gefunden habe, kompilierbar oder gut kommentiert, daher war es für mich schwierig, einen Weg zu finden, dies zu tun. Ich habe bereits viele Stackoverflow-Fragen zum Abrufen von CPU-Timings und dergleichen durchgelesen, konnte sie aber nicht richtig formulieren Teile zusammen.

Vielleicht übersehe ich den Punkt, aber es scheint eine beliebte Methode zu sein, die CPU zwei Mal mit mindestens 200 ms zwischen jeder Prüfung abzufragen, um Probleme mit etwas namens ... Auflösung zu vermeiden? Also ja! Wie zum Teufel mache ich das? :( Ich werde meinen Quellcode teilen, damit Sie es sehen können Was genau ich bisher gemacht habe. Es ist alles in nur einer .cpp-Datei, ich verwende VS2013 Express für C++ und es ist nur für Windows für Multicore-CPUs.

Vorabwarnung: Es tut mir so leid für alle Kommentare im Code :x Wenn Sie diesen Code kopieren und ausführen, wird eine .CSV generiert Datei namens log.CSV

Code: Select all

//included libraries/functionality for input/output
#include 
#include 
#include 
using namespace std;

//creates a static variable to convert Bytes to Megabytes
#define MB 1048576

//main program code loop
int main()
{
//Code block intiialization for the memory referenced in the Kernell
MEMORYSTATUSEX memStat;
memStat.dwLength = sizeof (memStat);
GlobalMemoryStatusEx(&memStat);

//loads the SYSTEMTIME
SYSTEMTIME sysTime;
//Retrieves data so that we have a way to Get it to output when using the pointers
GetSystemTime(&sysTime);

//setting the I/O for our log file to be "myfile"
ofstream myfile;
// ios::out means that we're outputting data to the file
// ios::app means that all the data we're outputting goes to the end of that log file instead of the start
myfile.open("log.csv", ios::out | ios::app);

//a while loop that gathers and logs data every quarter of a second to gather 4 data points in one second
int counter = 0;
while (counter < 4)
{
//Timestamp + Memory Info, and eventually CPU Load percentage
myfile

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post