From 1047bc110cfda2f4433d2c099c1fb4bd9ee1a6a2 Mon Sep 17 00:00:00 2001
From: Nick Zavaritsky <mejedi@gmail.com>
Date: Thu, 27 Aug 2015 21:54:59 +0300
Subject: [PATCH] gh-885: Trim thread pool once recovery ends

---
 src/box/box.cc | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/src/box/box.cc b/src/box/box.cc
index 3288794d37..4a8bfd406e 100644
--- a/src/box/box.cc
+++ b/src/box/box.cc
@@ -607,6 +607,25 @@ engine_init()
 	engine_register(sophia);
 }
 
+/**
+ * @brief Reduce the current number of threads in the thread pool to the
+ * bare minimum. Doesn't prevent the pool from spawning new threads later
+ * if demand mounts.
+ */
+static void
+thread_pool_trim()
+{
+	/*
+	 * Trim OpenMP thread pool.
+	 * Though we lack the direct control the workaround below works for
+	 * GNU OpenMP library. The library stops surplus threads on entering
+	 * a parallel region. Can't go below 2 threads due to the
+	 * implementation quirk.
+	 */
+#pragma omp parallel num_threads(2)
+	;
+}
+
 static inline void
 box_init(void)
 {
@@ -677,6 +696,11 @@ box_init(void)
 
 	engine_end_recovery();
 
+	/*
+	 * Recovery inflates the thread pool quite a bit (due to parallel sort).
+	 */
+	thread_pool_trim();
+
 	rmean_cleanup(rmean_box);
 
 	if (recovery_has_replica(recovery))
-- 
GitLab