Skip to content

Migrate to use libnvml to check nvlink status directly (BugFix)#2634

Open
hanhsuan wants to merge 1 commit into
mainfrom
fix_nv_link_check_issue
Open

Migrate to use libnvml to check nvlink status directly (BugFix)#2634
hanhsuan wants to merge 1 commit into
mainfrom
fix_nv_link_check_issue

Conversation

@hanhsuan

@hanhsuan hanhsuan commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Description

With nvidia-driver-580, the output of nvidia-smi nvlink --status was empty. However, with nvidia-driver-595, nvidia-smi nvlink --status now returns something, which broke the prime_offload_test.py and we don't want to depend on the output from nvidia-smi nvlink -s anymore. Therefore, migrating to use ctype would be a easier way to skip to add more dependencies into checkbox.
At the same time, add __GLX_VENDOR_LIBRARY_NAME=mesa for all non-nvidia to make sure the default is set to the right one.

Resolved issues

#2560

Documentation

Tests

26.04 (I+N): https://certification.canonical.com/hardware/202001-27667/submission/498212/
24.04 (I+N): https://certification.canonical.com/hardware/202001-27667/submission/498219/
22.04 run 24.04 test plan (I+N) : https://certification.canonical.com/hardware/202001-27667/submission/498211/
22.04 with NVLINK: https://certification.canonical.com/hardware/202303-31329/submission/498220/
24.04 (A+N): https://certification.canonical.com/hardware/202603-38521/submission/498366/

@codecov

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.33962% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.88%. Comparing base (24ae8db) to head (48908f0).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
providers/base/bin/prime_offload_tester.py 94.33% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2634      +/-   ##
==========================================
+ Coverage   59.84%   59.88%   +0.03%     
==========================================
  Files         485      485              
  Lines       48752    48801      +49     
  Branches     8732     8739       +7     
==========================================
+ Hits        29177    29223      +46     
- Misses      18658    18659       +1     
- Partials      917      919       +2     
Flag Coverage Δ
provider-base 36.15% <94.33%> (+0.16%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hanhsuan
hanhsuan force-pushed the fix_nv_link_check_issue branch from fb39fca to 91aae37 Compare July 1, 2026 12:29
2. set __GLX_VENDOR_LIBRARY_NAME=mesa for non NV card
@hanhsuan
hanhsuan force-pushed the fix_nv_link_check_issue branch from 91aae37 to 48908f0 Compare July 1, 2026 12:44
@hanhsuan
hanhsuan marked this pull request as ready for review July 3, 2026 02:16
Copilot AI review requested due to automatic review settings July 3, 2026 02:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the providers/base prime offload helper to stop relying on nvidia-smi nvlink --status output and instead query NVLink status directly via NVML (loaded with ctypes). It also adjusts the GLX vendor selection for non-NVIDIA offload runs to ensure Mesa is used by default, aligning with the reported offload failure scenario.

Changes:

  • Add PrimeOffloader.check_nv_link_status() using ctypes.CDLL("libnvidia-ml.so.1") to detect active NVLink via NVML APIs.
  • Update check_nv_offload_env() to use NVML-based NVLink detection rather than parsing nvidia-smi output.
  • Set __GLX_VENDOR_LIBRARY_NAME=mesa for non-NVIDIA offload environments and add unit tests covering NVML/NVLink detection and updated behavior.

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated no comments.

File Description
providers/base/bin/prime_offload_tester.py Adds NVML/ctypes NVLink detection, switches NVLink gating logic to use it, and sets Mesa GLX vendor for non-NVIDIA offload.
providers/base/tests/test_prime_offload_tester.py Adds unit tests for NVML-based NVLink detection and updates NVLink-related environment checks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@tomli380576 tomli380576 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm the test results look ok

Comment on lines +260 to +262
# Define NVML return codes
NVML_SUCCESS = 0
NVML_ERROR_NOT_SUPPORTED = 3

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment on lines +268 to +271
ret = nvmlInit()
if ret != NVML_SUCCESS:
self.logger.info("NVML initialization failed")
return False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment on lines +285 to +290
nvmlDeviceGetHandleByIndex = nvml.nvmlDeviceGetHandleByIndex_v2
nvmlDeviceGetHandleByIndex.argtypes = [
ctypes.c_uint,
ctypes.POINTER(ctypes.c_void_p),
]
nvmlDeviceGetHandleByIndex.restype = ctypes.c_int

@tomli380576 tomli380576 Jul 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment on lines +292 to +298
nvmlDeviceGetNvLinkState = nvml.nvmlDeviceGetNvLinkState
nvmlDeviceGetNvLinkState.argtypes = [
ctypes.c_void_p,
ctypes.c_uint,
ctypes.POINTER(ctypes.c_uint),
]
nvmlDeviceGetNvLinkState.restype = ctypes.c_int

@tomli380576 tomli380576 Jul 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@tomli380576 tomli380576 Jul 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the 1st arg is a void pointer because otherwise we need to bring in the entire nvmlDevice_st struct

@tomli380576

tomli380576 commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

the pseudocode is basically

check_nv_link_status():
    nvmlInit_v2()
    if init failed: 
        panic
    
    num_devices = nvmlDeviceGetCount()
    
    for device_i = 0...num_devices:
        device_handle = nvmlDeviceGetHandleByIndex(i)
        
        for link_i = 0...6:  
            has_nvlink = nvmlDeviceGetNvLinkState(device_handle, link_i)
            if has_nvlink:
                return True

    return False

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.

4 participants