This topic discusses the flow of Microsoft intermediate language (MSIL) code to native code and how a profiler can control code generation.
Manual compilation: The Native Image Generator (NGen.exe) tool can be used to create a native image.
Automatic compilation: The common language runtime (CLR) may perform just-in-time (JIT) compilation at run time.
Both NGen.exe and the JIT compiler provide flags that control code generation.
When an assembly is loaded, the CLR first looks for a native image for the assembly. If it cannot find a native image with the right set of code-generation flags, the CLR will JIT-compile the functions in the assembly as they are needed during the run. Even when a native image is found and loaded, the CLR may JIT-compile some of the functions in the assembly.
The profiler uses the flags in the following table to control code generation.
(Requires the .NET Framework version 2.0.) Causes the native image search to look for profiler-enhanced images (ngen /profile). If the search fails to find a profiler-enhanced native image for a given assembly, the CLR will instead JIT-compile the methods in that assembly as they are needed.
Has no effect on JIT-compiled code.
Has no effect on the native image search.
In JIT compilation, disables inlining. All other optimizations remain in effect.
In JIT compilation, disables all optimizations, including inlining.
Causes the native image search to look for profiler-enhanced images (ngen /profile).
In JIT compilation, inserts enter/leave hooks into the generated code.
In JIT compilation, inserts hooks at managed/unmanaged transition points.
Enter/leave hooks are inserted into the code.
Managed/unmanaged transition hooks are inserted into the code.
ICorProfilerCallback::JITCachedFunctionSearchStarted and ICorProfilerCallback::JITCachedFunctionSearchFinished notifications are issued as each function in the native image is invoked for the first time.
ICorProfilerCallback::ClassLoadStarted and ICorProfilerCallback::ClassLoadFinished notifications are issued as each class in the native image is used for the first time.
The method you use to profile your NGen-generated application depends on two considerations: the data that you need to acquire, and the effect of embedded profiling hooks on your application.
Regular native images (NGen-generated images without profiling hooks): These images do not cause any profiling overhead. However, the class load/unload callbacks are not available for regular native images. To handle this situation, a profiler that does not want to request profiler-enhanced native images will have to gather data about FunctionIDs or ClassIDs as the IDs are encountered. For example, a sampling profiler will not encounter a FunctionID when the FunctionID is first JIT-compiled or loaded from an NGen-generated image, because the class load/unload callbacks are not issued for regular NGen-generated images. The profiler will encounter the FunctionID later, when a sample shows that the process was executing at an instruction pointer (IP) inside the body of the function’s compiled code. In this case, the profiler could query for information about the function during the sampling, or it could log the FunctionID or its associated metadata token for querying even later. Therefore, the profiler will query for information about the FunctionID or ClassID only at the last possible moment (when it detects that the ID is actually used) instead of earlier, when the ID is first produced.
Profiler-enhanced native images (NGen-generated images with embedded profiling hooks): These images are larger, and they differ significantly from regular images. In addition, the behavior of the application may be different when it includes profiler hooks. Therefore, you should use profiler-enhanced native images only if the potential performance and behavioral consequences (overhead) are acceptable.