Skip to content

Commit

Permalink
Added tests for sample_data().
Browse files Browse the repository at this point in the history
Added more documentation for sample_data().
  • Loading branch information
crowbar27 committed Jun 25, 2023
1 parent a3fe398 commit 840f4f4
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 24 deletions.
25 changes: 25 additions & 0 deletions podump/adl_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@ void sample_adl_sensor(void) {
}


/*
* ::sample_adl_sensor_data
*/
void sample_adl_sensor_data(void) {
using namespace visus::power_overwhelming;

try {
std::vector<adl_sensor> sensors;
sensors.resize(adl_sensor::for_all(nullptr, 0));
adl_sensor::for_all(sensors.data(), sensors.size());

for (auto &s : sensors) {
std::wcout << s.name() << L":" << std::endl;
auto m = s.sample_data();
std::wcout << m.timestamp() << L": "
<< m.voltage() << L" V, "
<< m.current() << L" A, "
<< m.power() << L" W" << std::endl;
}
} catch (std::exception &ex) {
std::cerr << ex.what() << std::endl;
}
}


/*
* ::sample_adl_sensor_async
*/
Expand Down
5 changes: 5 additions & 0 deletions podump/adl_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
/// </summary>
void sample_adl_sensor(void);

/// <summary>
/// Print data for all supported AMD cards using the pure sample method.
/// </summary>
void sample_adl_sensor_data(void);

/// <summary>
/// Sample all supported ADL sensors for the specified number of seconds.
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions podump/emi_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,31 @@ void sample_emi_sensor(void) {
}


/*
* ::sample_emi_sensor_data
*/
void sample_emi_sensor_data(void) {
using namespace visus::power_overwhelming;

#if defined(_WIN32)
try {
std::vector<emi_sensor> sensors;
sensors.resize(emi_sensor::for_all(nullptr, 0));
emi_sensor::for_all(sensors.data(), sensors.size());

for (auto &s : sensors) {
std::wcout << s.name() << L":" << std::endl;
auto m = s.sample_data();
std::wcout << m.timestamp() << L": "
<< m.power() << L" W" << std::endl;
}
} catch (std::exception &ex) {
std::cerr << ex.what() << std::endl;
}
#endif /* defined(_WIN32) */
}


/*
* ::sample_emi_sensor_async
*/
Expand Down
6 changes: 6 additions & 0 deletions podump/emi_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
/// </summary>
void sample_emi_sensor(void);

/// <summary>
/// Samples all EMI sensors once using the pure sampling API and prints their
/// values on the console.
/// </summary>
void sample_emi_sensor_data(void);

/// <summary>
/// Samples all EMI sensors for the specified number of seconds.
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions podump/msr_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ void sample_msr_sensor(void) {
}


/*
* ::sample_msr_sensor_data
*/
void sample_msr_sensor_data(void) {
using namespace visus::power_overwhelming;

try {
std::vector<msr_sensor> sensors;
sensors.resize(msr_sensor::for_all(nullptr, 0));
msr_sensor::for_all(sensors.data(), sensors.size());

for (auto &s : sensors) {
std::wcout << s.name() << L":" << std::endl;
auto m = s.sample_data();
std::wcout << m.timestamp() << L": "
<< m.power() << L" W" << std::endl;
}
} catch (std::exception &ex) {
std::cerr << ex.what() << std::endl;
}
}


/*
* ::sample_msr_sensor_async
*/
Expand Down
7 changes: 6 additions & 1 deletion podump/msr_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@


/// <summary>
/// Print data for all CPU cores cards.
/// Print data for all CPU cores.
/// </summary>
void sample_msr_sensor(void);

/// <summary>
/// Print data for all CPU cores using the pure sampling API.
/// </summary>
void sample_msr_sensor_data(void);

/// <summary>
/// Sample all supported MSR sensors for the specified number of seconds.
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions podump/nvml_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ void sample_nvml_sensor(void) {
}


/*
* ::sample_nvml_sensor_data
*/
void sample_nvml_sensor_data(void) {
using namespace visus::power_overwhelming;

try {
std::vector<nvml_sensor> sensors;
sensors.resize(nvml_sensor::for_all(nullptr, 0));
nvml_sensor::for_all(sensors.data(), sensors.size());

for (auto &s : sensors) {
std::wcout << s.name() << L":" << std::endl;
auto m = s.sample_data();
std::wcout << m.timestamp() << L": " << m.power() << L" W"
<< std::endl;
}
} catch (std::exception &ex) {
std::cerr << ex.what() << std::endl;
}
}


/*
* ::sample_nvml_sensor_async
*/
Expand Down
5 changes: 5 additions & 0 deletions podump/nvml_sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
/// </summary>
void sample_nvml_sensor(void);

/// <summary>
/// Print data for all supported NVIDIA cards using the pure sample method.
/// </summary>
void sample_nvml_sensor_data(void);

/// <summary>
/// Sample all supported NVML sensors for the specified number of seconds.
/// </summary>
Expand Down
18 changes: 17 additions & 1 deletion podump/podump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ int _tmain(const int argc, const TCHAR **argv) {
#endif

// AMD sensors
#if false
#if true
#if true
::sample_adl_sensor();
#endif

#if true
::sample_adl_sensor_data();
#endif

#if true
::sample_adl_sensor_async(5);
#endif
Expand All @@ -54,6 +58,10 @@ int _tmain(const int argc, const TCHAR **argv) {
::sample_emi_sensor();
#endif

#if true
::sample_emi_sensor_data();
#endif

#if true
::sample_emi_sensor_async(5);
#endif
Expand All @@ -76,6 +84,10 @@ int _tmain(const int argc, const TCHAR **argv) {
::sample_nvml_sensor();
#endif

#if true
::sample_nvml_sensor_data();
#endif

#if true
::sample_nvml_sensor_async(5);
#endif
Expand All @@ -87,6 +99,10 @@ int _tmain(const int argc, const TCHAR **argv) {
::sample_tinkerforge_sensor();
#endif

#if true
::sample_tinkerforge_sensor_data();
#endif

#if true
::sample_tinkerforge_sensor_async(5);
#endif
Expand Down
33 changes: 33 additions & 0 deletions podump/tinkerforge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,39 @@ void sample_tinkerforge_sensor(void) {
}


/*
* ::sample_tinkerforge_sensor_data
*/
void sample_tinkerforge_sensor_data(void) {
using namespace visus::power_overwhelming;

try {
std::vector<tinkerforge_sensor_definition> descs;
descs.resize(tinkerforge_sensor::get_definitions(nullptr, 0));
auto cnt = tinkerforge_sensor::get_definitions(descs.data(),
descs.size());

if (cnt < descs.size()) {
descs.resize(cnt);
}

for (auto &d : descs) {
tinkerforge_sensor s(d);
std::wcout << s.name() << L":" << std::endl;
auto m = s.sample_data();
std::wcout << m.timestamp() << L": "
<< m.voltage() << " V * "
<< m.current() << " A = "
<< m.power() << L" W"
<< std::endl;
}

} catch (std::exception &ex) {
std::cerr << ex.what() << std::endl;
}
}


/*
* ::sample_tinkerforge_sensor_async
*/
Expand Down
5 changes: 5 additions & 0 deletions podump/tinkerforge.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
/// </summary>
void sample_tinkerforge_sensor(void);

/// <summary>
/// Print values of all Tinkerforge bricklets attached to the machine using the
/// pure sampling API.
/// </summary>
void sample_tinkerforge_sensor_data(void);

/// <summary>
/// Samples all Tinkerforge bricklets attached to the machine for the specified
Expand Down
42 changes: 20 additions & 22 deletions power_overwhelming/include/power_overwhelming/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ namespace power_overwhelming {
/// </summary>
/// <param name="resolution">The resolution of the timestamp to be
/// created. This value basically determines the unit in which the
/// timestamp in the return value is measured.</param>
/// timestamp in the return value is measured. This parameter defaults
/// to <see cref="timestamp_resolution::milliseconds" />.</param>
/// <returns>A single measurement made by the sensor.</returns>
/// <exception cref="std::runtime_error">If a sensor that has been moved
/// (and therefore disposed) is sampled.</exception>
Expand All @@ -69,7 +70,8 @@ namespace power_overwhelming {
/// instrument could not be sampled.</exception>
/// <exception cref="std::system_error">If a sensor could not be sampled
/// due to a system call failing.</exception>
measurement sample(_In_ const timestamp_resolution resolution) const;
measurement sample(_In_ const timestamp_resolution resolution
= timestamp_resolution::milliseconds) const;

/// <summary>
/// Sample the sensor using a timestamp with the specified resolution,
Expand All @@ -85,10 +87,23 @@ namespace power_overwhelming {
/// for applications using sensors that can only be sampled on the
/// machine under observation, like GPU power sensors and the RAPL
/// registers.</para>
/// <para>Please be aware that you might not be able to obtain all
/// information about the sensor that the data originate from when
/// using this method. This is the case if the underlying API can
/// produce samples from different sources. Such sensors must be
/// configured to make sure that they only return the data expected
/// when using this method for sampling.</para>
/// <para>Rationale: The naming of this method (and of
/// <see cref="measurement_data" />) is a bit weird, which is due to the
/// fact that samples without the originating sensor embedded have been
/// added later to the library. The previous type and method names have
/// not been changed at this point in order to not break existing code
/// relying on the library.</para>
/// </remarks>
/// <param name="resolution">The resolution of the timestamp to be
/// created. This value basically determines the unit in which the
/// timestamp in the return value is measured.</param>
/// timestamp in the return value is measured. This parameter defaults
/// to <see cref="timestamp_resolution::milliseconds" />.</param>
/// <returns>A single measurement made by the sensor.</returns>
/// <exception cref="std::runtime_error">If a sensor that has been moved
/// (and therefore disposed) is sampled.</exception>
Expand All @@ -103,28 +118,11 @@ namespace power_overwhelming {
/// <exception cref="std::system_error">If a sensor could not be sampled
/// due to a system call failing.</exception>
inline measurement_data sample_data(
_In_ const timestamp_resolution resolution) const {
_In_ const timestamp_resolution resolution
= timestamp_resolution::milliseconds) const {
return this->sample_sync(resolution);
}

/// <summary>
/// Sample the sensor using a timestamp with millisecond resolution.
/// </summary>
/// <returns>A single measurement made by the sensor.</returns>
/// <exception cref="std::runtime_error">If a sensor that has been moved
/// (and therefore disposed) is sampled.</exception>
/// <exception cref="adl_exception">If an ADL sensor could not be
/// sampled.</exception>
/// <exception cref="nvml_exception">If an NVML sensor could not be
/// sampled.</exception>
/// <exception cref="tinkerforge_exception">If a Tinkerforge sensor
/// could not be sampled.</exception>
/// <exception cref="visa_exception">If a sensor based on a VISA
/// instrument could not be sampled.</exception>
inline measurement sample(void) const {
return this->sample(timestamp_resolution::milliseconds);
}

/// <summary>
/// Determines whether the sensor is valid.
/// </summary>
Expand Down

0 comments on commit 840f4f4

Please sign in to comment.