Skip to contentSkip to conversation

multipath-tools: use direct IO and user-friend arguments for path-latency

PATCH Superseded Guan Junxiong 2 participants 5 messages Sep 5, 2017 – Sep 21, 2017 Device mapper

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover multipath-tools: use direct IO and user-friend arguments for path-latency

This series of patches help to make IO processing more common for path-latency prioritizer and make it more user-friendly.

First, the SCSI-to-NVMe translations which was blamed broken has been removed since linux kernel 4.13, so that SG_IO IOCTL used in the reading is not supported. Instead, this patch drops sg_read method and uses direct IO reading both for NVMe device and SCSI device.

Second, the original prio_args for prioritizer is like this: 20|10 which is somewhat unconvenient for user. Third patch drops it and use a syntax that similar to other prioritizers, for example :
"base_num=5 io_num=10".

PATCH 2/3 can be an independent patch.

Those above are the two things being addressed by this series of patches.

Note:
Martin has pointed that standard deviation in the path-latency prioritizer is wrong. We are solving this but it will take more days to prove the correctness and verify this by testing.

Thanks
Guan Junxiong

Junxiong Guan (3):
multipath-tools: use direct IO for path latency prioritizer
multipath-tools: move get_next_string to util
multipath-tools: use user-friendly prio_args for path-latency

 libmultipath/prioritizers/path_latency.c | 156 +++++++++++++++++++++++--------
 libmultipath/prioritizers/weightedpath.c |  10 +-
 libmultipath/util.c                      |   9 ++
 libmultipath/util.h                      |   1 +
 multipath/multipath.conf.5               |   2 +-
 5 files changed, 127 insertions(+), 51 deletions(-)
Signature · 1 line
2.11.1
#1 lore raw

PATCH 1/3 multipath-tools: use direct IO for path latency prioritizer

The SCSI-to-NVMe translations which was blamed broken has been removed since linux kernel 4.13, so that SG_IO IOCTL used in the reading is not supported. Instead, this patch drops sg_read method and uses direct IO reading both for NVMe device and SCSI device.

---
 libmultipath/prioritizers/path_latency.c | 85 +++++++++++++++++++++++++++++---
 1 file changed, 78 insertions(+), 7 deletions(-)
libmultipath/prioritizers/path_latency.c +78 -7
index 9fc2dfc0..c75ae03f 100644
@@ -19,15 +19,19 @@
  * This file is released under the GPL version 2, or any later version.
  */
 
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <math.h>
 #include <ctype.h>
 #include <time.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <linux/fs.h>
+#include <unistd.h>
 
 #include "debug.h"
 #include "prio.h"
 #include "structs.h"
-#include "../checkers/libsg.h"
 
 #define pp_pl_log(prio, fmt, args...) condlog(prio, "path_latency prio: " fmt, ##args)
 
@@ -47,6 +51,8 @@
 #define USEC_PER_SEC		1000000LL
 #define NSEC_PER_USEC		1000LL
 
+#define DEF_BLK_SIZE		4096
+
 static long long path_latency[MAX_IO_NUM];
 
 static inline long long timeval_to_us(const struct timespec *tv)
@@ -55,15 +61,72 @@ static inline long long timeval_to_us(const struct timespec *tv)
 	    (tv->tv_nsec / NSEC_PER_USEC);
 }
 
-static int do_readsector0(int fd, unsigned int timeout)
+static int prepare_directio_read(int fd, int *blksz, char **pbuf,
+		int *restore_flags)
+{
+	unsigned long pgsize = getpagesize();
+	long flags;
+
+	if (ioctl(fd, BLKBSZGET, blksz) < 0) {
+		pp_pl_log(3,"catnnot get blocksize, set default");
+		*blksz = DEF_BLK_SIZE;
+	}
+	if (posix_memalign((void **)pbuf, pgsize, *blksz))
+		return -1;
+
+	flags = fcntl(fd, F_GETFL);
+	if (flags < 0)
+		goto free_out;
+	if (!(flags & O_DIRECT)) {
+		flags |= O_DIRECT;
+		if (fcntl(fd, F_SETFL, flags) < 0)
+			goto free_out;
+		*restore_flags = 1;
+	}
+
+	return 0;
+
+free_out:
+	free(*pbuf);
+
+	return -1;
+}
+
+static void cleanup_directio_read(int fd, char *buf, int restore_flags)
 {
-	unsigned char buf[4096];
-	unsigned char sbuf[SENSE_BUFF_LEN];
+	long flags;
+
+	free(buf);
+
+	if (!restore_flags)
+		return;
+	if ((flags = fcntl(fd, F_GETFL)) >= 0) {
+		int ret __attribute__ ((unused));
+		flags &= ~O_DIRECT;
+		/* No point in checking for errors */
+		ret = fcntl(fd, F_SETFL, flags);
+	}
+}
+
+static int do_directio_read(int fd, unsigned int timeout, char *buf, int sz)
+{
+	fd_set read_fds;
+	struct timeval tm = { .tv_sec = timeout };
 	int ret;
+	int num_read;
 
-	ret = sg_read(fd, &buf[0], 4096, &sbuf[0], SENSE_BUFF_LEN, timeout);
+	if (lseek(fd, 0, SEEK_SET) == -1)
+		return -1;
+	FD_ZERO(&read_fds);
+	FD_SET(fd, &read_fds);
+	ret = select(fd+1, &read_fds, NULL, NULL, &tm);
+	if (ret <= 0)
+		return -1;
+	num_read = read(fd, buf, sz);
+	if (num_read != sz)
+		return -1;
 
-	return ret;
+	return 0;
 }
 
 int check_args_valid(int io_num, int base_num)
@@ -194,6 +257,9 @@ int getprio(struct path *pp, char *args, unsigned int timeout)
 	long long toldelay = 0;
 	long long before, after;
 	struct timespec tv;
+	int blksize;
+	char *buf;
+	int restore_flags = 0;
 
 	if (pp->fd < 0)
 		return -1;
@@ -205,13 +271,16 @@ int getprio(struct path *pp, char *args, unsigned int timeout)
 
 	memset(path_latency, 0, sizeof(path_latency));
 
+	prepare_directio_read(pp->fd, &blksize, &buf, &restore_flags);
+
 	temp = io_num;
 	while (temp-- > 0) {
 		(void)clock_gettime(CLOCK_MONOTONIC, &tv);
 		before = timeval_to_us(&tv);
 
-		if (do_readsector0(pp->fd, timeout) == 2) {
+		if (do_directio_read(pp->fd, timeout, buf, blksize)) {
 			pp_pl_log(0, "%s: path down", pp->dev);
+			cleanup_directio_read(pp->fd, buf, restore_flags);
 			return -1;
 		}
 
@@ -222,6 +291,8 @@ int getprio(struct path *pp, char *args, unsigned int timeout)
 		toldelay += path_latency[index++];
 	}
 
+	cleanup_directio_read(pp->fd, buf, restore_flags);
+
 	avglatency = toldelay / (long long)io_num;
 	pp_pl_log(4, "%s: average latency is (%lld us)", pp->dev, avglatency);
 
Signature · 1 line
2.11.1
#2 lore raw

PATCH 2/3 multipath-tools: move get_next_string to util

In reply to Guan Junxiong

The helper get_next_string is useful and generic. So move from exclusive weightedpath module to util module. It will be used in the next second patch.

---
 libmultipath/prioritizers/weightedpath.c | 10 +---------
 libmultipath/util.c                      |  9 +++++++++
 libmultipath/util.h                      |  1 +
 3 files changed, 11 insertions(+), 9 deletions(-)
libmultipath/prioritizers/weightedpath.c +1 -9
index 34a43a81..e0f3efbb 100644
@@ -34,15 +34,7 @@
 #include <regex.h>
 #include "structs_vec.h"
 #include "print.h"
-
-char *get_next_string(char **temp, char *split_char)
-{
-	char *token = NULL;
-	token = strsep(temp, split_char);
-	while (token != NULL && !strcmp(token, ""))
-		token = strsep(temp, split_char);
-	return token;
-}
+#include "util.h"
 
 #define CHECK_LEN \
 do { \
libmultipath/util.c +9 -0
index dff2ed3c..528ba73b 100644
@@ -64,6 +64,15 @@ filepresent (char * run) {
 	return 0;
 }
 
+char *get_next_string(char **temp, char *split_char)
+{
+	char *token = NULL;
+	token = strsep(temp, split_char);
+	while (token != NULL && !strcmp(token, ""))
+		token = strsep(temp, split_char);
+	return token;
+}
+
 int
 get_word (char * sentence, char ** word)
 {
libmultipath/util.h +1 -0
index 45291be8..584ad948 100644
@@ -6,6 +6,7 @@
 size_t strchop(char *);
 int basenamecpy (const char * src, char * dst, int);
 int filepresent (char * run);
+char *get_next_string(char **temp, char *split_char);
 int get_word (char * sentence, char ** word);
 size_t strlcpy(char *dst, const char *src, size_t size);
 size_t strlcat(char *dst, const char *src, size_t size);
Signature · 1 line
2.11.1
#3 lore raw

PATCH 3/3 multipath-tools: use user-friendly prio_args for path-latency

In reply to Guan Junxiong

The original prio_args for prioritizer is like this: 20|10 which is somewhat unconvenient for user. This patch drops it and use a syntax that similar to other prioritizers, for example :
"base_num=5 io_num=10".

---
 libmultipath/prioritizers/path_latency.c | 71 +++++++++++++++++---------------
 multipath/multipath.conf.5               |  2 +-
 2 files changed, 38 insertions(+), 35 deletions(-)
libmultipath/prioritizers/path_latency.c +37 -34
index c75ae03f..69c9bd65 100644
@@ -15,6 +15,7 @@
  *    scale, the priority "rc" of each path can be provided.
  *
  * Author(s): Yang Feng <[email protected]>
+ * Revised:   Guan Junxiong <[email protected]>
  *
  * This file is released under the GPL version 2, or any later version.
  */
@@ -32,6 +33,7 @@
 #include "debug.h"
 #include "prio.h"
 #include "structs.h"
+#include "util.h"
 
 #define pp_pl_log(prio, fmt, args...) condlog(prio, "path_latency prio: " fmt, ##args)
 
@@ -46,8 +48,6 @@
 
 #define DEFAULT_PRIORITY	0
 
-#define MAX_CHAR_SIZE		30
-
 #define USEC_PER_SEC		1000000LL
 #define NSEC_PER_USEC		1000LL
 
@@ -145,52 +145,55 @@ int check_args_valid(int io_num, int base_num)
 }
 
 /*
- * In multipath.conf, args form: io_num|base_num. For example,
- * args is "20|10", this function can get io_num value 20, and
+ * In multipath.conf, args form: io_num=n base_num=m. For example, args are
+ * "io_num=20 base_num=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);
+	char split_char[] = " \t";
+	char *arg, *temp;
+	char *str, *str_inval;
+	int i;
+	int flag_io = 0, flag_base = 0;
 
 	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);
+	arg = temp = STRDUP(args);
+	if (!arg)
 		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;
+	for (i = 0; i < 2; i++) {
+		str = get_next_string(&temp, split_char);
+		if (!str)
+			goto out;
+		if (!strncmp(str, "io_num=", 7) && strlen(str) > 7) {
+			*ionum = (int)strtoul(str + 7, &str_inval, 10);
+			if (str == str_inval)
+				goto out;
+			flag_io = 1;
+		}
+		else if (!strncmp(str, "base_num=", 9) && strlen(str) > 9) {
+			*basenum = (int)strtol(str + 9, &str_inval, 10);
+			if (str == str_inval)
+				goto out;
+			flag_base = 1;
+		}
 	}
 
-	*basenum = (long long)strtol(&endstrbefore[1], &endstrafter, 10);
-	if (check_args_valid(*ionum, *basenum) == 0) {
-		return 0;
-	}
+	if (!flag_io || !flag_base)
+		goto out;
+	if (check_args_valid(*ionum, *basenum) == 0)
+		goto out;
 
+	FREE(arg);
 	return 1;
+out:
+	FREE(arg);
+	return 0;
 }
 
 long long calc_standard_deviation(long long *path_latency, int size,
@@ -249,8 +252,8 @@ int getprio(struct path *pp, char *args, unsigned int timeout)
 {
 	int rc, temp;
 	int index = 0;
-	int io_num;
-	int base_num;
+	int io_num = 0;
+	int base_num = 0;
 	long long avglatency;
 	long long latency_interval;
 	long long standard_deviation;
multipath/multipath.conf.5 +1 -1
index d9ac279f..1f97dd8c 100644
@@ -346,7 +346,7 @@ these values can be looked up through sysfs or by running \fImultipathd show pat
 .TP 12
 .I path_latency
 Needs a value of the form
-\fI"<io_num>|<base_num>"\fR
+"io_num=\fI<20>\fR base_num=\fI<10>\fR"
 .RS
 .TP 8
 .I io_num
Signature · 1 line
2.11.1
#4 lore raw
In reply to Guan Junxiong
Quoted 2 lines · Guan Junxiong

This series of patches help to make IO processing more common for path-latency prioritizer and make it more user-friendly.

This patch set is missing.

Quoted 35 lines

First, the SCSI-to-NVMe translations which was blamed broken has been removed since linux kernel 4.13, so that SG_IO IOCTL used in the reading is not supported. Instead, this patch drops sg_read method and uses direct IO reading both for NVMe device and SCSI device.

Second, the original prio_args for prioritizer is like this: 20|10 which is somewhat unconvenient for user. Third patch drops it and use a syntax that similar to other prioritizers, for example :
"base_num=5 io_num=10".

PATCH 2/3 can be an independent patch.

Those above are the two things being addressed by this series of patches.

Note:
Martin has pointed that standard deviation in the path-latency prioritizer is wrong. We are solving this but it will take more days to prove the correctness and verify this by testing.

Thanks
Guan Junxiong

Junxiong Guan (3):
multipath-tools: use direct IO for path latency prioritizer
multipath-tools: move get_next_string to util
multipath-tools: use user-friendly prio_args for path-latency

 libmultipath/prioritizers/path_latency.c | 156 +++++++++++++++++++++++--------
 libmultipath/prioritizers/weightedpath.c |  10 +-
 libmultipath/util.c                      |   9 ++
 libmultipath/util.h                      |   1 +
 multipath/multipath.conf.5               |   2 +-
 5 files changed, 127 insertions(+), 51 deletions(-)