1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
use std::{ffi::CStr, os::raw::c_uint};
use ffi::bindings::{
nvmlVgpuCapability_t, nvmlVgpuTypeId_t, NVML_DEVICE_NAME_BUFFER_SIZE,
NVML_GRID_LICENSE_BUFFER_SIZE,
};
use static_assertions::assert_impl_all;
use crate::{
error::{nvml_sym, nvml_try, NvmlError},
Device,
};
pub struct VgpuType<'dev> {
id: nvmlVgpuTypeId_t,
device: &'dev Device<'dev>,
}
assert_impl_all!(VgpuType: Send, Sync);
impl<'dev> VgpuType<'dev> {
/// Create a new vGPU type wrapper.
///
/// You probably don't need to use this yourself, but rather through
/// [`Device::vgpu_supported_types`] and [`Device::vgpu_creatable_types`].
pub fn new(device: &'dev Device, id: nvmlVgpuTypeId_t) -> Self {
Self { id, device }
}
/// Access the `Device` this struct belongs to.
///
pub fn device(&self) -> &'dev Device<'_> {
self.device
}
/// Get the underlying vGPU type id.
pub fn id(&self) -> nvmlVgpuTypeId_t {
self.id
}
/// Retrieve the class of the vGPU type.
///
/// # Errors
///
/// * `Uninitialized`, if the library has not been successfully initialized
/// * `InvalidArg`, if this `Device` is invalid
/// * `Unknown`, on any unexpected error
///
/// # Device support
///
/// Kepler or newer fully supported devices.
#[doc(alias = "nvmlVgpuTypeGetClass")]
pub fn class_name(&self) -> Result<String, NvmlError> {
let sym = nvml_sym(self.device.nvml().lib.nvmlVgpuTypeGetClass.as_ref())?;
unsafe {
let mut size = NVML_DEVICE_NAME_BUFFER_SIZE;
let mut buffer = vec![0; size as usize];
nvml_try(sym(self.id, buffer.as_mut_ptr(), &mut size))?;
let version_raw = CStr::from_ptr(buffer.as_ptr());
Ok(version_raw.to_str()?.into())
}
}
/// Retrieve license requirements for a vGPU type.
///
/// The license type and version required to run the specified vGPU type is returned as an
/// alphanumeric string, in the form "\<license name\>,\<version\>", for example
/// "GRID-Virtual-PC,2.0". If a vGPU is runnable with* more than one type of license, the
/// licenses are delimited by a semicolon, for example
/// "GRID-Virtual-PC,2.0;GRID-Virtual-WS,2.0;GRID-Virtual-WS-Ext,2.0".
///
/// # Errors
///
/// * `Uninitialized`, if the library has not been successfully initialized
/// * `InsufficientSize`, if the passed-in `size` is 0 (must be > 0)
/// * `InvalidArg`, if this `Device` is invalid
/// * `Unknown`, on any unexpected error
///
/// # Device support
///
/// Kepler or newer fully supported devices.
#[doc(alias = "nvmlVgpuTypeGetLicense")]
pub fn license(&self) -> Result<String, NvmlError> {
let sym = nvml_sym(self.device.nvml().lib.nvmlVgpuTypeGetLicense.as_ref())?;
unsafe {
let mut buffer = vec![0; NVML_GRID_LICENSE_BUFFER_SIZE as usize];
nvml_try(sym(self.id, buffer.as_mut_ptr(), buffer.len() as u32))?;
let version_raw = CStr::from_ptr(buffer.as_ptr());
Ok(version_raw.to_str()?.into())
}
}
/// Retrieve the name of the vGPU type.
///
/// The name is an alphanumeric string that denotes a particular vGPU, e.g. GRID M60-2Q.
///
/// # Errors
///
/// * `Uninitialized`, if the library has not been successfully initialized
/// * `InvalidArg`, if this `Device` is invalid
/// * `Unknown`, on any unexpected error
///
/// # Device support
///
/// Kepler or newer fully supported devices.
#[doc(alias = "nvmlVgpuTypeGetName")]
pub fn name(&self) -> Result<String, NvmlError> {
let sym = nvml_sym(self.device.nvml().lib.nvmlVgpuTypeGetName.as_ref())?;
unsafe {
let mut size = NVML_DEVICE_NAME_BUFFER_SIZE;
let mut buffer = vec![0; size as usize];
nvml_try(sym(self.id, buffer.as_mut_ptr(), &mut size))?;
let version_raw = CStr::from_ptr(buffer.as_ptr());
Ok(version_raw.to_str()?.into())
}
}
/// Retrieve the requested capability for a given vGPU type. Refer to the
/// `nvmlVgpuCapability_t` structure for the specific capabilities that can be
/// queried.
///
/// # Errors
///
/// * `Uninitialized`, if the library has not been successfully initialized
/// * `InvalidArg`, if this `Device` is invalid
/// * `Unknown`, on any unexpected error
///
/// # Device Support
///
/// Maxwell or newer fully supported devices.
#[doc(alias = "nvmlVgpuTypeGetCapabilities")]
pub fn capabilities(&self, capability: nvmlVgpuCapability_t) -> Result<bool, NvmlError> {
let sym = nvml_sym(self.device.nvml().lib.nvmlVgpuTypeGetCapabilities.as_ref())?;
let mut result: c_uint = 0;
unsafe {
nvml_try(sym(self.id, capability, &mut result))?;
}
Ok(result != 0)
}
/// Retrieve the device ID of the vGPU type.
///
/// # Errors
///
/// * `Uninitialized`, if the library has not been successfully initialized
/// * `InvalidArg`, if this `Device` is invalid
/// * `Unknown`, on any unexpected error
///
/// # Device Support
///
/// Kepler or newer fully supported devices.
#[doc(alias = "nvmlVgpuTypeGetDeviceID")]
pub fn device_id(&self) -> Result<(u64, u64), NvmlError> {
let sym = nvml_sym(self.device.nvml().lib.nvmlVgpuTypeGetDeviceID.as_ref())?;
let (mut device_id, mut subsystem_id) = (0, 0);
unsafe {
nvml_try(sym(self.id, &mut device_id, &mut subsystem_id))?;
}
Ok((device_id, subsystem_id))
}
/// Retrieve the static frame rate limit value of the vGPU type.
///
/// # Errors
///
/// * `Uninitialized`, if the library has not been successfully initialized
/// * `NotSupported`, if frame rate limiter is turned off for the vGPU type
/// * `InvalidArg`, if this `Device` is invalid
/// * `Unknown`, on any unexpected error
///
/// # Device Support
///
/// Kepler or newer fully supported devices.
#[doc(alias = "nvmlVgpuTypeGetFrameRateLimit")]
pub fn frame_rate_limit(&self) -> Result<u32, NvmlError> {
let sym = nvml_sym(
self.device
.nvml()
.lib
.nvmlVgpuTypeGetFrameRateLimit
.as_ref(),
)?;
let mut limit = 0;
unsafe {
nvml_try(sym(self.id, &mut limit))?;
}
Ok(limit)
}
/// Retrieve the vGPU framebuffer size in bytes.
///
/// # Errors
///
/// * `Uninitialized`, if the library has not been successfully initialized
/// * `InvalidArg`, if this `Device` is invalid
/// * `Unknown`, on any unexpected error
///
/// # Device Support
///
/// Kepler or newer fully supported devices.
#[doc(alias = "nvmlVgpuTypeGetFramebufferSize")]
pub fn framebuffer_size(&self) -> Result<u64, NvmlError> {
let sym = nvml_sym(
self.device
.nvml()
.lib
.nvmlVgpuTypeGetFramebufferSize
.as_ref(),
)?;
let mut size = 0;
unsafe {
nvml_try(sym(self.id, &mut size))?;
}
Ok(size)
}
/// Retrieve the GPU Instance Profile ID for the vGPU type. The API will return a valid GPU
/// Instance Profile ID for the MIG capable vGPU types, else
/// [`crate::ffi::bindings::INVALID_GPU_INSTANCE_PROFILE_ID`] is returned.
///
/// # Errors
///
/// * `Uninitialized`, if the library has not been successfully initialized
/// * `InvalidArg`, if this `Device` is invalid
/// * `Unknown`, on any unexpected error
///
/// # Device Support
///
/// Kepler or newer fully supported devices.
#[doc(alias = "nvmlVgpuTypeGetGpuInstanceProfileId")]
pub fn instance_profile_id(&self) -> Result<u32, NvmlError> {
let sym = nvml_sym(
self.device
.nvml()
.lib
.nvmlVgpuTypeGetGpuInstanceProfileId
.as_ref(),
)?;
let mut profile_id = 0;
unsafe {
nvml_try(sym(self.id, &mut profile_id))?;
}
Ok(profile_id)
}
/// Retrieve the maximum number of vGPU instances creatable on a device for the vGPU type.
///
/// # Errors
///
/// * `Uninitialized`, if the library has not been successfully initialized
/// * `InvalidArg`, if this `Device` is invalid
/// * `Unknown`, on any unexpected error
///
/// # Device Support
///
/// Kepler or newer fully supported devices.
#[doc(alias = "nvmlVgpuTypeGetMaxInstances")]
pub fn max_instances(&self) -> Result<u32, NvmlError> {
let sym = nvml_sym(self.device.nvml().lib.nvmlVgpuTypeGetMaxInstances.as_ref())?;
let mut max = 0;
unsafe {
nvml_try(sym(self.device.handle(), self.id, &mut max))?;
}
Ok(max)
}
/// Retrieve the maximum number of vGPU instances supported per VM for the vGPU type.
///
/// # Errors
///
/// * `Uninitialized`, if the library has not been successfully initialized
/// * `InvalidArg`, if this `Device` is invalid
/// * `Unknown`, on any unexpected error
///
/// # Device Support
///
/// Kepler or newer fully supported devices.
#[doc(alias = "nvmlVgpuTypeGetMaxInstancesPerVm")]
pub fn max_instances_per_vm(&self) -> Result<u32, NvmlError> {
let sym = nvml_sym(
self.device
.nvml()
.lib
.nvmlVgpuTypeGetMaxInstancesPerVm
.as_ref(),
)?;
let mut max = 0;
unsafe {
nvml_try(sym(self.id, &mut max))?;
}
Ok(max)
}
/// Retrieve count of vGPU's supported display heads.
///
/// # Errors
///
/// * `Uninitialized`, if the library has not been successfully initialized
/// * `InvalidArg`, if this `Device` is invalid
/// * `Unknown`, on any unexpected error
///
/// # Device Support
///
/// Kepler or newer fully supported devices.
#[doc(alias = "nvmlVgpuTypeGetNumDisplayHeads")]
pub fn num_display_heads(&self) -> Result<u32, NvmlError> {
let sym = nvml_sym(
self.device
.nvml()
.lib
.nvmlVgpuTypeGetNumDisplayHeads
.as_ref(),
)?;
let mut heads = 0;
unsafe {
nvml_try(sym(self.id, &mut heads))?;
}
Ok(heads)
}
/// Retrieve vGPU display head's maximum supported resolution.
///
/// The `display_head` argument specifies the 0-based display index, the
/// maximum being what [`VgpuType::num_display_heads`] returns.
///
/// # Errors
///
/// * `Uninitialized`, if the library has not been successfully initialized
/// * `InvalidArg`, if this `Device` is invalid
/// * `Unknown`, on any unexpected error
///
/// # Device Support
///
/// Kepler or newer fully supported devices.
#[doc(alias = "nvmlVgpuTypeGetResolution")]
pub fn resolution(&self, display_head: u32) -> Result<(u32, u32), NvmlError> {
let sym = nvml_sym(self.device.nvml().lib.nvmlVgpuTypeGetResolution.as_ref())?;
let (mut x, mut y) = (0, 0);
unsafe {
nvml_try(sym(self.id, display_head, &mut x, &mut y))?;
}
Ok((x, y))
}
}