Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp868256ybi; Fri, 14 Jun 2019 04:49:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqzFeG5Q7iUkGNVd+6KY8AcjkG2agF0kr9eAiliX9NAwWtqrZfup0uBBrYUcwU4tCoxsIA6m X-Received: by 2002:a17:902:5984:: with SMTP id p4mr24275214pli.280.1560512994195; Fri, 14 Jun 2019 04:49:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560512994; cv=none; d=google.com; s=arc-20160816; b=aeeJqnjIsrnoUs1SyY1RHQZWzhajeCtENPICyjRjXsDM44j2dYGywl2PErlNWKOnjv DSN8hpJies2btXmniy1BPafjW2s2iQlHXas85YLspYBRG8UbydCkGAr6r8aTSoLAh1Dc +Iabnpcj+qXUGIPBcDJc7fuTknscp9YNmUYv3EV08OOCdTn7q9WK0BgAp9DhU+seIXPc CtL9ULQ1EGColnd+RRhv6kXafUejxc4HG+MHhF8mFlxlhoa3DJlL0K9Nc1++B+w7Rm8E gjt75idU44Gdw7wyTTXO5MXKrGJuYaQOpVvCNF6oYVdvO99R4R3Je00YOXQK+Dglk1WZ DVDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=j4tIopvAHkt6FY4+Y6Lo4ek2c4j4aI85q+AB/SfNRnc=; b=EDDCdQy22s0Kk86gVM148RgYL+aX3mXGgjtg8rEnEbEaCfexWh5XFKCHAfmp/HGYSS jjfJI83HZdqgS6/Wc6DnX1QkP6zBeP7UhgyRQVa8HcZm6lHucev5vAM+dO/O9vX/82h5 Z4/LTnkRFXLu90ZmxhroqYPdtQ/7Kk+nM7nxV87twctPTD2BllmZ53RxONV5MCiKgyBh kBYlB5TNAHzP/x9VXowTpO6pk8o7LioJKs5H+JgL+q8FIuSrNZus8am7uN6D9TPpoRBM Jmz+6ttXc6hldlvIyCHEc2RX15+bKKf9esI5ZDFC/uzkHaEkUs8G3es1CSnzfLNA5+sO UsiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=DVyisGSB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p5si2036621plq.198.2019.06.14.04.49.39; Fri, 14 Jun 2019 04:49:54 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=DVyisGSB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727611AbfFNLsQ (ORCPT + 99 others); Fri, 14 Jun 2019 07:48:16 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:44946 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727329AbfFNLsQ (ORCPT ); Fri, 14 Jun 2019 07:48:16 -0400 Received: by mail-wr1-f65.google.com with SMTP id r16so2174848wrl.11; Fri, 14 Jun 2019 04:48:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=j4tIopvAHkt6FY4+Y6Lo4ek2c4j4aI85q+AB/SfNRnc=; b=DVyisGSBG2m+9zrPQhqsUkYWGDipMPTHn1EfS26qfu1mMNutLtJ2OWxKpLkk/Ye78t YkAcOouXoIvD4IU6Lprl3qJCUNX2P0taFX+2nxBgbZfnhD1ISxJnHjB9m7u2pkEF4mBE CpET/xi5ANV4lGoBoq8iImWtRoZtcCx8y6kjBaWi6yz/A59W7WMuJl+NgLZOulA8uAUP 34o611fW6BIILrRU9Hy+s1mM8lRa1IKnIp15TR+A/X9p5OPPgTmwsvGWReYj0nUb8aI8 g3JBqw/WMgpGgejTS+m2ftT2Y7auFttOKL+3Hln9Lqj1DX4CSBMyMtJrBZCPqzKa5gLq sSjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=j4tIopvAHkt6FY4+Y6Lo4ek2c4j4aI85q+AB/SfNRnc=; b=ah8vqAlwFRywI93RPL/thtXbg291VGmmmKmB8hLd9QWY+P2RAEYLIbdML12k6jMgPL slU7z5b6Mu0mX9R6aAG0Nl0oFzieLi62ZHdzUuqCn7c3KnVT3cikNUZIge47haLbBarY P02OpwJ8b+lEMK1dkAWPigidp8cmBeXiF3te0/9n3YiQpwkLdmhRuUwKAe3RCOdTbXQO AoyvYH3dlgUdHFinaPtsguVGZnbHkvJH7iORR3qRHOGzYawcOmCEXu7U8kRAfldDZblt s4vhsezRsBgz4a5HCWVvkjTBGAOhq0WVfI9CaD9feU0iGGsBwxN4O1UMe/M1cFfMzuon FXZQ== X-Gm-Message-State: APjAAAVVrABUudBGYZuKNN0xncN1VQJwpil1kJSAlsi4zXNHfemuAvwQ BCkyuZHQ3kAxOqPv/b5NqOs= X-Received: by 2002:adf:c5c1:: with SMTP id v1mr46156893wrg.129.1560512894084; Fri, 14 Jun 2019 04:48:14 -0700 (PDT) Received: from localhost.localdomain ([185.107.117.129]) by smtp.gmail.com with ESMTPSA id v204sm4510656wma.20.2019.06.14.04.48.06 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 14 Jun 2019 04:48:13 -0700 (PDT) From: "Pavel Begunkov (Silence)" To: Jens Axboe , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, josef@toxicpanda.com, dennis@kernel.org Cc: Pavel Begunkov Subject: [PATCH 1/2] blk-iolatency: Fix zero mean in previous stats Date: Fri, 14 Jun 2019 14:47:47 +0300 Message-Id: <0b930bf6ebcc309748419877eb34fe50cb747299.1560510935.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Pavel Begunkov struct blk_rq_stat has two implicit states in which it can be: (1) per-cpu intermediate stats (i.e. staging, intermediate) (2) final stats / aggregation of (1) (see blk_rq_stat_collect) The states use different sets of fields. E.g. (1) uses @batch but not @mean, and vise versa for (2). So, any function that uses struct blk_rq_stat has implicit assumptions about the states. blk_rq_stat_sum() expects @src to be in (1) and @dst in (2). iolatency_check_latencies() violates that (passing struct blk_rq_stat, previously used as @dst, as @src). As a result, iolat->cur_stat.rqs.mean is always 0 for non-ssd devices. Use 2 distinct functions instead: one to collect intermediate stats (i.e. with valid batch), and the second one for merging already accumulated stats (i.e. with valid mean). Signed-off-by: Pavel Begunkov --- block/blk-iolatency.c | 21 ++++++++++++++++----- block/blk-stat.c | 20 ++++++++++++++++++-- block/blk-stat.h | 3 ++- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/block/blk-iolatency.c b/block/blk-iolatency.c index d22e61bced86..fc8ce1a0ae21 100644 --- a/block/blk-iolatency.c +++ b/block/blk-iolatency.c @@ -199,7 +199,7 @@ static inline void latency_stat_init(struct iolatency_grp *iolat, blk_rq_stat_init(&stat->rqs); } -static inline void latency_stat_sum(struct iolatency_grp *iolat, +static inline void latency_stat_merge(struct iolatency_grp *iolat, struct latency_stat *sum, struct latency_stat *stat) { @@ -207,7 +207,18 @@ static inline void latency_stat_sum(struct iolatency_grp *iolat, sum->ps.total += stat->ps.total; sum->ps.missed += stat->ps.missed; } else - blk_rq_stat_sum(&sum->rqs, &stat->rqs); + blk_rq_stat_merge(&sum->rqs, &stat->rqs); +} + +static inline void latency_stat_collect(struct iolatency_grp *iolat, + struct latency_stat *sum, + struct latency_stat *stat) +{ + if (iolat->ssd) { + sum->ps.total += stat->ps.total; + sum->ps.missed += stat->ps.missed; + } else + blk_rq_stat_collect(&sum->rqs, &stat->rqs); } static inline void latency_stat_record_time(struct iolatency_grp *iolat, @@ -531,7 +542,7 @@ static void iolatency_check_latencies(struct iolatency_grp *iolat, u64 now) for_each_online_cpu(cpu) { struct latency_stat *s; s = per_cpu_ptr(iolat->stats, cpu); - latency_stat_sum(iolat, &stat, s); + latency_stat_collect(iolat, &stat, s); latency_stat_init(iolat, s); } preempt_enable(); @@ -552,7 +563,7 @@ static void iolatency_check_latencies(struct iolatency_grp *iolat, u64 now) /* Somebody beat us to the punch, just bail. */ spin_lock_irqsave(&lat_info->lock, flags); - latency_stat_sum(iolat, &iolat->cur_stat, &stat); + latency_stat_merge(iolat, &iolat->cur_stat, &stat); lat_info->nr_samples -= iolat->nr_samples; lat_info->nr_samples += latency_stat_samples(iolat, &iolat->cur_stat); iolat->nr_samples = latency_stat_samples(iolat, &iolat->cur_stat); @@ -913,7 +924,7 @@ static size_t iolatency_ssd_stat(struct iolatency_grp *iolat, char *buf, for_each_online_cpu(cpu) { struct latency_stat *s; s = per_cpu_ptr(iolat->stats, cpu); - latency_stat_sum(iolat, &stat, s); + latency_stat_collect(iolat, &stat, s); } preempt_enable(); diff --git a/block/blk-stat.c b/block/blk-stat.c index 940f15d600f8..78389182b5d0 100644 --- a/block/blk-stat.c +++ b/block/blk-stat.c @@ -26,7 +26,7 @@ void blk_rq_stat_init(struct blk_rq_stat *stat) } /* src is a per-cpu stat, mean isn't initialized */ -void blk_rq_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src) +void blk_rq_stat_collect(struct blk_rq_stat *dst, struct blk_rq_stat *src) { if (!src->nr_samples) return; @@ -40,6 +40,21 @@ void blk_rq_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src) dst->nr_samples += src->nr_samples; } +void blk_rq_stat_merge(struct blk_rq_stat *dst, struct blk_rq_stat *src) +{ + if (!src->nr_samples) + return; + + dst->min = min(dst->min, src->min); + dst->max = max(dst->max, src->max); + + dst->mean = div_u64(src->mean * src->nr_samples + + dst->mean * dst->nr_samples, + dst->nr_samples + src->nr_samples); + + dst->nr_samples += src->nr_samples; +} + void blk_rq_stat_add(struct blk_rq_stat *stat, u64 value) { stat->min = min(stat->min, value); @@ -90,7 +105,8 @@ static void blk_stat_timer_fn(struct timer_list *t) cpu_stat = per_cpu_ptr(cb->cpu_stat, cpu); for (bucket = 0; bucket < cb->buckets; bucket++) { - blk_rq_stat_sum(&cb->stat[bucket], &cpu_stat[bucket]); + blk_rq_stat_collect(&cb->stat[bucket], + &cpu_stat[bucket]); blk_rq_stat_init(&cpu_stat[bucket]); } } diff --git a/block/blk-stat.h b/block/blk-stat.h index 17b47a86eefb..5597ecc34ef5 100644 --- a/block/blk-stat.h +++ b/block/blk-stat.h @@ -165,7 +165,8 @@ static inline void blk_stat_activate_msecs(struct blk_stat_callback *cb, } void blk_rq_stat_add(struct blk_rq_stat *, u64); -void blk_rq_stat_sum(struct blk_rq_stat *, struct blk_rq_stat *); +void blk_rq_stat_collect(struct blk_rq_stat *dst, struct blk_rq_stat *src); +void blk_rq_stat_merge(struct blk_rq_stat *dst, struct blk_rq_stat *src); void blk_rq_stat_init(struct blk_rq_stat *); #endif -- 2.22.0