I'm googling like crazy and I can't find an answer to something that should be pretty basic; reporting on the CPU & memory usage of a .net core process. We want our application to run on Linux containers (eventually) so what is inside the standard that would allow me to do this ?
Thanks
I don't think we have any APIs in .NET Core (or .NET) that allow you to directly get. @vancem do you have any recommendations?
Closing this issue as I don't think there is any action in the standard repo. If you would like you could ask your question in http://github.com/dotnet/corefx on how to do what you need in general.
System.Diagnostic.Process probably does what you want. Here
var proc = Process.GetCurrentProcess();
var mem = proc.WorkingSet64;
var cpu = proc.TotalProcessorTime;
Console.WriteLine("My process used working set {0:n3} K of working set and CPU {1:n} msec", mem / 1024.0, cpu.TotalMilliseconds);
foreach (var aProc in Process.GetProcesses())
Console.WriteLine("Proc {0,30} CPU {1,-20:n} msec", aProc.ProcessName, cpu.TotalMilliseconds);
Isn't there a simple way of getting CPU % and memory % for the whole OS? I mean for logging it would be very useful to log that a machine is running low on memory or has heavy CPU load without having to loop over all processes.
@opinionmachine I think that is covered by https://github.com/dotnet/corefx/issues/22660. Feel free to contribute there.
I actually think this issue may be distinct from https://github.com/dotnet/corefx/issues/22660 since this one only cares about the 'basics' (CPU usage and memory), and would probably be surfaced differently in the APIs (probably as APIs on System.Diagnostics.Process rather than a for 22660 it is probably a whole new class/dom).
As mentioned above, we already supply CPU and Memory stats on System.Diagnostics.Process. It is true we don't have something for all processes, but it is a small number of lines of code to do the accumulation (we could provide a API for that (implemented maybe as iteration or by calling direct OS apis).
For what it is worth...
Most helpful comment
Isn't there a simple way of getting CPU % and memory % for the whole OS? I mean for logging it would be very useful to log that a machine is running low on memory or has heavy CPU load without having to loop over all processes.