原文:

https://docs.microsoft.com/zh-cn/dotnet/core/diagnostics/dotnet-counters

 

摘要

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

描述

dotnet-trace 深入调查更严重的性能问题之前是否有任何可疑操作。

 

选项

 

  • --version

    显示 dotnet-counters 实用工具的版本。

  • -h|--help

    显示命令行帮助。

 

命令

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>

    要从中收集计数器数据的进程的 ID。

  • -n|--name <name>

    要从中收集计数器数据的进程的名称。

  • --diagnostic-port

    使用诊断端口,了解如何使用此选项从应用启动时开始监视计数器。

  • --refresh-interval <SECONDS>

    更新显示的计数器之间延迟的秒数

  • --counters <COUNTERS>

    dotnet-counters list 命令。

  • --format <csv|json>

    当前可用的格式:csv 和 json。

  • -o|--output <output>

    输出文件的名称。

 

-- <command>(仅适用于运行 .NET 5.0 或更高版本的目标应用程序)

这通常用于收集应用程序的启动路径的指标,并可用于诊断或监视在主入口点前后发生的问题。

 备注

dotnet exec <app.dll> 选项。

 备注

使用诊断端口。

 

 

示例

  • 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
    
  • 教程:使用 .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
    
  • dotnet-counters 中可用于 .NET 5 应用的所有已知计数器:

    控制台
    > 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.0 或更高版本):

     重要

    这仅适用于运行 .NET 5.0 或更高版本的应用。

    控制台
    > 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
    
  • arg2 作为命令行参数,并从其启动监视其工作集和 GC 堆大小(仅限 .NET 5.0 或更高版本):

     重要

    这仅适用于运行 .NET 5.0 或更高版本的应用。

    控制台
    > 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 进程的列表。

摘要

控制台
dotnet-counters ps [-h|--help]

示例

控制台
> dotnet-counters ps
  
  15683 WebApi     /home/user/repos/WebApi/WebApi
  16324 dotnet     /usr/local/share/dotnet/dotnet

使用诊断端口

 重要

这仅适用于运行 .NET 5.0 或更高版本的应用。

--diagnostic-port 选项。

dotnet-counters <collect|monitor> -- <command> 以子进程的形式启动应用程序,是从启动时开始对其进行快速监视的最简单方法。

dotnet-counters。

 

  1. myport.sock 的诊断套接字并等待连接。

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

    输出:

    Bash
    Waiting for connection on myport.sock
    Start an application with the following environment variable: DOTNET_DiagnosticPorts=/home/user/myport.sock
    
  2. dotnet-counters 输出中的值,启动目标应用程序。

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

    my-dotnet-app 上收集计数器:

    Bash
    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 exec 来启动应用程序。

 

 

 

安装

dotnet-counters:

  • dotnet 全局工具:

    dotnet tool install 命令:

    .NET Core CLI
    dotnet tool install --global dotnet-counters
    
  • 直接下载:

    下载与平台相匹配的工具可执行文件:

    表 1
    (OS) 平台
    Windows arm-x64
    macOS x64
    Linux musl-arm64

相关文章:

  • 2022-12-23
  • 2021-11-10
  • 2022-12-23
  • 2022-01-01
  • 2021-08-20
猜你喜欢
  • 2022-12-23
  • 2021-07-30
  • 2021-10-27
  • 2021-05-01
  • 2022-12-23
  • 2021-08-01
  • 2021-06-17
相关资源
相似解决方案