Skip to content

fix: use glxinfo -B as VRAM fallback for generic GPU drivers - #958

Closed
GongHeng2017 wants to merge 1 commit into
linuxdeepin:develop/eaglefrom
GongHeng2017:agent/agent/76ea10653a2a
Closed

GongHeng2017 wants to merge 1 commit into
linuxdeepin:develop/eaglefrom
GongHeng2017:agent/agent/76ea10653a2a

Conversation

@GongHeng2017

@GongHeng2017 GongHeng2017 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Root Cause

DeviceGpu has three VRAM detection paths, all of which fail for many GPU drivers. The primary path reads /sys/<SysFS_ID>/gpu-info (amdgpu-only); lshw maps memory to address range (not size); dmesg requires a VRAM keyword absent on many drivers. The previous fix attempted to read DRM sysfs mem_info_visible_vram_size, but that file name is also vendor-specific and absent on many drivers — not universally available.

Fix Approach

Replaced the sysfs direct-read fallback with a three-level chain: dmesg → nvidia-settings → glxinfo -B.

  • CmdTool::loadGlxInfoForVideoMemory() runs glxinfo -B, parses the Video memory field, and stores it via addMapInfo().
  • DeviceGpu::setGlxInfo() converts MB→GB and only fills m_GraphicsMemory when still empty (glxinfo reflects the current GL renderer only, so it's a last-resort fallback).
  • DeviceManager::setGpuSizeFromGlxInfo() iterates GPU list calling setGlxInfo().
  • DeviceGenerator::getGpuSizeFromDmesg() now has early-return three-level fallback.
  • Shared constants (kGraphicsMemory etc.) moved to commondefine.h.

Also fixed a C++ string literal bug in the regex: \s → \\s (backslash was dropped by compiler, breaking the whitespace match).

Change Safety

  • Risk Level: Low
  • dmesg and nvidia-settings paths unchanged; glxinfo only triggers as last resort when both fail
  • glxinfo not installed → QProcess::start fails gracefully, no crash
  • MB→GB conversion only when value ≥ 1024; values < 1GB kept as-is

Verification

  1. Test amdgpu driver — dmesg path works, no regression
  2. Test nvidia driver — nvidia-settings path works
  3. Test generic driver without dmesg VRAM — glxinfo fallback fills VRAM
  4. Test glxinfo not installed — graceful fallback, no crash

根因

DeviceGpu 有三条显存检测路径,在很多显卡驱动下全部失效。主路径读取 /sys/<SysFS_ID>/gpu-info(amdgpu 专有);lshw 将 memory 映射为地址范围(非大小);dmesg 要求 VRAM 关键字但很多驱动不含。前次修复尝试读取 DRM sysfs mem_info_visible_vram_size,但该文件名同样为部分厂商专有,很多驱动无此文件,不具备通用性。

修复方案

将 sysfs 直读替换为三级 fallback 链:dmesg → nvidia-settings → glxinfo -B。

  • CmdTool::loadGlxInfoForVideoMemory() 运行 glxinfo -B,解析 Video memory 字段,通过 addMapInfo() 存储。
  • DeviceGpu::setGlxInfo() 将 MB 转 GB,仅在 m_GraphicsMemory 为空时填充(glxinfo 仅反映当前 GL 渲染 GPU,作为兜底)。
  • DeviceManager::setGpuSizeFromGlxInfo() 遍历 GPU 列表调用 setGlxInfo()。
  • DeviceGenerator::getGpuSizeFromDmesg() 改为带 early-return 的三级 fallback。
  • 共享常量(kGraphicsMemory 等)移至 commondefine.h。

同时修复了正则表达式中 C++ 字符串字面量的 bug:\s → \\s(反斜杠被编译器丢弃导致空白匹配失效)。

改动安全评估

  • 风险等级:低
  • dmesg 和 nvidia-settings 路径不变;glxinfo 仅在两者均失败时作为兜底触发
  • glxinfo 未安装时 QProcess::start 优雅失败,不崩溃
  • MB→GB 转换仅在值 ≥ 1024 时执行;小于 1GB 的值保持原样

验证建议

  1. 测试 amdgpu 驱动——dmesg 路径生效,无回归
  2. 测试 nvidia 驱动——nvidia-settings 路径生效
  3. 测试无 dmesg VRAM 的通用驱动——glxinfo 兜底填充显存
  4. 测试 glxinfo 未安装——优雅降级,不崩溃

Bug: #380847
PMS: BUG-380847

Summary by Sourcery

Improve GPU VRAM detection by adding a graceful glxinfo fallback after dmesg and nvidia-settings sources fail.

New Features:

  • Add glxinfo -B as a fallback source for GPU video memory detection on drivers unsupported by existing methods.

Bug Fixes:

  • Fix GPU memory detection fallback ordering and whitespace parsing so generic drivers can report VRAM reliably.

Enhancements:

  • Preserve previously detected GPU memory while converting glxinfo values to gigabytes when appropriate.
  • Centralize shared device information keys for reuse across GPU memory detection paths.

1. Root cause: previous sysfs mem_info_visible_vram_size file is
   vendor-specific and absent on many GPU drivers, leaving
   m_GraphicsMemory empty for non-amdgpu/radeon cards
2. Fix: replace sysfs direct-read with a three-level fallback chain:
   dmesg -> nvidia-settings -> glxinfo -B; add loadGlxInfoForVideoMemory()
   to parse "Video memory" from glxinfo output, setGlxInfo() converts
   MB to GB and only fills when m_GraphicsMemory is still empty
3. Impact: GPU VRAM now displays for cards without dmesg/nvidia-settings
   coverage; existing dmesg and nvidia-settings paths unchanged

Log: Fixed GPU VRAM not displayed with generic GPU drivers

Influence:
1. Test GPU VRAM display with amdgpu driver (dmesg path, no regression)
2. Test GPU VRAM display with nvidia driver (nvidia-settings path)
3. Test GPU VRAM display with glxinfo fallback when dmesg/nvidia fail
4. Test glxinfo not installed scenario (graceful fallback, no crash)

fix: 使用glxinfo -B作为通用显卡驱动的显存兜底方案

1. 根因:此前直接读取sysfs文件mem_info_visible_vram_size,该文件名
   仅部分厂商驱动存在,很多显卡驱动无此文件,导致非amdgpu/radeon
   显卡的m_GraphicsMemory为空
2. 方案:替换sysfs直读为三级fallback链:dmesg -> nvidia-settings
   -> glxinfo -B;新增loadGlxInfoForVideoMemory()解析glxinfo输出中
   的"Video memory"字段,setGlxInfo()将MB转GB且仅在显存为空时填充
3. 影响:无dmesg/nvidia-settings覆盖的显卡可正常显示显存;
   原有dmesg和nvidia-settings路径行为不变

Log: 修复通用显卡驱动下显存不显示的问题

Influence:
1. 测试amdgpu驱动下GPU显存显示(dmesg路径,无回归)
2. 测试nvidia驱动下GPU显存显示(nvidia-settings路径)
3. 测试dmesg/nvidia失败时glxinfo兜底显存显示
4. 测试glxinfo未安装场景(优雅降级,不崩溃)

Bug: #380847
PMS: BUG-380847
@sourcery-ai

sourcery-ai Bot commented Oct 29, 2026

Copy link
Copy Markdown

Reviewer's Guide

The PR replaces the non-portable sysfs VRAM fallback with a dmesg → nvidia-settings → glxinfo -B chain, adding command collection, parsing, unit conversion, and guarded application of the current GL renderer’s memory while preserving higher-priority results.

Sequence diagram for GPU VRAM fallback detection

sequenceDiagram
    participant Generator as DeviceGenerator
    participant Manager as DeviceManager
    participant Dmesg as dmesg
    participant Nvidia as nvidia-settings
    participant Glx as glxinfo
    participant GPU as DeviceGpu

    Generator->>Manager: cmdInfo(dmesg)
    alt dmesg data available
        Manager->>GPU: setDmesgInfo(mapInfo)
    else dmesg unavailable
        Generator->>Manager: cmdInfo(nvidia)
        alt nvidia-settings data available
            Manager->>GPU: setDmesgInfo(mapInfo)
        else nvidia-settings unavailable
            Generator->>Manager: cmdInfo(glxinfo)
            Manager->>GPU: setGpuSizeFromGlxInfo(mapInfo)
            GPU->>GPU: setGlxInfo(mapInfo)
            alt Graphics Memory is empty
                GPU->>GPU: Store Video memory
            else Graphics Memory already set
                GPU-->>GPU: Preserve existing value
            end
        end
    end
Loading

Flow diagram for glxinfo VRAM parsing and conversion

flowchart TD
    A[loadGlxInfoForVideoMemory] --> B[Run /usr/bin/glxinfo -B]
    B --> C{Process completes}
    C -->|No| D[Return without map data]
    C -->|Yes| E[Parse Video memory field]
    E --> F{Field found}
    F -->|No| D
    F -->|Yes| G[addMapInfo]
    G --> H[setGlxInfo]
    H --> I{Graphics Memory already set}
    I -->|Yes| J[Preserve existing value]
    I -->|No| K{Value is MB and at least 1024}
    K -->|Yes| L[Convert MB to GB]
    K -->|No| M[Keep parsed value]
Loading

File-Level Changes

Change Details Files
Adds glxinfo -B as the final generic GPU VRAM detection fallback.
  • Registers glxinfo collection and parses its Video memory field with a corrected whitespace regex.
  • Converts parsed MB values to GB where applicable and only applies them when no VRAM value is already available.
  • Adds GPU-manager plumbing to apply glxinfo data across detected GPUs.
  • Reworks VRAM lookup control flow to use dmesg, then nvidia-settings, then glxinfo with early returns.
deepin-devicemanager/src/GenerateDevice/CmdTool.cpp
deepin-devicemanager/src/GenerateDevice/CmdTool.h
deepin-devicemanager/src/GenerateDevice/GetInfoPool.cpp
deepin-devicemanager/src/GenerateDevice/DeviceGenerator.cpp
deepin-devicemanager/src/DeviceManager/DeviceGpu.cpp
deepin-devicemanager/src/DeviceManager/DeviceGpu.h
deepin-devicemanager/src/DeviceManager/DeviceManager.cpp
deepin-devicemanager/src/DeviceManager/DeviceManager.h
Centralizes shared device-information keys for use by the new fallback path.
  • Moves Name, Vendor, Model, Version, and Graphics Memory constants into the common definitions header.
  • Updates the common tools implementation to consume the shared constants.
deepin-devicemanager/src/commondefine.h
deepin-devicemanager/src/Tool/commontools.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant