From c3fe0f4000139c6e46e4dbea1346c0e3855e17ac Mon Sep 17 00:00:00 2001 From: Evan Klitzke Date: Mon, 2 Dec 2019 18:18:17 -0800 Subject: [PATCH] use SCHED_BATCH scheduling priority for indexing threads --- src/messages/initialize.cc | 1 + src/platform.hh | 2 ++ src/platform_posix.cc | 22 ++++++++++++++++++++++ src/platform_win.cc | 2 ++ 4 files changed, 27 insertions(+) diff --git a/src/messages/initialize.cc b/src/messages/initialize.cc index b015d98d..c3380642 100644 --- a/src/messages/initialize.cc +++ b/src/messages/initialize.cc @@ -255,6 +255,7 @@ void *indexer(void *arg_) { delete arg; std::string name = "indexer" + std::to_string(idx); set_thread_name(name.c_str()); + setBatchPriority(); pipeline::indexer_Main(h->manager, h->vfs, h->project, h->wfiles); pipeline::threadLeave(); return nullptr; diff --git a/src/platform.hh b/src/platform.hh index 5fa7cfe7..dd81ae60 100644 --- a/src/platform.hh +++ b/src/platform.hh @@ -17,4 +17,6 @@ void freeUnusedMemory(); void traceMe(); void spawnThread(void *(*fn)(void *), void *arg); + +void setBatchPriority(); } // namespace ccls diff --git a/src/platform_posix.cc b/src/platform_posix.cc index 6fd04995..c8556882 100644 --- a/src/platform_posix.cc +++ b/src/platform_posix.cc @@ -4,6 +4,7 @@ #if defined(__unix__) || defined(__APPLE__) #include "platform.hh" +#include "log.hh" #include "utils.hh" #include @@ -17,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -75,6 +77,26 @@ void spawnThread(void *(*fn)(void *), void *arg) { pthread_create(&thd, &attr, fn, arg); pthread_attr_destroy(&attr); } + +void setBatchPriority() { +#ifdef SCHED_BATCH + pid_t this_thread_id = gettid(); + + // get a sched_param with the existing thread sched_priority + errno = 0; + struct sched_param p; + p.sched_priority = getpriority(PRIO_PROCESS, this_thread_id); + if (p.sched_priority == -1 && errno != 0) { + LOG_S(ERROR) << "failed to getpriority(): " << strerror(errno); + return; + } + + // retain the existing sched_priority, but use SCHED_BATCH policy + if (sched_setscheduler(this_thread_id, SCHED_BATCH, &p) == -1) { + LOG_S(ERROR) << "failed to sched_setscheduler(): " << strerror(errno); + } +} +#endif } // namespace ccls #endif diff --git a/src/platform_win.cc b/src/platform_win.cc index 9fa28e3b..29ba5f9a 100644 --- a/src/platform_win.cc +++ b/src/platform_win.cc @@ -48,6 +48,8 @@ void traceMe() {} void spawnThread(void *(*fn)(void *), void *arg) { std::thread(fn, arg).detach(); } + +void setBatchPriority() {} } // namespace ccls #endif