From 69fc902b52f2319b1dbd55b15f253197143aa585 Mon Sep 17 00:00:00 2001 From: Michael Cook Date: Mon, 18 Oct 2021 22:04:19 +0000 Subject: [PATCH] Set alarm --- common/utils/utils.c | 32 +++++++++++++++++++++++++++++++ common/utils/utils.h | 1 + executables/nr-uesoftmodem.c | 7 +++++++ nfapi/oai_integration/nfapi.c | 10 ++-------- nfapi/oai_integration/nfapi_pnf.c | 9 ++------- targets/RT/USER/lte-softmodem.c | 10 +--------- targets/RT/USER/lte-uesoftmodem.c | 10 +--------- 7 files changed, 46 insertions(+), 33 deletions(-) diff --git a/common/utils/utils.c b/common/utils/utils.c index 05e5fdf3a7..f0f3ddf8a7 100644 --- a/common/utils/utils.c +++ b/common/utils/utils.c @@ -2,6 +2,9 @@ #include #include #include +#include +#include +#include #include "utils.h" void *calloc_or_fail(size_t size) { @@ -137,3 +140,32 @@ void *memcpy1(void *dst,const void *src,size_t n) { asm volatile("rep movsb" : "+D" (dst) : "c"(n), "S"(src) : "cc","memory"); return(ret); } + +void set_priority(int priority) +{ + /* When running with a realtime scheduler, a buggy run-away process can take + down the host requiring a reboot to recover. To avoid this scenario, we + use alarm(2) to set a maximum running time */ + unsigned max_runtime_seconds = 10 * 60; /* default */ + const char *env = getenv("MAX_RUNTIME_SECONDS"); + if (env != NULL) + { + max_runtime_seconds = atoi(env); + } + unsigned was_alarm = alarm(max_runtime_seconds); + /* Normally, was_alarm should be 0 indicating there was no previous alarm set. + A non-zero value could indicate a mistake */ + fprintf(stderr, "Set alarm for %u seconds, was %u (see $MAX_RUNTIME_SECONDS)\n", + max_runtime_seconds, was_alarm); + + struct sched_param param = + { + .sched_priority = priority, + }; + fprintf(stderr, "Calling sched_setscheduler(%d)\n", priority); + if (sched_setscheduler(0, SCHED_RR, ¶m) == -1) + { + fprintf(stderr, "sched_setscheduler: %s\n", strerror(errno)); + abort(); + } +} diff --git a/common/utils/utils.h b/common/utils/utils.h index 9295ab7b54..d4337e71a6 100644 --- a/common/utils/utils.h +++ b/common/utils/utils.h @@ -19,6 +19,7 @@ int hex_string_to_hex_value (uint8_t *hex_value, const char *hex_string, int siz void *memcpy1(void *dst,const void *src,size_t n); +void set_priority(int priority); char *itoa(int i); diff --git a/executables/nr-uesoftmodem.c b/executables/nr-uesoftmodem.c index cef324db34..0e25d5115f 100644 --- a/executables/nr-uesoftmodem.c +++ b/executables/nr-uesoftmodem.c @@ -415,6 +415,13 @@ void *rrc_enb_process_msg(void *notUsed) { int main( int argc, char **argv ) { + set_priority(79); + if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) + { + fprintf(stderr, "mlockall: %s\n", strerror(errno)); + return EXIT_FAILURE; + } + //uint8_t beta_ACK=0,beta_RI=0,beta_CQI=2; PHY_VARS_NR_UE *UE[MAX_NUM_CCs]; start_background_system(); diff --git a/nfapi/oai_integration/nfapi.c b/nfapi/oai_integration/nfapi.c index 3b14eaabd5..5e71c5e2a4 100644 --- a/nfapi/oai_integration/nfapi.c +++ b/nfapi/oai_integration/nfapi.c @@ -40,15 +40,9 @@ typedef struct { static nfapi_params_t nfapi_params = {0}; void set_thread_priority(int priority) { - //printf("%s(priority:%d)\n", __FUNCTION__, priority); + set_priority(priority); + pthread_attr_t ptAttr; - struct sched_param schedParam; - schedParam.__sched_priority = priority; //79; - - if(sched_setscheduler(0, SCHED_RR, &schedParam) != 0) { - printf("Failed to set scheduler to SCHED_RR\n"); - } - if(pthread_attr_setschedpolicy(&ptAttr, SCHED_RR) != 0) { printf("Failed to set pthread sched policy SCHED_RR\n"); } diff --git a/nfapi/oai_integration/nfapi_pnf.c b/nfapi/oai_integration/nfapi_pnf.c index 1cd33292a6..d1a154cf73 100644 --- a/nfapi/oai_integration/nfapi_pnf.c +++ b/nfapi/oai_integration/nfapi_pnf.c @@ -218,14 +218,9 @@ void pnf_nfapi_trace(nfapi_trace_level_t nfapi_level, const char *message, ...) } void pnf_set_thread_priority(int priority) { + set_priority(priority); + pthread_attr_t ptAttr; - struct sched_param schedParam; - schedParam.__sched_priority = priority; - - if(sched_setscheduler(0, SCHED_RR, &schedParam) != 0) { - printf("failed to set SCHED_RR\n"); - } - if(pthread_attr_setschedpolicy(&ptAttr, SCHED_RR) != 0) { printf("failed to set pthread SCHED_RR %d\n", errno); } diff --git a/targets/RT/USER/lte-softmodem.c b/targets/RT/USER/lte-softmodem.c index 2c84085d37..16c2f80c48 100644 --- a/targets/RT/USER/lte-softmodem.c +++ b/targets/RT/USER/lte-softmodem.c @@ -524,15 +524,7 @@ static void wait_nfapi_init(char *thread_name) { int main ( int argc, char **argv ) { - struct sched_param param = - { - .sched_priority = 79 - }; - if (sched_setscheduler( 0, SCHED_RR, ¶m ) == -1 ) - { - fprintf(stderr, "sched_setscheduler: %s\n", strerror(errno)); - return EXIT_FAILURE; - } + set_priority(79); if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) { fprintf(stderr, "mlockall: %s\n", strerror(errno)); diff --git a/targets/RT/USER/lte-uesoftmodem.c b/targets/RT/USER/lte-uesoftmodem.c index fb16af0fa1..349362b3b2 100644 --- a/targets/RT/USER/lte-uesoftmodem.c +++ b/targets/RT/USER/lte-uesoftmodem.c @@ -547,15 +547,7 @@ AssertFatal(false,""); } int main( int argc, char **argv ) { - struct sched_param param = - { - .sched_priority = 79 - }; - if (sched_setscheduler( 0, SCHED_RR, ¶m ) == -1 ) - { - fprintf(stderr, "sched_setscheduler: %s\n", strerror(errno)); - return EXIT_FAILURE; - } + set_priority(79); if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) { fprintf(stderr, "mlockall: %s\n", strerror(errno));