use SCHED_BATCH scheduling priority for indexing threads

This commit is contained in:
Evan Klitzke 2019-12-02 18:18:17 -08:00
parent cef0203484
commit c3fe0f4000
No known key found for this signature in database
GPG Key ID: 3279048B58C0BCC2
4 changed files with 27 additions and 0 deletions

View File

@ -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;

View File

@ -17,4 +17,6 @@ void freeUnusedMemory();
void traceMe();
void spawnThread(void *(*fn)(void *), void *arg);
void setBatchPriority();
} // namespace ccls

View File

@ -4,6 +4,7 @@
#if defined(__unix__) || defined(__APPLE__)
#include "platform.hh"
#include "log.hh"
#include "utils.hh"
#include <assert.h>
@ -17,6 +18,7 @@
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <sched.h>
#include <signal.h>
#include <sys/resource.h>
#include <sys/stat.h>
@ -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

View File

@ -48,6 +48,8 @@ void traceMe() {}
void spawnThread(void *(*fn)(void *), void *arg) {
std::thread(fn, arg).detach();
}
void setBatchPriority() {}
} // namespace ccls
#endif