[PATCH] D59130: [llvm][Support] Provide interface to set thread priorities
Kadir Cetinkaya via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 16 07:32:55 PDT 2019
This revision was automatically updated to reflect the committed changes.
Closed by commit rL358494: [llvm][Support] Provide interface to set thread priorities (authored by kadircet, committed by ).
Herald added a subscriber: dexonsmith.
Repository:
rL LLVM
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D59130/new/
https://reviews.llvm.org/D59130
Files:
llvm/trunk/include/llvm/Support/Threading.h
llvm/trunk/lib/Support/Unix/Threading.inc
llvm/trunk/lib/Support/Windows/Threading.inc
Index: llvm/trunk/include/llvm/Support/Threading.h
===================================================================
--- llvm/trunk/include/llvm/Support/Threading.h
+++ llvm/trunk/include/llvm/Support/Threading.h
@@ -167,6 +167,19 @@
/// purposes, and as with setting a thread's name no indication of whether
/// the operation succeeded or failed is returned.
void get_thread_name(SmallVectorImpl<char> &Name);
+
+ enum class ThreadPriority {
+ Background = 0,
+ Default = 1,
+ };
+ /// If priority is Background tries to lower current threads priority such
+ /// that it does not affect foreground tasks significantly. Can be used for
+ /// long-running, latency-insensitive tasks to make sure cpu is not hogged by
+ /// this task.
+ /// If the priority is default tries to restore current threads priority to
+ /// default scheduling priority.
+ enum class SetThreadPriorityResult { FAILURE, SUCCESS };
+ SetThreadPriorityResult set_thread_priority(ThreadPriority Priority);
}
#endif
Index: llvm/trunk/lib/Support/Unix/Threading.inc
===================================================================
--- llvm/trunk/lib/Support/Unix/Threading.inc
+++ llvm/trunk/lib/Support/Unix/Threading.inc
@@ -217,3 +217,42 @@
#endif
#endif
}
+
+SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) {
+#if defined(__linux__) && defined(SCHED_IDLE)
+ // Some *really* old glibcs are missing SCHED_IDLE.
+ // http://man7.org/linux/man-pages/man3/pthread_setschedparam.3.html
+ // http://man7.org/linux/man-pages/man2/sched_setscheduler.2.html
+ sched_param priority;
+ // For each of the above policies, param->sched_priority must be 0.
+ priority.sched_priority = 0;
+ // SCHED_IDLE for running very low priority background jobs.
+ // SCHED_OTHER the standard round-robin time-sharing policy;
+ return !pthread_setschedparam(
+ pthread_self(),
+ Priority == ThreadPriority::Background ? SCHED_IDLE : SCHED_OTHER,
+ &priority)
+ ? SetThreadPriorityResult::SUCCESS
+ : SetThreadPriorityResult::FAILURE;
+#elif defined(__APPLE__)
+ // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/getpriority.2.html
+ // When setting a thread into background state the scheduling priority is set
+ // to lowest value, disk and network IO are throttled. Network IO will be
+ // throttled for any sockets the thread opens after going into background
+ // state. Any previously opened sockets are not affected.
+
+ // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/getiopolicy_np.3.html
+ // I/Os with THROTTLE policy are called THROTTLE I/Os. If a THROTTLE I/O
+ // request occurs within a small time window (usually a fraction of a second)
+ // of another NORMAL I/O request, the thread that issues the THROTTLE I/O is
+ // forced to sleep for a certain interval. This slows down the thread that
+ // issues the THROTTLE I/O so that NORMAL I/Os can utilize most of the disk
+ // I/O bandwidth.
+ return !setpriority(PRIO_DARWIN_THREAD, 0,
+ Priority == ThreadPriority::Background ? PRIO_DARWIN_BG
+ : 0)
+ ? SetThreadPriorityResult::SUCCESS
+ : SetThreadPriorityResult::FAILURE;
+#endif
+ return SetThreadPriorityResult::FAILURE;
+}
Index: llvm/trunk/lib/Support/Windows/Threading.inc
===================================================================
--- llvm/trunk/lib/Support/Windows/Threading.inc
+++ llvm/trunk/lib/Support/Windows/Threading.inc
@@ -106,3 +106,19 @@
// value.
Name.clear();
}
+
+SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) {
+ // https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-setthreadpriority
+ // Begin background processing mode. The system lowers the resource scheduling
+ // priorities of the thread so that it can perform background work without
+ // significantly affecting activity in the foreground.
+ // End background processing mode. The system restores the resource scheduling
+ // priorities of the thread as they were before the thread entered background
+ // processing mode.
+ return SetThreadPriority(GetCurrentThread(),
+ Priority == ThreadPriority::Background
+ ? THREAD_MODE_BACKGROUND_BEGIN
+ : THREAD_MODE_BACKGROUND_END)
+ ? SetThreadPriorityResult::SUCCESS
+ : SetThreadPriorityResult::FAILURE;
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59130.195377.patch
Type: text/x-patch
Size: 4669 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190416/3a7b3b89/attachment.bin>
More information about the llvm-commits
mailing list