{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/4382","repository_url":"https://api.github.com/repos/dotnet/coreclr","labels_url":"https://api.github.com/repos/dotnet/coreclr/issues/4382/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/4382/comments","events_url":"https://api.github.com/repos/dotnet/coreclr/issues/4382/events","html_url":"https://github.com/dotnet/coreclr/issues/4382","id":148896143,"number":4382,"title":"Profiling: How to get GC Metrics in-process","user":{"login":"discostu105","id":10918780,"avatar_url":"https://avatars.githubusercontent.com/u/10918780?v=3","gravatar_id":"","url":"https://api.github.com/users/discostu105","html_url":"https://github.com/discostu105","followers_url":"https://api.github.com/users/discostu105/followers","following_url":"https://api.github.com/users/discostu105/following{/other_user}","gists_url":"https://api.github.com/users/discostu105/gists{/gist_id}","starred_url":"https://api.github.com/users/discostu105/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/discostu105/subscriptions","organizations_url":"https://api.github.com/users/discostu105/orgs","repos_url":"https://api.github.com/users/discostu105/repos","events_url":"https://api.github.com/users/discostu105/events{/privacy}","received_events_url":"https://api.github.com/users/discostu105/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/coreclr/labels/GC","name":"GC","color":"d4c5f9"},{"url":"https://api.github.com/repos/dotnet/coreclr/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2016-04-16T21:49:41Z","updated_at":"2016-04-19T22:43:32Z","closed_at":null,"body":"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?\r\n\r\nSome thoughts:\r\n * 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.\r\n * 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).\r\n * 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.\r\n * 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.\r\n * 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)?\r\n\r\n[0] https://social.msdn.microsoft.com/Forums/en-US/2664a9cf-172a-45c3-81cc-5a4b93b3be7d/etw-private-session-without-logfile?forum=clr"},"comment":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/comments/212157862","html_url":"https://github.com/dotnet/coreclr/issues/4382#issuecomment-212157862","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/4382","id":212157862,"user":{"login":"Maoni0","id":10837357,"avatar_url":"https://avatars.githubusercontent.com/u/10837357?v=3","gravatar_id":"","url":"https://api.github.com/users/Maoni0","html_url":"https://github.com/Maoni0","followers_url":"https://api.github.com/users/Maoni0/followers","following_url":"https://api.github.com/users/Maoni0/following{/other_user}","gists_url":"https://api.github.com/users/Maoni0/gists{/gist_id}","starred_url":"https://api.github.com/users/Maoni0/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Maoni0/subscriptions","organizations_url":"https://api.github.com/users/Maoni0/orgs","repos_url":"https://api.github.com/users/Maoni0/repos","events_url":"https://api.github.com/users/Maoni0/events{/privacy}","received_events_url":"https://api.github.com/users/Maoni0/received_events","type":"User","site_admin":false},"created_at":"2016-04-19T22:43:32Z","updated_at":"2016-04-19T22:43:32Z","body":"I have been advocating that we should have a low overhead GC profiling mode (but haven't succeeded :-) apparently this hasn't been an urgent thing to have for most of our customers who use the profiling API) - for some people it's enough to know some stats that are very cheap for the runtime to provide (eg, they don't need the root reference, or moved references and etc). And profiling has the very nice attribute of being real time. It shouldn't be much work to allow an additional mode that gives you a subset of things GC profiling currently offers (you don't have to disable concurrent GC - it was just work that hasn't been done to enable it). If you want, you are certainly welcome to open an issue for it to discuss if someone (could be yourself :)) wants work on it."}}