From: David Howells <dhowells@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: dhowells@redhat.com, nfsv4@linux-nfs.org, linux-fsdevel@vger.kernel.org
Subject: [PATCH 02/41] Make slow-work thread pool actually dynamic [ver #48]
Date: Fri, 03 Apr 2009 16:54:46 +0100 [thread overview]
Message-ID: <20090403155446.28714.85222.stgit@warthog.procyon.org.uk> (raw)
In-Reply-To: <20090403155436.28714.23368.stgit@warthog.procyon.org.uk>
Make the slow-work thread pool actually dynamic in the number of threads it
contains. With this patch, it will both create additional threads when it has
extra work to do, and cull excess threads that aren't doing anything.
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: Serge Hallyn <serue@us.ibm.com>
Acked-by: Steve Dickson <steved@redhat.com>
Acked-by: Trond Myklebust <Trond.Myklebust@netapp.com>
Acked-by: Al Viro <viro@zeniv.linux.org.uk>
Tested-by: Daire Byrne <Daire.Byrne@framestore.com>
---
kernel/slow-work.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 137 insertions(+), 1 deletions(-)
diff --git a/kernel/slow-work.c b/kernel/slow-work.c
index 5a73927..454abb2 100644
--- a/kernel/slow-work.c
+++ b/kernel/slow-work.c
@@ -16,6 +16,14 @@
#include <linux/wait.h>
#include <asm/system.h>
+#define SLOW_WORK_CULL_TIMEOUT (5 * HZ) /* cull threads 5s after running out of
+ * things to do */
+#define SLOW_WORK_OOM_TIMEOUT (5 * HZ) /* can't start new threads for 5s after
+ * OOM */
+
+static void slow_work_cull_timeout(unsigned long);
+static void slow_work_oom_timeout(unsigned long);
+
/*
* The pool of threads has at least min threads in it as long as someone is
* using the facility, and may have as many as max.
@@ -29,6 +37,12 @@ static unsigned vslow_work_proportion = 50; /* % of threads that may process
static atomic_t slow_work_thread_count;
static atomic_t vslow_work_executing_count;
+static bool slow_work_may_not_start_new_thread;
+static bool slow_work_cull; /* cull a thread due to lack of activity */
+static DEFINE_TIMER(slow_work_cull_timer, slow_work_cull_timeout, 0, 0);
+static DEFINE_TIMER(slow_work_oom_timer, slow_work_oom_timeout, 0, 0);
+static struct slow_work slow_work_new_thread; /* new thread starter */
+
/*
* The queues of work items and the lock governing access to them. These are
* shared between all the CPUs. It doesn't make sense to have per-CPU queues
@@ -89,6 +103,14 @@ static bool slow_work_execute(void)
vsmax = slow_work_calc_vsmax();
+ /* see if we can schedule a new thread to be started if we're not
+ * keeping up with the work */
+ if (!waitqueue_active(&slow_work_thread_wq) &&
+ (!list_empty(&slow_work_queue) || !list_empty(&vslow_work_queue)) &&
+ atomic_read(&slow_work_thread_count) < slow_work_max_threads &&
+ !slow_work_may_not_start_new_thread)
+ slow_work_enqueue(&slow_work_new_thread);
+
/* find something to execute */
spin_lock_irq(&slow_work_queue_lock);
if (!list_empty(&vslow_work_queue) &&
@@ -243,6 +265,33 @@ cant_get_ref:
EXPORT_SYMBOL(slow_work_enqueue);
/*
+ * Worker thread culling algorithm
+ */
+static bool slow_work_cull_thread(void)
+{
+ unsigned long flags;
+ bool do_cull = false;
+
+ spin_lock_irqsave(&slow_work_queue_lock, flags);
+
+ if (slow_work_cull) {
+ slow_work_cull = false;
+
+ if (list_empty(&slow_work_queue) &&
+ list_empty(&vslow_work_queue) &&
+ atomic_read(&slow_work_thread_count) >
+ slow_work_min_threads) {
+ mod_timer(&slow_work_cull_timer,
+ jiffies + SLOW_WORK_CULL_TIMEOUT);
+ do_cull = true;
+ }
+ }
+
+ spin_unlock_irqrestore(&slow_work_queue_lock, flags);
+ return do_cull;
+}
+
+/*
* Determine if there is slow work available for dispatch
*/
static inline bool slow_work_available(int vsmax)
@@ -273,7 +322,8 @@ static int slow_work_thread(void *_data)
TASK_INTERRUPTIBLE);
if (!freezing(current) &&
!slow_work_threads_should_exit &&
- !slow_work_available(vsmax))
+ !slow_work_available(vsmax) &&
+ !slow_work_cull)
schedule();
finish_wait(&slow_work_thread_wq, &wait);
@@ -285,11 +335,20 @@ static int slow_work_thread(void *_data)
if (slow_work_available(vsmax) && slow_work_execute()) {
cond_resched();
+ if (list_empty(&slow_work_queue) &&
+ list_empty(&vslow_work_queue) &&
+ atomic_read(&slow_work_thread_count) >
+ slow_work_min_threads)
+ mod_timer(&slow_work_cull_timer,
+ jiffies + SLOW_WORK_CULL_TIMEOUT);
continue;
}
if (slow_work_threads_should_exit)
break;
+
+ if (slow_work_cull && slow_work_cull_thread())
+ break;
}
if (atomic_dec_and_test(&slow_work_thread_count))
@@ -297,6 +356,77 @@ static int slow_work_thread(void *_data)
return 0;
}
+/*
+ * Handle thread cull timer expiration
+ */
+static void slow_work_cull_timeout(unsigned long data)
+{
+ slow_work_cull = true;
+ wake_up(&slow_work_thread_wq);
+}
+
+/*
+ * Get a reference on slow work thread starter
+ */
+static int slow_work_new_thread_get_ref(struct slow_work *work)
+{
+ return 0;
+}
+
+/*
+ * Drop a reference on slow work thread starter
+ */
+static void slow_work_new_thread_put_ref(struct slow_work *work)
+{
+}
+
+/*
+ * Start a new slow work thread
+ */
+static void slow_work_new_thread_execute(struct slow_work *work)
+{
+ struct task_struct *p;
+
+ if (slow_work_threads_should_exit)
+ return;
+
+ if (atomic_read(&slow_work_thread_count) >= slow_work_max_threads)
+ return;
+
+ if (!mutex_trylock(&slow_work_user_lock))
+ return;
+
+ slow_work_may_not_start_new_thread = true;
+ atomic_inc(&slow_work_thread_count);
+ p = kthread_run(slow_work_thread, NULL, "kslowd");
+ if (IS_ERR(p)) {
+ printk(KERN_DEBUG "Slow work thread pool: OOM\n");
+ if (atomic_dec_and_test(&slow_work_thread_count))
+ BUG(); /* we're running on a slow work thread... */
+ mod_timer(&slow_work_oom_timer,
+ jiffies + SLOW_WORK_OOM_TIMEOUT);
+ } else {
+ /* ratelimit the starting of new threads */
+ mod_timer(&slow_work_oom_timer, jiffies + 1);
+ }
+
+ mutex_unlock(&slow_work_user_lock);
+}
+
+static const struct slow_work_ops slow_work_new_thread_ops = {
+ .get_ref = slow_work_new_thread_get_ref,
+ .put_ref = slow_work_new_thread_put_ref,
+ .execute = slow_work_new_thread_execute,
+};
+
+/*
+ * post-OOM new thread start suppression expiration
+ */
+static void slow_work_oom_timeout(unsigned long data)
+{
+ slow_work_may_not_start_new_thread = false;
+}
+
/**
* slow_work_register_user - Register a user of the facility
*
@@ -316,6 +446,10 @@ int slow_work_register_user(void)
init_completion(&slow_work_last_thread_exited);
slow_work_threads_should_exit = false;
+ slow_work_init(&slow_work_new_thread,
+ &slow_work_new_thread_ops);
+ slow_work_may_not_start_new_thread = false;
+ slow_work_cull = false;
/* start the minimum number of threads */
for (loop = 0; loop < slow_work_min_threads; loop++) {
@@ -369,6 +503,8 @@ void slow_work_unregister_user(void)
" Shut down complete\n");
}
+ del_timer_sync(&slow_work_cull_timer);
+
mutex_unlock(&slow_work_user_lock);
}
EXPORT_SYMBOL(slow_work_unregister_user);
next prev parent reply other threads:[~2009-04-03 15:54 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-04-03 15:54 [PATCH 00/41] Permit filesystem local caching [ver #48] David Howells
2009-04-03 15:54 ` [PATCH 01/41] Create a dynamically sized pool of threads for doing very slow work items " David Howells
2009-04-03 15:54 ` David Howells [this message]
2009-04-03 15:54 ` [PATCH 03/41] Make the slow work pool configurable " David Howells
2009-04-03 15:54 ` [PATCH 04/41] Document the slow work thread pool " David Howells
2009-04-03 15:55 ` [PATCH 05/41] FS-Cache: Release page->private after failed readahead " David Howells
2009-04-04 6:00 ` Nick Piggin
2009-04-03 15:55 ` [PATCH 06/41] FS-Cache: Recruit a page flags for cache management " David Howells
2009-04-04 6:04 ` Nick Piggin
2009-04-03 15:55 ` [PATCH 07/41] FS-Cache: Add the FS-Cache netfs API and documentation " David Howells
2009-04-03 15:55 ` [PATCH 08/41] FS-Cache: Add the FS-Cache cache backend " David Howells
2009-04-03 15:55 ` [PATCH 09/41] FS-Cache: Add main configuration option, module entry points and debugging " David Howells
2009-04-03 15:55 ` [PATCH 10/41] FS-Cache: Add use of /proc and presentation of statistics " David Howells
2009-04-04 17:39 ` Christoph Hellwig
2009-04-03 15:55 ` [PATCH 11/41] FS-Cache: Root index definition " David Howells
2009-04-03 15:55 ` [PATCH 12/41] FS-Cache: Add cache tag handling " David Howells
2009-04-03 15:55 ` [PATCH 13/41] FS-Cache: Add cache management " David Howells
2009-04-03 15:55 ` [PATCH 14/41] FS-Cache: Provide a slab for cookie allocation " David Howells
2009-04-03 15:55 ` [PATCH 15/41] FS-Cache: Add netfs registration " David Howells
2009-04-03 15:55 ` [PATCH 16/41] FS-Cache: Bit waiting helpers " David Howells
2009-04-03 15:56 ` [PATCH 17/41] FS-Cache: Object management state machine " David Howells
2009-04-03 15:56 ` [PATCH 18/41] FS-Cache: Implement the cookie management part of the netfs API " David Howells
2009-04-03 15:56 ` [PATCH 19/41] FS-Cache: Add and document asynchronous operation handling " David Howells
2009-04-03 15:56 ` [PATCH 20/41] FS-Cache: Implement data I/O part of netfs API " David Howells
2009-04-03 15:56 ` [PATCH 21/41] CacheFiles: Permit the page lock state to be monitored " David Howells
2009-04-04 6:09 ` Nick Piggin
2009-04-04 14:22 ` Nick Piggin
2009-04-04 22:13 ` David Howells
2009-04-06 8:31 ` Nick Piggin
2009-04-04 11:31 ` David Howells
2009-04-06 9:34 ` Nick Piggin
2009-04-03 15:56 ` [PATCH 22/41] CacheFiles: Export things for CacheFiles " David Howells
2009-04-03 15:56 ` [PATCH 23/41] CacheFiles: A cache that backs onto a mounted filesystem " David Howells
2009-04-03 15:56 ` [PATCH 24/41] FS-Cache: Make kAFS use FS-Cache " David Howells
2009-04-03 15:56 ` [PATCH 25/41] NFS: Add comment banners to some NFS functions " David Howells
2009-04-03 15:56 ` [PATCH 26/41] NFS: Add FS-Cache option bit and debug bit " David Howells
2009-04-03 15:56 ` [PATCH 27/41] NFS: Permit local filesystem caching to be enabled for NFS " David Howells
2009-04-03 15:57 ` [PATCH 28/41] NFS: Register NFS for caching and retrieve the top-level index " David Howells
2009-04-03 15:57 ` [PATCH 29/41] NFS: Define and create server-level objects " David Howells
2009-04-03 15:57 ` [PATCH 30/41] NFS: Define and create superblock-level " David Howells
2009-04-03 15:57 ` [PATCH 31/41] NFS: Define and create inode-level cache " David Howells
2009-04-03 15:57 ` [PATCH 32/41] NFS: Use local disk inode cache " David Howells
2009-04-03 15:57 ` [PATCH 33/41] NFS: Invalidate FsCache page flags when cache removed " David Howells
2009-04-03 15:57 ` [PATCH 34/41] NFS: Add some new I/O counters for FS-Cache doing things for NFS " David Howells
2009-04-03 15:57 ` [PATCH 35/41] NFS: FS-Cache page management " David Howells
2009-04-03 15:57 ` [PATCH 36/41] NFS: Add read context retention for FS-Cache to call back with " David Howells
2009-04-03 15:57 ` [PATCH 37/41] NFS: nfs_readpage_async() needs to be accessible as a fallback for local caching " David Howells
2009-04-03 15:57 ` [PATCH 38/41] NFS: Read pages from FS-Cache into an NFS inode " David Howells
2009-04-03 15:57 ` [PATCH 39/41] NFS: Store pages from an NFS inode into a local cache " David Howells
2009-04-03 15:58 ` [PATCH 40/41] NFS: Display local caching state " David Howells
2009-04-03 15:58 ` [PATCH 41/41] NFS: Add mount options to enable local caching on NFS " David Howells
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20090403155446.28714.85222.stgit@warthog.procyon.org.uk \
--to=dhowells@redhat.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nfsv4@linux-nfs.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).