Skip to content

Power consumption monitoring - #324

Open
AlexPG1 wants to merge 25 commits into
HSF:mainfrom
AlexPG1:main
Open

AlexPG1 wants to merge 25 commits into
HSF:mainfrom
AlexPG1:main

Conversation

@AlexPG1

@AlexPG1 AlexPG1 commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

This contribution aims to include two additional collectors onto prmon. Machmon extracts the full machine cpu times and curlmon reads power consumption metrics if exposed into ports 9100 or 9290 as done by prometheus exporters. These two together would allow an estimation of power consumption due to the monitored process over time. See this presentation for more information.

@graeme-a-stewart

Copy link
Copy Markdown
Member

Hello @AlexPG1 - thanks for this. I am starting to review now.

I fixed a few CI issues for you - we now need curl development packages in the CI environment. The CI failure now looks genuine, in testUnits:

You are producing average values for ntime_total, utime_total, stime_total without units. Although to me I think these values make no sense as averages. So here:

  const prmon::parameter_list params = {
      {"utime_total", "s", "Total user CPU time from /proc/stat"},
      {"stime_total", "s", "Total system CPU time from /proc/stat"},
      {"ntime_total", "s", "Total nice CPU time from /proc/stat"},
  };

The third string value should rather be an empty string, "", which signals to suppress an average value in the final stats. See https://github.com/HSF/prmon/blob/main/doc/ADDING_MONITORS.md#parameter-class.

@graeme-a-stewart

Copy link
Copy Markdown
Member

Re the CodeFactor issues: fix the blank line on; the method complexity is a false positive for me, so ignore it.

@graeme-a-stewart graeme-a-stewart left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexPG1 - great start here, this mostly sound in terms of logic when everything works, but the robustness needs work - see comments inline.

Just again to underline that curlmon is a bad name, powermon would be better.

I do suggest that we bite the bullet and allow the endpoints to be configurable, from the environment (which makes the robustness checks all the more important). As this monitor is a bit special (relying on non-standard infrastructure) we need to document how it works (new file in doc, and a link from the README).

Also needed - tests of the monitor! The idea here would be to precook some inputs as if they came from the curl call, and use that to check the monitor behaves correctly (for expected and unexpected inputs). See the PRECOOKED_SOURCES.md file for how this works for a few of the current monitors...

Comment thread package/src/curlmon.h Resolved
Comment thread package/src/machmon.h Resolved
Comment thread package/src/curlmon.h Resolved
Comment thread package/src/curlmon.h Resolved
Comment thread package/src/curlmon.h Resolved
Comment thread package/src/curlmon.cpp Resolved
Comment thread package/src/powermon.cpp Resolved
Comment thread package/src/curlmon.cpp Resolved
Comment thread package/src/curlmon.cpp Resolved
Comment thread package/src/curlmon.cpp Resolved
@graeme-a-stewart

Copy link
Copy Markdown
Member

Thanks @AlexPG1 - can you check the build failures? FYI, I think it would now be totally acceptable to move to C++14 (I'd even consider 17...).

I have invited you as a developer to the project, so that the CI will run automatically in the future.

@AlexPG1

AlexPG1 commented Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

Thank you @graeme-a-stewart for all the corrections and the permissions to keep testing automatically!

Now powermon reads this PROMETHEUS_LOCAL_URL variable. It first checks whether this variable exists, then extracts directories or urls, then checks if either of those yield the proper metrics. This way, we would be able to read for instance HEP's proposal at CERN just by them defining the environment variable for /var/cache/node_exporter/ipmi.prom.

I also updated precook_test.py to generate values for machmon and powermon. I will now start looking through the tests to make a similar one / update existing ones for both of these collectors!

@graeme-a-stewart graeme-a-stewart left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @AlexPG1 - great, much improved. I did a review with Claude and it found a few other issues that need fixed.

Comment thread package/src/powermon.cpp
};

// Execute curl, check filesize
CURLcode curl_response = curl_easy_perform(curl);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a resource leak! There's no curl_easy_cleanup called here. Unless we can hold the curl connection between calls, there should be a curl_easy_cleanup() immediately.

Comment thread package/src/powermon.cpp Outdated
rc = curl_easy_setopt(curl, option, value);
if (rc != CURLE_OK) {
error("curl_easy_setopt failed: " + std::string(curl_easy_strerror(rc)));
curl_easy_cleanup(curl);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this cleanup line. If you fail one of the calls in the L150:162 block below then you automatically call curl_easy_cleanup at L163. Then you have a double "free".

Comment thread package/src/powermon.cpp Outdated

// Execute curl, check filesize
CURLcode curl_response = curl_easy_perform(curl);
if (curl_response == CURLE_FILESIZE_EXCEEDED || curl_response == CURLE_WRITE_ERROR) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be a bit stricter here:

  • Set the CURLOPT_FAILONERROR option above
  • Consider anything which is not CURLE_OK as an error

Comment thread package/src/powermon.cpp Outdated
// Collect Prometheus-style text from, in order of priority:
// 1. exporter URLs (PROMETHEUS_LOCAL_URL or default)
// 2. directories from PROMETHEUS_LOCAL_URL
// 3. default directory <read_path>/power

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like option (3) is not actually implemented, right? read_path is not used (compiler issued a warning). Isn't this needed, or am I not seeing the logic correctly?

Comment thread package/src/powermon.cpp Outdated
double hwmon = 0.0;
try {
if (!hwmon_lines.empty()) {
double val = parse_metric_line(hwmon_lines.back(), "node_hwmon_power_average_watt");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't you missing parsing node_hwmon_power_watt here? This is accepted by collect_lines().

Comment thread package/src/machmon.cpp Outdated
const std::string stat_path = read_path + "/proc/stat";
std::ifstream proc_stat{stat_path};
if (!proc_stat) {
throw std::runtime_error("Cannot open " + stat_path);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't throw unless your monitor is also catching - you would abort prmon in this case! Just print a warning and skip this iteration.

Comment thread package/src/machmon.cpp Outdated

// First line: "cpu user nice system idle ..."
if (!(proc_stat >> cpu_label >> user >> nice >> system)) {
throw std::runtime_error("Unexpected format in " + stat_path);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above!

Comment thread package/src/machmon.cpp
#define MONITOR_NAME "machmon"

// Constructor; uses RAII pattern to be valid after construction
machmon::machmon() : mach_stats{} {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For these machine level stats, you should take baseline measurements at initialisation, as is done with netmon. That ensures you subtract the baselines that have been determined as early as possible,

Comment thread package/src/machmon.cpp Outdated
initialised = true;
}

const long clk = sysconf(_SC_CLK_TCK);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor point - sysconf(_SC_CLK_TCK) could be read during initialisation and stored, rather than read each time.

@AlexPG1

AlexPG1 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

Hi @graeme-a-stewart, I fixed the issues you pointed out! I also added two new tests (test_mach.py and test_power.py) along with a first draft for the additional documentation on powermon. Perhaps I should have been less verbose about power consumption metrics and/or more verbose about how powermon works?

Thanks in advance,
Alejandro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants