44// </copyright>
55// <author>Christoph Müller</author>
66
7+ #if defined(POWER_OVERWHELMING_WITH_NVML)
78#include " nvml_sensor.h"
89
910#include < array>
1011#include < stdexcept>
1112
13+ #include " nvml_error_category.h"
14+
1215
1316/*
1417 * PWROWG_DETAIL_NAMESPACE_BEGIN::nvml_sensor::descriptions
@@ -32,9 +35,7 @@ std::size_t PWROWG_DETAIL_NAMESPACE::nvml_sensor::descriptions(
3235 {
3336 auto status = nvidia_management_library::instance ()
3437 .nvmlDeviceGetCount (&cnt_devices);
35- if (status != NVML_SUCCESS) {
36- throw nvml_exception (status);
37- }
38+ throw_if_nvml_failed (status);
3839 }
3940
4041 // Create descriptors for each device.
@@ -47,9 +48,7 @@ std::size_t PWROWG_DETAIL_NAMESPACE::nvml_sensor::descriptions(
4748 {
4849 auto status = nvidia_management_library::instance ()
4950 .nvmlDeviceGetHandleByIndex (retval, &device);
50- if (status != NVML_SUCCESS) {
51- throw nvml_exception (status);
52- }
51+ throw_if_nvml_failed (status);
5352
5453 builder.with_private_data (device);
5554 }
@@ -58,9 +57,7 @@ std::size_t PWROWG_DETAIL_NAMESPACE::nvml_sensor::descriptions(
5857 auto status = nvidia_management_library::instance ()
5958 .nvmlDeviceGetName (device, name.data (),
6059 static_cast <unsigned int >(name.size ()));
61- if (status != NVML_SUCCESS) {
62- throw nvml_exception (status);
63- }
60+ throw_if_nvml_failed (status);
6461 }
6562
6663 // {
@@ -75,9 +72,7 @@ std::size_t PWROWG_DETAIL_NAMESPACE::nvml_sensor::descriptions(
7572 {
7673 auto status = nvidia_management_library::instance ()
7774 .nvmlDeviceGetPciInfo (device, &pci_info);
78- if (status != NVML_SUCCESS) {
79- throw nvml_exception (status);
80- }
75+ throw_if_nvml_failed (status);
8176
8277 builder.with_path (pci_info.busId );
8378 builder.with_id (" NVML/%s" , pci_info.busId );
@@ -109,9 +104,7 @@ PWROWG_DETAIL_NAMESPACE::nvml_sensor::from_bus_id(
109104
110105 auto status = nvidia_management_library::instance ()
111106 .nvmlDeviceGetHandleByPciBusId (pciBusId, &device);
112- if (status != NVML_SUCCESS) {
113- throw nvml_exception (status);
114- }
107+ throw_if_nvml_failed (status);
115108
116109 return std::make_shared<nvml_sensor>(device, index);
117110}
@@ -127,9 +120,7 @@ PWROWG_DETAIL_NAMESPACE::nvml_sensor::from_guid(_In_z_ const char *guid,
127120
128121 auto status = nvidia_management_library::instance ()
129122 .nvmlDeviceGetHandleByUUID (guid, &device);
130- if (status != NVML_SUCCESS) {
131- throw nvml_exception (status);
132- }
123+ throw_if_nvml_failed (status);
133124
134125 return std::make_shared<nvml_sensor>(device, index);
135126}
@@ -146,9 +137,7 @@ PWROWG_DETAIL_NAMESPACE::nvml_sensor::from_index(
146137
147138 auto status = nvidia_management_library::instance ()
148139 .nvmlDeviceGetHandleByIndex (idx, &device);
149- if (status != NVML_SUCCESS) {
150- throw nvml_exception (status);
151- }
140+ throw_if_nvml_failed (status);
152141
153142 return std::make_shared<nvml_sensor>(device, index);
154143}
@@ -165,9 +154,7 @@ PWROWG_DETAIL_NAMESPACE::nvml_sensor::from_serial(
165154
166155 auto status = nvidia_management_library::instance ()
167156 .nvmlDeviceGetHandleBySerial (serial, &device);
168- if (status != NVML_SUCCESS) {
169- throw nvml_exception (status);
170- }
157+ throw_if_nvml_failed (status);
171158
172159 return std::make_shared<nvml_sensor>(device, index);
173160}
@@ -191,12 +178,12 @@ void PWROWG_DETAIL_NAMESPACE::nvml_sensor::sample(
191178 unsigned int mw = 0 ;
192179 auto status = nvidia_management_library::instance ()
193180 .nvmlDeviceGetPowerUsage (this ->_device , &mw);
194- if (status != NVML_SUCCESS) {
195- throw nvml_exception (status);
196- }
181+ throw_if_nvml_failed (status);
197182
198183 // Convert to Watts.
199184 s.reading .floating_point = static_cast <value_type>(mw) / thousand;
200185
201186 callback (&s, 1 , sensors, context);
202187}
188+
189+ #endif /* defined(POWER_OVERWHELMING_WITH_NVML) */
0 commit comments