views:

113

answers:

2

I’m running some performance tests on an ASP.NET MVC application. I see a high contention rate and the number of threads increasing overtime. I believe the two to be related, as threads are blocked new threads are created by the thread pool to handle incoming requests. I believe this in turn is making the contention worse (i.e. more threads more contention).

The correct approach is probably to take the cause of the contention, i.e. make the critical sections smaller, verify that all the locks are really needed etc. However, as an intermitted step I’d like to limit the number of threads that can be created by the thread pool. My belief is that all though this may lead to requests staying in the queue longer it will improve overall though put as it will reduce contention and thread context switching.

However, I can find how to configure this in IIS 7.5, can anyone help me?

Thanks, Rob

A: 

If possible, you could call ThreadPool.SetMaxThreads from code.

EDIT:

It seems a better place to make this change would be with the processModel/maxWorkerThreads property in the web.config file:

<configuration>
  <system.web>
    <processModel maxWorkerThreads="5"/>
...

Would mean max 5 threads per CPU.

spender
Not possible for apps hosted in IIS.
Robert
If you trying adding the configuration you suggest the web app crashes with an error state the config used can only be placed in the machine.config. I found this post (http://blogs.msdn.com/b/tmarq/archive/2007/07/21/asp-net-thread-usage-on-iis-7-0-and-6-0.aspx) which suggest it can be controlled though a reg key or aspnet.config (depending on your service pack), but this is only true for iis 7.0 not 7.5.
Robert
A: 

Did finally find how to do this on IIS 7.5 you need to add a CLRConfigFile attribute to the applicationHost.config (located in C:\Windows\System32\inetsrv\config).

    <add name="ASP.NET v4.0" CLRConfigFile="C:\code\ThreadLeakWebSite\apppool.config" managedRuntimeVersion="v4.0" managedPipelineMode="Integrated" />

You can then add the parameters restricting the number of threads to the apppool.config config you point to, i.e.:

<configuration>
    <system.web>
        <applicationPool maxConcurrentRequestsPerCPU="5000" maxConcurrentThreadsPerCPU="0" requestQueueLimit="5000"/>
    </system.web>
</configuration>
Robert