Jump to content
  • Advertisement
Sign in to follow this  
ChrisArmitt

CPU and Memory Usage, Problem Obtaining it [C++]

This topic is 2361 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings,

I have been working on my final year project for, the content of which is irrelevant to the problem, but I need to obtain the CPU Usage each frame and the memory usage of the program.

I have implemented a snippet of code I found on Stack Overflow (http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process) but I have a huge problem.

Quite frequently the GetCPUUsage() returns 0 or INF.
This is obviously because the current and last are the same values.

This is moving into programming territory I know extremely little or absolutely nothing about. What are the potential solutions to this problem, I have no problem implementing a different method of obtaining resource usage if anybody has any suggestions.

The exact code I am using:

#pragma once
/*
http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process
*/
#include <Windows.h>
#include <Psapi.h>
#pragma comment(lib, "Psapi.lib")
class ResourceUsage
{
private:
PROCESS_MEMORY_COUNTERS pmc;
int numProcessors;
HANDLE self;
ULARGE_INTEGER lastCPU;
ULARGE_INTEGER lastSysCPU;
ULARGE_INTEGER lastUserCPU;
FILETIME ftime, fsys, fuser;
ULARGE_INTEGER now, sys, user;
ResourceUsage()
{
SYSTEM_INFO sysInfo;
FILETIME ftime, fsys, fuser;
GetSystemInfo(&sysInfo);
numProcessors = sysInfo.dwNumberOfProcessors;

GetSystemTimeAsFileTime(&ftime);
memcpy(&lastCPU, &ftime, sizeof(FILETIME));

self = GetCurrentProcess();
GetProcessTimes(self, &ftime, &ftime, &fsys, &fuser);
memcpy(&lastSysCPU, &fsys, sizeof(FILETIME));
memcpy(&lastUserCPU, &fuser, sizeof(FILETIME));
}
~ResourceUsage(){}
public:
static ResourceUsage& GetSingleton()
{
static ResourceUsage ru;
return ru;
}

SIZE_T GetMemoryUsage(void)
{

GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
return pmc.WorkingSetSize;
}
double GetCPUUsage()
{
double percent = 0.0f;
GetSystemTimeAsFileTime(&ftime);
memcpy(&now, &ftime, sizeof(FILETIME));
GetProcessTimes(self, &ftime, &ftime, &fsys, &fuser);
memcpy(&sys, &fsys, sizeof(FILETIME));
memcpy(&user, &fuser, sizeof(FILETIME));
percent = (sys.QuadPart - lastSysCPU.QuadPart) +
(user.QuadPart - lastUserCPU.QuadPart);
percent /= (now.QuadPart - lastCPU.QuadPart);
percent /= numProcessors;
lastCPU = now;
lastUserCPU = user;
lastSysCPU = sys;
return percent * 100;
}
};


Thanks in advance.

-MaGuSware

Share this post


Link to post
Share on other sites
Advertisement
It looks like your infinity will happen when now.QuadPart == lastCPU.QuadPart. I'd suggest if that's the case then nothing has changed since last time and you should return the previous result.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!