Contents

检查和监视Linux中的活动GPU

1. 简介

尽管它们通常是准系统,但 Linux 机器有时具有图形处理单元 (GPU),也称为视频或图形卡。无论是用于加密货币挖掘、游戏服务器,还是仅仅为了更好的桌面体验,主动显卡监控都是必不可少的。

在本教程中,我们将讨论检查哪个 GPU 当前处于活动状态和正在使用的方法。首先,我们描述一些常见的视频卡设置。接下来,探索枚举图形控制器的方法。之后,我们将渲染器信息视为获取 GPU 信息的一种手段。最后,我们深入研究了驱动程序实用程序以及按进程列出图形使用情况的方法。

我们使用 GNU Bash 5.1.4 在 Debian 11 (Bullseye) 上测试了本教程中的代码。它应该在大多数符合 POSIX 的环境中工作。

2. GPU 设置

GPU 在内存、速度和性能方面的差异很大。正因为如此,机器有时有两个或更多。

例如,多显示器游戏可以使用双显卡、三显卡或更多显卡进行优化。在大多数情况下,这意味着多个相同的 GPU。在其他设置中,卡片可能会有所不同。例如,对于三维 (3D) 渲染,我们想要实时图像还是静态图像很重要。

另一方面,加密货币挖掘和其他 GPU 密集型应用程序对故障硬件很敏感。在这样的设置中,我们应该能够轻松地查看和修复故障。

最后,GPU 可以是离散的(外部)或集成的(内部)。当两种类型都可用时,在它们之间切换的能力不仅方便而且经常需要。

当然,了解卡片的品牌和型号对于找到适合工作的工具至关重要。

3. 使用pciutilslshw列出设备

由于**市场上的大多数视频卡都运行在PCI (Express) **上,我们可以提取针对这些总线架构的基本信息。

例如,要查找显卡型号和品牌,我们使用lspci ,它是pciutils包的一部分:

$ lspci
00:00.0 Host bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX Host bridge (AGP disabled) (rev 03)
00:03.0 VGA compatible controller: Intel Corporation Haswell-ULT Integrated Graphics Controller (rev 0b)
00:07.0 ISA bridge: Intel Corporation 82371AB/EB/MB PIIX4 ISA (rev 01)
00:07.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
00:07.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 02)
01:00.0 Display controller: Advanced Micro Devices, Inc. [AMD/ATI] Sun LE [Radeon HD 8550M / R5
02:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 10)

在这里,我们看到有两个 GPU:一个是外部的,一个是集成的。此外,我们可以从VGA 兼容控制器显示控制器后面的文字中知道它们的确切制造商和类型。 或者,我们可以使用lshw (列出硬件)工具:

$ lshw -C display
  *-display
       description: VGA compatible controller
       product: Hyper-V virtual VGA
       vendor: Microsoft Corporation
       physical id: 8
       bus info: [[email protected]](/cdn_cgi/l/email_protection):00:08.0
       version: 00
       width: 32 bits
       clock: 33MHz
       capabilities: vga_controller bus_master
       configuration: driver=hyperv_fb latency=0
       resources: irq:11 memory:f8000000-fbffffff memory:c0000-dffff
  *-graphics
       product: hyperv_fb
       physical id: 1
       logical name: /dev/fb0
       capabilities: fb
       configuration: depth=32 resolution=1024,768

请注意,上面的输出来自虚拟机。但是,产品供应商应该显示我们所追求的数据。

让我们看看另一种获取这些细节的方法,但是通过OpenGL 和 Mesa

4. 显示渲染器信息

一旦我们有了系统识别的硬件,我们就可以根据显示 渲染器对其进行审查。为此,我们可以使用glxinfo (图形库扩展信息):

 $ glxinfo -B
name of display: :0
display: :0  screen: 0
[...]
OpenGL vendor string: X.Org
OpenGL renderer string: AMD RV620 (DRM 2.50.0 / 5.10.12-arch1-1, LLVM 11.0.1)
[...]

请注意,我们需要打开一个显示来获取它的属性。事实上,这意味着我们实际上看到了用于渲染显示的 GPU,即它的活动视频卡

或者,我们可以使用它的*–listproviders*开关运行xrandr

$ xrandr --listproviders
Providers: number : 2
Provider 0: id: 0x11f cap: 0x1, Source Output crtcs: 4 outputs: 6 associated providers: 0 name:NVIDIA-0
Provider 1: id: 0x52 cap: 0x2, Sink Output crtcs: 3 outputs: 3 associated providers: 0 name:modesetting

xrandr的输出取决于当前配置和物理连接。特别是,上面的* NVIDIA-0*意味着我们正在为当前显示器使用该驱动程序。

一般来说,我们用于更深入检查的工具很大程度上取决于所使用的驱动程序。

5. GPU 实用程序

事实上,管理 GPU 的最佳方式是通过有针对性的工具。由于视频卡市场是双头垄断的,我们将看看两大供应商。

5.1. 英伟达

对于这个品牌,主要实用程序位于nvidia-utils 包中。特别是,nvidia-smi 可以提供充足的数据:

$ nvidia-smi
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 390.14                 Driver Version: 390.14                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX TIT...  Off  | 0000:04:00.0     Off |                  N/A |
| 21%   50C    P8    31W / 250W |  11666MiB / 16280MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  GeForce GTX TIT...  Off  | 0000:06:00.0     Off |                  N/A |
| 21%   60C    P8    18W / 250W |    666MiB / 16280MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   2  GeForce GTX TIT...  Off  | 0000:07:00.0     Off |                  N/A |
| 25%   66C    P2    72W / 250W |   8656MiB / 12207MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
| GPU        PID    Type   Process name                                 Usage |
|=============================================================================|
|   1        666       G   /usr/bin/X                                 587 MiB |
|   0        667       C   perl                                     11307 MiB |
[...]
+-----------------------------------------------------------------------------+

我们看到三张 NVIDIA 卡,每张卡的内存使用量不同。此外,下表显示了使用视频卡的进程。

事实上,第一列是对顶层表的引用,包含正在使用的GPU编号。因此,我们对显卡的活动和使用情况进行了概述和统计。

5.2. AMD

同样,旧版 ATI/AMD Catalyst 驱动程序 包中的aticonfig可以起到类似的作用:

$ aticonfig --odgc --odgt
Default Adapter - AMD Radeon HD 7970
                            Core (MHz)   Memory (MHz)
           Current Clocks :     880         1375
             Current Peak :     880         1375
  Configurable Peak Range : [800-1000]   [1300-1600]
                 GPU load :    66%
Default Adapter - AMD Radeon HD 7970
           Sensor 0: Temperature - 66.60 C

请注意这种情况下的数据稀缺性。此外,较新的AMDGPU All-Open 和 AMDGPU-Pro 驱动程序 似乎还没有提供此类工具。

有了这个输出,让我们探索更多方法来检查哪个是活动的 GPU 以及它在做什么。

6. 更多主动显卡检测方法

对于 NVIDIA,为了确保我们使用独立卡运行显示器或工具,我们可以使用optirun (优化运行)或其后继primusrun ,这两个部分都是bumblebee 包:

$ primusrun glxinfo | grep OpenGL
OpenGL vendor string: NVIDIA Corporation
OpenGL renderer string: GeForce 940M/PCIe/SSE2
OpenGL core profile version string: 4.5.0 NVIDIA 387.34
OpenGL core profile shading language version string: 4.50 NVIDIA
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile
OpenGL core profile extensions:
OpenGL version string: 4.6.0 NVIDIA 387.34
OpenGL shading language version string: 4.60 NVIDIA
OpenGL context flags: (none)
OpenGL profile mask: (none)
OpenGL extensions:

我们看到 GeForce 卡是活跃的 NVIDIA GPU。在撰写本文时,AMD 还没有类似的工具。

此外,我们可以通过**radeontopnvtop 工具确认哪个进程正在使用外部卡**。后者提供了大量的数据和选项:

$ nvtop
 Device 0 [NVIDIA GeForce GTX 1060 6GB] PCIe GEN [[email protected]](/cdn_cgi/l/email_protection) 8x RX: 328.1 MiB/s TX: 28.32 MiB/s
 GPU 1518MHz MEM 4006mhz TEMP  45 C FAN  27% POW  28 / 120 W
 GPU[|||                     12%] MEM[||                0.404Gi/5.933Gi]
 Device 1 [NVIDIA GeForce GTX 1060 6GB] PCIe GEN [[email protected]](/cdn_cgi/l/email_protection) 8x RX: 0.000 KiB/s TX: 0.000 KiB/s
 GPU 139MHz  MEM 405MHz  TEMP  30 C FAN  33% POW   6 / 120 W
Setup       Chart Options              Metric Displayed in Graph (NVIDIA GeForce GTX 1060)
General     [ ] Reverse plot direction Maximum of 4 metrics per GPU
Devices      -> Displayed all GPUs     [*] GPU utilization rate
Chart     >  -> Displayed GPU 0        [ ] GPU memory utilization rate
Processes    -> Displayed GPU 1        [*] GPU encoder rate
                                       [ ] GPU decoder rate
                                       [ ] GPU temperature
                                       [ ] Power draw rate (current/max)
                                       [ ] Fan speed
                                       [ ] GPU clock rate
                                       [ ] GPU memory clock rate
                                                                                           
                                                                                           
EnterToggle ESCExit Arrow keysNavigate Menu +/-Increment/Decrement Values F12Save Config

相比之下,radeontop相当有限:

$ radeontop
      radeontop v1.2-29-g50cc248, running on VEGA10 bus 44, 120 samples/sec      
                                                                                 
##                     Graphics pipe  81,67% |]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
                     Event engine   0,00% |                                      
      Vertex Grouper + Tesselator  10,00% |]]]
                                          |                                      
                Texture addresser  41,67% |]]]]]]]]]]]]]]]                       
                                          |                                      
                    Shared Export  52,50% |]]]]]]]]]]]]]]]]]]]
      Sequencer Instruction Cache   1,67% |
              Shader Interpolator  55,00% |]]]]]]]]]]]]]]]]]]]]
                                          |                                      
                   Scan Converter  57,50% |]]]]]]]]]]]]]]]]]]]]]
               Primitive Assembly  13,33% |]]]]
                                          |                                      
                      Depth Block  50,00% |]]]]]]]]]]]]]]]]]
                      Color Block  50,00% |]]]]]]]]]]]]]]]]]
                                          |                                      
                496M / 8143M VRAM   6,10% |]]
                 153M / 8166M GTT   1,87% |
       0,95G / 0,94G Memory Clock 100,00% |]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
       1,77G / 1,75G Shader Clock 101,33% |]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
[...]

显然,两者都是为了复制 Linux top 的界面。活动卡通过它们的内存使用来识别。

在具有多个 GPU 的radeontop的情况下,我们必须通过*-b* ( –bus ) 选择总线 来查看给定卡的详细信息。