Skip to content

Add immediate and final sampling to ProcessMonitor#300

Merged
graeme-a-stewart merged 5 commits intoHSF:mainfrom
Siddhant2306:feature/immediate-and-final-sampling
Feb 24, 2026
Merged

Add immediate and final sampling to ProcessMonitor#300
graeme-a-stewart merged 5 commits intoHSF:mainfrom
Siddhant2306:feature/immediate-and-final-sampling

Conversation

@Siddhant2306
Copy link
Contributor

@Siddhant2306 Siddhant2306 commented Feb 18, 2026

Ensure at least one sample is recorded for short-lived processes

Summary

This PR fixes an edge case where prmon may produce no output when monitoring very short-lived processes.

Previously, if the monitored process exited before the first sampling interval elapsed, no sampling iteration was executed and no data rows were written.

With this change, the monitoring loop guarantees that the first sampling iteration executes immediately, ensuring that at least one sample is always recorded — even for very short-lived processes.

The existing interval-based behavior for long-running processes remains unchanged.


Problem Description

prmon performs sampling only when:

time(0) - lastIteration > interval

If the monitored process terminates before the first interval elapses, the sampling condition is never satisfied.

As a result:

  • No data rows are written to prmon.txt
  • JSON output contains no runtime statistics
  • A warning is emitted indicating no samples were recorded

This behavior affects very short-lived processes.


Proposed Solution

The monitoring loop is modified to ensure the first sampling iteration always executes immediately.

Specifically:

  • A first flag is introduced inside the monitoring loop
  • The sampling condition becomes: if (first || time(0) - lastIteration > interval
  • After the first iteration, first is set to false
  • All existing sampling, JSON update, and aggregation logic remains unchanged

This guarantees:

  • At least one sampling iteration
  • No structural change to output format
  • No change in behavior for long-running processes
  • No additional iterations after process exit

Testing

The following scenarios were tested:

Short-lived process

./prmon --interval 5 -- sleep 0.1

✔ One sample recorded
✔ No warning emitted
✔ Output files contain valid data

Full test suite

All existing unit and integration tests pass, including:

  • testFieldsAll
  • testFieldsSomeDisabled
  • basicNET
  • All CPU, memory, IO, and JSON tests

This change preserves existing behavior while ensuring consistent output for short-lived processes.

@graeme-a-stewart
Copy link
Member

Hello @Siddhant2306

Thank you for your interest in prmon and for contributing to it!

I have to say that this really is a very minor issue - prmon is not designed to monitor in a useful way very short lived processes, hence the warning which is printed when not even one sample period elapsed. No one should be using prmon in those circumstances.

However, with your patch it does make the output a bit more consistent, so we could accept it as a minor fix.

Copy link
Member

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

Choose a reason for hiding this comment

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

A few tidy up changes

Copy link
Member

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

Choose a reason for hiding this comment

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

Looking good now - thanks!

@graeme-a-stewart graeme-a-stewart merged commit f585915 into HSF:main Feb 24, 2026
8 checks passed
@graeme-a-stewart
Copy link
Member

@all-contributors please add @Siddhant2306 for code

@allcontributors
Copy link
Contributor

@graeme-a-stewart

I've put up a pull request to add @Siddhant2306! 🎉

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