From patchwork Sat Jul 7 15:24:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Timofey Titovets X-Patchwork-Id: 10512905 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 7AF7D60532 for ; Sat, 7 Jul 2018 15:24:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7671828882 for ; Sat, 7 Jul 2018 15:24:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6ACD928891; Sat, 7 Jul 2018 15:24:24 +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=-7.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, MAILING_LIST_MULTI, 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 DA9D328882 for ; Sat, 7 Jul 2018 15:24:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753730AbeGGPYV (ORCPT ); Sat, 7 Jul 2018 11:24:21 -0400 Received: from mail-lj1-f194.google.com ([209.85.208.194]:38600 "EHLO mail-lj1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753586AbeGGPYU (ORCPT ); Sat, 7 Jul 2018 11:24:20 -0400 Received: by mail-lj1-f194.google.com with SMTP id p6-v6so11143744ljc.5 for ; Sat, 07 Jul 2018 08:24:19 -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:mime-version :content-transfer-encoding; bh=NolY8ypZMb+5SKQhUHfA/PfZSjvo2TDDAHeqyiE6kuo=; b=JuJ3m6OHp7m+zPH8yiiBU7KIG5W1J5NPowXb72yu1lpgq0ZVyQymmmCvHxdn6A+w6i 7bHov0KY1w6NSzSY4m1Bo4rftnxyzh8RiVbN5uOYF38R7UGb8tw8JqbGzUrTnYBmXMmr VnV6j7QTJ3CuWPyxL5UoFv8f2J9OLoJpK1zvqMDiSLcxP+PRMIvQRnGdcFoxfj1tuhhD kHlgIM3zyiPK/mIwb3L+E5EFfG5lGPQl/hhoRQRhlnNp1hMCtnyD1xgQTcQqCUrx9WDs 4ipvJIm53LV/I/mnnjU8BZ4KUqsmblZo++W1ORbMuilHvm7rhots5ozDmJxRWurSobF9 neMg== 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=NolY8ypZMb+5SKQhUHfA/PfZSjvo2TDDAHeqyiE6kuo=; b=cTOIjpFoC0PnMW7era96GqFQCUROX8skD58goHH0y1AJA1Y55bS3lE/x0MN7XQjI/N 27fbUMI8NPueuVfQjf6DZjCRD5pLTwgr7BDTAWBALB/4qrS8nbGzjE1H3EsWSWRdMNrj EXWOs0Dj5axInJROzvHDZ+OtE8Dssb7o4MdHs3MoOEVkiRYYjdLgdqUD8vkgRWxcUosy cxUREun1Q4M3W0rB98yzarrrLefsvaTrZNBXaeXPfFiz7kj4aqdkleBzZKndH99ZtGJT p43SCBSSU6kBmx+m/3UoL6rDbQp6MR7wbJj1k8yVxJS69IFCc0c6IPPlszypq9u5XxKc WyWA== X-Gm-Message-State: APt69E3FjKUnvu/Inb+gIoB88gFljkOuiq7T0Qebs/+E+6ARhNdfUP4g mYGflNRFQQ2MEScX+OGO21O75A== X-Google-Smtp-Source: AAOMgpe4IZkU8n8CmgtS/qyKjY9+0ZlGoYYbV4YXhScGjpCpgocdDU1At3sLqbtYVyBdXI6lcKcPTQ== X-Received: by 2002:a2e:7113:: with SMTP id m19-v6mr8734186ljc.66.1530977058770; Sat, 07 Jul 2018 08:24:18 -0700 (PDT) Received: from TitovetsT.synesis.local ([86.57.155.118]) by smtp.gmail.com with ESMTPSA id c83-v6sm2798046lfg.18.2018.07.07.08.24.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 07 Jul 2018 08:24:18 -0700 (PDT) From: Timofey Titovets X-Google-Original-From: Timofey Titovets To: linux-btrfs@vger.kernel.org Cc: Timofey Titovets Subject: [PATCH V5] Btrfs: enchanse raid1/10 balance heuristic Date: Sat, 7 Jul 2018 18:24:08 +0300 Message-Id: <20180707152408.31485-1-timofey.titovets@synesis.ru> X-Mailer: git-send-email 2.18.0 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 From: Timofey Titovets 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 v2 -> v3: - Fix arg for RAID10 - use sub_stripes, instead of num_stripes v3 -> v4: - Rebased on latest misc-next v4 -> v5: - Rebased on latest misc-next Signed-off-by: Timofey Titovets --- block/genhd.c | 1 + fs/btrfs/volumes.c | 111 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 110 insertions(+), 2 deletions(-) diff --git a/block/genhd.c b/block/genhd.c index 9656f9e9f99e..5ea5acc88d3c 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); void part_in_flight_rw(struct request_queue *q, struct hd_struct *part, unsigned int inflight[2]) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index c95af358b71f..fa7dd6ac087f 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include "ctree.h" #include "extent_map.h" @@ -5201,6 +5202,111 @@ 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 num, int optimal) +{ + int i; + int round_down = 8; + 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 dev_replace_is_ongoing) @@ -5219,7 +5325,8 @@ static int find_live_mirror(struct btrfs_fs_info *fs_info, else num_stripes = map->num_stripes; - preferred_mirror = first + current->pid % num_stripes; + preferred_mirror = first + guess_optimal(map, num_stripes, + current->pid % num_stripes); if (dev_replace_is_ongoing && fs_info->dev_replace.cont_reading_from_srcdev_mode ==