Skip to content

Commit cb7566a

Browse files
committed
Switch from custom nvml_exception to std::system_error with nvml_category.
Fully hide NVML based on CMake configuration.
1 parent 9632faf commit cb7566a

File tree

11 files changed

+43
-118
lines changed

11 files changed

+43
-118
lines changed

power_overwhelming/src/nvml_exception.cpp

Lines changed: 0 additions & 19 deletions
This file was deleted.

power_overwhelming/src/nvml_exception.h

Lines changed: 0 additions & 63 deletions
This file was deleted.

power_overwhelming/src/nvml_scope.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@
44
// </copyright>
55
// <author>Christoph Müller</author>
66

7+
#if defined(POWER_OVERWHELMING_WITH_NVML)
78
#include "nvml_scope.h"
89

910
#include "nvidia_management_library.h"
10-
#include "nvml_exception.h"
11+
#include "nvml_error_category.h"
1112

1213

1314
/*
1415
* PWROWG_DETAIL_NAMESPACE::nvml_scope::nvml_scope
1516
*/
1617
PWROWG_DETAIL_NAMESPACE::nvml_scope::nvml_scope(void) {
1718
auto status = nvidia_management_library::instance().nvmlInit();
18-
if (status != NVML_SUCCESS) {
19-
throw nvml_exception(status);
20-
}
19+
throw_if_nvml_failed(status);
2120
}
2221

2322

@@ -27,3 +26,4 @@ PWROWG_DETAIL_NAMESPACE::nvml_scope::nvml_scope(void) {
2726
PWROWG_DETAIL_NAMESPACE::nvml_scope::~nvml_scope(void) {
2827
nvidia_management_library::instance().nvmlShutdown();
2928
}
29+
#endif /* defined(POWER_OVERWHELMING_WITH_NVML) */

power_overwhelming/src/nvml_scope.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#if !defined(_PWROWG_NVML_SCOPE_H)
88
#define _PWROWG_NVML_SCOPE_H
99
#pragma once
10+
#if defined(POWER_OVERWHELMING_WITH_NVML)
1011

1112
#include "visus/pwrowg/api.h"
1213

@@ -44,4 +45,5 @@ class PWROWG_TEST_API nvml_scope final {
4445

4546
PWROWG_DETAIL_NAMESPACE_END
4647

48+
#endif /* defined(POWER_OVERWHELMING_WITH_NVML) */
4749
#endif /* !defined(_PWROWG_NVML_SCOPE_H) */

power_overwhelming/src/nvml_sensor.cpp

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
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) */

power_overwhelming/src/nvml_sensor.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#if !defined(_PWROWG_NVML_SENSOR_H)
88
#define _PWROWG_NVML_SENSOR_H
99
#pragma once
10+
#if defined(POWER_OVERWHELMING_WITH_NVML)
1011

1112
#include <cassert>
1213
#include <list>
@@ -22,7 +23,6 @@
2223
#include "visus/pwrowg/sensor_filters.h"
2324

2425
#include "nvidia_management_library.h"
25-
#include "nvml_exception.h"
2626
#include "nvml_scope.h"
2727
#include "sensor_description_builder.h"
2828
#include "sensor_utilities.h"
@@ -116,7 +116,7 @@ class PWROWG_TEST_API nvml_sensor final {
116116
/// </summary>
117117
/// <param name="guid"></param>
118118
/// <returns></returns>
119-
/// <exception cref="nvml_exception">If the specified device was not
119+
/// <exception cref="std::system_error">If the specified device was not
120120
/// found, is not unique or another error occurred in NVML.</exception>
121121
static std::shared_ptr<nvml_sensor> from_guid(_In_z_ const char *guid,
122122
_In_ const std::size_t index);
@@ -132,7 +132,7 @@ class PWROWG_TEST_API nvml_sensor final {
132132
/// </remarks>
133133
/// <param name="idx"></param>
134134
/// <returns></returns>
135-
/// <exception cref="nvml_exception">If the specified device was not
135+
/// <exception cref="std::system_error">If the specified device was not
136136
/// found, is not unique or another error occurred in NVML.</exception>
137137
static std::shared_ptr<nvml_sensor> from_index(_In_ const unsigned int idx,
138138
_In_ const std::size_t index);
@@ -143,7 +143,7 @@ class PWROWG_TEST_API nvml_sensor final {
143143
/// </summary>
144144
/// <param name="serial"></param>
145145
/// <returns></returns>
146-
/// <exception cref="nvml_exception">If the specified device was not
146+
/// <exception cref="std::system_error">If the specified device was not
147147
/// found, is not unique or another error occurred in NVML.</exception>
148148
static std::shared_ptr<nvml_sensor> from_serial(_In_z_ const char *serial,
149149
_In_ const std::size_t index);
@@ -185,4 +185,5 @@ PWROWG_DETAIL_NAMESPACE_END
185185

186186
#include "nvml_sensor.inl"
187187

188+
#endif /*^defined(POWER_OVERWHELMING_WITH_NVML) */
188189
#endif /* defined(_PWROWG_NVML_SENSOR_H) */

power_overwhelming/src/sensor_registry.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,9 @@ typedef basic_sensor_registry<
317317
hmc8015_sensor,
318318
marker_sensor,
319319
msr_sensor,
320+
#if defined(POWER_OVERWHELMING_WITH_NVML)
320321
nvml_sensor,
322+
#endif /* defined(POWER_OVERWHELMING_WITH_NVML) */
321323
#if defined(POWER_OVERWHELMING_WITH_POWENETICS)
322324
powenetics_sensor,
323325
#endif /* defined(POWER_OVERWHELMING_WITH_POWENETICS) */

test/nvml_test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ TEST_CLASS(nvml_test) {
1717
public:
1818

1919
TEST_METHOD(test_scope) {
20+
#if defined(POWER_OVERWHELMING_WITH_NVML)
2021
detail::nvml_scope scope;
2122
Assert::IsTrue(true, L"Not crashed in scope ctor.", LINE_INFO());
23+
#endif /* defined(POWER_OVERWHELMING_WITH_NVML) */
2224
}
2325

2426
TEST_METHOD(test_descriptions) {
27+
#if defined(POWER_OVERWHELMING_WITH_NVML)
2528
typedef detail::nvml_sensor type;
2629

2730
type::configuration_type config;
@@ -40,9 +43,11 @@ TEST_CLASS(nvml_test) {
4043
Assert::AreNotEqual(std::size_t(0), ::wcslen(d.name()), L"Name not empty", LINE_INFO());
4144
Assert::AreNotEqual(std::size_t(0), ::wcslen(d.path()), L"Path not empty", LINE_INFO());
4245
}
46+
#endif /* defined(POWER_OVERWHELMING_WITH_NVML) */
4347
}
4448

4549
TEST_METHOD(test_sensor_creation) {
50+
#if defined(POWER_OVERWHELMING_WITH_NVML)
4651
typedef detail::nvml_sensor type;
4752

4853
type::configuration_type config;
@@ -62,6 +67,7 @@ TEST_CLASS(nvml_test) {
6267
Assert::AreEqual(std::size_t(1), cnt, L"NVML creates single sample", LINE_INFO());
6368
}, descs.data());
6469
}
70+
#endif /* defined(POWER_OVERWHELMING_WITH_NVML) */
6571
}
6672
};
6773

test/pch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#include <marker_sensor.h>
4646
#include <msr_magic.h>
4747
#include <msr_sensor.h>
48-
#include <nvml_exception.h>
48+
#include <nvml_error_category.h>
4949
#include <nvml_scope.h>
5050
#include <nvml_sensor.h>
5151
#include <rtx_serialisation.h>

test/sensor_array_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ TEST_CLASS(sensor_array_test) {
1717

1818
TEST_METHOD(test_configuration) {
1919
sensor_array_configuration config;
20+
#if defined(POWER_OVERWHELMING_WITH_NVML)
2021
config.configure<nvml_configuration>([](nvml_configuration& c) {
2122
Assert::IsTrue(true, L"NVML configure called.", LINE_INFO());
2223
});
24+
#endif /* defined(POWER_OVERWHELMING_WITH_NVML) */
25+
config.configure<msr_configuration>([](msr_configuration& c) {
26+
Assert::IsTrue(true, L"MSR configure called.", LINE_INFO());
27+
});
2328
}
2429

2530
TEST_METHOD(test_descriptions) {
@@ -85,7 +90,9 @@ TEST_CLASS(sensor_array_test) {
8590
TEST_METHOD(test_exclude) {
8691
sensor_array_configuration config;
8792
config.exclude<hmc8015_configuration>()
93+
#if defined(POWER_OVERWHELMING_WITH_NVML)
8894
.exclude<nvml_configuration>()
95+
#endif /* defined(POWER_OVERWHELMING_WITH_NVML) */
8996
.exclude<tinkerforge_configuration>();
9097

9198
std::vector<sensor_description> descs;

0 commit comments

Comments
 (0)