windows - 如何检索 Windows 容器的内存使用情况?

标签 windows kubernetes winapi containers windows-container

在Linux下可以读取控制文件memory.usage_in_bytes for Control Group v1Control Group v2memory.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-MSFTcomment ,我只是尝试在我的 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;
}

Visual Studio

我得到: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/

相关文章:

c# - 使用 SetParent 窃取另一个进程的主窗口但保持消息循环独立

c++ - MFC 对话框控件的 "Accept Files"选项如何工作?

windows - 在 Windows 关机时调用 WCF 方法有时会失败

windows - 如何以实时优先级启动可执行文件?

node.js - 无法使用 Node.js 从 MongoDB slave 读取

vagrant - 在没有gcloud的情况下使用kubectl进行本地开发?

c++ - 如何处理缺少 msvcp100.dll、msvcp100.dll 等 dll?

windows - 腻子错误 : Unable to open connection to hostname : Host does not exist

apache-spark - java.lang.ClassNotFoundException : org. apache.spark.deploy.kubernetes.submit.Client

c++ - 当用户调整对话框大小时,如何强制Windows不要在对话框中重绘任何内容?