如何在 C++ 中获取当前的 CPU 和 RAM 使用率?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/479722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get current CPU and RAM usage in C++?
提问by tunnuz
is it possible, in C++, to get the current RAM and CPU usage? Is there a platform-indepentent function call?
是否有可能在 C++ 中获取当前的 RAM 和 CPU 使用率?是否有独立于平台的函数调用?
采纳答案by ididak
回答by Kosi2801
Sadly these things rely heavily on the underlying OS, so there are no platform-independent calls. (Maybe there are some wrapper frameworks, but I don't know of any.)
遗憾的是,这些东西严重依赖底层操作系统,因此没有独立于平台的调用。(也许有一些包装框架,但我不知道。)
On Linux you could have a look at the getrusage()function call, on Windows you can use GetProcessMemoryInfo()for RAM Usage. Have also a look at the other functions in the Process Status APIof Windows.
在 Linux 上,您可以查看getrusage()函数调用,在 Windows 上,您可以使用GetProcessMemoryInfo()来获取 RAM 使用情况。还可以查看Windows的进程状态 API中的其他函数。
回答by jheriko
There is not a platform independent function for this that I know of. IF you plan to target multiple versions of Windows be aware that the implementation differs across some versions. I hit this problem when testing an app under NT 3.51 for instance... (archaic, I know).
据我所知,没有与平台无关的功能。如果您计划面向多个版本的 Windows,请注意某些版本的实现有所不同。例如,我在 NT 3.51 下测试应用程序时遇到了这个问题......(我知道是过时的)。
Here is some code I used for the memory side of things. This doesn't work across platforms other than windows, and will just return 0 when compiled without the WIN32 define:
这是我用于事物内存方面的一些代码。这不适用于 Windows 以外的平台,并且在没有 WIN32 定义的情况下编译时只会返回 0:
EDIT: I forgot to mention, this code divides and rounds down to the nearest MB, hence the >> 20 all over the place.
编辑:我忘了提到,这段代码会划分并四舍五入到最近的 MB,因此整个地方都是 >> 20。
// get memory info...
int getTotalRAM()
{
int ret = 0;
#ifdef WIN32
DWORD v = GetVersion();
DWORD major = (DWORD)(LOBYTE(LOWORD(v)));
DWORD minor = (DWORD)(HIBYTE(LOWORD(v)));
DWORD build;
if (v < 0x80000000) build = (DWORD)(HIWORD(v));
else build = 0;
// because compiler static links the function...
BOOL (__stdcall*GMSEx)(LPMEMORYSTATUSEX) = 0;
HINSTANCE hIL = LoadLibrary(L"kernel32.dll");
GMSEx = (BOOL(__stdcall*)(LPMEMORYSTATUSEX))GetProcAddress(hIL, "GlobalMemoryStatusEx");
if(GMSEx)
{
MEMORYSTATUSEX m;
m.dwLength = sizeof(m);
if(GMSEx(&m))
{
ret = (int)(m.ullTotalPhys>>20);
}
}
else
{
MEMORYSTATUS m;
m.dwLength = sizeof(m);
GlobalMemoryStatus(&m);
ret = (int)(m.dwTotalPhys>>20);
}
#endif
return ret;
}
int getAvailRAM()
{
int ret = 0;
#ifdef WIN32
DWORD v = GetVersion();
DWORD major = (DWORD)(LOBYTE(LOWORD(v)));
DWORD minor = (DWORD)(HIBYTE(LOWORD(v)));
DWORD build;
if (v < 0x80000000) build = (DWORD)(HIWORD(v));
else build = 0;
// because compiler static links the function...
BOOL (__stdcall*GMSEx)(LPMEMORYSTATUSEX) = 0;
HINSTANCE hIL = LoadLibrary(L"kernel32.dll");
GMSEx = (BOOL(__stdcall*)(LPMEMORYSTATUSEX))GetProcAddress(hIL, "GlobalMemoryStatusEx");
if(GMSEx)
{
MEMORYSTATUSEX m;
m.dwLength = sizeof(m);
if(GMSEx(&m))
{
ret = (int)(m.ullAvailPhys>>20);
}
}
else
{
MEMORYSTATUS m;
m.dwLength = sizeof(m);
GlobalMemoryStatus(&m);
ret = (int)(m.dwAvailPhys>>20);
}
#endif
return ret;
}
int getTotalMemory()
{
int ret = 0;
#ifdef WIN32
DWORD v = GetVersion();
DWORD major = (DWORD)(LOBYTE(LOWORD(v)));
DWORD minor = (DWORD)(HIBYTE(LOWORD(v)));
DWORD build;
if (v < 0x80000000) build = (DWORD)(HIWORD(v));
else build = 0;
// because compiler static links the function...
BOOL (__stdcall*GMSEx)(LPMEMORYSTATUSEX) = 0;
HINSTANCE hIL = LoadLibrary(L"kernel32.dll");
GMSEx = (BOOL(__stdcall*)(LPMEMORYSTATUSEX))GetProcAddress(hIL, "GlobalMemoryStatusEx");
if(GMSEx)
{
MEMORYSTATUSEX m;
m.dwLength = sizeof(m);
if(GMSEx(&m))
{
ret = (int)(m.ullTotalPhys>>20) + (int)(m.ullTotalVirtual>>20);
}
}
else
{
MEMORYSTATUS m;
m.dwLength = sizeof(m);
GlobalMemoryStatus(&m);
ret = (int)(m.dwTotalPhys>>20) + (int)(m.dwTotalVirtual>>20);
}
#endif
return ret;
}
int getAvailMemory()
{
int ret = 0;
#ifdef WIN32
DWORD v = GetVersion();
DWORD major = (DWORD)(LOBYTE(LOWORD(v)));
DWORD minor = (DWORD)(HIBYTE(LOWORD(v)));
DWORD build;
if (v < 0x80000000) build = (DWORD)(HIWORD(v));
else build = 0;
// because compiler static links the function...
BOOL (__stdcall*GMSEx)(LPMEMORYSTATUSEX) = 0;
HINSTANCE hIL = LoadLibrary(L"kernel32.dll");
GMSEx = (BOOL(__stdcall*)(LPMEMORYSTATUSEX))GetProcAddress(hIL, "GlobalMemoryStatusEx");
if(GMSEx)
{
MEMORYSTATUSEX m;
m.dwLength = sizeof(m);
if(GMSEx(&m))
{
ret = (int)(m.ullAvailPhys>>20) + (int)(m.ullAvailVirtual>>20);
}
}
else
{
MEMORYSTATUS m;
m.dwLength = sizeof(m);
GlobalMemoryStatus(&m);
ret = (int)(m.dwAvailPhys>>20) + (int)(m.dwAvailVirtual>>20);
}
#endif
return ret;
}
回答by HUAGHAGUAH
No, there isn't, not in the standard.
不,没有,不在标准中。
If you truly need this information, you will have to write platform-specific #ifdefs or link against a library that provides it.
如果您确实需要此信息,则必须编写特定于平台的 #ifdefs 或链接到提供它的库。
回答by Aaron McDaid
On Linux, this will use /proc/self/status . More work is required to turn this into a number. I find this useful as it is though, just to print the memory usage directly to the screen as a string.
在 Linux 上,这将使用 /proc/self/status 。需要做更多的工作才能将其转化为数字。我发现这很有用,只是将内存使用情况作为字符串直接打印到屏幕上。
static string memory_usage() {
ostringstream mem;
PP("hi");
ifstream proc("/proc/self/status");
string s;
while(getline(proc, s), !proc.fail()) {
if(s.substr(0, 6) == "VmSize") {
mem << s;
return mem.str();
}
}
return mem.str();
}
回答by Samrat Patil
There is no platform independent way to do this. Although for windows, you can get the CPU usage and performance metrics by using PDH.dll(Performance Data Helper) and its related APIs in your code.
没有独立于平台的方法可以做到这一点。尽管对于 Windows,您可以通过在代码中使用 PDH.dll(性能数据助手)及其相关 API 来获取 CPU 使用率和性能指标。
回答by Bartosz Pacho?ek
If that is still the case please check:
如果仍然是这种情况,请检查:
http://sourceforge.net/projects/cpp-cpu-monitor/
http://sourceforge.net/projects/cpp-cpu-monitor/
It gives you an example how to get CPU and RAM usage of a Linux (tested on Debian and CentOS) and a quite simple instruction of how to install.
它为您提供了如何获取 Linux(在 Debian 和 CentOS 上测试)的 CPU 和 RAM 使用率的示例以及有关如何安装的非常简单的说明。
Please feel free to ask if you have any questions regarding this small project.
如果您对这个小项目有任何疑问,请随时提问。
回答by GoFaster
I notice that ACEis ported to vcpkgwhich would make it easy to compile & link a cross-platform C++ app.
我注意到ACE被移植到vcpkg,这将使编译和链接跨平台 C++ 应用程序变得容易。
In C++ I'd like to monitor available system CPU and memory resources, so that my app can flex its consumption in response to resource availability.
在 C++ 中,我想监视可用的系统 CPU 和内存资源,以便我的应用程序可以根据资源可用性灵活调整其消耗。
Is anyone please able to offer an ACE code snippet to get started on this?
有没有人可以提供一个ACE代码片段来开始这个?
回答by Martin York
Not directly.
不直接。
But you can use a library that abstracts the OS (such as ACE).
Though this might by a bit heavy if you just want CPU and Memory.
但是您可以使用抽象操作系统的库(例如 ACE)。
尽管如果您只想要 CPU 和内存,这可能会有点沉重。