Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/src/plc_app/plc_state_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ void *plc_cycle_thread(void *arg)
{
PluginManager *pm = (PluginManager *)arg;

// Initialize PLC
// Initialize PLC with real-time optimizations
set_realtime_priority();
lock_memory();
symbols_init(pm);
ext_config_init__();
ext_glueVars();
Expand Down
14 changes: 14 additions & 0 deletions core/src/plc_app/utils/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>

unsigned long long *ext_common_ticktime__ = NULL;
Expand Down Expand Up @@ -55,6 +56,19 @@ void set_realtime_priority(void)
}
}

// Lock all memory pages to prevent page faults during PLC execution
void lock_memory(void)
{
if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0)
{
log_error("mlockall failed: %s", strerror(errno));
}
else
{
log_info("Memory locked successfully (MCL_CURRENT | MCL_FUTURE)");
}
}

size_t parse_hex_string(const char *hex_string, uint8_t *data)
{
size_t count = 0;
Expand Down
9 changes: 9 additions & 0 deletions core/src/plc_app/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <dlfcn.h>
#include <sched.h>
#include <sys/mman.h>
#include <time.h>
#include <stdint.h>

Expand Down Expand Up @@ -42,6 +43,14 @@ void timespec_diff(struct timespec *a, struct timespec *b,
*/
void set_realtime_priority(void);

/**
* @brief Lock all current and future memory pages to prevent page faults
*
* This prevents the kernel from swapping out memory pages during PLC execution,
* which could cause unpredictable latency spikes in the scan cycle.
*/
void lock_memory(void);

/**
* @brief Parse a hex string into a byte array
*
Expand Down