Skip to content

Commit e98be2d

Browse files
author
Wu Fengguang
committed
writeback: bdi write bandwidth estimation
The estimation value will start from 100MB/s and adapt to the real bandwidth in seconds. It tries to update the bandwidth only when disk is fully utilized. Any inactive period of more than one second will be skipped. The estimated bandwidth will be reflecting how fast the device can writeout when _fully utilized_, and won't drop to 0 when it goes idle. The value will remain constant at disk idle time. At busy write time, if not considering fluctuations, it will also remain high unless be knocked down by possible concurrent reads that compete for the disk time and bandwidth with async writes. The estimation is not done purely in the flusher because there is no guarantee for write_cache_pages() to return timely to update bandwidth. The bdi->avg_write_bandwidth smoothing is very effective for filtering out sudden spikes, however may be a little biased in long term. The overheads are low because the bdi bandwidth update only occurs at 200ms intervals. The 200ms update interval is suitable, because it's not possible to get the real bandwidth for the instance at all, due to large fluctuations. The NFS commits can be as large as seconds worth of data. One XFS completion may be as large as half second worth of data if we are going to increase the write chunk to half second worth of data. In ext4, fluctuations with time period of around 5 seconds is observed. And there is another pattern of irregular periods of up to 20 seconds on SSD tests. That's why we are not only doing the estimation at 200ms intervals, but also averaging them over a period of 3 seconds and then go further to do another level of smoothing in avg_write_bandwidth. CC: Li Shaohua <shaohua.li@intel.com> CC: Peter Zijlstra <a.p.zijlstra@chello.nl> Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
1 parent f7d2b1e commit e98be2d

5 files changed

Lines changed: 120 additions & 0 deletions

File tree

‎fs/fs-writeback.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,16 @@ static inline bool over_bground_thresh(void)
692692
global_page_state(NR_UNSTABLE_NFS) > background_thresh);
693693
}
694694

695+
/*
696+
* Called under wb->list_lock. If there are multiple wb per bdi,
697+
* only the flusher working on the first wb should do it.
698+
*/
699+
static void wb_update_bandwidth(struct bdi_writeback *wb,
700+
unsigned long start_time)
701+
{
702+
__bdi_update_bandwidth(wb->bdi, start_time);
703+
}
704+
695705
/*
696706
* Explicit flushing or periodic writeback of "old" data.
697707
*
@@ -710,6 +720,7 @@ static inline bool over_bground_thresh(void)
710720
static long wb_writeback(struct bdi_writeback *wb,
711721
struct wb_writeback_work *work)
712722
{
723+
unsigned long wb_start = jiffies;
713724
long nr_pages = work->nr_pages;
714725
unsigned long oldest_jif;
715726
struct inode *inode;
@@ -758,6 +769,8 @@ static long wb_writeback(struct bdi_writeback *wb,
758769
progress = __writeback_inodes_wb(wb, work);
759770
trace_writeback_written(wb->bdi, work);
760771

772+
wb_update_bandwidth(wb, wb_start);
773+
761774
/*
762775
* Did we write something? Try for more
763776
*

‎include/linux/backing-dev.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ struct backing_dev_info {
7373

7474
struct percpu_counter bdi_stat[NR_BDI_STAT_ITEMS];
7575

76+
unsigned long bw_time_stamp; /* last time write bw is updated */
77+
unsigned long written_stamp; /* pages written at bw_time_stamp */
78+
unsigned long write_bandwidth; /* the estimated write bandwidth */
79+
unsigned long avg_write_bandwidth; /* further smoothed write bw */
80+
7681
struct prop_local_percpu completions;
7782
int dirty_exceeded;
7883

‎include/linux/writeback.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ void global_dirty_limits(unsigned long *pbackground, unsigned long *pdirty);
118118
unsigned long bdi_dirty_limit(struct backing_dev_info *bdi,
119119
unsigned long dirty);
120120

121+
void __bdi_update_bandwidth(struct backing_dev_info *bdi,
122+
unsigned long start_time);
123+
121124
void page_writeback_init(void);
122125
void balance_dirty_pages_ratelimited_nr(struct address_space *mapping,
123126
unsigned long nr_pages_dirtied);

‎mm/backing-dev.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,11 @@ static void bdi_wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi)
638638
setup_timer(&wb->wakeup_timer, wakeup_timer_fn, (unsigned long)bdi);
639639
}
640640

641+
/*
642+
* Initial write bandwidth: 100 MB/s
643+
*/
644+
#define INIT_BW (100 << (20 - PAGE_SHIFT))
645+
641646
int bdi_init(struct backing_dev_info *bdi)
642647
{
643648
int i, err;
@@ -660,6 +665,13 @@ int bdi_init(struct backing_dev_info *bdi)
660665
}
661666

662667
bdi->dirty_exceeded = 0;
668+
669+
bdi->bw_time_stamp = jiffies;
670+
bdi->written_stamp = 0;
671+
672+
bdi->write_bandwidth = INIT_BW;
673+
bdi->avg_write_bandwidth = INIT_BW;
674+
663675
err = prop_local_init_percpu(&bdi->completions);
664676

665677
if (err) {

‎mm/page-writeback.c‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
#include <linux/pagevec.h>
3737
#include <trace/events/writeback.h>
3838

39+
/*
40+
* Estimate write bandwidth at 200ms intervals.
41+
*/
42+
#define BANDWIDTH_INTERVAL max(HZ/5, 1)
43+
3944
/*
4045
* After a CPU has dirtied this many pages, balance_dirty_pages_ratelimited
4146
* will look to see if it needs to force writeback or throttling.
@@ -471,6 +476,85 @@ unsigned long bdi_dirty_limit(struct backing_dev_info *bdi, unsigned long dirty)
471476
return bdi_dirty;
472477
}
473478

479+
static void bdi_update_write_bandwidth(struct backing_dev_info *bdi,
480+
unsigned long elapsed,
481+
unsigned long written)
482+
{
483+
const unsigned long period = roundup_pow_of_two(3 * HZ);
484+
unsigned long avg = bdi->avg_write_bandwidth;
485+
unsigned long old = bdi->write_bandwidth;
486+
u64 bw;
487+
488+
/*
489+
* bw = written * HZ / elapsed
490+
*
491+
* bw * elapsed + write_bandwidth * (period - elapsed)
492+
* write_bandwidth = ---------------------------------------------------
493+
* period
494+
*/
495+
bw = written - bdi->written_stamp;
496+
bw *= HZ;
497+
if (unlikely(elapsed > period)) {
498+
do_div(bw, elapsed);
499+
avg = bw;
500+
goto out;
501+
}
502+
bw += (u64)bdi->write_bandwidth * (period - elapsed);
503+
bw >>= ilog2(period);
504+
505+
/*
506+
* one more level of smoothing, for filtering out sudden spikes
507+
*/
508+
if (avg > old && old >= (unsigned long)bw)
509+
avg -= (avg - old) >> 3;
510+
511+
if (avg < old && old <= (unsigned long)bw)
512+
avg += (old - avg) >> 3;
513+
514+
out:
515+
bdi->write_bandwidth = bw;
516+
bdi->avg_write_bandwidth = avg;
517+
}
518+
519+
void __bdi_update_bandwidth(struct backing_dev_info *bdi,
520+
unsigned long start_time)
521+
{
522+
unsigned long now = jiffies;
523+
unsigned long elapsed = now - bdi->bw_time_stamp;
524+
unsigned long written;
525+
526+
/*
527+
* rate-limit, only update once every 200ms.
528+
*/
529+
if (elapsed < BANDWIDTH_INTERVAL)
530+
return;
531+
532+
written = percpu_counter_read(&bdi->bdi_stat[BDI_WRITTEN]);
533+
534+
/*
535+
* Skip quiet periods when disk bandwidth is under-utilized.
536+
* (at least 1s idle time between two flusher runs)
537+
*/
538+
if (elapsed > HZ && time_before(bdi->bw_time_stamp, start_time))
539+
goto snapshot;
540+
541+
bdi_update_write_bandwidth(bdi, elapsed, written);
542+
543+
snapshot:
544+
bdi->written_stamp = written;
545+
bdi->bw_time_stamp = now;
546+
}
547+
548+
static void bdi_update_bandwidth(struct backing_dev_info *bdi,
549+
unsigned long start_time)
550+
{
551+
if (time_is_after_eq_jiffies(bdi->bw_time_stamp + BANDWIDTH_INTERVAL))
552+
return;
553+
spin_lock(&bdi->wb.list_lock);
554+
__bdi_update_bandwidth(bdi, start_time);
555+
spin_unlock(&bdi->wb.list_lock);
556+
}
557+
474558
/*
475559
* balance_dirty_pages() must be called by processes which are generating dirty
476560
* data. It looks at the number of dirty pages in the machine and will force
@@ -490,6 +574,7 @@ static void balance_dirty_pages(struct address_space *mapping,
490574
unsigned long pause = 1;
491575
bool dirty_exceeded = false;
492576
struct backing_dev_info *bdi = mapping->backing_dev_info;
577+
unsigned long start_time = jiffies;
493578

494579
for (;;) {
495580
nr_reclaimable = global_page_state(NR_FILE_DIRTY) +
@@ -544,6 +629,8 @@ static void balance_dirty_pages(struct address_space *mapping,
544629
if (!bdi->dirty_exceeded)
545630
bdi->dirty_exceeded = 1;
546631

632+
bdi_update_bandwidth(bdi, start_time);
633+
547634
/* Note: nr_reclaimable denotes nr_dirty + nr_unstable.
548635
* Unstable writes are a feature of certain networked
549636
* filesystems (i.e. NFS) in which data may have been

0 commit comments

Comments
 (0)