X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Futil%2Fu_queue.c;h=8f6dc08b3326a9d5f4475c238ccd9f8286fe0e56;hb=e5899c1e8818f7cfdd23c06c504009e5659794b7;hp=81225a80faa46129fe691ee69269503605f05ef4;hpb=9d3fc737afcb98e3882b4d54c9d093980cfb4874;p=mesa.git diff --git a/src/util/u_queue.c b/src/util/u_queue.c index 81225a80faa..8f6dc08b332 100644 --- a/src/util/u_queue.c +++ b/src/util/u_queue.c @@ -33,6 +33,16 @@ #include "util/u_thread.h" #include "u_process.h" +#if defined(__linux__) +#include +#include +#include +#endif + + +/* Define 256MB */ +#define S_256MB (256 * 1024 * 1024) + static void util_queue_kill_threads(struct util_queue *queue, unsigned keep_num_threads, bool finish_locked); @@ -64,7 +74,7 @@ atexit_handler(void) static void global_init(void) { - LIST_INITHEAD(&queue_list); + list_inithead(&queue_list); atexit(atexit_handler); } @@ -74,7 +84,7 @@ add_to_atexit_list(struct util_queue *queue) call_once(&atexit_once_flag, global_init); mtx_lock(&exit_mutex); - LIST_ADD(&queue->head, &queue_list); + list_add(&queue->head, &queue_list); mtx_unlock(&exit_mutex); } @@ -86,7 +96,7 @@ remove_from_atexit_list(struct util_queue *queue) mtx_lock(&exit_mutex); LIST_FOR_EACH_ENTRY_SAFE(iter, tmp, &queue_list, head) { if (iter == queue) { - LIST_DEL(&iter->head); + list_del(&iter->head); break; } } @@ -255,6 +265,13 @@ util_queue_thread_func(void *input) } #endif +#if defined(__linux__) + if (queue->flags & UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY) { + /* The nice() function can only set a maximum of 19. */ + setpriority(PRIO_PROCESS, syscall(SYS_gettid), 19); + } +#endif + if (strlen(queue->name) > 0) { char name[16]; snprintf(name, sizeof(name), "%s%i", queue->name, thread_index); @@ -283,6 +300,8 @@ util_queue_thread_func(void *input) queue->num_queued--; cnd_signal(&queue->has_space_cond); + if (job.job) + queue->total_jobs_size -= job.job_size; mtx_unlock(&queue->lock); if (job.job) { @@ -326,16 +345,17 @@ util_queue_create_thread(struct util_queue *queue, unsigned index) } if (queue->flags & UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY) { -#if defined(__linux__) && defined(SCHED_IDLE) +#if defined(__linux__) && defined(SCHED_BATCH) struct sched_param sched_param = {0}; /* The nice() function can only set a maximum of 19. - * SCHED_IDLE is the same as nice = 20. + * SCHED_BATCH gives the scheduler a hint that this is a latency + * insensitive thread. * * Note that Linux only allows decreasing the priority. The original * priority can't be restored. */ - pthread_setschedparam(queue->threads[index], SCHED_IDLE, &sched_param); + pthread_setschedparam(queue->threads[index], SCHED_BATCH, &sched_param); #endif } return true; @@ -513,7 +533,8 @@ util_queue_add_job(struct util_queue *queue, void *job, struct util_queue_fence *fence, util_queue_execute_func execute, - util_queue_execute_func cleanup) + util_queue_execute_func cleanup, + const size_t job_size) { struct util_queue_job *ptr; @@ -531,7 +552,8 @@ util_queue_add_job(struct util_queue *queue, assert(queue->num_queued >= 0 && queue->num_queued <= queue->max_jobs); if (queue->num_queued == queue->max_jobs) { - if (queue->flags & UTIL_QUEUE_INIT_RESIZE_IF_FULL) { + if (queue->flags & UTIL_QUEUE_INIT_RESIZE_IF_FULL && + queue->total_jobs_size + job_size < S_256MB) { /* If the queue is full, make it larger to avoid waiting for a free * slot. */ @@ -570,7 +592,10 @@ util_queue_add_job(struct util_queue *queue, ptr->fence = fence; ptr->execute = execute; ptr->cleanup = cleanup; + ptr->job_size = job_size; + queue->write_idx = (queue->write_idx + 1) % queue->max_jobs; + queue->total_jobs_size += ptr->job_size; queue->num_queued++; cnd_signal(&queue->has_queued_cond); @@ -637,12 +662,20 @@ util_queue_finish(struct util_queue *queue) * wait for it exclusively. */ mtx_lock(&queue->finish_lock); + + /* The number of threads can be changed to 0, e.g. by the atexit handler. */ + if (!queue->num_threads) { + mtx_unlock(&queue->finish_lock); + return; + } + fences = malloc(queue->num_threads * sizeof(*fences)); util_barrier_init(&barrier, queue->num_threads); for (unsigned i = 0; i < queue->num_threads; ++i) { util_queue_fence_init(&fences[i]); - util_queue_add_job(queue, &barrier, &fences[i], util_queue_finish_execute, NULL); + util_queue_add_job(queue, &barrier, &fences[i], + util_queue_finish_execute, NULL, 0); } for (unsigned i = 0; i < queue->num_threads; ++i) {