調查效能計數器 (dotnet-counters)

本文適用於: ✔️ dotnet-counters 3.0.47001 版與更新版本

安裝

有兩種方法可下載並安裝 dotnet-counters

注意

若要在 x86 應用程式上使用 dotnet-counters,就需要對應的 x86 版工具。

概要

dotnet-counters [-h|--help] [--version] <command>

描述

dotnet-counters 是效能監視工具,適用於臨機操作的狀況監控和第一層級的效能調查。 其可觀察透過 EventCounter API 或 Meter API 發佈的效能計數器值。 舉例來說,您可以快速監控 CPU 使用率或在 .NET Core 應用程式中擲回例外狀況的頻率,以便在使用 PerfViewdotnet-trace 進行更認真的深入效能調查之前,確認是否有任何可疑的項目。

選項。

  • --version

    顯示 dotnet-counters 公用程式的版本。

  • -h|--help

    顯示命令列說明。

命令

Command
dotnet-counters collect
dotnet-counters list
dotnet-counters monitor
dotnet-counters ps

dotnet-counters collect

定期收集選取的計數器值,並將其匯出為指定的檔案格式進行後續處理。

概要

dotnet-counters collect [-h|--help] [-p|--process-id] [-n|--name] [--diagnostic-port] [--refresh-interval] [--counters <COUNTERS>] [--format] [-o|--output] [-- <command>]

選項。

  • -p|--process-id <PID>

    要從中收集計數器資料的處理序識別碼。

  • -n|--name <name>

    要從中收集計數器資料的處理序名稱。

  • --diagnostic-port

    要建立的診斷連接埠名稱。 請參閱使用診斷連接埠,了解如何使用此選項從應用程式啟動開始監視計數器。

  • --refresh-interval <SECONDS>

    每次更新所顯示之計數器之間的延遲秒數

  • --counters <COUNTERS>

    計數器的逗號分隔清單。 計數器可獲指定 provider_name[:counter_name]。 如果在沒有計數器限定清單的情況下使用 provider_name,則會顯示來自提供者的所有計數器。 如要探索提供者和計數器名稱,請使用 dotnet-counters list 命令。 針對 EventCounters, provider_name 是 EventSource 的名稱,而如為 Metersprovider_name 是 Meter 的名稱。

  • --format <csv|json>

    要匯出的格式。 目前可用:csv、json。

  • -o|--output <output>

    輸出檔的名稱。

  • -- <command> (僅適用於執行 .NET 5 或更新版本的目標應用程式)

    使用者可以在集合組態參數之後附加 --,並接著會啟動最少為 5.0 執行階段之 .NET 應用程式的命令。 dotnet-counters 將以提供的命令啟動流程,並收集要求的計量。 這通常有助於收集應用程式啟動路徑的計量,且能用來針對在主要進入點之前或之後的問題進行診斷或監控。

    注意

    使用此選項會對和工具進行反向通訊的第一個 .NET 5 處理序,這代表如果您的命令啟動了數個 .NET 應用程式,則只會收集第一個應用程式。 因此,建議您在獨立應用程式上使用此選項,或使用 dotnet exec <app.dll> 選項。

    注意

    透過 dotnet-counters 啟動 .NET 可執行檔會使其輸入/輸出重新導向,讓您無法與其 stdin/stdout 互動。 透過 CTRL+C 或 SIGTERM 結束工具將會安全地結束工具和子處理序。 如果子處理序在工具之前結束,則工具也會結束,您應可安全地檢視追蹤。 如果您需要使用 stdin/stdout,可以使用 --diagnostic-port 選項。 如需詳細資訊,請參閱使用診斷連接埠

注意

在 Linux 和 macOS 上,此命令需要目標應用程式與 dotnet-counters 共用相同的 TMPDIR 環境變數。 否則,命令將會逾時。

注意

若要使用 dotnet-counters 收集計量,其執行身分必須與執行目標處理序的使用者或根使用者相同。 否則,此工具將無法與目標處理序建立連線。

範例

  • 以重新整理間隔 3 秒收集所有計數器,並產生 csv 作為輸出:

    > dotnet-counters collect --process-id 1902 --refresh-interval 3 --format csv
    
    --counters is unspecified. Monitoring System.Runtime counters by default.
    Starting a counter session. Press Q to quit.
    
  • 以子處理序啟動 dotnet mvc.dll,並從啟動開始收集執行階段計數器和 ASP.NET Core 裝載計數器,並將其儲存為 JSON 輸出:

    > dotnet-counters collect --format json --counters System.Runtime,Microsoft.AspNetCore.Hosting -- dotnet mvc.dll
    Starting a counter session. Press Q to quit.
    File saved to counter.json
    

dotnet-counters list

顯示依提供者分組的計數器名稱和描述清單。

概要

dotnet-counters list [-h|--help]

範例

> dotnet-counters list
Showing well-known counters only. Specific processes may support additional counters.

System.Runtime
    cpu-usage                                    Amount of time the process has utilized the CPU (ms)
    working-set                                  Amount of working set used by the process (MB)
    gc-heap-size                                 Total heap size reported by the GC (MB)
    gen-0-gc-count                               Number of Gen 0 GCs per interval
    gen-1-gc-count                               Number of Gen 1 GCs per interval
    gen-2-gc-count                               Number of Gen 2 GCs per interval
    time-in-gc                                   % time in GC since the last GC
    gen-0-size                                   Gen 0 Heap Size
    gen-1-size                                   Gen 1 Heap Size
    gen-2-size                                   Gen 2 Heap Size
    loh-size                                     LOH Heap Size
    alloc-rate                                   Allocation Rate
    assembly-count                               Number of Assemblies Loaded
    exception-count                              Number of Exceptions per interval
    threadpool-thread-count                      Number of ThreadPool Threads
    monitor-lock-contention-count                Monitor Lock Contention Count
    threadpool-queue-length                      ThreadPool Work Items Queue Length
    threadpool-completed-items-count             ThreadPool Completed Work Items Count
    active-timer-count                           Active Timers Count

Microsoft.AspNetCore.Hosting
    requests-per-second                  Request rate
    total-requests                       Total number of requests
    current-requests                     Current number of requests
    failed-requests                      Failed number of requests

注意

當識別到有處理序支援這些計數器時會顯示 Microsoft.AspNetCore.Hosting 計數器,舉例來說,當 ASP.NET Core 應用程式正在主機電腦上執行。

dotnet-counters monitor

顯示所選計數器的定期重新整理值。

概要

dotnet-counters monitor [-h|--help] [-p|--process-id] [-n|--name] [--diagnostic-port] [--refresh-interval] [--counters] [-- <command>]

選項。

  • -p|--process-id <PID>

    要監控之處理序的識別碼。

  • -n|--name <name>

    要監控之處理序的名稱。

  • --diagnostic-port

    要建立的診斷連接埠名稱。 請參閱使用診斷連接埠,了解如何使用此選項從應用程式啟動開始監視計數器。

  • --refresh-interval <SECONDS>

    每次更新所顯示之計數器之間的延遲秒數

  • --counters <COUNTERS>

    計數器的逗號分隔清單。 計數器可獲指定 provider_name[:counter_name]。 如果在沒有計數器限定清單的情況下使用 provider_name,則會顯示來自提供者的所有計數器。 如要探索提供者和計數器名稱,請使用 dotnet-counters list 命令。 針對 EventCounters, provider_name 是 EventSource 的名稱,而如為 Metersprovider_name 是 Meter 的名稱。

-- <command> (僅適用於執行 .NET 5 或更新版本的目標應用程式)

使用者可以在集合組態參數之後附加 --,並接著會啟動最少為 5.0 執行階段之 .NET 應用程式的命令。 dotnet-counters 將以提供的命令啟動流程,並監控要求的計量。 這通常有助於收集應用程式啟動路徑的計量,且能用來針對在主要進入點之前或之後的問題進行診斷或監控。

注意

使用此選項會對和工具進行反向通訊的第一個 .NET 5 處理序,這代表如果您的命令啟動了數個 .NET 應用程式,則只會收集第一個應用程式。 因此,建議您在獨立應用程式上使用此選項,或使用 dotnet exec <app.dll> 選項。

注意

透過 dotnet-counters 啟動 .NET 可執行檔會使其輸入/輸出重新導向,讓您無法與其 stdin/stdout 互動。 透過 CTRL+C 或 SIGTERM 結束工具將會安全地結束工具和子處理序。 如果子處理序在工具之前結束,工具也會結束。 如果您需要使用 stdin/stdout,可以使用 --diagnostic-port 選項。 如需詳細資訊,請參閱使用診斷連接埠

注意

在 Linux 和 macOS 上,此命令需要目標應用程式與 dotnet-counters 共用相同的 TMPDIR 環境變數。

注意

若要使用 dotnet-counters 監控計量,其執行身分必須與執行目標處理序的使用者或根使用者相同。

注意

如果您看見以下類似的錯誤訊息:[ERROR] System.ComponentModel.Win32Exception (299): A 32 bit processes cannot access modules of a 64 bit process.,則代表您在嘗試使用位元與目標處理序不相符的 dotnet-counters。 請務必在安裝連結中下載正確位元的工具。

範例

  • 以 3 秒為重新整理間隔,監控所有來自 System.Runtime 的計數器:

    > dotnet-counters monitor --process-id 1902  --refresh-interval 3 --counters System.Runtime
    Press p to pause, r to resume, q to quit.
        Status: Running
    
    [System.Runtime]
        % Time in GC since last GC (%)                                 0
        Allocation Rate (B / 1 sec)                                5,376
        CPU Usage (%)                                                  0
        Exception Count (Count / 1 sec)                                0
        GC Fragmentation (%)                                          48.467
        GC Heap Size (MB)                                              0
        Gen 0 GC Count (Count / 1 sec)                                 1
        Gen 0 Size (B)                                                24
        Gen 1 GC Count (Count / 1 sec)                                 1
        Gen 1 Size (B)                                                24
        Gen 2 GC Count (Count / 1 sec)                                 1
        Gen 2 Size (B)                                           272,000
        IL Bytes Jitted (B)                                       19,449
        LOH Size (B)                                              19,640
        Monitor Lock Contention Count (Count / 1 sec)                  0
        Number of Active Timers                                        0
        Number of Assemblies Loaded                                    7
        Number of Methods Jitted                                     166
        POH (Pinned Object Heap) Size (B)                             24
        ThreadPool Completed Work Item Count (Count / 1 sec)           0
        ThreadPool Queue Length                                        0
        ThreadPool Thread Count                                        2
        Working Set (MB)                                              19
    
  • 僅監控來自 System.Runtime 的 CPU 使用率和 GC 堆積大小:

    > dotnet-counters monitor --process-id 1902 --counters System.Runtime[cpu-usage,gc-heap-size]
    
    Press p to pause, r to resume, q to quit.
      Status: Running
    
    [System.Runtime]
        CPU Usage (%)                                 24
        GC Heap Size (MB)                            811
    
  • 監視來自使用者定義之 EventSourceEventCounter 值。 如需詳細資訊,請參閱教學課程:在 .NET Core 中使用 EventCounters 測量效能

    > dotnet-counters monitor --process-id 1902 --counters Samples-EventCounterDemos-Minimal
    
    Press p to pause, r to resume, q to quit.
        request                                      100
    
  • 檢視可在 dotnet-counters 中使用的所有已知計數器:

    > dotnet-counters list
    
    Showing well-known counters for .NET (Core) version 3.1 only. Specific processes may support additional counters.
    System.Runtime
        cpu-usage                          The percent of process' CPU usage relative to all of the system CPU resources [0-100]
        working-set                        Amount of working set used by the process (MB)
        gc-heap-size                       Total heap size reported by the GC (MB)
        gen-0-gc-count                     Number of Gen 0 GCs between update intervals
        gen-1-gc-count                     Number of Gen 1 GCs between update intervals
        gen-2-gc-count                     Number of Gen 2 GCs between update intervals
        time-in-gc                         % time in GC since the last GC
        gen-0-size                         Gen 0 Heap Size
        gen-1-size                         Gen 1 Heap Size
        gen-2-size                         Gen 2 Heap Size
        loh-size                           LOH Size
        alloc-rate                         Number of bytes allocated in the managed heap between update intervals
        assembly-count                     Number of Assemblies Loaded
        exception-count                    Number of Exceptions / sec
        threadpool-thread-count            Number of ThreadPool Threads
        monitor-lock-contention-count      Number of times there were contention when trying to take the monitor lock between update intervals
        threadpool-queue-length            ThreadPool Work Items Queue Length
        threadpool-completed-items-count   ThreadPool Completed Work Items Count
        active-timer-count                 Number of timers that are currently active
    
    Microsoft.AspNetCore.Hosting
        requests-per-second                Number of requests between update intervals
        total-requests                     Total number of requests
        current-requests                   Current number of requests
        failed-requests                    Failed number of requests
    
  • 檢視可在適用於 .NET 5 應用程式之 dotnet-counters 中使用的所有已知計數器:

    > dotnet-counters list --runtime-version 5.0
    
    Showing well-known counters for .NET (Core) version 5.0 only. Specific processes may support additional counters.
    System.Runtime
        cpu-usage                          The percent of process' CPU usage relative to all of the system CPU resources [0-100]
        working-set                        Amount of working set used by the process (MB)
        gc-heap-size                       Total heap size reported by the GC (MB)
        gen-0-gc-count                     Number of Gen 0 GCs between update intervals
        gen-1-gc-count                     Number of Gen 1 GCs between update intervals
        gen-2-gc-count                     Number of Gen 2 GCs between update intervals
        time-in-gc                         % time in GC since the last GC
        gen-0-size                         Gen 0 Heap Size
        gen-1-size                         Gen 1 Heap Size
        gen-2-size                         Gen 2 Heap Size
        loh-size                           LOH Size
        poh-size                           POH (Pinned Object Heap) Size
        alloc-rate                         Number of bytes allocated in the managed heap between update intervals
        gc-fragmentation                   GC Heap Fragmentation
        assembly-count                     Number of Assemblies Loaded
        exception-count                    Number of Exceptions / sec
        threadpool-thread-count            Number of ThreadPool Threads
        monitor-lock-contention-count      Number of times there were contention when trying to take the monitor lock between update intervals
        threadpool-queue-length            ThreadPool Work Items Queue Length
        threadpool-completed-items-count   ThreadPool Completed Work Items Count
        active-timer-count                 Number of timers that are currently active
        il-bytes-jitted                    Total IL bytes jitted
        methods-jitted-count               Number of methods jitted
    
    Microsoft.AspNetCore.Hosting
        requests-per-second   Number of requests between update intervals
        total-requests        Total number of requests
        current-requests      Current number of requests
        failed-requests       Failed number of requests
    
    Microsoft-AspNetCore-Server-Kestrel
        connections-per-second      Number of connections between update intervals
        total-connections           Total Connections
        tls-handshakes-per-second   Number of TLS Handshakes made between update intervals
        total-tls-handshakes        Total number of TLS handshakes made
        current-tls-handshakes      Number of currently active TLS handshakes
        failed-tls-handshakes       Total number of failed TLS handshakes
        current-connections         Number of current connections
        connection-queue-length     Length of Kestrel Connection Queue
        request-queue-length        Length total HTTP request queue
    
    System.Net.Http
        requests-started        Total Requests Started
        requests-started-rate   Number of Requests Started between update intervals
        requests-aborted        Total Requests Aborted
        requests-aborted-rate   Number of Requests Aborted between update intervals
        current-requests        Current Requests
    
  • 啟動 my-aspnet-server.exe 並監控從其啟動載入的組件數目 (僅限 .NET 5 或更新版本):

    重要

    這僅適用於執行 .NET 5 或更新版本的應用程式。

    > dotnet-counters monitor --counters System.Runtime[assembly-count] -- my-aspnet-server.exe
    
    Press p to pause, r to resume, q to quit.
      Status: Running
    
    [System.Runtime]
        Number of Assemblies Loaded                   24
    
  • arg1arg2 作為命令列引數啟動 my-aspnet-server.exe,並從其啟動監控工作集合和 GC 堆積大小 (僅限 .NET 5 或更新版本):

    重要

    這僅適用於執行 .NET 5 或更新版本的應用程式。

    > dotnet-counters monitor --counters System.Runtime[working-set,gc-heap-size] -- my-aspnet-server.exe arg1 arg2
    
    Press p to pause, r to resume, q to quit.
      Status: Running
    
    [System.Runtime]
        GC Heap Size (MB)                                 39
        Working Set (MB)                                  59
    

dotnet-counters ps

列出可由 dotnet-counters 監控的 dotnet 處理序。 dotnet-counters 6.0.320703 版和更新版本也會顯示啟動每個處理序時所用的命令列引數 (如果有的話)。

概要

dotnet-counters ps [-h|--help]

範例

假設您使用 dotnet run --configuration Release 命令,啟動了一個長時間執行的應用程式。 在另一個視窗中,您執行了 dotnet-counters ps 命令。 看到的輸出就會如下所示。 若為 dotnet-counters 6.0.320703 版和更新版本,會顯示命令列引數 (如果有的話)。

> dotnet-counters ps
  
  21932 dotnet     C:\Program Files\dotnet\dotnet.exe   run --configuration Release
  36656 dotnet     C:\Program Files\dotnet\dotnet.exe

使用診斷連接埠

重要

這僅適用於執行 .NET 5 或更新版本的應用程式。

診斷連接埠是在 .NET 5 中新增的執行階段功能,可讓您從應用程式啟動開始監控或收集計數器。 如要使用 dotnet-counters 這麼做,您可以使用如上述範例中所述的 dotnet-counters <collect|monitor> -- <command>,或是使用 --diagnostic-port 選項。

如要快速地從應用程式啟動開始加以監控,那麼使用 dotnet-counters <collect|monitor> -- <command> 將應用程式作為子處理序啟動是最簡單的方法。

不過,當您想更精細地控制受監控的應用程式存留期 (例如只監控應用程式前 10 分鐘並繼續執行),或是需要使用 CLI 與應用程式互動時,使用 --diagnostic-port 選項可讓您同時控制監控的目標應用程式與 dotnet-counters

  1. 下列命令會讓 dotnet-counters 建立名為 myport.sock 的診斷通訊端並等候連線。

    dotnet-counters collect --diagnostic-port myport.sock
    

    輸出:

    Waiting for connection on myport.sock
    Start an application with the following environment variable: DOTNET_DiagnosticPorts=/home/user/myport.sock
    
  2. 在另一個主控台中,使用環境變數 DOTNET_DiagnosticPorts (設定為 dotnet-counters 輸出中的值) 來啟動目標應用程式。

    export DOTNET_DiagnosticPorts=/home/user/myport.sock
    ./my-dotnet-app arg1 arg2
    

    這應該會接著啟用 dotnet-counters 來開始在 my-dotnet-app 上收集計數器:

    Waiting for connection on myport.sock
    Start an application with the following environment variable: DOTNET_DiagnosticPorts=myport.sock
    Starting a counter session. Press Q to quit.
    

    重要

    使用 dotnet run 啟動應用程式可能會發生問題,因為 dotnet CLI 可能會繁衍許多不是您應用程式的子處理序,且它們能在您的應用程式之前連線至 dotnet-counters,使您的應用程式在執行階段受到暫止。 建議您直接使用獨立版本的應用程式,或是 dotnet exec 來啟動應用程式。