Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests against missing fields in prmon txt and json outputs #205

Merged
merged 2 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package/src/Imonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Imonitor {
virtual prmon::monitored_value_map const get_json_total_stats() = 0;
virtual prmon::monitored_average_map const get_json_average_stats(
prmon::mon_value elapsed_clock_ticks) = 0;
virtual prmon::parameter_list const get_parameter_list() = 0;

virtual void const get_hardware_info(nlohmann::json& hw_json) = 0;
virtual void const get_unit_info(nlohmann::json& unit_json) = 0;
Expand Down
3 changes: 2 additions & 1 deletion package/src/MessageBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <map>

#include "Imonitor.h"
#include "prmonutils.h"
#include "registry.h"
#include "spdlog/spdlog.h"

Expand Down Expand Up @@ -32,7 +33,7 @@ void processLevel(std::string s) {

// Check validity of monitor name
bool valid_monitor = false;
auto monitors = registry::Registry<Imonitor>::list_registered();
auto monitors = prmon::get_all_registered();
for (const auto& monitor : monitors) {
if (monitor == monitor_name) {
valid_monitor = true;
Expand Down
5 changes: 4 additions & 1 deletion package/src/countmon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ prmon::monitored_value_map const countmon::get_json_total_stats() {
return count_max_stat_map;
}

// An the averages
// And the averages
prmon::monitored_average_map const countmon::get_json_average_stats(
unsigned long long elapsed_clock_ticks) {
prmon::monitored_average_map count_avg_stat_map{};
Expand All @@ -84,6 +84,9 @@ prmon::monitored_average_map const countmon::get_json_average_stats(
return count_avg_stat_map;
}

// Return the parameter list
prmon::parameter_list const countmon::get_parameter_list() { return params; }

// Collect related hardware information
void const countmon::get_hardware_info(nlohmann::json& hw_json) { return; }

Expand Down
1 change: 1 addition & 0 deletions package/src/countmon.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class countmon final : public Imonitor, public MessageBase {
prmon::monitored_value_map const get_json_total_stats();
prmon::monitored_average_map const get_json_average_stats(
unsigned long long elapsed_clock_ticks);
prmon::parameter_list const get_parameter_list();

// This is the hardware information getter that runs once
void const get_hardware_info(nlohmann::json& hw_json);
Expand Down
3 changes: 3 additions & 0 deletions package/src/cpumon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ prmon::monitored_average_map const cpumon::get_json_average_stats(
return empty_average_stats;
}

// Return the parameter list
prmon::parameter_list const cpumon::get_parameter_list() { return params; }

// Collect related hardware information
void const cpumon::get_hardware_info(nlohmann::json& hw_json) {
// Define the command and run it
Expand Down
1 change: 1 addition & 0 deletions package/src/cpumon.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class cpumon final : public Imonitor, public MessageBase {
prmon::monitored_value_map const get_json_total_stats();
prmon::monitored_average_map const get_json_average_stats(
unsigned long long elapsed_clock_ticks);
prmon::parameter_list const get_parameter_list();

// This is the hardware information getter that runs once
void const get_hardware_info(nlohmann::json& hw_json);
Expand Down
2 changes: 2 additions & 0 deletions package/src/iomon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ prmon::monitored_average_map const iomon::get_json_average_stats(
return io_average_stats;
}

// Return the parameter list
prmon::parameter_list const iomon::get_parameter_list() { return params; }
// Collect related hardware information
void const iomon::get_hardware_info(nlohmann::json& hw_json) { return; }

Expand Down
1 change: 1 addition & 0 deletions package/src/iomon.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class iomon final : public Imonitor, public MessageBase {
prmon::monitored_value_map const get_json_total_stats();
prmon::monitored_average_map const get_json_average_stats(
unsigned long long elapsed_clock_ticks);
prmon::parameter_list const get_parameter_list();

// This is the hardware information getter that runs once
void const get_hardware_info(nlohmann::json& hw_json);
Expand Down
3 changes: 3 additions & 0 deletions package/src/memmon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ prmon::monitored_average_map const memmon::get_json_average_stats(
return count_avg_stat_map;
}

// Return the parameter list
prmon::parameter_list const memmon::get_parameter_list() { return params; }

// Collect related hardware information
void const memmon::get_hardware_info(nlohmann::json& hw_json) {
// Read some information from /proc/meminfo
Expand Down
1 change: 1 addition & 0 deletions package/src/memmon.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class memmon final : public Imonitor, public MessageBase {
std::map<std::string, unsigned long long> const get_json_total_stats();
std::map<std::string, double> const get_json_average_stats(
unsigned long long elapsed_clock_ticks);
prmon::parameter_list const get_parameter_list();

// This is the hardware information getter that runs once
void const get_hardware_info(nlohmann::json& hw_json);
Expand Down
3 changes: 3 additions & 0 deletions package/src/netmon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ prmon::monitored_average_map const netmon::get_json_average_stats(
return json_average_stats;
}

// Return the parameter list
prmon::parameter_list const netmon::get_parameter_list() { return params; }

// Collect related hardware information
void const netmon::get_hardware_info(nlohmann::json& hw_json) { return; }

Expand Down
1 change: 1 addition & 0 deletions package/src/netmon.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class netmon final : public Imonitor, public MessageBase {
prmon::monitored_value_map const get_json_total_stats();
prmon::monitored_average_map const get_json_average_stats(
unsigned long long elapsed_clock_ticks);
prmon::parameter_list const get_parameter_list();

// This is the hardware information getter that runs once
void const get_hardware_info(nlohmann::json& hw_json);
Expand Down
3 changes: 3 additions & 0 deletions package/src/nvidiamon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ bool nvidiamon::test_nvidia_smi() {
return true;
}

// Return the parameter list
prmon::parameter_list const nvidiamon::get_parameter_list() { return params; }

// Collect related hardware information
void const nvidiamon::get_hardware_info(nlohmann::json& hw_json) {
// Record the number of GPUs
Expand Down
1 change: 1 addition & 0 deletions package/src/nvidiamon.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class nvidiamon final : public Imonitor, public MessageBase {
prmon::monitored_value_map const get_json_total_stats();
prmon::monitored_average_map const get_json_average_stats(
unsigned long long elapsed_clock_ticks);
prmon::parameter_list const get_parameter_list();

void const get_hardware_info(nlohmann::json& hw_json);
void const get_unit_info(nlohmann::json& unit_json);
Expand Down
9 changes: 2 additions & 7 deletions package/src/prmon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ int ProcessMonitor(const pid_t mpid, const std::string filename,
// This is the vector of all monitoring components
std::unordered_map<std::string, std::unique_ptr<Imonitor>> monitors{};

auto registered_monitors = registry::Registry<Imonitor>::list_registered();
for (const auto& class_name :
registry::Registry<Imonitor,
std::vector<std::string>>::list_registered()) {
registered_monitors.push_back(class_name);
}
auto registered_monitors = prmon::get_all_registered();
for (const auto& class_name : registered_monitors) {
// Check if the monitor should be enabled
bool state = true;
Expand Down Expand Up @@ -361,7 +356,7 @@ int main(int argc, char* argv[]) {
<< "One of --pid or a child program must be given (but not both)\n"
<< std::endl;
std::cout << "Monitors available:" << std::endl;
auto monitors = registry::Registry<Imonitor>::list_registered();
auto monitors = prmon::get_all_registered();
for (const auto& name : monitors) {
std::cout << " - " << name << " : "
<< registry::Registry<Imonitor>::get_description(name)
Expand Down
17 changes: 16 additions & 1 deletion package/src/prmonutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ bool valid_monitor_disable(const std::string disable_name) {
spdlog::error("wallmon monitor cannot be disabled (ignored)");
return false;
}
auto monitors = registry::Registry<Imonitor>::list_registered();
auto monitors = prmon::get_all_registered();
for (const auto &monitor_name : monitors) {
if (monitor_name == disable_name) {
return true;
Expand Down Expand Up @@ -174,4 +174,19 @@ void snip_string_and_test(char *env_string, unsigned start, unsigned pos,
disabled_monitors.push_back(monitor_name);
}

// Return all registered monitors, regardless of template type
// In practice this means combining monitors with no constructor
// arguments with those (=netmon) which take a list of strings
const std::vector<std::string> get_all_registered() {
// Standard monitors
auto registered_monitors = registry::Registry<Imonitor>::list_registered();
// Special monitors
auto special_monitors =
registry::Registry<Imonitor, std::vector<std::string>>::list_registered();
// Merge
registered_monitors.insert(registered_monitors.end(),
special_monitors.begin(), special_monitors.end());
return registered_monitors;
}

} // namespace prmon
3 changes: 3 additions & 0 deletions package/src/prmonutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ void SignalCallbackHandler(int);
// Child process reaper
int reap_children();

// Utility function to return list of all registered monitors
const std::vector<std::string> get_all_registered();

// Precision specifier for average output, to truncate to an integer
// for anything >avg_precision and round the fraction to
// essentially 1/avg_precision (thus 1000 = 3 decimal places) for
Expand Down
3 changes: 3 additions & 0 deletions package/src/wallmon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ prmon::monitored_average_map const wallmon::get_json_average_stats(
return empty_average_stats;
}

// Return the parameter list
prmon::parameter_list const wallmon::get_parameter_list() { return params; }

// Collect related hardware information
void const wallmon::get_hardware_info(nlohmann::json& hw_json) { return; }

Expand Down
1 change: 1 addition & 0 deletions package/src/wallmon.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class wallmon final : public Imonitor, public MessageBase {
prmon::monitored_value_map const get_json_total_stats();
prmon::monitored_average_map const get_json_average_stats(
unsigned long long elapsed_clock_ticks);
prmon::parameter_list const get_parameter_list();

// This is the hardware information getter that runs once
void const get_hardware_info(nlohmann::json& hw_json);
Expand Down
35 changes: 35 additions & 0 deletions package/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ if (${CMAKE_VERSION} VERSION_GREATER "3.14.0" AND BUILD_GTESTS)
${CMAKE_CURRENT_SOURCE_DIR}/../src/iomon.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/utils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/MessageBase.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/prmonutils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/parameter.cpp
)
target_compile_definitions(test_values PRIVATE TESTS_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}/../scripts/precooked_tests/)
Expand All @@ -68,6 +69,40 @@ if (${CMAKE_VERSION} VERSION_GREATER "3.14.0" AND BUILD_GTESTS)
)
gtest_discover_tests(test_values)

add_executable(test_fields test_fields.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/countmon.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/cpumon.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/memmon.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/netmon.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/nvidiamon.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/iomon.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/utils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/MessageBase.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/parameter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/prmonutils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/utils.cpp
)
target_compile_definitions(test_fields PRIVATE PRMON_SOURCE_DIR=${CMAKE_BINARY_DIR})

target_include_directories(test_fields PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/../include
${CMAKE_CURRENT_SOURCE_DIR}/../src
${CMAKE_CURRENT_SOURCE_DIR}/../..
)
if(TARGET nlohmann_json::nlohmann_json)
target_link_libraries(test_fields
PRIVATE
nlohmann_json::nlohmann_json
)
endif(TARGET nlohmann_json::nlohmann_json)
target_link_libraries(test_fields
PRIVATE
Threads::Threads
gtest_main
)
add_test(NAME testFieldsAll COMMAND test_fields)
add_test(NAME testFieldsSomeDisabled COMMAND test_fields --disable netmon --disable cpumon)

endif()

# Custom targets for handling scripted wrappers for tests
Expand Down
Loading