我们在生产环境中使用简单的 ASP.NET Web 应用程序(ASP.NET 2.0),有 2 个 Web 服务器,每个 Web 服务器有 64GB RAM 和 32 个核心。生产站点有时会突然将两台服务器上的 CPU 飙升至 90-100%(当然只有 w3wp.exe)。此外,我们发现 pagefile.sys 也变得太大(曾经超过 70GB)。我们对 w3wp.exe 进程进行了一些转储,发现了一些有趣的结果。例如,我们发现一个线程仅执行就需要 22 分钟,
Thread 2840
General Operation Info
Type Unknown Operation
Dumps present All dumps
Elapsed Time (unknown)
Thread Info
Entry point mscorwks!Thread::intermediateThreadProc
System ID 2840 (0xB18)
Create time 21/01/2018 10:55:34
All functions in this operation (excludes boiler-plate functions)
System.Security.CodeAccessSecurityEngine.Assert(System.Security.CodeAccessPermission, System.Threading.StackCrawlMark ByRef)
System.Security.CodeAccessPermission.Assert()
System.Security.PermissionToken.FindToken(System.Type)
System.Security.PermissionSet.RemovePermission(System.Type)
System.Security.PermissionSet.CopyWithNoIdentityPermissions()
System.Security.CodeAccessSecurityEngine.ReflectionTargetDemandHelper(Int32, System.Security.PermissionSet, System.Threading.CompressedStack)
System.Security.CodeAccessSecurityEngine.Assert(System.Security.CodeAccessPermission, System.Threading.StackCrawlMark ByRef)
System.Security.CodeAccessPermission.Assert()
System.Security.PermissionToken.FindToken(System.Type)
System.Security.PermissionSet.RemovePermission(System.Type)
System.Security.PermissionSet.CopyWithNoIdentityPermissions()
System.Security.CodeAccessSecurityEngine.ReflectionTargetDemandHelper(Int32, System.Security.PermissionSet, System.Threading.CompressedStack)
User Time 00:22:42.359
Kernel time 00:01:59.765
我的问题是为什么 CodeAccessSecurityEngine 要花 22 分钟才能执行?
我们在转储中发现至少有 48 个线程使用 CodeAccessSecurityEngine 并耗费大量时间。更多示例如下:
Thread 8092
General Operation Info
Type Unknown Operation
Dumps present All dumps
Elapsed Time (unknown)
Thread Info
Entry point mscorwks!Thread::intermediateThreadProc
System ID 8092 (0x1F9C)
Create time 21/01/2018 10:25:40
System.String.InternalSubString(Int32, Int32, Boolean)
System.RuntimeType.SplitName(System.String, System.String ByRef, System.String ByRef)
System.RuntimeType.GetInterface(System.String, Boolean)
System.Security.PermissionToken.FindToken(System.Type)
System.Security.PermissionSet.RemovePermission(System.Type)
System.Security.PermissionSet.CopyWithNoIdentityPermissions()
System.Security.CodeAccessSecurityEngine.ReflectionTargetDemandHelper(Int32, System.Security.PermissionSet, System.Threading.CompressedStack)
User Time 00:26:49.734
Kernel time 00:02:38.296
-------------------------------------------------------------------------------
Thread 2360
General Operation Info
Type Unknown Operation
Dumps present All dumps
Elapsed Time (unknown)
Thread Info
Entry point mscorwks!Thread::intermediateThreadProc
System ID 2360 (0x938)
Create time 21/01/2018 10:47:53
System.Collections.Generic.List`1[[System.__Canon, mscorlib]].set_Capacity(Int32)
System.Collections.Generic.List`1[[System.__Canon, mscorlib]].EnsureCapacity(Int32)
System.Collections.Generic.List`1[[System.__Canon, mscorlib]].Add(System.__Canon)
System.RuntimeType.GetMethodCandidates(System.String, System.Reflection.BindingFlags, System.Reflection.CallingConventions, System.Type[], Boolean)
System.RuntimeType.GetMethodImpl(System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Reflection.CallingConventions, System.Type[], System.Reflection.ParameterModifier[])
System.Security.PermissionToken.FindToken(System.Type)
System.Security.PermissionSet.RemovePermission(System.Type)
System.Security.PermissionSet.CopyWithNoIdentityPermissions()
System.Security.CodeAccessSecurityEngine.ReflectionTargetDemandHelper(Int32, System.Security.PermissionSet, System.Threading.CompressedStack)
User Time 00:25:37.671
Kernel time 00:02:29.953
-------------------------------------------------------------------------------
Thread 7008
General Operation Info
Type Unknown Operation
Dumps present All dumps
Elapsed Time (unknown)
Thread Info
Entry point mscorwks!Thread::intermediateThreadProc
System ID 7008 (0x1B60)
Create time 21/01/2018 10:54:20
System.Reflection.MethodBase.CheckArguments(System.Object[], System.Reflection.Binder, System.Reflection.BindingFlags, System.Globalization.CultureInfo, System.Signature)
System.Reflection.RuntimeConstructorInfo.Invoke(System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
System.RuntimeType.CreateInstanceImpl(System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo, System.Object[])
System.Security.PermissionSet.AddPermission(System.Security.IPermission)
System.Security.CodeAccessSecurityEngine.ReflectionTargetDemandHelper(Int32, System.Security.PermissionSet, System.Threading.CompressedStack)
User Time 00:23:01.656
Kernel time 00:02:04.625
-------------------------------------------------------------------------------
Thread 5584
General Operation Info
Type Unknown Operation
Dumps present All dumps
Elapsed Time (unknown)
Thread Info
Entry point mscorwks!Thread::intermediateThreadProc
System ID 5584 (0x15D0)
Create time 21/01/2018 10:53:13
System.Security.CodeAccessSecurityEngine.ReflectionTargetDemandHelper(Int32, System.Security.PermissionSet, System.Threading.CompressedStack)
User Time 00:23:40.421
Kernel time 00:02:08.296
已使用调试诊断进行转储和分析,如下所示 https://docs.microsoft.com/en-us/iis/troubleshoot/performance-issues/troubleshooting-high-cpu-in-an-iis-7x-application-pool
CodeAccessSecurityEngine 真的是罪魁祸首吗?还是其他什么?