From patchwork Fri Dec 29 02:09:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Timofey Titovets X-Patchwork-Id: 10136019 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BD5EB60353 for ; Fri, 29 Dec 2017 02:09:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B052D2DF3D for ; Fri, 29 Dec 2017 02:09:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A4DB42DF40; Fri, 29 Dec 2017 02:09:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 887BB2DF3D for ; Fri, 29 Dec 2017 02:09:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754236AbdL2CJ0 (ORCPT ); Thu, 28 Dec 2017 21:09:26 -0500 Received: from mail-wm0-f46.google.com ([74.125.82.46]:46156 "EHLO mail-wm0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753498AbdL2CJZ (ORCPT ); Thu, 28 Dec 2017 21:09:25 -0500 Received: by mail-wm0-f46.google.com with SMTP id r78so46632244wme.5 for ; Thu, 28 Dec 2017 18:09:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=AvNIHSpTaeA9zLynd4670/eJN0VKh4lsx/rIZA2Q+8k=; b=GDALnpAvsdoJR1GHsUdVh49UHFsZPP1oaK0LyqzmLemhw/jMhnP8hgELK9RqsYqDzB eTmcba2v4V1pz/eHznKN+jr7vyaSTm7E9iTkGo086uGYne04btaa/4i0oxS77kmqPrbB 1GIXARqA2qz6u/I9f/X1qVBSVZPBPPesZeHRTHi7t3qpxh+r/TL2l4Pcnb5Pr7mHHh4y wNiBClyP2spA2WUnRT+3jnv4QdR+V439vVqUEj77sMY9jVgfudad+fjEHZTc8CQgpRKn 7Aeq+vKkN22eDMB/KUXExbUFjG152JUnYnNDdw3GNBg/Sj87YtLESgkAxR3vkOblD0ki YEig== 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:mime-version :content-transfer-encoding; bh=AvNIHSpTaeA9zLynd4670/eJN0VKh4lsx/rIZA2Q+8k=; b=rNASzEZjSgBqIqpzCtrHTmW+A18rM8d0wVJ4OYGvshN3KyHVHgXuYjKPk2LFBEZ8i+ Bh+Pi7ISxrnNeMRvtuhbaSSHNhy32zjPdjypUZiEybuCSUVLZuHowM1gc5Ckbqn8neAl gupJg8rcpfwz4eJthK82L0J+3sJ65nYiOSw9L3lY1Y4I6v7xQS6NFNnw9FcVB4p8Wq1S wC7+R4kBihsGimPxLY0eGSwtkz+NXU/qKk9iTTtQXIUfPeVTJf/VWSb2WllnKEZku1SD IKjGjWno2mO/3DYwrPKyFxKAgMGHSSECnzd77ZbOy1F+xGrCjJZe1bTGpcifRdCvBFzN Rt8w== X-Gm-Message-State: AKGB3mKJtLZcPy0KftPS4ueTlx0+nY63ZTEZWzR/gRCPiBUccmtGeTgJ iMvTR6Kgc6XdZsfAqVj1dofZsA== X-Google-Smtp-Source: ACJfBosmhgevOn+tJfInGn+3nt1bbplTDZjg10vHRMqCFXa9YLt0EHSCFSQmwg7cXLSfwM8fnGH/eA== X-Received: by 10.28.14.141 with SMTP id 135mr28465092wmo.104.1514513363379; Thu, 28 Dec 2017 18:09:23 -0800 (PST) Received: from titovetst-beplan.itransition.corp (nat6-minsk-pool-46-53-208-190.telecom.by. [46.53.208.190]) by smtp.gmail.com with ESMTPSA id o98sm97683042wrb.40.2017.12.28.18.09.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 28 Dec 2017 18:09:22 -0800 (PST) From: Timofey Titovets To: linux-btrfs@vger.kernel.org Cc: Timofey Titovets Subject: [PATCH v2] Btrfs: enchanse raid1/10 balance heuristic Date: Fri, 29 Dec 2017 05:09:14 +0300 Message-Id: <20171229020914.3618-1-nefelim4ag@gmail.com> X-Mailer: git-send-email 2.15.1 MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently btrfs raid1/10 balancer bаlance requests to mirrors, based on pid % num of mirrors. Make logic understood: - if one of underline devices are non rotational - Queue leght to underline devices By default try use pid % num_mirrors guessing, but: - If one of mirrors are non rotational, repick optimal to it - If underline mirror have less queue leght then optimal, repick to that mirror For avoid round-robin request balancing, lets round down queue leght: - By 8 for rotational devs - By 2 for all non rotational devs Changes: v1 -> v2: - Use helper part_in_flight() from genhd.c to get queue lenght - Move guess code to guess_optimal() - Change balancer logic, try use pid % mirror by default Make balancing on spinning rust if one of underline devices are overloaded Signed-off-by: Timofey Titovets --- block/genhd.c | 1 + fs/btrfs/volumes.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 115 insertions(+), 2 deletions(-) diff --git a/block/genhd.c b/block/genhd.c index 96a66f671720..a7742bbbb6a7 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -81,6 +81,7 @@ void part_in_flight(struct request_queue *q, struct hd_struct *part, atomic_read(&part->in_flight[1]); } } +EXPORT_SYMBOL_GPL(part_in_flight); struct hd_struct *__disk_get_part(struct gendisk *disk, int partno) { diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 9a04245003ab..1c84534df9a5 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include "ctree.h" #include "extent_map.h" @@ -5216,6 +5217,112 @@ int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len) return ret; } +/** + * bdev_get_queue_len - return rounded down in flight queue lenght of bdev + * + * @bdev: target bdev + * @round_down: round factor big for hdd and small for ssd, like 8 and 2 + */ +static int bdev_get_queue_len(struct block_device *bdev, int round_down) +{ + int sum; + struct hd_struct *bd_part = bdev->bd_part; + struct request_queue *rq = bdev_get_queue(bdev); + uint32_t inflight[2] = {0, 0}; + + part_in_flight(rq, bd_part, inflight); + + sum = max_t(uint32_t, inflight[0], inflight[1]); + + /* + * Try prevent switch for every sneeze + * By roundup output num by some value + */ + return ALIGN_DOWN(sum, round_down); +} + +/** + * guess_optimal - return guessed optimal mirror + * + * Optimal expected to be pid % num_stripes + * + * That's generaly ok for spread load + * Add some balancer based on queue leght to device + * + * Basic ideas: + * - Sequential read generate low amount of request + * so if load of drives are equal, use pid % num_stripes balancing + * - For mixed rotate/non-rotate mirrors, pick non-rotate as optimal + * and repick if other dev have "significant" less queue lenght + * - Repick optimal if queue leght of other mirror are less + */ +static int guess_optimal(struct map_lookup *map, int optimal) +{ + int i; + int round_down = 8; + int num = map->num_stripes; + int qlen[num]; + bool is_nonrot[num]; + bool all_bdev_nonrot = true; + bool all_bdev_rotate = true; + struct block_device *bdev; + + if (num == 1) + return optimal; + + /* Check accessible bdevs */ + for (i = 0; i < num; i++) { + /* Init for missing bdevs */ + is_nonrot[i] = false; + qlen[i] = INT_MAX; + bdev = map->stripes[i].dev->bdev; + if (bdev) { + qlen[i] = 0; + is_nonrot[i] = blk_queue_nonrot(bdev_get_queue(bdev)); + if (is_nonrot[i]) + all_bdev_rotate = false; + else + all_bdev_nonrot = false; + } + } + + /* + * Don't bother with computation + * if only one of two bdevs are accessible + */ + if (num == 2 && qlen[0] != qlen[1]) { + if (qlen[0] < qlen[1]) + return 0; + else + return 1; + } + + if (all_bdev_nonrot) + round_down = 2; + + for (i = 0; i < num; i++) { + if (qlen[i]) + continue; + bdev = map->stripes[i].dev->bdev; + qlen[i] = bdev_get_queue_len(bdev, round_down); + } + + /* For mixed case, pick non rotational dev as optimal */ + if (all_bdev_rotate == all_bdev_nonrot) { + for (i = 0; i < num; i++) { + if (is_nonrot[i]) + optimal = i; + } + } + + for (i = 0; i < num; i++) { + if (qlen[optimal] > qlen[i]) + optimal = i; + } + + return optimal; +} + static int find_live_mirror(struct btrfs_fs_info *fs_info, struct map_lookup *map, int first, int num, int optimal, int dev_replace_is_ongoing) @@ -5664,6 +5771,7 @@ static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int i; int ret = 0; int num_stripes; + int optimal; int max_errors = 0; int tgtdev_indexes = 0; struct btrfs_bio *bbio = NULL; @@ -5776,9 +5884,11 @@ static int __btrfs_map_block(struct btrfs_fs_info *fs_info, else if (mirror_num) stripe_index = mirror_num - 1; else { + optimal = guess_optimal(map, + current->pid % map->num_stripes); stripe_index = find_live_mirror(fs_info, map, 0, map->num_stripes, - current->pid % map->num_stripes, + optimal, dev_replace_is_ongoing); mirror_num = stripe_index + 1; } @@ -5804,10 +5914,12 @@ static int __btrfs_map_block(struct btrfs_fs_info *fs_info, stripe_index += mirror_num - 1; else { int old_stripe_index = stripe_index; + optimal = guess_optimal(map, + current->pid % map->num_stripes); stripe_index = find_live_mirror(fs_info, map, stripe_index, map->sub_stripes, stripe_index + - current->pid % map->sub_stripes, + optimal, dev_replace_is_ongoing); mirror_num = stripe_index - old_stripe_index + 1; }