Skip to content

Commit b9147dd

Browse files
shligitaxboe
authored andcommitted
blk-throttle: add a mechanism to estimate IO latency
User configures latency target, but the latency threshold for each request size isn't fixed. For a SSD, the IO latency highly depends on request size. To calculate latency threshold, we sample some data, eg, average latency for request size 4k, 8k, 16k, 32k .. 1M. The latency threshold of each request size will be the sample latency (I'll call it base latency) plus latency target. For example, the base latency for request size 4k is 80us and user configures latency target 60us. The 4k latency threshold will be 80 + 60 = 140us. To sample data, we calculate the order base 2 of rounded up IO sectors. If the IO size is bigger than 1M, it will be accounted as 1M. Since the calculation does round up, the base latency will be slightly smaller than actual value. Also if there isn't any IO dispatched for a specific IO size, we will use the base latency of smaller IO size for this IO size. But we shouldn't sample data at any time. The base latency is supposed to be latency where disk isn't congested, because we use latency threshold to schedule IOs between cgroups. If disk is congested, the latency is higher, using it for scheduling is meaningless. Hence we only do the sampling when block throttling is in the LOW limit, with assumption disk isn't congested in such state. If the assumption isn't true, eg, low limit is too high, calculated latency threshold will be higher. Hard disk is completely different. Latency depends on spindle seek instead of request size. Currently this feature is SSD only, we probably can use a fixed threshold like 4ms for hard disk though. Signed-off-by: Shaohua Li <shli@fb.com> Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent 88eeca4 commit b9147dd

5 files changed

Lines changed: 185 additions & 10 deletions

File tree

‎block/blk-stat.c‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99

1010
#include "blk-stat.h"
1111
#include "blk-mq.h"
12+
#include "blk.h"
1213

1314
#define BLK_RQ_STAT_BATCH 64
1415

1516
struct blk_queue_stats {
1617
struct list_head callbacks;
1718
spinlock_t lock;
19+
bool enable_accounting;
1820
};
1921

2022
unsigned int blk_stat_rq_ddir(const struct request *rq)
@@ -96,6 +98,8 @@ void blk_stat_add(struct request *rq)
9698

9799
value = now - blk_stat_time(&rq->issue_stat);
98100

101+
blk_throtl_stat_add(rq, value);
102+
99103
rcu_read_lock();
100104
list_for_each_entry_rcu(cb, &q->stats->callbacks, list) {
101105
if (blk_stat_is_active(cb)) {
@@ -190,7 +194,7 @@ void blk_stat_remove_callback(struct request_queue *q,
190194
{
191195
spin_lock(&q->stats->lock);
192196
list_del_rcu(&cb->list);
193-
if (list_empty(&q->stats->callbacks))
197+
if (list_empty(&q->stats->callbacks) && !q->stats->enable_accounting)
194198
clear_bit(QUEUE_FLAG_STATS, &q->queue_flags);
195199
spin_unlock(&q->stats->lock);
196200

@@ -215,6 +219,14 @@ void blk_stat_free_callback(struct blk_stat_callback *cb)
215219
}
216220
EXPORT_SYMBOL_GPL(blk_stat_free_callback);
217221

222+
void blk_stat_enable_accounting(struct request_queue *q)
223+
{
224+
spin_lock(&q->stats->lock);
225+
q->stats->enable_accounting = true;
226+
set_bit(QUEUE_FLAG_STATS, &q->queue_flags);
227+
spin_unlock(&q->stats->lock);
228+
}
229+
218230
struct blk_queue_stats *blk_alloc_queue_stats(void)
219231
{
220232
struct blk_queue_stats *stats;
@@ -225,6 +237,7 @@ struct blk_queue_stats *blk_alloc_queue_stats(void)
225237

226238
INIT_LIST_HEAD(&stats->callbacks);
227239
spin_lock_init(&stats->lock);
240+
stats->enable_accounting = false;
228241

229242
return stats;
230243
}

‎block/blk-stat.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ static inline void blk_stat_set_issue(struct blk_issue_stat *stat,
108108
(((u64)blk_capped_size(size)) << BLK_STAT_SIZE_SHIFT);
109109
}
110110

111+
/* record time/size info in request but not add a callback */
112+
void blk_stat_enable_accounting(struct request_queue *q);
113+
111114
/*
112115
* blk_stat_rq_ddir() - Bucket callback function for the request data direction.
113116
* @rq: Request.

‎block/blk-throttle.c‎

Lines changed: 161 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ static int throtl_quantum = 32;
2828
/* default latency target is 0, eg, guarantee IO latency by default */
2929
#define DFL_LATENCY_TARGET (0)
3030

31+
#define SKIP_LATENCY (((u64)1) << BLK_STAT_RES_SHIFT)
32+
3133
static struct blkcg_policy blkcg_policy_throtl;
3234

3335
/* A workqueue to queue throttle related work */
@@ -165,6 +167,19 @@ struct throtl_grp {
165167
unsigned long idletime_threshold; /* us */
166168
};
167169

170+
/* We measure latency for request size from <= 4k to >= 1M */
171+
#define LATENCY_BUCKET_SIZE 9
172+
173+
struct latency_bucket {
174+
unsigned long total_latency; /* ns / 1024 */
175+
int samples;
176+
};
177+
178+
struct avg_latency_bucket {
179+
unsigned long latency; /* ns / 1024 */
180+
bool valid;
181+
};
182+
168183
struct throtl_data
169184
{
170185
/* service tree for active throtl groups */
@@ -188,6 +203,13 @@ struct throtl_data
188203
unsigned long low_downgrade_time;
189204

190205
unsigned int scale;
206+
207+
struct latency_bucket tmp_buckets[LATENCY_BUCKET_SIZE];
208+
struct avg_latency_bucket avg_buckets[LATENCY_BUCKET_SIZE];
209+
struct latency_bucket __percpu *latency_buckets;
210+
unsigned long last_calculate_time;
211+
212+
bool track_bio_latency;
191213
};
192214

193215
static void throtl_pending_timer_fn(unsigned long arg);
@@ -306,6 +328,9 @@ static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw)
306328
return ret;
307329
}
308330

331+
#define request_bucket_index(sectors) \
332+
clamp_t(int, order_base_2(sectors) - 3, 0, LATENCY_BUCKET_SIZE - 1)
333+
309334
/**
310335
* throtl_log - log debug message via blktrace
311336
* @sq: the service_queue being reported
@@ -1931,6 +1956,73 @@ static void blk_throtl_update_idletime(struct throtl_grp *tg)
19311956
tg->checked_last_finish_time = last_finish_time;
19321957
}
19331958

1959+
#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
1960+
static void throtl_update_latency_buckets(struct throtl_data *td)
1961+
{
1962+
struct avg_latency_bucket avg_latency[LATENCY_BUCKET_SIZE];
1963+
int i, cpu;
1964+
unsigned long last_latency = 0;
1965+
unsigned long latency;
1966+
1967+
if (!blk_queue_nonrot(td->queue))
1968+
return;
1969+
if (time_before(jiffies, td->last_calculate_time + HZ))
1970+
return;
1971+
td->last_calculate_time = jiffies;
1972+
1973+
memset(avg_latency, 0, sizeof(avg_latency));
1974+
for (i = 0; i < LATENCY_BUCKET_SIZE; i++) {
1975+
struct latency_bucket *tmp = &td->tmp_buckets[i];
1976+
1977+
for_each_possible_cpu(cpu) {
1978+
struct latency_bucket *bucket;
1979+
1980+
/* this isn't race free, but ok in practice */
1981+
bucket = per_cpu_ptr(td->latency_buckets, cpu);
1982+
tmp->total_latency += bucket[i].total_latency;
1983+
tmp->samples += bucket[i].samples;
1984+
bucket[i].total_latency = 0;
1985+
bucket[i].samples = 0;
1986+
}
1987+
1988+
if (tmp->samples >= 32) {
1989+
int samples = tmp->samples;
1990+
1991+
latency = tmp->total_latency;
1992+
1993+
tmp->total_latency = 0;
1994+
tmp->samples = 0;
1995+
latency /= samples;
1996+
if (latency == 0)
1997+
continue;
1998+
avg_latency[i].latency = latency;
1999+
}
2000+
}
2001+
2002+
for (i = 0; i < LATENCY_BUCKET_SIZE; i++) {
2003+
if (!avg_latency[i].latency) {
2004+
if (td->avg_buckets[i].latency < last_latency)
2005+
td->avg_buckets[i].latency = last_latency;
2006+
continue;
2007+
}
2008+
2009+
if (!td->avg_buckets[i].valid)
2010+
latency = avg_latency[i].latency;
2011+
else
2012+
latency = (td->avg_buckets[i].latency * 7 +
2013+
avg_latency[i].latency) >> 3;
2014+
2015+
td->avg_buckets[i].latency = max(latency, last_latency);
2016+
td->avg_buckets[i].valid = true;
2017+
last_latency = td->avg_buckets[i].latency;
2018+
}
2019+
}
2020+
#else
2021+
static inline void throtl_update_latency_buckets(struct throtl_data *td)
2022+
{
2023+
}
2024+
#endif
2025+
19342026
bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
19352027
struct bio *bio)
19362028
{
@@ -1939,6 +2031,7 @@ bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
19392031
struct throtl_service_queue *sq;
19402032
bool rw = bio_data_dir(bio);
19412033
bool throttled = false;
2034+
struct throtl_data *td = tg->td;
19422035
int ret;
19432036

19442037
WARN_ON_ONCE(!rcu_read_lock_held());
@@ -1949,13 +2042,16 @@ bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
19492042

19502043
spin_lock_irq(q->queue_lock);
19512044

2045+
throtl_update_latency_buckets(td);
2046+
19522047
if (unlikely(blk_queue_bypass(q)))
19532048
goto out_unlock;
19542049

19552050
ret = bio_associate_current(bio);
19562051
#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
19572052
if (ret == 0 || ret == -EBUSY)
19582053
bio->bi_cg_private = tg;
2054+
blk_stat_set_issue(&bio->bi_issue_stat, bio_sectors(bio));
19592055
#endif
19602056
blk_throtl_update_idletime(tg);
19612057

@@ -1974,8 +2070,8 @@ bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
19742070
/* if above limits, break to queue */
19752071
if (!tg_may_dispatch(tg, bio, NULL)) {
19762072
tg->last_low_overflow_time[rw] = jiffies;
1977-
if (throtl_can_upgrade(tg->td, tg)) {
1978-
throtl_upgrade_state(tg->td);
2073+
if (throtl_can_upgrade(td, tg)) {
2074+
throtl_upgrade_state(td);
19792075
goto again;
19802076
}
19812077
break;
@@ -2019,7 +2115,7 @@ bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
20192115

20202116
tg->last_low_overflow_time[rw] = jiffies;
20212117

2022-
tg->td->nr_queued[rw]++;
2118+
td->nr_queued[rw]++;
20232119
throtl_add_bio_tg(bio, qn, tg);
20242120
throttled = true;
20252121

@@ -2044,20 +2140,67 @@ bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
20442140
*/
20452141
if (!throttled)
20462142
bio_clear_flag(bio, BIO_THROTTLED);
2143+
2144+
#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
2145+
if (throttled || !td->track_bio_latency)
2146+
bio->bi_issue_stat.stat |= SKIP_LATENCY;
2147+
#endif
20472148
return throttled;
20482149
}
20492150

20502151
#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
2152+
static void throtl_track_latency(struct throtl_data *td, sector_t size,
2153+
int op, unsigned long time)
2154+
{
2155+
struct latency_bucket *latency;
2156+
int index;
2157+
2158+
if (!td || td->limit_index != LIMIT_LOW || op != REQ_OP_READ ||
2159+
!blk_queue_nonrot(td->queue))
2160+
return;
2161+
2162+
index = request_bucket_index(size);
2163+
2164+
latency = get_cpu_ptr(td->latency_buckets);
2165+
latency[index].total_latency += time;
2166+
latency[index].samples++;
2167+
put_cpu_ptr(td->latency_buckets);
2168+
}
2169+
2170+
void blk_throtl_stat_add(struct request *rq, u64 time_ns)
2171+
{
2172+
struct request_queue *q = rq->q;
2173+
struct throtl_data *td = q->td;
2174+
2175+
throtl_track_latency(td, blk_stat_size(&rq->issue_stat),
2176+
req_op(rq), time_ns >> 10);
2177+
}
2178+
20512179
void blk_throtl_bio_endio(struct bio *bio)
20522180
{
20532181
struct throtl_grp *tg;
2182+
u64 finish_time_ns;
2183+
unsigned long finish_time;
2184+
unsigned long start_time;
2185+
unsigned long lat;
20542186

20552187
tg = bio->bi_cg_private;
20562188
if (!tg)
20572189
return;
20582190
bio->bi_cg_private = NULL;
20592191

2060-
tg->last_finish_time = ktime_get_ns() >> 10;
2192+
finish_time_ns = ktime_get_ns();
2193+
tg->last_finish_time = finish_time_ns >> 10;
2194+
2195+
start_time = blk_stat_time(&bio->bi_issue_stat) >> 10;
2196+
finish_time = __blk_stat_time(finish_time_ns) >> 10;
2197+
/* this is only for bio based driver */
2198+
if (start_time && finish_time > start_time &&
2199+
!(bio->bi_issue_stat.stat & SKIP_LATENCY)) {
2200+
lat = finish_time - start_time;
2201+
throtl_track_latency(tg->td, blk_stat_size(&bio->bi_issue_stat),
2202+
bio_op(bio), lat);
2203+
}
20612204
}
20622205
#endif
20632206

@@ -2133,6 +2276,12 @@ int blk_throtl_init(struct request_queue *q)
21332276
td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
21342277
if (!td)
21352278
return -ENOMEM;
2279+
td->latency_buckets = __alloc_percpu(sizeof(struct latency_bucket) *
2280+
LATENCY_BUCKET_SIZE, __alignof__(u64));
2281+
if (!td->latency_buckets) {
2282+
kfree(td);
2283+
return -ENOMEM;
2284+
}
21362285

21372286
INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
21382287
throtl_service_queue_init(&td->service_queue);
@@ -2147,8 +2296,10 @@ int blk_throtl_init(struct request_queue *q)
21472296

21482297
/* activate policy */
21492298
ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
2150-
if (ret)
2299+
if (ret) {
2300+
free_percpu(td->latency_buckets);
21512301
kfree(td);
2302+
}
21522303
return ret;
21532304
}
21542305

@@ -2157,6 +2308,7 @@ void blk_throtl_exit(struct request_queue *q)
21572308
BUG_ON(!q->td);
21582309
throtl_shutdown_wq(q);
21592310
blkcg_deactivate_policy(q, &blkcg_policy_throtl);
2311+
free_percpu(q->td->latency_buckets);
21602312
kfree(q->td);
21612313
}
21622314

@@ -2181,6 +2333,10 @@ void blk_throtl_register_queue(struct request_queue *q)
21812333
td->throtl_slice = DFL_THROTL_SLICE_HD;
21822334
#endif
21832335

2336+
td->track_bio_latency = !q->mq_ops && !q->request_fn;
2337+
if (!td->track_bio_latency)
2338+
blk_stat_enable_accounting(q);
2339+
21842340
/*
21852341
* some tg are created before queue is fully initialized, eg, nonrot
21862342
* isn't initialized yet

‎block/blk.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,10 @@ extern ssize_t blk_throtl_sample_time_show(struct request_queue *q, char *page);
331331
extern ssize_t blk_throtl_sample_time_store(struct request_queue *q,
332332
const char *page, size_t count);
333333
extern void blk_throtl_bio_endio(struct bio *bio);
334+
extern void blk_throtl_stat_add(struct request *rq, u64 time);
334335
#else
335336
static inline void blk_throtl_bio_endio(struct bio *bio) { }
337+
static inline void blk_throtl_stat_add(struct request *rq, u64 time) { }
336338
#endif
337339

338340
#endif /* BLK_INTERNAL_H */

‎include/linux/blk_types.h‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ struct io_context;
1717
struct cgroup_subsys_state;
1818
typedef void (bio_end_io_t) (struct bio *);
1919

20+
struct blk_issue_stat {
21+
u64 stat;
22+
};
23+
2024
/*
2125
* main unit of I/O for the block layer and lower layers (ie drivers and
2226
* stacking drivers)
@@ -60,6 +64,7 @@ struct bio {
6064
struct cgroup_subsys_state *bi_css;
6165
#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
6266
void *bi_cg_private;
67+
struct blk_issue_stat bi_issue_stat;
6368
#endif
6469
#endif
6570
union {
@@ -286,10 +291,6 @@ static inline bool blk_qc_t_is_internal(blk_qc_t cookie)
286291
return (cookie & BLK_QC_T_INTERNAL) != 0;
287292
}
288293

289-
struct blk_issue_stat {
290-
u64 stat;
291-
};
292-
293294
struct blk_rq_stat {
294295
s64 mean;
295296
u64 min;

0 commit comments

Comments
 (0)