Add memory locking for deterministic PLC execution#48
Merged
Conversation
- 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>
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
mlockall(MCL_CURRENT | MCL_FUTURE)to the PLC cycle thread initialization to prevent page faults during scan cycle execution. This complements the existingset_realtime_priority()call by ensuring memory pages are not swapped out, eliminating unpredictable latency spikes.Changes:
lock_memory()function inutils.cthat callsmlockall()set_realtime_priority()inplc_cycle_thread()Note: This requires the container to have
--ulimit memlock=-1set. A companion PR in orchestrator-agent adds this ulimit to vPLC container creation.Review & Testing Checklist for Human
<sys/mman.h>include andmlockall()call compile correctly on all target architectures--ulimit memlock=-1and verify "Memory locked successfully" appears in logsMCL_FUTUREcould cause issues on Raspberry Pi or other low-memory devices where locking all future allocations might exhaust physical memoryRecommended test plan:
docker run --cap-add=SYS_NICE --ulimit rtprio=99 --ulimit memlock=-1 ...Notes
<sys/mman.h>include appears in bothutils.handutils.c- this is slightly redundant but harmlessLink to Devin run: https://app.devin.ai/sessions/4a976ea543434bc1b5ee2caf9c8a093e
Requested by: Thiago Alves (@thiagoralves)