diff --git a/runtime/include/runtime.h b/runtime/include/runtime.h index 81cfbd2..5e80a0a 100644 --- a/runtime/include/runtime.h +++ b/runtime/include/runtime.h @@ -40,35 +40,26 @@ enum RUNTIME_SIGALRM_HANDLER RUNTIME_SIGALRM_HANDLER_TRIAGED = 1 }; -extern bool runtime_preemption_enabled; - -/* - * Assumption: All cores are the same speed - * See runtime_get_processor_speed_MHz for further details - */ -extern uint32_t runtime_processor_speed_MHz; - -extern uint32_t runtime_quantum_us; - -/* Optional path to a file to log sandbox perf metrics */ -extern FILE *runtime_sandbox_perf_log; - +extern bool runtime_preemption_enabled; +extern uint32_t runtime_processor_speed_MHz; +extern uint32_t runtime_quantum_us; +extern FILE * runtime_sandbox_perf_log; extern enum RUNTIME_SCHEDULER runtime_scheduler; extern enum RUNTIME_SIGALRM_HANDLER runtime_sigalrm_handler; +extern pthread_t runtime_worker_threads[]; +extern uint32_t runtime_worker_threads_count; +extern int runtime_worker_threads_argument[RUNTIME_WORKER_THREAD_CORE_COUNT]; +extern uint64_t runtime_worker_threads_deadline[RUNTIME_WORKER_THREAD_CORE_COUNT]; -/* Count of worker threads and array of their pthread identifiers */ -extern pthread_t runtime_worker_threads[]; -extern uint32_t runtime_worker_threads_count; -extern int runtime_worker_threads_argument[RUNTIME_WORKER_THREAD_CORE_COUNT]; -extern uint64_t runtime_worker_threads_deadline[RUNTIME_WORKER_THREAD_CORE_COUNT]; - +extern void runtime_initialize(void); +extern void runtime_set_pthread_prio(pthread_t thread, unsigned int nice); +extern void runtime_set_resource_limits_to_max(); +/* External Symbols */ extern void alloc_linear_memory(void); extern void expand_memory(void); INLINE char *get_function_from_table(uint32_t idx, uint32_t type_id); INLINE char *get_memory_ptr_for_runtime(uint32_t offset, uint32_t bounds_check); -extern void runtime_initialize(void); -extern void runtime_set_resource_limits_to_max(); extern void stub_init(int32_t offset); static inline char * diff --git a/runtime/src/runtime.c b/runtime/src/runtime.c index a3c2e6f..b03fdb0 100644 --- a/runtime/src/runtime.c +++ b/runtime/src/runtime.c @@ -1,8 +1,14 @@ +#include +#include #include #include +#include +#include #include #include -#include +#include +#include +#include #include "admissions_control.h" #include "arch/context.h" @@ -110,3 +116,77 @@ runtime_initialize(void) http_parser_settings_initialize(); admissions_control_initialize(); } + +static void +runtime_call_getrlimit(int id, char *name) +{ + struct rlimit rl; + + if (getrlimit(id, &rl)) { + perror("getrlimit: "); + exit(-1); + } +} + +static void +runtime_call_setrlimit(int id, rlim_t c, rlim_t m) +{ + struct rlimit rl; + + rl.rlim_cur = c; + rl.rlim_max = m; + if (setrlimit(id, &rl)) { + perror("getrlimit: "); + exit(-1); + } +} + +void +runtime_pthread_prio(pthread_t thread, unsigned int nice) +{ + struct sched_param sp; + int policy; + + call_getrlimit(RLIMIT_CPU, "CPU"); + call_getrlimit(RLIMIT_RTTIME, "RTTIME"); + call_getrlimit(RLIMIT_RTPRIO, "RTPRIO"); + call_setrlimit(RLIMIT_RTPRIO, RLIM_INFINITY, RLIM_INFINITY); + call_getrlimit(RLIMIT_RTPRIO, "RTPRIO"); + call_getrlimit(RLIMIT_NICE, "NICE"); + + if (pthread_getschedparam(thread, &policy, &sp) < 0) { perror("pth_getparam: "); } + sp.sched_priority = sched_get_priority_max(SCHED_RR) - nice; + if (pthread_setschedparam(thread, SCHED_RR, &sp) < 0) { + perror("pth_setparam: "); + exit(-1); + } + if (pthread_getschedparam(thread, &policy, &sp) < 0) { perror("getparam: "); } + assert(sp.sched_priority == sched_get_priority_max(SCHED_RR) - nice); + + return; +} + +void +runtime_set_prio(unsigned int nice) +{ + struct sched_param sp; + + call_getrlimit(RLIMIT_CPU, "CPU"); + call_getrlimit(RLIMIT_RTTIME, "RTTIME"); + call_getrlimit(RLIMIT_RTPRIO, "RTPRIO"); + call_setrlimit(RLIMIT_RTPRIO, RLIM_INFINITY, RLIM_INFINITY); + call_getrlimit(RLIMIT_RTPRIO, "RTPRIO"); + + call_getrlimit(RLIMIT_NICE, "NICE"); + + if (sched_getparam(0, &sp) < 0) { perror("getparam: "); } + sp.sched_priority = sched_get_priority_max(SCHED_RR) - nice; + if (sched_setscheduler(0, SCHED_RR, &sp) < 0) { + perror("setscheduler: "); + exit(-1); + } + if (sched_getparam(0, &sp) < 0) { perror("getparam: "); } + assert(sp.sched_priority == sched_get_priority_max(SCHED_RR) - nice); + + return; +}