Skip to content

Comments

Add immediate and final sampling to ProcessMonitor#300

Open
Siddhant2306 wants to merge 3 commits intoHSF:mainfrom
Siddhant2306:feature/immediate-and-final-sampling
Open

Add immediate and final sampling to ProcessMonitor#300
Siddhant2306 wants to merge 3 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.

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.

1 participant