From 416670671f01abeebb807b043529223f04744f86 Mon Sep 17 00:00:00 2001 From: David Sansome Date: Sun, 21 Mar 2010 15:55:58 +0000 Subject: [PATCH] Run the background library scanner at IDLE io priority on linux --- src/backgroundthread.h | 54 +++++++++++++++++++++++++++++++++++++++++- src/library.cpp | 4 +++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/backgroundthread.h b/src/backgroundthread.h index 81dccbf68..04590b4d7 100644 --- a/src/backgroundthread.h +++ b/src/backgroundthread.h @@ -6,13 +6,42 @@ #include +#ifdef Q_OS_LINUX +# include +#endif + class BackgroundThreadBase : public QThread { Q_OBJECT public: - BackgroundThreadBase(QObject* parent = 0) : QThread(parent) {} + BackgroundThreadBase(QObject* parent = 0) : QThread(parent), io_priority_(IOPRIO_CLASS_NONE) {} virtual ~BackgroundThreadBase() {} + + // Borrowed from schedutils + enum IoPriority { + IOPRIO_CLASS_NONE = 0, + IOPRIO_CLASS_RT, + IOPRIO_CLASS_BE, + IOPRIO_CLASS_IDLE, + }; + + void set_io_priority(IoPriority priority) { io_priority_ = priority; } + signals: void Initialised(); + + protected: + // Borrowed from schedutils + static inline int ioprio_set(int which, int who, int ioprio); + static inline int gettid(); + + enum { + IOPRIO_WHO_PROCESS = 1, + IOPRIO_WHO_PGRP, + IOPRIO_WHO_USER, + }; + static const int IOPRIO_CLASS_SHIFT = 13; + + IoPriority io_priority_; }; template @@ -49,6 +78,13 @@ BackgroundThread::~BackgroundThread() { template void BackgroundThread::run() { +#ifdef Q_OS_LINUX + if (io_priority_ != IOPRIO_CLASS_NONE) { + ioprio_set(IOPRIO_WHO_PROCESS, gettid(), + 4 | io_priority_ << IOPRIO_CLASS_SHIFT); + } +#endif + worker_.reset(new T); emit Initialised(); @@ -57,4 +93,20 @@ void BackgroundThread::run() { worker_.reset(); } +int BackgroundThreadBase::ioprio_set(int which, int who, int ioprio) { +#ifdef Q_OS_LINUX + return syscall(SYS_ioprio_set, which, who, ioprio); +#else + return 0; +#endif +} + +int BackgroundThreadBase::gettid() { +#ifdef Q_OS_LINUX + return syscall(SYS_gettid); +#else + return 0; +#endif +} + #endif // BACKGROUNDTHREAD_H diff --git a/src/library.cpp b/src/library.cpp index ed749454f..7d88286d2 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -35,7 +35,9 @@ void Library::StartThreads() { Q_ASSERT(waiting_for_threads_); backend_->start(); - watcher_->start(); + + watcher_->set_io_priority(BackgroundThreadBase::IOPRIO_CLASS_IDLE); + watcher_->start(QThread::IdlePriority); } void Library::BackendInitialised() {