Skip to content

Commit fa6d0bb

Browse files
committed
Resolve ncu executable path for Windows compatibility
1 parent 425ff30 commit fa6d0bb

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

nsight/collection/ncu.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def get_injection_library_path(ncu_path: str) -> str:
114114

115115
def load_library(path: str) -> ctypes.CDLL:
116116
"""Load the injection shared library."""
117-
if os.name != "posix":
117+
if os.name not in ("posix", "nt"):
118118
raise exceptions.ProfilerException(f"Unsupported operating system: {os.name}")
119119
try:
120120
return ctypes.CDLL(path)
@@ -148,8 +148,10 @@ def try_init_injection() -> None:
148148
elif os.name == "nt":
149149
inj_lib_path = os.path.join(inj_dir, "cuda-injection.dll")
150150
else:
151-
raise exceptions.ProfilerException(f"Unsupported operating system: {os.name}")
152-
151+
raise exceptions.ProfilerException(
152+
f"Unsupported operating system: {os.name}"
153+
)
154+
153155
if not os.path.isfile(inj_lib_path):
154156
raise exceptions.ProfilerException("Failed to find NCU injection library")
155157

@@ -226,11 +228,16 @@ def launch_ncu(
226228

227229
log_path = os.path.splitext(report_path)[0] + ".log"
228230

231+
# Resolve the full path to the executable. On Windows, CreateProcess does
232+
# not reliably resolve a bare "ncu" against PATH (it needs "ncu.exe"), so we
233+
# resolve it explicitly here. Falls back to the bare name if not found.
234+
ncu_exe = shutil.which("ncu") or "ncu"
235+
229236
# Ensures ncu attaches to the correct process when multiple attachable processes exist
230237
target_pid = os.getpid()
231238

232239
ncu_cmd = [
233-
"ncu",
240+
ncu_exe,
234241
"--mode",
235242
"attach",
236243
"--process-id",

tests/test_collection.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ def func_name(x: int, y: int, z: int) -> None:
1414
pass
1515

1616

17+
@patch("shutil.which", return_value="resolved-ncu")
1718
@patch("subprocess.Popen")
18-
def test_launch_ncu_runs_with_ncu_available(mock_popen: MagicMock) -> None:
19+
def test_launch_ncu_runs_with_ncu_available(
20+
mock_popen: MagicMock, mock_which: MagicMock
21+
) -> None:
1922
mock_popen.return_value = MagicMock()
2023

2124
target_pid = os.getpid()
@@ -32,7 +35,7 @@ def test_launch_ncu_runs_with_ncu_available(mock_popen: MagicMock) -> None:
3235
assert mock_popen.call_count == 1
3336
cmd = mock_popen.call_args_list[0].args[0]
3437
assert cmd == [
35-
"ncu",
38+
"resolved-ncu",
3639
"--mode",
3740
"attach",
3841
"--process-id",

0 commit comments

Comments
 (0)