From nobody Mon Feb 9 04:44:21 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1518009018808979.1632522358535; Wed, 7 Feb 2018 05:10:18 -0800 (PST) Received: from localhost ([::1]:35076 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ejPUL-00048T-TA for importer@patchew.org; Wed, 07 Feb 2018 08:10:13 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33916) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ejPBW-0002GX-On for qemu-devel@nongnu.org; Wed, 07 Feb 2018 07:50:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ejPBR-0008Po-Tx for qemu-devel@nongnu.org; Wed, 07 Feb 2018 07:50:46 -0500 Received: from new-relay.sw.ru ([195.214.232.40]:40742) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ejPBR-0008NJ-Ae; Wed, 07 Feb 2018 07:50:41 -0500 Received: from msk-vpn.virtuozzo.com ([195.214.232.6] helo=kvm.sw.ru) by new-relay.sw.ru with esmtp (Exim 4.89) (envelope-from ) id 1ejPBO-0003mQ-9M; Wed, 07 Feb 2018 15:50:38 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Wed, 7 Feb 2018 15:50:36 +0300 Message-Id: <20180207125037.13510-2-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180207125037.13510-1-vsementsov@virtuozzo.com> References: <20180207125037.13510-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 195.214.232.40 Subject: [Qemu-devel] [PATCH v2 1/2] block/accounting: introduce latency histogram X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, armbru@redhat.com, mreitz@redhat.com, nshirokovskiy@virtuozzo.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Introduce latency histogram statics for block devices. For each accounted operation type latency region [0, +inf) is divided into subregions by several points. Then, calculate hits for each subregion. Signed-off-by: Vladimir Sementsov-Ogievskiy --- include/block/accounting.h | 9 +++++ block/accounting.c | 97 ++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 106 insertions(+) diff --git a/include/block/accounting.h b/include/block/accounting.h index b833d26d6c..9679020f64 100644 --- a/include/block/accounting.h +++ b/include/block/accounting.h @@ -45,6 +45,12 @@ struct BlockAcctTimedStats { QSLIST_ENTRY(BlockAcctTimedStats) entries; }; =20 +typedef struct BlockLatencyHistogram { + int size; + uint64_t *points; /* @size-1 points here (all points, except 0 and +in= f) */ + uint64_t *histogram[BLOCK_MAX_IOTYPE]; /* @size elements for each type= */ +} BlockLatencyHistogram; + struct BlockAcctStats { QemuMutex lock; uint64_t nr_bytes[BLOCK_MAX_IOTYPE]; @@ -57,6 +63,7 @@ struct BlockAcctStats { QSLIST_HEAD(, BlockAcctTimedStats) intervals; bool account_invalid; bool account_failed; + BlockLatencyHistogram latency_histogram; }; =20 typedef struct BlockAcctCookie { @@ -82,5 +89,7 @@ void block_acct_merge_done(BlockAcctStats *stats, enum Bl= ockAcctType type, int64_t block_acct_idle_time_ns(BlockAcctStats *stats); double block_acct_queue_depth(BlockAcctTimedStats *stats, enum BlockAcctType type); +int block_latency_histogram_set(BlockAcctStats *stats, uint64List *latency= ); +void block_latency_histogram_clear(BlockAcctStats *stats); =20 #endif diff --git a/block/accounting.c b/block/accounting.c index 87ef5bbfaa..0051ff0c24 100644 --- a/block/accounting.c +++ b/block/accounting.c @@ -94,6 +94,100 @@ void block_acct_start(BlockAcctStats *stats, BlockAcctC= ookie *cookie, cookie->type =3D type; } =20 +/* block_latency_histogram_compare_func + * Compare @key with interval [@el, @el+1), where @el+1 is a next array el= ement + * after @el. + * Return: -1 if @key < @el + * 0 if @key in [@el, @el+1) + * +1 if @key >=3D @el+1 + */ +static int block_latency_histogram_compare_func(const void *key, const voi= d *el) +{ + uint64_t k =3D *(uint64_t *)key; + uint64_t a =3D *(uint64_t *)el; + uint64_t b =3D *((uint64_t *)el + 1); + + return k < a ? -1 : (k < b ? 0 : 1); +} + +static void block_latency_histogram_account(BlockLatencyHistogram *hist, + enum BlockAcctType type, + int64_t latency_ns) +{ + uint64_t *data, *pos; + + if (hist->points =3D=3D NULL) { + /* histogram disabled */ + return; + } + + data =3D hist->histogram[type]; + + if (latency_ns < hist->points[0]) { + data[0]++; + return; + } + + if (latency_ns >=3D hist->points[hist->size - 2]) { + data[hist->size - 1]++; + return; + } + + pos =3D bsearch(&latency_ns, hist->points, hist->size - 2, + sizeof(hist->points[0]), + block_latency_histogram_compare_func); + assert(pos !=3D NULL); + + data[pos - hist->points + 1]++; +} + +int block_latency_histogram_set(BlockAcctStats *stats, uint64List *latency) +{ + BlockLatencyHistogram *hist =3D &stats->latency_histogram; + uint64List *entry; + uint64_t *ptr; + int i; + uint64_t prev =3D 0; + + hist->size =3D 1; + + for (entry =3D latency; entry; entry =3D entry->next) { + if (entry->value <=3D prev) { + return -EINVAL; + } + hist->size++; + prev =3D entry->value; + } + + hist->points =3D g_renew(uint64_t, hist->points, hist->size - 1); + for (entry =3D latency, ptr =3D hist->points; entry; + entry =3D entry->next, ptr++) + { + *ptr =3D entry->value; + } + + for (i =3D 0; i < BLOCK_MAX_IOTYPE; i++) { + hist->histogram[i] =3D g_renew(uint64_t, hist->histogram[i], hist-= >size); + memset(hist->histogram[i], 0, hist->size * sizeof(uint64_t)); + } + + return 0; +} + +void block_latency_histogram_clear(BlockAcctStats *stats) +{ + BlockLatencyHistogram *hist =3D &stats->latency_histogram; + int i; + + g_free(hist->points); + hist->points =3D NULL; + + for (i =3D 0; i < BLOCK_MAX_IOTYPE; i++) { + g_free(hist->histogram[i]); + hist->histogram[i] =3D NULL; + } +} + static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *c= ookie, bool failed) { @@ -116,6 +210,9 @@ static void block_account_one_io(BlockAcctStats *stats,= BlockAcctCookie *cookie, stats->nr_ops[cookie->type]++; } =20 + block_latency_histogram_account(&stats->latency_histogram, cookie->typ= e, + latency_ns); + if (!failed || stats->account_failed) { stats->total_time_ns[cookie->type] +=3D latency_ns; stats->last_access_time_ns =3D time_ns; --=20 2.11.1