繁体   English   中英

GCFrame中卡住的高线程数导致CPU使用率过高

[英]High thread count stuck in GCFrame causes high CPU usage

我们有一个使用Kestrel来提供HTTP请求的应用程序。 我们在过去遇到过一些问题,高负载导致线程池产生数千个线程,此时我们会得到锁定护航症状。 大多数情况下,线程会在我们代码中的某个地方的Monitor.Enter()处开始相互阻塞,导致延迟和更多争用,直到应用程序由于上下文切换而导致100%CPU使用率无响应。 在我们重新启动应用程序之前,问题不会消失。

但是,我们已经删除了大多数锁并实现了限制机制,因此我们不允许超过1000个线程进入应用程序。 我们使用System.Threading.Semaphore类只允许一定数量的线程继续。 这解决了我们的锁争用问题,但可能引入了一个新问题:

我们仍然会得到100%CPU使用率和高线程数(500-1000个线程)的情况,尽管这次在Monitor.Enter()上没有阻塞线程。 相反,当我们进行线程转储(使用Microsoft.Diagnostics.Runtime.ClrRuntime )时,我们会看到以下调用堆栈(对于数百个线​​程):

thread id = 892
  GCFrame
  GCFrame
  HelperMethodFrame
  System.Threading.TimerQueueTimer.Fire()
  System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
  System.Threading.ThreadPoolWorkQueue.Dispatch()
  DebuggerU2MCatchHandlerFrame

在这种情况下,问题是应用程序变得无响应,但在大多数情况下,它会在几分钟后解决。 有时需要几个小时。

这样的调用栈是什么意思? 这是Kestrel的一个已知问题,还是Kestrel和Semaphore某种组合引起的?

更新:内存转储显示调用堆栈中的HelperMethodFrame可能是对Monitor.Enter()的调用。 但是,我们仍然无法确定这是在我们的代码中还是在Kestrel或其他库中。 当我们之前遇到锁定护航问题时,我们会在调用堆栈中看到我们的代码。 现在它似乎是TimerQueueTimerMonitor.Enter()调用,我们在代码中没有使用它。 内存转储如下所示:

.NET堆栈跟踪:

Child SP               IP Call Site
0000005a92b5e438 00007ff8a11c0c6a [GCFrame: 0000005a92b5e438] 
0000005a92b5e660 00007ff8a11c0c6a [GCFrame: 0000005a92b5e660] 
0000005a92b5e698 00007ff8a11c0c6a [HelperMethodFrame: 0000005a92b5e698] System.Threading.Monitor.Enter(System.Object)
0000005a92b5e790 00007ff88f30096b System.Threading.TimerQueueTimer.Fire()
0000005a92b5e7e0 00007ff88f2e1a1d System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
0000005a92b5e820 00007ff88f2e1f70 System.Threading.ThreadPoolWorkQueue.Dispatch()
0000005a92b5ed48 00007ff890413753 [DebuggerU2MCatchHandlerFrame: 0000005a92b5ed48]

完整堆栈跟踪:

 # Child-SP          RetAddr           : Args to Child                                                           : Call Site
00 0000005a`9cf5e9a8 00007ff8`9e6513ed : 00000000`00000000 00000000`00000001 00000000`00000001 00000000`00000000 : ntdll!ZwWaitForMultipleObjects+0xa
01 0000005a`9cf5e9b0 00007ff8`904e92aa : 0000005a`9cf5ef48 00007ff5`fffce000 0000005a`00000000 00000000`00000000 : KERNELBASE!WaitForMultipleObjectsEx+0xe1
02 0000005a`9cf5ec90 00007ff8`904e91bf : 00000000`00000001 00000000`00000000 0000005a`66b48e20 00000000`ffffffff : clr!WaitForMultipleObjectsEx_SO_TOLERANT+0x62
03 0000005a`9cf5ecf0 00007ff8`904e8fb1 : 0000005a`66b48e20 00000000`00000001 00000000`00000018 00007ff8`00000000 : clr!Thread::DoAppropriateWaitWorker+0x243
04 0000005a`9cf5edf0 00007ff8`90731267 : 00000000`00000000 00007ff8`00000001 0000004f`a419c548 0000004f`a419c548 : clr!Thread::DoAppropriateWait+0x7d
05 0000005a`9cf5ee70 00007ff8`90834a56 : 0000005a`5aec0308 0000005a`9cf5f0d0 00000000`00000000 0000005a`66b48e20 : clr!CLREventBase::WaitEx+0x28e6b7
06 0000005a`9cf5ef00 00007ff8`9083495a : 0000005a`5aec0308 0000005a`66b48e20 00000000`00000000 00000050`22945ab8 : clr!AwareLock::EnterEpilogHelper+0xca
07 0000005a`9cf5efc0 00007ff8`90763c8c : 0000005a`66b48e20 0000005a`5aec0308 0000005a`5aec0308 00000000`002d0d01 : clr!AwareLock::EnterEpilog+0x62
08 0000005a`9cf5f020 00007ff8`908347ed : 00000000`00000000 0000005a`9cf5f0d0 0000005a`5aec0308 0000005a`5aec0301 : clr!AwareLock::Enter+0x24390c
09 0000005a`9cf5f050 00007ff8`908338a5 : 00000050`22945ab8 0000005a`9cf5f201 0000005a`66b48e20 00007ff8`90419050 : clr!AwareLock::Contention+0x2fd
0a 0000005a`9cf5f110 00007ff8`8f30096b : 0000005a`5aec0308 0000005a`9cf5f2d0 0000005a`9cf5f560 00000000`00000000 : clr!JITutil_MonContention+0xc5
0b 0000005a`9cf5f2a0 00007ff8`8f2e1a1d : 00000051`a2bb6bb0 00007ff8`90417d0e 00000050`229491d8 0000005a`9cf5f330 : mscorlib_ni+0x49096b
0c 0000005a`9cf5f2f0 00007ff8`8f2e1f70 : 00000000`00000000 0000005a`9cf5f3a8 00000000`00000001 0000005a`9cf5f370 : mscorlib_ni+0x471a1d
0d 0000005a`9cf5f330 00007ff8`90413753 : 00000000`00000004 00000000`00000000 0000005a`9cf5f600 0000005a`9cf5f688 : mscorlib_ni+0x471f70
0e 0000005a`9cf5f3d0 00007ff8`9041361c : 00000050`22945ab8 00000000`00000000 0000005a`9cf5f640 0000005a`9cf5f6c8 : clr!CallDescrWorkerInternal+0x83
0f 0000005a`9cf5f410 00007ff8`904144d3 : 00000000`00000000 00000000`00000004 0000005a`9cf5f858 0000005a`9cf5f688 : clr!CallDescrWorkerWithHandler+0x4e
10 0000005a`9cf5f450 00007ff8`9041b73d : 0000005a`9cf5fb70 0000005a`9cf5fb20 0000005a`9cf5fb70 00000000`00000001 : clr!MethodDescCallSite::CallTargetWorker+0x2af
11 0000005a`9cf5f5e0 00007ff8`90416810 : 00000000`00000007 00007ff8`00000000 ffffffff`fffffffe 0000005a`66b48e20 : clr!QueueUserWorkItemManagedCallback+0x2a
12 0000005a`9cf5f6d0 00007ff8`904167c0 : 00670061`00500064 00000000`00730065 ffffffff`fffffffe 0000005a`66b48e20 : clr!ManagedThreadBase_DispatchInner+0x29
13 0000005a`9cf5f710 00007ff8`90416705 : ffffffff`ffffffff 00007ff8`90414051 0000005a`9cf5f7b8 00000000`ffffffff : clr!ManagedThreadBase_DispatchMiddle+0x6c
14 0000005a`9cf5f810 00007ff8`90416947 : ffffffff`ffffffff 0000005a`66b48e20 0000005a`66b48e20 00000000`00000001 : clr!ManagedThreadBase_DispatchOuter+0x75
15 0000005a`9cf5f8a0 00007ff8`9041b6a2 : 0000005a`9cf5f988 00000000`00000000 00000000`00000001 00007ff8`9e651118 : clr!ManagedThreadBase_FullTransitionWithAD+0x2f
16 0000005a`9cf5f900 00007ff8`904158ba : 0000005a`9cf5fb70 0000005a`9cf5fb68 00000000`00000000 00000000`00000200 : clr!ManagedPerAppDomainTPCount::DispatchWorkItem+0x11c
17 0000005a`9cf5fa90 00007ff8`904157da : 0000010b`010b010b 0000005a`9cf5fb20 00000000`00000000 0000005a`66b48e20 : clr!ThreadpoolMgr::ExecuteWorkRequest+0x64
18 0000005a`9cf5fac0 00007ff8`90433e1e : 00000000`00000000 00000000`00000000 00000000`00000001 00000000`0000041d : clr!ThreadpoolMgr::WorkerThreadStart+0x3b5
19 0000005a`9cf5fb60 00007ff8`9e7c13d2 : 00007ff8`90433da8 0000005a`5add4db0 00000000`00000000 00000000`00000000 : clr!Thread::intermediateThreadProc+0x7d
1a 0000005a`9cf5fca0 00007ff8`a11454e4 : 00007ff8`9e7c13b0 00000000`00000000 00000000`00000000 00000000`00000000 : kernel32!BaseThreadInitThunk+0x22
1b 0000005a`9cf5fcd0 00000000`00000000 : 00000000`00000000 00000000`00000000 00000000`00000000 00000000`00000000 : ntdll!RtlUserThreadStart+0x34

更新2:WinDbg syncblock命令给我们这个:

The version of SOS does not match the version of CLR you are debugging.  Please
load the matching version of SOS for the version of CLR you are debugging.
CLR Version: 4.6.1055.0
SOS Version: 4.6.1637.0
Index SyncBlock MonitorHeld Recursion Owning Thread Info  SyncBlock Owner
  148 0000005a5aec0308          426         0 0000000000000000     none    0000005022945ab8 System.Threading.TimerQueue
-----------------------------
Total           152
CCW             1
RCW             1
ComClassFactory 0
Free            66

更新3:更多挖掘表明我们有大约42000个Timer对象:

00007ff8871bedd0    41728      1001472 System.Runtime.Caching.MemoryCacheEqualityComparer
00007ff88f4a0998    42394      1017456 System.Threading.TimerHolder
00007ff8871bbed0    41728      1335296 System.Runtime.Caching.UsageBucket[]
00007ff88f51ab30    41749      1335968 Microsoft.Win32.SafeHandles.SafeWaitHandle
00007ff88f519de0    42394      1356608 System.Threading.Timer
00007ff8871be870    41728      1669120 System.Runtime.Caching.CacheUsage
00007ff88f50ea80    41734      2003232 System.Threading.ManualResetEvent
00007ff8871be810    41728      2336768 System.Runtime.Caching.CacheExpires
00007ff88f519f08    42390      2712960 System.Threading.TimerCallback
00007ff8871be558    41728      3338240 System.Runtime.Caching.MemoryCacheStore
00007ff88f4a0938    42394      3730672 System.Threading.TimerQueueTimer
00007ff8871be8d0    41728      4005888 System.Runtime.Caching.UsageBucket
00007ff8871bb9c8    41728     11016192 System.Runtime.Caching.ExpiresBucket[]

检查一些_methodPtr引用,它们都指向:

00007ff8`871b22c0 0f1f440000      nop     dword ptr [rax+rax]
00007ff8`871b22c5 33d2            xor     edx,edx
00007ff8`871b22c7 4533c0          xor     r8d,r8d
00007ff8`871b22ca 488d055ffeffff  lea     rax,[System_Runtime_Caching_ni+0x32130 (00007ff8`871b2130)]
00007ff8`871b22d1 48ffe0          jmp     rax

GC Traces看起来与此类似:

0:000> !gcroot 00000055629e5ca0
The version of SOS does not match the version of CLR you are debugging.  Please
load the matching version of SOS for the version of CLR you are debugging.
CLR Version: 4.6.1055.0
SOS Version: 4.6.1637.0
Thread 27a368:
    0000005a61c4ed10 00007ff88f2d2490 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
        r14: 
            ->  0000004b6296f840 System.Threading.ThreadHelper
            ->  0000004b6296f7a0 System.Threading.ThreadStart
            ->  0000004b6296f750 Quartz.Simpl.SimpleThreadPool+WorkerThread
            ->  0000004b6296f7e0 System.Threading.Thread
            ->  0000004b62959710 System.Runtime.Remoting.Contexts.Context
            ->  0000004aa29315a8 System.AppDomain
            ->  0000004c22c4b368 System.EventHandler
            ->  00000051e2eb5f48 System.Object[]
            ->  00000050629e6180 System.EventHandler
            ->  000000506298b268 System.Runtime.Caching.MemoryCache
            ->  000000506298b348 System.Runtime.Caching.MemoryCacheStore[]
            ->  000000506298d470 System.Runtime.Caching.MemoryCacheStore
            ->  000000506298d5a0 System.Runtime.Caching.CacheExpires
            ->  000000506298e868 System.Threading.Timer
            ->  000000506298eaa8 System.Threading.TimerHolder
            ->  000000506298e888 System.Threading.TimerQueueTimer
            ->  000000506298fe78 System.Threading.TimerQueueTimer

只是想为未来的interwebz旅行者添加这个。 根本原因是我们使用了System.Runtime.Caching.MemoryCache实例,我们经常重新创建它而没有适当的处理。 MemoryCache为函数调用创建了定时器,并且在更换缓存时这些定时器没有从内存中清除,并且定时器会不时地获取线程池线程以检查它们是否应该触发,因此当定时器构建时,所以做了CPU使用率。

难以察觉的原因是它没有出现在堆栈跟踪中,即使在转储文件中也是如此。 相反,我们会看到定时器调用的类(lambdas,大多数)。 我们基本上通过对所有代码进行广泛审计来发现问题。 MemoryCache的文档特别说不要做我们做的事情。

解决方案是在创建新缓存之前处置旧缓存,然后问题就消失了。

编辑:看看上面的堆栈跟踪,看起来它实际上给了我们很好的证据表明问题出在System.Runtime.Caching 我想我们只是盲目的,并不认为问题会在System命名空间内。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM