Skip to content

refactor: eliminate powermetrics temp file growth with in-memory buffer#4

Merged
inureyes merged 2 commits into
mainfrom
update/reduce-powermetrics-temp-file-size
Jul 10, 2025
Merged

refactor: eliminate powermetrics temp file growth with in-memory buffer#4
inureyes merged 2 commits into
mainfrom
update/reduce-powermetrics-temp-file-size

Conversation

@inureyes

@inureyes inureyes commented Jul 10, 2025

Copy link
Copy Markdown
Member

Summary

This PR refactors the PowerMetricsManager to use an in-memory circular buffer instead of writing to temporary files, eliminating the unbounded file growth issue where /tmp/all-smi_powermetrics_* files would grow at ~7MB/hour for long-running instances.

Problem

  • Powermetrics was writing to temp files that grew continuously (1.2GB/week)
  • No rotation or truncation mechanism existed
  • Files required sudo privileges to clean up
  • Disk space could be exhausted on systems with limited /tmp capacity

Solution

Implemented a pipe-based approach with in-memory circular buffer:

  • Capture powermetrics stdout directly via pipe
  • Store last 120 samples (2 minutes) in a VecDeque circular buffer
  • Dedicated reader thread processes stdout lines without blocking
  • Remove all file-based operations and cleanup logic

Benefits

  • Zero disk usage - no temporary files created
  • Better performance - eliminates file I/O overhead
  • More secure - no sensitive metrics data written to disk
  • Simpler cleanup - no sudo rm operations needed

Implementation Details

  • BufReader prevents stdout buffer deadlock
  • lines() iterator ensures only complete lines are processed
  • Process restart functionality maintained with new channel creation
  • Tests updated to work with buffer-based approach

Test plan

  • Unit tests pass
  • Build completes without warnings
  • Manual testing shows powermetrics data collection works
  • Long-running test to verify no disk usage growth
  • Verify process restart handling works correctly

…lar buffer

This refactoring eliminates the unbounded growth of temporary files at /tmp/all-smi_powermetrics_*
by using a pipe-based approach with an in-memory circular buffer.

Key changes:
- Removed file-based output (-o/-u flags) in favor of stdout piping
- Implemented circular buffer (VecDeque) to store last 120 powermetrics samples (2 minutes)
- Added dedicated reader thread to process stdout lines without blocking
- Removed all temporary file cleanup logic as it's no longer needed
- Updated tests to work with the new buffer-based approach

Benefits:
- Zero disk usage - no more GB-sized temp files for long-running instances
- Better performance - no file I/O overhead
- More secure - no sensitive data written to disk
- Cleaner cleanup - no sudo rm required for temp files

The implementation handles all edge cases:
- BufReader prevents stdout buffer deadlock
- lines() iterator ensures complete line parsing
- Process death detection and restart remain functional
- Channel-based communication avoids lock contention
@inureyes inureyes added the type:refactor Code refactoring label Jul 10, 2025
Added 10 new test cases covering:
- Circular buffer limits and overflow protection
- Reader thread line parsing and shutdown handling
- Process info extraction from buffer
- Multiple sections in buffer
- Empty buffer fallback to cache
- Malformed section handling
- Concurrent buffer access
- Process restart scenarios

All tests pass, ensuring the pipe-based implementation is robust.
@inureyes inureyes self-assigned this Jul 10, 2025
@inureyes inureyes merged commit ab8b5ac into main Jul 10, 2025
3 checks passed
@inureyes inureyes deleted the update/reduce-powermetrics-temp-file-size branch July 10, 2025 17:13
@inureyes inureyes added priority:medium Medium priority issue status:done Completed feature labels Sep 10, 2025
@inureyes inureyes added type:enhancement New feature or request and removed feature labels Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority issue status:done Completed type:enhancement New feature or request type:refactor Code refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant