From c5eafd8a7a09e97bf73e6cef8ebe49107dc49a17 Mon Sep 17 00:00:00 2001 From: lucas Date: Thu, 9 Sep 2010 12:45:39 +0000 Subject: [PATCH] Fix priority dropping Lowering priority was wrong. Some bugs: 1) You don't lower the priority by setting the scheduler policy to some of the real-time ones (SCHED_RR or SCHER_FIFO). If you do so, you are actually increasing the priority of the workers and your main thread you be preempted and stalled until the workers complete their job. Fortunately this will only happen if your programming is running as root, as normal users (without CAP_SYS_NICE) are unable to set priority to real-time values. 2) setpriority() and getpriority() are not part of pthread and you can't use the id returned by pthread. Manpage explicitly says so on pthread_self(3): "The thread ID returned by pthread_self() is not the same thing as the kernel thread ID returned by a call to gettid(2)." Since glibc does not have a gettid, here we are using syscall(SYS_gettid) This patch was tested with the program below. Compile and run: $ gcc p_hello2.c -o p_hello2 -lpthread $ ./p_hello2 10 You'll see that the main thread remains with its priority and threads created by the main thread change their own niceness. #include #include #include #include #include #include #include #include #include /* Lower priority of current thread. * * It's used by worker threads so they use up "bg cpu" as it was really intended * to work. If current thread is running with real-time priority, we decrease * our priority by 5. This is done in a portable way. Otherwise we are * running with SCHED_OTHER policy and there's no portable way to set the nice * level on current thread. In Linux, it does work and it's the only one that is * implemented. */ static void _ecore_thread_pri_drop(void) { struct sched_param param; int pol, prio, ret; pid_t tid; pthread_t pthread_id; pthread_id = pthread_self(); ret = pthread_getschedparam(pthread_id, &pol, ¶m); if (ret) { fprintf(stderr, "Unable to query sched parameters\n"); return; } if (pol == SCHED_RR || pol == SCHED_FIFO) { prio = sched_get_priority_max(pol); param.sched_priority += 5; if (prio > 0 && param.sched_priority > prio) param.sched_priority = prio; pthread_setschedparam(pthread_id, pol, ¶m); } #ifdef __linux__ else { tid = syscall(SYS_gettid); errno = 0; prio = getpriority(PRIO_PROCESS, tid); if (errno == 0) { prio += 5; if (prio > 19) prio = 19; setpriority(PRIO_PROCESS, tid, prio); } } #endif } /* * p_hello.c -- a hello program (in pthread) */ #define MAX_THREAD 1000 typedef struct { int id; } parm; void *hello(void *arg) { parm *p=(parm *)arg; pid_t tid; int prio; tid = syscall(SYS_gettid); printf("[%d] Hello from node %d\n", tid, p->id); pthread_yield(); printf("[%d] HELLO!\n", tid); _ecore_thread_pri_drop(); prio = getpriority(PRIO_PROCESS, tid); printf("[%d] New nice value: %d\n", tid, prio); return (NULL); } int main(int argc, char* argv[]) { int n,i; pthread_t *threads; pthread_attr_t pthread_custom_attr; parm *p; pid_t tid; int prio; if (argc != 2) { printf ("Usage: %s n\n where n is no. of threads\n",argv[0]); exit(1); } n=atoi(argv[1]); if ((n < 1) || (n > MAX_THREAD)) { printf ("The no of thread should between 1 and %d.\n",MAX_THREAD); exit(1); } threads = (pthread_t *)malloc(n * sizeof(*threads)); pthread_attr_init(&pthread_custom_attr); p = (parm *)malloc(n * sizeof(parm)); /* Start up thread */ tid = syscall(SYS_gettid); for (i=0; i +# include +# ifdef __linux__ +# include +# endif #endif #include "evas_common.h" @@ -64,12 +68,65 @@ _evas_preload_thread_done(void *target __UNUSED__, Evas_Callback_Type type __UNU free(work); } +/* Lower priority of current thread. + * + * It's used by worker threads so they use up "bg cpu" as it was really intended + * to work. If current thread is running with real-time priority, we decrease + * our priority by 5. This is done in a portable way. Otherwise we are + * running with SCHED_OTHER policy and there's no portable way to set the nice + * level on current thread. In Linux, it does work and it's the only one that is + * implemented. + */ +static void +_evas_preload_thread_pri_drop(void) +{ + struct sched_param param; + int pol, prio, ret; + pid_t tid; + pthread_t pthread_id; + + pthread_id = pthread_self(); + ret = pthread_getschedparam(pthread_id, &pol, ¶m); + if (ret) + { + ERR("Unable to query sched parameters"); + return; + } + + if (EINA_UNLIKELY(pol == SCHED_RR || pol == SCHED_FIFO)) + { + prio = sched_get_priority_max(pol); + param.sched_priority += 5; + if (prio > 0 && param.sched_priority > prio) + param.sched_priority = prio; + + pthread_setschedparam(pthread_id, pol, ¶m); + } +#ifdef __linux__ + else + { + tid = syscall(SYS_gettid); + errno = 0; + prio = getpriority(PRIO_PROCESS, tid); + if (errno == 0) + { + prio += 5; + if (prio > 19) + prio = 19; + + setpriority(PRIO_PROCESS, tid, prio); + } + } +#endif +} + static void * _evas_preload_thread_worker(void *data) { Evas_Preload_Pthread_Data *pth = data; Evas_Preload_Pthread_Worker *work; + _evas_preload_thread_pri_drop(); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); on_error: @@ -179,31 +236,6 @@ evas_preload_thread_run(void (*func_heavy) (void *data), if (pthread_create(&pth->thread, NULL, _evas_preload_thread_worker, pth) == 0) { -#ifdef __linux__ - struct sched_param param; - - /* lower priority of async loader threads so they use up "bg cpu" - * as it was really intended to work. - * yes - this may fail if not root. there is no portable way to do - * this so try - if it fails. meh. nothnig to be done. - */ - memset(¶m, 0, sizeof(param)); - param.sched_priority = sched_get_priority_max(SCHED_RR); - if (pthread_setschedparam(pth->thread, SCHED_RR, ¶m) != 0) - { - int newp; - - errno = 0; - newp = getpriority(PRIO_PROCESS, 0); - if (errno == 0) - { - newp += 5; - if (newp > 19) newp = 19; - setpriority(PRIO_PROCESS, pth->thread, newp); - } - } -#endif - LKL(_mutex); _threads_count++; LKU(_mutex); -- 2.7.4