mbltml.py Source File

mbltml.py Source File#

mbltml: mbltml.py Source File
mbltml Test Preview
mbltml — EN
mbltml.py
Go to the documentation of this file.
1
4
5import os
6import glob
7import platform
8from ctypes import *
9from pathlib import Path
10from typing import List, Set
11
12
15
16
17MBLTML_DRIVER_VERSION_BUFFER_SIZE = 80
18
19MBLTML_FIRMWARE_VERSION_BUFFER_SIZE = 80
20
21MBLTML_NODE_NAME_BUFFER_SIZE = 80
22
23
27mbltmlDeviceType_t = c_uint
28MBLTML_DEVICE_ERROR = 0x0
29MBLTML_DEVICE_ARIES = 0x1
30MBLTML_DEVICE_REGULUS = 0x2
31MBLTML_DEVICE_REGULUS_USB = 0x4
32
33
34mbltmlHardwareVersion_t = c_uint
35MBLTML_HARDWARE_VERSION_ARIES = 0x00000001
36MBLTML_HARDWARE_VERSION_REGULUS = 0x00000002
37MBLTML_HARDWARE_VERSION_ARIES2 = 0x00000003
38MBLTML_HARDWARE_VERSION_REGULUS2 = 0x00000004
39MBLTML_HARDWARE_VERSION_ERROR = 0x7FFFFFFF
40
41
44mbltmlStatusCode_t = c_uint
45MBLTML_SUCCESS = 0
46MBLTML_DRIVER_NOT_FOUND = 2
47MBLTML_INVALID_ARGUMENT = 3
48MBLTML_UNINITIALIZED = 1
49MBLTML_NOT_SUPPORTED = 4
50MBLTML_INSUFFICIENT_LENGTH = 5
51
52
56mbltmlExtraPmicId_t = c_uint
57MBLTML_EXTRA_PMIC_ID_NPU = 0
58MBLTML_EXTRA_PMIC_ID_DDR = 1
59MBLTML_EXTRA_PMIC_ID_PMIC = 2
60MBLTML_EXTRA_PMIC_ID_GOLDFINGER = 3
61
62
63mbltmlCluster_t = c_uint
64MBLTML_CLUSTER_0 = 0x00010000
65MBLTML_CLUSTER_1 = 0x00020000
66MBLTML_CLUSTER_ERROR = 0x7FFF0000
67
68
69mbltmlCore_t = c_uint
70MBLTML_CORE_0 = 1
71MBLTML_CORE_1 = 2
72MBLTML_CORE_2 = 3
73MBLTML_CORE_3 = 4
74MBLTML_CORE_GLOBAL = 0x0000FFFE
75MBLTML_CORE_ERROR = 0x0000FFFF
76
77
78# Classes
79class mbltmlCoreId_t(Structure):
80 """@brief Composite identifier of an NPU core (cluster + core).
81
82 @var cluster Cluster to which the core belongs (mbltmlCluster_t).
83 @var core Core within the cluster (mbltmlCore_t).
84 """
85
86 cluster: mbltmlCluster_t
87 core: mbltmlCore_t
88 _fields_ = [
89 ("cluster", mbltmlCluster_t),
90 ("core", mbltmlCore_t),
91 ]
92
93 def __repr__(self):
94 d = {"cluster": self.cluster, "core": self.core}
95 return "{}({})".format(
96 self.__class__.__name__,
97 ", ".join("{}={}".format(k, v) for k, v in d.items()),
98 )
99
100
101class mbltmlCoreInfo_t(Structure):
102 """@brief Per-core usage record returned by mbltmlGetCoreInfos().
103
104 Utilization for the core can be computed as @c npu_time / @c interval
105 when @c interval is non-zero.
106
107 @var core_id Identifier of the core (mbltmlCoreId_t).
108 @var npu_time Accumulated NPU active time on the core, in microseconds.
109 @var interval Sampling interval covered by @c npu_time, in microseconds.
110 """
111
112 core_id: mbltmlCoreId_t
113 npu_time: c_int64
114 interval: c_int64
115 _fields_ = [
116 ("core_id", mbltmlCoreId_t),
117 ("npu_time", c_int64),
118 ("interval", c_int64),
119 ]
120
121 def __repr__(self):
122 d = {
123 "core_id": self.core_id,
124 "npu_time": self.npu_time,
125 "interval": self.interval,
126 }
127 return "{}({})".format(
128 self.__class__.__name__,
129 ", ".join("{}={}".format(k, v) for k, v in d.items()),
130 )
131
132
133class mbltmlProcessInfo_t(Structure):
134 """@brief Per-process usage record returned by mbltmlGetProcessInfos().
135
136 Utilization for the process can be computed as
137 @c total_npu_time_us / @c total_interval_us when @c total_interval_us
138 is non-zero.
139
140 @var pid Process ID.
141 @var counts Number of NPU samples attributed to the process.
142 @var npu_memory_usage Device memory currently used by the process, in bytes.
143 @var total_interval_us Total sampling interval, in microseconds.
144 @var total_npu_time_us Total NPU active time used by the process, in microseconds.
145 """
146
147 pid: c_int
148 counts: c_int
149 npu_memory_usage: c_int64
150 total_interval_us: c_int64
151 total_npu_time_us: c_int64
152 _fields_ = [
153 ("pid", c_int),
154 ("counts", c_int),
155 ("npu_memory_usage", c_int64),
156 ("total_interval_us", c_int64),
157 ("total_npu_time_us", c_int64),
158 ]
159
160 def __repr__(self):
161 d = {
162 "pid": self.pid,
163 "counts": self.counts,
164 "npu_memory_usage": self.npu_memory_usage,
165 "total_interval_us": self.total_interval_us,
166 "total_npu_time_us": self.total_npu_time_us,
167 }
168 return "{}({})".format(
169 self.__class__.__name__,
170 ", ".join("{}={}".format(k, v) for k, v in d.items()),
171 )
172
173
174_clib = None
175
176
177def _loadLibrary() -> None:
178 global _clib
179 if _clib != None:
180 return
181 lib_fname = "mbltml.dll" if platform.system() == "Windows" else f"libmbltml.so*"
182 pattern = os.path.join(Path(__file__).resolve().parent, lib_fname)
183 candidates = glob.glob(pattern)
184 if not candidates:
185 raise FileNotFoundError(f"No library matching {pattern}")
186 candidates.sort(key=len, reverse=True)
187 _clib = CDLL(str(candidates[0]))
188
189 _clib.mbltmlInit.argtypes = []
190 _clib.mbltmlInit.restype = mbltmlStatusCode_t
191 _clib.mbltmlInitDevices.argtypes = [mbltmlDeviceType_t]
192 _clib.mbltmlInitDevices.restype = mbltmlStatusCode_t
193 _clib.mbltmlShutdown.argtypes = []
194 _clib.mbltmlShutdown.restype = mbltmlStatusCode_t
195 _clib.mbltmlGetDeviceCount.argtypes = [POINTER(c_uint)]
196 _clib.mbltmlGetDeviceCount.restype = mbltmlStatusCode_t
197 _clib.mbltmlGetDriverVersion.argtypes = [mbltmlDeviceType_t, POINTER(c_char), c_int]
198 _clib.mbltmlGetDriverVersion.restype = mbltmlStatusCode_t
199 _clib.mbltmlGetDriverRevision.argtypes = [mbltmlDeviceType_t, POINTER(c_uint)]
200 _clib.mbltmlGetDriverRevision.restype = mbltmlStatusCode_t
201 _clib.mbltmlGetNodeName.argtypes = [c_int, POINTER(c_char), c_int]
202 _clib.mbltmlGetNodeName.restype = mbltmlStatusCode_t
203 _clib.mbltmlGetDeviceType.argtypes = [c_int, POINTER(mbltmlDeviceType_t)]
204 _clib.mbltmlGetDeviceType.restype = mbltmlStatusCode_t
205 _clib.mbltmlGetHardwareVersion.argtypes = [c_int, POINTER(mbltmlHardwareVersion_t)]
206 _clib.mbltmlGetHardwareVersion.restype = mbltmlStatusCode_t
207 _clib.mbltmlGetFirmwareVersion.argtypes = [c_int, POINTER(c_char), c_uint]
208 _clib.mbltmlGetFirmwareVersion.restype = mbltmlStatusCode_t
209 _clib.mbltmlGetFirmwareRevision.argtypes = [c_int, POINTER(c_uint)]
210 _clib.mbltmlGetFirmwareRevision.restype = mbltmlStatusCode_t
211 _clib.mbltmlGetFirmwareCRC.argtypes = [c_int, POINTER(c_uint)]
212 _clib.mbltmlGetFirmwareCRC.restype = mbltmlStatusCode_t
213 _clib.mbltmlGetTemperature.argtypes = [c_int, POINTER(c_int)]
214 _clib.mbltmlGetTemperature.restype = mbltmlStatusCode_t
215 _clib.mbltmlGetSignalType.argtypes = [c_int, POINTER(c_uint)]
216 _clib.mbltmlGetSignalType.restype = mbltmlStatusCode_t
217 _clib.mbltmlGetNPUClock.argtypes = [c_int, POINTER(c_uint)]
218 _clib.mbltmlGetNPUClock.restype = mbltmlStatusCode_t
219 _clib.mbltmlGetBusClock.argtypes = [c_int, POINTER(c_uint)]
220 _clib.mbltmlGetBusClock.restype = mbltmlStatusCode_t
221 _clib.mbltmlGetFanDuty.argtypes = [c_int, POINTER(c_int)]
222 _clib.mbltmlGetFanDuty.restype = mbltmlStatusCode_t
223 _clib.mbltmlGetVendorId.argtypes = [c_int, POINTER(c_uint)]
224 _clib.mbltmlGetVendorId.restype = mbltmlStatusCode_t
225 _clib.mbltmlGetDeviceId.argtypes = [c_int, POINTER(c_uint)]
226 _clib.mbltmlGetDeviceId.restype = mbltmlStatusCode_t
227 _clib.mbltmlGetSubVendorId.argtypes = [c_int, POINTER(c_uint)]
228 _clib.mbltmlGetSubVendorId.restype = mbltmlStatusCode_t
229 _clib.mbltmlGetSubDeviceId.argtypes = [c_int, POINTER(c_uint)]
230 _clib.mbltmlGetSubDeviceId.restype = mbltmlStatusCode_t
231 _clib.mbltmlGetPcieGen.argtypes = [c_int, POINTER(c_uint)]
232 _clib.mbltmlGetPcieGen.restype = mbltmlStatusCode_t
233 _clib.mbltmlGetPcieLanes.argtypes = [c_int, POINTER(c_uint)]
234 _clib.mbltmlGetPcieLanes.restype = mbltmlStatusCode_t
235 _clib.mbltmlGetPcieRev.argtypes = [c_int, POINTER(c_uint)]
236 _clib.mbltmlGetPcieRev.restype = mbltmlStatusCode_t
237 _clib.mbltmlGetPcieClassCode.argtypes = [c_int, POINTER(c_uint)]
238 _clib.mbltmlGetPcieClassCode.restype = mbltmlStatusCode_t
239 _clib.mbltmlGetTotalPower.argtypes = [c_int, POINTER(c_double)]
240 _clib.mbltmlGetTotalPower.restype = mbltmlStatusCode_t
241 _clib.mbltmlGetTotalCurrent.argtypes = [c_int, POINTER(c_double)]
242 _clib.mbltmlGetTotalCurrent.restype = mbltmlStatusCode_t
243 _clib.mbltmlGetTotalVoltage.argtypes = [c_int, POINTER(c_double)]
244 _clib.mbltmlGetTotalVoltage.restype = mbltmlStatusCode_t
245 _clib.mbltmlGetExtraPmicPower.argtypes = [c_int, POINTER(c_double)]
246 _clib.mbltmlGetExtraPmicPower.restype = mbltmlStatusCode_t
247 _clib.mbltmlGetExtraPmicCurrent.argtypes = [c_int, POINTER(c_double)]
248 _clib.mbltmlGetExtraPmicCurrent.restype = mbltmlStatusCode_t
249 _clib.mbltmlGetExtraPmicVoltage.argtypes = [c_int, POINTER(c_double)]
250 _clib.mbltmlGetExtraPmicVoltage.restype = mbltmlStatusCode_t
251 _clib.mbltmlGetExtraPmicId.argtypes = [c_int, POINTER(mbltmlExtraPmicId_t)]
252 _clib.mbltmlGetExtraPmicId.restype = mbltmlStatusCode_t
253 _clib.mbltmlGetTotalUtilization.argtypes = [c_int, POINTER(c_double)]
254 _clib.mbltmlGetTotalUtilization.restype = mbltmlStatusCode_t
255 _clib.mbltmlGetMemoryUsage.argtypes = [c_int, POINTER(c_int64)]
256 _clib.mbltmlGetMemoryUsage.restype = mbltmlStatusCode_t
257 _clib.mbltmlGetMemoryTotal.argtypes = [c_int, POINTER(c_int64)]
258 _clib.mbltmlGetMemoryTotal.restype = mbltmlStatusCode_t
259 _clib.mbltmlGetProcessInfos.argtypes = [
260 c_int,
261 POINTER(mbltmlProcessInfo_t),
262 POINTER(c_uint),
263 ]
264 _clib.mbltmlGetProcessInfos.restype = mbltmlStatusCode_t
265 _clib.mbltmlGetCoreInfos.argtypes = [
266 c_int,
267 POINTER(mbltmlCoreInfo_t),
268 POINTER(c_uint),
269 ]
270 _clib.mbltmlGetCoreInfos.restype = mbltmlStatusCode_t
271 _clib.mbltmlSetExtraPmicID.argtypes = [c_int, mbltmlExtraPmicId_t]
272 _clib.mbltmlSetExtraPmicID.restype = mbltmlStatusCode_t
273
274
275class MBLTMLError(Exception):
276 """@brief Base exception raised by the mbltml Python binding.
277
278 Wraps a non-success mbltmlStatusCode_t returned by the underlying C
279 library. Instantiating MBLTMLError with a known status code yields the
280 matching subclass (e.g., MBLTMLInvalidArgumentError).
281
282 @var code The mbltmlStatusCode_t value associated with this exception.
283 """
284
285 _code_to_class = {}
286 _errcode_to_string = {
287 MBLTML_DRIVER_NOT_FOUND: "Driver is not found",
288 MBLTML_INVALID_ARGUMENT: "Invalid Argument",
289 MBLTML_UNINITIALIZED: "Uninitilized mbltml.",
290 MBLTML_NOT_SUPPORTED: "Unspported Function for Target Device",
291 MBLTML_INSUFFICIENT_LENGTH: "Insufficient Length",
292 }
293
294 def __new__(cls, code: mbltmlStatusCode_t):
295 if cls is MBLTMLError and code in MBLTMLError._code_to_class:
296 cls = MBLTMLError._code_to_class[code]
297 obj = Exception.__new__(cls)
298 obj.code = code
299 return obj
300
301 def __str__(self):
302 return MBLTMLError._errcode_to_string[self.code]
303
304 def __eq__(self, other):
305 return self.code == other.code
306
307
308class MBLTMLDriverNotFoundError(MBLTMLError):
309 """@brief Raised when the required driver cannot be located.
310
311 Wraps MBLTML_DRIVER_NOT_FOUND.
312 """
313
314 pass
315
316
317class MBLTMLInvalidArgumentError(MBLTMLError):
318 """@brief Raised when one or more arguments are invalid.
319
320 Wraps MBLTML_INVALID_ARGUMENT. Typical causes include a NULL pointer or
321 a @c dev_no that is out of range of the device count.
322 """
323
324 pass
325
326
327class MBLTMLUninitializedError(MBLTMLError):
328 """@brief Raised when an API is called before initialization.
329
330 Wraps MBLTML_UNINITIALIZED. Call mbltmlInit() or mbltmlInitDevices() first.
331 """
332
333 pass
334
335
336class MBLTMLNotSupportedError(MBLTMLError):
337 """@brief Raised when the feature is not supported on the target device.
338
339 Wraps MBLTML_NOT_SUPPORTED.
340 """
341
342 pass
343
344
346 """@brief Raised when the output buffer is too small to hold the result.
347
348 Wraps MBLTML_INSUFFICIENT_LENGTH.
349 """
350
351 pass
352
353
354MBLTMLError._code_to_class = {
355 MBLTML_DRIVER_NOT_FOUND: MBLTMLDriverNotFoundError,
356 MBLTML_INVALID_ARGUMENT: MBLTMLInvalidArgumentError,
357 MBLTML_UNINITIALIZED: MBLTMLUninitializedError,
358 MBLTML_NOT_SUPPORTED: MBLTMLNotSupportedError,
359 MBLTML_INSUFFICIENT_LENGTH: MBLTMLInsufficientLengthError,
360}
361
362
363def _mbltmlThrowError(ret: mbltmlStatusCode_t) -> None:
364 if ret != MBLTML_SUCCESS:
365 raise MBLTMLError(ret)
366
367
368# Function calls
369def mbltmlInit() -> None:
370 """@brief Initialize the mbltml library for all supported device types.
371
372 Equivalent to calling mbltmlInitDevices() with every mbltmlDeviceType_t
373 enabled. Must be called (directly or via mbltmlInitDevices()) before any
374 other mbltml API.
375
376 @throws MBLTMLError on failure.
377 """
378 _loadLibrary()
379 ret = _clib.mbltmlInit()
380 _mbltmlThrowError(ret)
381
382
383def mbltmlInitDevices(device_types: Set[mbltmlDeviceType_t]) -> None:
384 """@brief Initialize the mbltml library for a selected set of device types.
385
386 @param device_types Set of mbltmlDeviceType_t values indicating which
387 device families to enumerate and monitor.
388 @throws MBLTMLError on failure.
389 """
390 _loadLibrary()
391 all_device_types = 0
392 for dt in device_types:
393 all_device_types = all_device_types | dt
394 ret = _clib.mbltmlInitDevices(all_device_types)
395 _mbltmlThrowError(ret)
396
397
398def mbltmlShutdown() -> None:
399 """@brief Release all resources held by the mbltml library.
400
401 After this call, any further API usage requires re-initialization via
402 mbltmlInit() or mbltmlInitDevices().
403
404 @throws MBLTMLError on failure.
405 """
406 _loadLibrary()
407 ret = _clib.mbltmlShutdown()
408 _mbltmlThrowError(ret)
409
410
411def mbltmlGetDeviceCount() -> int:
412 """@brief Get the total number of devices detected by the library.
413
414 @note Supported on all device types.
415
416 @return The number of devices detected.
417 @throws MBLTMLError on failure.
418 """
419 _loadLibrary()
420 value = c_uint()
421 ret = _clib.mbltmlGetDeviceCount(byref(value))
422 _mbltmlThrowError(ret)
423 return value.value
424
425
426def mbltmlGetDriverVersion(device_type: mbltmlDeviceType_t) -> str:
427 """@brief Get the driver version string for a specific device family.
428
429 @note Supported on all device types.
430
431 @param device_type Device family whose driver version is queried.
432 @return The driver version string.
433 @throws MBLTMLError on failure.
434 """
435 _loadLibrary()
436 buf = create_string_buffer(MBLTML_DRIVER_VERSION_BUFFER_SIZE)
437 ret = _clib.mbltmlGetDriverVersion(
438 device_type, buf, MBLTML_DRIVER_VERSION_BUFFER_SIZE
439 )
440 _mbltmlThrowError(ret)
441 return buf.value.decode()
442
443
444def mbltmlGetDriverRevision(device_type: mbltmlDeviceType_t) -> int:
445 """@brief Get the driver revision number for a specific device family.
446
447 @note Supported on all device types.
448
449 @param device_type Device family whose driver revision is queried.
450 @return The driver revision number.
451 @throws MBLTMLError on failure.
452 """
453 _loadLibrary()
454 value = c_uint()
455 ret = _clib.mbltmlGetDriverRevision(device_type, byref(value))
456 _mbltmlThrowError(ret)
457 return value.value
458
459
460def mbltmlGetNodeName(dev_no: int) -> str:
461 """@brief Get the node name of a device.
462
463 @note Supported on all device types.
464
465 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
466 @return The node name string.
467 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
468 @throws MBLTMLError on other failures.
469 """
470 _loadLibrary()
471 buf = create_string_buffer(MBLTML_NODE_NAME_BUFFER_SIZE)
472 ret = _clib.mbltmlGetNodeName(dev_no, buf, MBLTML_NODE_NAME_BUFFER_SIZE)
473 _mbltmlThrowError(ret)
474 return buf.value.decode()
475
476
477def mbltmlGetDeviceType(dev_no: int) -> mbltmlDeviceType_t:
478 """@brief Get the device family type of a device.
479
480 @note Supported on all device types.
481
482 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
483 @return The mbltmlDeviceType_t of the device.
484 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
485 @throws MBLTMLError on other failures.
486 """
487 _loadLibrary()
488 value = c_uint()
489 ret = _clib.mbltmlGetDeviceType(dev_no, byref(value))
490 _mbltmlThrowError(ret)
491 return value.value
492
493
494def mbltmlGetHardwareVersion(dev_no: int) -> mbltmlHardwareVersion_t:
495 """@brief Get the hardware version of a device.
496
497 @note Supported on all device types.
498
499 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
500 @return The mbltmlHardwareVersion_t of the device.
501 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
502 @throws MBLTMLError on other failures.
503 """
504 _loadLibrary()
505 value = c_uint()
506 ret = _clib.mbltmlGetHardwareVersion(dev_no, byref(value))
507 _mbltmlThrowError(ret)
508 return value.value
509
510
511def mbltmlGetFirmwareVersion(dev_no: int) -> str:
512 """@brief Get the firmware version string of a device.
513
514 @note Supported on all device types.
515
516 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
517 @return The firmware version string.
518 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
519 @throws MBLTMLError on other failures.
520 """
521 _loadLibrary()
522 buf = create_string_buffer(MBLTML_FIRMWARE_VERSION_BUFFER_SIZE)
523 ret = _clib.mbltmlGetFirmwareVersion(
524 dev_no, buf, MBLTML_FIRMWARE_VERSION_BUFFER_SIZE
525 )
526 _mbltmlThrowError(ret)
527 return buf.value.decode()
528
529
530def mbltmlGetFirmwareRevision(dev_no: int) -> int:
531 """@brief Get the firmware revision number of a device.
532
533 @note Supported on all device types.
534
535 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
536 @return The firmware revision.
537 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
538 @throws MBLTMLError on other failures.
539 """
540 _loadLibrary()
541 value = c_uint()
542 ret = _clib.mbltmlGetFirmwareRevision(dev_no, byref(value))
543 _mbltmlThrowError(ret)
544 return value.value
545
546
547def mbltmlGetFirmwareCRC(dev_no: int) -> int:
548 """@brief Get the CRC of the currently-loaded firmware image.
549
550 @note Supported on MBLTML_DEVICE_ARIES only.
551
552 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
553 @return The firmware CRC.
554 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
555 @throws MBLTMLError on other failures.
556 """
557 _loadLibrary()
558 value = c_uint()
559 ret = _clib.mbltmlGetFirmwareCRC(dev_no, byref(value))
560 _mbltmlThrowError(ret)
561 return value.value
562
563
564def mbltmlGetTemperature(dev_no: int) -> int:
565 """@brief Get the current die temperature of a device.
566
567 @note Supported on all device types.
568
569 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
570 @return The temperature reading.
571 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
572 @throws MBLTMLError on other failures.
573 """
574 _loadLibrary()
575 value = c_int()
576 ret = _clib.mbltmlGetTemperature(dev_no, byref(value))
577 _mbltmlThrowError(ret)
578 return value.value
579
580
581def mbltmlGetSignalType(dev_no: int) -> int:
582 """@brief Get the current signal-type indicator reported by the device.
583
584 @note Supported on all device types.
585
586 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
587 @return The signal-type value.
588 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
589 @throws MBLTMLError on other failures.
590 """
591 _loadLibrary()
592 value = c_uint()
593 ret = _clib.mbltmlGetSignalType(dev_no, byref(value))
594 _mbltmlThrowError(ret)
595 return value.value
596
597
598def mbltmlGetNPUClock(dev_no: int) -> int:
599 """@brief Get the current NPU clock frequency of a device.
600
601 @note Supported on all device types.
602
603 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
604 @return The NPU clock value.
605 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
606 @throws MBLTMLError on other failures.
607 """
608 _loadLibrary()
609 value = c_uint()
610 ret = _clib.mbltmlGetNPUClock(dev_no, byref(value))
611 _mbltmlThrowError(ret)
612 return value.value
613
614
615def mbltmlGetBusClock(dev_no: int) -> int:
616 """@brief Get the current bus clock frequency of a device.
617
618 @note Supported on MBLTML_DEVICE_ARIES only.
619
620 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
621 @return The bus clock value.
622 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
623 @throws MBLTMLError on other failures.
624 """
625 _loadLibrary()
626 value = c_uint()
627 ret = _clib.mbltmlGetBusClock(dev_no, byref(value))
628 _mbltmlThrowError(ret)
629 return value.value
630
631
632def mbltmlGetFanDuty(dev_no: int) -> int:
633 """@brief Get the current cooling-fan duty cycle of a device.
634
635 @note Supported on MBLTML_DEVICE_ARIES only.
636
637 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
638 @return The fan duty value.
639 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
640 @throws MBLTMLError on other failures.
641 """
642 _loadLibrary()
643 value = c_int()
644 ret = _clib.mbltmlGetFanDuty(dev_no, byref(value))
645 _mbltmlThrowError(ret)
646 return value.value
647
648
649def mbltmlGetVendorId(dev_no: int) -> int:
650 """@brief Get the PCIe vendor ID of a device.
651
652 @note Supported on MBLTML_DEVICE_ARIES only.
653
654 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
655 @return The vendor ID.
656 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
657 @throws MBLTMLError on other failures.
658 """
659 _loadLibrary()
660 value = c_uint()
661 ret = _clib.mbltmlGetVendorId(dev_no, byref(value))
662 _mbltmlThrowError(ret)
663 return value.value
664
665
666def mbltmlGetDeviceId(dev_no: int) -> int:
667 """@brief Get the PCIe device ID of a device.
668
669 @note Supported on MBLTML_DEVICE_ARIES only.
670
671 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
672 @return The device ID.
673 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
674 @throws MBLTMLError on other failures.
675 """
676 _loadLibrary()
677 value = c_uint()
678 ret = _clib.mbltmlGetDeviceId(dev_no, byref(value))
679 _mbltmlThrowError(ret)
680 return value.value
681
682
683def mbltmlGetSubVendorId(dev_no: int) -> int:
684 """@brief Get the PCIe subsystem vendor ID of a device.
685
686 @note Supported on MBLTML_DEVICE_ARIES only.
687
688 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
689 @return The subsystem vendor ID.
690 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
691 @throws MBLTMLError on other failures.
692 """
693 _loadLibrary()
694 value = c_uint()
695 ret = _clib.mbltmlGetSubVendorId(dev_no, byref(value))
696 _mbltmlThrowError(ret)
697 return value.value
698
699
700def mbltmlGetSubDeviceId(dev_no: int) -> int:
701 """@brief Get the PCIe subsystem device ID of a device.
702
703 @note Supported on MBLTML_DEVICE_ARIES only.
704
705 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
706 @return The subsystem device ID.
707 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
708 @throws MBLTMLError on other failures.
709 """
710 _loadLibrary()
711 value = c_uint()
712 ret = _clib.mbltmlGetSubDeviceId(dev_no, byref(value))
713 _mbltmlThrowError(ret)
714 return value.value
715
716
717def mbltmlGetPcieGen(dev_no: int) -> int:
718 """@brief Get the negotiated PCIe link generation of a device.
719
720 @note Supported on MBLTML_DEVICE_ARIES only.
721
722 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
723 @return The PCIe generation.
724 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
725 @throws MBLTMLError on other failures.
726 """
727 _loadLibrary()
728 value = c_uint()
729 ret = _clib.mbltmlGetPcieGen(dev_no, byref(value))
730 _mbltmlThrowError(ret)
731 return value.value
732
733
734def mbltmlGetPcieLanes(dev_no: int) -> int:
735 """@brief Get the negotiated PCIe link width (in lanes) of a device.
736
737 @note Supported on MBLTML_DEVICE_ARIES only.
738
739 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
740 @return The lane count.
741 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
742 @throws MBLTMLError on other failures.
743 """
744 _loadLibrary()
745 value = c_uint()
746 ret = _clib.mbltmlGetPcieLanes(dev_no, byref(value))
747 _mbltmlThrowError(ret)
748 return value.value
749
750
751def mbltmlGetPcieRev(dev_no: int) -> int:
752 """@brief Get the PCIe configuration-space revision ID of a device.
753
754 @note Supported on MBLTML_DEVICE_ARIES only.
755
756 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
757 @return The revision value.
758 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
759 @throws MBLTMLError on other failures.
760 """
761 _loadLibrary()
762 value = c_uint()
763 ret = _clib.mbltmlGetPcieRev(dev_no, byref(value))
764 _mbltmlThrowError(ret)
765 return value.value
766
767
768def mbltmlGetPcieClassCode(dev_no: int) -> int:
769 """@brief Get the PCIe configuration-space class code of a device.
770
771 @note Supported on MBLTML_DEVICE_ARIES only.
772
773 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
774 @return The class code.
775 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
776 @throws MBLTMLError on other failures.
777 """
778 _loadLibrary()
779 value = c_uint()
780 ret = _clib.mbltmlGetPcieClassCode(dev_no, byref(value))
781 _mbltmlThrowError(ret)
782 return value.value
783
784
785def mbltmlGetTotalPower(dev_no: int) -> float:
786 """@brief Get the total instantaneous power consumption of a device (W).
787
788 @note Supported on MBLTML_DEVICE_ARIES only.
789
790 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
791 @return The power reading.
792 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
793 @throws MBLTMLError on other failures.
794 """
795 _loadLibrary()
796 value = c_double()
797 ret = _clib.mbltmlGetTotalPower(dev_no, byref(value))
798 _mbltmlThrowError(ret)
799 return value.value
800
801
802def mbltmlGetTotalCurrent(dev_no: int) -> float:
803 """@brief Get the total instantaneous current draw of a device (A).
804
805 @note Supported on MBLTML_DEVICE_ARIES only.
806
807 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
808 @return The current reading.
809 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
810 @throws MBLTMLError on other failures.
811 """
812 _loadLibrary()
813 value = c_double()
814 ret = _clib.mbltmlGetTotalCurrent(dev_no, byref(value))
815 _mbltmlThrowError(ret)
816 return value.value
817
818
819def mbltmlGetTotalVoltage(dev_no: int) -> float:
820 """@brief Get the total instantaneous supply voltage of a device (V).
821
822 @note Supported on MBLTML_DEVICE_ARIES only.
823
824 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
825 @return The voltage reading.
826 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
827 @throws MBLTMLError on other failures.
828 """
829 _loadLibrary()
830 value = c_double()
831 ret = _clib.mbltmlGetTotalVoltage(dev_no, byref(value))
832 _mbltmlThrowError(ret)
833 return value.value
834
835
836def mbltmlGetExtraPmicPower(dev_no: int) -> float:
837 """@brief Get the power consumption of the extra PMIC rail currently
838 selected for the device (W).
839
840 @note Supported on MBLTML_DEVICE_ARIES only.
841
842 The selected rail is controlled by mbltmlSetExtraPmicID() and can be
843 queried via mbltmlGetExtraPmicId().
844
845 The firmware refreshes this reading once per second. After changing the
846 selected rail with mbltmlSetExtraPmicID(), allow up to 1 second for the
847 new rail's value to be reported.
848
849 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
850 @return The power reading.
851 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
852 @throws MBLTMLError on other failures.
853 """
854 _loadLibrary()
855 value = c_double()
856 ret = _clib.mbltmlGetExtraPmicPower(dev_no, byref(value))
857 _mbltmlThrowError(ret)
858 return value.value
859
860
861def mbltmlGetExtraPmicCurrent(dev_no: int) -> float:
862 """@brief Get the current of the extra PMIC rail currently selected
863 for the device (A).
864
865 @note Supported on MBLTML_DEVICE_ARIES only.
866
867 The firmware refreshes this reading once per second. After changing the
868 selected rail with mbltmlSetExtraPmicID(), allow up to 1 second for the
869 new rail's value to be reported.
870
871 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
872 @return The current reading.
873 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
874 @throws MBLTMLError on other failures.
875 """
876 _loadLibrary()
877 value = c_double()
878 ret = _clib.mbltmlGetExtraPmicCurrent(dev_no, byref(value))
879 _mbltmlThrowError(ret)
880 return value.value
881
882
883def mbltmlGetExtraPmicVoltage(dev_no: int) -> float:
884 """@brief Get the voltage of the extra PMIC rail currently selected
885 for the device (V).
886
887 @note Supported on MBLTML_DEVICE_ARIES only.
888
889 The firmware refreshes this reading once per second. After changing the
890 selected rail with mbltmlSetExtraPmicID(), allow up to 1 second for the
891 new rail's value to be reported.
892
893 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
894 @return The voltage reading.
895 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
896 @throws MBLTMLError on other failures.
897 """
898 _loadLibrary()
899 value = c_double()
900 ret = _clib.mbltmlGetExtraPmicVoltage(dev_no, byref(value))
901 _mbltmlThrowError(ret)
902 return value.value
903
904
905def mbltmlGetExtraPmicId(dev_no: int) -> mbltmlExtraPmicId_t:
906 """@brief Get the extra PMIC rail currently selected for the device.
907
908 @note Supported on MBLTML_DEVICE_ARIES only.
909
910 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
911 @return The selected mbltmlExtraPmicId_t value.
912 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
913 @throws MBLTMLError on other failures.
914 """
915 _loadLibrary()
916 value = c_uint()
917 ret = _clib.mbltmlGetExtraPmicId(dev_no, byref(value))
918 _mbltmlThrowError(ret)
919 return value.value
920
921
922def mbltmlGetTotalUtilization(dev_no: int) -> float:
923 """@brief Get the overall NPU utilization of a device.
924
925 @note Supported on all device types.
926
927 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
928 @return The utilization value (typically a ratio in [0.0, 1.0] or a percentage).
929 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
930 @throws MBLTMLError on other failures.
931 """
932 _loadLibrary()
933 value = c_double()
934 ret = _clib.mbltmlGetTotalUtilization(dev_no, byref(value))
935 _mbltmlThrowError(ret)
936 return value.value
937
938
939def mbltmlGetMemoryUsage(dev_no: int) -> int:
940 """@brief Get the amount of device memory currently in use.
941
942 @note Supported on all device types.
943
944 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
945 @return The in-use memory in bytes.
946 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
947 @throws MBLTMLError on other failures.
948 """
949 _loadLibrary()
950 value = c_int64()
951 ret = _clib.mbltmlGetMemoryUsage(dev_no, byref(value))
952 _mbltmlThrowError(ret)
953 return value.value
954
955
956def mbltmlGetMemoryTotal(dev_no: int) -> int:
957 """@brief Get the total amount of device memory available.
958
959 @note Supported on all device types.
960
961 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
962 @return The total memory in bytes.
963 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
964 @throws MBLTMLError on other failures.
965 """
966 _loadLibrary()
967 value = c_int64()
968 ret = _clib.mbltmlGetMemoryTotal(dev_no, byref(value))
969 _mbltmlThrowError(ret)
970 return value.value
971
972
973def mbltmlGetProcessInfos(dev_no: int) -> List[mbltmlProcessInfo_t]:
974 """@brief Get per-process usage information for a device.
975
976 @note Supported on all device types.
977
978 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
979 @return List of mbltmlProcessInfo_t records; empty if no process is running.
980 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
981 @throws MBLTMLError on other failures.
982 """
983 _loadLibrary()
984 c_count = c_uint(0)
985 ret = _clib.mbltmlGetProcessInfos(dev_no, None, byref(c_count))
986 _mbltmlThrowError(ret)
987 if c_count.value == 0: # No running process
988 return []
989 c_count.value = c_count.value * 2 # Padding to handle increased process number
990 info_array = mbltmlProcessInfo_t * c_count.value
991 infos = info_array()
992 ret = _clib.mbltmlGetProcessInfos(dev_no, infos, byref(c_count))
993 _mbltmlThrowError(ret)
994 return [infos[i] for i in range(c_count.value)]
995
996
997def mbltmlGetCoreInfos(dev_no: int) -> List[mbltmlCoreInfo_t]:
998 """@brief Get per-core usage information for a device.
999
1000 @note Supported on all device types.
1001
1002 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
1003 @return List of mbltmlCoreInfo_t records.
1004 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
1005 @throws MBLTMLError on other failures.
1006 """
1007 _loadLibrary()
1008 c_count = c_uint(0)
1009 ret = _clib.mbltmlGetCoreInfos(dev_no, None, byref(c_count))
1010 _mbltmlThrowError(ret)
1011 if c_count.value == 0: # No core
1012 return []
1013 c_count.value = c_count.value
1014 info_array = mbltmlCoreInfo_t * c_count.value
1015 infos = info_array()
1016 ret = _clib.mbltmlGetCoreInfos(dev_no, infos, byref(c_count))
1017 _mbltmlThrowError(ret)
1018 return [infos[i] for i in range(c_count.value)]
1019
1020
1021def mbltmlSetExtraPmicID(dev_no: int, pmic_id: mbltmlExtraPmicId_t) -> None:
1022 """@brief Select which extra PMIC rail subsequent mbltmlGetExtraPmic*() queries refer to.
1023
1024 @note Supported on Aries2 hardware (MBLTML_HARDWARE_VERSION_ARIES2) only.
1025
1026 The underlying readings are refreshed by firmware once per second; allow
1027 up to 1 second after this call before mbltmlGetExtraPmicPower(),
1028 mbltmlGetExtraPmicCurrent() and mbltmlGetExtraPmicVoltage() reflect the
1029 newly selected rail.
1030
1031 @param dev_no Device index. Total device count is obtained via mbltmlGetDeviceCount().
1032 @param pmic_id Identifier of the extra PMIC rail to select.
1033 @throws MBLTMLInvalidArgumentError if @p dev_no is out of range of the device count.
1034 @throws MBLTMLError on other failures.
1035 """
1036 _loadLibrary()
1037 ret = _clib.mbltmlSetExtraPmicID(dev_no, pmic_id)
1038 _mbltmlThrowError(ret)
1039
1040
Raised when the required driver cannot be located.
Definition mbltml.py:308
Raised when the output buffer is too small to hold the result.
Definition mbltml.py:345
Raised when the feature is not supported on the target device.
Definition mbltml.py:336
Raised when an API is called before initialization.
Definition mbltml.py:327
int mbltmlGetSubVendorId(int dev_no)
Get the PCIe subsystem vendor ID of a device.
Definition mbltml.py:683
None mbltmlShutdown()
Release all resources held by the mbltml library.
Definition mbltml.py:398
mbltmlHardwareVersion_t mbltmlGetHardwareVersion(int dev_no)
Get the hardware version of a device.
Definition mbltml.py:494
int mbltmlGetDeviceId(int dev_no)
Get the PCIe device ID of a device.
Definition mbltml.py:666
int mbltmlGetFanDuty(int dev_no)
Get the current cooling-fan duty cycle of a device.
Definition mbltml.py:632
int mbltmlGetPcieGen(int dev_no)
Get the negotiated PCIe link generation of a device.
Definition mbltml.py:717
int mbltmlGetPcieLanes(int dev_no)
Get the negotiated PCIe link width (in lanes) of a device.
Definition mbltml.py:734
float mbltmlGetExtraPmicVoltage(int dev_no)
Get the voltage of the extra PMIC rail currently selected for the device (V).
Definition mbltml.py:883
List[mbltmlCoreInfo_t] mbltmlGetCoreInfos(int dev_no)
Get per-core usage information for a device.
Definition mbltml.py:997
float mbltmlGetExtraPmicCurrent(int dev_no)
Get the current of the extra PMIC rail currently selected for the device (A).
Definition mbltml.py:861
None mbltmlSetExtraPmicID(int dev_no, mbltmlExtraPmicId_t pmic_id)
Select which extra PMIC rail subsequent mbltmlGetExtraPmic*() queries refer to.
Definition mbltml.py:1021
None mbltmlInitDevices(Set[mbltmlDeviceType_t] device_types)
Initialize the mbltml library for a selected set of device types.
Definition mbltml.py:383
float mbltmlGetTotalPower(int dev_no)
Get the total instantaneous power consumption of a device (W).
Definition mbltml.py:785
int mbltmlGetFirmwareRevision(int dev_no)
Get the firmware revision number of a device.
Definition mbltml.py:530
str mbltmlGetNodeName(int dev_no)
Get the node name of a device.
Definition mbltml.py:460
int mbltmlGetFirmwareCRC(int dev_no)
Get the CRC of the currently-loaded firmware image.
Definition mbltml.py:547
str mbltmlGetFirmwareVersion(int dev_no)
Get the firmware version string of a device.
Definition mbltml.py:511
float mbltmlGetTotalVoltage(int dev_no)
Get the total instantaneous supply voltage of a device (V).
Definition mbltml.py:819
int mbltmlGetMemoryUsage(int dev_no)
Get the amount of device memory currently in use.
Definition mbltml.py:939
int mbltmlGetSignalType(int dev_no)
Get the current signal-type indicator reported by the device.
Definition mbltml.py:581
int mbltmlGetSubDeviceId(int dev_no)
Get the PCIe subsystem device ID of a device.
Definition mbltml.py:700
mbltmlExtraPmicId_t mbltmlGetExtraPmicId(int dev_no)
Get the extra PMIC rail currently selected for the device.
Definition mbltml.py:905
int mbltmlGetPcieRev(int dev_no)
Get the PCIe configuration-space revision ID of a device.
Definition mbltml.py:751
int mbltmlGetPcieClassCode(int dev_no)
Get the PCIe configuration-space class code of a device.
Definition mbltml.py:768
str mbltmlGetDriverVersion(mbltmlDeviceType_t device_type)
Get the driver version string for a specific device family.
Definition mbltml.py:426
int mbltmlGetTemperature(int dev_no)
Get the current die temperature of a device.
Definition mbltml.py:564
List[mbltmlProcessInfo_t] mbltmlGetProcessInfos(int dev_no)
Get per-process usage information for a device.
Definition mbltml.py:973
mbltmlDeviceType_t mbltmlGetDeviceType(int dev_no)
Get the device family type of a device.
Definition mbltml.py:477
int mbltmlGetNPUClock(int dev_no)
Get the current NPU clock frequency of a device.
Definition mbltml.py:598
int mbltmlGetVendorId(int dev_no)
Get the PCIe vendor ID of a device.
Definition mbltml.py:649
float mbltmlGetTotalCurrent(int dev_no)
Get the total instantaneous current draw of a device (A).
Definition mbltml.py:802
int mbltmlGetDriverRevision(mbltmlDeviceType_t device_type)
Get the driver revision number for a specific device family.
Definition mbltml.py:444
int mbltmlGetBusClock(int dev_no)
Get the current bus clock frequency of a device.
Definition mbltml.py:615
float mbltmlGetTotalUtilization(int dev_no)
Get the overall NPU utilization of a device.
Definition mbltml.py:922
int mbltmlGetMemoryTotal(int dev_no)
Get the total amount of device memory available.
Definition mbltml.py:956
float mbltmlGetExtraPmicPower(int dev_no)
Get the power consumption of the extra PMIC rail currently selected for the device (W).
Definition mbltml.py:836
int mbltmlGetDeviceCount()
Get the total number of devices detected by the library.
Definition mbltml.py:411
None mbltmlInit()
Initialize the mbltml library for all supported device types.
Definition mbltml.py:369
Composite identifier of an NPU core (cluster + core).
Definition type.h:93
Per-core usage record returned by mbltmlGetCoreInfos().
Definition type.h:104
Per-process usage record returned by mbltmlGetProcessInfos().
Definition type.h:117