|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +""" |
| 5 | +Unit tests for nsight.activate / nsight.deactivate / nsight.get_active_tool. |
| 6 | +
|
| 7 | +The active tool is module-global state, and NCU cannot be unloaded once loaded, so |
| 8 | +every test runs in its own subprocess via pytest-isolated. The root conftest.py |
| 9 | +leaves the subprocess with no tool active. The tool loaders are replaced with |
| 10 | +recorders, so no GPU, ncu or CUPTI is needed. |
| 11 | +""" |
| 12 | + |
| 13 | +import warnings |
| 14 | +from typing import Any |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | + |
| 19 | +class Recorder: |
| 20 | + """Stands in for the tool loaders and records how often each was called.""" |
| 21 | + |
| 22 | + def __init__(self) -> None: |
| 23 | + self.calls: list[str] = [] |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def tools(monkeypatch: pytest.MonkeyPatch) -> Any: |
| 28 | + # Keep `import nsight` from loading NCU itself, which would make NCU active. |
| 29 | + monkeypatch.setenv("NSPY_NCU_INIT_AT_IMPORT", "0") |
| 30 | + |
| 31 | + import nsight |
| 32 | + from nsight.collection import cupti, ncu |
| 33 | + |
| 34 | + assert nsight.get_active_tool() is None, "child must start with no tool active" |
| 35 | + |
| 36 | + rec = Recorder() |
| 37 | + monkeypatch.setattr(ncu, "init_injection", lambda: rec.calls.append("ncu.init")) |
| 38 | + monkeypatch.setattr(cupti, "init", lambda: rec.calls.append("cupti.init")) |
| 39 | + monkeypatch.setattr(cupti, "finalize", lambda: rec.calls.append("cupti.finalize")) |
| 40 | + return nsight, rec |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.isolated |
| 44 | +def test_no_tool_active_initially(tools: Any) -> None: |
| 45 | + nsight, rec = tools |
| 46 | + assert nsight.get_active_tool() is None |
| 47 | + assert rec.calls == [] |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.isolated |
| 51 | +def test_activate_rejects_non_tool(tools: Any) -> None: |
| 52 | + nsight, rec = tools |
| 53 | + with pytest.raises(ValueError, match="nsight.Tool"): |
| 54 | + nsight.activate("ncu") |
| 55 | + assert nsight.get_active_tool() is None |
| 56 | + assert rec.calls == [] |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.isolated |
| 60 | +@pytest.mark.parametrize( |
| 61 | + "tool_name, load_call", [("NCU", "ncu.init"), ("CUPTI", "cupti.init")] |
| 62 | +) |
| 63 | +def test_activate_loads_tool_once(tools: Any, tool_name: str, load_call: str) -> None: |
| 64 | + nsight, rec = tools |
| 65 | + tool = nsight.Tool[tool_name] |
| 66 | + nsight.activate(tool) |
| 67 | + assert nsight.get_active_tool() == tool |
| 68 | + nsight.activate(tool) # same tool again is a no-op |
| 69 | + assert nsight.get_active_tool() == tool |
| 70 | + assert rec.calls == [load_call] |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.isolated |
| 74 | +@pytest.mark.parametrize("first, second", [("NCU", "CUPTI"), ("CUPTI", "NCU")]) |
| 75 | +def test_activate_different_tool_raises(tools: Any, first: str, second: str) -> None: |
| 76 | + nsight, rec = tools |
| 77 | + nsight.activate(nsight.Tool[first]) |
| 78 | + with pytest.raises(RuntimeError, match="already active"): |
| 79 | + nsight.activate(nsight.Tool[second]) |
| 80 | + assert nsight.get_active_tool() == nsight.Tool[first] |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.isolated |
| 84 | +def test_deactivate_without_active_tool_is_noop(tools: Any) -> None: |
| 85 | + nsight, rec = tools |
| 86 | + with warnings.catch_warnings(): |
| 87 | + warnings.simplefilter("error") |
| 88 | + nsight.deactivate() |
| 89 | + assert nsight.get_active_tool() is None |
| 90 | + assert rec.calls == [] |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.isolated |
| 94 | +def test_deactivate_ncu_warns_and_stays_active(tools: Any) -> None: |
| 95 | + nsight, rec = tools |
| 96 | + nsight.activate(nsight.Tool.NCU) |
| 97 | + with pytest.warns(RuntimeWarning, match="cannot be deactivated"): |
| 98 | + nsight.deactivate() |
| 99 | + assert nsight.get_active_tool() == nsight.Tool.NCU |
| 100 | + # NCU stays loaded, so no other tool can take over. |
| 101 | + with pytest.raises(RuntimeError): |
| 102 | + nsight.activate(nsight.Tool.CUPTI) |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.isolated |
| 106 | +def test_deactivate_cupti_clears_selection(tools: Any) -> None: |
| 107 | + nsight, rec = tools |
| 108 | + nsight.activate(nsight.Tool.CUPTI) |
| 109 | + nsight.deactivate() |
| 110 | + assert nsight.get_active_tool() is None |
| 111 | + assert rec.calls == ["cupti.init", "cupti.finalize"] |
| 112 | + # With CUPTI torn down, another tool can be activated. |
| 113 | + nsight.activate(nsight.Tool.NCU) |
| 114 | + assert nsight.get_active_tool() == nsight.Tool.NCU |
0 commit comments