繁体   English   中英

以百分比计算 CPU 使用率 UWP 应用程序 Windows 10 IOT

[英]Calculate CPU Usage in Percentage UWP Application Windows 10 IOT

我想以百分比计算 CPU 使用率。 目前我正在使用ProcessDiagnosticInfo来获取内核时间和用户时间。 如果有的话,我如何将这个时间转换为百分比或建议我任何其他方法来找到它。

private TimeSpan GetTotalCpuTime()
    {
        var totalKernelTime = new TimeSpan();
        var totalUserTime = new TimeSpan();

        var pdis = ProcessDiagnosticInfo.GetForProcesses();
        foreach (var pdi in pdis)
        {
            var cpuUsage = pdi.CpuUsage;
            var report = cpuUsage.GetReport();
            totalKernelTime += report.KernelTime;
            totalUserTime += report.UserTime;
        }

        return totalKernelTime + totalUserTime;
    }

我也知道 Windows 10 IoT 仪表板 API“ /api/resourcemanager/systemperf ”,它返回系统统计信息,其中包括 CPU 使用百分比,但需要凭据才能访问它,所以我不想使用它。

每个进程在内核模式下花费一些时间,在用户模式下花费一些时间。 需要注意的是,我们没有考虑空闲时间。 请参考以下代码。

    private static readonly Stopwatch Stopwatch = new Stopwatch();
    private static TimeSpan _oldElapsed, _oldKernelTime, _oldUserTime;
    private static int ProcessorCount { get; }
    private static double _carryOver;

    static CpuUsage()
    {
        // Stopwatch will be used to track how much time/usage has elapsed.
        Stopwatch.Start();
        // We'll divide the total used CPU time by the number of processors.
        ProcessorCount = System.Environment.ProcessorCount;
        // Run to store the initial "oldKernel/UserTime" so the first read 
        // isn't super inflated by the application's start-up.
        GetTotalCpuTime();
    }

    /// <summary>
    /// Returns the average percentage of CPU time used since the last time this call was made.
    /// </summary>
    /// <returns></returns>
    private static TimeSpan GetTotalCpuTime()
    {
        // Because we could have more than one process running, add all of them up.
        var totalKernelTime = new TimeSpan();
        var totalUserTime = new TimeSpan();

        // Grab the diagnostic infos for all existing processes.
        var pdis = ProcessDiagnosticInfo.GetForProcesses();
        foreach (var pdi in pdis)
        {
            var cpuUsage = pdi.CpuUsage;
            var report = cpuUsage.GetReport();
            totalKernelTime += report.KernelTime;
            totalUserTime += report.UserTime;
        }

        // Subtract the amount of "Total CPU Time" that was previously calculated.
        var elapsedKernelTime = totalKernelTime - _oldKernelTime;
        var elapsedUserTime = totalUserTime - _oldUserTime;

        // Track the "old" variables.
        _oldKernelTime = totalKernelTime;
        _oldUserTime = totalUserTime;

        // Between both is all of the CPU time that's been consumed by the application.
        return elapsedKernelTime + elapsedUserTime;
    }

    public static double GetPercentage()
    {
        // Because there's a small amount of time between when the "elapsed" is grabbed, 
        // and all of the process KernelTime and UserTimes are tallied, the overall CPU
        // usage will be off by a fraction of a percent, but it's nominal.  Like in the 
        // 0.001% range.
        var elapsed = Stopwatch.Elapsed;
        var elapsedTime = elapsed - _oldElapsed;

        var elapsedCpuTime = GetTotalCpuTime();

        // Divide the result by the amount of time that's elapsed since the last check to 
        // get the percentage of CPU time that has been consumed by this application.
        var ret = elapsedCpuTime / elapsedTime / ProcessorCount * 100;

        // Track the "old" variables.
        _oldElapsed = elapsed;

        // This part is completely optional.  Because the thread could be called between 
        // the time that "elapsed" is grabbed, and the CPU times are calculated, this will
        // cause a "pause" that results in spiking the "CPU usage time" over 100%.  However
        // on the next call, the difference will be "lost" (so if it the CPU percent was
        // at 100% for two calls, but this 'pause' happened, one could report 150% while
        // the next would report 50%.)  By carrying over the values above 100%, we can get
        // a slightly more accurate "average" usage.  
        ret += _carryOver;
        if (ret > 100)
        {
            _carryOver = ret - 100;
            ret = 100;
        }
        else
        {
            _carryOver = 0;
        }

        return ret;
    }

更新:您需要在清单中声明appDiagnosticspackageQuery功能。

  • appDiagnostics功能允许应用程序获取诊断信息。
  • packageQuery设备功能允许应用程序收集有关其他应用程序的信息。

*.appxmanifest :

  <Capabilities>
    <Capability Name="internetClient" />
    <rescap:Capability Name="appDiagnostics" />
    <rescap:Capability Name="packageQuery" />
  </Capabilities>

这里有一篇关于UWP App Diagnostics博客,希望对你有所帮助。 此外,您可以参考此示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM