From 85d2d4dd26c88b2f49efb2c7d440ee83f466c448 Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Thu, 18 Mar 2021 12:00:36 +0100 Subject: [PATCH] Add an experimental --scheduling-policy option Only expected to work on FreeBSD-based systems. While it was worth the try, the benchmark results at: https://www.fabiankeil.de/gehacktes/privoxy-tls-benchmarks/#2021-03-18-siege-scheduling-policy seem to suggest that changing the scheduling policy doesn't actually improve things ... --- jcc.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/jcc.c b/jcc.c index 3cc1b3f2..3c8fcadf 100644 --- a/jcc.c +++ b/jcc.c @@ -43,6 +43,9 @@ #include #include #include +#include +#include +#include #ifdef _WIN32 # ifndef FEATURE_PTHREAD @@ -5304,6 +5307,43 @@ static void initialize_mutexes(void) #endif /* def MUTEX_LOCKS_AVAILABLE */ } +static void set_scheduling_policy(int new_scheduling_policy) +{ + struct sched_param sched_param; + int current_scheduling_policy = sched_getscheduler(0); + + if (current_scheduling_policy == -1) + { + log_error(LOG_LEVEL_ERROR, "Failed to get the current " + "scheduling policy. Not setting new policy: %E"); + } + else if (current_scheduling_policy != new_scheduling_policy) + { + log_error(LOG_LEVEL_INFO, + "Changing the scheduling policy from %d to %d", + current_scheduling_policy, new_scheduling_policy); + errno = 0; + sched_param.sched_priority = getpriority(PRIO_PROCESS, 0); + if (sched_param.sched_priority == -1 && errno != 0) + { + log_error(LOG_LEVEL_ERROR, "Failed to get the process priority. " + "Can't set new scheduling policy: %E"); + } + else if (sched_setscheduler(0, new_scheduling_policy, &sched_param) != 0) + { + log_error(LOG_LEVEL_ERROR, + "Failed to set the scheduling policy from %d to %d: %E", + current_scheduling_policy, new_scheduling_policy); + } + } + else + { + log_error(LOG_LEVEL_INFO, "Scheduling policy %d is already set", + new_scheduling_policy); + } +} + + /********************************************************************* * * Function : main @@ -5421,6 +5461,14 @@ int main(int argc, char **argv) daemon_mode = 0; } + else if (strcmp(argv[argc_pos], "--scheduling-policy") == 0) + { + int scheduling_policy; + if (++argc_pos == argc) usage(argv[0]); + scheduling_policy = (int)strtol(argv[argc_pos], (char **)NULL, 10); + set_scheduling_policy(scheduling_policy); + } + else if (strcmp(argv[argc_pos], "--pidfile") == 0) { if (++argc_pos == argc) usage(argv[0]); -- 2.30.0