Skip to content

Commit 126f257

Browse files
committed
cuda.core.system: Add ProcessInfo APIs
1 parent 82e6bb8 commit 126f257

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

cuda_core/cuda/core/system/_device.pyx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ include "_inforom.pxi"
3434
include "_memory.pxi"
3535
include "_pci_info.pxi"
3636
include "_performance.pxi"
37+
include "_process.pxi"
3738
include "_repair_status.pxi"
3839
include "_temperature.pxi"
3940

@@ -716,6 +717,33 @@ cdef class Device:
716717
"""
717718
return [Pstates(x) for x in nvml.device_get_supported_performance_states(self._handle)]
718719
720+
##########################################################################
721+
# PROCESS
722+
# See external class definitions in _process.pxi
723+
724+
@property
725+
def compute_running_processes(self) -> list[ProcessInfo]:
726+
"""
727+
Get information about processes with a compute context on a device
728+
729+
For Fermi™ or newer fully supported devices.
730+
731+
This function returns information only about compute running processes
732+
(e.g. CUDA application which have active context). Any graphics
733+
applications (e.g. using OpenGL, DirectX) won't be listed by this
734+
function.
735+
736+
Keep in mind that information returned by this call is dynamic and the
737+
number of elements might change in time.
738+
739+
In MIG mode, if device handle is provided, the API returns aggregate
740+
information, only if the caller has appropriate privileges. Per-instance
741+
information can be queried by using specific MIG device handles.
742+
Querying per-instance information using MIG device handles is not
743+
supported if the device is in vGPU Host virtualization mode.
744+
"""
745+
return [ProcessInfo(proc) for proc in nvml.device_get_compute_running_processes_v3(self._handle)]
746+
719747
##########################################################################
720748
# REPAIR STATUS
721749
# See external class definitions in _repair_status.pxi
@@ -855,6 +883,7 @@ __all__ = [
855883
"MemoryInfo",
856884
"PcieUtilCounter",
857885
"PciInfo",
886+
"ProcessInfo",
858887
"Pstates",
859888
"RepairStatus",
860889
"Temperature",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
6+
class ProcessInfo:
7+
"""
8+
Information about a process using the GPU.
9+
"""
10+
def __init__(self, process_info: nvml.ProcessInfo):
11+
self._process_info = process_info
12+
13+
@property
14+
def pid(self) -> int:
15+
"""
16+
The PID of the process.
17+
"""
18+
return self._process_info.pid
19+
20+
@property
21+
def used_gpu_memory(self) -> int:
22+
"""
23+
The amount of GPU memory (in bytes) used by the process.
24+
"""
25+
return self._process_info.used_gpu_memory
26+
27+
@property
28+
def gpu_instance_id(self) -> int:
29+
"""
30+
The GPU instance ID for MIG devices.
31+
32+
Only valid for processes running on MIG devices.
33+
"""
34+
return self._process_info.gpu_instance_id
35+
36+
@property
37+
def compute_instance_id(self) -> int:
38+
"""
39+
The Compute instance ID for MIG devices.
40+
41+
Only valid for processes running on MIG devices.
42+
"""
43+
return self._process_info.compute_instance_id

cuda_core/tests/system/test_system_device.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,3 +729,16 @@ def test_pstates():
729729
assert isinstance(utilization.percentage, int)
730730
assert isinstance(utilization.inc_threshold, int)
731731
assert isinstance(utilization.dec_threshold, int)
732+
733+
734+
def test_compute_running_processes():
735+
for device in system.Device.get_all_devices():
736+
with unsupported_before(device, "FERMI"):
737+
processes = device.compute_running_processes
738+
assert isinstance(processes, list)
739+
for proc in processes:
740+
assert isinstance(proc, system.ProcessInfo)
741+
assert isinstance(proc.pid, int)
742+
assert isinstance(proc.used_gpu_memory, int)
743+
assert isinstance(proc.gpu_instance_id, int)
744+
assert isinstance(proc.compute_instance_id, int)

0 commit comments

Comments
 (0)