Commit e07f7183 authored by Josef Bacik's avatar Josef Bacik Committed by Theodore Ts'o

jbd2: improve jbd2 fsync batching

This patch removes the static sleep time in favor of a more self
optimizing approach where we measure the average amount of time it
takes to commit a transaction to disk and the ammount of time a
transaction has been running.  If somebody does a sync write or an
fsync() traditionally we would sleep for 1 jiffies, which depending on
the value of HZ could be a significant amount of time compared to how
long it takes to commit a transaction to the underlying storage.  With
this patch instead of sleeping for a jiffie, we check to see if the
amount of time this transaction has been running is less than the
average commit time, and if it is we sleep for the delta using
schedule_hrtimeout to give us a higher precision sleep time.  This
greatly benefits high end storage where you could end up sleeping for
longer than it takes to commit the transaction and therefore sitting
idle instead of allowing the transaction to be committed by keeping
the sleep time to a minimum so you are sure to always be doing
something.
Signed-off-by: default avatarJosef Bacik <jbacik@redhat.com>
Signed-off-by: default avatar"Theodore Ts'o" <tytso@mit.edu>
parent 032115fc
...@@ -355,6 +355,8 @@ void jbd2_journal_commit_transaction(journal_t *journal) ...@@ -355,6 +355,8 @@ void jbd2_journal_commit_transaction(journal_t *journal)
int flags; int flags;
int err; int err;
unsigned long long blocknr; unsigned long long blocknr;
ktime_t start_time;
u64 commit_time;
char *tagp = NULL; char *tagp = NULL;
journal_header_t *header; journal_header_t *header;
journal_block_tag_t *tag = NULL; journal_block_tag_t *tag = NULL;
...@@ -481,6 +483,7 @@ void jbd2_journal_commit_transaction(journal_t *journal) ...@@ -481,6 +483,7 @@ void jbd2_journal_commit_transaction(journal_t *journal)
commit_transaction->t_state = T_FLUSH; commit_transaction->t_state = T_FLUSH;
journal->j_committing_transaction = commit_transaction; journal->j_committing_transaction = commit_transaction;
journal->j_running_transaction = NULL; journal->j_running_transaction = NULL;
start_time = ktime_get();
commit_transaction->t_log_start = journal->j_head; commit_transaction->t_log_start = journal->j_head;
wake_up(&journal->j_wait_transaction_locked); wake_up(&journal->j_wait_transaction_locked);
spin_unlock(&journal->j_state_lock); spin_unlock(&journal->j_state_lock);
...@@ -995,6 +998,17 @@ restart_loop: ...@@ -995,6 +998,17 @@ restart_loop:
J_ASSERT(commit_transaction == journal->j_committing_transaction); J_ASSERT(commit_transaction == journal->j_committing_transaction);
journal->j_commit_sequence = commit_transaction->t_tid; journal->j_commit_sequence = commit_transaction->t_tid;
journal->j_committing_transaction = NULL; journal->j_committing_transaction = NULL;
commit_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
/*
* weight the commit time higher than the average time so we don't
* react too strongly to vast changes in the commit time
*/
if (likely(journal->j_average_commit_time))
journal->j_average_commit_time = (commit_time +
journal->j_average_commit_time*3) / 4;
else
journal->j_average_commit_time = commit_time;
spin_unlock(&journal->j_state_lock); spin_unlock(&journal->j_state_lock);
if (journal->j_commit_callback) if (journal->j_commit_callback)
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <linux/timer.h> #include <linux/timer.h>
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/highmem.h> #include <linux/highmem.h>
#include <linux/hrtimer.h>
static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh); static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh);
...@@ -48,6 +49,7 @@ jbd2_get_transaction(journal_t *journal, transaction_t *transaction) ...@@ -48,6 +49,7 @@ jbd2_get_transaction(journal_t *journal, transaction_t *transaction)
{ {
transaction->t_journal = journal; transaction->t_journal = journal;
transaction->t_state = T_RUNNING; transaction->t_state = T_RUNNING;
transaction->t_start_time = ktime_get();
transaction->t_tid = journal->j_transaction_sequence++; transaction->t_tid = journal->j_transaction_sequence++;
transaction->t_expires = jiffies + journal->j_commit_interval; transaction->t_expires = jiffies + journal->j_commit_interval;
spin_lock_init(&transaction->t_handle_lock); spin_lock_init(&transaction->t_handle_lock);
...@@ -1193,7 +1195,7 @@ int jbd2_journal_stop(handle_t *handle) ...@@ -1193,7 +1195,7 @@ int jbd2_journal_stop(handle_t *handle)
{ {
transaction_t *transaction = handle->h_transaction; transaction_t *transaction = handle->h_transaction;
journal_t *journal = transaction->t_journal; journal_t *journal = transaction->t_journal;
int old_handle_count, err; int err;
pid_t pid; pid_t pid;
J_ASSERT(journal_current_handle() == handle); J_ASSERT(journal_current_handle() == handle);
...@@ -1216,24 +1218,52 @@ int jbd2_journal_stop(handle_t *handle) ...@@ -1216,24 +1218,52 @@ int jbd2_journal_stop(handle_t *handle)
/* /*
* Implement synchronous transaction batching. If the handle * Implement synchronous transaction batching. If the handle
* was synchronous, don't force a commit immediately. Let's * was synchronous, don't force a commit immediately. Let's
* yield and let another thread piggyback onto this transaction. * yield and let another thread piggyback onto this
* Keep doing that while new threads continue to arrive. * transaction. Keep doing that while new threads continue to
* It doesn't cost much - we're about to run a commit and sleep * arrive. It doesn't cost much - we're about to run a commit
* on IO anyway. Speeds up many-threaded, many-dir operations * and sleep on IO anyway. Speeds up many-threaded, many-dir
* by 30x or more... * operations by 30x or more...
* *
* But don't do this if this process was the most recent one to * We try and optimize the sleep time against what the
* perform a synchronous write. We do this to detect the case where a * underlying disk can do, instead of having a static sleep
* single process is doing a stream of sync writes. No point in waiting * time. This is useful for the case where our storage is so
* for joiners in that case. * fast that it is more optimal to go ahead and force a flush
* and wait for the transaction to be committed than it is to
* wait for an arbitrary amount of time for new writers to
* join the transaction. We achieve this by measuring how
* long it takes to commit a transaction, and compare it with
* how long this transaction has been running, and if run time
* < commit time then we sleep for the delta and commit. This
* greatly helps super fast disks that would see slowdowns as
* more threads started doing fsyncs.
*
* But don't do this if this process was the most recent one
* to perform a synchronous write. We do this to detect the
* case where a single process is doing a stream of sync
* writes. No point in waiting for joiners in that case.
*/ */
pid = current->pid; pid = current->pid;
if (handle->h_sync && journal->j_last_sync_writer != pid) { if (handle->h_sync && journal->j_last_sync_writer != pid) {
u64 commit_time, trans_time;
journal->j_last_sync_writer = pid; journal->j_last_sync_writer = pid;
do {
old_handle_count = transaction->t_handle_count; spin_lock(&journal->j_state_lock);
schedule_timeout_uninterruptible(1); commit_time = journal->j_average_commit_time;
} while (old_handle_count != transaction->t_handle_count); spin_unlock(&journal->j_state_lock);
trans_time = ktime_to_ns(ktime_sub(ktime_get(),
transaction->t_start_time));
commit_time = min_t(u64, commit_time,
1000*jiffies_to_usecs(1));
if (trans_time < commit_time) {
ktime_t expires = ktime_add_ns(ktime_get(),
commit_time);
set_current_state(TASK_UNINTERRUPTIBLE);
schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
}
} }
current->journal_info = NULL; current->journal_info = NULL;
......
...@@ -637,6 +637,11 @@ struct transaction_s ...@@ -637,6 +637,11 @@ struct transaction_s
*/ */
unsigned long t_expires; unsigned long t_expires;
/*
* When this transaction started, in nanoseconds [no locking]
*/
ktime_t t_start_time;
/* /*
* How many handles used this transaction? [t_handle_lock] * How many handles used this transaction? [t_handle_lock]
*/ */
...@@ -939,8 +944,18 @@ struct journal_s ...@@ -939,8 +944,18 @@ struct journal_s
struct buffer_head **j_wbuf; struct buffer_head **j_wbuf;
int j_wbufsize; int j_wbufsize;
/*
* this is the pid of hte last person to run a synchronous operation
* through the journal
*/
pid_t j_last_sync_writer; pid_t j_last_sync_writer;
/*
* the average amount of time in nanoseconds it takes to commit a
* transaction to disk. [j_state_lock]
*/
u64 j_average_commit_time;
/* This function is called when a transaction is closed */ /* This function is called when a transaction is closed */
void (*j_commit_callback)(journal_t *, void (*j_commit_callback)(journal_t *,
transaction_t *); transaction_t *);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment