Skip to content

Add memory locking for deterministic PLC execution#48

Merged
thiagoralves merged 1 commit into
developmentfrom
devin/1765471497-add-memory-locking
Dec 11, 2025
Merged

Add memory locking for deterministic PLC execution#48
thiagoralves merged 1 commit into
developmentfrom
devin/1765471497-add-memory-locking

Conversation

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Summary

Adds mlockall(MCL_CURRENT | MCL_FUTURE) to the PLC cycle thread initialization to prevent page faults during scan cycle execution. This complements the existing set_realtime_priority() call by ensuring memory pages are not swapped out, eliminating unpredictable latency spikes.

Changes:

  • New lock_memory() function in utils.c that calls mlockall()
  • Called immediately after set_realtime_priority() in plc_cycle_thread()
  • Logs success/failure but continues execution if locking fails (non-fatal)

Note: This requires the container to have --ulimit memlock=-1 set. A companion PR in orchestrator-agent adds this ulimit to vPLC container creation.

Review & Testing Checklist for Human

  • Verify compilation: Build the project to ensure <sys/mman.h> include and mlockall() call compile correctly on all target architectures
  • Test with proper ulimits: Run container with --ulimit memlock=-1 and verify "Memory locked successfully" appears in logs
  • Test without ulimits: Run container without memlock ulimit and verify it logs an error but continues running (graceful degradation)
  • Memory impact on constrained devices: Consider if MCL_FUTURE could cause issues on Raspberry Pi or other low-memory devices where locking all future allocations might exhaust physical memory

Recommended test plan:

  1. Build Docker image with these changes
  2. Run with: docker run --cap-add=SYS_NICE --ulimit rtprio=99 --ulimit memlock=-1 ...
  3. Start PLC and check logs for "Memory locked successfully"
  4. Monitor memory usage during extended operation

Notes

  • The <sys/mman.h> include appears in both utils.h and utils.c - this is slightly redundant but harmless
  • This change was not locally compiled/tested before submission

Link to Devin run: https://app.devin.ai/sessions/4a976ea543434bc1b5ee2caf9c8a093e
Requested by: Thiago Alves (@thiagoralves)

- Add lock_memory() function that calls mlockall(MCL_CURRENT | MCL_FUTURE)
- Call lock_memory() after set_realtime_priority() in plc_cycle_thread()
- Add sys/mman.h include for mlockall support

This prevents the kernel from swapping out memory pages during PLC
execution, eliminating unpredictable latency spikes caused by page faults
in the scan cycle. Requires container to have memlock ulimit set to
unlimited (--ulimit memlock=-1).

Co-Authored-By: Thiago Alves <thiagoralves@gmail.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@thiagoralves thiagoralves merged commit d3c0cc7 into development Dec 11, 2025
1 check passed
@thiagoralves thiagoralves deleted the devin/1765471497-add-memory-locking branch December 11, 2025 18:25
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