Lines Matching +full:step +full:- +full:down

1 // SPDX-License-Identifier: GPL-2.0
6 * - Monitor latencies in a defined window of time.
7 * - If the minimum latency in the above window exceeds some target, increment
8 * scaling step and scale down queue depth by a factor of 2x. The monitoring
9 * window is then shrunk to 100 / sqrt(scaling step + 1).
10 * - For any window where we don't have solid data on what the latencies
12 * - If latencies look good, decrement scaling step.
13 * - If we're only doing writes, allow the scaling step to go negative. This
15 * scaling step of 0 if reads show up or the heavy writers finish. Unlike
17 * scaling step retains the default step==0 window size.
25 #include <linux/backing-dev.h>
28 #include "blk-wbt.h"
29 #include "blk-rq-qos.h"
36 rq->wbt_flags = 0; in wbt_clear_state()
41 return rq->wbt_flags; in wbt_flags()
46 return rq->wbt_flags & WBT_TRACKED; in wbt_is_tracked()
51 return rq->wbt_flags & WBT_READ; in wbt_is_read()
56 * Default setting, we'll scale up (to 75% of QD max) or down (min 1)
73 * information to scale up or down, scale up.
80 return rwb && rwb->enable_state != WBT_STATE_OFF_DEFAULT && in rwb_enabled()
81 rwb->wb_normal != 0; in rwb_enabled()
100 struct bdi_writeback *wb = &rwb->rqos.q->disk->bdi->wb; in wb_recent_wait()
102 return time_before(jiffies, wb->dirty_sleep + HZ); in wb_recent_wait()
109 return &rwb->rq_wait[WBT_RWQ_KSWAPD]; in get_rq_wait()
111 return &rwb->rq_wait[WBT_RWQ_DISCARD]; in get_rq_wait()
113 return &rwb->rq_wait[WBT_RWQ_BG]; in get_rq_wait()
121 struct rq_wait *rqw = &rwb->rq_wait[i]; in rwb_wake_all()
123 if (wq_has_sleeper(&rqw->wait)) in rwb_wake_all()
124 wake_up_all(&rqw->wait); in rwb_wake_all()
133 inflight = atomic_dec_return(&rqw->inflight); in wbt_rqw_done()
146 * the device does write back caching, drop further down before we in wbt_rqw_done()
150 limit = rwb->wb_background; in wbt_rqw_done()
151 else if (rwb->wc && !wb_recent_wait(rwb)) in wbt_rqw_done()
154 limit = rwb->wb_normal; in wbt_rqw_done()
162 if (wq_has_sleeper(&rqw->wait)) { in wbt_rqw_done()
163 int diff = limit - inflight; in wbt_rqw_done()
165 if (!inflight || diff >= rwb->wb_background / 2) in wbt_rqw_done()
166 wake_up_all(&rqw->wait); in wbt_rqw_done()
191 if (rwb->sync_cookie == rq) { in wbt_done()
192 rwb->sync_issue = 0; in wbt_done()
193 rwb->sync_cookie = NULL; in wbt_done()
197 wb_timestamp(rwb, &rwb->last_comp); in wbt_done()
199 WARN_ON_ONCE(rq == rwb->sync_cookie); in wbt_done()
219 u64 now, issue = READ_ONCE(rwb->sync_issue); in rwb_sync_issue_lat()
221 if (!issue || !rwb->sync_cookie) in rwb_sync_issue_lat()
225 return now - issue; in rwb_sync_issue_lat()
237 struct backing_dev_info *bdi = rwb->rqos.q->disk->bdi; in latency_exceeded()
238 struct rq_depth *rqd = &rwb->rq_depth; in latency_exceeded()
251 if (thislat > rwb->cur_win_nsec || in latency_exceeded()
252 (thislat > rwb->min_lat_nsec && !stat[READ].nr_samples)) { in latency_exceeded()
274 * If the 'min' latency exceeds our target, step down. in latency_exceeded()
276 if (stat[READ].min > rwb->min_lat_nsec) { in latency_exceeded()
282 if (rqd->scale_step) in latency_exceeded()
290 struct backing_dev_info *bdi = rwb->rqos.q->disk->bdi; in rwb_trace_step()
291 struct rq_depth *rqd = &rwb->rq_depth; in rwb_trace_step()
293 trace_wbt_step(bdi, msg, rqd->scale_step, rwb->cur_win_nsec, in rwb_trace_step()
294 rwb->wb_background, rwb->wb_normal, rqd->max_depth); in rwb_trace_step()
299 if (rwb->min_lat_nsec == 0) { in calc_wb_limits()
300 rwb->wb_normal = rwb->wb_background = 0; in calc_wb_limits()
301 } else if (rwb->rq_depth.max_depth <= 2) { in calc_wb_limits()
302 rwb->wb_normal = rwb->rq_depth.max_depth; in calc_wb_limits()
303 rwb->wb_background = 1; in calc_wb_limits()
305 rwb->wb_normal = (rwb->rq_depth.max_depth + 1) / 2; in calc_wb_limits()
306 rwb->wb_background = (rwb->rq_depth.max_depth + 3) / 4; in calc_wb_limits()
312 if (!rq_depth_scale_up(&rwb->rq_depth)) in scale_up()
315 rwb->unknown_cnt = 0; in scale_up()
322 if (!rq_depth_scale_down(&rwb->rq_depth, hard_throttle)) in scale_down()
325 rwb->unknown_cnt = 0; in scale_down()
326 rwb_trace_step(rwb, tracepoint_string("scale down")); in scale_down()
331 struct rq_depth *rqd = &rwb->rq_depth; in rwb_arm_timer()
333 if (rqd->scale_step > 0) { in rwb_arm_timer()
340 rwb->cur_win_nsec = div_u64(rwb->win_nsec << 4, in rwb_arm_timer()
341 int_sqrt((rqd->scale_step + 1) << 8)); in rwb_arm_timer()
344 * For step < 0, we don't want to increase/decrease the in rwb_arm_timer()
347 rwb->cur_win_nsec = rwb->win_nsec; in rwb_arm_timer()
350 blk_stat_activate_nsecs(rwb->cb, rwb->cur_win_nsec); in rwb_arm_timer()
355 struct rq_wb *rwb = cb->data; in wb_timer_fn()
356 struct rq_depth *rqd = &rwb->rq_depth; in wb_timer_fn()
360 if (!rwb->rqos.q->disk) in wb_timer_fn()
363 status = latency_exceeded(rwb, cb->stat); in wb_timer_fn()
365 trace_wbt_timer(rwb->rqos.q->disk->bdi, status, rqd->scale_step, in wb_timer_fn()
369 * If we exceeded the latency target, step down. If we did not, in wb_timer_fn()
370 * step one level up. If we don't know enough to say either exceeded in wb_timer_fn()
382 * We started a the center step, but don't have a valid in wb_timer_fn()
384 * Allow step to go negative, to increase write perf. in wb_timer_fn()
389 if (++rwb->unknown_cnt < RWB_UNKNOWN_BUMP) in wb_timer_fn()
394 * case, slowly return to center state (step == 0). in wb_timer_fn()
396 if (rqd->scale_step > 0) in wb_timer_fn()
398 else if (rqd->scale_step < 0) in wb_timer_fn()
406 * Re-arm timer, if we have IO in flight in wb_timer_fn()
408 if (rqd->scale_step || inflight) in wb_timer_fn()
414 struct rq_depth *rqd = &rwb->rq_depth; in wbt_update_limits()
416 rqd->scale_step = 0; in wbt_update_limits()
417 rqd->scaled_max = false; in wbt_update_limits()
430 return RQWB(rqos)->min_lat_nsec; in wbt_get_min_lat()
438 RQWB(rqos)->min_lat_nsec = val; in wbt_set_min_lat()
439 RQWB(rqos)->enable_state = WBT_STATE_ON_MANUAL; in wbt_set_min_lat()
448 return time_before(now, rwb->last_issue + HZ / 10) || in close_io()
449 time_before(now, rwb->last_comp + HZ / 10); in close_io()
466 return rwb->wb_background; in get_limit()
477 limit = rwb->rq_depth.max_depth; in get_limit()
483 limit = rwb->wb_background; in get_limit()
485 limit = rwb->wb_normal; in get_limit()
499 return rq_wait_inc_below(rqw, get_limit(data->rwb, data->opf)); in wbt_inflight_cb()
505 wbt_rqw_done(data->rwb, rqw, data->wb_acct); in wbt_cleanup_cb()
532 if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) == in wbt_should_throttle()
572 * If we do sleep, we'll release and re-grab it.
582 wb_timestamp(rwb, &rwb->last_issue); in wbt_wait()
586 __wbt_wait(rwb, flags, bio->bi_opf); in wbt_wait()
588 if (!blk_stat_is_active(rwb->cb)) in wbt_wait()
595 rq->wbt_flags |= bio_to_wbt_flags(rwb, bio); in wbt_track()
612 if (wbt_is_read(rq) && !rwb->sync_issue) { in wbt_issue()
613 rwb->sync_cookie = rq; in wbt_issue()
614 rwb->sync_issue = rq->io_start_time_ns; in wbt_issue()
623 if (rq == rwb->sync_cookie) { in wbt_requeue()
624 rwb->sync_issue = 0; in wbt_requeue()
625 rwb->sync_cookie = NULL; in wbt_requeue()
633 RQWB(rqos)->wc = write_cache_on; in wbt_set_write_cache()
645 if (RQWB(rqos)->enable_state == WBT_STATE_OFF_DEFAULT) in wbt_enable_default()
646 RQWB(rqos)->enable_state = WBT_STATE_ON_DEFAULT; in wbt_enable_default()
650 /* Queue not registered? Maybe shutting down... */ in wbt_enable_default()
662 * We default to 2msec for non-rotational storage, and 75msec in wbt_default_latency_nsec()
681 return -1; in wbt_data_dir()
686 RQWB(rqos)->rq_depth.queue_depth = blk_queue_depth(rqos->q); in wbt_queue_depth_changed()
693 struct request_queue *q = rqos->q; in wbt_exit()
695 blk_stat_remove_callback(q, rwb->cb); in wbt_exit()
696 blk_stat_free_callback(rwb->cb); in wbt_exit()
710 if (rwb->enable_state == WBT_STATE_ON_DEFAULT) { in wbt_disable_default()
711 blk_stat_deactivate(rwb->cb); in wbt_disable_default()
712 rwb->enable_state = WBT_STATE_OFF_DEFAULT; in wbt_disable_default()
723 seq_printf(m, "%llu\n", rwb->cur_win_nsec); in wbt_curr_win_nsec_show()
732 seq_printf(m, "%d\n", rwb->enable_state); in wbt_enabled_show()
740 seq_printf(m, "%u\n", rqos->id); in wbt_id_show()
752 atomic_read(&rwb->rq_wait[i].inflight)); in wbt_inflight_show()
761 seq_printf(m, "%lu\n", rwb->min_lat_nsec); in wbt_min_lat_nsec_show()
770 seq_printf(m, "%u\n", rwb->unknown_cnt); in wbt_unknown_cnt_show()
779 seq_printf(m, "%u\n", rwb->wb_normal); in wbt_normal_show()
788 seq_printf(m, "%u\n", rwb->wb_background); in wbt_background_show()
827 return -ENOMEM; in wbt_init()
829 rwb->cb = blk_stat_alloc_callback(wb_timer_fn, wbt_data_dir, 2, rwb); in wbt_init()
830 if (!rwb->cb) { in wbt_init()
832 return -ENOMEM; in wbt_init()
836 rq_wait_init(&rwb->rq_wait[i]); in wbt_init()
838 rwb->rqos.id = RQ_QOS_WBT; in wbt_init()
839 rwb->rqos.ops = &wbt_rqos_ops; in wbt_init()
840 rwb->rqos.q = q; in wbt_init()
841 rwb->last_comp = rwb->last_issue = jiffies; in wbt_init()
842 rwb->win_nsec = RWB_WINDOW_NSEC; in wbt_init()
843 rwb->enable_state = WBT_STATE_ON_DEFAULT; in wbt_init()
844 rwb->wc = test_bit(QUEUE_FLAG_WC, &q->queue_flags); in wbt_init()
845 rwb->rq_depth.default_depth = RWB_DEF_DEPTH; in wbt_init()
846 rwb->min_lat_nsec = wbt_default_latency_nsec(q); in wbt_init()
848 wbt_queue_depth_changed(&rwb->rqos); in wbt_init()
853 ret = rq_qos_add(q, &rwb->rqos); in wbt_init()
857 blk_stat_add_callback(q, rwb->cb); in wbt_init()
862 blk_stat_free_callback(rwb->cb); in wbt_init()