2

Under Linux it's possible to read the control file memory.usage_in_bytes for Control Group v1 or memory.current for Control Group v2. How to get the memory usage of a Windows container?

According to the Windows resource management Kubernetes docs, the Windows concept for the process isolation is about job objects. I found that it's possible to get JOBOBJECT_EXTENDED_LIMIT_INFORMATION information providing PeakJobMemoryUsed. However, it appears querying this information requires elevated rights (because I got Could not get job information: [0x0005] Access is denied.).

Is there any other way to get information about Windows containers memory usage?

Christian Ammer
  • 7,464
  • 6
  • 51
  • 108
  • Hi \`Christian Ammer\` you can monitor your windows container using some eternal tools like prometheus, zabbix, nagios etc.. If your container runtime is docker you can get the metrics from docker desktop and if you are using kubernetes you can find these metrics using kubectl commands. There one more tool called Windows Performance Counters which can help you in fetching the CPU, Memory and other resource utilization details. – Kranthiveer Dontineni Aug 07 '23 at 10:07
  • Thank you for pointing out these possibilities. However, I would like to determine the memory usage from a C++ program. – Christian Ammer Aug 07 '23 at 15:42
  • Use [Performance Counters](https://learn.microsoft.com/en-us/windows/win32/perfctrs/browsing-performance-counters). For example, https://stackoverflow.com/questions/73476947/how-to-get-the-dedicated-gpu-memory-number-for-every-running-process-in-window – YangXiaoPo-MSFT Aug 09 '23 at 05:43

1 Answers1

5

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;
}

Visual Studio

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250