Profiling methods in generic typeHi,<br/> <br/>   I recently discovered a bug in my IL-rewriting profiler.  A method in a generic type is being re-JITted given different generic parameters.  For example, the methods MyType&lt;int&gt;.Foobar() and MyType&lt;string&gt;.Foobar() will each be JITted once with unique FunctionIDs but identical mdTokens.  Let's say my simplified profiler code looks like<br/> <br/> <pre>HRESULT ProfilerCallback::JITCompilationStarted(FunctionID functionID, BOOL isSafeToBlock) { ModuleID moduleID; mdToken token; LPBYTE original; ULONG sz; LPBYTE updated; fProfilerInfo-&gt;getFunctionInfo(functionID, NULL, &amp;moduleID, &amp;token); fProfilerInfo-&gt;GetILFunctionBody(moduleID, token, (LPCBYTE*)&amp;original, (ULONG*)&amp;sz); // ... insert IL equivalent of Console.WriteLine(&quot;Hello World&quot;); ... fProfilerInfo-&gt;SetILFunctionBody(moduleID, token, updated); return S_OK; }</pre> <br/> My questions are<br/> <br/>   1.  In the case where MyType&lt;int&gt;.Foobar() and MyType&lt;string&gt;.Foobar() are called, this code would cause MyType&lt;string&gt;.Foobar() to be instrumented twice.  When I invoke MyType&lt;string&gt;.Foobar(), &quot;Hello World&quot; would be printed twice.  To the callback JITCompilationStarted, modifying IL for (identical) method token fetched from the two (unique) function ID does not make sense, so why is it being JIT-ted multiple times?<br/>   2.  Did re-JIT just work???  Or was this a lucky accident?<br/> <br/> Yitao<br/>© 2009 Microsoft Corporation. All rights reserved.Wed, 08 Jul 2009 13:39:30 Zdf84711e-542a-4236-8702-2f74475b3e2dhttp://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/df84711e-542a-4236-8702-2f74475b3e2d#df84711e-542a-4236-8702-2f74475b3e2dhttp://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/df84711e-542a-4236-8702-2f74475b3e2d#df84711e-542a-4236-8702-2f74475b3e2dYitaohttp://social.msdn.microsoft.com/Profile/en-US/?user=YitaoProfiling methods in generic typeHi,<br/> <br/>   I recently discovered a bug in my IL-rewriting profiler.  A method in a generic type is being re-JITted given different generic parameters.  For example, the methods MyType&lt;int&gt;.Foobar() and MyType&lt;string&gt;.Foobar() will each be JITted once with unique FunctionIDs but identical mdTokens.  Let's say my simplified profiler code looks like<br/> <br/> <pre>HRESULT ProfilerCallback::JITCompilationStarted(FunctionID functionID, BOOL isSafeToBlock) { ModuleID moduleID; mdToken token; LPBYTE original; ULONG sz; LPBYTE updated; fProfilerInfo-&gt;getFunctionInfo(functionID, NULL, &amp;moduleID, &amp;token); fProfilerInfo-&gt;GetILFunctionBody(moduleID, token, (LPCBYTE*)&amp;original, (ULONG*)&amp;sz); // ... insert IL equivalent of Console.WriteLine(&quot;Hello World&quot;); ... fProfilerInfo-&gt;SetILFunctionBody(moduleID, token, updated); return S_OK; }</pre> <br/> My questions are<br/> <br/>   1.  In the case where MyType&lt;int&gt;.Foobar() and MyType&lt;string&gt;.Foobar() are called, this code would cause MyType&lt;string&gt;.Foobar() to be instrumented twice.  When I invoke MyType&lt;string&gt;.Foobar(), &quot;Hello World&quot; would be printed twice.  To the callback JITCompilationStarted, modifying IL for (identical) method token fetched from the two (unique) function ID does not make sense, so why is it being JIT-ted multiple times?<br/>   2.  Did re-JIT just work???  Or was this a lucky accident?<br/> <br/> Yitao<br/>Wed, 01 Jul 2009 17:58:39 Z2009-07-01T17:59:30Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/df84711e-542a-4236-8702-2f74475b3e2d#d915e8f5-047e-42db-a23f-d271601ed1a4http://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/df84711e-542a-4236-8702-2f74475b3e2d#d915e8f5-047e-42db-a23f-d271601ed1a4David Bromanhttp://social.msdn.microsoft.com/Profile/en-US/?user=David%20BromanProfiling methods in generic type<p>Hi, Yitao.  With generics, you'll see a single methodDef in metadata for the function's definition (MyType&lt;T&gt;.Foobar()), but multiple FunctionIDs for different instantiations.  Generally, for each instantiation using a value type you'll see a different FunctionID, and all instantiations using a reference type share a FunctionID.</p> <p>Since generics instantiate into multiple FunctionIDs, you see separate JIT notifications for them.  As you discovered, you must be careful with your instrumentation when you see the second, third, etc., JIT for the same methodDef.  You need to scan through the IL the CLR gives you, and not re-instrument it, if it's already instrumented.  (Often you can tell, as your instrumentation probably does something rather unique that you can search for.)</p> <p><br/>Thanks,<br/>Dave</p>Thu, 02 Jul 2009 16:42:05 Z2009-07-02T16:42:05Z