From patchwork Thu Jun 22 16:46:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xose Vazquez Perez X-Patchwork-Id: 9804803 X-Patchwork-Delegate: christophe.varoqui@free.fr 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 E17DD60386 for ; Thu, 22 Jun 2017 16:50:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CF2E627DCD for ; Thu, 22 Jun 2017 16:50:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C35DD286E4; Thu, 22 Jun 2017 16:50:20 +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 mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id BC69927DCD for ; Thu, 22 Jun 2017 16:50:19 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3A2818F034; Thu, 22 Jun 2017 16:50:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3A2818F034 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dm-devel-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 3A2818F034 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="d7qZofjn" Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C11355C7A9; Thu, 22 Jun 2017 16:50:16 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 53D811853DD3; Thu, 22 Jun 2017 16:50:14 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v5MGkWM8027500 for ; Thu, 22 Jun 2017 12:46:32 -0400 Received: by smtp.corp.redhat.com (Postfix) id 8A02618EEF; Thu, 22 Jun 2017 16:46:32 +0000 (UTC) Delivered-To: dm-devel@redhat.com Received: from mx1.redhat.com (ext-mx08.extmail.prod.ext.phx2.redhat.com [10.5.110.32]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8500317C5B for ; Thu, 22 Jun 2017 16:46:30 +0000 (UTC) Received: from mail-wr0-f193.google.com (mail-wr0-f193.google.com [209.85.128.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 36784C057EC9 for ; Thu, 22 Jun 2017 16:46:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 36784C057EC9 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=xose.vazquez@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 36784C057EC9 Received: by mail-wr0-f193.google.com with SMTP id x23so6004303wrb.0 for ; Thu, 22 Jun 2017 09:46:28 -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; bh=c52ijKrOBsNz5uYG2VeF/C9eRayxuvEaaUoELOdg9Qw=; b=d7qZofjnbZ7OJFXK/tnNVNjLGG9DukssiwLcQ4fXDbSQ0x0gY5OrSaMPhfOtV32IVs +zYGHAXv/4B12BR8EGMpcHxyQeGGzy+aEPdmzxWiMn8xnrLXTx6aPNMXnatkDzL6ELd0 PjH3NwoD9YYnypVzGcVVlcBsabBI0HNlQRkarPIWVHskL6pdwoZhH/zFDfPT3x/dnZD3 v4jKG4r47qCkW+z1EttVSZqJhGmIczQ9HEpf0HjGvPtpIQBhrgFmk1vnaD+wltPRCXBn U0du+rfaUNMbYqr1AvEm0KGvb2MsHVryZc1R1eljkxwGYWcWLz1+Iwui/REXrwNNa4P+ /0UQ== 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; bh=c52ijKrOBsNz5uYG2VeF/C9eRayxuvEaaUoELOdg9Qw=; b=sj1tJOX++5EDZs9LAATUvyrza1V+kuZs4xT+tmrQ4QPIlM/FlbISPkezhODfloS6Hn bw1IeRqoQhQERyvXKINSaREBPWyZ8SysZEfqCLsJZ7TDr2S3aT2TgQB8qk5y5PmEZihk S7NHP0TnqHXVAmya0UaMxjYFzeqf/JVVifO1BpP5iz7mrxc3Cw8EPJgcL7EOKynnv3T5 GKKMqcxOSsF6ZIoOlR+IzEUgfOkBCwqxFlrHabye4dzHBuLEn27kOfLmJbUhXMGawF3z 0PX9mg1QEKOv80u6E6dV4EEbDgthMfYIbQcTaDqDVWr0Mi8IARqkV2wfXJ28nODwyEey Th4A== X-Gm-Message-State: AKS2vOw9Kbr9myifWzEE0D8B8hrx8ij3jDT9WidB3c9Bm5BtafSzk2a5 X3Rmqup4l7jpgA== X-Received: by 10.223.145.227 with SMTP id 90mr2822617wri.171.1498149986535; Thu, 22 Jun 2017 09:46:26 -0700 (PDT) Received: from localhost ([46.222.219.177]) by smtp.gmail.com with ESMTPSA id j13sm4007396wra.56.2017.06.22.09.46.24 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 22 Jun 2017 09:46:25 -0700 (PDT) From: Xose Vazquez Perez To: Date: Thu, 22 Jun 2017 18:46:23 +0200 Message-Id: <20170622164623.4511-1-xose.vazquez@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 22 Jun 2017 16:46:28 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 22 Jun 2017 16:46:28 +0000 (UTC) for IP:'209.85.128.193' DOMAIN:'mail-wr0-f193.google.com' HELO:'mail-wr0-f193.google.com' FROM:'xose.vazquez@gmail.com' RCPT:'' X-RedHat-Spam-Score: -0.31 (BAYES_50, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_PASS) 209.85.128.193 mail-wr0-f193.google.com 209.85.128.193 mail-wr0-f193.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.32 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: dm-devel@redhat.com Cc: device-mapper development , Yang Feng , Xose Vazquez Perez Subject: [dm-devel] [PATCH] multipath-tools: beautify path_latency.c code X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Thu, 22 Jun 2017 16:50:18 +0000 (UTC) X-Virus-Scanned: ClamAV using ClamSMTP Mainly running scripts/Lindent, from kernel dir, to replace indent spaces by tabs. Cc: Yang Feng Cc: Christophe Varoqui Cc: device-mapper development Signed-off-by: Xose Vazquez Perez --- libmultipath/prioritizers/path_latency.c | 354 +++++++++++++++---------------- 1 file changed, 177 insertions(+), 177 deletions(-) diff --git a/libmultipath/prioritizers/path_latency.c b/libmultipath/prioritizers/path_latency.c index 34b734b..9fc2dfc 100644 --- a/libmultipath/prioritizers/path_latency.c +++ b/libmultipath/prioritizers/path_latency.c @@ -17,8 +17,8 @@ * Author(s): Yang Feng * * This file is released under the GPL version 2, or any later version. - * */ + #include #include #include @@ -31,27 +31,28 @@ #define pp_pl_log(prio, fmt, args...) condlog(prio, "path_latency prio: " fmt, ##args) -#define MAX_IO_NUM 200 -#define MIN_IO_NUM 2 +#define MAX_IO_NUM 200 +#define MIN_IO_NUM 2 -#define MAX_BASE_NUM 10 -#define MIN_BASE_NUM 2 +#define MAX_BASE_NUM 10 +#define MIN_BASE_NUM 2 -#define MAX_AVG_LATENCY 100000000. /*Unit: us*/ -#define MIN_AVG_LATENCY 1. /*Unit: us*/ +#define MAX_AVG_LATENCY 100000000. /* Unit: us */ +#define MIN_AVG_LATENCY 1. /* Unit: us */ -#define DEFAULT_PRIORITY 0 +#define DEFAULT_PRIORITY 0 -#define MAX_CHAR_SIZE 30 +#define MAX_CHAR_SIZE 30 -#define USEC_PER_SEC 1000000LL -#define NSEC_PER_USEC 1000LL +#define USEC_PER_SEC 1000000LL +#define NSEC_PER_USEC 1000LL static long long path_latency[MAX_IO_NUM]; static inline long long timeval_to_us(const struct timespec *tv) { - return ((long long) tv->tv_sec * USEC_PER_SEC) + (tv->tv_nsec / NSEC_PER_USEC); + return ((long long)tv->tv_sec * USEC_PER_SEC) + + (tv->tv_nsec / NSEC_PER_USEC); } static int do_readsector0(int fd, unsigned int timeout) @@ -60,198 +61,197 @@ static int do_readsector0(int fd, unsigned int timeout) unsigned char sbuf[SENSE_BUFF_LEN]; int ret; - ret = sg_read(fd, &buf[0], 4096, &sbuf[0], - SENSE_BUFF_LEN, timeout); + ret = sg_read(fd, &buf[0], 4096, &sbuf[0], SENSE_BUFF_LEN, timeout); return ret; } int check_args_valid(int io_num, int base_num) { - if ((io_num < MIN_IO_NUM) || (io_num > MAX_IO_NUM)) - { - pp_pl_log(0, "args io_num is outside the valid range"); - return 0; - } - - if ((base_num < MIN_BASE_NUM) || (base_num > MAX_BASE_NUM)) - { - pp_pl_log(0, "args base_num is outside the valid range"); - return 0; - } - - return 1; + if ((io_num < MIN_IO_NUM) || (io_num > MAX_IO_NUM)) { + pp_pl_log(0, "args io_num is outside the valid range"); + return 0; + } + + if ((base_num < MIN_BASE_NUM) || (base_num > MAX_BASE_NUM)) { + pp_pl_log(0, "args base_num is outside the valid range"); + return 0; + } + + return 1; } -/* In multipath.conf, args form: io_num|base_num. For example, -* args is "20|10", this function can get io_num value 20, and - base_num value 10. -*/ -static int get_ionum_and_basenum(char *args, - int *ionum, - int *basenum) +/* + * In multipath.conf, args form: io_num|base_num. For example, + * args is "20|10", this function can get io_num value 20, and + * base_num value 10. + */ +static int get_ionum_and_basenum(char *args, int *ionum, int *basenum) { - char source[MAX_CHAR_SIZE]; - char vertica = '|'; - char *endstrbefore = NULL; - char *endstrafter = NULL; - unsigned int size = strlen(args); - - if ((args == NULL) || (ionum == NULL) || (basenum == NULL)) - { - pp_pl_log(0, "args string is NULL"); - return 0; - } - - if ((size < 1) || (size > MAX_CHAR_SIZE-1)) - { - pp_pl_log(0, "args string's size is too long"); - return 0; - } - - memcpy(source, args, size+1); - - if (!isdigit(source[0])) - { - pp_pl_log(0, "invalid prio_args format: %s", source); - return 0; - } - - *ionum = (int)strtoul(source, &endstrbefore, 10); - if (endstrbefore[0] != vertica) - { - pp_pl_log(0, "invalid prio_args format: %s", source); - return 0; - } - - if (!isdigit(endstrbefore[1])) - { - pp_pl_log(0, "invalid prio_args format: %s", source); - return 0; - } - - *basenum = (long long)strtol(&endstrbefore[1], &endstrafter, 10); - if (check_args_valid(*ionum, *basenum) == 0) - { - return 0; - } - - return 1; + char source[MAX_CHAR_SIZE]; + char vertica = '|'; + char *endstrbefore = NULL; + char *endstrafter = NULL; + unsigned int size = strlen(args); + + if ((args == NULL) || (ionum == NULL) || (basenum == NULL)) { + pp_pl_log(0, "args string is NULL"); + return 0; + } + + if ((size < 1) || (size > MAX_CHAR_SIZE - 1)) { + pp_pl_log(0, "args string's size is too long"); + return 0; + } + + memcpy(source, args, size + 1); + + if (!isdigit(source[0])) { + pp_pl_log(0, "invalid prio_args format: %s", source); + return 0; + } + + *ionum = (int)strtoul(source, &endstrbefore, 10); + if (endstrbefore[0] != vertica) { + pp_pl_log(0, "invalid prio_args format: %s", source); + return 0; + } + + if (!isdigit(endstrbefore[1])) { + pp_pl_log(0, "invalid prio_args format: %s", source); + return 0; + } + + *basenum = (long long)strtol(&endstrbefore[1], &endstrafter, 10); + if (check_args_valid(*ionum, *basenum) == 0) { + return 0; + } + + return 1; } -long long calc_standard_deviation(long long *path_latency, int size, long long avglatency) +long long calc_standard_deviation(long long *path_latency, int size, + long long avglatency) { - int index; - long long total = 0; + int index; + long long total = 0; - for (index = 0; index < size; index++) - { - total += (path_latency[index] - avglatency) * (path_latency[index] - avglatency); - } + for (index = 0; index < size; index++) { + total += + (path_latency[index] - avglatency) * (path_latency[index] - + avglatency); + } - total /= (size-1); + total /= (size - 1); - return (long long)sqrt((double)total); + return (long long)sqrt((double)total); } -int calcPrio(double avglatency, double max_avglatency, double min_avglatency, double base_num) +int calcPrio(double avglatency, double max_avglatency, double min_avglatency, + double base_num) { - double lavglatency = log(avglatency)/log(base_num); - double lmax_avglatency = log(max_avglatency)/log(base_num); - double lmin_avglatency = log(min_avglatency)/log(base_num); + double lavglatency = log(avglatency) / log(base_num); + double lmax_avglatency = log(max_avglatency) / log(base_num); + double lmin_avglatency = log(min_avglatency) / log(base_num); - if (lavglatency <= lmin_avglatency) - return (int)(lmax_avglatency + 1.); + if (lavglatency <= lmin_avglatency) + return (int)(lmax_avglatency + 1.); - if (lavglatency > lmax_avglatency) - return 0; + if (lavglatency > lmax_avglatency) + return 0; - return (int)(lmax_avglatency - lavglatency + 1.); + return (int)(lmax_avglatency - lavglatency + 1.); } /* Calc the latency interval corresponding to the average latency */ long long calc_latency_interval(double avglatency, double max_avglatency, - double min_avglatency, double base_num) + double min_avglatency, double base_num) { - double lavglatency = log(avglatency)/log(base_num); - double lmax_avglatency = log(max_avglatency)/log(base_num); - double lmin_avglatency = log(min_avglatency)/log(base_num); - - if ((lavglatency <= lmin_avglatency) - || (lavglatency > lmax_avglatency)) - return 0;/* Invalid value */ - - if ((double)((int)lavglatency) == lavglatency) - return (long long)(avglatency - (avglatency / base_num)); - else - return (long long)(pow(base_num, (double)((int)lavglatency + 1)) - - pow(base_num, (double)((int)lavglatency))); + double lavglatency = log(avglatency) / log(base_num); + double lmax_avglatency = log(max_avglatency) / log(base_num); + double lmin_avglatency = log(min_avglatency) / log(base_num); + + if ((lavglatency <= lmin_avglatency) + || (lavglatency > lmax_avglatency)) + return 0; /* Invalid value */ + + if ((double)((int)lavglatency) == lavglatency) + return (long long)(avglatency - (avglatency / base_num)); + else + return (long long)(pow(base_num, (double)((int)lavglatency + 1)) + - pow(base_num, (double)((int)lavglatency))); } -int getprio (struct path *pp, char *args, unsigned int timeout) +int getprio(struct path *pp, char *args, unsigned int timeout) { - int rc, temp; - int index = 0; - int io_num; - int base_num; - long long avglatency; - long long latency_interval; - long long standard_deviation; - long long toldelay = 0; - long long before, after; - struct timespec tv; - - if (pp->fd < 0) - return -1; - - if (get_ionum_and_basenum(args, &io_num, &base_num) == 0) - { - pp_pl_log(0, "%s: get path_latency args fail", pp->dev); - return DEFAULT_PRIORITY; - } - - memset(path_latency, 0, sizeof(path_latency)); - - temp = io_num; - while (temp-- > 0) - { - (void)clock_gettime(CLOCK_MONOTONIC, &tv); - before = timeval_to_us(&tv); - - if (do_readsector0(pp->fd, timeout) == 2) - { - pp_pl_log(0, "%s: path down", pp->dev); - return -1; - } - - (void)clock_gettime(CLOCK_MONOTONIC, &tv); - after = timeval_to_us(&tv); - - path_latency[index] = after - before; - toldelay += path_latency[index++]; - } - - avglatency = toldelay/(long long)io_num; - pp_pl_log(4, "%s: average latency is (%lld us)", pp->dev, avglatency); - - if (avglatency > MAX_AVG_LATENCY) - { - pp_pl_log(0, "%s: average latency (%lld us) is outside the thresold (%lld us)", - pp->dev, avglatency, (long long)MAX_AVG_LATENCY); - return DEFAULT_PRIORITY; - } - - /* Min average latency and max average latency are constant, the args base_num - set can change latency_interval value corresponding to avglatency and is not constant. - Warn the user if latency_interval is smaller than (2 * standard_deviation), or equal */ - standard_deviation = calc_standard_deviation(path_latency, index, avglatency); - latency_interval = calc_latency_interval(avglatency, MAX_AVG_LATENCY, MIN_AVG_LATENCY, base_num); - if ((latency_interval!= 0) - && (latency_interval <= (2 * standard_deviation))) - pp_pl_log(3, "%s: latency interval (%lld) according to average latency (%lld us) is smaller than " - "2 * standard deviation (%lld us), or equal, args base_num (%d) needs to be set bigger value", - pp->dev, latency_interval, avglatency, standard_deviation, base_num); - - rc = calcPrio(avglatency, MAX_AVG_LATENCY, MIN_AVG_LATENCY, base_num); - return rc; + int rc, temp; + int index = 0; + int io_num; + int base_num; + long long avglatency; + long long latency_interval; + long long standard_deviation; + long long toldelay = 0; + long long before, after; + struct timespec tv; + + if (pp->fd < 0) + return -1; + + if (get_ionum_and_basenum(args, &io_num, &base_num) == 0) { + pp_pl_log(0, "%s: get path_latency args fail", pp->dev); + return DEFAULT_PRIORITY; + } + + memset(path_latency, 0, sizeof(path_latency)); + + temp = io_num; + while (temp-- > 0) { + (void)clock_gettime(CLOCK_MONOTONIC, &tv); + before = timeval_to_us(&tv); + + if (do_readsector0(pp->fd, timeout) == 2) { + pp_pl_log(0, "%s: path down", pp->dev); + return -1; + } + + (void)clock_gettime(CLOCK_MONOTONIC, &tv); + after = timeval_to_us(&tv); + + path_latency[index] = after - before; + toldelay += path_latency[index++]; + } + + avglatency = toldelay / (long long)io_num; + pp_pl_log(4, "%s: average latency is (%lld us)", pp->dev, avglatency); + + if (avglatency > MAX_AVG_LATENCY) { + pp_pl_log(0, + "%s: average latency (%lld us) is outside the thresold (%lld us)", + pp->dev, avglatency, (long long)MAX_AVG_LATENCY); + return DEFAULT_PRIORITY; + } + + /* + * Min average latency and max average latency are constant, the args + * base_num set can change latency_interval value corresponding to + * avglatency and is not constant. + * Warn the user if latency_interval is smaller than (2 * standard_deviation), + * or equal. + */ + standard_deviation = + calc_standard_deviation(path_latency, index, avglatency); + latency_interval = + calc_latency_interval(avglatency, MAX_AVG_LATENCY, MIN_AVG_LATENCY, + base_num); + if ((latency_interval != 0) + && (latency_interval <= (2 * standard_deviation))) + pp_pl_log(3, + "%s: latency interval (%lld) according to average latency (%lld us) is smaller than " + "2 * standard deviation (%lld us), or equal, args base_num (%d) needs to be set bigger value", + pp->dev, latency_interval, avglatency, + standard_deviation, base_num); + + rc = calcPrio(avglatency, MAX_AVG_LATENCY, MIN_AVG_LATENCY, base_num); + return rc; }