I recently migrated from c# to .net core. In c# I use to get CPU usage with this:

```
PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;

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

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

```

but in .net core `PerformanceCounter` is not available what is the solution ? please give me an advice.
['https://Stackoverflow.com/questions/59465212', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11589744/']
Performance counters are not in Linux thus not in NET Core. Alternative way:

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

```
On Mac, I went the same route as [you already have to go to get memory usage](https://apple.stackexchange.com/questions/317483/how-to-calculate-used-memory-on-mac-os-by-command-line): shell out to a command-line utility, such as `top`, and parse the output.

Here's my code:

```

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

```