I would like to determine the memory usage from a C++ program
To expand on YangXiaoPo-MSFT's comment, I just tried on my PC (outside a Kubernetes setting though) to retrieve these metrics programmatically from C++ using the Performance Data Helper (PDH) library:
I added pdh.lib
to my linker input, and use it with:
GPUProcessMemoryQuery.cpp:
#include <windows.h>
#include <pdh.h>
#include <pdhmsg.h>
#include <iostream>
int main()
{
PDH_HQUERY hQuery = NULL;
PDH_HCOUNTER hCounter = NULL;
PDH_STATUS pdhStatus = PdhOpenQuery(NULL, NULL, &hQuery);
if (pdhStatus != ERROR_SUCCESS)
{
std::cerr << "Failed to open PDH query.\n";
return 1;
}
pdhStatus = PdhAddCounter(hQuery, L"\\GPU Process Memory(*)\\Dedicated Usage", 0, &hCounter);
if (pdhStatus != ERROR_SUCCESS)
{
std::cerr << "Failed to add PDH counter.\n";
return 1;
}
pdhStatus = PdhCollectQueryData(hQuery);
if (pdhStatus != ERROR_SUCCESS)
{
std::cerr << "Failed to collect PDH query data.\n";
return 1;
}
PDH_FMT_COUNTERVALUE counterValue;
pdhStatus = PdhGetFormattedCounterValue(hCounter, PDH_FMT_LONG, NULL, &counterValue);
if (pdhStatus != ERROR_SUCCESS)
{
std::cerr << "Failed to get formatted counter value.\n";
return 1;
}
std::wcout << L"GPU Process Memory (Dedicated Usage): " << counterValue.longValue << L"\n";
PdhCloseQuery(hQuery);
return 0;
}

I got: GPU Process Memory (Dedicated Usage): 35549184
, without requiring elevated rights.
In your case, measure the memory usage of a specific Windows container using the Performance Counters on Windows is a bit more involved.
You need first to determine the appropriate performance counter: Windows containers' performance metrics should be under namespaces like Hyper-V Container
or similar (this may change depending on the Windows Server version and container runtime).
Before diving into C++, you might want to use Performance Monitor
(type perfmon
in the Windows start menu) to browse available counters related to containers. That will give you an idea of what is available and the exact path you will need to use in your code.
Once you have the right counter path:
// Change this line:
pdhStatus = PdhAddCounter(hQuery, L"\\GPU Process Memory(*)\\Dedicated Usage", 0, &hCounter);
// To something like:
pdhStatus = PdhAddCounter(hQuery, L"\\Hyper-V Container\\Memory Metric (or appropriate counter)", 0, &hCounter);
It is crucial to identify which instance of the counter you are interested in, since each container should have its own instance. In the Performance Monitor, you can see instances of each counter (like individual container names or IDs). Replace the *
in the counter path with the exact instance name to monitor a specific container.