Skip to content

Commit 1d316e6

Browse files
mlyleaxboe
authored andcommitted
bcache: implement PI controller for writeback rate
bcache uses a control system to attempt to keep the amount of dirty data in cache at a user-configured level, while not responding excessively to transients and variations in write rate. Previously, the system was a PD controller; but the output from it was integrated, turning the Proportional term into an Integral term, and turning the Derivative term into a crude Proportional term. Performance of the controller has been uneven in production, and it has tended to respond slowly, oscillate, and overshoot. This patch set replaces the current control system with an explicit PI controller and tuning that should be correct for most hardware. By default, it attempts to write at a rate that would retire 1/40th of the current excess blocks per second. An integral term in turn works to remove steady state errors. IMO, this yields benefits in simplicity (removing weighted average filtering, etc) and system performance. Another small change is a tunable parameter is introduced to allow the user to specify a minimum rate at which dirty blocks are retired. There is a slight difference from earlier versions of the patch in integral handling to prevent excessive negative integral windup. Signed-off-by: Michael Lyle <mlyle@lyle.org> Reviewed-by: Coly Li <colyli@suse.de> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 5fa89fb commit 1d316e6

3 files changed

Lines changed: 66 additions & 52 deletions

File tree

‎drivers/md/bcache/bcache.h‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,6 @@ struct bcache_device {
265265
atomic_t *stripe_sectors_dirty;
266266
unsigned long *full_dirty_stripes;
267267

268-
unsigned long sectors_dirty_last;
269-
long sectors_dirty_derivative;
270-
271268
struct bio_set *bio_split;
272269

273270
unsigned data_csum:1;
@@ -362,12 +359,14 @@ struct cached_dev {
362359

363360
uint64_t writeback_rate_target;
364361
int64_t writeback_rate_proportional;
365-
int64_t writeback_rate_derivative;
362+
int64_t writeback_rate_integral;
363+
int64_t writeback_rate_integral_scaled;
366364
int64_t writeback_rate_change;
367365

368366
unsigned writeback_rate_update_seconds;
369-
unsigned writeback_rate_d_term;
367+
unsigned writeback_rate_i_term_inverse;
370368
unsigned writeback_rate_p_term_inverse;
369+
unsigned writeback_rate_minimum;
371370
};
372371

373372
enum alloc_reserve {

‎drivers/md/bcache/sysfs.c‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ rw_attribute(writeback_delay);
8181
rw_attribute(writeback_rate);
8282

8383
rw_attribute(writeback_rate_update_seconds);
84-
rw_attribute(writeback_rate_d_term);
84+
rw_attribute(writeback_rate_i_term_inverse);
8585
rw_attribute(writeback_rate_p_term_inverse);
86+
rw_attribute(writeback_rate_minimum);
8687
read_attribute(writeback_rate_debug);
8788

8889
read_attribute(stripe_size);
@@ -130,23 +131,24 @@ SHOW(__bch_cached_dev)
130131
sysfs_hprint(writeback_rate, dc->writeback_rate.rate << 9);
131132

132133
var_print(writeback_rate_update_seconds);
133-
var_print(writeback_rate_d_term);
134+
var_print(writeback_rate_i_term_inverse);
134135
var_print(writeback_rate_p_term_inverse);
136+
var_print(writeback_rate_minimum);
135137

136138
if (attr == &sysfs_writeback_rate_debug) {
137139
char rate[20];
138140
char dirty[20];
139141
char target[20];
140142
char proportional[20];
141-
char derivative[20];
143+
char integral[20];
142144
char change[20];
143145
s64 next_io;
144146

145147
bch_hprint(rate, dc->writeback_rate.rate << 9);
146148
bch_hprint(dirty, bcache_dev_sectors_dirty(&dc->disk) << 9);
147149
bch_hprint(target, dc->writeback_rate_target << 9);
148150
bch_hprint(proportional,dc->writeback_rate_proportional << 9);
149-
bch_hprint(derivative, dc->writeback_rate_derivative << 9);
151+
bch_hprint(integral, dc->writeback_rate_integral_scaled << 9);
150152
bch_hprint(change, dc->writeback_rate_change << 9);
151153

152154
next_io = div64_s64(dc->writeback_rate.next - local_clock(),
@@ -157,11 +159,11 @@ SHOW(__bch_cached_dev)
157159
"dirty:\t\t%s\n"
158160
"target:\t\t%s\n"
159161
"proportional:\t%s\n"
160-
"derivative:\t%s\n"
162+
"integral:\t%s\n"
161163
"change:\t\t%s/sec\n"
162164
"next io:\t%llims\n",
163165
rate, dirty, target, proportional,
164-
derivative, change, next_io);
166+
integral, change, next_io);
165167
}
166168

167169
sysfs_hprint(dirty_data,
@@ -213,7 +215,7 @@ STORE(__cached_dev)
213215
dc->writeback_rate.rate, 1, INT_MAX);
214216

215217
d_strtoul_nonzero(writeback_rate_update_seconds);
216-
d_strtoul(writeback_rate_d_term);
218+
d_strtoul(writeback_rate_i_term_inverse);
217219
d_strtoul_nonzero(writeback_rate_p_term_inverse);
218220

219221
d_strtoi_h(sequential_cutoff);
@@ -319,7 +321,7 @@ static struct attribute *bch_cached_dev_files[] = {
319321
&sysfs_writeback_percent,
320322
&sysfs_writeback_rate,
321323
&sysfs_writeback_rate_update_seconds,
322-
&sysfs_writeback_rate_d_term,
324+
&sysfs_writeback_rate_i_term_inverse,
323325
&sysfs_writeback_rate_p_term_inverse,
324326
&sysfs_writeback_rate_debug,
325327
&sysfs_dirty_data,

‎drivers/md/bcache/writeback.c‎

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,48 +25,62 @@ static void __update_writeback_rate(struct cached_dev *dc)
2525
bcache_flash_devs_sectors_dirty(c);
2626
uint64_t cache_dirty_target =
2727
div_u64(cache_sectors * dc->writeback_percent, 100);
28-
2928
int64_t target = div64_u64(cache_dirty_target * bdev_sectors(dc->bdev),
3029
c->cached_dev_sectors);
3130

32-
/* PD controller */
33-
31+
/*
32+
* PI controller:
33+
* Figures out the amount that should be written per second.
34+
*
35+
* First, the error (number of sectors that are dirty beyond our
36+
* target) is calculated. The error is accumulated (numerically
37+
* integrated).
38+
*
39+
* Then, the proportional value and integral value are scaled
40+
* based on configured values. These are stored as inverses to
41+
* avoid fixed point math and to make configuration easy-- e.g.
42+
* the default value of 40 for writeback_rate_p_term_inverse
43+
* attempts to write at a rate that would retire all the dirty
44+
* blocks in 40 seconds.
45+
*
46+
* The writeback_rate_i_inverse value of 10000 means that 1/10000th
47+
* of the error is accumulated in the integral term per second.
48+
* This acts as a slow, long-term average that is not subject to
49+
* variations in usage like the p term.
50+
*/
3451
int64_t dirty = bcache_dev_sectors_dirty(&dc->disk);
35-
int64_t derivative = dirty - dc->disk.sectors_dirty_last;
36-
int64_t proportional = dirty - target;
37-
int64_t change;
38-
39-
dc->disk.sectors_dirty_last = dirty;
40-
41-
/* Scale to sectors per second */
42-
43-
proportional *= dc->writeback_rate_update_seconds;
44-
proportional = div_s64(proportional, dc->writeback_rate_p_term_inverse);
45-
46-
derivative = div_s64(derivative, dc->writeback_rate_update_seconds);
47-
48-
derivative = ewma_add(dc->disk.sectors_dirty_derivative, derivative,
49-
(dc->writeback_rate_d_term /
50-
dc->writeback_rate_update_seconds) ?: 1, 0);
51-
52-
derivative *= dc->writeback_rate_d_term;
53-
derivative = div_s64(derivative, dc->writeback_rate_p_term_inverse);
54-
55-
change = proportional + derivative;
52+
int64_t error = dirty - target;
53+
int64_t proportional_scaled =
54+
div_s64(error, dc->writeback_rate_p_term_inverse);
55+
int64_t integral_scaled, new_rate;
56+
57+
if ((error < 0 && dc->writeback_rate_integral > 0) ||
58+
(error > 0 && time_before64(local_clock(),
59+
dc->writeback_rate.next + NSEC_PER_MSEC))) {
60+
/*
61+
* Only decrease the integral term if it's more than
62+
* zero. Only increase the integral term if the device
63+
* is keeping up. (Don't wind up the integral
64+
* ineffectively in either case).
65+
*
66+
* It's necessary to scale this by
67+
* writeback_rate_update_seconds to keep the integral
68+
* term dimensioned properly.
69+
*/
70+
dc->writeback_rate_integral += error *
71+
dc->writeback_rate_update_seconds;
72+
}
5673

57-
/* Don't increase writeback rate if the device isn't keeping up */
58-
if (change > 0 &&
59-
time_after64(local_clock(),
60-
dc->writeback_rate.next + NSEC_PER_MSEC))
61-
change = 0;
74+
integral_scaled = div_s64(dc->writeback_rate_integral,
75+
dc->writeback_rate_i_term_inverse);
6276

63-
dc->writeback_rate.rate =
64-
clamp_t(int64_t, (int64_t) dc->writeback_rate.rate + change,
65-
1, NSEC_PER_MSEC);
77+
new_rate = clamp_t(int64_t, (proportional_scaled + integral_scaled),
78+
dc->writeback_rate_minimum, NSEC_PER_MSEC);
6679

67-
dc->writeback_rate_proportional = proportional;
68-
dc->writeback_rate_derivative = derivative;
69-
dc->writeback_rate_change = change;
80+
dc->writeback_rate_proportional = proportional_scaled;
81+
dc->writeback_rate_integral_scaled = integral_scaled;
82+
dc->writeback_rate_change = new_rate - dc->writeback_rate.rate;
83+
dc->writeback_rate.rate = new_rate;
7084
dc->writeback_rate_target = target;
7185
}
7286

@@ -499,8 +513,6 @@ void bch_sectors_dirty_init(struct bcache_device *d)
499513

500514
bch_btree_map_keys(&op.op, d->c, &KEY(op.inode, 0, 0),
501515
sectors_dirty_init_fn, 0);
502-
503-
d->sectors_dirty_last = bcache_dev_sectors_dirty(d);
504516
}
505517

506518
void bch_cached_dev_writeback_init(struct cached_dev *dc)
@@ -514,10 +526,11 @@ void bch_cached_dev_writeback_init(struct cached_dev *dc)
514526
dc->writeback_percent = 10;
515527
dc->writeback_delay = 30;
516528
dc->writeback_rate.rate = 1024;
529+
dc->writeback_rate_minimum = 1;
517530

518531
dc->writeback_rate_update_seconds = 5;
519-
dc->writeback_rate_d_term = 30;
520-
dc->writeback_rate_p_term_inverse = 6000;
532+
dc->writeback_rate_p_term_inverse = 40;
533+
dc->writeback_rate_i_term_inverse = 10000;
521534

522535
INIT_DELAYED_WORK(&dc->writeback_rate_update, update_writeback_rate);
523536
}

0 commit comments

Comments
 (0)