【问题标题】:.net core cpu usage for machine机器的.net核心cpu使用情况
【发布时间】:2019-12-24 07:17:04
【问题描述】:

我最近从 c# 迁移到 .net 核心。在 c# 中,我用这个来获取 CPU 使用率:

PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;

cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");

public string getCurrentCpuUsage(){
            return cpuCounter.NextValue()+"%";
}

但是.net core PerformanceCounter 不可用,解决方案是什么?请给我一个建议。

【问题讨论】:

    标签: c# .net .net-core


    【解决方案1】:

    性能计数器不在 Linux 中,因此不在 NET Core 中。替代方式:

    private async Task<double> GetCpuUsageForProcess()
    {
        var startTime = DateTime.UtcNow;
        var startCpuUsage = Process.GetProcesses().Sum(a => a.TotalProcessorTime.TotalMilliseconds);
        await Task.Delay(500);
    
        var endTime = DateTime.UtcNow;
        var endCpuUsage = Process.GetProcesses().Sum(a => a.TotalProcessorTime.TotalMilliseconds);
        var cpuUsedMs = endCpuUsage - startCpuUsage;
        var totalMsPassed = (endTime - startTime).TotalMilliseconds;
        var cpuUsageTotal = cpuUsedMs / (Environment.ProcessorCount * totalMsPassed);
        return cpuUsageTotal * 100;
    }
    

    【讨论】:

    【解决方案2】:

    在 Mac 上,我采用与 you already have to go to get memory usage 相同的方法:shell 输出到命令行实用程序,例如 top,然后解析输出。

    这是我的代码:

    
            private static string[] GetOsXTopOutput()
            {
                var info = new ProcessStartInfo("top");
                info.Arguments = "-l 1 -n 0";
                info.RedirectStandardOutput = true;
    
                string output;
                using (var process = Process.Start(info))
                {
                    output = process.StandardOutput.ReadToEnd();
                }
    
                return output.Split('\n');
            }
    
            public static double GetOverallCpuUsagePercentage()
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                {
                    var lines = GetOsXTopOutput();
    
                    // Example: "CPU usage: 8.69% user, 21.73% sys, 69.56% idle"
                    var pattern = @"CPU usage: \d+\.\d+% user, \d+\.\d+% sys, (\d+\.\d+)% idle";
                    Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
                    foreach (var line in lines)
                    {
                        Match m = r.Match(line);
                        if (m.Success)
                        {
                            var idle = double.Parse(m.Groups[1].Value);
                            var used = 100 - idle;
                            return used;
                        }
                    }
    
                    // Or throw an exception
                    return -1.0;
                }
                else
                {
                    // E.g., Melih Altıntaş's solution: https://stackoverflow.com/a/59465268/132042
                    ...
                }
            }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多