記一次 .NET 某娛樂聊天流平臺 CPU 爆高分析( 二 )

【記一次 .NET 某娛樂聊天流平臺 CPU 爆高分析】從線程棧上看,這個 CPU 是 4個核,剛好對應著 4 個 HandleError 報錯,看樣子是什么網絡出問題了,接下來切到 80 號線程看一下有沒有什么異常類 。
0:000> ~80sclr!AwareLock::Contention+0x194:00007ffa`deb86e40 4883e801        sub     rax,10:080> !mdsoThread 80:Location          Object            Type------------------------------------------------------------000000000d24e098  000000015765e028  System.Net.WebException000000000d24e0f8  0000000340b07110  System.Collections.ArrayList000000000d24e110  000000015765e2b8  System.Net.HttpWebRequest[]000000000d24e1c0  0000000340b070b8  System.Net.ConnectionGroup000000000d24e258  0000000144a79678  System.Net.Connection0:080> !mdt 000000015765e028000000015765e028 (System.Net.WebException)    _className:NULL (System.String)    _exceptionMethod:NULL (System.Reflection.MethodBase)    _exceptionMethodString:NULL (System.String)    _message:000000015765df70 (System.String) Length=77, String="The underlying connection was closed: The connection was closed unexpectedly."    ...果然看到了 System.Net.WebException, 從異常信息看貌似是 連接關閉了,到這里我就有了一個大膽的猜測,是不是高頻的異常輸出導致的 CPU 爆高呢? 為了驗證,可以到托管堆上找下 WebException 的個數 。
0:080> !dumpheap -statStatistics:              MT    Count    TotalSize Class Name...00007ffacecc38b0    13315      2343440 System.Net.WebException00007ffadcdf6570    11369      1909992 System.IO.IOException00007ffadcdf5fb8    13380      2247840 System.ObjectDisposedException...看到這么多異常還是挺嚇人的,剛好朋友抓了兩個dump可以做個比較 。
0:048> !dumpheap -statStatistics:              MT    Count    TotalSize Class Name00007ffacecc38b0    26745      4707120 System.Net.WebException00007ffadcdf6570    26722      4489296 System.IO.IOException00007ffadcdf5fb8    28745      4829160 System.ObjectDisposedException可以看到 , 2 min 之內異常增加了合計 4w 左右,這就驗證了程序確實是瘋狂的拋異常,在 Windows 平臺上不管是硬件異常還是軟件異常都由 Windows SEH 異常處理框架統一處理 , 會出現用戶態和內核態的切換,這樣瘋狂的拋出 , 必然會導致 CPU 爆高 , 終于找到原因了,接下來就是尋找誘發因素 。
3. 異常是誰誘發的再回頭看 HandleError 函數的調用棧都是底層的庫函數,從線程棧的 PerformIOCompletionCallback 函數來看是 IO線程 誘發的,能被 IO 線程兜到是因為這是做了異步處理,既然是 異步,自然 OverlappedData 也會非常多 。
0:080> !gchandles -statStatistics:              MT    Count    TotalSize Class Name00007ffadc6f7b98    14511      1625232 System.Threading.OverlappedDataTotal 17550 objectsHandles:    Strong Handles:       426    Pinned Handles:       23    Async Pinned Handles: 14511    Ref Count Handles:    24    Weak Long Handles:    2430    Weak Short Handles:   132    SizedRef Handles:     4說明此時有大概 1.5w 的異步請求待回頭,請求量還是蠻大的,但還是沒找到異常的用戶代碼,只能找下到底是誰發起了什么請求 。
0:080> !mdsoThread 80:Location          Object            Type------------------------------------------------------------...000000000d24e488  0000000358c57918  System.Net.HttpWebRequest000000000d24e2e8  00000001407b5b40  System.String  "net_io_readfailure"...0:080> !mdt -r:2 0000000358c579180000000358c57918 (System.Net.HttpWebRequest)    _Uri:0000000358c57210 (System.Uri)        m_String:00000002407ee430 (System.String) Length=98, String="https://api.xxxx/peer_messages"        ....

推薦閱讀