From 16f1c520255924280d309db73444759002fdfb00 Mon Sep 17 00:00:00 2001 From: Denis Date: Mon, 4 Mar 2024 03:12:54 +0100 Subject: [PATCH] Added support for real-time kernels --- allynone.c | 8 ++++++++ main.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- stray_ally.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 99 insertions(+), 2 deletions(-) diff --git a/allynone.c b/allynone.c index d2b0fb5..e95d6a7 100644 --- a/allynone.c +++ b/allynone.c @@ -10,9 +10,17 @@ #include "rog_ally.h" #include "legion_go.h" +#include + static const char* configuration_file = "/etc/ROGueENEMY/config.cfg"; int main(int argc, char ** argv) { + // Lock all current and future pages from preventing of being paged to swap + const int lockall_res = mlockall( MCL_CURRENT | MCL_FUTURE ); + if (lockall_res) { + fprintf(stderr, "mlockall failed: %d", lockall_res); + } + int ret = 0; // fill in configuration from file: automatic fallback to default diff --git a/main.c b/main.c index 9446964..a598955 100644 --- a/main.c +++ b/main.c @@ -10,9 +10,17 @@ #include "rog_ally.h" #include "legion_go.h" +#include + static const char* configuration_file = "/etc/ROGueENEMY/config.cfg"; int main(int argc, char ** argv) { + // Lock all current and future pages from preventing of being paged to swap + const int lockall_res = mlockall( MCL_CURRENT | MCL_FUTURE ); + if (lockall_res) { + fprintf(stderr, "mlockall failed: %d", lockall_res); + } + int ret = 0; dev_in_settings_t in_settings = { @@ -74,8 +82,45 @@ int main(int argc, char ** argv) { //memset(&dev_in_thread_data.communication.endpoint.socket.serveraddr, 0, sizeof(dev_in_thread_data.communication.endpoint.socket.serveraddr)); + // Initialize pthread attributes (default values) + struct sched_param param; + pthread_attr_t attr; + ret = pthread_attr_init(&attr); + if (ret) { + printf("init pthread attributes failed\n"); + goto main_err; + } + + // Set a specific stack size + ret = pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 8192); + if (ret) { + printf("pthread setstacksize failed\n"); + goto main_err; + } + + // Set scheduler policy and priority of pthread + ret = pthread_attr_setschedpolicy(&attr, SCHED_FIFO); + if (ret) { + printf("pthread setschedpolicy failed\n"); + goto main_err; + } + param.sched_priority = 80; + ret = pthread_attr_setschedparam(&attr, ¶m); + if (ret) { + printf("pthread setschedparam failed\n"); + goto main_err; + } + + // Use scheduling parameters of attr + ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); + if (ret) { + printf("pthread setinheritsched failed\n"); + goto main_err; + } + + pthread_t dev_in_thread; - const int dev_in_thread_creation = pthread_create(&dev_in_thread, NULL, dev_in_thread_func, (void*)(&dev_in_thread_data)); + const int dev_in_thread_creation = pthread_create(&dev_in_thread, &attr, dev_in_thread_func, (void*)(&dev_in_thread_data)); if (dev_in_thread_creation != 0) { fprintf(stderr, "Error creating dev_in thread: %d\n", dev_in_thread_creation); ret = -1; diff --git a/stray_ally.c b/stray_ally.c index b156adb..dd39718 100644 --- a/stray_ally.c +++ b/stray_ally.c @@ -7,9 +7,17 @@ #include "dev_out.h" #include "settings.h" +#include + static const char* configuration_file = "/etc/ROGueENEMY/config.cfg"; int main(int argc, char ** argv) { + // Lock all current and future pages from preventing of being paged to swap + const int lockall_res = mlockall( MCL_CURRENT | MCL_FUTURE ); + if (lockall_res) { + fprintf(stderr, "mlockall failed: %d", lockall_res); + } + int ret = 0; dev_out_settings_t out_settings = { @@ -62,8 +70,44 @@ int main(int argc, char ** argv) { load_out_config(&dev_out_thread_data.settings, configuration_file); + // Initialize pthread attributes (default values) + struct sched_param param; + pthread_attr_t attr; + ret = pthread_attr_init(&attr); + if (ret) { + printf("init pthread attributes failed\n"); + goto main_err; + } + + // Set a specific stack size + ret = pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + 8192); + if (ret) { + printf("pthread setstacksize failed\n"); + goto main_err; + } + + // Set scheduler policy and priority of pthread + ret = pthread_attr_setschedpolicy(&attr, SCHED_FIFO); + if (ret) { + printf("pthread setschedpolicy failed\n"); + goto main_err; + } + param.sched_priority = 80; + ret = pthread_attr_setschedparam(&attr, ¶m); + if (ret) { + printf("pthread setschedparam failed\n"); + goto main_err; + } + + // Use scheduling parameters of attr + ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); + if (ret) { + printf("pthread setinheritsched failed\n"); + goto main_err; + } + pthread_t dev_out_thread; - const int dev_out_thread_creation = pthread_create(&dev_out_thread, NULL, dev_out_thread_func, (void*)(&dev_out_thread_data)); + const int dev_out_thread_creation = pthread_create(&dev_out_thread, &attr, dev_out_thread_func, (void*)(&dev_out_thread_data)); if (dev_out_thread_creation != 0) { fprintf(stderr, "Error creating dev_out thread: %d\n", dev_out_thread_creation); ret = -1;