dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.34k stars 4.74k forks source link

PerformanceCounters - Read OS counters on .NET Core #15468

Closed manu-st closed 4 years ago

manu-st commented 9 years ago

Currently CoreFX generates the reference assembly System.Diagnostics.PerformanceCounter.dll however, there doesn't seem to be any counter part implementation for it, nor can I see an entry for this in the progress page at https://github.com/dotnet/corefx-progress/blob/master/src-diff/README.md.

Is this expected?

Thanks

joshfree commented 8 years ago

related https://github.com/dotnet/corefx/issues/4577

karelz commented 8 years ago

Next step: We need implementation of the Desktop API surface - Windows can just port it from Desktop. Mono has implementation on Linux, maybe we can leverage part of it here for Linux.

Complexity: Medium (packaging will be similar to Registry)

vancem commented 7 years ago

Perf counters mean different things. Often people only want to READ existing perf counters. This is relative straightforward, and is the kind of thing that we might add (or others could simply make a clone of this that is .NET Core compatible). For this case, however we really would rather we identify the most important of these that are CROSS PLATFORM (e.g. working set, CPU usage ...), and provide a SIMPLER interface to get these that is platform neutral (basically a class or set of classes that have properties that represent he live value of this in a particular process).

However creating NEW performance counter (you are logging new data), is more problematic because windows Performance counters require ADMIN install, and are a MACHINE WIDE SHARED RESOURCE (which makes xcopy deployment and side-by-side problematic).

We would prefer that people who need to log use EventSources or EventCounters for this.

See Event Counter Tutorial for more on event counters.

See EventSource Tutorial for more on EventSource.

karelz commented 7 years ago

Note: Fully x-plat Performance Counters are tracked by dotnet/corefx#4577.

roji commented 7 years ago

Just wanted to second @vancem's distinction between reading existing perf counters vs. creating a publishing new ones. I maintain Npgsql, which publishes connection pool perf information on Windows (just like SqlClient does), and it's important for something like this to be possible in a x-plat way with .NET Core.

galvesribeiro commented 7 years ago

@roji yeah we understand that. Perhaps we should have a revamp on those counters in .Net Core to make it crossplat.

danmoseley commented 7 years ago

Also note that if and when we implement this, the ref definition is incomplete. It is missing at least these from its desktop counterparts:

MembersMustExist : Member 'System.Diagnostics.PerformanceCounter.CloseSharedResources()' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'System.Diagnostics.PerformanceCounter.Decrement()' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'System.Diagnostics.PerformanceCounter.Dispose(System.Boolean)' does not exist in the implementation but it does exist in the contract.
karelz commented 7 years ago

Looks like PerformanceCoutners are used for example by NLog, we could look at it as example of customer usage, to see how to migrate such code to .NET Core / .NET Standard.

bloudraak commented 7 years ago

I started a project to allow an external system to get performance metrics. I'm really interested not only in extracting metrics or"performance counters", but an API to discover them and report them for a given process.

I'm actively working on making it possible for Prometheus amongst others to extract metrics from an aspnet core process.

So in summary, I'd like to discover what metrics are available in the process (be it specific to the app, a subsystem like EF or ASPNET, platform like GC or exceptions/sec , or OS like memory, disk queue length.

@karelz

mi-hol commented 7 years ago

Also Powershell Core is affected by missing basic PerformanceCounter features like ElapsedTime https://github.com/PowerShell/PowerShell/issues/4295 From my view adding just the basic 'read' counters dealing with time measures would be a great improvement for many use cases and would boost compatibility! This improvement could potentially be accomplished within just a few hours :)

danmoseley commented 7 years ago

cc @brianrob

evil-shrike commented 7 years ago

Currently (2.0) we can reference a nuget/assembly lib built for full .net in .net core 2.0. But if that library uses PerformanceCounterType we'll get runtime exception like: Message: System.TypeLoadException : Could not load type 'System.Diagnostics.PerformanceCounterType' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. I understand that it's expected. But is these any workaround for this? Like a nuget package with stubs for all PerformanceCounters types?

danmoseley commented 7 years ago

@brianrob ?

brianrob commented 7 years ago

I suspect that there are no workarounds at this time as PerformanceCounters are a desktop-only feature. Do you have a scenario where you need to reference this type other than for using PerformanceCounters, which don't currently run on core?

evil-shrike commented 7 years ago

I encountered runtime exception Could not load type 'System.Diagnostics.PerformanceCounterType' while tried to use Devart's ado.net provider for Postgres (built for .net fw 3.5) on core2.

danmoseley commented 7 years ago

Folks who would use this on .NET Core please thumb up the top post.

smbecker commented 7 years ago

For those that need this sooner, I used this article to implement what I needed. Then I just light up the various perf counters that I need when running on windows.

danmoseley commented 6 years ago

This port is done (eg https://github.com/dotnet/corefx/pull/24579)!

It is currently up on nuget marked preview. It will be marked stable next year, but can be used now (we don't know of issues with it.) It is included in the new compat pack. so it's easiest to just reference that to get it.

I'll close this as complete - please try out the package and open new issues with any bugs you find.

blugri commented 6 years ago

I am trying to use this in a .NET core 2.0 application to use a 3rd party library that is written in .NETFramework,Version=v4.6.1. Unfortunately I still get the following exception

TypeLoadException: Could not load type 'System.Diagnostics.PerformanceCounter' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

@danmosemsft Am I doing something wrong? Should your port solve my issue?

Cheers

danmoseley commented 6 years ago

@blugri can you please instead reference the full compat pack and see I'd that fixes this? https://www.nuget.org/packages/Microsoft.Windows.Compatibility

blugri commented 6 years ago

@danmosemsft Thanks. It is working :-)