From patchwork Wed Jan 20 07:52:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anand Jain X-Patchwork-Id: 12031621 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36E6DC433E6 for ; Wed, 20 Jan 2021 07:53:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EA5C223131 for ; Wed, 20 Jan 2021 07:53:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729027AbhATHxc (ORCPT ); Wed, 20 Jan 2021 02:53:32 -0500 Received: from aserp2130.oracle.com ([141.146.126.79]:54118 "EHLO aserp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729524AbhATHxU (ORCPT ); Wed, 20 Jan 2021 02:53:20 -0500 Received: from pps.filterd (aserp2130.oracle.com [127.0.0.1]) by aserp2130.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 10K7meGU162964; Wed, 20 Jan 2021 07:52:15 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=corp-2020-01-29; bh=y937VeGWFB1iJucaXFao/WtEd31mdn6dclhagdvfEgA=; b=SqIK0b7VGxLdKNT+y8GXQ6VgekgB4NQj5dDIaKwRjbz7qbL3K1vuHakTsPgdUD5qJ5EU QYMnnZ3PGvK/0vDGkft6j3rDUIoyTEydheP1g8w01hjjqDMUjEjCU+vC3yjhpYMkAmjG zDt9YUqWg9UfwSYt8a896y1vmmyfq8Fd2Gcjl4KSSUKzsJQVCNHUziLrIYp70wrhw/PH h/rDt/7+CvvaaOzil2bXNHgbhLRBri8az9Demf6vNPYDk9ISsynvAsloSW0gm9+FzLo5 hvEFaPFynMCTy+0lZWQ6jpiW50IqeJiKGGII1qtm7OXFFT8IB7spz4/3m8HRZvSYbtmr XQ== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by aserp2130.oracle.com with ESMTP id 3668qr9589-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Jan 2021 07:52:15 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 10K7nnNQ162597; Wed, 20 Jan 2021 07:52:15 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserp3020.oracle.com with ESMTP id 3668rdgmhe-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Jan 2021 07:52:15 +0000 Received: from abhmp0004.oracle.com (abhmp0004.oracle.com [141.146.116.10]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 10K7qEZW002921; Wed, 20 Jan 2021 07:52:14 GMT Received: from ca-dev104.us.oracle.com (/10.129.135.33) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 19 Jan 2021 23:52:14 -0800 From: Anand Jain To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.com, josef@toxicpanda.com Subject: [PATCH v4 1/3] btrfs: add read_policy latency Date: Tue, 19 Jan 2021 23:52:05 -0800 Message-Id: <63f6f00e2ecc741efd2200c3c87b5db52c6be2fd.1611114341.git.anand.jain@oracle.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9869 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 spamscore=0 suspectscore=0 adultscore=0 mlxlogscore=999 bulkscore=0 malwarescore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101200043 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9869 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 mlxscore=0 suspectscore=0 lowpriorityscore=0 bulkscore=0 adultscore=0 spamscore=0 phishscore=0 priorityscore=1501 impostorscore=0 malwarescore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101200043 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org The read policy type latency routes the read IO based on the historical average wait-time experienced by the read IOs through the individual device. This patch obtains the historical read IO stats from the kernel block layer and calculates its average. Example usage: echo "latency" > /sys/fs/btrfs/$uuid/read_policy Signed-off-by: Anand Jain Reviewed-by: Josef Bacik --- v4: For btrfs_debug_rl() use fs_info instead of device->fs_devices->fs_info. v3: The block layer commit 0d02129e76ed (block: merge struct block_device and struct hd_struct) has changed the first argument in the function part_stat_read_all() in 5.11-rc1. So the compilation will fail. This patch fixes it. Commit log updated. v2: Use btrfs_debug_rl() instead of btrfs_info_rl() It is better we have this debug until we test this on at least few hardwares. Drop the unrelated changes. Update change log. rfc->v1: Drop part_stat_read_all instead use part_stat_read Drop inflight fs/btrfs/sysfs.c | 3 ++- fs/btrfs/volumes.c | 38 ++++++++++++++++++++++++++++++++++++++ fs/btrfs/volumes.h | 2 ++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c index 4522a1c4cd08..7c0324fe97b2 100644 --- a/fs/btrfs/sysfs.c +++ b/fs/btrfs/sysfs.c @@ -915,7 +915,8 @@ static bool strmatch(const char *buffer, const char *string) return false; } -static const char * const btrfs_read_policy_name[] = { "pid" }; +/* Must follow the order as in enum btrfs_read_policy */ +static const char * const btrfs_read_policy_name[] = { "pid", "latency" }; static ssize_t btrfs_read_policy_show(struct kobject *kobj, struct kobj_attribute *a, char *buf) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 62d6a890fc50..f361f1c87eb6 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -14,6 +14,7 @@ #include #include #include +#include #include "misc.h" #include "ctree.h" #include "extent_map.h" @@ -5490,6 +5491,39 @@ int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len) return ret; } +static int btrfs_find_best_stripe(struct btrfs_fs_info *fs_info, + struct map_lookup *map, int first, + int num_stripe) +{ + u64 est_wait = 0; + int best_stripe = 0; + int index; + + for (index = first; index < first + num_stripe; index++) { + u64 read_wait; + u64 avg_wait = 0; + unsigned long read_ios; + struct btrfs_device *device = map->stripes[index].dev; + + read_wait = part_stat_read(device->bdev, nsecs[READ]); + read_ios = part_stat_read(device->bdev, ios[READ]); + + if (read_wait && read_ios && read_wait >= read_ios) + avg_wait = div_u64(read_wait, read_ios); + else + btrfs_debug_rl(fs_info, + "devid: %llu avg_wait ZERO read_wait %llu read_ios %lu", + device->devid, read_wait, read_ios); + + if (est_wait == 0 || est_wait > avg_wait) { + est_wait = avg_wait; + best_stripe = index; + } + } + + return best_stripe; +} + static int find_live_mirror(struct btrfs_fs_info *fs_info, struct map_lookup *map, int first, int dev_replace_is_ongoing) @@ -5519,6 +5553,10 @@ static int find_live_mirror(struct btrfs_fs_info *fs_info, case BTRFS_READ_POLICY_PID: preferred_mirror = first + (current->pid % num_stripes); break; + case BTRFS_READ_POLICY_LATENCY: + preferred_mirror = btrfs_find_best_stripe(fs_info, map, first, + num_stripes); + break; } if (dev_replace_is_ongoing && diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h index 1997a4649a66..71ba1f0e93f4 100644 --- a/fs/btrfs/volumes.h +++ b/fs/btrfs/volumes.h @@ -222,6 +222,8 @@ enum btrfs_chunk_allocation_policy { enum btrfs_read_policy { /* Use process PID to choose the stripe */ BTRFS_READ_POLICY_PID, + /* Find and use device with the lowest latency */ + BTRFS_READ_POLICY_LATENCY, BTRFS_NR_READ_POLICY, }; From patchwork Wed Jan 20 07:52:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anand Jain X-Patchwork-Id: 12031627 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 03059C433E9 for ; Wed, 20 Jan 2021 07:55:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9432D2313A for ; Wed, 20 Jan 2021 07:55:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729841AbhATHzO (ORCPT ); Wed, 20 Jan 2021 02:55:14 -0500 Received: from aserp2120.oracle.com ([141.146.126.78]:59552 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726790AbhATHxU (ORCPT ); Wed, 20 Jan 2021 02:53:20 -0500 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 10K7oE1o163363; Wed, 20 Jan 2021 07:52:16 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=corp-2020-01-29; bh=7CHtSN3HKN/iP31TJve+MjXjZZTRXY0RwC3lYao2mwM=; b=O3fhqrVcVK7ygbOdsi53QTq8yvaRcNaxN+I7pbUPIZaINy15bV3htSiJ7jdFlgjcUwVP PfP4qxBelb2DSt5mYzbFAdz6Etu6ajY4CGzoxCQxCfrtM6E4Et/rVQzlYo6PvFCtTj5i XE9MNXgjS6VnwunIWlGa7qHBe1xK3HeubksseLdrKaY/FlrmwoFaIZDUot2EkmztNNMJ 59kvLLp7vdxBfcuCnRa/8ZLwSq/yV2I9tDlp3pFGOHuIwMOf0BJZ+E9WN0QUbhHvllxf SI62LWn4Hx4+xZSORBl2lF/ixUvzO+Pa8YYymCqUsKVWkcps//bRQ61KooYuuniarOGy Ug== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by aserp2120.oracle.com with ESMTP id 3668qms504-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Jan 2021 07:52:16 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 10K7nmck162268; Wed, 20 Jan 2021 07:52:16 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by aserp3020.oracle.com with ESMTP id 3668rdgmhy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Jan 2021 07:52:16 +0000 Received: from abhmp0004.oracle.com (abhmp0004.oracle.com [141.146.116.10]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id 10K7qFs0027184; Wed, 20 Jan 2021 07:52:15 GMT Received: from ca-dev104.us.oracle.com (/10.129.135.33) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 19 Jan 2021 23:52:14 -0800 From: Anand Jain To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.com, josef@toxicpanda.com Subject: [PATCH v4 2/3] btrfs: introduce new device-state read_preferred Date: Tue, 19 Jan 2021 23:52:06 -0800 Message-Id: <151811057c559d9212c7fc55e9c9cbbad1f62f05.1611114341.git.anand.jain@oracle.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9869 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 spamscore=0 suspectscore=0 adultscore=0 mlxlogscore=999 bulkscore=0 malwarescore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101200043 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9869 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 priorityscore=1501 adultscore=0 impostorscore=0 mlxlogscore=999 spamscore=0 suspectscore=0 phishscore=0 clxscore=1015 bulkscore=0 mlxscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101200043 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org This is a preparatory patch and introduces a new device flag 'read_preferred', RW-able using sysfs interface. Signed-off-by: Anand Jain --- v4: - v2: C style fixes. Drop space in between '! test_bit' and extra lines after it. fs/btrfs/sysfs.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/volumes.h | 1 + 2 files changed, 54 insertions(+) diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c index 7c0324fe97b2..5888e15e3d14 100644 --- a/fs/btrfs/sysfs.c +++ b/fs/btrfs/sysfs.c @@ -1422,11 +1422,64 @@ static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj, } BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show); +static ssize_t btrfs_devinfo_read_pref_show(struct kobject *kobj, + struct kobj_attribute *a, char *buf) +{ + int val; + struct btrfs_device *device = container_of(kobj, struct btrfs_device, + devid_kobj); + + val = !!test_bit(BTRFS_DEV_STATE_READ_PREFERRED, &device->dev_state); + + return snprintf(buf, PAGE_SIZE, "%d\n", val); +} + +static ssize_t btrfs_devinfo_read_pref_store(struct kobject *kobj, + struct kobj_attribute *a, + const char *buf, size_t len) +{ + int ret; + unsigned long val; + struct btrfs_device *device; + + ret = kstrtoul(skip_spaces(buf), 0, &val); + if (ret) + return ret; + + if (val != 0 && val != 1) + return -EINVAL; + + /* + * lock is not required, the btrfs_device struct can't be freed while + * its kobject btrfs_device::devid_kobj is still open. + */ + device = container_of(kobj, struct btrfs_device, devid_kobj); + + if (val && + !test_bit(BTRFS_DEV_STATE_READ_PREFERRED, &device->dev_state)) { + set_bit(BTRFS_DEV_STATE_READ_PREFERRED, &device->dev_state); + btrfs_info(device->fs_devices->fs_info, + "set read preferred on devid %llu (%d)", + device->devid, task_pid_nr(current)); + } else if (!val && + test_bit(BTRFS_DEV_STATE_READ_PREFERRED, &device->dev_state)) { + clear_bit(BTRFS_DEV_STATE_READ_PREFERRED, &device->dev_state); + btrfs_info(device->fs_devices->fs_info, + "reset read preferred on devid %llu (%d)", + device->devid, task_pid_nr(current)); + } + + return len; +} +BTRFS_ATTR_RW(devid, read_preferred, btrfs_devinfo_read_pref_show, + btrfs_devinfo_read_pref_store); + static struct attribute *devid_attrs[] = { BTRFS_ATTR_PTR(devid, in_fs_metadata), BTRFS_ATTR_PTR(devid, missing), BTRFS_ATTR_PTR(devid, replace_target), BTRFS_ATTR_PTR(devid, writeable), + BTRFS_ATTR_PTR(devid, read_preferred), NULL }; ATTRIBUTE_GROUPS(devid); diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h index 71ba1f0e93f4..ea786864b903 100644 --- a/fs/btrfs/volumes.h +++ b/fs/btrfs/volumes.h @@ -51,6 +51,7 @@ struct btrfs_io_geometry { #define BTRFS_DEV_STATE_REPLACE_TGT (3) #define BTRFS_DEV_STATE_FLUSH_SENT (4) #define BTRFS_DEV_STATE_NO_READA (5) +#define BTRFS_DEV_STATE_READ_PREFERRED (6) struct btrfs_zoned_device_info; From patchwork Wed Jan 20 07:52:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anand Jain X-Patchwork-Id: 12031625 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F7BFC433DB for ; Wed, 20 Jan 2021 07:55:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1EE9F2313A for ; Wed, 20 Jan 2021 07:55:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729978AbhATHys (ORCPT ); Wed, 20 Jan 2021 02:54:48 -0500 Received: from aserp2120.oracle.com ([141.146.126.78]:59546 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729803AbhATHxW (ORCPT ); Wed, 20 Jan 2021 02:53:22 -0500 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 10K7oEJW163359; Wed, 20 Jan 2021 07:52:17 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=corp-2020-01-29; bh=887Vbwhm+l8xNebwBNlrCEzwQMlkYzZS39Ff8VN9p8c=; b=ZwM0hXv0Djw+HOMt1by+aMaTkjwyTWDyRN8Yp3fGtZD7pdC4WY+OiwSub6gGhssp9DmD VMFLanzgQW7lLE9aw6lyDv6QQtTalNmfg+CWeTS+N7bGjX754QglznPM1n+Tan8x9sU1 RmSrrdGyRqtX8Kx1TFSVwCLFrRLiU0jOcZZn5N+fSiyhGFj8xvHNHfvLg6554BrhUBWu cUjmWanxaal1QGxlS2/9xVvvBTqlde1iMJgCtIunQs2GbkJNGJkX3CsIMk00maiLqkiw ojaXvBeI6mu9FoQbf6dLM7+9lqhN6Wze7quaMhtOq0fuuvsx0zO20qJz1/qgSxAyEWix mQ== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by aserp2120.oracle.com with ESMTP id 3668qms503-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Jan 2021 07:52:17 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 10K7ocHf122865; Wed, 20 Jan 2021 07:52:16 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userp3020.oracle.com with ESMTP id 3668qvnkkj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 20 Jan 2021 07:52:16 +0000 Received: from abhmp0004.oracle.com (abhmp0004.oracle.com [141.146.116.10]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 10K7qFSE010050; Wed, 20 Jan 2021 07:52:15 GMT Received: from ca-dev104.us.oracle.com (/10.129.135.33) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 19 Jan 2021 23:52:15 -0800 From: Anand Jain To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.com, josef@toxicpanda.com Subject: [PATCH v4 3/3] btrfs: introduce new read_policy device Date: Tue, 19 Jan 2021 23:52:07 -0800 Message-Id: <227061e4c1d8c5a9465f6bd1ab15873a64de1c4f.1611114341.git.anand.jain@oracle.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9869 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 phishscore=0 adultscore=0 malwarescore=0 mlxscore=0 mlxlogscore=999 spamscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101200043 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9869 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 priorityscore=1501 adultscore=0 impostorscore=0 mlxlogscore=999 spamscore=0 suspectscore=0 phishscore=0 clxscore=1015 bulkscore=0 mlxscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2101200043 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Read-policy type 'device' and device flag 'read-preferred': The read-policy type device picks the device(s) flagged as read-preferred for reading stripes of type raid1, raid10, raid1c3 and raid1c4. A system might contain SSD, nvme, iscsi, or san lun, and which are all a non-rotational device, so it is not a good idea to set the read-preferred automatically. Instead, device read-policy along with the read-preferred flag provides an ability to do it manually. This advanced tuning is useful in more than one situation, for example, - In heterogeneous-disk volume, it provides an ability to manually choose the low latency disks for reading. - Useful for more accurate testing. - Avoid known problematic device from reading the chunk until it is replaced (by marking the other good devices as read-preferred). Note: If the read-policy type is set to 'device', but there isn't any device which is flagged as read-preferred, then stripe 0 is used for reading. The device replacement won't migrate the read-preferred flag to the new replace the target device. As of now, this is an in-memory only feature. It's pointless to set the read-preferred flag on the missing device, as IOs aren't submitted to the missing device. If there is more than one read-preferred device in a chunk, the read IO shall go to the stripe 0 as of now. Usage example: Consider a typical two disks raid1. Configure devid1 for reading. $ echo 1 > devinfo/1/read_preferred $ cat devinfo/1/read_preferred 1 $ cat devinfo/2/read_preferred 0 $ pwd /sys/fs/btrfs/12345678-1234-1234-1234-123456789abc $ cat read_policy [pid] device $ echo device > ./read_policy $ cat read_policy pid [device] Now read IOs are sent to devid 1 (sdb). $ echo 3 > /proc/sys/vm/drop_caches $ md5sum /btrfs/YkZI $ iostat -zy 1 | egrep 'sdb|sdc' (from another terminal) sdb 50.00 40048.00 0.00 40048 0 Change the read-preferred device from devid 1 to devid 2 (sdc). $ echo 0 > ./devinfo/1/read_preferred [ 3343.918658] BTRFS info (device sdb): reset read preferred on devid 1 (1334) $ echo 1 > ./devinfo/2/read_preferred [ 3343.919876] BTRFS info (device sdb): set read preferred on devid 2 (1334) $ echo 3 > /proc/sys/vm/drop_caches $ md5sum /btrfs/YkZI Further read ios are sent to devid 2 (sdc). $ iostat -zy 1 | egrep 'sdb|sdc' (from another terminal) sdc 49.00 40048.00 0.00 40048 0 Signed-off-by: Anand Jain Reviewed-by: Josef Bacik --- v4: add Josef rb. v3: update the change log. v2: - rfc->v1: - fs/btrfs/sysfs.c | 3 ++- fs/btrfs/volumes.c | 22 ++++++++++++++++++++++ fs/btrfs/volumes.h | 2 ++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c index 5888e15e3d14..dd1835a2a7ab 100644 --- a/fs/btrfs/sysfs.c +++ b/fs/btrfs/sysfs.c @@ -916,7 +916,8 @@ static bool strmatch(const char *buffer, const char *string) } /* Must follow the order as in enum btrfs_read_policy */ -static const char * const btrfs_read_policy_name[] = { "pid", "latency" }; +static const char * const btrfs_read_policy_name[] = { "pid", "latency", + "device" }; static ssize_t btrfs_read_policy_show(struct kobject *kobj, struct kobj_attribute *a, char *buf) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index f361f1c87eb6..d942260f8d2c 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -5524,6 +5524,25 @@ static int btrfs_find_best_stripe(struct btrfs_fs_info *fs_info, return best_stripe; } +static int btrfs_find_read_preferred(struct map_lookup *map, int first, int num_stripe) +{ + int stripe_index; + int last = first + num_stripe; + + /* + * If there are more than one read preferred devices, then just pick the + * first found read preferred device as of now. + */ + for (stripe_index = first; stripe_index < last; stripe_index++) { + if (test_bit(BTRFS_DEV_STATE_READ_PREFERRED, + &map->stripes[stripe_index].dev->dev_state)) + return stripe_index; + } + + /* If there is no read preferred device then just use the first stripe */ + return first; +} + static int find_live_mirror(struct btrfs_fs_info *fs_info, struct map_lookup *map, int first, int dev_replace_is_ongoing) @@ -5557,6 +5576,9 @@ static int find_live_mirror(struct btrfs_fs_info *fs_info, preferred_mirror = btrfs_find_best_stripe(fs_info, map, first, num_stripes); break; + case BTRFS_READ_POLICY_DEVICE: + preferred_mirror = btrfs_find_read_preferred(map, first, num_stripes); + break; } if (dev_replace_is_ongoing && diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h index ea786864b903..8d5a2cddc0ab 100644 --- a/fs/btrfs/volumes.h +++ b/fs/btrfs/volumes.h @@ -225,6 +225,8 @@ enum btrfs_read_policy { BTRFS_READ_POLICY_PID, /* Find and use device with the lowest latency */ BTRFS_READ_POLICY_LATENCY, + /* Use the device marked with READ_PREFERRED state */ + BTRFS_READ_POLICY_DEVICE, BTRFS_NR_READ_POLICY, };