公司的產品一直緊跟 .net core 3.0 preview 不斷升級, 部署到 Linux 服務器後, 偶爾會出現某個進程CPU佔用100%.
因爲服務部署在雲上, 不能使用遠程調試; 在局域網內的Linux 服務器 或 Windows開發機上又不能重現這個問題, 聯想到Java的jstack, 非常羨慕啊. 想到.net core 已經出來這麼久了, 仍是試着找找看吧, 結果還真找到一篇博客Introducing diagnostics improvements in .NET Core 3.0ubuntu
這篇文章介紹了3個工具bash
此次使用的是dotnet-dump, 即便程序沒有崩潰, 也能夠dump程序快照, 用於分析服務器
實驗環境session
ubuntu-16.04.5-desktop-amd64
SDK 3.0.100-preview6-012264 工具
1. 新建一個簡單Console程序(只能是 .net core 3.0的程序, 不支持 .net core 2.2), 模擬CPU佔用100%的狀況oop
mkdir NetCoreDumpTest && cd NetCoreDumpTest dotnet new console
編輯Program.cs性能
namespace NetCoreDumpTest { using System; using System.Threading.Tasks; class Program { static void Main(string[] args) { Task.Factory.StartNew(() => PrintNumber("Print", 5)); Console.WriteLine("Press any key to exit."); Console.ReadKey(); } static void PrintNumber(string message, int startNumber) { var number = startNumber; while (true) Console.WriteLine($"{message} {number++}"); } } }
2. 安裝dotnet-dumpspa
dotnet tool install --global dotnet-dump --version 1.0.4-preview6.19311.1
提示.net
If you are using bash, you can add it to your profile by running the following command: cat << \EOF >> ~/.bash_profile # Add .NET Core SDK tools export PATH="$PATH:/home/****/.dotnet/tools" EOF You can add it to the current session by running the following command: export PATH="$PATH:/home/****/.dotnet/tools" You can invoke the tool using the following command: dotnet-dump Tool 'dotnet-dump' (version '1.0.4-preview6.19311.1') was successfully installed.
建議將 $HOME/.dotnet/tools加入到PATH, 好吧, 照着作吧, 記得使用下面的命令使設置當即生效線程
source ~/.bash_profile
3. 使用 dotnet NetCoreDumpTest.dll 啓動咱們的問題程序, 而後使用 ps -ef | grep dotnet 查看程序的進程ID, 能夠看到進程ID是 3411
ps -ef | grep dotnet
z*****e 3411 1464 22 07:51 pts/8 00:00:59 dotnet NetCoreDumpTest.dll z*****e 3431 2935 0 07:55 pts/9 00:00:00 grep --color=auto dotnet
針對進程3411, 咱們還須要知道是哪一個線程佔CPU, 使用 top -Hp 3411 能夠列出全部線程, 因爲top每隔3秒刷新一次, 因此可能須要多觀察幾秒才能看到具體是哪一個線程佔用CPU比較高, 這裏咱們能夠看到是PID=3418的線程(Linux的進程ID和線程ID請自行了解一下).
top -Hp 3411 PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3418 z*****e 20 0 2997700 29060 22400 R 10.3 1.4 0:20.68 dotnet 3411 z*****e 20 0 2997700 29060 22400 S 0.0 1.4 0:00.11 dotnet 3412 z*****e 20 0 2997700 29060 22400 S 0.0 1.4 0:00.02 dotnet 3413 z*****e 20 0 2997700 29060 22400 S 0.0 1.4 0:00.00 dotnet 3414 z*****e 20 0 2997700 29060 22400 S 0.0 1.4 0:00.00 dotnet 3415 z*****e 20 0 2997700 29060 22400 S 0.0 1.4 0:00.01 dotnet 3416 z*****e 20 0 2997700 29060 22400 S 0.0 1.4 0:00.00 dotnet 3417 z*****e 20 0 2997700 29060 22400 S 0.0 1.4 0:00.00 dotnet 3421 z*****e 20 0 2997700 29060 22400 S 0.0 1.4 0:00.00 dotnet
獲取dump, 只能正對進程進行dump, 因此咱們輸入的是 3411
dotnet-dump collect -p 3411 Writing minidump with heap to /tmp/core_20190623_075649 Complete
4. 分析
dotnet-dump analyze core_20190623_075649
使用clrthreads 查看全部線程
>clrthreads ThreadCount: 4 UnstartedThread: 0 BackgroundThread: 3 PendingThread: 0 DeadThread: 0 Hosted Runtime: no Lock DBG ID OSID ThreadOBJ State GC Mode GC Alloc Context Domain Count Apt Exception 0 1 d53 0000000001307D80 20020 Preemptive 0000000000000000:0000000000000000 0000000001306450 1 Ukn 4 2 d57 000000000135BBD0 21220 Preemptive 0000000000000000:0000000000000000 0000000001306450 0 Ukn (Finalizer) 6 3 d59 00007F666C0009F0 1020220 Preemptive 0000000000000000:0000000000000000 0000000001306450 0 Ukn (Threadpool Worker) 7 4 d5a 000000000130DA40 1021220 Preemptive 00007F6678106860:00007F6678106F20 0000000001306450 1 Ukn (Threadpool Worker)
咱們關心的線程3418的16進制是d5a, 也就是最後一行, 它的DBG是7, 咱們須要使用 setthread 7, 將其設置爲 當前操做的線程
而後使用 clrstack 獲取線程調用信息
> setthread 7 > clrstack OS Thread Id: 0xd5a (7) Child SP IP Call Site 00007F6715561558 00007f671a2bd4bd [InlinedCallFrame: 00007f6715561558] Interop+Sys.Write(System.Runtime.InteropServices.SafeHandle, Byte*, Int32) 00007F6715561558 00007f669f669a9e [InlinedCallFrame: 00007f6715561558] Interop+Sys.Write(System.Runtime.InteropServices.SafeHandle, Byte*, Int32) 00007F6715561540 00007F669F669A9E ILStubClass.IL_STUB_PInvoke 00007F67155615E0 00007F669F67333E System.ConsolePal.Write(Microsoft.Win32.SafeHandles.SafeFileHandle, Byte*, Int32, Boolean) 00007F67155616A0 00007F669F67360C System.ConsolePal.Write(Microsoft.Win32.SafeHandles.SafeFileHandle, Byte[], Int32, Int32, Boolean) [/_/src/System.Console/src/System/ConsolePal.Unix.cs @ 1236] 00007F67155616C0 00007F669F672B2A System.IO.StreamWriter.Flush(Boolean, Boolean) [/_/src/System.Private.CoreLib/shared/System/IO/StreamWriter.cs @ 261] 00007F6715561710 00007F669F6729F3 System.IO.StreamWriter.WriteLine(System.String) [/_/src/System.Private.CoreLib/shared/System/IO/StreamWriter.cs @ 474] 00007F6715561760 00007F669F6727D3 System.IO.TextWriter+SyncTextWriter.WriteLine(System.String) [/_/src/System.Private.CoreLib/shared/System/IO/TextWriter.cs @ 891] 00007F67155617A0 00007F669F672770 System.Console.WriteLine(System.String) [/_/src/System.Console/src/System/Console.cs @ 550] 00007F67155617C0 00007F669F663791 NetCoreDumpTest.Program.PrintNumber(System.String, Int32) [/home/zhouke/NetCoreDumpTest/Program.cs @ 18] 00007F6715561800 00007F669F6636D9 NetCoreDumpTest.Program+<>c.<Main>b__0_0() 00007F6715561820 00007F669F1872A1 System.Threading.Tasks.Task.InnerInvoke() [/_/src/System.Private.CoreLib/shared/System/Threading/Tasks/Task.cs @ 2466] 00007F6715561840 00007F669F18CBC2 System.Threading.Tasks.Task+<>c.<.cctor>b__274_0(System.Object) [/_/src/System.Private.CoreLib/shared/System/Threading/Tasks/Task.cs @ 2445] 00007F6715561850 00007F669F171AF2 System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(System.Threading.Thread, System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) [/_/src/System.Private.CoreLib/shared/System/Threading/ExecutionContext.cs @ 289] 00007F6715561890 00007F669F187111 System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef, System.Threading.Thread) [/_/src/System.Private.CoreLib/shared/System/Threading/Tasks/Task.cs @ 2406] 00007F6715561910 00007F669F186F28 System.Threading.Tasks.Task.ExecuteEntryUnsafe(System.Threading.Thread) [/_/src/System.Private.CoreLib/shared/System/Threading/Tasks/Task.cs @ 2344] 00007F6715561930 00007F669F186EBB System.Threading.Tasks.Task.ExecuteFromThreadPool(System.Threading.Thread) 00007F6715561940 00007F669F17B754 System.Threading.ThreadPoolWorkQueue.Dispatch() [/_/src/System.Private.CoreLib/shared/System/Threading/ThreadPool.cs @ 663] 00007F67155619C0 00007F669F169A5B System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() [/_/src/System.Private.CoreLib/src/System/Threading/ThreadPool.CoreCLR.cs @ 29] 00007F6715561D50 00007f6718a1ccaf [DebuggerU2MCatchHandlerFrame: 00007f6715561d50]
嘩啦啦一大片, 有點Java調用堆棧的味道, 不過咱們仍是找到了咱們的問題代碼
NetCoreDumpTest.Program.PrintNumber(System.String, Int32)
有時候咱們想知道傳入的什麼參數致使CPU佔用高, 能夠給clrstack加上參數 -a
> clrstack -a .............. 00007F0DD6FFC7C0 00007F0D6EEF3791 NetCoreDumpTest.Program.PrintNumber(System.String, Int32) [/home/zhouke/NetCoreDumpTest/Program.cs @ 18] PARAMETERS: message (0x00007F0DD6FFC7E8) = 0x00007f0d4800b8b0 startNumber (0x00007F0DD6FFC7E4) = 0x0000000000000005 LOCALS: 0x00007F0DD6FFC7E0 = 0x000000000014e42b 0x00007F0DD6FFC7DC = 0x0000000000000001 ...............
能夠看到PARAMETERS裏, startNumber做爲值類型, 能夠直接看到數值爲5, 而message是引用類型, 指向0x00007f0d4800b8b0, 這時候須要用到 dumpobj 命令
> dumpobj 0x00007f0d4800b8b0 Name: System.String MethodTable: 00007f0d6ef70f90 EEClass: 00007f0d6eede1c0 Size: 32(0x20) bytes File: /home/zhouke/dotnet/shared/Microsoft.NETCore.App/3.0.0-preview6-27804-01/System.Private.CoreLib.dll String: Print Fields: MT Field Offset Type VT Attr Value Name 00007f0d6ef6a138 400022b 8 System.Int32 1 instance 5 _stringLength 00007f0d6ef66f38 400022c c System.Char 1 instance 50 _firstChar 00007f0d6ef70f90 400022d 108 System.String 0 static 00007f0d47fff360 Empty
好了, 能夠看到它是一個字符串, 內容爲 "Print"
假如message是一個複雜類型, 能夠查看Fields下面的信息進一步查看
clrstack 還有一個實驗性質的參數 -i, 協助查看各類變量信息, 須要用到lldb, 按照官方教程, 我暫時沒有實驗成功.
查看進程ID和線程ID, 更方便的方法是 htop(須要安裝), 而後按 F4 進行過濾, 輸入dotnet 便可
這張圖是從新運行問題程序的結果, 進程ID和線程ID與前面不同
第二行白色的是進程ID=1650, 第一行CPU佔用高, 是問題線程ID=1658
End