在Linux下可以读取控制文件memory.usage_in_bytes
for Control Group v1或 Control Group v2 的 memory.current
。如何获取Windows container的内存使用情况?
根据Windows resource management Kubernetes 文档,进程隔离的 Windows 概念是关于 job objects 。我发现可以得到JOBOBJECT_EXTENDED_LIMIT_INFORMATION提供PeakJobMemoryUsed
的信息。但是,查询此信息似乎需要提升权限(因为我得到无法获取工作信息:[0x0005]访问被拒绝。
)。
还有其他方法可以获取有关 Windows 容器内存使用情况的信息吗?
最佳答案
I would like to determine the memory usage from a C++ program
扩展YangXiaoPo-MSFT的comment ,我只是尝试在我的 PC 上(尽管在 Kubernetes 设置之外)使用 Performance Data Helper (PDH) library 以编程方式从 C++ 检索这些指标。 :
我将 pdh.lib
添加到我的链接器输入中,并将其用于:
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;
}
我得到:GPU 进程内存(专用):35549184
,无需提升权限。
就您的情况而言,使用 Windows 上的性能计数器测量特定 Windows 容器的内存使用情况会更加复杂。
您首先需要确定适当的性能计数器:Windows 容器的性能指标应位于 Hyper-V Container
或类似 namespace 下(这可能会根据 Windows Server 版本和容器运行时而变化) .
在深入研究 C++ 之前,您可能需要使用性能监视器
(在 Windows 开始菜单中键入 perfmon
)来浏览与容器相关的可用计数器。这将使您了解可用的内容以及您需要在代码中使用的确切路径。
一旦你有了正确的反路径:
// 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);
确定您感兴趣的计数器实例至关重要,因为每个容器都应该有自己的实例。在性能监视器中,您可以查看每个计数器的实例(例如各个容器名称或 ID)。将计数器路径中的 *
替换为准确的实例名称以监视特定容器。
关于windows - 如何检索 Windows 容器的内存使用情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76849219/