mm: Implement balance_dirty_pages() through waiting for flusher thread
Subsystems (6)
Jan Kara
maintainerRFC PATCH 1/1 mm: Implement balance_dirty_pages() through waiting for flusher thread
This patch changes balance_dirty_pages() throttling so that the function does not submit writes on its own but rather waits for flusher thread to do enough writes. This has an advantage that we have a single source of IO allowing for better writeback locality. Also we do not have to reenter filesystems from a non-trivial context.
The waiting is implemented as follows: Each BDI has a FIFO of wait requests. When balance_dirty_pages() finds it needs to throttle the writer, it adds a request for writing write_chunk of pages to the FIFO and waits until the request is fulfilled or we drop below dirty limits. A flusher thread tracks amount of pages it has written and when enough pages are written since the first request in the FIFO became first, it wakes up the waiter and removes request from the FIFO (thus another request becomes first and flusher thread starts writing out on behalf of this request).
--- include/linux/backing-dev.h | 18 +++++ include/linux/writeback.h | 1 + mm/backing-dev.c | 119 +++++++++++++++++++++++++++++++ mm/page-writeback.c | 164 +++++++++++++++---------------------------- 4 files changed, 195 insertions(+), 107 deletions(-)
This is mostly a proof-of-concept patch. It's mostly untested and probably
doesn't completely work. I was just thinking about this for some time already and would like to get some feedback on the approach...
include/linux/backing-dev.h +18 -0
@@ -39,6 +39,7 @@ typedef int (congested_fn)(void *, int);enum bdi_stat_item {BDI_RECLAIMABLE ,BDI_WRITEBACK , +BDI_WRITTEN ,NR_BDI_STAT_ITEMS };
@@ -58,6 +59,11 @@ struct bdi_writeback {struct list_head b_more_io;/* parked for more writeback */ }; +struct bdi_written_count { +struct list_head list; +u64 written; +}; +struct backing_dev_info {struct list_head bdi_list;struct rcu_head rcu_head;
@@ -85,6 +91,16 @@ struct backing_dev_info {unsigned long wb_mask;/* bitmask of registered tasks */ unsigned int wb_cnt;/* number of registered tasks */ +/* +* We abuse wb_written_wait.lock to also guard consistency of +* wb_written_wait together with wb_written_list and wb_written_head +*/ +wait_queue_head_t wb_written_wait;/* waitqueue of waiters for writeout */ +struct list_head wb_written_list;/* list with numbers of writes to +* wait for */ +u64 wb_written_head;/* value of BDI_WRITTEN when we should check +* the first waiter */ +struct list_head work_list;struct device *dev;
@@ -110,6 +126,8 @@ void bdi_start_writeback(struct backing_dev_info *bdi, struct super_block *sb,int bdi_writeback_task (struct bdi_writeback *wb);int bdi_has_dirty_io (struct backing_dev_info *bdi);void bdi_arm_supers_timer (void ); +void bdi_wakeup_writers (struct backing_dev_info *bdi); +void bdi_wait_written (struct backing_dev_info *bdi,long write_chunk);extern spinlock_t bdi_lock;extern struct list_head bdi_list;
include/linux/writeback.h +1 -0
@@ -129,6 +129,7 @@ int dirty_writeback_centisecs_handler(struct ctl_table *, int,void get_dirty_limits (unsigned long *pbackground,unsigned long *pdirty,unsigned long *pbdi_dirty,struct backing_dev_info *bdi); +int dirty_limits_exceeded (struct backing_dev_info *bdi);void page_writeback_init (void ); void balance_dirty_pages_ratelimited_nr(struct address_space *mapping,
mm/backing-dev.c +119 -0
131 lines not on this page · raw message
mm/page-writeback.c +57 -107
198 lines not on this page · raw message
Signature · 1 line
1.6.4.2
Quoted 27 lines · Jan Kara
This patch changes
balance_dirty_pages()throttling so that the function does not submit writes on its own but rather waits for flusher thread to do enough writes. This has an advantage that we have a single source of IO allowing for better writeback locality. Also we do not have to reenter filesystems from a non-trivial context.The waiting is implemented as follows: Each BDI has a FIFO of wait requests. When
balance_dirty_pages()finds it needs to throttle the writer, it adds a request for writingwrite_chunkof pages to the FIFO and waits until the request is fulfilled or we drop below dirty limits. A flusher thread tracks amount of pages it has written and when enough pages are written since the first request in the FIFO became first, it wakes up the waiter and removes request from the FIFO (thus another request becomes first and flusher thread starts writing out on behalf of this request).Signed-off-by Jan Kara <[email protected]>--- include/linux/backing-dev.h | 18 +++++ include/linux/writeback.h | 1 + mm/backing-dev.c | 119 +++++++++++++++++++++++++++++++ mm/page-writeback.c | 164 +++++++++++++++---------------------------- 4 files changed, 195 insertions(+), 107 deletions(-)
Quoted 3 lines
This is mostly a proof-of-concept patch. It's mostly untested and probably
doesn't completely work. I was just thinking about this for some time already and would like to get some feedback on the approach...
It matches the way I'd like to see writeback throttling work. I think this approach is likely to have less jitter and latency because throttling is not tied to a specific set of pages or IOs to complete.
Quoted 18 lines
+++
b/mm/backing-dev.cmm/backing-dev.c +12
-0@@ -390,6 +390,122 @@ static void sync_supers_timer_fn(unsigned long unused)bdi_arm_supers_timer (); } +/* +* Remove writer from the list, update wb_written_head as needed +* +* Needs wb_written_wait.lock held +*/ +static void bdi_remove_writer (struct backing_dev_info *bdi, +struct bdi_written_count *wc) +{ +int first =0 ; + +if (bdi->wb_written_list.next == &wc->list) + first =1 ;
mm/backing-dev.c static void sync_supers_timer_fn(unsigned long unused) · line 414
Quoted 10 lines
mm/backing-dev.c +10
-0+/* Initialize list so that entry owner knows it's removed */ +list_del_init (&wc->list); +if (first) { +if (list_empty (&bdi->wb_written_list)) { + bdi->wb_written_head = ~(u64 )0 ; +return ; + } + wc =list_entry (bdi->wb_written_list.next, + struct bdi_written_count, + list);
list_first_entry()?
Review static void sync_supers_timer_fn(unsigned long unused) · line 415
Quoted 1 line
(quoted diff) +1
-0+ bdi->wb_written_head = bdi_stat(bdi, BDI_WRITTEN) + wc->written;
The resolution of the percpu counters is an issue here, I think. percpu counters update in batches of 32 counts per CPU. wc->written is going to have a value of roughly 8 or 32 depending on whether bdi->dirty_exceeded is set or not. I note that you take this into account when checking dirty threshold limits, but it doesn't appear to be taken in to here.
That means for ratelimits of 8 pages on writes and only one CPU doing IO completion wakeups are only going to occur in completion batches of 32 pages. we'll end up with something like:
waiter, written count at head count when woken
A, 8 n n + 32
B, 8 n + 32 n + 64
C, 8 n + 64 n + 96
.....
And so on. This isn't necessarily bad - we'll throttle for longer than we strictly need to - but the cumulative counter resolution error gets worse as the number of CPUs doing IO completion grows. Worst case ends up at for (num cpus * 31) + 1 pages of writeback for just the first waiter. For an arbitrary FIFO queue of depth d, the worst case is more like d * (num cpus * 31 + 1).
Hence I think that instead of calculating the next wakeup threshold when the head changes the wakeup threshold needs to be a function of the FIFO depth. That is, it is calculated at queueing time from the current tail of the queue.
e.g. something like this when queuing:
if (list_empty(&bdi->wb_written_list))
wc->wakeup_at = bdi_stat(bdi, BDI_WRITTEN) + written;
bdi->wb_written_head = wc->wakeup_at;
else {
tail = list_last_entry(&bdi->wb_written_list);
wc->wakeup_at = tail->wakeup_at + written;
}
list_add_tail(&wc->list, &bdi->wb_written_list);And this when the wakeup threshold is tripped:
written = bdi_stat(bdi, BDI_WRITTEN);
while (!list_empty(&bdi->wb_written_list)) {
wc = list_first_entry();
if (wc->wakeup_at > written)
break;
list_del_init(wc)
wakeup(wc)
}
if (!list_empty(&bdi->wb_written_list)) {
wc = list_first_entry();
bdi->wb_written_head = wc->wakeup_at;
} else
bdi->wb_written_head = ~0ULL;This takes the counter resolution completely out of the picture - if the counter resolution is 32, and there are 4 waiters on the fifo each waiting for 8 pages, then a single tick of the counter will wake them all up.
Other than that, it seems like a good approach to me...
Cheers,
Dave.
Signature · 8 lines
Dave Chinner [email protected] -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 8 lines · Dave Chinner
Quoted 1 line
+
bdi->wb_written_head=bdi_stat(bdi, BDI_WRITTEN)+wc->written;The resolution of the percpu counters is an issue here, I think. percpu counters update in batches of 32 counts per CPU.
wc->writtenis going to have a value of roughly 8 or 32 depending on whetherbdi->dirty_exceededis set or not. I note that you take this into account when checking dirty threshold limits, but it doesn't appear to be taken in to here.
The BDI stuff uses a custom batch-size, see bdi_stat_error() and related. The total error is in the order of O(n log n) where n is the number of CPUs.
But yeah, the whole dirty_exceeded thing makes life more interesting.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Dave Chinner
Quoted 1 line
+
bdi->wb_written_head=bdi_stat(bdi, BDI_WRITTEN)+wc->written;The resolution of the percpu counters is an issue here, I think. percpu counters update in batches of 32 counts per CPU.
wc->writtenis going to have a value of roughly 8 or 32 depending on whetherbdi->dirty_exceededis set or not. I note that you take this into account when checking dirty threshold limits, but it doesn't appear to be taken in to here.
Quoted 3 lines
The BDI stuff uses a custom batch-size, see
bdi_stat_error()and related. The total error is in the order of O(n log n) where n is the number of CPUs.
#define BDI_STAT_BATCH (8*(1+ilog2(nr_cpu_ids)))
A dual socket server I have here:
[ 0.000000] NR_CPUS:512 nr_cpumask_bits:512 nr_cpu_ids:32 nr_node_ids:2
Which means that the bdi per-cpu counter batch size on it would be 48 and the inaccuracy would be even bigger than I described. ;)
Quoted 2 lines
But yeah, the whole
dirty_exceededthing makes life more interesting.
I suspect the 8 vs 32 pages could go away without too much impact with the Jan's mechanism...
Another thing to consider is the impact of mulipage writes on the incoming value to balance_dirty_pages - if we get 1024 page writes running, then that may impact on the way throttling behaves, too.
Cheers,
Dave.
Signature · 8 lines
Dave Chinner [email protected] -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
maintainerQuoted 21 lines · Dave Chinner
Quoted 20 lines · Jan Kara
This patch changes
balance_dirty_pages()throttling so that the function does not submit writes on its own but rather waits for flusher thread to do enough writes. This has an advantage that we have a single source of IO allowing for better writeback locality. Also we do not have to reenter filesystems from a non-trivial context.The waiting is implemented as follows: Each BDI has a FIFO of wait requests. When
balance_dirty_pages()finds it needs to throttle the writer, it adds a request for writingwrite_chunkof pages to the FIFO and waits until the request is fulfilled or we drop below dirty limits. A flusher thread tracks amount of pages it has written and when enough pages are written since the first request in the FIFO became first, it wakes up the waiter and removes request from the FIFO (thus another request becomes first and flusher thread starts writing out on behalf of this request).Signed-off-by Jan Kara <[email protected]>
...
Quoted 5 lines
mm/backing-dev.c static void sync_supers_timer_fn(unsigned long unused) · line 414
Quoted 3 lines
mm/backing-dev.c +3
-0+ wc =list_entry (bdi->wb_written_list.next, + struct bdi_written_count, + list);
list_first_entry()?
Done, thanks.
Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +1
-0+ bdi->wb_written_head = bdi_stat(bdi, BDI_WRITTEN) + wc->written;The resolution of the percpu counters is an issue here, I think. percpu counters update in batches of 32 counts per CPU.
wc->writtenis going to have a value of roughly 8 or 32 depending on whetherbdi->dirty_exceededis set or not. I note that you take this into account when checking dirty threshold limits, but it doesn't appear to be taken in to here.
Hmm, are you sure about the number of pages? I think that the ratelimits
you speak about influence only how often we *check* the limits. Looking at sync_writeback_pages() I see:
static inline long sync_writeback_pages(unsigned long dirtied)
{
if (dirtied < ratelimit_pages)
dirtied = ratelimit_pages;
return dirtied + dirtied / 2;
} So it returns at least ratelimit_pages * 3/2. Now ratelimit_pages is
computed in writeback_set_ratelimit() and for most machines I expect the math there to end up with ratelimit_pages == 1024. So we enter balance_dirty_pages with number 1536... That being said, the error of percpu counters can still be significant - with 16 CPUs doing completion the average error is 384 (on your max_cpus == 512 machine - our distro kernels have that too).
Quoted 10 lines
That means for ratelimits of 8 pages on writes and only one CPU doing IO completion wakeups are only going to occur in completion batches of 32 pages. we'll end up with something like:
waiter, written count at head count when woken
A, 8 n n + 32
B, 8 n + 32 n + 64
C, 8 n + 64 n + 96
.....
Quoted 6 lines
And so on. This isn't necessarily bad - we'll throttle for longer than we strictly need to - but the cumulative counter resolution error gets worse as the number of CPUs doing IO completion grows. Worst case ends up at for (num cpus * 31) + 1 pages of writeback for just the first waiter. For an arbitrary FIFO queue of depth d, the worst case is more like d * (num cpus * 31 + 1).
Hmm, I don't see how the error would depend on the FIFO depth. I'd rather
think that the number of written pages after which a thread is woken is a random variable equally distributed in a range
<wanted_nr - max_counter_err, wanted_nr + max_counter_err>. And over time (or with more threads in the queue) these errors cancel out... Now as I wrote above the error for single waiter might be just too large to accept but I just wanted to argue here that I don't see how it would be getting worse with more waiters.
Quoted 7 lines
Hence I think that instead of calculating the next wakeup threshold when the head changes the wakeup threshold needs to be a function of the FIFO depth. That is, it is calculated at queueing time from the current tail of the queue.
e.g. something like this when queuing:
Quoted 8 lines
if (list_empty(&bdi->wb_written_list))
wc->wakeup_at = bdi_stat(bdi, BDI_WRITTEN) + written;
bdi->wb_written_head = wc->wakeup_at;
else {
tail = list_last_entry(&bdi->wb_written_list);
wc->wakeup_at = tail->wakeup_at + written;
}
list_add_tail(&wc->list, &bdi->wb_written_list); I specifically wanted to avoid computing the wake up time while queueing
because if some thread quits the queue (a signal is currently probably the only good reason; if a system gets below dirty limits for a short while so that only a few threads notice is an arguable reason), you would need to recompute them or live with waiting unnecessarily long.
But we could compute next wake up time from wb_written_head (when valid)
which would make more sense. I like that.
Quoted 19 lines
And this when the wakeup threshold is tripped:
written = bdi_stat(bdi, BDI_WRITTEN); while (!list_empty(&bdi->wb_written_list)) { wc = list_first_entry(); if (wc->wakeup_at > written) break; list_del_init(wc) wakeup(wc) } if (!list_empty(&bdi->wb_written_list)) { wc = list_first_entry(); bdi->wb_written_head = wc->wakeup_at; } else bdi->wb_written_head = ~0ULL;
Quoted 4 lines
This takes the counter resolution completely out of the picture - if the counter resolution is 32, and there are 4 waiters on the fifo each waiting for 8 pages, then a single tick of the counter will wake them all up.
Yeah, this would work with what I suggest above too. One just has to
write the loop carefully enough ;). Thanks for great suggestions.
Honza
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 43 lines · Jan Kara
Quoted 21 lines · Dave Chinner
Quoted 20 lines · Jan Kara
This patch changes
balance_dirty_pages()throttling so that the function does not submit writes on its own but rather waits for flusher thread to do enough writes. This has an advantage that we have a single source of IO allowing for better writeback locality. Also we do not have to reenter filesystems from a non-trivial context.The waiting is implemented as follows: Each BDI has a FIFO of wait requests. When
balance_dirty_pages()finds it needs to throttle the writer, it adds a request for writingwrite_chunkof pages to the FIFO and waits until the request is fulfilled or we drop below dirty limits. A flusher thread tracks amount of pages it has written and when enough pages are written since the first request in the FIFO became first, it wakes up the waiter and removes request from the FIFO (thus another request becomes first and flusher thread starts writing out on behalf of this request).Signed-off-by Jan Kara <[email protected]>...
Quoted 5 lines
mm/backing-dev.c static void sync_supers_timer_fn(unsigned long unused) · line 414
Quoted 3 lines
mm/backing-dev.c +3
-0+ wc =list_entry (bdi->wb_written_list.next, + struct bdi_written_count, + list);
list_first_entry()?Done, thanks.
Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +1
-0+ bdi->wb_written_head = bdi_stat(bdi, BDI_WRITTEN) + wc->written;The resolution of the percpu counters is an issue here, I think. percpu counters update in batches of 32 counts per CPU.
wc->writtenis going to have a value of roughly 8 or 32 depending on whetherbdi->dirty_exceededis set or not. I note that you take this into account when checking dirty threshold limits, but it doesn't appear to be taken in to here.Hmm, are you sure about the number of pages? I think that the ratelimits
you speak about influence only how often we *check* the limits. Looking atsync_writeback_pages()I see:
static inline longsync_writeback_pages(unsigned long dirtied)
Quoted 6 lines
{
if (dirtied < ratelimit_pages)
dirtied = ratelimit_pages;
return dirtied + dirtied / 2;
}It's declared as:
39 /*
40 * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited 41 * will look to see if it needs to force writeback or throttling. 42 */
43 static long ratelimit_pages = 32;
Quoted 4 lines
So it returns at least
ratelimit_pages* 3/2. Nowratelimit_pagesis
computed inwriteback_set_ratelimit()and for most machines I expect the math there to end up withratelimit_pages== 1024. So we enterbalance_dirty_pageswith number 1536...
But I missed this. That means we typically will check every 1024 pages until dirty_exceeded is set, then we'll check every 8 pages.
Quoted 4 lines
That being said, the error of
percpu counters can still be significant - with 16 CPUs doing completion the average error is 384 (on yourmax_cpus== 512 machine - our distro kernels have that too).
The error gets larger as the number of CPUs goes up - Peter pointed that out because:
#define BDI_STAT_BATCH (8*(1+ilog2(nr_cpu_ids)))
So for a system reporting nr_cpu_ids = 32 (as my 8 core nehalem server currently reports), the error could be 16 * 48 = 768.
Quoted 10 lines
Quoted 10 lines
That means for ratelimits of 8 pages on writes and only one CPU doing IO completion wakeups are only going to occur in completion batches of 32 pages. we'll end up with something like:
waiter, written count at head count when woken
A, 8 n n + 32
B, 8 n + 32 n + 64
C, 8 n + 64 n + 96
.....
Quoted 7 lines
Quoted 6 lines
And so on. This isn't necessarily bad - we'll throttle for longer than we strictly need to - but the cumulative counter resolution error gets worse as the number of CPUs doing IO completion grows. Worst case ends up at for (num cpus * 31) + 1 pages of writeback for just the first waiter. For an arbitrary FIFO queue of depth d, the worst case is more like d * (num cpus * 31 + 1).
Hmm, I don't see how the error would depend on the FIFO depth.
It's the cumulative error that depends on the FIFO depth, not the error seen by a single waiter.
Quoted 4 lines
I'd rather
think that the number of written pages after which a thread is woken is a random variable equally distributed in a range
<wanted_nr-max_counter_err,wanted_nr+max_counter_err>.
Right, that the error range seenby a single waiter. i.e. between
bdi->wb_written_head = bdi_stat(written) + wc->pages_to_wait_for;
If we only want to wait for, say, 10 pages from a counter value of N, the per-cpu counter increments in batches of BDI_STAT_BATCH. Hence the first time we see bdi_stat(written) pass bdi->wb_written_head will be when it reaches (N + BDI_STAT_BATCH). We will have waited (BDI_STAT_BATCH - 10) pages more than we needed to. i.e the wait time is a minimum of roundup(pages_to_wait_for, BDI_STAT_BATCH).
Quoted 5 lines
And over time
(or with more threads in the queue) these errors cancel out... Now as I wrote above the error for single waiter might be just too large to accept but I just wanted to argue here that I don't see how it would be getting worse with more waiters.
That's where I think you go wrong - the error occurs when we remove the waiter at the head of the queue, so each subsequent waiter has a resolution error introduced. These will continue to aggregate as long as there are more waiters on the queue.
e.g if we've got 3 waiters of 10 pages each, they should all be woken after 30 pages have been cleared. Instead, the first is only woken after the counter ticks (BDI_STAT_BATCH), then the second is queued, and it doesn't get woken for another counter tick. Similarly for the third waiter. When the third waiter is woken, it has waited for 3 * BDI_STAT_BATCH pages. In reality, what we realy wanted was all three waiters to be woken by the time 30 pages had been written back. i.e. the last waiter waited for (3 * (BDI_STAT_BATCH - 10)), which in general terms gives a wait overshoot for any given waiter of roughly (queue depth when queued * BDI_STAT_BATCH * nr_cpu_ids) pages.
However, if we calculate the wakeup time when we queue, each waiter ends up with a wakeup time of N + 10, N + 20, and N + 30, and the counter tick resolution is taken out of the picture. i.e. if the tick is 8, then the first wakeup occurs after 2 counter ticks, the second is woken after 3 counter ticks and the last is woken after 4 counter ticks. If the counter tick is >= 32, then all three will be woken on the first counter tick. In this case, the worst case wait overshoot for any waiter regardless of queue depth is (BDI_STAT_BATCH * nr_cpu_ids) pages.
Quoted 15 lines
Quoted 7 lines
Hence I think that instead of calculating the next wakeup threshold when the head changes the wakeup threshold needs to be a function of the FIFO depth. That is, it is calculated at queueing time from the current tail of the queue.
e.g. something like this when queuing:
Quoted 8 lines
if (list_empty(&bdi->wb_written_list)) wc->wakeup_at = bdi_stat(bdi, BDI_WRITTEN) + written; bdi->wb_written_head = wc->wakeup_at; else { tail = list_last_entry(&bdi->wb_written_list); wc->wakeup_at = tail->wakeup_at + written; } list_add_tail(&wc->list, &bdi->wb_written_list);
Quoted 3 lines
I specifically wanted to avoid computing the wake up time while queueing
because if some thread quits the queue (a signal is currently probably the only good reason;
Fair point.
Quoted 3 lines
if a system gets below dirty limits for a short while so that only a few threads notice is an arguable reason), you would need to recompute them or live with waiting unnecessarily long.
I'd suggest that we shouldn't be waking waiters prematurely if we've dropped below the dirty threshold - we need some hysteresis in the system to prevent oscillating rapidly around the threshold. If we are not issuing IO, then if we don't damp the wakeup frequency we could easily thrash around the threshold.
I'd prefer to strip down the mechanism to the simplest, most accurate form before trying to add "interesting" tweaks like faster wakeup semantics at dirty thresholds....
Quoted 2 lines
But we could compute next wake up time from
wb_written_head(when valid)
which would make more sense. I like that.
Yes, I think that would work because everything would then be delta values from a constant base - it just delays the wakeup threshold calculation until the waiter hits the head of the queue.....
Cheers,
Dave.
Signature · 8 lines
Dave Chinner [email protected] -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerQuoted 10 lines · On Tue, 22 Jun 2010 15:44:09 +1000 Dave Chinner <[email protected]> wrote:
Quoted 7 lines
Quoted 6 lines
And so on. This isn't necessarily bad - we'll throttle for longer than we strictly need to - but the cumulative counter resolution error gets worse as the number of CPUs doing IO completion grows. Worst case ends up at for (num cpus * 31) + 1 pages of writeback for just the first waiter. For an arbitrary FIFO queue of depth d, the worst case is more like d * (num cpus * 31 + 1).
Hmm, I don't see how the error would depend on the FIFO depth.
It's the cumulative error that depends on the FIFO depth, not the error seen by a single waiter.
Could use the below to basically eliminate the inaccuracies.
Obviously things might get a bit expensive in certain threshold cases but with some hysteresis that should be manageable.
From: Tim Chen <[email protected]>
Add percpu_counter_compare that allows for a quick but accurate comparison of percpu_counter with a given value.
A rough count is provided by the count field in percpu_counter structure, without accounting for the other values stored in individual cpu counters.
The actual count is a sum of count and the cpu counters. However, count
field is never different from the actual value by a factor of batch*num_online_cpu. We do not need to get actual count for comparison if count is different from the given value by this factor and allows for quick comparison without summing up all the per cpu counters.
---
include/linux/percpu_counter.h | 11 +++++++++++ lib/percpu_counter.c | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+)
include/linux/percpu_counter.h renamed from include/linux/percpu_counter.h~tmpfs-add-accurate-compare-function-to-percpu_counter-library +11 -0
@@ -40,6 +40,7 @@ void percpu_counter_destroy(struct percpvoid percpu_counter_set (struct percpu_counter *fbc,s64 amount);void __percpu_counter_add (struct percpu_counter *fbc,s64 amount,s32 batch);s64 __percpu_counter_sum (struct percpu_counter *fbc); +int percpu_counter_compare (struct percpu_counter *fbc,s64 rhs);static inline void percpu_counter_add (struct percpu_counter *fbc,s64 amount) {
@@ -98,6 +99,16 @@ static inline void percpu_counter_set(st fbc->count = amount; } +static inline int percpu_counter_compare (struct percpu_counter *fbc,s64 rhs) +{ +if (fbc->count > rhs) +return 1 ; +else if (fbc->count < rhs) +return -1 ; +else +return 0 ; +} +static inline void percpu_counter_add (struct percpu_counter *fbc,s64 amount) {
lib/percpu_counter.c renamed from lib/percpu_counter.c~tmpfs-add-accurate-compare-function-to-percpu_counter-library +27 -0
@@ -138,6 +138,33 @@ static int __cpuinit percpu_counter_hotcreturn NOTIFY_OK ; } +/* +* Compare counter against given value. +* Return 1 if greater, 0 if equal and -1 if less +*/ +int percpu_counter_compare (struct percpu_counter *fbc,s64 rhs) +{ +s64 count; + + count =percpu_counter_read (fbc); +/* Check to see if rough count will be sufficient for comparison */ +if (abs (count - rhs) > (percpu_counter_batch*num_online_cpus ())) { +if (count > rhs) +return 1 ; +else +return -1 ; + } +/* Need to use precise count */ + count =percpu_counter_sum (fbc); +if (count > rhs) +return 1 ; +else if (count < rhs) +return -1 ; +else +return 0 ; +} +EXPORT_SYMBOL (percpu_counter_compare); +static int __init percpu_counter_startup (void ) {compute_batch_value ();
_
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 16 lines · Andrew Morton
lib/percpu_counter.c renamed from lib/percpu_counter.c~tmpfs-add-accurate-compare-function-to-percpu_counter-library +16
-0+/* +* Compare counter against given value. +* Return 1 if greater, 0 if equal and -1 if less +*/ +int percpu_counter_compare (struct percpu_counter *fbc,s64 rhs) +{ +s64 count; + + count =percpu_counter_read (fbc); +/* Check to see if rough count will be sufficient for comparison */ +if (abs (count - rhs) > (percpu_counter_batch*num_online_cpus ())) { +if (count > rhs) +return 1 ; +else +return -1 ; + }
lib/percpu_counter.c static int __cpuinit percpu_counter_hotc · line 166
Quoted 10 lines
lib/percpu_counter.c renamed from lib/percpu_counter.c~tmpfs-add-accurate-compare-function-to-percpu_counter-library +10
-0+/* Need to use precise count */ + count =percpu_counter_sum (fbc); +if (count > rhs) +return 1 ; +else if (count < rhs) +return -1 ; +else +return 0 ; +} +EXPORT_SYMBOL (percpu_counter_compare);
That won't quite work as advertised for the bdi stuff since we use a custom batch size.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerQuoted 22 lines · On Tue, 22 Jun 2010 09:45:22 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 21 lines · Andrew Morton
lib/percpu_counter.c renamed from lib/percpu_counter.c~tmpfs-add-accurate-compare-function-to-percpu_counter-library +21
-0+/* +* Compare counter against given value. +* Return 1 if greater, 0 if equal and -1 if less +*/ +int percpu_counter_compare (struct percpu_counter *fbc,s64 rhs) +{ +s64 count; + + count =percpu_counter_read (fbc); +/* Check to see if rough count will be sufficient for comparison */ +if (abs (count - rhs) > (percpu_counter_batch*num_online_cpus ())) { +if (count > rhs) +return 1 ; +else +return -1 ; + } +/* Need to use precise count */ + count =percpu_counter_sum (fbc); +if (count > rhs) +return 1 ; +else if (count < rhs)
Quoted 8 lines
lib/percpu_counter.c static int __cpuinit percpu_counter_hotc · line 166
Quoted 5 lines
lib/percpu_counter.c renamed from lib/percpu_counter.c~tmpfs-add-accurate-compare-function-to-percpu_counter-library +5
-0+return -1 ; + else + return 0; +} +EXPORT_SYMBOL (percpu_counter_compare);That won't quite work as advertised for the bdi stuff since we use a custom batch size.
Oh come on, of course it will. It just needs __percpu_counter_compare() as I mentioned when merging it.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 3 lines · Andrew Morton
Oh come on, of course it will. It just needs
__percpu_counter_compare()as I mentioned when merging it.
Sure, all I was saying is that something like that needs doing..
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 10 lines · Andrew Morton
Quoted 8 lines · On Tue, 22 Jun 2010 15:44:09 +1000 Dave Chinner <[email protected]> wrote:
Quoted 7 lines
Quoted 6 lines
And so on. This isn't necessarily bad - we'll throttle for longer than we strictly need to - but the cumulative counter resolution error gets worse as the number of CPUs doing IO completion grows. Worst case ends up at for (num cpus * 31) + 1 pages of writeback for just the first waiter. For an arbitrary FIFO queue of depth d, the worst case is more like d * (num cpus * 31 + 1).
Hmm, I don't see how the error would depend on the FIFO depth.
Quoted 7 lines
Quoted 2 lines
It's the cumulative error that depends on the FIFO depth, not the error seen by a single waiter.
Could use the below to basically eliminate the inaccuracies.
Obviously things might get a bit expensive in certain threshold cases but with some hysteresis that should be manageable.
That seems a lot more... unpredictable than modifying the accounting to avoid cumulative errors.
lib/percpu_counter.c static int __cpuinit percpu_counter_hotc · line 151
Quoted 2 lines
lib/percpu_counter.c renamed from lib/percpu_counter.c~tmpfs-add-accurate-compare-function-to-percpu_counter-library +2
-0+/* Check to see if rough count will be sufficient for comparison */ +if (abs (count - rhs) > (percpu_counter_batch*num_online_cpus ())) {
Also, that's a big margin when we are doing equality matches for every page IO completion. If we a large CPU count machine where per-cpu counters actually improve performance (say 16p) then we're going to be hitting the slow path for the last 512 pages of every waiter. Hence I think the counter sum is compared too often to scale with this method of comparison.
Cheers,
Dave.
Signature · 8 lines
Dave Chinner [email protected] -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
maintainerQuoted 22 lines · Dave Chinner
Quoted 10 lines · Andrew Morton
Quoted 8 lines · On Tue, 22 Jun 2010 15:44:09 +1000 Dave Chinner <[email protected]> wrote:
Quoted 7 lines
Quoted 6 lines
And so on. This isn't necessarily bad - we'll throttle for longer than we strictly need to - but the cumulative counter resolution error gets worse as the number of CPUs doing IO completion grows. Worst case ends up at for (num cpus * 31) + 1 pages of writeback for just the first waiter. For an arbitrary FIFO queue of depth d, the worst case is more like d * (num cpus * 31 + 1).
Hmm, I don't see how the error would depend on the FIFO depth.
Quoted 7 lines
Quoted 2 lines
It's the cumulative error that depends on the FIFO depth, not the error seen by a single waiter.
Could use the below to basically eliminate the inaccuracies.
Obviously things might get a bit expensive in certain threshold cases but with some hysteresis that should be manageable.
That seems a lot more... unpredictable than modifying the accounting to avoid cumulative errors.
Quoted 9 lines
lib/percpu_counter.c static int __cpuinit percpu_counter_hotc · line 151
Quoted 2 lines
lib/percpu_counter.c renamed from lib/percpu_counter.c~tmpfs-add-accurate-compare-function-to-percpu_counter-library +2
-0+/* Check to see if rough count will be sufficient for comparison */ +if (abs (count - rhs) > (percpu_counter_batch*num_online_cpus ())) {Also, that's a big margin when we are doing equality matches for every page IO completion. If we a large CPU count machine where per-cpu counters actually improve performance (say 16p) then we're going to be hitting the slow path for the last 512 pages of every waiter. Hence I think the counter sum is compared too often to scale with this method of comparison.
On the other hand I think we will have to come up with something
more clever than what I do now because for some huge machines with nr_cpu_ids == 256, the error of the counter is 256*9*8 = 18432 so that's already unacceptable given the amounts we want to check (like 1536) - already for nr_cpu_ids == 32, the error is the same as the difference we want to check. I think we'll have to come up with some scheme whose error is not dependent on the number of cpus or if it is dependent, it's only a weak dependency (like a logarithm or so).
Or we could rely on the fact that IO completions for a bdi won't happen on
all CPUs and thus the error would be much more bounded. But I'm not sure how much that is true or not.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 11 lines
On the other hand I think we will have to come up with something
more clever than what I do now because for some huge machines withnr_cpu_ids== 256, the error of the counter is 256*9*8 = 18432 so that's already unacceptable given the amounts we want to check (like 1536) - already fornr_cpu_ids== 32, the error is the same as the difference we want to check. I think we'll have to come up with some scheme whose error is not dependent on the number of cpus or if it is dependent, it's only a weak dependency (like a logarithm or so).
Or we could rely on the fact that IO completions for a bdi won't happen on
all CPUs and thus the error would be much more bounded. But I'm not sure how much that is true or not.
Yes the per CPU counter seems tricky. How about plain atomic operations?
This test shows that atomic_dec_and_test() is about 4.5 times slower than plain i-- in a 4-core CPU. Not bad.
Note that
1) we can avoid the atomic operations when there are no active waiters
2) most writeback will be submitted by one per-bdi-flusher, so no worry of cache bouncing (this also means the per CPU counter error is normally bounded by the batch size)
3) the cost of atomic inc/dec will be weakly related to core numbers but never socket numbers (based on 2), so won't scale too bad
Thanks,
Fengguang
---
$ perf stat ./atomic
Performance counter stats for './atomic':
903.875304 task-clock-msecs # 0.998 CPUs
76 context-switches # 0.000 M/sec
0 CPU-migrations # 0.000 M/sec
98 page-faults # 0.000 M/sec
3011186459 cycles # 3331.418 M/sec
1608926490 instructions # 0.534 IPC
301481656 branches # 333.543 M/sec
94932 branch-misses # 0.031 %
88687 cache-references # 0.098 M/sec
1286 cache-misses # 0.001 M/sec0.905576197 seconds time elapsed
$ perf stat ./non-atomic
Performance counter stats for './non-atomic':
215.315814 task-clock-msecs # 0.996 CPUs
18 context-switches # 0.000 M/sec
0 CPU-migrations # 0.000 M/sec
99 page-faults # 0.000 M/sec
704358635 cycles # 3271.281 M/sec
303445790 instructions # 0.431 IPC
100574889 branches # 467.104 M/sec
39323 branch-misses # 0.039 %
36064 cache-references # 0.167 M/sec
850 cache-misses # 0.004 M/sec0.216175521 seconds time elapsed
--------------------------------------------------------------------------------
$ cat atomic.c #include <stdio.h>
typedef struct {
int counter;
} atomic_t;static inline int atomic_dec_and_test(atomic_t *v)
{
unsigned char c;
asm volatile("lock; decl %0; sete %1"
: "+m" (v->counter), "=qm" (c)
: : "memory");
return c != 0;
}int main(void)
{
atomic_t i;
i.counter = 100000000;
for (; !atomic_dec_and_test(&i);)
;
return 0;
}--------------------------------------------------------------------------------
$ cat non-atomic.c #include <stdio.h>
int main(void)
{
int i;
for (i = 100000000; i; i--)
;
return 0;
}Quoted 3 lines · Wu Fengguang
This test shows that
atomic_dec_and_test()is about 4.5 times slower than plain i-- in a 4-core CPU. Not bad.
It gets worse - much worse - on larger machines.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 17 lines · Wu Fengguang
#include <
stdio.h>typedef struct { int counter; } atomic_t;static inline int
atomic_dec_and_test(atomic_t *v){ unsigned char c; asm volatile("lock; decl %0; sete %1" : "+m" (v->counter), "=qm" (c) : : "memory"); return c != 0; }int main(void)
Quoted 10 lines
{
atomic_t i;
i.counter = 100000000;
for (; !atomic_dec_and_test(&i);)
;
return 0;
} This test utterly fails to stress the concurrency, you want to create nr_cpus threads and then pound the global variable. Then compare it against the per-cpu-counter variant.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 26 lines · Peter Zijlstra
Quoted 25 lines · Wu Fengguang
#include <
stdio.h>typedef struct { int counter; } atomic_t;static inline int
atomic_dec_and_test(atomic_t *v){ unsigned char c; asm volatile("lock; decl %0; sete %1" : "+m" (v->counter), "=qm" (c) : : "memory"); return c != 0; }int main(void)
{ atomic_t i; i.counter = 100000000; for (; !atomic_dec_and_test(&i);) ;
Quoted 6 lines
Quoted 2 lines
return 0; }This test utterly fails to stress the concurrency, you want to create
nr_cpusthreads and then pound the global variable. Then compare it against the per-cpu-counter variant.
I mean to test an atomic value that is mainly visited by one single CPU.
It sounds not reasonable for the IO completion IRQs to land randomly on every CPU in the system.. when the IOs are submitted mostly by a dedicated thread and to one single BDI (but yes, the BDI may be some "compound" device).
Thanks,
Fengguang
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
maintainerQuoted 14 lines · Wu Fengguang
Quoted 11 lines
On the other hand I think we will have to come up with something
more clever than what I do now because for some huge machines withnr_cpu_ids== 256, the error of the counter is 256*9*8 = 18432 so that's already unacceptable given the amounts we want to check (like 1536) - already fornr_cpu_ids== 32, the error is the same as the difference we want to check. I think we'll have to come up with some scheme whose error is not dependent on the number of cpus or if it is dependent, it's only a weak dependency (like a logarithm or so).
Or we could rely on the fact that IO completions for a bdi won't happen on
all CPUs and thus the error would be much more bounded. But I'm not sure how much that is true or not.Yes the per CPU counter seems tricky. How about plain atomic operations?
Quoted 8 lines
This test shows that
atomic_dec_and_test()is about 4.5 times slower than plain i-- in a 4-core CPU. Not bad.Note that
1) we can avoid the atomic operations when there are no active waiters
2) most writeback will be submitted by one per-bdi-flusher, so no worry of cache bouncing (this also means the per CPU counter error is normally bounded by the batch size)
Yes, writeback will be submitted by one flusher thread but the question
is rather where the writeback will be completed. And that depends on which CPU that particular irq is handled. As far as my weak knowledge of HW goes, this very much depends on the system configuration (i.e., irq affinity and other things).
Quoted 2 lines
3) the cost of atomic inc/dec will be weakly related to core numbers but never socket numbers (based on 2), so won't scale too bad
Honza
Quoted 80 lines
---
$ perf stat ./atomicPerformance counter stats for './atomic':
903.875304 task-clock-msecs # 0.998 CPUs 76 context-switches # 0.000 M/sec 0 CPU-migrations # 0.000 M/sec 98 page-faults # 0.000 M/sec 3011186459 cycles # 3331.418 M/sec 1608926490 instructions # 0.534 IPC 301481656 branches # 333.543 M/sec 94932 branch-misses # 0.031 % 88687 cache-references # 0.098 M/sec 1286 cache-misses # 0.001 M/sec0.905576197 seconds time elapsed$ perf stat ./non-atomicPerformance counter stats for './non-atomic':
215.315814 task-clock-msecs # 0.996 CPUs 18 context-switches # 0.000 M/sec 0 CPU-migrations # 0.000 M/sec 99 page-faults # 0.000 M/sec 704358635 cycles # 3271.281 M/sec 303445790 instructions # 0.431 IPC 100574889 branches # 467.104 M/sec 39323 branch-misses # 0.039 % 36064 cache-references # 0.167 M/sec 850 cache-misses # 0.004 M/sec0.216175521 seconds time elapsed--------------------------------------------------------------------------------
$ cat atomic.c #include <stdio.h>typedef struct { int counter; } atomic_t;static inline int
atomic_dec_and_test(atomic_t *v){ unsigned char c; asm volatile("lock; decl %0; sete %1" : "+m" (v->counter), "=qm" (c) : : "memory"); return c != 0; }int main(void)
{ atomic_t i; i.counter = 100000000; for (; !atomic_dec_and_test(&i);) ; return 0; }--------------------------------------------------------------------------------
$ cat non-atomic.c #include <stdio.h>int main(void)
{ int i; for (i = 100000000; i; i--) ; return 0; }
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 23 lines · Jan Kara
Quoted 14 lines · Wu Fengguang
Quoted 11 lines
On the other hand I think we will have to come up with something
more clever than what I do now because for some huge machines withnr_cpu_ids== 256, the error of the counter is 256*9*8 = 18432 so that's already unacceptable given the amounts we want to check (like 1536) - already fornr_cpu_ids== 32, the error is the same as the difference we want to check. I think we'll have to come up with some scheme whose error is not dependent on the number of cpus or if it is dependent, it's only a weak dependency (like a logarithm or so).
Or we could rely on the fact that IO completions for a bdi won't happen on
all CPUs and thus the error would be much more bounded. But I'm not sure how much that is true or not.Yes the per CPU counter seems tricky. How about plain atomic operations?
Quoted 8 lines
This test shows that
atomic_dec_and_test()is about 4.5 times slower than plain i-- in a 4-core CPU. Not bad.Note that
1) we can avoid the atomic operations when there are no active waiters
2) most writeback will be submitted by one per-bdi-flusher, so no worry of cache bouncing (this also means the per CPU counter error is normally bounded by the batch size)
Quoted 5 lines
Yes, writeback will be submitted by one flusher thread but the question
is rather where the writeback will be completed. And that depends on which CPU that particular irq is handled. As far as my weak knowledge of HW goes, this very much depends on the system configuration (i.e., irq affinity and other things).
Either the irq goes to the io submit CPU, or some fixed CPU (somehow determined by the bdi?) I guess? My wild guess is, it may be bad for the irq to goto some random CPU...
Thanks,
Fengguang
Quoted 13 lines · Jan Kara
Quoted 12 lines · Wu Fengguang
Quoted 11 lines
On the other hand I think we will have to come up with something
more clever than what I do now because for some huge machines withnr_cpu_ids== 256, the error of the counter is 256*9*8 = 18432 so that's already unacceptable given the amounts we want to check (like 1536) - already fornr_cpu_ids== 32, the error is the same as the difference we want to check. I think we'll have to come up with some scheme whose error is not dependent on the number of cpus or if it is dependent, it's only a weak dependency (like a logarithm or so).
Or we could rely on the fact that IO completions for a bdi won't happen on
all CPUs and thus the error would be much more bounded. But I'm not sure how much that is true or not.
Quoted 4 lines
Quoted 4 lines
Yes the per CPU counter seems tricky. How about plain atomic operations?
This test shows that
atomic_dec_and_test()is about 4.5 times slower than plain i-- in a 4-core CPU. Not bad.
It's not how fast an uncontended operation runs that matter - it's what happens when it is contended by lots of CPUs. In my experience, atomics in writeback paths scale to medium sized machines (say 16-32p) but bottleneck on larger configurations due to the increased cost of cacheline propagation on larger machines.
Quoted 2 lines
Quoted 2 lines
Note that
1) we can avoid the atomic operations when there are no active waiters
Under heavy IO load there will always be waiters.
Quoted 8 lines
Quoted 3 lines
2) most writeback will be submitted by one per-bdi-flusher, so no worry of cache bouncing (this also means the per CPU counter error is normally bounded by the batch size)
Yes, writeback will be submitted by one flusher thread but the question
is rather where the writeback will be completed. And that depends on which CPU that particular irq is handled. As far as my weak knowledge of HW goes, this very much depends on the system configuration (i.e., irq affinity and other things).
And how many paths to the storage you are using, how threaded the underlying driver is, whether it is using MSI to direct interrupts to multiple CPUs instead of just one, etc.
As we scale up we're more likely to see multiple CPUs doing IO completion for the same BDI because the storage configs are more complex in high end machines. Hence IMO preventing cacheline bouncing between submission and completion is a significant scalability concern.
Cheers,
Dave.
Signature · 8 lines
Dave Chinner [email protected] -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
maintainerQuoted 14 lines · Dave Chinner
Quoted 8 lines · Jan Kara
Quoted 3 lines
2) most writeback will be submitted by one per-bdi-flusher, so no worry of cache bouncing (this also means the per CPU counter error is normally bounded by the batch size)
Yes, writeback will be submitted by one flusher thread but the question
is rather where the writeback will be completed. And that depends on which CPU that particular irq is handled. As far as my weak knowledge of HW goes, this very much depends on the system configuration (i.e., irq affinity and other things).And how many paths to the storage you are using, how threaded the underlying driver is, whether it is using MSI to direct interrupts to multiple CPUs instead of just one, etc.
Quoted 5 lines
As we scale up we're more likely to see multiple CPUs doing IO completion for the same BDI because the storage configs are more complex in high end machines. Hence IMO preventing cacheline bouncing between submission and completion is a significant scalability concern.
Thanks for details. I'm wondering whether we could assume that although
IO completion can run on several CPUs, it will be still a fairly limited number of CPUs. If this is the case, we could then implement a per-cpu counter that would additionally track number of CPUs modifying the counter (the number of CPUs would get zeroed in ???_counter_sum). This way the number of atomic operations won't be much higher (only one atomic inc when a CPU updates the counter for the first time) and if only several CPUs modify the counter, we would be able to bound the error much better.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 15 lines · Jan Kara
Quoted 14 lines · Dave Chinner
Quoted 8 lines · Jan Kara
Quoted 3 lines
2) most writeback will be submitted by one per-bdi-flusher, so no worry of cache bouncing (this also means the per CPU counter error is normally bounded by the batch size)
Yes, writeback will be submitted by one flusher thread but the question
is rather where the writeback will be completed. And that depends on which CPU that particular irq is handled. As far as my weak knowledge of HW goes, this very much depends on the system configuration (i.e., irq affinity and other things).And how many paths to the storage you are using, how threaded the underlying driver is, whether it is using MSI to direct interrupts to multiple CPUs instead of just one, etc.
Quoted 8 lines
Quoted 5 lines
As we scale up we're more likely to see multiple CPUs doing IO completion for the same BDI because the storage configs are more complex in high end machines. Hence IMO preventing cacheline bouncing between submission and completion is a significant scalability concern.
Thanks for details. I'm wondering whether we could assume that although
IO completion can run on several CPUs, it will be still a fairly limited number of CPUs.
I don't think we can. For example, the 10GB/s test bed I used in 2006 had 32 dual port FC HBAs in it and only 24 CPUs. The result was that every single CPU was taking HBA interrupts and hence doing IO completion processing.
Quoted 7 lines
If this is the case, we could then implement a per-cpu counter that would additionally track number of CPUs modifying the counter (the number of CPUs would get zeroed in ???_counter_sum). This way the number of atomic operations won't be much higher (only one atomic inc when a CPU updates the counter for the first time) and if only several CPUs modify the counter, we would be able to bound the error much better.
Effectively track the number of changed counters? That might work on several levels. Firstly, the number of changed counters could be a trigger for accurate summing during counter reads, and secondly it could be used to optimise the summing only to lock and sum changed CPUs, which should also speed that operation up...
I also had a thought for removing the error completely from the default complete-on-submitter-cpu behaviour - all the percpu accounting in that case is likely to be on the local counter, so the counter read could return (fbc->count + local cpu count) rather than just fbc->count. This gives us finer-grained visibility of local completions at no extra cost.
This only falls down when completions are on another CPU, but if we can track the maximum counter error bounds across those other CPUs like you've suggest then we should be able to make this work...
Cheers,
Dave.
Signature · 8 lines
Dave Chinner [email protected] -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 3 lines · Wu Fengguang
2) most writeback will be submitted by one per-bdi-flusher, so no worry of cache bouncing (this also means the per CPU counter error is normally bounded by the batch size)
What counter are we talking about exactly? Once balanance_dirty_pages stops submitting I/O the per-bdi flusher thread will in fact be the only thing submitting writeback, unless you count direct invocations of writeback_single_inode.
Jan Kara
maintainerQuoted 6 lines · Christoph Hellwig
Quoted 3 lines · Wu Fengguang
2) most writeback will be submitted by one per-bdi-flusher, so no worry of cache bouncing (this also means the per CPU counter error is normally bounded by the batch size)
What counter are we talking about exactly? Once
balanance_dirty_pages
The new per-bdi counter I'd like to introduce.
Quoted 3 lines
stops submitting I/O the per-bdi flusher thread will in fact be the only thing submitting writeback, unless you count direct invocations of
writeback_single_inode.
Yes, I agree that the per-bdi flusher thread should be the only thread
submitting lots of IO (there is direct reclaim or kswapd if we change direct reclaim but those should be negligible). So does this mean that also I/O completions will be local to the CPU running per-bdi flusher thread? Because the counter is incremented from the I/O completion callback.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 12 lines · Jan Kara
Quoted 6 lines · Christoph Hellwig
Quoted 3 lines · Wu Fengguang
2) most writeback will be submitted by one per-bdi-flusher, so no worry of cache bouncing (this also means the per CPU counter error is normally bounded by the batch size)
What counter are we talking about exactly? Once
balanance_dirty_pagesThe new per-bdi counter I'd like to introduce.
Quoted 3 lines
stops submitting I/O the per-bdi flusher thread will in fact be the only thing submitting writeback, unless you count direct invocations of
writeback_single_inode.
Quoted 6 lines
Yes, I agree that the per-bdi flusher thread should be the only thread
submitting lots of IO (there is direct reclaim or kswapd if we change direct reclaim but those should be negligible). So does this mean that also I/O completions will be local to the CPU running per-bdi flusher thread? Because the counter is incremented from the I/O completion callback.
By default we set QUEUE_FLAG_SAME_COMP, which means we hand completions back to the submitter CPU during blk_complete_request(). Completion processing is then handled by a softirq on the CPU selected for completion processing.
This was done, IIRC, because it provided some OLTP benchmark 1-2% better results. It can, however, be turned off via /sys/block/<foo>/queue/rq_affinity, and there's no guarantee that the completion processing doesn't get handled off to some other CPU (e.g. via a workqueue) so we cannot rely on this completion behaviour to avoid cacheline bouncing.
Cheers,
Dave.
Signature · 8 lines
Dave Chinner [email protected] -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 20 lines · Dave Chinner
Quoted 12 lines · Jan Kara
Quoted 6 lines · Christoph Hellwig
Quoted 3 lines · Wu Fengguang
2) most writeback will be submitted by one per-bdi-flusher, so no worry of cache bouncing (this also means the per CPU counter error is normally bounded by the batch size)
What counter are we talking about exactly? Once
balanance_dirty_pagesThe new per-bdi counter I'd like to introduce.
Quoted 3 lines
stops submitting I/O the per-bdi flusher thread will in fact be the only thing submitting writeback, unless you count direct invocations of
writeback_single_inode.Quoted 6 lines
Yes, I agree that the per-bdi flusher thread should be the only thread
submitting lots of IO (there is direct reclaim or kswapd if we change direct reclaim but those should be negligible). So does this mean that also I/O completions will be local to the CPU running per-bdi flusher thread? Because the counter is incremented from the I/O completion callback.
Quoted 4 lines
By default we set
QUEUE_FLAG_SAME_COMP, which means we hand completions back to the submitter CPU duringblk_complete_request(). Completion processing is then handled by a softirq on the CPU selected for completion processing.
Good to know about that, thanks!
Quoted 6 lines
This was done, IIRC, because it provided some OLTP benchmark 1-2% better results. It can, however, be turned off via /sys/block/<foo>/queue/rq_affinity, and there's no guarantee that the completion processing doesn't get handled off to some other CPU (e.g. via a workqueue) so we cannot rely on this completion behaviour to avoid cacheline bouncing.
If rq_affinity does not work reliably somewhere in the IO completion path, why not trying to fix it? Otherwise all the page/mapping/zone cachelines covered by test_set_page_writeback()/test_clear_page_writeback() (and more other functions) will also be bounced.
Another option is to put atomic accounting into test_set_page_writeback() ie. the IO submission path. This actually matches the current balanance_dirty_pages() behavior. It may then block on get_request(). The down side is, get_request() blocks until queue depth goes down from nr_congestion_on to nr_congestion_off, which is not as smooth as the IO completion path. As a result balanance_dirty_pages() may get delayed much more than necessary when there is only 1 waiter, and wake up multiple waiters in bursts.
Thanks,
Fengguang
Quoted 35 lines · Wu Fengguang
Quoted 20 lines · Dave Chinner
Quoted 12 lines · Jan Kara
Quoted 6 lines · Christoph Hellwig
Quoted 3 lines · Wu Fengguang
2) most writeback will be submitted by one per-bdi-flusher, so no worry of cache bouncing (this also means the per CPU counter error is normally bounded by the batch size)
What counter are we talking about exactly? Once
balanance_dirty_pagesThe new per-bdi counter I'd like to introduce.
Quoted 3 lines
stops submitting I/O the per-bdi flusher thread will in fact be the only thing submitting writeback, unless you count direct invocations of
writeback_single_inode.Quoted 6 lines
Yes, I agree that the per-bdi flusher thread should be the only thread
submitting lots of IO (there is direct reclaim or kswapd if we change direct reclaim but those should be negligible). So does this mean that also I/O completions will be local to the CPU running per-bdi flusher thread? Because the counter is incremented from the I/O completion callback.Quoted 4 lines
By default we set
QUEUE_FLAG_SAME_COMP, which means we hand completions back to the submitter CPU duringblk_complete_request(). Completion processing is then handled by a softirq on the CPU selected for completion processing.Good to know about that, thanks!
Quoted 6 lines
This was done, IIRC, because it provided some OLTP benchmark 1-2% better results. It can, however, be turned off via /sys/block/<foo>/queue/rq_affinity, and there's no guarantee that the completion processing doesn't get handled off to some other CPU (e.g. via a workqueue) so we cannot rely on this completion behaviour to avoid cacheline bouncing.
Quoted 2 lines
If
rq_affinitydoes not work reliably somewhere in the IO completion path, why not trying to fix it?
Because completion on the submitter CPU is not ideal for high bandwidth buffered IO.
Quoted 3 lines
Otherwise all the page/mapping/zone
cachelines covered by test_set_page_writeback()/test_clear_page_writeback() (and more other functions) will also be bounced.
Yes, but when the flusher thread is approaching being CPU bound for high throughput IO, bouncing cachelines to another CPU during completion costs far less in terms of throughput compared to reducing the amount of time available to issue IO on that CPU.
Quoted 8 lines
Another option is to put atomic accounting into
test_set_page_writeback()ie. the IO submission path. This actually matches the currentbalanance_dirty_pages()behavior. It may then block onget_request(). The down side is,get_request()blocks until queue depth goes down fromnr_congestion_ontonr_congestion_off, which is not as smooth as the IO completion path. As a resultbalanance_dirty_pages()may get delayed much more than necessary when there is only 1 waiter, and wake up multiple waiters in bursts.
Being reliant on the block layer queuing behaviour for VM congestion control is exactly the problem are trying to avoid...
Cheers,
Dave.
Signature · 8 lines
Dave Chinner [email protected] -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 36 lines · Dave Chinner
Quoted 35 lines · Wu Fengguang
Quoted 20 lines · Dave Chinner
Quoted 12 lines · Jan Kara
Quoted 6 lines · Christoph Hellwig
Quoted 3 lines · Wu Fengguang
2) most writeback will be submitted by one per-bdi-flusher, so no worry of cache bouncing (this also means the per CPU counter error is normally bounded by the batch size)
What counter are we talking about exactly? Once
balanance_dirty_pagesThe new per-bdi counter I'd like to introduce.
Quoted 3 lines
stops submitting I/O the per-bdi flusher thread will in fact be the only thing submitting writeback, unless you count direct invocations of
writeback_single_inode.Quoted 6 lines
Yes, I agree that the per-bdi flusher thread should be the only thread
submitting lots of IO (there is direct reclaim or kswapd if we change direct reclaim but those should be negligible). So does this mean that also I/O completions will be local to the CPU running per-bdi flusher thread? Because the counter is incremented from the I/O completion callback.Quoted 4 lines
By default we set
QUEUE_FLAG_SAME_COMP, which means we hand completions back to the submitter CPU duringblk_complete_request(). Completion processing is then handled by a softirq on the CPU selected for completion processing.Good to know about that, thanks!
Quoted 6 lines
This was done, IIRC, because it provided some OLTP benchmark 1-2% better results. It can, however, be turned off via /sys/block/<foo>/queue/rq_affinity, and there's no guarantee that the completion processing doesn't get handled off to some other CPU (e.g. via a workqueue) so we cannot rely on this completion behaviour to avoid cacheline bouncing.
Quoted 5 lines
Quoted 2 lines
If
rq_affinitydoes not work reliably somewhere in the IO completion path, why not trying to fix it?Because completion on the submitter CPU is not ideal for high bandwidth buffered IO.
Yes there may be heavy post-processing for read data, however for writes it is mainly the pre-processing that costs CPU? So perfect rq_affinity should always benefit write IO?
Quoted 8 lines
Quoted 3 lines
Otherwise all the page/mapping/zone
cachelines covered by test_set_page_writeback()/test_clear_page_writeback() (and more other functions) will also be bounced.Yes, but when the flusher thread is approaching being CPU bound for high throughput IO, bouncing cachelines to another CPU during completion costs far less in terms of throughput compared to reducing the amount of time available to issue IO on that CPU.
Yes, reasonable for reads.
Quoted 11 lines
Quoted 8 lines
Another option is to put atomic accounting into
test_set_page_writeback()ie. the IO submission path. This actually matches the currentbalanance_dirty_pages()behavior. It may then block onget_request(). The down side is,get_request()blocks until queue depth goes down fromnr_congestion_ontonr_congestion_off, which is not as smooth as the IO completion path. As a resultbalanance_dirty_pages()may get delayed much more than necessary when there is only 1 waiter, and wake up multiple waiters in bursts.Being reliant on the block layer queuing behaviour for VM congestion control is exactly the problem are trying to avoid...
Yes this is not a good option. The paragraph looks more like stating a potential benefit of the proposed patch :)
Thanks,
Fengguang
Quoted 37 lines · Wu Fengguang
Quoted 36 lines · Dave Chinner
Quoted 35 lines · Wu Fengguang
Quoted 20 lines · Dave Chinner
Quoted 12 lines · Jan Kara
Quoted 6 lines · Christoph Hellwig
Quoted 3 lines · Wu Fengguang
2) most writeback will be submitted by one per-bdi-flusher, so no worry of cache bouncing (this also means the per CPU counter error is normally bounded by the batch size)
What counter are we talking about exactly? Once
balanance_dirty_pagesThe new per-bdi counter I'd like to introduce.
Quoted 3 lines
stops submitting I/O the per-bdi flusher thread will in fact be the only thing submitting writeback, unless you count direct invocations of
writeback_single_inode.Quoted 6 lines
Yes, I agree that the per-bdi flusher thread should be the only thread
submitting lots of IO (there is direct reclaim or kswapd if we change direct reclaim but those should be negligible). So does this mean that also I/O completions will be local to the CPU running per-bdi flusher thread? Because the counter is incremented from the I/O completion callback.Quoted 4 lines
By default we set
QUEUE_FLAG_SAME_COMP, which means we hand completions back to the submitter CPU duringblk_complete_request(). Completion processing is then handled by a softirq on the CPU selected for completion processing.Good to know about that, thanks!
Quoted 6 lines
This was done, IIRC, because it provided some OLTP benchmark 1-2% better results. It can, however, be turned off via /sys/block/<foo>/queue/rq_affinity, and there's no guarantee that the completion processing doesn't get handled off to some other CPU (e.g. via a workqueue) so we cannot rely on this completion behaviour to avoid cacheline bouncing.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
If
rq_affinitydoes not work reliably somewhere in the IO completion path, why not trying to fix it?Because completion on the submitter CPU is not ideal for high bandwidth buffered IO.
Yes there may be heavy post-processing for read data, however for writes it is mainly the pre-processing that costs CPU?
Could be either - delayed allocation requires significant pre-processing for allocation. Avoiding this by using preallocation just moves the processing load to IO completion which needs to issue transactions to mark the region written.
Quoted 2 lines
So perfect
rq_affinity
should always benefit write IO?
No, because the flusher thread gets to be CPU bound just writing pages, allocating blocks and submitting IO. It might take 5-10GB/s to get there (say a million dirty pages a second being processed by a single CPU), but that's the sort of storage subsystem XFS is capable of driving. IO completion time for such a workload is significant, too, so putting that on the same CPU as the flusher thread will slow things down by far more than gain from avoiding cacheline bouncing.
Quoted 10 lines
Quoted 8 lines
Quoted 3 lines
Otherwise all the page/mapping/zone
cachelines covered by test_set_page_writeback()/test_clear_page_writeback() (and more other functions) will also be bounced.Yes, but when the flusher thread is approaching being CPU bound for high throughput IO, bouncing cachelines to another CPU during completion costs far less in terms of throughput compared to reducing the amount of time available to issue IO on that CPU.
Yes, reasonable for reads.
I was taking about writes - the flusher threads don't do any reading ;)
Cheers,
Dave.
Signature · 8 lines
Dave Chinner [email protected] -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 44 lines · Dave Chinner
Quoted 43 lines · Wu Fengguang
Quoted 41 lines · Dave Chinner
Quoted 35 lines · Wu Fengguang
Quoted 20 lines · Dave Chinner
Quoted 12 lines · Jan Kara
Quoted 6 lines · Christoph Hellwig
Quoted 3 lines · Wu Fengguang
2) most writeback will be submitted by one per-bdi-flusher, so no worry of cache bouncing (this also means the per CPU counter error is normally bounded by the batch size)
What counter are we talking about exactly? Once
balanance_dirty_pagesThe new per-bdi counter I'd like to introduce.
Quoted 3 lines
stops submitting I/O the per-bdi flusher thread will in fact be the only thing submitting writeback, unless you count direct invocations of
writeback_single_inode.Quoted 6 lines
Yes, I agree that the per-bdi flusher thread should be the only thread
submitting lots of IO (there is direct reclaim or kswapd if we change direct reclaim but those should be negligible). So does this mean that also I/O completions will be local to the CPU running per-bdi flusher thread? Because the counter is incremented from the I/O completion callback.Quoted 4 lines
By default we set
QUEUE_FLAG_SAME_COMP, which means we hand completions back to the submitter CPU duringblk_complete_request(). Completion processing is then handled by a softirq on the CPU selected for completion processing.Good to know about that, thanks!
Quoted 6 lines
This was done, IIRC, because it provided some OLTP benchmark 1-2% better results. It can, however, be turned off via /sys/block/<foo>/queue/rq_affinity, and there's no guarantee that the completion processing doesn't get handled off to some other CPU (e.g. via a workqueue) so we cannot rely on this completion behaviour to avoid cacheline bouncing.
Quoted 2 lines
If
rq_affinitydoes not work reliably somewhere in the IO completion path, why not trying to fix it?Because completion on the submitter CPU is not ideal for high bandwidth buffered IO.
Quoted 7 lines
Quoted 2 lines
Yes there may be heavy post-processing for read data, however for writes it is mainly the pre-processing that costs CPU?
Could be either - delayed allocation requires significant pre-processing for allocation. Avoiding this by using preallocation just moves the processing load to IO completion which needs to issue transactions to mark the region written.
Good point, thanks.
Quoted 11 lines
Quoted 2 lines
So perfect
rq_affinity
should always benefit write IO?No, because the flusher thread gets to be CPU bound just writing pages, allocating blocks and submitting IO. It might take 5-10GB/s to get there (say a million dirty pages a second being processed by a single CPU), but that's the sort of storage subsystem XFS is capable of driving. IO completion time for such a workload is significant, too, so putting that on the same CPU as the flusher thread will slow things down by far more than gain from avoiding cacheline bouncing.
So super fast storage is going to demand multiple flushers per bdi. And once we run multiple flushers for one bdi, it will again be beneficial to schedule IO completion to the flusher CPU :)
However in this case per bdi atomic value (and some others) will be bounced among the flusher CPUs...
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Otherwise all the page/mapping/zone
cachelines covered by test_set_page_writeback()/test_clear_page_writeback() (and more other functions) will also be bounced.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Yes, but when the flusher thread is approaching being CPU bound for high throughput IO, bouncing cachelines to another CPU during completion costs far less in terms of throughput compared to reducing the amount of time available to issue IO on that CPU.
Yes, reasonable for reads.
I was taking about writes - the flusher threads don't do any reading ;)
Ah, sorry for not catching the words "flusher thread" :)
Thanks,
Fengguang
Quoted 47 lines · Wu Fengguang
Quoted 24 lines · Dave Chinner
Quoted 23 lines · Wu Fengguang
Quoted 21 lines · Dave Chinner
Quoted 15 lines · Wu Fengguang
Quoted 4 lines · Dave Chinner
By default we set
QUEUE_FLAG_SAME_COMP, which means we hand completions back to the submitter CPU duringblk_complete_request(). Completion processing is then handled by a softirq on the CPU selected for completion processing.Good to know about that, thanks!
Quoted 6 lines
This was done, IIRC, because it provided some OLTP benchmark 1-2% better results. It can, however, be turned off via /sys/block/<foo>/queue/rq_affinity, and there's no guarantee that the completion processing doesn't get handled off to some other CPU (e.g. via a workqueue) so we cannot rely on this completion behaviour to avoid cacheline bouncing.
Quoted 2 lines
If
rq_affinitydoes not work reliably somewhere in the IO completion path, why not trying to fix it?Because completion on the submitter CPU is not ideal for high bandwidth buffered IO.
Quoted 7 lines
Quoted 2 lines
Yes there may be heavy post-processing for read data, however for writes it is mainly the pre-processing that costs CPU?
Could be either - delayed allocation requires significant pre-processing for allocation. Avoiding this by using preallocation just moves the processing load to IO completion which needs to issue transactions to mark the region written.
Good point, thanks.
Quoted 11 lines
Quoted 2 lines
So perfect
rq_affinity
should always benefit write IO?No, because the flusher thread gets to be CPU bound just writing pages, allocating blocks and submitting IO. It might take 5-10GB/s to get there (say a million dirty pages a second being processed by a single CPU), but that's the sort of storage subsystem XFS is capable of driving. IO completion time for such a workload is significant, too, so putting that on the same CPU as the flusher thread will slow things down by far more than gain from avoiding cacheline bouncing.
Quoted 3 lines
So super fast storage is going to demand multiple flushers per bdi. And once we run multiple flushers for one bdi, it will again be beneficial to schedule IO completion to the flusher CPU :)
Yes - that is where we want to get to with XFS. But we don't have multiple bdi-flusher thread support yet for any filesystem, so I think it will be a while before the we can ignore this issue...
Cheers,
Dave.>
Signature · 8 lines
Dave Chinner [email protected] -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 6 lines · Christoph Hellwig
Quoted 3 lines · Wu Fengguang
2) most writeback will be submitted by one per-bdi-flusher, so no worry of cache bouncing (this also means the per CPU counter error is normally bounded by the batch size)
What counter are we talking about exactly? Once
balanance_dirty_pages
bdi_stat(bdi, BDI_WRITTEN) introduced in this patch.
Quoted 3 lines
stops submitting I/O the per-bdi flusher thread will in fact be the only thing submitting writeback, unless you count direct invocations of
writeback_single_inode.
Right.
Thanks,
Fengguang
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
maintainerQuoted 23 lines · Dave Chinner
Quoted 22 lines · Jan Kara
Quoted 21 lines · Dave Chinner
Quoted 20 lines · Jan Kara
This patch changes
balance_dirty_pages()throttling so that the function does not submit writes on its own but rather waits for flusher thread to do enough writes. This has an advantage that we have a single source of IO allowing for better writeback locality. Also we do not have to reenter filesystems from a non-trivial context.The waiting is implemented as follows: Each BDI has a FIFO of wait requests. When
balance_dirty_pages()finds it needs to throttle the writer, it adds a request for writingwrite_chunkof pages to the FIFO and waits until the request is fulfilled or we drop below dirty limits. A flusher thread tracks amount of pages it has written and when enough pages are written since the first request in the FIFO became first, it wakes up the waiter and removes request from the FIFO (thus another request becomes first and flusher thread starts writing out on behalf of this request).Signed-off-by Jan Kara <[email protected]>
...
Quoted 19 lines
Quoted 12 lines
Quoted 8 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ bdi->wb_written_head = bdi_stat(bdi, BDI_WRITTEN) + wc->written;The resolution of the percpu counters is an issue here, I think. percpu counters update in batches of 32 counts per CPU.
wc->writtenis going to have a value of roughly 8 or 32 depending on whetherbdi->dirty_exceededis set or not. I note that you take this into account when checking dirty threshold limits, but it doesn't appear to be taken in to here.Hmm, are you sure about the number of pages? I think that the ratelimits
you speak about influence only how often we *check* the limits. Looking atsync_writeback_pages()I see:
static inline longsync_writeback_pages(unsigned long dirtied)Quoted 6 lines
{ if (dirtied < ratelimit_pages) dirtied = ratelimit_pages; return dirtied + dirtied / 2; }
Quoted 7 lines
It's declared as:
39 /*
40 * After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited 41 * will look to see if it needs to force writeback or throttling. 42 */43 static long
ratelimit_pages= 32;
Yeah, I know, it's a bit confusing...
Quoted 7 lines
Quoted 4 lines
So it returns at least
ratelimit_pages* 3/2. Nowratelimit_pagesis
computed inwriteback_set_ratelimit()and for most machines I expect the math there to end up withratelimit_pages== 1024. So we enterbalance_dirty_pageswith number 1536...But I missed this. That means we typically will check every 1024 pages until
dirty_exceededis set, then we'll check every 8 pages.
Yes, with dirty_exceeded we check every 8 pages but still require 1536
pages written.
Quoted 62 lines
Quoted 4 lines
That being said, the error of
percpu counters can still be significant - with 16 CPUs doing completion the average error is 384 (on yourmax_cpus== 512 machine - our distro kernels have that too).The error gets larger as the number of CPUs goes up - Peter pointed that out because:
#define
BDI_STAT_BATCH(8*(1+ilog2(nr_cpu_ids)))So for a system reporting
nr_cpu_ids= 32 (as my 8 core nehalem server currently reports), the error could be 16 * 48 = 768.Quoted 10 lines
Quoted 10 lines
That means for ratelimits of 8 pages on writes and only one CPU doing IO completion wakeups are only going to occur in completion batches of 32 pages. we'll end up with something like:
waiter, written count at head count when woken
A, 8 n n + 32
B, 8 n + 32 n + 64
C, 8 n + 64 n + 96
.....Quoted 7 lines
Quoted 6 lines
And so on. This isn't necessarily bad - we'll throttle for longer than we strictly need to - but the cumulative counter resolution error gets worse as the number of CPUs doing IO completion grows. Worst case ends up at for (num cpus * 31) + 1 pages of writeback for just the first waiter. For an arbitrary FIFO queue of depth d, the worst case is more like d * (num cpus * 31 + 1).
Hmm, I don't see how the error would depend on the FIFO depth.
It's the cumulative error that depends on the FIFO depth, not the error seen by a single waiter.
Quoted 4 lines
I'd rather
think that the number of written pages after which a thread is woken is a random variable equally distributed in a range
<wanted_nr-max_counter_err,wanted_nr+max_counter_err>.Right, that the error range seenby a single waiter. i.e. between
bdi->wb_written_head = bdi_stat(written) + wc->pages_to_wait_for;If we only want to wait for, say, 10 pages from a counter value of N, the per-cpu counter increments in batches of
BDI_STAT_BATCH. Hence the first time we seebdi_stat(written)passbdi->wb_written_headwill be when it reaches (N +BDI_STAT_BATCH). We will have waited (BDI_STAT_BATCH- 10) pages more than we needed to. i.e the wait time is a minimum ofroundup(pages_to_wait_for, BDI_STAT_BATCH).Quoted 5 lines
And over time
(or with more threads in the queue) these errors cancel out... Now as I wrote above the error for single waiter might be just too large to accept but I just wanted to argue here that I don't see how it would be getting worse with more waiters.That's where I think you go wrong - the error occurs when we remove the waiter at the head of the queue, so each subsequent waiter has a resolution error introduced. These will continue to aggregate as long as there are more waiters on the queue.
Quoted 10 lines
e.g if we've got 3 waiters of 10 pages each, they should all be woken after 30 pages have been cleared. Instead, the first is only woken after the counter ticks (
BDI_STAT_BATCH), then the second is queued, and it doesn't get woken for another counter tick. Similarly for the third waiter. When the third waiter is woken, it has waited for 3 *BDI_STAT_BATCHpages. In reality, what we realy wanted was all three waiters to be woken by the time 30 pages had been written back. i.e. the last waiter waited for (3 * (BDI_STAT_BATCH- 10)), which in general terms gives a wait overshoot for any given waiter of roughly (queue depth when queued *BDI_STAT_BATCH*nr_cpu_ids) pages.
Ah, you are right... Thanks for the correction.
Quoted 9 lines
However, if we calculate the wakeup time when we queue, each waiter ends up with a wakeup time of N + 10, N + 20, and N + 30, and the counter tick resolution is taken out of the picture. i.e. if the tick is 8, then the first wakeup occurs after 2 counter ticks, the second is woken after 3 counter ticks and the last is woken after 4 counter ticks. If the counter tick is >= 32, then all three will be woken on the first counter tick. In this case, the worst case wait overshoot for any waiter regardless of queue depth is (
BDI_STAT_BATCH*nr_cpu_ids) pages.
Yes.
Quoted 9 lines
Quoted 3 lines
if a system gets below dirty limits for a short while so that only a few threads notice is an arguable reason), you would need to recompute them or live with waiting unnecessarily long.
I'd suggest that we shouldn't be waking waiters prematurely if we've dropped below the dirty threshold - we need some hysteresis in the system to prevent oscillating rapidly around the threshold. If we are not issuing IO, then if we don't damp the wakeup frequency we could easily thrash around the threshold.
The current logic is that we do not wake up threads when the system
gets below limit. But threads wake up after a certain time passes, recheck dirty limits, and quit waiting if limits are not exceeded. This is quite similar to the old behavior and should provide a reasonably smooth start.
Quoted 3 lines
I'd prefer to strip down the mechanism to the simplest, most accurate form before trying to add "interesting" tweaks like faster wakeup semantics at dirty thresholds....
I am a bit reluctant to not stop waiting when the system is below dirty
limits for two reasons:
a) it's a bit more unfair because the thread has to wait longer (until
enough is written) while other threads happily perform writes because the system isn't above dirty limits anymore.
b) we require 1536 pages written regardless of how many pages were
dirtied. Now in theory, if there are say 8 threads writing, the threads would require 12288 pages written in total. But when a system has just 32768 pages, the dirty limit is set at 6553 pages so there even cannot be that many dirty pages in the system.
Of course, we could trim down the number of pages we wait for when we
aren't doing actual IO with new balance_dirty_pages() (which would reduce scope of the above problem). But still we shouldn't set it too low because all that checking, sleeping, and waking costs something so longer sleeps are beneficial from this POV. So all in all I would prefer to keep some 'backup' waking mechanism in place...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
mm/backing-dev.c static void sync_supers_timer_fn(unsigned long unused) · line 437
Quoted 7 lines · Jan Kara
mm/backing-dev.c +7
-0+/* +* Now we can wakeup the writer which frees wc entry +* The barrier is here so that woken task sees the +* modification of wc. +*/ +smp_wmb (); +__wake_up_locked (&bdi->wb_written_wait,TASK_NORMAL );
wakeups imply a wmb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
maintainerQuoted 10 lines · Peter Zijlstra
mm/backing-dev.c static void sync_supers_timer_fn(unsigned long unused) · line 437
Quoted 7 lines · Jan Kara
mm/backing-dev.c +7
-0+/* +* Now we can wakeup the writer which frees wc entry +* The barrier is here so that woken task sees the +* modification of wc. +*/ +smp_wmb (); +__wake_up_locked (&bdi->wb_written_wait,TASK_NORMAL );wakeups imply a wmb.
Thanks. Removed smp_wmb and updated comment.
Honza
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 41 lines · Jan Kara
mm/backing-dev.c +41
-0+/* Wait until write_chunk is written or we get below dirty limits */ +void bdi_wait_written (struct backing_dev_info *bdi,long write_chunk) +{ +struct bdi_written_count wc = { + .list =LIST_HEAD_INIT (wc.list), + .written = write_chunk, + }; +DECLARE_WAITQUEUE (wait, current); +int pause =1 ; + +bdi_add_writer (bdi, &wc, &wait); +for (;;) { +if (signal_pending_state (TASK_KILLABLE , current)) +break ; + +/* +* Make the task just killable so that tasks cannot circumvent +* throttling by sending themselves non-fatal signals... +*/ +__set_current_state (TASK_KILLABLE ); +io_schedule_timeout (pause); + +/* +* The following check is save without wb_written_wait.lock +* because once bdi_remove_writer removes us from the list +* noone will touch us and it's impossible for list_empty check +* to trigger as false positive. The barrier is there to avoid +* missing the wakeup when we are removed from the list. +*/ +smp_rmb (); +if (list_empty (&wc.list)) +break ; + +if (!dirty_limits_exceeded (bdi)) +break ; + +/* +* Increase the delay for each loop, up to our previous +* default of taking a 100ms nap. +*/ + pause <<=1 ;
mm/backing-dev.c static void sync_supers_timer_fn(unsigned long unused) · line 507
Quoted 10 lines
mm/backing-dev.c +10
-0+if (pause >HZ /10 ) + pause =HZ /10 ; + } + +spin_lock_irq (&bdi->wb_written_wait.lock); +__remove_wait_queue (&bdi->wb_written_wait, &wait); +if (!list_empty (&wc.list)) +bdi_remove_writer (bdi, &wc); +spin_unlock_irq (&bdi->wb_written_wait.lock); +}
OK, so the whole pause thing is simply because we don't get a wakeup when we drop below the limit, right?
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
maintainerQuoted 47 lines · Peter Zijlstra
Quoted 46 lines · Jan Kara
mm/backing-dev.c +46
-0+/* Wait until write_chunk is written or we get below dirty limits */ +void bdi_wait_written (struct backing_dev_info *bdi,long write_chunk) +{ +struct bdi_written_count wc = { + .list =LIST_HEAD_INIT (wc.list), + .written = write_chunk, + }; +DECLARE_WAITQUEUE (wait, current); +int pause =1 ; + +bdi_add_writer (bdi, &wc, &wait); +for (;;) { +if (signal_pending_state (TASK_KILLABLE , current)) +break ; + +/* +* Make the task just killable so that tasks cannot circumvent +* throttling by sending themselves non-fatal signals... +*/ +__set_current_state (TASK_KILLABLE ); +io_schedule_timeout (pause); + +/* +* The following check is save without wb_written_wait.lock +* because once bdi_remove_writer removes us from the list +* noone will touch us and it's impossible for list_empty check +* to trigger as false positive. The barrier is there to avoid +* missing the wakeup when we are removed from the list. +*/ +smp_rmb (); +if (list_empty (&wc.list)) +break ; + +if (!dirty_limits_exceeded (bdi)) +break ; + +/* +* Increase the delay for each loop, up to our previous +* default of taking a 100ms nap. +*/ + pause <<=1 ; +if (pause >HZ /10 ) + pause =HZ /10 ; + } + +spin_lock_irq (&bdi->wb_written_wait.lock);
Quoted 8 lines
mm/backing-dev.c static void sync_supers_timer_fn(unsigned long unused) · line 507
Quoted 5 lines
mm/backing-dev.c +5
-0+__remove_wait_queue (&bdi->wb_written_wait, &wait); +if (!list_empty (&wc.list)) +bdi_remove_writer (bdi, &wc); +spin_unlock_irq (&bdi->wb_written_wait.lock); +}OK, so the whole pause thing is simply because we don't get a wakeup when we drop below the limit, right?
Yes. I will write a comment about it before the loop. I was also thinking
about sending a wakeup when we get below limits but then all threads would start thundering the device at the same time and likely cause a congestion again. This way we might get a smoother start. But I'll have to measure whether we aren't too unfair with this approach...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
maintainerQuoted 56 lines · Jan Kara
Quoted 47 lines · Peter Zijlstra
Quoted 46 lines · Jan Kara
mm/backing-dev.c +46
-0+/* Wait until write_chunk is written or we get below dirty limits */ +void bdi_wait_written (struct backing_dev_info *bdi,long write_chunk) +{ +struct bdi_written_count wc = { + .list =LIST_HEAD_INIT (wc.list), + .written = write_chunk, + }; +DECLARE_WAITQUEUE (wait, current); +int pause =1 ; + +bdi_add_writer (bdi, &wc, &wait); +for (;;) { +if (signal_pending_state (TASK_KILLABLE , current)) +break ; + +/* +* Make the task just killable so that tasks cannot circumvent +* throttling by sending themselves non-fatal signals... +*/ +__set_current_state (TASK_KILLABLE ); +io_schedule_timeout (pause); + +/* +* The following check is save without wb_written_wait.lock +* because once bdi_remove_writer removes us from the list +* noone will touch us and it's impossible for list_empty check +* to trigger as false positive. The barrier is there to avoid +* missing the wakeup when we are removed from the list. +*/ +smp_rmb (); +if (list_empty (&wc.list)) +break ; + +if (!dirty_limits_exceeded (bdi)) +break ; + +/* +* Increase the delay for each loop, up to our previous +* default of taking a 100ms nap. +*/ + pause <<=1 ; +if (pause >HZ /10 ) + pause =HZ /10 ; + } + +spin_lock_irq (&bdi->wb_written_wait.lock);Quoted 8 lines
mm/backing-dev.c static void sync_supers_timer_fn(unsigned long unused) · line 507
Quoted 5 lines
mm/backing-dev.c +5
-0+__remove_wait_queue (&bdi->wb_written_wait, &wait); +if (!list_empty (&wc.list)) +bdi_remove_writer (bdi, &wc); +spin_unlock_irq (&bdi->wb_written_wait.lock); +}OK, so the whole pause thing is simply because we don't get a wakeup when we drop below the limit, right?
Quoted 5 lines
Yes. I will write a comment about it before the loop. I was also thinking
about sending a wakeup when we get below limits but then all threads would start thundering the device at the same time and likely cause a congestion again. This way we might get a smoother start. But I'll have to measure whether we aren't too unfair with this approach...
I just got an idea - if the sleeping is too unfair (as threads at the end
of the FIFO are likely to have 'pause' smaller and thus could find out earlier that the system is below dirty limits), we could share 'pause' among all threads waiting for that BDI. That way threads would wake up in a FIFO order...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 5 lines · Jan Kara
I just got an idea - if the sleeping is too unfair (as threads at the end
of the FIFO are likely to have 'pause' smaller and thus could find out earlier that the system is below dirty limits), we could share 'pause' among all threads waiting for that BDI. That way threads would wake up in a FIFO order...
Sounds sensible.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
mm/page-writeback.c int test_clear_page_writeback(struct page *page) · line 1251
Quoted 2 lines · Jan Kara
mm/page-writeback.c +2
-0+if (bdi_stat (bdi,BDI_WRITTEN ) >= bdi->wb_written_head) +bdi_wakeup_writers (bdi);
For the paranoid amongst us you could make wb_written_head s64 and write the above as:
if (bdi_stat(bdi, BDI_WRITTEN) - bdi->wb_written_head > 0)
Which, if you assume both are monotonic and wb_written_head is always within 2^63 of the actual bdi_stat() value, should give the same end result and deal with wrap-around.
For when we manage to create a device that can write 2^64 pages in our uptime :-)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Jan Kara
maintainerQuoted 7 lines · Peter Zijlstra
mm/page-writeback.c int test_clear_page_writeback(struct page *page) · line 1251
Quoted 2 lines · Jan Kara
mm/page-writeback.c +2
-0+if (bdi_stat (bdi,BDI_WRITTEN ) >= bdi->wb_written_head) +bdi_wakeup_writers (bdi);For the paranoid amongst us you could make
wb_written_heads64 and write the above as:
Quoted 8 lines
if (
bdi_stat(bdi, BDI_WRITTEN)-bdi->wb_written_head> 0)Which, if you assume both are monotonic and
wb_written_headis always within 2^63 of the actualbdi_stat()value, should give the same end result and deal with wrap-around.For when we manage to create a device that can write 2^64 pages in our uptime :-)
OK, the fix is simple enough so I've changed it, although I'm not
paranoic enough ;) (I actually did the math before writing that test).
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 10 lines · Jan Kara
Quoted 9 lines · Peter Zijlstra
mm/page-writeback.c int test_clear_page_writeback(struct page *page) · line 1251
Quoted 2 lines · Jan Kara
mm/page-writeback.c +2
-0+if (bdi_stat (bdi,BDI_WRITTEN ) >= bdi->wb_written_head) +bdi_wakeup_writers (bdi);For the paranoid amongst us you could make
wb_written_heads64 and write the above as:if (
bdi_stat(bdi, BDI_WRITTEN)-bdi->wb_written_head> 0)
Quoted 8 lines
Quoted 6 lines
Which, if you assume both are monotonic and
wb_written_headis always within 2^63 of the actualbdi_stat()value, should give the same end result and deal with wrap-around.For when we manage to create a device that can write 2^64 pages in our uptime :-)
OK, the fix is simple enough so I've changed it, although I'm not
paranoic enough ;) (I actually did the math before writing that test).
a bit more change :)
type:
- u64 wb_written_head
+ s64 wb_written_head
resetting:
- bdi->wb_written_head = ~(u64)0;
+ bdi->wb_written_head = 0;
setting:
bdi->wb_written_head = bdi_stat(bdi, BDI_WRITTEN) + wc->written;
+ bdi->wb_written_head |= 1;
testing:
if (bdi->wb_written_head &&
bdi_stat(bdi, BDI_WRITTEN) - bdi->wb_written_head > 0)This avoids calling into bdi_wakeup_writers() pointlessly when no one is being throttled (which is the normal case).
Thanks,
Fengguang
Jan Kara
maintainerQuoted 37 lines · Wu Fengguang
Quoted 10 lines · Jan Kara
Quoted 9 lines · Peter Zijlstra
mm/page-writeback.c int test_clear_page_writeback(struct page *page) · line 1251
Quoted 2 lines · Jan Kara
mm/page-writeback.c +2
-0+if (bdi_stat (bdi,BDI_WRITTEN ) >= bdi->wb_written_head) +bdi_wakeup_writers (bdi);For the paranoid amongst us you could make
wb_written_heads64 and write the above as:if (
bdi_stat(bdi, BDI_WRITTEN)-bdi->wb_written_head> 0)Quoted 8 lines
Quoted 6 lines
Which, if you assume both are monotonic and
wb_written_headis always within 2^63 of the actualbdi_stat()value, should give the same end result and deal with wrap-around.For when we manage to create a device that can write 2^64 pages in our uptime :-)
OK, the fix is simple enough so I've changed it, although I'm not
paranoic enough ;) (I actually did the math before writing that test).a bit more change :)
type:
(quoted diff) +1
-1- u64 wb_written_head + s64 wb_written_headresetting:
(quoted diff) +1
-1- bdi->wb_written_head = ~(u64)0; + bdi->wb_written_head = 0;setting:
bdi->wb_written_head = bdi_stat(bdi, BDI_WRITTEN) + wc->written;+
bdi->wb_written_head|= 1;
Quoted 7 lines
testing:
if (bdi->wb_written_head && bdi_stat(bdi, BDI_WRITTEN) - bdi->wb_written_head > 0)This avoids calling into
bdi_wakeup_writers()pointlessly when no one is being throttled (which is the normal case).
Actually, I've already changed wb_written_head to s64. I kept settingwb_written_head to s64 maximum. That also avoids calling into bdi_wakeup_writers() unnecessarily...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 40 lines · Jan Kara
Quoted 39 lines · Wu Fengguang
Quoted 10 lines · Jan Kara
Quoted 9 lines · Peter Zijlstra
mm/page-writeback.c int test_clear_page_writeback(struct page *page) · line 1251
Quoted 2 lines · Jan Kara
mm/page-writeback.c +2
-0+if (bdi_stat (bdi,BDI_WRITTEN ) >= bdi->wb_written_head) +bdi_wakeup_writers (bdi);For the paranoid amongst us you could make
wb_written_heads64 and write the above as:if (
bdi_stat(bdi, BDI_WRITTEN)-bdi->wb_written_head> 0)Quoted 8 lines
Quoted 6 lines
Which, if you assume both are monotonic and
wb_written_headis always within 2^63 of the actualbdi_stat()value, should give the same end result and deal with wrap-around.For when we manage to create a device that can write 2^64 pages in our uptime :-)
OK, the fix is simple enough so I've changed it, although I'm not
paranoic enough ;) (I actually did the math before writing that test).a bit more change :)
type:
(quoted diff) +1
-1- u64 wb_written_head + s64 wb_written_headresetting:
(quoted diff) +1
-1- bdi->wb_written_head = ~(u64)0; + bdi->wb_written_head = 0;setting:
bdi->wb_written_head = bdi_stat(bdi, BDI_WRITTEN) + wc->written;+
bdi->wb_written_head|= 1;testing:
Quoted 8 lines
Quoted 5 lines
if (bdi->wb_written_head && bdi_stat(bdi, BDI_WRITTEN) - bdi->wb_written_head > 0)This avoids calling into
bdi_wakeup_writers()pointlessly when no one is being throttled (which is the normal case).Actually, I've already changed
wb_written_headto s64. I kept settingwb_written_headto s64 maximum. That also avoids calling intobdi_wakeup_writers()unnecessarily...
Ah OK, I forgot bdi_stat() calls percpu_counter_read_positive() which is always in range [0, s64 max].
Thanks,
Fengguang
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>