Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use SCHED_BATCH scheduling priority for indexing threads #537

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/messages/initialize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/platform.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ void freeUnusedMemory();
void traceMe();

void spawnThread(void *(*fn)(void *), void *arg);

void setBatchPriority();
} // namespace ccls
22 changes: 22 additions & 0 deletions src/platform_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#if defined(__unix__) || defined(__APPLE__)
#include "platform.hh"

#include "log.hh"
#include "utils.hh"

#include <assert.h>
Expand All @@ -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>
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/platform_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ void traceMe() {}
void spawnThread(void *(*fn)(void *), void *arg) {
std::thread(fn, arg).detach();
}

void setBatchPriority() {}
} // namespace ccls

#endif