共用方式為


ASP.NET Internals – Prefetch and Multi-Core Jitting.

Introduction.

A couple of new features are introduced in ASP.NET 4.5 to improve startup time of web apps. Both features use a flag on the web.config to enable them. The features don’t do any magic by themself. They just enable features available on the operating system underneath or in the CLR APIs.

Prefetch feature

The prefetch feature only works on Windows 8 Server or newer OS. It requires the Os to be configured and the enablePrefetchOptimization flag on the <system.web/compilation> section of web.config to be set. The flag only has effect at the app level. No subdirectory config or location tag makes sense here. The setting is set to false by default, so users must explicitly set it to true in order for it to work.

To enable the feature, run the Enable-MMAgent power shell command:

    powershell Enable-MMAgent -OperationAPI

The SuperFetch service might need to be started. Verify is running:

C:\>sc query sysmain

 

SERVICE_NAME: sysmain

        TYPE : 20 WIN32_SHARE_PROCESS

        STATE : 4 RUNNING

                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)

        WIN32_EXIT_CODE : 0 (0x0)

        SERVICE_EXIT_CODE : 0 (0x0)

        CHECKPOINT : 0x0

        WAIT_HINT : 0x0

Prefetch Implementation.

The features simple uses the operation recorder API. See OperationStart and OperationEnd APIs on msdn. This APIs require an integer used as an Id of the operation. We use a hash code of the app domain app id, which is a string.

string domainId = HttpRuntime.AppDomainAppId;

int operationId = domainId.GetHashCode();

 

The OperationStart call is made the first time the app domain is created. The OperationEnd API is called at the end of the request in which we called OperationStart. A .pf file should be created on the prefetch directory:

C:\>dir %windir%\Prefetch

 Volume in drive C has no label.

 Volume Serial Number is CC5B-113C

 

 Directory of C:\Windows\Prefetch

 

02/16/2012 04:17 PM <DIR> .

02/16/2012 04:17 PM <DIR> ..

02/16/2012 04:17 PM 164,832 Op-W3WP.EXE-90E54C33-765F360E.pf

               1 File(s) 164,832 bytes

               2 Dir(s) 30,203,154,432 bytes free

Multi-Core Jitting feature.

The multi-core jitting feature is enabled by default, and work on all OS where ASP.NET 4.5 is available. You can disable it by setting the profileGuidedOptimizations enumerated value on the <system.web/compilation> section on the web.config, to None.

Multi-Core Jitting Implementation.

The feature uses the new class ProfileOptimization class of the System.Runtime namespace. The class is called the first time the app domain is created. First, we set the profile root to the codegen directory, which by default is something like:

"%windir%\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\myapp\weirdhash1\weirdhash2"

Remember that the temporary files folder value can be changed on tempDirectory attribute of the <system.web/compilation> section of the web.config.

After setting the profile root, we call StartProfile() with the filename "profileoptimization.prof". The CLR code underneath handles all the magic. If you examine the contents of the temporary ASP.NET file folder, you will see the file there:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\dotnetnuke\59a60e53\a2ac2624>dir *.prof

 Volume in drive C has no label.

 Volume Serial Number is CC5B-113C

 

 Directory of C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\dotnetnuke\59a60e53\a2ac2624

 

02/16/2012 04:38 PM 9,828 profileoptimization.prof

               1 File(s) 9,828 bytes

               0 Dir(s) 30,203,035,648 bytes free    

Conclusion.

The ASP.NET Prefetch feature uses the operation recording APIs to improve startup time of web applications. The feature is enabled when a Windows Server 8 machine is configured for the operation API and enabledPrefetchOptimization flag is set on the web.config.

The multi-core jitting feature is available in more operating systems and is on by default. It uses a new CLR feature for profile base optimization.

Thanks for reading.

Originally posted at https://blogs.msdn.com/b/josere/