Skip to content

Commit ad46716

Browse files
authored
Use documented timestamps in NVML samples test (#2891)
1 parent 1723666 commit ad46716

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

‎cuda_bindings/tests/nvml/test_device.py‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -198,26 +198,31 @@ def test_device_get_samples_zero_result_returns_tuple(all_devices, subtests):
198198
even when there are zero samples to report, instead of a bare Sample instance.
199199
"""
200200
for device in all_devices:
201-
with subtests.test(device_index=nvml.device_get_index(device)):
202-
with unsupported_before(device, None):
203-
# _FUTURE_TIMESTAMP is newer than any sample can ever be, so the
204-
# zero-sample result is deterministic and cannot race with a
205-
# newly arriving sample (unlike querying "now" and re-querying).
201+
with subtests.test(device_index=nvml.device_get_index(device)), unsupported_before(device, None):
202+
last_seen_timestamp = 0
203+
for _ in range(3):
206204
try:
207205
result = nvml.device_get_samples(
208-
device, nvml.SamplingType.GPU_UTILIZATION_SAMPLES, _FUTURE_TIMESTAMP
206+
device, nvml.SamplingType.GPU_UTILIZATION_SAMPLES, last_seen_timestamp
209207
)
210208
except nvml.NotFoundError:
211209
# Some drivers report NotFoundError instead of a zero-sample
212-
# SUCCESS when there is nothing newer than the timestamp;
213-
# that is also an acceptable (non-crashing) outcome.
214-
continue
210+
# SUCCESS when there is nothing newer than the timestamp.
211+
break
215212

216-
assert isinstance(result, tuple)
217-
assert len(result) == 2
218-
sample_val_type, samples = result
219-
assert isinstance(sample_val_type, int)
220-
assert len(samples) == 0
213+
assert isinstance(result, tuple)
214+
assert len(result) == 2
215+
sample_val_type, samples = result
216+
assert isinstance(sample_val_type, int)
217+
if len(samples) == 0:
218+
break
219+
220+
# NVML documents zero or a timestamp from a previous query.
221+
# Advance to the newest returned sample and try to observe the
222+
# zero-result path before another sample arrives.
223+
last_seen_timestamp = max(sample.time_stamp for sample in samples)
224+
else:
225+
pytest.skip("NVML continued producing samples before an empty result could be observed")
221226

222227

223228
def _check_vgpu_type_id_list(type_ids):

0 commit comments

Comments
 (0)