从获得.NET程序的全球根 [英] Obtaining global roots from .NET programs

查看:185
本文介绍了从获得.NET程序的全球根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用蚂蚁分析工具进行生产的工作。除了其迷死被惊讶,我忍不住想知道他们是如何工作的。例如,最有用的功能之一,您可以可视化运行的程序完成引用了不同类型的值数的全球根源。

I recently started using the ANTS profiling tools for production work. Aside from being amazed by their awesomeness, I couldn't help but wonder how they work. For example, one of the most useful features lets you visualize the global roots of a running program complete with the number of references to values of different types.

请问这个工具得到的信息保持

How does this tool get hold of that information?

推荐答案

(全面披露:我在Visual Studio中探查球队,但下面的信息公众)

(Full disclosure: I'm on the Visual Studio Profiler team, but the below information is public)

您可以通过编写您所定位的过程中运行的CLR探查做到这一点。 CLR分析器是得到运行时实例化 COR_PROFILER COR_PROFILING_ENABLED 环境变量设置(见当C ++ COM对象< A HREF =http://msdn.microsoft.com/en-us/library/bb384689.aspx相对=nofollow>这里)。主要有两种 CLR分析的接口,特别的 ICorProfilerCallback 和的 ICorProfilerInfo ICorProfilerCallback 是什么CLR用来通知您,您订阅(模块加载,功能JIT compliation,创建线程,GC事件)的特定事件,而 ICorProfilerInfo 可以通过您的分析器可用于获取有关线程,模块,类型,方法和元数据的加载的程序集的附加信息。这个接口就是你的可能的使用,以获取有关分配的类型符号信息。

You can do this by writing a CLR profiler that runs inside the process you're targeting. CLR profilers are C++ COM objects that get instantiated by the runtime when the COR_PROFILER and COR_PROFILING_ENABLED environment variables are set (see here). There are two main CLR profiling interfaces, specifically, ICorProfilerCallback and ICorProfilerInfo. ICorProfilerCallback is what the CLR uses to notify you about specific events that you subscribe to (module loads, function JIT compliation, thread creation, GC events), while ICorProfilerInfo can be used by your profiler to obtain additional information about threads, modules, types, methods, and metadata for the loaded assemblies. This interface is what you could use to obtain symbol information about the types allocated.

随着进程的探查,你可以强制GC通过 ICorProfilerInfo :: ForceGC 。在GC完成后,你的分析器将获得通过 ICorProfilerCallback2通知: :GarbageCollectionFinished ,你会得到通过的 ICorProfilerCallback2 :: RootReferences2 。当您与 ICorProfilerCallback :: ObjectReferences <结合根参考信息/ code> ,你可以为你的.NET应用程序的完整对象引用图表。

With your profiler in-process, you can force a GC through ICorProfilerInfo::ForceGC. After the GC completes, your profiler will get notified via ICorProfilerCallback2::GarbageCollectionFinished, and you will get the root references via ICorProfilerCallback2::RootReferences2. When you combine the root reference information with ICorProfilerCallback::ObjectReferences, you can get the complete object reference graph for your .NET application.

您可以通过获得更多的实时信息< A HREF =http://msdn.microsoft.com/en-us/library/ms233146.aspx相对=nofollow> ICorProfilerCallback :: ObjectAllocated 回调,以确定何时获得创建单独的CLR对象。这可能是昂贵的,虽然,因为你至少承担一个额外的函数调用每一个分配的对象。您可以通过映射跟踪单个对象的CLR分配对象ID 来自己的内部ID。一个对象ID 给定对象是一个短暂的指针,因为它可以改变,因为垃圾收集发生,这可能会导致物体压实过程中移动。这个过程是说明 rel=\"nofollow\">。您可以从 ICorProfilerCallback :: MovedReferences 追踪移动物体。

You can get more realtime information by using the ICorProfilerCallback::ObjectAllocated callback to determine when individual CLR objects get created. This can be expensive, though, since you're incurring at least an additional function call for each allocated object. You can track individual objects by mapping the CLR-assigned ObjectID to your own internal ID. An ObjectID for a given object is an ephemeral pointer since it can change as garbage collections happen, which can cause objects to move during compaction. This process is illustrated here. You can use the information from ICorProfilerCallback::MovedReferences to track moving objects.

为了激活上面提到的回调,你需要告诉CLR分析API,你有兴趣在他们中。您可以通过指定 COR_PRF_MONITOR_GC COR_PRF_MONITOR_OBJECT_ALLOCATED 为您的活动标志的一部分调用的 ICorProfilingInfo :: SetEventMask

In order to activate the callbacks mentioned above, you need to tell the CLR profiling API that you're interested in them. You can do this by specifying COR_PRF_MONITOR_GC and COR_PRF_MONITOR_OBJECT_ALLOCATED as part of your event flags when calling ICorProfilingInfo::SetEventMask.

大卫Broman是在CLR探查开发商和的他的博客对一般包括所有的疯狂的陷阱,你可能会遇到问题剖析吨的大量信息。

David Broman is the developer on the CLR profiler, and his blog has tons of great information on profiling in general, including all the crazy pitfalls and issues you might run into.

这篇关于从获得.NET程序的全球根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆