Skip to content

Profiling: How to get GC Metrics in-process #5638

@discostu105

Description

@discostu105

Let's say, you have a profiler which is loaded inside of the process via Profiling API. Let's say you want to capture the impact the GC has on your process (just the CLR in the process your profiler is loaded in). Stats that would be interesting are: Gen0,1,2,LOH collection count and duration. Which native API would I use to properly get such statistics periodically with low overhead?

Some thoughts:

  • In full framework, this would be very possible via CLR Memory Performance counter metrics ("% Time in GC", "gen X collections", ect). However, perfcounters have been removed for CoreCLR.
  • Yes, there are callbacks from the Profiling API (ICorProfilerCallback::RuntimeSuspendStarted) that indicate "suspensions". However, these callbacks don't give much details on the reason of suspension (e.g. no info about which generation is being collected). Also, I am unsure about background/concurrent GC. I guess those won't trigger runtime suspensions (in all cases).
  • Then there is ETW. At first, it does not give you nice statistics, but events instead. So, you can capture events such as GCStart, GCEnd and you have to calculate the timings you want yourself. So far so good, but it seems that efficiently capturing ETW events from within the process (via native API) is not an easy thing. One way seem to be "private sessions", but apparently, they only work via log-files that need to be parsed [0]. That seems much too complicated and too much overhead (file-IO) for just getting some GC-stats.
  • In ETW, there are also so called "real time sessions", which seem to be more what I want (no file IO, just in-memory buffers). However, there's a global limit of how many such sessions can be active, and also it requires admin-privileges to create them. Not optimal either.
  • Browsing through the sources, I've found COR_GC_STATS to be an interesting datastructure. It contains pretty much what I am looking. It's available via CorGCHost::GetStats. Is there any way for a profiler to get to a CorGCHost instance (if the CLR is not self-hosted)?

[0] https://social.msdn.microsoft.com/Forums/en-US/2664a9cf-172a-45c3-81cc-5a4b93b3be7d/etw-private-session-without-logfile?forum=clr

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-GC-coreclrquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions