Skip to contentSkip to conversation

multipath-tools:Prioritizer based on a time-delay algorithm

PATCH Superseded Yang Feng 4 participants 19 messages May 8, 2017 – Jun 1, 2017 Device mapper

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Yang Feng

author
#0 lore raw

PATCH 1/1 multipath-tools:Prioritizer based on a time-delay algorithm

Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a time-delay algorithm. And the time-delay algorithm is dependent on the following arguments(delay_interval, cons_num).
The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path continuously, the IOs' average delay can be calculated.
2. According to the average delay of each path and the weight value "delay_interval", the priority "rc" of each path can be provided.

delay_interval delay_interval delay_interval delay_interval
|---------------|---------------|---------------| |---------------|
|priority rank1 |priority rank2 |priority rank3 |... |priority rank4 |
|---------------|---------------|---------------| |---------------|
Priority Rank Partitioning

---
 libmultipath/Makefile                   |   2 +-
 libmultipath/checkers/Makefile          |   7 +-
 libmultipath/checkers/emc_clariion.c    |   2 +-
 libmultipath/checkers/libsg.c           |  94 ------------
 libmultipath/checkers/libsg.h           |   9 --
 libmultipath/checkers/readsector0.c     |   2 +-
 libmultipath/libsg.c                    |  94 ++++++++++++
 libmultipath/libsg.h                    |   9 ++
 libmultipath/prioritizers/Makefile      |   6 +-
 libmultipath/prioritizers/delayedpath.c | 246 ++++++++++++++++++++++++++++++++
 libmultipath/prioritizers/delayedpath.h |  14 ++
 11 files changed, 373 insertions(+), 112 deletions(-)
 delete mode 100644 libmultipath/checkers/libsg.c
 delete mode 100644 libmultipath/checkers/libsg.h
 create mode 100644 libmultipath/libsg.c
 create mode 100644 libmultipath/libsg.h
 create mode 100644 libmultipath/prioritizers/delayedpath.c
 create mode 100644 libmultipath/prioritizers/delayedpath.h
libmultipath/Makefile +1 -1
index 1f5ec25..a4d725a 100644
@@ -41,7 +41,7 @@ OBJS = memory.o parser.o vector.o devmapper.o callout.o \
 	structs.o discovery.o propsel.o dict.o \
 	pgpolicies.o debug.o defaults.o uevent.o time-util.o \
 	switchgroup.o uxsock.o print.o alias.o log_pthread.o \
-	log.o configure.o structs_vec.o sysfs.o prio.o checkers.o \
+	log.o configure.o structs_vec.o sysfs.o libsg.o prio.o checkers.o \
 	lock.o waiter.o file.o wwids.o prioritizers/alua_rtpg.o
 
 all: $(LIBS)
libmultipath/checkers/Makefile +2 -5
index 4970fc0..7e433ca 100644
@@ -14,19 +14,16 @@ LIBS= \
 	libcheckemc_clariion.so \
 	libcheckhp_sw.so \
 	libcheckrdac.so
-ifneq ($(ENABLE_RADOS),0)
-LIBS += libcheckrbd.so
-endif
 
 all: $(LIBS)
 
 libcheckrbd.so: rbd.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^ -lrados -ludev
 
-libcheckdirectio.so: libsg.o directio.o
+libcheckdirectio.so: ../libsg.o directio.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^ -laio
 
-libcheck%.so: libsg.o %.o
+libcheck%.so: ../libsg.o %.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
 install:
libmultipath/checkers/emc_clariion.c +1 -1
index 9c1ffed..e4ba757 100644
@@ -12,7 +12,7 @@
 #include <errno.h>
 
 #include "../libmultipath/sg_include.h"
-#include "libsg.h"
+#include "../libmultipath/libsg.h"
 #include "checkers.h"
 #include "debug.h"
 #include "memory.h"
libmultipath/checkers/libsg.c deleted +0 -94

94 lines not on this page · raw message

libmultipath/checkers/libsg.h deleted +0 -9
deleted file mode 100644
index 3994f45..0000000
@@ -1,9 +0,0 @@
-#ifndef _LIBSG_H
-#define _LIBSG_H
-
-#define SENSE_BUFF_LEN 32
-
-int sg_read (int sg_fd, unsigned char * buff, int buff_len,
-	     unsigned char * sense, int sense_len, unsigned int timeout);
-
-#endif /* _LIBSG_H */
libmultipath/checkers/readsector0.c +1 -1
index 8fccb46..d70c5c5 100644
@@ -4,7 +4,7 @@
 #include <stdio.h>
 
 #include "checkers.h"
-#include "libsg.h"
+#include "../libmultipath/libsg.h"
 
 #define MSG_READSECTOR0_UP	"readsector0 checker reports path is up"
 #define MSG_READSECTOR0_DOWN	"readsector0 checker reports path is down"
libmultipath/libsg.c new file +94 -0

94 lines not on this page · raw message

libmultipath/libsg.h new file +9 -0
new file mode 100644
index 0000000..3994f45
@@ -0,0 +1,9 @@
+#ifndef _LIBSG_H
+#define _LIBSG_H
+
+#define SENSE_BUFF_LEN 32
+
+int sg_read (int sg_fd, unsigned char * buff, int buff_len,
+	     unsigned char * sense, int sense_len, unsigned int timeout);
+
+#endif /* _LIBSG_H */
libmultipath/prioritizers/Makefile +5 -1
index 36b42e4..7e3da51 100644
@@ -18,13 +18,17 @@ LIBS = \
 	libpriorandom.so \
 	libpriordac.so \
 	libprioweightedpath.so \
-	libpriosysfs.so
+	libpriodelayedpath.so \
+	libpriosysfs.so 
 
 all: $(LIBS)
 
 libprioalua.so: alua.o alua_rtpg.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
+libpriodelayedpath.so: delayedpath.o  ../libsg.o
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
+
 libprio%.so: %.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
libmultipath/prioritizers/delayedpath.c new file +246 -0

246 lines not on this page · raw message

libmultipath/prioritizers/delayedpath.h new file +14 -0
new file mode 100644
index 0000000..ca89702
@@ -0,0 +1,14 @@
+#ifndef _DELAYEDPATH_H
+#define _DELAYEDPATH_H
+
+#define PRIO_DELAYED_PATH "delayedpath"
+#define USEC_PER_SEC      1000000LL
+#define USEC_PER_MSEC     1000LL
+#define USEC_PER_USEC     1LL
+
+static inline long long timeval_to_us(const struct timeval *tv)
+{
+	return ((long long) tv->tv_sec * USEC_PER_SEC) + tv->tv_usec;
+}
+
+#endif
Signature · 1 line
2.6.4.windows.1
#1 lore raw
Quoted 4 lines · Yang Feng

Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a time-delay algorithm. And the time-delay algorithm is dependent on the following arguments(delay_interval, cons_num).

This new feature should be documented in multipath/multipath.conf.5

libmultipath/checkers/Makefile LIBS= \ · line 19

Quoted 11 lines
libmultipath/checkers/Makefile +0 -3
index 4970fc0..7e433ca 100644
@@ -14,19 +14,16 @@ LIBS= \
 	libcheckemc_clariion.so \
 	libcheckhp_sw.so \
 	libcheckrdac.so
-ifneq ($(ENABLE_RADOS),0)
-LIBS += libcheckrbd.so
-endif

Is it right?

#2 lore raw

Hi Xose and Christophe,

Quoted 8 lines · Xose Vazquez Perez
Quoted 4 lines · Yang Feng

Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a time-delay algorithm. And the time-delay algorithm is dependent on the following arguments(delay_interval, cons_num).

This new feature should be documented in multipath/multipath.conf.5

multipath/multipath.conf.5 has be documented in the following patch.

Quoted 5 lines
Quoted 5 lines
libmultipath/checkers/Makefile +0 -0
index 4970fc0..7e433ca 100644
@@ -14,19 +14,16 @@ LIBS= \
Quoted 10 lines

libmultipath/checkers/Makefile LIBS= \ · line 19

Quoted 6 lines
libmultipath/checkers/Makefile +0 -3
 	libcheckemc_clariion.so \
 	libcheckhp_sw.so \
 	libcheckrdac.so
-ifneq ($(ENABLE_RADOS),0)
-LIBS += libcheckrbd.so
-endif

Is it right?

Thanks, fixed as the flollowing patch.

---
Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a time-delay algorithm. And the time-delay algorithm is dependent on the following arguments(delay_interval, cons_num). The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path
continuously, the IOs' average delay can be calculated.
2. According to the average delay of each path and the weight value
"delay_interval", the priority "rc" of each path can be provided.

delay_interval delay_interval delay_interval delay_interval
|---------------|---------------|---------------| |---------------|
|priority rank 1|priority rank 2|priority rank 3|... |priority rank x|
|---------------|---------------|---------------| |---------------|
Priority Rank Partitioning

---
 libmultipath/Makefile                   |   2 +-
 libmultipath/checkers/Makefile          |   4 +-
 libmultipath/checkers/emc_clariion.c    |   2 +-
 libmultipath/checkers/libsg.c           |  94 ------------
 libmultipath/checkers/libsg.h           |   9 --
 libmultipath/checkers/readsector0.c     |   2 +-
 libmultipath/libsg.c                    |  94 ++++++++++++
 libmultipath/libsg.h                    |   9 ++
 libmultipath/prioritizers/Makefile      |   6 +-
 libmultipath/prioritizers/delayedpath.c | 246 ++++++++++++++++++++++++++++++++
 libmultipath/prioritizers/delayedpath.h |  14 ++
 multipath/multipath.conf.5              |  19 +++++++++++++++++++
 12 files changed, 392 insertions(+), 109 deletions(-)  delete mode 100644 libmultipath/checkers/libsg.c  delete mode 100644 libmultipath/checkers/libsg.h  create mode 100644 libmultipath/libsg.c  create mode 100644 libmultipath/libsg.h  create mode 100644 libmultipath/prioritizers/delayedpath.c
 create mode 100644 libmultipath/prioritizers/delayedpath.h
libmultipath/Makefile +1 -1
@@ -41,7 +41,7 @@ OBJS = memory.o parser.o vector.o devmapper.o callout.o \
 	structs.o discovery.o propsel.o dict.o \
 	pgpolicies.o debug.o defaults.o uevent.o time-util.o \
 	switchgroup.o uxsock.o print.o alias.o log_pthread.o \
-	log.o configure.o structs_vec.o sysfs.o prio.o checkers.o \
+	log.o configure.o structs_vec.o sysfs.o libsg.o prio.o checkers.o \
 	lock.o waiter.o file.o wwids.o prioritizers/alua_rtpg.o
 
 all: $(LIBS)
libmultipath/checkers/Makefile +2 -2
@@ -14,19 +14,16 @@ LIBS= \
 	libcheckemc_clariion.so \
 	libcheckhp_sw.so \
 	libcheckrdac.so
 ifneq ($(ENABLE_RADOS),0)
 LIBS += libcheckrbd.so
 endif
 
 all: $(LIBS)
 
 libcheckrbd.so: rbd.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^ -lrados -ludev
 
-libcheckdirectio.so: libsg.o directio.o
+libcheckdirectio.so: ../libsg.o directio.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^ -laio
 
-libcheck%.so: libsg.o %.o
+libcheck%.so: ../libsg.o %.o

$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^

install:

libmultipath/checkers/emc_clariion.c +1 -1
index 9c1ffed..e4ba757 100644
@@ -12,7 +12,7 @@
 #include <errno.h>
 
 #include "../libmultipath/sg_include.h"
-#include "libsg.h"
+#include "../libmultipath/libsg.h"
 #include "checkers.h"
 #include "debug.h"
 #include "memory.h"
libmultipath/checkers/libsg.c deleted +0 -94

94 lines not on this page · raw message

libmultipath/checkers/libsg.h deleted +0 -9
@@ -1,9 +0,0 @@
-#ifndef _LIBSG_H
-#define _LIBSG_H
-
-#define SENSE_BUFF_LEN 32
-
-int sg_read (int sg_fd, unsigned char * buff, int buff_len,
-	     unsigned char * sense, int sense_len, unsigned int timeout);
-
-#endif /* _LIBSG_H */
libmultipath/checkers/readsector0.c +1 -1
index 8fccb46..d70c5c5 100644
@@ -4,7 +4,7 @@
 #include <stdio.h>
 
 #include "checkers.h"
-#include "libsg.h"
+#include "../libmultipath/libsg.h"
 
 #define MSG_READSECTOR0_UP	"readsector0 checker reports path is up"
 #define MSG_READSECTOR0_DOWN	"readsector0 checker reports path is down"
libmultipath/libsg.c new file +90 -0

90 lines not on this page · raw message

libmultipath/libsg.h new file +9 -0
@@ -0,0 +1,9 @@
+#ifndef _LIBSG_H
+#define _LIBSG_H
+
+#define SENSE_BUFF_LEN 32
+
+int sg_read (int sg_fd, unsigned char * buff, int buff_len,
+	     unsigned char * sense, int sense_len, unsigned int timeout);
+
+#endif /* _LIBSG_H */
libmultipath/prioritizers/Makefile +5 -1
index 36b42e4..7e3da51 100644
@@ -18,13 +18,17 @@ LIBS = \
 	libpriorandom.so \
 	libpriordac.so \
 	libprioweightedpath.so \
-	libpriosysfs.so
+	libpriodelayedpath.so \
+	libpriosysfs.so
 
 all: $(LIBS)
 
 libprioalua.so: alua.o alua_rtpg.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
+libpriodelayedpath.so: delayedpath.o  ../libsg.o
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
+
 libprio%.so: %.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
libmultipath/prioritizers/delayedpath.c new file +243 -0

243 lines not on this page · raw message

libmultipath/prioritizers/delayedpath.h new file +12 -0
new file mode 100644
index 0000000..ca89702
@@ -0,0 +1,14 @@
+#ifndef _DELAYEDPATH_H
+#define _DELAYEDPATH_H
+
+#define PRIO_DELAYED_PATH "delayedpath"
+#define USEC_PER_SEC      1000000LL
+#define USEC_PER_MSEC     1000LL
+#define USEC_PER_USEC     1LL
+
+static inline long long timeval_to_us(const struct timeval *tv) {
+	return ((long long) tv->tv_sec * USEC_PER_SEC) + tv->tv_usec; }
+
+#endif
multipath/multipath.conf.5 +19 -0
index 5939688..b787634 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I delayedpath
+Generate the path priority based on a time-delay algorithm.
+Requires prio_args keyword.
+.TP
 .I datacore
 .\" XXX
 ???. Requires prio_args keyword.
@@ -333,6 +337,21 @@ these values can be looked up through sysfs or by running \fImultipathd show pat
 "%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.*
 .RE
 .TP 12
+.I delayed
+Needs a value of the form
+\fI"<delay_interval|cons_num>"\fR
+.RS
+.TP 8
+.I delay_interval
+The interval values of average IO-time-delay between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXSEC, or XXXUSEC, or XXXMSEC. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, SEC [1, 60], USEC [1, 1000), MSEC [1, 1000),
+For example: 10SEC, or 100USEC, or 100MSEC. The default is: 10MSEC.
+.TP
+.I cons_num
+The number of read IOs sent to the current path continuously, used to calculate the average IO-time-delay. Valid Values: Integer, [3, 1000].
+For example: 30. The default is: 20.
+.RE
+.TP 12
 .I alua
 If \fIexclusive_pref_bit\fR is set, paths with the \fIpreferred path\fR bit
 set will always be in their own path group.
#3 lore raw
In reply to Yang Feng

Hello Yang,

thank you for your work. Please find my remarks below.

Quoted 14 lines · Yang Feng

Prioritizer for device mapper multipath, where the corresponding priority
values of specific paths are provided by a time-delay algorithm. And the
time-delay algorithm is dependent on the following arguments(delay_interval,
cons_num).
The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path
   continuously, the IOs' average delay can be calculated.
2. According to the average delay of each path and the weight value "delay_interval", the priority "rc" of each path can be provided.

Quoted 1 line

     delay_interval  delay_interval  delay_interval       delay_inter

How does this algorithm behave under load? Can we be sure that priorities don't start to fluctuate wildly because busy paths will usually have longer latencies than idle ones?

Quoted 7 lines

val

    |---------------|---------------|---------------|	 |----
-----------|
    |priority rank1 |priority rank2 |priority rank3 |... |priority
rank4 |
    |---------------|---------------|---------------|    |-----------
----|
Quoted 12 lines

                       Priority Rank Partitioning
---
 libmultipath/Makefile                   |   2 +- libmultipath/checkers/Makefile          |   7 +- libmultipath/checkers/emc_clariion.c    |   2 +- libmultipath/checkers/libsg.c           |  94 ------------ libmultipath/checkers/libsg.h           |   9 -- libmultipath/checkers/readsector0.c     |   2 +- libmultipath/libsg.c                    |  94 ++++++++++++ libmultipath/libsg.h                    |   9 ++ libmultipath/prioritizers/Makefile      |   6 +- libmultipath/prioritizers/delayedpath.c | 246

Why do you have to move libsg for this? It's already used by various checkers, why can't your checker do the same? If you really need to do it, you should at least separate that part of the patch from the added code.

Quoted 43 lines
libmultipath/prioritizers/delayedpath.c new file +2 -0
new file mode 100644
index 0000000..4c1cfea
@@ -0,0 +1,246 @@
+/*
+ * (C) Copyright HUAWEI Technology Corp. 2017, 2021   All Rights

Reserved.
+ *
+ * main.c
+ *
+ * Prioritizer for device mapper multipath, where the corresponding priority
+ * values of specific paths are provided by a time-delay algorithm. And the
+ * time-delay algorithm is dependent on arguments.
+ *
+ * The principle of the algorithm as follows:
+ * 1. By sending a certain number "cons_num" of read IOs to the current path
+ *    continuously, the IOs' average delay can be calculated.
+ * 2. According to the average delay of each path and the weight value
+ *    "delay_interval", the priority "rc" of each path can be provided.
+ *
+ * Author(s): Yang Feng <[email protected]>
+ *            Zou Ming <[email protected]>
+ *
+ * This file is released under the GPL.
+ */

libmultipath/prioritizers/delayedpath.c new file +10 -0
+#include <stdio.h>
+#include <ctype.h>
+#include <sys/time.h>
+
+#include "debug.h"
+#include "prio.h"
+#include "structs.h"
+#include "../libmultipath/libsg.h"
+
+#include "delayedpath.h"

libmultipath/prioritizers/delayedpath.c line 40

Quoted 10 lines
libmultipath/prioritizers/delayedpath.c new file +10 -0
+
+#define THRES_USEC_VALUE        300000000LL    /*USEC, 300SEC*/
+#define DEFAULT_DELAY_INTERVAL  10             /*MSEC*/
+#define DEFAULT_CONS_NUM        20    
+
+#define MAX_CHAR_SIZE           30
+
+#define CHAR_SEC                "SEC"
+#define CHAR_MSEC               "MSEC"
+#define CHAR_USEC               "USEC"

I suggest to use "s", "ms", and "us" here instead.

If you create an array of "const char*" instead like you did for conversion_ratio below, you could implement get_interval_type() more elegantly using a loop over that array.

Quoted 23 lines
libmultipath/prioritizers/delayedpath.c new file +23 -0
+
+enum interval_type {
+    INTERVAL_SEC,
+    INTERVAL_MSEC,
+    INTERVAL_USEC,
+    INTERVAL_INVALID
+};
+
+static int conversion_ratio[] = {
+	[INTERVAL_SEC]		= USEC_PER_SEC,
+	[INTERVAL_MSEC]	    = USEC_PER_MSEC,
+	[INTERVAL_USEC]		= USEC_PER_USEC,
+	[INTERVAL_INVALID]	= 0,
+};
+
+
+static int do_readsector0(int fd, unsigned int timeout)
+{
+	unsigned char buf[4096];
+	unsigned char sbuf[SENSE_BUFF_LEN];
+	int ret;
+
+	ret = sg_read(fd, &buf[0], 4096, &sbuf[0],

libmultipath/prioritizers/delayedpath.c line 73

Quoted 10 lines
libmultipath/prioritizers/delayedpath.c new file +10 -0
+		      SENSE_BUFF_LEN, timeout);
+    
+	return ret;
+}
+
+static int get_interval_type(char *source, char *type)
+{  
+    /*is USEC*/
+    if ((strstr(source, CHAR_USEC) != NULL)
+        && (strstr(source, CHAR_USEC)[4] == '_'))

Please avoid these double strstr() invocation. The compiler may optimize it away, but it just looks strange. The following would look better to me, and I find it actually more readable:

        if (((p = strstr(source, CHAR_USEC)) != NULL) && p[4] == '_')
Quoted 18 lines

+    {
+        memcpy(type, CHAR_USEC, strlen(CHAR_USEC)+1); +        return INTERVAL_USEC;
+    }

libmultipath/prioritizers/delayedpath.c new file +14 -0
+
+    /*is MSEC*/
+    if ((strstr(source, CHAR_MSEC) != NULL) 
+        && (strstr(source, CHAR_MSEC)[4] == '_'))
+    {
+        memcpy(type, CHAR_MSEC, strlen(CHAR_MSEC)+1);
+        return INTERVAL_MSEC;
+    }
+
+    /*is SEC*/
+	if ((strstr(source, CHAR_SEC) != NULL)
+        && (strstr(source, CHAR_SEC)[4] == '_'))
+    {
+        memcpy(type, CHAR_SEC, strlen(CHAR_SEC)+1);

libmultipath/prioritizers/delayedpath.c line 101

Quoted 10 lines
libmultipath/prioritizers/delayedpath.c new file +10 -0
+        return INTERVAL_SEC;
+    }
+
+    return INTERVAL_INVALID;
+}
+
+static int get_string_from_under(char *args,
+                                        char *beforestring,
+                                        char *afterstring,
+                                        int *type)

Maybe you could figure out a more descriptive name for this function?

A comment in the code showing how the string to be parsed typically looks like would be helpful for the reader.

Quoted 17 lines
libmultipath/prioritizers/delayedpath.c new file +17 -0
+{
+    char source[MAX_CHAR_SIZE];
+    char char_type[MAX_CHAR_SIZE];
+    char under[] = "_";
+    char *token  = NULL;
+    char *tmp = NULL;
+    char *saveptr = NULL;
+    unsigned int size = strlen(args);
+
+    if ((args == NULL) || (beforestring == NULL) 
+        || (afterstring == NULL) || (type == NULL))
+        return 0;
+
+    /* int type */
+    if ((size < 1) || (size > MAX_CHAR_SIZE-1))
+        return 0;
+    

libmultipath/prioritizers/delayedpath.c line 128

Quoted 10 lines
libmultipath/prioritizers/delayedpath.c new file +10 -0
+    memcpy(source, args, size+1);
+    if (strstr(source, under) == NULL)
+        return 0;
+
+    *type = get_interval_type(source, char_type);
+    if (*type == INTERVAL_INVALID)
+        return 0;
+
+    token = strtok_r(source, under, &saveptr);
+    token = strtok(token, char_type);

I'm pretty sure this is is not what you intended to write. If char_type is "usec", this would split the string at the possible delimiters 'u', 's', 'e', and 'c' (the 2nd argument of strtok(3) is not a sequence, but a 'set' of bytes). It might accidentally work with the input strings you are using (in particular because you only look at the first token), but nevertheless it's wrong.

Quoted 7 lines

+    if ((token == NULL) || (saveptr == NULL))
+        return 0;

libmultipath/prioritizers/delayedpath.c new file +5 -0
+
+    tmp = token;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+            return 0;

libmultipath/prioritizers/delayedpath.c line 145

Quoted 10 lines
libmultipath/prioritizers/delayedpath.c new file +10 -0
+
+    tmp = saveptr;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+            return 0;
+
+    strncpy(beforestring, token, strlen(token) + 1);
+    strncpy(afterstring, saveptr, strlen(saveptr) + 1);
+    return 1;
+}

I don't think it's safe to use saveptr the way you do it. The strtok_r man page says this parameter is for "internal use". While it makes sense to assume that it points to the next token, I'm not sure if that's guaranteed. You would be safe by calling

somevar = strtok_r(NULL, under, &saveptr)

and use "somevar".

In general, this whole parsing code is odd. IIUC this parses input looking like ([0-9]+)(SEC|MSEC|USEC)_([0-9]+) and sets beforestring, type, and afterstring to the regex matches \1, \2, and \3, respectively.

Why don't you start parsing from the beginning of the input, e.g. with strtoul(), and look at the rest later?

libmultipath/prioritizers/delayedpath.c line 158

Quoted 13 lines
libmultipath/prioritizers/delayedpath.c new file +13 -0
+
+int checkargvalid(int delay_interval, int cons_num, int type)
+{
+    if (type == INTERVAL_SEC)
+    {
+        if ((delay_interval < 1) || (delay_interval > 60))
+            return 0;
+    }
+    else if (type != INTERVAL_INVALID)
+    {
+        if ((delay_interval < 1) || (delay_interval >= 1000))
+            return 0;
+    }

You could be more forgiving here. 15000MSEC could be a legal value.

Quoted 14 lines

+
+    if ((cons_num < 3) || (cons_num > 1000))
+        return 0;

libmultipath/prioritizers/delayedpath.c new file +5 -0
+
+    return 1;
+}
+
+int get_delay_pref_arg(char *args, int *delay_interval, int

*cons_num, int *type)

libmultipath/prioritizers/delayedpath.c new file +5 -0
+{
+    char delayintervalstr[MAX_CHAR_SIZE];
+    char consnumstr[MAX_CHAR_SIZE];
+
+    if (get_string_from_under(args, delayintervalstr, consnumstr,
Quoted 2 lines

type) == 0)
+        return 0;

It might be good to write the parser so that the consnum part can be left out by the user, and assume a reasonable default in that case.

Quoted 37 lines
libmultipath/prioritizers/delayedpath.c new file +26 -0
+
+    *delay_interval = atoi(delayintervalstr);
+    *cons_num = atoi(consnumstr);
+
+    if (checkargvalid(*delay_interval, *cons_num, *type) == 0)
+        return 0;
+    
+    return 1;
+}
+
+long long get_conversion_ratio(int type)
+{
+    return conversion_ratio[type];
+}
+    
+int getprio (struct path *pp, char *args, unsigned int timeout)
+{
+    int rc, delay_interval, cons_num, type, temp;
+    long long delay, avgdelay, ratio;
+    long long min = THRES_USEC_VALUE;
+    long long max = 0;
+    long long toldelay = 0;
+    long long before, after;
+    struct timeval tv;
+
+    if (get_delay_pref_arg(args, &delay_interval, &cons_num, &type)

== 0)
+    {
+        condlog(3, "%s: get delay arg fail", pp->dev); +        delay_interval = DEFAULT_DELAY_INTERVAL; +        cons_num = DEFAULT_CONS_NUM;
+        type = INTERVAL_MSEC;
+    }

libmultipath/prioritizers/delayedpath.c new file +4 -0
+
+    temp = cons_num;
+    while (temp-- > 0)
+    {

libmultipath/prioritizers/delayedpath.c line 218

Quoted 10 lines
libmultipath/prioritizers/delayedpath.c new file +10 -0
+        (void)gettimeofday(&tv, NULL);
+        before = timeval_to_us(&tv);		
+
+        if (do_readsector0(pp->fd, timeout) == 2)
+        {
+            condlog(0, "%s: path down", pp->dev);
+            return 1;
+        }
+        
+        (void)gettimeofday(&tv, NULL);

It's better to use clock_gettime(CLOCK_MONOTONIC, ...) here. Then you can throw away the delay < 0 check below.

Quoted 26 lines

+        after = timeval_to_us(&tv);

libmultipath/prioritizers/delayedpath.c new file +24 -0
+
+        delay = after - before;
+        if (delay < 0)
+        {
+            condlog(0, "%s: delay calc error", pp->dev);
+            return 1;
+        }
+    	
+        min = (min <= delay) ? min : delay;
+        max = (max >= delay) ? max : delay;
+                
+        toldelay += delay;
+    }
+
+    toldelay -= min + max;
+    avgdelay = toldelay/(long long)(cons_num - 2);
+    if (avgdelay > THRES_USEC_VALUE) 
+    {           
+        condlog(0, "%s: avgdelay is more than thresold", pp->dev);
+        return 1;
+    }
+    
+	ratio = get_conversion_ratio(type);
+	rc = (int)(THRES_USEC_VALUE - (avgdelay/(((long

long)delay_interval) * ratio)));

libmultipath/prioritizers/delayedpath.c line 246

Quoted 3 lines
libmultipath/prioritizers/delayedpath.c new file +3 -0
+
+    return rc;
+}

Is it reasonable to do these interval calculations synchronously in getprio()? cons_num is limited to 1000, so this routine could issue 1000 reads on the device before returning. In particular if the device is under IO load and the delay is high, execution if this routine could be really slow.

It would make more sense to me to have a separate thread that calculates some sort of "running average" for the delay of the different paths, and have getprio() just fetch the current value of that variable.

Regards
Martin

Signature · 8 lines
Dr. Martin Wilck <[email protected]>, Tel. +49 (0)911 74053 2107
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
author
#4 lore raw

Hello Martin,

Thank you very much for your remarks. I am sorry for late reply, Please find my answer and the updated patch.

Quoted 19 lines

Hello Yang,

thank you for your work. Please find my remarks below.

Quoted 14 lines · Yang Feng

Prioritizer for device mapper multipath, where the corresponding priority
values of specific paths are provided by a time-delay algorithm. And the
time-delay algorithm is dependent on the following arguments(delay_interval,
cons_num).
The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path
continuously, the IOs' average delay can be calculated.
2. According to the average delay of each path and the weight value "delay_interval", the priority "rc" of each path can be provided.

Quoted 5 lines
Quoted 1 line

delay_interval delay_interval delay_interval delay_inter

How does this algorithm behave under load? Can we be sure that priorities don't start to fluctuate wildly because busy paths will usually have longer latencies than idle ones?

I have a lot of test under load. When the appropriate value of argument "delay_interval" is set, this algorithm behave well and can separate the paths who's average delay is more than others. When add a new path or the path's state change from down to up, getprio() of the prioritizer is triggered, and the current path is not under IOs.

Quoted 11 lines
Quoted 10 lines
 libmultipath/Makefile                   |   2 +-
 libmultipath/checkers/Makefile          |   7 +-
 libmultipath/checkers/emc_clariion.c    |   2 +-
 libmultipath/checkers/libsg.c           |  94 ------------
 libmultipath/checkers/libsg.h           |   9 --
 libmultipath/checkers/readsector0.c     |   2 +-
 libmultipath/libsg.c                    |  94 ++++++++++++
 libmultipath/libsg.h                    |   9 ++
 libmultipath/prioritizers/Makefile      |   6 +-
 libmultipath/prioritizers/delayedpath.c | 246 
Quoted 4 lines

Why do you have to move libsg for this? It's already used by various checkers, why can't your checker do the same? If you really need to do it, you should at least separate that part of the patch from the added code.

OK, this time, libsg will not be moved.

Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+
+#define CHAR_SEC                "SEC"
+#define CHAR_MSEC               "MSEC"
+#define CHAR_USEC               "USEC"

I suggest to use "s", "ms", and "us" here instead.

OK, as the following patch.

Quoted 3 lines

If you create an array of "const char*" instead like you did for conversion_ratio below, you could implement get_interval_type() more elegantly using a loop over that array.

OK, as the following patch.

Quoted 11 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+static int get_interval_type(char *source, char *type)
+{  
+    /*is USEC*/
+    if ((strstr(source, CHAR_USEC) != NULL)
+        && (strstr(source, CHAR_USEC)[4] == '_'))

Please avoid these double strstr() invocation. The compiler may optimize it away, but it just looks strange. The following would look better to me, and I find it actually more readable:

        if (((p = strstr(source, CHAR_USEC)) != NULL) && p[4] == '_')

OK, as the following patch.

Quoted 9 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+static int get_string_from_under(char *args,
+                                        char *beforestring,
+                                        char *afterstring,
+                                        int *type)

Maybe you could figure out a more descriptive name for this function?

A comment in the code showing how the string to be parsed typically looks like would be helpful for the reader.

OK, as the following patch.

Quoted 9 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+    token = strtok_r(source, under, &saveptr);
+    token = strtok(token, char_type);

I'm pretty sure this is is not what you intended to write. If char_type is "usec", this would split the string at the possible delimiters 'u', 's', 'e', and 'c' (the 2nd argument of strtok(3) is not a sequence, but a 'set' of bytes). It might accidentally work with the input strings you are using (in particular because you only look at the first token), but nevertheless it's wrong.

OK, as the following patch.

Quoted 23 lines
Quoted 7 lines
(quoted diff) +7 -0
+    if ((token == NULL) || (saveptr == NULL))
+        return 0;
+
+    tmp = token;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+            return 0;

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+    tmp = saveptr;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+            return 0;
+
+    strncpy(beforestring, token, strlen(token) + 1);
+    strncpy(afterstring, saveptr, strlen(saveptr) + 1);
+    return 1;
+}

I don't think it's safe to use saveptr the way you do it. The strtok_r man page says this parameter is for "internal use". While it makes sense to assume that it points to the next token, I'm not sure if that's guaranteed. You would be safe by calling

Quoted 3 lines

somevar = strtok_r(NULL, under, &saveptr)

and use "somevar".

OK, as the following patch.

Quoted 8 lines

In general, this whole parsing code is odd. IIUC this parses input looking like ([0-9]+)(SEC|MSEC|USEC)_([0-9]+) and sets beforestring, type, and afterstring to the regex matches \1, \2, and \3, respectively.

Why don't you start parsing from the beginning of the input, e.g. with strtoul(), and look at the rest later?

OK, as the following patch.

Quoted 7 lines
Quoted 7 lines
(quoted diff) +7 -0
+
+int checkargvalid(int delay_interval, int cons_num, int type)
+{
+    if (type == INTERVAL_SEC)
+    {
+        if ((delay_interval < 1) || (delay_interval > 60))
+            return 0;
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+    }
+    else if (type != INTERVAL_INVALID)
+    {
+        if ((delay_interval < 1) || (delay_interval >= 1000))
+            return 0;
+    }

You could be more forgiving here. 15000MSEC could be a legal value.

Because this value is more than 1 second, you can use the unit second.

Quoted 12 lines
Quoted 12 lines
(quoted diff) +8 -0
+    
+    if ((cons_num < 3) || (cons_num > 1000))
+        return 0;
+
+    return 1;
+}
+
+int get_delay_pref_arg(char *args, int *delay_interval, int

*cons_num, int *type)

(quoted diff) +2 -0
+{
+    char delayintervalstr[MAX_CHAR_SIZE];
Quoted 8 lines
Quoted 5 lines
(quoted diff) +3 -0
+    char consnumstr[MAX_CHAR_SIZE];
+
+    if (get_string_from_under(args, delayintervalstr, consnumstr,

type) == 0)
+ return 0;

It might be good to write the parser so that the consnum part can be left out by the user, and assume a reasonable default in that case.

OK, as the following patch.

Quoted 13 lines

Review

Quoted 12 lines
(quoted diff) +12 -0
+    while (temp-- > 0)
+    {
+        (void)gettimeofday(&tv, NULL);
+        before = timeval_to_us(&tv);		
+
+        if (do_readsector0(pp->fd, timeout) == 2)
+        {
+            condlog(0, "%s: path down", pp->dev);
+            return 1;
+        }
+        
+        (void)gettimeofday(&tv, NULL);
Quoted 2 lines

It's better to use clock_gettime(CLOCK_MONOTONIC, ...) here. Then you can throw away the delay < 0 check below.

OK, as the following patch.

Quoted 21 lines
Quoted 11 lines
(quoted diff) +10 -0
+    toldelay -= min + max;
+    avgdelay = toldelay/(long long)(cons_num - 2);
+    if (avgdelay > THRES_USEC_VALUE) 
+    {           
+        condlog(0, "%s: avgdelay is more than thresold", pp->dev);
+        return 1;
+    }
+    
+	ratio = get_conversion_ratio(type);
+	rc = (int)(THRES_USEC_VALUE - (avgdelay/(((long

long)delay_interval) * ratio)));

Review

Quoted 3 lines
(quoted diff) +3 -0
+
+    return rc;
+}

Is it reasonable to do these interval calculations synchronously in getprio()? cons_num is limited to 1000, so this routine could issue 1000 reads on the device before returning. In particular if the device is under IO load and the delay is high, execution if this routine could be really slow.

Quoted 8 lines

It would make more sense to me to have a separate thread that calculates some sort of "running average" for the delay of the different paths, and have getprio() just fetch the current value of that variable.

Regards
Martin

When add a new path or the path's state change from down to up, getprio() of the prioritizer is triggered, and the current path is not under IOs.
Usually, the 1000 reads will be finish fastly.
And have getprio() need to get the up-to-date average delay of the current path, these interval calculations synchronously is essential because of unreasonable asynchronously process's executing time D-value, particulayly when add a new path or the path's state change down to up.

Please find the up-to-date patch below:

---
From 1a9426dfbad00b5dbefc7020603e40e8896e4869 Mon Sep 17 00:00:00 2001
From: Yang Feng <[email protected]>
Date: Mon, 15 May 2017 18:33:29 +0800
Subject: [PATCH] [dm-devel] [PATCH] multipath-tools:Prioritizer based on a time-delay algorithm
Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a time-delay algorithm. And the time-delay algorithm is dependent on the following arguments(delay_interval, cons_num). The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path continuously, the IOs' average delay can be calculated.
2. According to the average delay of each path and the weight value "delay_interval", the priority "rc" of each path can be provided.

delay_interval delay_interval delay_interval delay_interval
|---------------|---------------|---------------| |---------------|
|priority rank 1|priority rank 2|priority rank 3|...|priority rank x|
|---------------|---------------|---------------| |---------------|
Priority Rank Partitioning

---
 libmultipath/prioritizers/Makefile      |   6 +-
 libmultipath/prioritizers/delayedpath.c | 261 ++++++++++++++++++++++++++++++++
 libmultipath/prioritizers/delayedpath.h |  17 +++
 multipath/multipath.conf.5              |  19 +++
 4 files changed, 302 insertions(+), 1 deletion(-)
 create mode 100644 libmultipath/prioritizers/delayedpath.c
 create mode 100644 libmultipath/prioritizers/delayedpath.h
libmultipath/prioritizers/Makefile +5 -1
index 36b42e4..8df5234 100644
@@ -18,13 +18,17 @@ LIBS = \
 	libpriorandom.so \
 	libpriordac.so \
 	libprioweightedpath.so \
-	libpriosysfs.so
+	libpriodelayedpath.so \
+	libpriosysfs.so
 
 all: $(LIBS)
 
 libprioalua.so: alua.o alua_rtpg.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
+libpriodelayedpath.so: delayedpath.o  ../checkers/libsg.o
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
+
 libprio%.so: %.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
libmultipath/prioritizers/delayedpath.c new file +261 -0

261 lines not on this page · raw message

libmultipath/prioritizers/delayedpath.h new file +17 -0
new file mode 100644
index 0000000..d8213e9
@@ -0,0 +1,17 @@
+#ifndef _DELAYEDPATH_H
+#define _DELAYEDPATH_H
+
+#define PRIO_DELAYED_PATH "delayedpath"
+
+#define PRIO_NO_INFORMATION 5
+
+#define USEC_PER_SEC      1000000LL
+#define USEC_PER_MSEC     1000LL
+#define USEC_PER_USEC     1LL
+
+static inline long long timeval_to_us(const struct timespec *tv)
+{
+	return ((long long) tv->tv_sec * USEC_PER_SEC) + (tv->tv_nsec >> 10);
+}
+
+#endif
multipath/multipath.conf.5 +19 -0
index 5939688..f1e126e 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I delayedpath
+Generate the path priority based on a time-delay algorithm.
+Requires prio_args keyword.
+.TP
 .I datacore
 .\" XXX
 ???. Requires prio_args keyword.
@@ -333,6 +337,21 @@ these values can be looked up through sysfs or by running \fImultipathd show pat
 "%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.*
 .RE
 .TP 12
+.I delayed
+Needs a value of the form
+\fI"<delay_interval|cons_num>"\fR
+.RS
+.TP 8
+.I delay_interval
+The interval values of average IO-time-delay between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 1000), us [1, 1000),
+For example: 10s, or 100us, or 100ms. The default is: 10ms.
+.TP
+.I cons_num
+The number of read IOs sent to the current path continuously, used to calculate the average IO-time-delay. Valid Values: Integer, [3, 1000].
+For example: 30. The default is: 20.
+.RE
+.TP 12
 .I alua
 If \fIexclusive_pref_bit\fR is set, paths with the \fIpreferred path\fR bit
 set will always be in their own path group.
author
#5 lore raw

Hello Martin,

How about this patch?
Thanks a lot,
Best regards.

Quoted 606 lines · Yang Feng

Hello Martin,

Thank you very much for your remarks. I am sorry for late reply, Please find my answer and the updated patch.

Quoted 19 lines

Hello Yang,

thank you for your work. Please find my remarks below.

Quoted 14 lines · Yang Feng

Prioritizer for device mapper multipath, where the corresponding priority
values of specific paths are provided by a time-delay algorithm. And the
time-delay algorithm is dependent on the following arguments(delay_interval,
cons_num).
The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path
continuously, the IOs' average delay can be calculated.
2. According to the average delay of each path and the weight value "delay_interval", the priority "rc" of each path can be provided.

Quoted 5 lines
Quoted 1 line

delay_interval delay_interval delay_interval delay_inter

How does this algorithm behave under load? Can we be sure that priorities don't start to fluctuate wildly because busy paths will usually have longer latencies than idle ones?

I have a lot of test under load. When the appropriate value of argument "delay_interval" is set, this algorithm behave well and can separate the paths who's average delay is more than others. When add a new path or the path's state change from down to up, getprio() of the prioritizer is triggered, and the current path is not under IOs.

Quoted 11 lines
Quoted 10 lines
 libmultipath/Makefile                   |   2 +-
 libmultipath/checkers/Makefile          |   7 +-
 libmultipath/checkers/emc_clariion.c    |   2 +-
 libmultipath/checkers/libsg.c           |  94 ------------
 libmultipath/checkers/libsg.h           |   9 --
 libmultipath/checkers/readsector0.c     |   2 +-
 libmultipath/libsg.c                    |  94 ++++++++++++
 libmultipath/libsg.h                    |   9 ++
 libmultipath/prioritizers/Makefile      |   6 +-
 libmultipath/prioritizers/delayedpath.c | 246 
Quoted 4 lines

Why do you have to move libsg for this? It's already used by various checkers, why can't your checker do the same? If you really need to do it, you should at least separate that part of the patch from the added code.

OK, this time, libsg will not be moved.

Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+
+#define CHAR_SEC                "SEC"
+#define CHAR_MSEC               "MSEC"
+#define CHAR_USEC               "USEC"

I suggest to use "s", "ms", and "us" here instead.

OK, as the following patch.

Quoted 3 lines

If you create an array of "const char*" instead like you did for conversion_ratio below, you could implement get_interval_type() more elegantly using a loop over that array.

OK, as the following patch.

Quoted 11 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+static int get_interval_type(char *source, char *type)
+{  
+    /*is USEC*/
+    if ((strstr(source, CHAR_USEC) != NULL)
+        && (strstr(source, CHAR_USEC)[4] == '_'))

Please avoid these double strstr() invocation. The compiler may optimize it away, but it just looks strange. The following would look better to me, and I find it actually more readable:

        if (((p = strstr(source, CHAR_USEC)) != NULL) && p[4] == '_')

OK, as the following patch.

Quoted 9 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+static int get_string_from_under(char *args,
+                                        char *beforestring,
+                                        char *afterstring,
+                                        int *type)

Maybe you could figure out a more descriptive name for this function?

A comment in the code showing how the string to be parsed typically looks like would be helpful for the reader.

OK, as the following patch.

Quoted 9 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+    token = strtok_r(source, under, &saveptr);
+    token = strtok(token, char_type);

I'm pretty sure this is is not what you intended to write. If char_type is "usec", this would split the string at the possible delimiters 'u', 's', 'e', and 'c' (the 2nd argument of strtok(3) is not a sequence, but a 'set' of bytes). It might accidentally work with the input strings you are using (in particular because you only look at the first token), but nevertheless it's wrong.

OK, as the following patch.

Quoted 23 lines
Quoted 7 lines
libmultipath/prioritizers/delayedpath.c new file +7 -0
+    if ((token == NULL) || (saveptr == NULL))
+        return 0;
+
+    tmp = token;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+            return 0;

libmultipath/prioritizers/delayedpath.c line 159

Quoted 10 lines
libmultipath/prioritizers/delayedpath.c new file +10 -0
+
+    tmp = saveptr;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+            return 0;
+
+    strncpy(beforestring, token, strlen(token) + 1);
+    strncpy(afterstring, saveptr, strlen(saveptr) + 1);
+    return 1;
+}

I don't think it's safe to use saveptr the way you do it. The strtok_r man page says this parameter is for "internal use". While it makes sense to assume that it points to the next token, I'm not sure if that's guaranteed. You would be safe by calling

Quoted 3 lines

somevar = strtok_r(NULL, under, &saveptr)

and use "somevar".

OK, as the following patch.

Quoted 8 lines

In general, this whole parsing code is odd. IIUC this parses input looking like ([0-9]+)(SEC|MSEC|USEC)_([0-9]+) and sets beforestring, type, and afterstring to the regex matches \1, \2, and \3, respectively.

Why don't you start parsing from the beginning of the input, e.g. with strtoul(), and look at the rest later?

OK, as the following patch.

Quoted 7 lines
Quoted 7 lines
libmultipath/prioritizers/delayedpath.c new file +7 -0
+
+int checkargvalid(int delay_interval, int cons_num, int type)
+{
+    if (type == INTERVAL_SEC)
+    {
+        if ((delay_interval < 1) || (delay_interval > 60))
+            return 0;
Quoted 8 lines

libmultipath/prioritizers/delayedpath.c line 177

Quoted 6 lines
libmultipath/prioritizers/delayedpath.c new file +6 -0
+    }
+    else if (type != INTERVAL_INVALID)
+    {
+        if ((delay_interval < 1) || (delay_interval >= 1000))
+            return 0;
+    }

You could be more forgiving here. 15000MSEC could be a legal value.

Because this value is more than 1 second, you can use the unit second.

Quoted 12 lines
Quoted 12 lines
libmultipath/prioritizers/delayedpath.c new file +8 -0
+    
+    if ((cons_num < 3) || (cons_num > 1000))
+        return 0;
+
+    return 1;
+}
+
+int get_delay_pref_arg(char *args, int *delay_interval, int

*cons_num, int *type)

(quoted diff) +2 -0
+{
+    char delayintervalstr[MAX_CHAR_SIZE];
Quoted 8 lines
Quoted 5 lines
(quoted diff) +3 -0
+    char consnumstr[MAX_CHAR_SIZE];
+
+    if (get_string_from_under(args, delayintervalstr, consnumstr,

type) == 0)
+ return 0;

It might be good to write the parser so that the consnum part can be left out by the user, and assume a reasonable default in that case.

OK, as the following patch.

Quoted 13 lines

libmultipath/prioritizers/delayedpath.c

Quoted 12 lines
libmultipath/prioritizers/delayedpath.c new file +12 -0
+    while (temp-- > 0)
+    {
+        (void)gettimeofday(&tv, NULL);
+        before = timeval_to_us(&tv);		
+
+        if (do_readsector0(pp->fd, timeout) == 2)
+        {
+            condlog(0, "%s: path down", pp->dev);
+            return 1;
+        }
+        
+        (void)gettimeofday(&tv, NULL);
Quoted 2 lines

It's better to use clock_gettime(CLOCK_MONOTONIC, ...) here. Then you can throw away the delay < 0 check below.

OK, as the following patch.

Quoted 21 lines
Quoted 11 lines
libmultipath/prioritizers/delayedpath.c new file +10 -0
+    toldelay -= min + max;
+    avgdelay = toldelay/(long long)(cons_num - 2);
+    if (avgdelay > THRES_USEC_VALUE) 
+    {           
+        condlog(0, "%s: avgdelay is more than thresold", pp->dev);
+        return 1;
+    }
+    
+	ratio = get_conversion_ratio(type);
+	rc = (int)(THRES_USEC_VALUE - (avgdelay/(((long

long)delay_interval) * ratio)));

libmultipath/prioritizers/delayedpath.c line 261

Quoted 3 lines
libmultipath/prioritizers/delayedpath.c new file +3 -0
+
+    return rc;
+}

Is it reasonable to do these interval calculations synchronously in getprio()? cons_num is limited to 1000, so this routine could issue 1000 reads on the device before returning. In particular if the device is under IO load and the delay is high, execution if this routine could be really slow.

Quoted 8 lines

It would make more sense to me to have a separate thread that calculates some sort of "running average" for the delay of the different paths, and have getprio() just fetch the current value of that variable.

Regards
Martin

When add a new path or the path's state change from down to up, getprio() of the prioritizer is triggered, and the current path is not under IOs.
Usually, the 1000 reads will be finish fastly.
And have getprio() need to get the up-to-date average delay of the current path, these interval calculations synchronously is essential because of unreasonable asynchronously process's executing time D-value, particulayly when add a new path or the path's state change down to up.

Please find the up-to-date patch below:

---

Quoted 1 line

From 1a9426dfbad00b5dbefc7020603e40e8896e4869 Mon Sep 17 00:00:00 2001

From: Yang Feng <[email protected]>
Date: Mon, 15 May 2017 18:33:29 +0800
Subject: [PATCH] [dm-devel] [PATCH] multipath-tools:Prioritizer based on a time-delay algorithm
Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a time-delay algorithm. And the time-delay algorithm is dependent on the following arguments(delay_interval, cons_num). The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path continuously, the IOs' average delay can be calculated.
2. According to the average delay of each path and the weight value "delay_interval", the priority "rc" of each path can be provided.

delay_interval delay_interval delay_interval delay_interval
|---------------|---------------|---------------| |---------------|
|priority rank 1|priority rank 2|priority rank 3|...|priority rank x|
|---------------|---------------|---------------| |---------------|
Priority Rank Partitioning

---
 libmultipath/prioritizers/Makefile      |   6 +-
 libmultipath/prioritizers/delayedpath.c | 261 ++++++++++++++++++++++++++++++++
 libmultipath/prioritizers/delayedpath.h |  17 +++
 multipath/multipath.conf.5              |  19 +++
 4 files changed, 302 insertions(+), 1 deletion(-)
 create mode 100644 libmultipath/prioritizers/delayedpath.c
 create mode 100644 libmultipath/prioritizers/delayedpath.h
libmultipath/prioritizers/Makefile +5 -1
index 36b42e4..8df5234 100644
@@ -18,13 +18,17 @@ LIBS = \
 	libpriorandom.so \
 	libpriordac.so \
 	libprioweightedpath.so \
-	libpriosysfs.so
+	libpriodelayedpath.so \
+	libpriosysfs.so
 
 all: $(LIBS)
 
 libprioalua.so: alua.o alua_rtpg.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
+libpriodelayedpath.so: delayedpath.o  ../checkers/libsg.o
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
+
 libprio%.so: %.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
libmultipath/prioritizers/delayedpath.c new file +261 -0
new file mode 100644
index 0000000..0490e8d
@@ -0,0 +1,261 @@
+/*
+ * (C) Copyright HUAWEI Technology Corp. 2017, 2021   All Rights Reserved.
+ *
+ * main.c
+ *
+ * Prioritizer for device mapper multipath, where the corresponding priority
+ * values of specific paths are provided by a time-delay algorithm. And the
+ * time-delay algorithm is dependent on arguments.
+ *
+ * The principle of the algorithm as follows:
+ * 1. By sending a certain number "cons_num" of read IOs to the current path
+ *    continuously, the IOs' average delay can be calculated.
+ * 2. According to the average delay of each path and the weight value
+ *    "delay_interval", the priority "rc" of each path can be provided.
+ *
+ * Author(s): Yang Feng <[email protected]>
+ *            Zou Ming <[email protected]>
+ *
+ * This file is released under the GPL.
+ */
+#include <stdio.h>
+#include <ctype.h>
+#include <time.h>
+
+#include "debug.h"
+#include "prio.h"
+#include "structs.h"
+#include "../checkers/libsg.h"
+
+#include "delayedpath.h"
+
+#define THRES_USEC_VALUE        300000000LL    /*USEC, 300SEC*/
+#define DEFAULT_DELAY_INTERVAL  10             /*MSEC*/
+#define DEFAULT_CONS_NUM        20
+
+#define MAX_CHAR_SIZE           30
+
+#define CHAR_SEC                "s"
+#define CHAR_MSEC               "ms"
+#define CHAR_USEC               "us"
+
+enum interval_type {
+    INTERVAL_SEC,
+    INTERVAL_MSEC,
+    INTERVAL_USEC,
+    INTERVAL_INVALID
+};
+
+/* interval_unit_str and interval_unit_type keep the same assignment sequence */
+static const char interval_unit_str[][MAX_CHAR_SIZE] = {
+    CHAR_USEC, CHAR_MSEC, CHAR_SEC
+};
+static const int interval_unit_type[] = {
+    INTERVAL_USEC, INTERVAL_MSEC, INTERVAL_SEC
+};
+
+static const int conversion_ratio[] = {
+	[INTERVAL_SEC]		= USEC_PER_SEC,
+	[INTERVAL_MSEC]	        = USEC_PER_MSEC,
+	[INTERVAL_USEC]		= USEC_PER_USEC,
+	[INTERVAL_INVALID]	= 0
+};
+
+
+static int do_readsector0(int fd, unsigned int timeout)
+{
+	unsigned char buf[4096];
+	unsigned char sbuf[SENSE_BUFF_LEN];
+	int ret;
+
+	ret = sg_read(fd, &buf[0], 4096, &sbuf[0],
+		      SENSE_BUFF_LEN, timeout);
+
+	return ret;
+}
+
+static int get_interval_type(char *source, char *type)
+{
+    char *p;
+    int size;
+    int i;
+
+    for (i = 0; i < sizeof(interval_unit_str)/MAX_CHAR_SIZE; i++)
+    {
+        size = strlen(interval_unit_str[i]);
+        p = strstr(source, interval_unit_str[i]);
+        if (p != NULL && p[size] == '|')
+        {
+            memcpy(type, interval_unit_str[i], size+1);
+            return interval_unit_type[i];
+        }
+    }
+
+    return INTERVAL_INVALID;
+}
+
+/* In multipath.conf, args form: delay_interval|cons_num. For example,
+*  args is "10ms|20", this function can get 10, ms, and 20.
+*/
+static int get_digit_and_type(char *args,
+                              int *interval,
+                              int *consnum,
+                              int *type)
+{
+    char typestr[MAX_CHAR_SIZE];
+    char source[MAX_CHAR_SIZE];
+    char vertica[] = "|";
+    char *tokenbefore = NULL;
+    char *tokenafter = NULL;
+    char *tmp = NULL;
+    unsigned int size = strlen(args);
+
+    if ((args == NULL) || (interval == NULL)
+        || (consnum == NULL) || (type == NULL))
+        return 0;
+
+    /* int type */
+    if ((size < 1) || (size > MAX_CHAR_SIZE-1))
+        return 0;
+
+    memcpy(source, args, size+1);
+    if (strstr(source, vertica) == NULL)
+        return 0;
+
+    *type = get_interval_type(source, typestr);
+    if (*type == INTERVAL_INVALID)
+    {
+        condlog(0, "delay_interval type is invalid");
+        return 0;
+    }
+
+    tokenbefore = strtok(source, vertica);
+    tokenafter = strtok(NULL, vertica);
+    typestr[1] = '\0';
+    tokenbefore = strtok(tokenbefore, typestr);
+    if ((tokenbefore == NULL) || (tokenafter == NULL))
+        return 0;
+
+    tmp = tokenbefore;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+        {
+            condlog(0, "delay_interval string include invalid char");
+            return 0;
+        }
+
+    tmp = tokenafter;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+        {
+            condlog(0, "cons_num string include invalid char");
+            return 0;
+        }
+
+    *interval = atoi(tokenbefore);
+    *consnum = atoi(tokenafter);
+
+    return 1;
+}
+
+int check_args_valid(int delay_interval, int cons_num, int type)
+{
+    if (type == INTERVAL_SEC)
+    {
+        if ((delay_interval < 1) || (delay_interval > 60))
+        {
+            condlog(0, "delay_interval values is invalid");
+            return 0;
+        }
+    }
+    else if (type != INTERVAL_INVALID)
+    {
+        if ((delay_interval < 1) || (delay_interval >= 1000))
+        {
+            condlog(0, "delay_interval values is invalid");
+            return 0;
+        }
+    }
+
+    if ((cons_num < 3) || (cons_num > 1000))
+    {
+        condlog(0, "cons_num values is invalid");
+        return 0;
+    }
+
+    return 1;
+}
+
+int get_delay_pref_arg(char *args, int *delay_interval, int *cons_num, int *interval_type)
+{
+    if (get_digit_and_type(args, delay_interval, cons_num, interval_type) == 0)
+        return 0;
+
+    if (check_args_valid(*delay_interval, *cons_num, *interval_type) == 0)
+        return 0;
+
+    return 1;
+}
+
+long long get_conversion_ratio(int type)
+{
+    return conversion_ratio[type];
+}
+
+int getprio (struct path *pp, char *args, unsigned int timeout)
+{
+    int rc, delay_interval, cons_num, type, temp;
+    long long delay, avgdelay, ratio;
+    long long min = THRES_USEC_VALUE;
+    long long max = 0;
+    long long toldelay = 0;
+    long long before, after;
+    struct timespec tv;
+
+	if (pp->fd < 0)
+	    return -PRIO_NO_INFORMATION;
+
+    if (get_delay_pref_arg(args, &delay_interval, &cons_num, &type) == 0)
+    {
+        condlog(3, "%s: get delay arg fail", pp->dev);
+        delay_interval = DEFAULT_DELAY_INTERVAL;
+        cons_num = DEFAULT_CONS_NUM;
+        type = INTERVAL_MSEC;
+    }
+
+    temp = cons_num;
+    while (temp-- > 0)
+    {
+        (void)clock_gettime(CLOCK_MONOTONIC, &tv);
+        before = timeval_to_us(&tv);		
+
+        if (do_readsector0(pp->fd, timeout) == 2)
+        {
+            condlog(0, "%s: path down", pp->dev);
+            return -1;
+        }
+
+        (void)clock_gettime(CLOCK_MONOTONIC, &tv);
+        after = timeval_to_us(&tv);
+
+        delay = after - before;
+    	
+        min = (min <= delay) ? min : delay;
+        max = (max >= delay) ? max : delay;
+
+        toldelay += delay;
+    }
+
+    toldelay -= min + max;
+    avgdelay = toldelay/(long long)(cons_num - 2);
+    if (avgdelay > THRES_USEC_VALUE)
+    {
+        condlog(0, "%s: avgdelay is more than thresold", pp->dev);
+        return 1;
+    }
+
+	ratio = get_conversion_ratio(type);
+	rc = (int)(THRES_USEC_VALUE - (avgdelay/(((long long)delay_interval) * ratio)));
+
+    return rc;
+}
libmultipath/prioritizers/delayedpath.h new file +17 -0
new file mode 100644
index 0000000..d8213e9
@@ -0,0 +1,17 @@
+#ifndef _DELAYEDPATH_H
+#define _DELAYEDPATH_H
+
+#define PRIO_DELAYED_PATH "delayedpath"
+
+#define PRIO_NO_INFORMATION 5
+
+#define USEC_PER_SEC      1000000LL
+#define USEC_PER_MSEC     1000LL
+#define USEC_PER_USEC     1LL
+
+static inline long long timeval_to_us(const struct timespec *tv)
+{
+	return ((long long) tv->tv_sec * USEC_PER_SEC) + (tv->tv_nsec >> 10);
+}
+
+#endif
multipath/multipath.conf.5 +19 -0
index 5939688..f1e126e 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I delayedpath
+Generate the path priority based on a time-delay algorithm.
+Requires prio_args keyword.
+.TP
 .I datacore
 .\" XXX
 ???. Requires prio_args keyword.
@@ -333,6 +337,21 @@ these values can be looked up through sysfs or by running \fImultipathd show pat
 "%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.*
 .RE
 .TP 12
+.I delayed
+Needs a value of the form
+\fI"<delay_interval|cons_num>"\fR
+.RS
+.TP 8
+.I delay_interval
+The interval values of average IO-time-delay between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 1000), us [1, 1000),
+For example: 10s, or 100us, or 100ms. The default is: 10ms.
+.TP
+.I cons_num
+The number of read IOs sent to the current path continuously, used to calculate the average IO-time-delay. Valid Values: Integer, [3, 1000].
+For example: 30. The default is: 20.
+.RE
+.TP 12
 .I alua
 If \fIexclusive_pref_bit\fR is set, paths with the \fIpreferred path\fR bit
 set will always be in their own path group.
#6 lore raw
In reply to Yang Feng

Hi Yang,

Thanks for improving your patch. Please find my comments below.

Quoted 35 lines · Yang Feng

Hello Martin,

Thank you very much for your remarks. I am sorry for late reply, Please find my answer and the updated patch.

Quoted 23 lines

Hello Yang,

thank you for your work. Please find my remarks below.

Quoted 18 lines · Yang Feng

Prioritizer for device mapper multipath, where the corresponding priority
values of specific paths are provided by a time-delay algorithm. And
the
time-delay algorithm is dependent on the following arguments(delay_interval,
cons_num).
The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current
path
   continuously, the IOs' average delay can be calculated.
2. According to the average delay of each path and the weight value
   "delay_interval", the priority "rc" of each path can be provided.

Quoted 6 lines
Quoted 2 lines

     delay_interval  delay_interval  delay_interval       delay_i nter

How does this algorithm behave under load? Can we be sure that priorities don't start to fluctuate wildly because busy paths will usually have longer latencies than idle ones?

Quoted 18 lines

I have a lot of test under load. When the appropriate value of argument "delay_interval" is set,
this algorithm behave well and can separate the paths who's average delay is more than others.
When add a new path or the path's state change from down to up, getprio() of the prioritizer is triggered, and
the current path is not under IOs.

Quoted 10 lines
Quoted 10 lines

 libmultipath/Makefile                   |   2 +- libmultipath/checkers/Makefile          |   7 +- libmultipath/checkers/emc_clariion.c    |   2 +- libmultipath/checkers/libsg.c           |  94 ------------ libmultipath/checkers/libsg.h           |   9 -- libmultipath/checkers/readsector0.c     |   2 +- libmultipath/libsg.c                    |  94 ++++++++++++ libmultipath/libsg.h                    |   9 ++ libmultipath/prioritizers/Makefile      |   6 +- libmultipath/prioritizers/delayedpath.c | 246

Please think about the name once again. Maybe you should call it "io_latency" or "path_latency" instead of "delayedpath"?

Quoted 109 lines
Quoted 8 lines

Why do you have to move libsg for this? It's already used by various
checkers, why can't your checker do the same? If you really need to do
it, you should at least separate that part of the patch from the added
code.

OK, this time, libsg will not be moved.

Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+
+#define CHAR_SEC                "SEC"
+#define CHAR_MSEC               "MSEC"
+#define CHAR_USEC               "USEC"

I suggest to use "s", "ms", and "us" here instead.

OK, as the following patch.

Quoted 4 lines

If you create an array of "const char*" instead like you did for conversion_ratio below, you could implement get_interval_type() more
elegantly using a loop over that array.

OK, as the following patch.

Quoted 10 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+static int get_interval_type(char *source, char *type)
+{  
+    /*is USEC*/
+    if ((strstr(source, CHAR_USEC) != NULL)
+        && (strstr(source, CHAR_USEC)[4] == '_'))

Please avoid these double strstr() invocation. The compiler may optimize it away, but it just looks strange. The following would look better to me, and I find it actually more readable:

Quoted 2 lines

        if (((p = strstr(source, CHAR_USEC)) != NULL) && p[4] == '_')

OK, as the following patch.

Quoted 10 lines
Quoted 4 lines

+static int get_string_from_under(char *args,
+                                        char *beforestring, +                                        char *afterstring, +                                        int *type)

Maybe you could figure out a more descriptive name for this function?

A comment in the code showing how the string to be parsed typically looks like would be helpful for the reader.

OK, as the following patch.

Quoted 14 lines
Quoted 2 lines

+    token = strtok_r(source, under, &saveptr);
+    token = strtok(token, char_type);

I'm pretty sure this is is not what you intended to write. If char_type
is "usec", this would split the string at the possible delimiters 'u',
's', 'e', and 'c' (the 2nd argument of strtok(3) is not a sequence, but
a 'set' of bytes). It might accidentally work with the input strings
you are using (in particular because you only look at the first token),
but nevertheless it's wrong.

OK, as the following patch.

Quoted 24 lines
Quoted 7 lines

+    if ((token == NULL) || (saveptr == NULL))
+        return 0;

libmultipath/prioritizers/delayedpath.c new file +5 -0
+
+    tmp = token;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+            return 0;

libmultipath/prioritizers/delayedpath.c line 159

Quoted 10 lines
libmultipath/prioritizers/delayedpath.c new file +10 -0
+
+    tmp = saveptr;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+            return 0;
+
+    strncpy(beforestring, token, strlen(token) + 1);
+    strncpy(afterstring, saveptr, strlen(saveptr) + 1);
+    return 1;
+}

I don't think it's safe to use saveptr the way you do it. The strtok_r
man page says this parameter is for "internal use". While it makes sense to assume that it points to the next token, I'm not sure if that's guaranteed. You would be safe by calling

Quoted 3 lines

    somevar = strtok_r(NULL, under, &saveptr)

and use "somevar".

OK, as the following patch.

Quoted 7 lines

In general, this whole parsing code is odd. IIUC this parses input looking like ([0-9]+)(SEC|MSEC|USEC)_([0-9]+) and sets beforestring,
type, and afterstring to the regex matches \1, \2, and \3, respectively.

Quoted 5 lines
Quoted 3 lines

Why don't you start parsing from the beginning of the input, e.g. with
strtoul(), and look at the rest later?

OK, as the following patch.

Hm, I can't see a lot of difference in the parsing code wrt the previous version. IMO it's still non-straightforward and hard to comprehend. Maybe I didn't express myself clearly enough. Here is how I'd code this:

1. Verify that the string starts with a digit. Error if it does not.
2. Parse the delay interval using strtoul().
3. The "end" pointer of strtoul() points to the unit, which has to be
"s", "ms" or "us". Verify, and set the unit accordingly.
4. Verify that the next character is '|', and that it's followed by a
digit.
5. Parse the number with strtoul()
6. Verify that there's no garbage at the end of the string.

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
libmultipath/prioritizers/delayedpath.c new file +7 -0
+
+int checkargvalid(int delay_interval, int cons_num, int type)
+{
+    if (type == INTERVAL_SEC)
+    {
+        if ((delay_interval < 1) || (delay_interval > 60))
+            return 0;
Quoted 8 lines
Quoted 6 lines

+    }
+    else if (type != INTERVAL_INVALID)
+    {
+        if ((delay_interval < 1) || (delay_interval >= 1000)) +            return 0;
+    }

You could be more forgiving here. 15000MSEC could be a legal value.

Quoted 2 lines

Because this value is more than 1 second, you can use the unit second.

Please follow the https://en.wikipedia.org/wiki/Robustness_principle.
If a user enters "1500ms" here, the parsing will silently fail, and with it the whole prio algorithm. This will cause user confusion. Please don't do this.

Quoted 78 lines
Quoted 15 lines
Quoted 15 lines

+
+    if ((cons_num < 3) || (cons_num > 1000))
+        return 0;

libmultipath/prioritizers/delayedpath.c new file +5 -0
+
+    return 1;
+}
+
+int get_delay_pref_arg(char *args, int *delay_interval, int

*cons_num, int *type)

libmultipath/prioritizers/delayedpath.c new file +4 -0
+{
+    char delayintervalstr[MAX_CHAR_SIZE];
+    char consnumstr[MAX_CHAR_SIZE];
+
Quoted 8 lines
Quoted 4 lines

+    if (get_string_from_under(args, delayintervalstr, consnumstr,
type) == 0)
+        return 0;

It might be good to write the parser so that the consnum part can be
left out by the user, and assume a reasonable default in that case.

OK, as the following patch.

Quoted 13 lines
Quoted 4 lines

+    while (temp-- > 0)
+    {
+        (void)gettimeofday(&tv, NULL);
+        before = timeval_to_us(&tv);

Quoted 8 lines

+
+        if (do_readsector0(pp->fd, timeout) == 2) +        {
+            condlog(0, "%s: path down", pp->dev); +            return 1;
+        }
+
+        (void)gettimeofday(&tv, NULL);

Quoted 3 lines

It's better to use clock_gettime(CLOCK_MONOTONIC, ...) here. Then you
can throw away the delay < 0 check below.

OK, as the following patch.

Quoted 24 lines
Quoted 12 lines

+    toldelay -= min + max;
+    avgdelay = toldelay/(long long)(cons_num - 2); +    if (avgdelay > THRES_USEC_VALUE)
+    {
+        condlog(0, "%s: avgdelay is more than thresold", pp-

Quoted 1 line

dev);

+        return 1;
+    }
+

(quoted diff) +2 -0
+	ratio = get_conversion_ratio(type);
+	rc = (int)(THRES_USEC_VALUE - (avgdelay/(((long

long)delay_interval) * ratio)));

libmultipath/prioritizers/delayedpath.c line 261

Quoted 3 lines
libmultipath/prioritizers/delayedpath.c new file +3 -0
+
+    return rc;
+}

Is it reasonable to do these interval calculations synchronously in getprio()? cons_num is limited to 1000, so this routine could issue 1000 reads on the device before returning. In particular if the device
is under IO load and the delay is high, execution if this routine could
be really slow.

Quoted 8 lines

It would make more sense to me to have a separate thread that calculates some sort of "running average" for the delay of the different paths, and have getprio() just fetch the current value of that variable.

Regards
Martin

Quoted 9 lines

When add a new path or the path's state change from down to up, getprio() of the prioritizer is triggered, and
the current path is not under IOs.
Usually, the 1000 reads will be finish fastly.
And have getprio() need to get the up-to-date average delay of the current path, these interval calculations synchronously is essential because of unreasonable asynchronously process's executing time D-value, particulayly when add a new path or the path's state change down to up.

I am sorry, I was on the wrong track here. Doing this asynchronously is really not going to work. Sorry about that. The default "cons" value is 20, so that should be acceptable.

However please consider lowering the upper bound, I kind of doubt that 1000 IOs will finish quickly. More often than not, a lot of paths will appear at the same time (e.g. if a port of a storage array is enabled) and we'll have to send 1000 IOs to each one.

Quoted 126 lines

Please find the up-to-date patch below:

---
From 1a9426dfbad00b5dbefc7020603e40e8896e4869 Mon Sep 17 00:00:00 2001
From: Yang Feng <[email protected]>
Date: Mon, 15 May 2017 18:33:29 +0800
Subject: [PATCH] [dm-devel] [PATCH] multipath-tools:Prioritizer based on a time-delay algorithm
 Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a time-delay algorithm. And the time-delay algorithm is dependent on the following arguments(delay_interval, cons_num). The principle of the algorithm is illustrated as follows:
 1. By sending a certain number "cons_num" of read IOs to the current path    continuously, the IOs' average delay can be calculated.
 2. According to the average delay of each path and the weight value "delay_interval", the priority "rc" of each path can be provided.

           delay_interval  delay_interval  delay_interval      delay_ interval

	 |---------------|---------------|---------------|   |---------
------|
	 |priority rank 1|priority rank 2|priority rank 3|...|priority
rank x|
         |---------------|---------------|---------------|   |-------
--------|

               Priority Rank Partitioning
---
 libmultipath/prioritizers/Makefile      |   6 +- libmultipath/prioritizers/delayedpath.c | 261
++++++++++++++++++++++++++++++++
 libmultipath/prioritizers/delayedpath.h |  17 +++ multipath/multipath.conf.5              |  19 +++

 4 files changed, 302 insertions(+), 1 deletion(-)

 create mode 100644 libmultipath/prioritizers/delayedpath.c create mode 100644 libmultipath/prioritizers/delayedpath.h

libmultipath/prioritizers/Makefile +2 -1
index 36b42e4..8df5234 100644
@@ -18,13 +18,17 @@ LIBS = \
  	libpriorandom.so \
  	libpriordac.so \
  	libprioweightedpath.so \
-	libpriosysfs.so
+	libpriodelayedpath.so \
+	libpriosysfs.so
 
  all: $(LIBS)
 

 libprioalua.so: alua.o alua_rtpg.o
  $(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^

libmultipath/prioritizers/Makefile +3 -0
+libpriodelayedpath.so: delayedpath.o  ../checkers/libsg.o
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
+

 libprio%.so: %.o
  $(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^

libmultipath/prioritizers/delayedpath.c new file +2 -0
new file mode 100644
index 0000000..0490e8d
@@ -0,0 +1,261 @@
+/*
+ * (C) Copyright HUAWEI Technology Corp. 2017, 2021   All Rights

Reserved.
+ *
+ * main.c
+ *
+ * Prioritizer for device mapper multipath, where the corresponding priority
+ * values of specific paths are provided by a time-delay algorithm. And the
+ * time-delay algorithm is dependent on arguments.
+ *
+ * The principle of the algorithm as follows:
+ * 1. By sending a certain number "cons_num" of read IOs to the current path
+ *    continuously, the IOs' average delay can be calculated.
+ * 2. According to the average delay of each path and the weight value
+ *    "delay_interval", the priority "rc" of each path can be provided.
+ *
+ * Author(s): Yang Feng <[email protected]>
+ *            Zou Ming <[email protected]>
+ *
+ * This file is released under the GPL.
+ */

libmultipath/prioritizers/delayedpath.c new file +29 -0
+#include <stdio.h>
+#include <ctype.h>
+#include <time.h>
+
+#include "debug.h"
+#include "prio.h"
+#include "structs.h"
+#include "../checkers/libsg.h"
+
+#include "delayedpath.h"
+
+#define THRES_USEC_VALUE        300000000LL    /*USEC, 300SEC*/
+#define DEFAULT_DELAY_INTERVAL  10             /*MSEC*/
+#define DEFAULT_CONS_NUM        20
+
+#define MAX_CHAR_SIZE           30
+
+#define CHAR_SEC                "s"
+#define CHAR_MSEC               "ms"
+#define CHAR_USEC               "us"
+
+enum interval_type {
+    INTERVAL_SEC,
+    INTERVAL_MSEC,
+    INTERVAL_USEC,
+    INTERVAL_INVALID
+};
+
+/* interval_unit_str and interval_unit_type keep the same assignment

sequence */

libmultipath/prioritizers/delayedpath.c line 55

Quoted 6 lines
libmultipath/prioritizers/delayedpath.c new file +6 -0
+static const char interval_unit_str[][MAX_CHAR_SIZE] = {
+    CHAR_USEC, CHAR_MSEC, CHAR_SEC
+};
+static const int interval_unit_type[] = {
+    INTERVAL_USEC, INTERVAL_MSEC, INTERVAL_SEC
+};

It's irritiating that you use a different ordering here than in the interval_type enum.

Quoted 96 lines
libmultipath/prioritizers/delayedpath.c new file +42 -0
+
+static const int conversion_ratio[] = {
+	[INTERVAL_SEC]		= USEC_PER_SEC,
+	[INTERVAL_MSEC]	        = USEC_PER_MSEC,
+	[INTERVAL_USEC]		= USEC_PER_USEC,
+	[INTERVAL_INVALID]	= 0
+};
+
+
+static int do_readsector0(int fd, unsigned int timeout)
+{
+	unsigned char buf[4096];
+	unsigned char sbuf[SENSE_BUFF_LEN];
+	int ret;
+
+	ret = sg_read(fd, &buf[0], 4096, &sbuf[0],
+		      SENSE_BUFF_LEN, timeout);
+
+	return ret;
+}
+
+static int get_interval_type(char *source, char *type)
+{
+    char *p;
+    int size;
+    int i;
+
+    for (i = 0; i < sizeof(interval_unit_str)/MAX_CHAR_SIZE; i++)
+    {
+        size = strlen(interval_unit_str[i]);
+        p = strstr(source, interval_unit_str[i]);
+        if (p != NULL && p[size] == '|')
+        {
+            memcpy(type, interval_unit_str[i], size+1);
+            return interval_unit_type[i];
+        }
+    }
+
+    return INTERVAL_INVALID;
+}
+
+/* In multipath.conf, args form: delay_interval|cons_num. For

example,

libmultipath/prioritizers/delayedpath.c new file +46 -0
+*  args is "10ms|20", this function can get 10, ms, and 20.
+*/
+static int get_digit_and_type(char *args,
+                              int *interval,
+                              int *consnum,
+                              int *type)
+{
+    char typestr[MAX_CHAR_SIZE];
+    char source[MAX_CHAR_SIZE];
+    char vertica[] = "|";
+    char *tokenbefore = NULL;
+    char *tokenafter = NULL;
+    char *tmp = NULL;
+    unsigned int size = strlen(args);
+
+    if ((args == NULL) || (interval == NULL)
+        || (consnum == NULL) || (type == NULL))
+        return 0;
+
+    /* int type */
+    if ((size < 1) || (size > MAX_CHAR_SIZE-1))
+        return 0;
+
+    memcpy(source, args, size+1);
+    if (strstr(source, vertica) == NULL)
+        return 0;
+
+    *type = get_interval_type(source, typestr);
+    if (*type == INTERVAL_INVALID)
+    {
+        condlog(0, "delay_interval type is invalid");
+        return 0;
+    }
+
+    tokenbefore = strtok(source, vertica);
+    tokenafter = strtok(NULL, vertica);
+    typestr[1] = '\0';
+    tokenbefore = strtok(tokenbefore, typestr);
+    if ((tokenbefore == NULL) || (tokenafter == NULL))
+        return 0;
+
+    tmp = tokenbefore;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+        {
+            condlog(0, "delay_interval string include invalid

char");
+            return 0;
+        }

libmultipath/prioritizers/delayedpath.c new file +4 -0
+
+    tmp = tokenafter;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))

libmultipath/prioritizers/delayedpath.c line 159

Quoted 10 lines
libmultipath/prioritizers/delayedpath.c new file +10 -0
+        {
+            condlog(0, "cons_num string include invalid char");
+            return 0;
+        }
+
+    *interval = atoi(tokenbefore);
+    *consnum = atoi(tokenafter);
+
+    return 1;
+}

see above.

Quoted 19 lines
libmultipath/prioritizers/delayedpath.c new file +19 -0
+
+int check_args_valid(int delay_interval, int cons_num, int type)
+{
+    if (type == INTERVAL_SEC)
+    {
+        if ((delay_interval < 1) || (delay_interval > 60))
+        {
+            condlog(0, "delay_interval values is invalid");
+            return 0;
+        }
+    }
+    else if (type != INTERVAL_INVALID)
+    {
+        if ((delay_interval < 1) || (delay_interval >= 1000))
+        {
+            condlog(0, "delay_interval values is invalid");
+            return 0;
+        }
+    }

libmultipath/prioritizers/delayedpath.c line 188

Quoted 10 lines
libmultipath/prioritizers/delayedpath.c new file +10 -0
+
+    if ((cons_num < 3) || (cons_num > 1000))
+    {
+        condlog(0, "cons_num values is invalid");
+        return 0;
+    }
+
+    return 1;
+}
+

see above.

Quoted 55 lines

+int get_delay_pref_arg(char *args, int *delay_interval, int *cons_num, int *interval_type)
+{
+    if (get_digit_and_type(args, delay_interval, cons_num, interval_type) == 0)
+        return 0;
+
+    if (check_args_valid(*delay_interval, *cons_num, *interval_type) == 0)
+        return 0;

libmultipath/prioritizers/delayedpath.c new file +23 -0
+
+    return 1;
+}
+
+long long get_conversion_ratio(int type)
+{
+    return conversion_ratio[type];
+}
+
+int getprio (struct path *pp, char *args, unsigned int timeout)
+{
+    int rc, delay_interval, cons_num, type, temp;
+    long long delay, avgdelay, ratio;
+    long long min = THRES_USEC_VALUE;
+    long long max = 0;
+    long long toldelay = 0;
+    long long before, after;
+    struct timespec tv;
+
+	if (pp->fd < 0)
+	    return -PRIO_NO_INFORMATION;
+
+    if (get_delay_pref_arg(args, &delay_interval, &cons_num, &type)

== 0)
+    {
+        condlog(3, "%s: get delay arg fail", pp->dev); +        delay_interval = DEFAULT_DELAY_INTERVAL; +        cons_num = DEFAULT_CONS_NUM;
+        type = INTERVAL_MSEC;
+    }

libmultipath/prioritizers/delayedpath.c new file +15 -0
+
+    temp = cons_num;
+    while (temp-- > 0)
+    {
+        (void)clock_gettime(CLOCK_MONOTONIC, &tv);
+        before = timeval_to_us(&tv);		
+
+        if (do_readsector0(pp->fd, timeout) == 2)
+        {
+            condlog(0, "%s: path down", pp->dev);
+            return -1;
+        }
+
+        (void)clock_gettime(CLOCK_MONOTONIC, &tv);
+        after = timeval_to_us(&tv);

libmultipath/prioritizers/delayedpath.c line 249

Quoted 10 lines
libmultipath/prioritizers/delayedpath.c new file +10 -0
+
+        delay = after - before;
+    	
+        min = (min <= delay) ? min : delay;
+        max = (max >= delay) ? max : delay;
+
+        toldelay += delay;
+    }
+
+    toldelay -= min + max;

Why are you doing this? If you want to discard "extreme" values, this is probably not sufficient. If cons == 3, this will have the effect to use a single measurement rather than an average, is that intended?

Btw, as you are doing statistics here anyway, you may want to calculate the estimate of the standard deviation and warn the user if the delay_interval is smaller than, say, 2 * standard deviation.

Please consider printing a message with the measured value at debug level 3 or higher.

Quoted 54 lines

+    avgdelay = toldelay/(long long)(cons_num - 2);
+    if (avgdelay > THRES_USEC_VALUE)
+    {
+        condlog(0, "%s: avgdelay is more than thresold", pp->dev); +        return 1;
+    }

libmultipath/prioritizers/delayedpath.c new file +3 -0
+
+	ratio = get_conversion_ratio(type);
+	rc = (int)(THRES_USEC_VALUE - (avgdelay/(((long

long)delay_interval) * ratio)));

libmultipath/prioritizers/delayedpath.c new file +3 -0
+
+    return rc;
+}
libmultipath/prioritizers/delayedpath.h new file +14 -0
new file mode 100644
index 0000000..d8213e9
@@ -0,0 +1,17 @@
+#ifndef _DELAYEDPATH_H
+#define _DELAYEDPATH_H
+
+#define PRIO_DELAYED_PATH "delayedpath"
+
+#define PRIO_NO_INFORMATION 5
+
+#define USEC_PER_SEC      1000000LL
+#define USEC_PER_MSEC     1000LL
+#define USEC_PER_USEC     1LL
+
+static inline long long timeval_to_us(const struct timespec *tv)
+{
+	return ((long long) tv->tv_sec * USEC_PER_SEC) + (tv-
Quoted 1 line

tv_nsec >> 10);

libmultipath/prioritizers/delayedpath.h new file +3 -0
+}
+
+#endif
multipath/multipath.conf.5 +0 -0
index 5939688..f1e126e 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.

 Generate the path priority based on the regular expression and the priority provided as argument. Requires prio_args keyword. .TP
+.I delayedpath

multipath/multipath.conf.5 +3 -0
+Generate the path priority based on a time-delay algorithm.
+Requires prio_args keyword.
+.TP

 .I datacore
 .\" XXX
 ???. Requires prio_args keyword.

multipath/multipath.conf.5 +0 -0
@@ -333,6 +337,21 @@ these values can be looked up through sysfs or
Quoted 5 lines

by running \fImultipathd show pat
 "%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.* .RE
 .TP 12
+.I delayed

should be "delayedpath" here?

Quoted 8 lines
multipath/multipath.conf.5 +6 -0
+Needs a value of the form
+\fI"<delay_interval|cons_num>"\fR
+.RS
+.TP 8
+.I delay_interval
+The interval values of average IO-time-delay between two different

neighbour ranks of path priority, used to partition different priority ranks.

It might be good to give an example here, like this:

"If delay_interval=10ms, the paths will be grouped in priority groups with path latency 0-10ms, 10-20ms, 20-30ms, etc."

Quoted 16 lines

+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 1000), us [1, 1000),

multipath/multipath.conf.5 +4 -0
+For example: 10s, or 100us, or 100ms. The default is: 10ms.
+.TP
+.I cons_num
+The number of read IOs sent to the current path continuously, used

to calculate the average IO-time-delay. Valid Values: Integer, [3, 1000].
+For example: 30. The default is: 20.
+.RE
+.TP 12
 .I alua
 If \fIexclusive_pref_bit\fR is set, paths with the \fIpreferred path\fR bit
 set will always be in their own path group.

Signature · 8 lines
Dr. Martin Wilck <[email protected]>, Tel. +49 (0)911 74053 2107
SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
author
#7 lore raw

Hello Martin,

Firstly, thank you very much for your comments.
And find my replys and the up-to-date patch.

Best regards!

Quoted 2 lines

Please think about the name once again. Maybe you should call it "io_latency" or "path_latency" instead of "delayedpath"?

OK, as the following patch.

Quoted 6 lines

Hm, I can't see a lot of difference in the parsing code wrt the previous version. IMO it's still non-straightforward and hard to comprehend. Maybe I didn't express myself clearly enough. Here is how I'd code this:

Quoted 8 lines

1. Verify that the string starts with a digit. Error if it does not.
2. Parse the delay interval using strtoul().
3. The "end" pointer of strtoul() points to the unit, which has to be
"s", "ms" or "us". Verify, and set the unit accordingly.
4. Verify that the next character is '|', and that it's followed by a
digit.
5. Parse the number with strtoul()
6. Verify that there's no garbage at the end of the string.

Thank you , as the following patch.

Quoted 5 lines

Please follow the https://en.wikipedia.org/wiki/Robustness_principle.
If a user enters "1500ms" here, the parsing will silently fail, and with it the whole prio algorithm. This will cause user confusion. Please don't do this

Thank you , as the following patch.

Quoted 6 lines

However please consider lowering the upper bound, I kind of doubt that 1000 IOs will finish quickly. More often than not, a lot of paths will appear at the same time (e.g. if a port of a storage array is enabled) and we'll have to send 1000 IOs to each one.

OK, the upper bound lower to 200, as the following patch.

Quoted 28 lines
Quoted 13 lines
(quoted diff) +13 -0
+    while (temp-- > 0)
+    {
+        (void)clock_gettime(CLOCK_MONOTONIC, &tv);
+        before = timeval_to_us(&tv);		
+
+        if (do_readsector0(pp->fd, timeout) == 2)
+        {
+            condlog(0, "%s: path down", pp->dev);
+            return -1;
+        }
+
+        (void)clock_gettime(CLOCK_MONOTONIC, &tv);
+        after = timeval_to_us(&tv);

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+        delay = after - before;
+    	
+        min = (min <= delay) ? min : delay;
+        max = (max >= delay) ? max : delay;
+
+        toldelay += delay;
+    }
+
+    toldelay -= min + max;

Why are you doing this? If you want to discard "extreme" values, this is probably not sufficient. If cons == 3, this will have the effect to use a single measurement rather than an average, is that intended?

Quoted 6 lines

Btw, as you are doing statistics here anyway, you may want to calculate the estimate of the standard deviation and warn the user if the delay_interval is smaller than, say, 2 * standard deviation.

Please consider printing a message with the measured value at debug level 3 or higher.

OK, as the following patch.

Quoted 6 lines
Quoted 4 lines

"%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.* .RE
.TP 12
+.I delayed

should be "delayedpath" here?

OK, as the following patch.

Quoted 12 lines
Quoted 9 lines
(quoted diff) +6 -0
+Needs a value of the form
+\fI"<delay_interval|cons_num>"\fR
+.RS
+.TP 8
+.I delay_interval
+The interval values of average IO-time-delay between two different

neighbour ranks of path priority, used to partition different priority ranks.

It might be good to give an example here, like this:

Quoted 2 lines

"If delay_interval=10ms, the paths will be grouped in priority groups with path latency 0-10ms, 10-20ms, 20-30ms, etc."

OK, as the following patch.>

---
From 58d718fdd34550bd9c4a32c6e9a87099c1e45a9f Mon Sep 17 00:00:00 2001
From: Yang Feng <[email protected]>
Date: Fri, 19 May 2017 16:09:07 +0800
Subject: [PATCH] libmultipath/prioritizers: Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a latency algorithm. And the latency algorithm is dependent on the following arguments(latency_interval and io_num). The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path continuously, the IOs' average latency can be calculated.
2. According to the average latency of each path and the weight value "latency_interval", the priority "rc" of each path can be provided.

latency_interval latency_interval latency_interval latency_interval
|------------------|------------------|------------------|...|------------------| | priority rank 1 | priority rank 2 | priority rank 3 |...| priority rank x | |------------------|------------------|------------------|...|------------------|
Priority Rank Partitioning

---
 libmultipath/prioritizers/Makefile       |   6 +-
 libmultipath/prioritizers/path_latency.c | 271 +++++++++++++++++++++++++++++++
 multipath/multipath.conf.5               |  18 ++
 3 files changed, 294 insertions(+), 1 deletion(-)
 create mode 100644 libmultipath/prioritizers/path_latency.c
libmultipath/prioritizers/Makefile +5 -1
index 36b42e4..d2f20f6 100644
@@ -18,13 +18,17 @@ LIBS = \
 	libpriorandom.so \
 	libpriordac.so \
 	libprioweightedpath.so \
-	libpriosysfs.so
+	libpriopath_latency.so \
+	libpriosysfs.so
 
 all: $(LIBS)
 
 libprioalua.so: alua.o alua_rtpg.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
+libpriopath_latency.so: path_latency.o  ../checkers/libsg.o
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^ -lm
+
 libprio%.so: %.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
libmultipath/prioritizers/path_latency.c new file +271 -0

271 lines not on this page · raw message

multipath/multipath.conf.5 +18 -0
index 5939688..3dd0d77 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I path_latency
+Generate the path priority based on a latency algorithm.
+Requires prio_args keyword.
+.TP
 .I datacore
 .\" XXX
 ???. Requires prio_args keyword.
@@ -333,6 +337,20 @@ these values can be looked up through sysfs or by running \fImultipathd show pat
 "%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.*
 .RE
 .TP 12
+.I path_latency
+Needs a value of the form
+\fI"<latency_interval>|<io_num>"\fR
+.RS
+.TP 8
+.I latency_interval
+The interval values of average latency between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 60000], us [1, 60000000],
+For example: If latency_interval=10ms, the paths will be grouped in priority groups with path latency 0-10ms, 10-20ms, 20-30ms, etc..
+.TP
+.I io_num
+The number of read IOs sent to the current path continuously, used to calculate the average path latency. Valid Values: Integer, [10, 200].
+.RE
+.TP 12
 .I alua
 If \fIexclusive_pref_bit\fR is set, paths with the \fIpreferred path\fR bit
 set will always be in their own path group.
author
#8 lore raw

Hello Martin and Christophe,

How about this patch?
Thanks a lot.
Best.

Quoted 461 lines · Yang Feng

Hello Martin,

Firstly, thank you very much for your comments.
And find my replys and the up-to-date patch.

Best regards!

Quoted 2 lines

Please think about the name once again. Maybe you should call it "io_latency" or "path_latency" instead of "delayedpath"?

OK, as the following patch.

Quoted 6 lines

Hm, I can't see a lot of difference in the parsing code wrt the previous version. IMO it's still non-straightforward and hard to comprehend. Maybe I didn't express myself clearly enough. Here is how I'd code this:

Quoted 8 lines

1. Verify that the string starts with a digit. Error if it does not.
2. Parse the delay interval using strtoul().
3. The "end" pointer of strtoul() points to the unit, which has to be
"s", "ms" or "us". Verify, and set the unit accordingly.
4. Verify that the next character is '|', and that it's followed by a
digit.
5. Parse the number with strtoul()
6. Verify that there's no garbage at the end of the string.

Thank you , as the following patch.

Quoted 5 lines

Please follow the https://en.wikipedia.org/wiki/Robustness_principle.
If a user enters "1500ms" here, the parsing will silently fail, and with it the whole prio algorithm. This will cause user confusion. Please don't do this

Thank you , as the following patch.

Quoted 6 lines

However please consider lowering the upper bound, I kind of doubt that 1000 IOs will finish quickly. More often than not, a lot of paths will appear at the same time (e.g. if a port of a storage array is enabled) and we'll have to send 1000 IOs to each one.

OK, the upper bound lower to 200, as the following patch.

Quoted 28 lines
Quoted 13 lines
libmultipath/prioritizers/path_latency.c new file +13 -0
+    while (temp-- > 0)
+    {
+        (void)clock_gettime(CLOCK_MONOTONIC, &tv);
+        before = timeval_to_us(&tv);		
+
+        if (do_readsector0(pp->fd, timeout) == 2)
+        {
+            condlog(0, "%s: path down", pp->dev);
+            return -1;
+        }
+
+        (void)clock_gettime(CLOCK_MONOTONIC, &tv);
+        after = timeval_to_us(&tv);

libmultipath/prioritizers/path_latency.c

Quoted 10 lines
libmultipath/prioritizers/path_latency.c new file +10 -0
+
+        delay = after - before;
+    	
+        min = (min <= delay) ? min : delay;
+        max = (max >= delay) ? max : delay;
+
+        toldelay += delay;
+    }
+
+    toldelay -= min + max;

Why are you doing this? If you want to discard "extreme" values, this is probably not sufficient. If cons == 3, this will have the effect to use a single measurement rather than an average, is that intended?

Quoted 6 lines

Btw, as you are doing statistics here anyway, you may want to calculate the estimate of the standard deviation and warn the user if the delay_interval is smaller than, say, 2 * standard deviation.

Please consider printing a message with the measured value at debug level 3 or higher.

OK, as the following patch.

Quoted 6 lines
Quoted 4 lines

"%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.* .RE
.TP 12
+.I delayed

should be "delayedpath" here?

OK, as the following patch.

Quoted 12 lines
Quoted 9 lines
multipath/multipath.conf.5 +6 -0
+Needs a value of the form
+\fI"<delay_interval|cons_num>"\fR
+.RS
+.TP 8
+.I delay_interval
+The interval values of average IO-time-delay between two different

neighbour ranks of path priority, used to partition different priority ranks.

It might be good to give an example here, like this:

Quoted 2 lines

"If delay_interval=10ms, the paths will be grouped in priority groups with path latency 0-10ms, 10-20ms, 20-30ms, etc."

OK, as the following patch.>

---

Quoted 1 line

From 58d718fdd34550bd9c4a32c6e9a87099c1e45a9f Mon Sep 17 00:00:00 2001

From: Yang Feng <[email protected]>
Date: Fri, 19 May 2017 16:09:07 +0800
Subject: [PATCH] libmultipath/prioritizers: Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a latency algorithm. And the latency algorithm is dependent on the following arguments(latency_interval and io_num). The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path continuously, the IOs' average latency can be calculated.
2. According to the average latency of each path and the weight value "latency_interval", the priority "rc" of each path can be provided.

latency_interval latency_interval latency_interval latency_interval
|------------------|------------------|------------------|...|------------------| | priority rank 1 | priority rank 2 | priority rank 3 |...| priority rank x | |------------------|------------------|------------------|...|------------------|
Priority Rank Partitioning

---
 libmultipath/prioritizers/Makefile       |   6 +-
 libmultipath/prioritizers/path_latency.c | 271 +++++++++++++++++++++++++++++++
 multipath/multipath.conf.5               |  18 ++
 3 files changed, 294 insertions(+), 1 deletion(-)
 create mode 100644 libmultipath/prioritizers/path_latency.c
libmultipath/prioritizers/Makefile +5 -1
index 36b42e4..d2f20f6 100644
@@ -18,13 +18,17 @@ LIBS = \
 	libpriorandom.so \
 	libpriordac.so \
 	libprioweightedpath.so \
-	libpriosysfs.so
+	libpriopath_latency.so \
+	libpriosysfs.so
 
 all: $(LIBS)
 
 libprioalua.so: alua.o alua_rtpg.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
+libpriopath_latency.so: path_latency.o  ../checkers/libsg.o
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^ -lm
+
 libprio%.so: %.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
libmultipath/prioritizers/path_latency.c new file +271 -0
new file mode 100644
index 0000000..a666b6c
@@ -0,0 +1,271 @@
+/*
+ * (C) Copyright HUAWEI Technology Corp. 2017, 2021   All Rights Reserved.
+ *
+ * main.c
+ *
+ * Prioritizer for device mapper multipath, where the corresponding priority
+ * values of specific paths are provided by a latency algorithm. And the
+ * latency algorithm is dependent on arguments.
+ *
+ * The principle of the algorithm as follows:
+ * 1. By sending a certain number "io_num" of read IOs to the current path
+ *    continuously, the IOs' average latency can be calculated.
+ * 2. According to the average latency of each path and the weight value
+ *    "latency_interval", the priority "rc" of each path can be provided.
+ *
+ * Author(s): Yang Feng <[email protected]>
+ *            Zou Ming <[email protected]>
+ *
+ * This file is released under the GPL.
+ */
+#include <stdio.h>
+#include <math.h>
+#include <ctype.h>
+#include <time.h>
+
+#include "debug.h"
+#include "prio.h"
+#include "structs.h"
+#include "../checkers/libsg.h"
+
+#define THRES_USEC_VALUE        120000000LL    /*unit: us, =120s*/
+
+#define MAX_IO_NUM              200
+#define MIN_IO_NUM              10
+
+#define MAX_LATENCY_INTERVAL    60            /*unit: s*/
+#define MIN_LATENCY_INTERVAL    1             /*unit: us, or ms, or s*/
+
+#define DEFAULT_PRIORITY        0
+
+#define MAX_CHAR_SIZE           30
+
+#define CHAR_USEC               "us"
+#define CHAR_MSEC               "ms"
+#define CHAR_SEC                "s"
+
+enum interval_type {
+    INTERVAL_USEC,
+    INTERVAL_MSEC,
+    INTERVAL_SEC,
+    INTERVAL_INVALID
+};
+
+/* interval_unit_str and interval_unit_type keep the same assignment sequence */
+static const char *interval_unit_str[MAX_CHAR_SIZE] = {
+    CHAR_USEC, CHAR_MSEC, CHAR_SEC
+};
+static const int interval_unit_type[] = {
+    INTERVAL_USEC, INTERVAL_MSEC, INTERVAL_SEC
+};
+
+#define USEC_PER_SEC      1000000LL
+#define USEC_PER_MSEC     1000LL
+#define USEC_PER_USEC     1LL
+
+static const int conversion_ratio[] = {
+    [INTERVAL_USEC]		= USEC_PER_USEC,
+    [INTERVAL_MSEC]     = USEC_PER_MSEC,
+    [INTERVAL_SEC]		= USEC_PER_SEC,
+    [INTERVAL_INVALID]	= 0
+};
+
+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 >> 10);
+}
+
+static int do_readsector0(int fd, unsigned int timeout)
+{
+	unsigned char buf[4096];
+	unsigned char sbuf[SENSE_BUFF_LEN];
+	int ret;
+
+	ret = sg_read(fd, &buf[0], 4096, &sbuf[0],
+		      SENSE_BUFF_LEN, timeout);
+
+	return ret;
+}
+
+int check_args_valid(int io_num, long long latency_interval, int type)
+{
+    if ((io_num < MIN_IO_NUM) || (io_num > MAX_IO_NUM))
+    {
+        condlog(0, "args io_num is more than the valid values range");
+        return 0;
+    }
+
+    /* s:[1, 60], ms:[1, 60000], us:[1, 60000000] */
+    if ((latency_interval < MIN_LATENCY_INTERVAL) || (latency_interval > (MAX_LATENCY_INTERVAL * USEC_PER_SEC / conversion_ratio[type])))
+    {
+        condlog(0, "args latency_interval is more than the valid values range");
+        return 0;
+    }
+
+    return 1;
+}
+
+static int get_interval_type(char *type)
+{
+    int index;
+
+    for (index = 0; index < sizeof(interval_unit_str)/MAX_CHAR_SIZE; index++)
+    {
+        if (strcmp(type, interval_unit_str[index]) == 0)
+        {
+            return interval_unit_type[index];
+        }
+    }
+
+    return INTERVAL_INVALID;
+}
+
+long long get_conversion_ratio(int type)
+{
+    return conversion_ratio[type];
+}
+
+/* In multipath.conf, args form: io_num|latency_interval. For example,
+*  args is "20|10ms", this function can get 20, 10.
+*/
+static int get_interval_and_ionum(char *args,
+                                        int *ionum,
+                                        long long *interval)
+{
+    char source[MAX_CHAR_SIZE];
+    char vertica = '|';
+    char *endstrbefore = NULL;
+    char *endstrafter = NULL;
+    int type;
+    unsigned int size = strlen(args);
+    long long ratio;
+
+    if ((args == NULL) || (ionum == NULL) || (interval == NULL))
+    {
+        condlog(0, "args string is NULL");
+        return 0;
+    }
+
+    if ((size < 1) || (size > MAX_CHAR_SIZE-1))
+    {
+        condlog(0, "args string's size is too long");
+        return 0;
+    }
+
+    memcpy(source, args, size+1);
+
+    if (!isdigit(source[0]))
+    {
+        condlog(0, "args io_num string's first char is not digit");
+        return 0;
+    }
+
+    *ionum = (int)strtoul(source, &endstrbefore, 10);
+    if (endstrbefore[0] != vertica)
+    {
+        condlog(0, "segmentation char is invalid");
+        return 0;
+    }
+
+    if (!isdigit(endstrbefore[1]))
+    {
+        condlog(0, "args latency_interval string's first char is not digit");
+        return 0;
+    }
+
+    *interval = (long long)strtol(&endstrbefore[1], &endstrafter, 10);
+    type = get_interval_type(endstrafter);
+    if (type == INTERVAL_INVALID)
+    {
+        condlog(0, "args latency_interval type is invalid");
+        return 0;
+    }
+
+    if (check_args_valid(*ionum, *interval, type) == 0)
+    {
+        return 0;
+    }
+
+	ratio = get_conversion_ratio(type);
+    *interval *= (long long)ratio;
+
+    return 1;
+}
+
+long long calc_standard_deviation(long long *path_latency, int size, long long avglatency)
+{
+    int index;
+    long long total = 0;
+
+    for (index = 0; index < size; index++)
+    {
+        total += (path_latency[index] - avglatency) * (path_latency[index] - avglatency);
+    }
+
+    total /= (size-1);
+
+    return (long long)sqrt((double)total);
+}
+
+int getprio (struct path *pp, char *args, unsigned int timeout)
+{
+    int rc, temp;
+    int index = 0;
+    int io_num;
+    long long latency_interval;
+    long long avglatency;
+    long long standard_deviation;
+    long long toldelay = 0;
+    long long before, after;
+    struct timespec tv;
+
+	if (pp->fd < 0)
+		return -1;
+
+    if (get_interval_and_ionum(args, &io_num, &latency_interval) == 0)
+    {
+        condlog(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)
+        {
+            condlog(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;
+    condlog(4, "%s: average latency is (%lld)", pp->dev, avglatency);
+
+    if (avglatency > THRES_USEC_VALUE)
+    {
+        condlog(0, "%s: average latency (%lld) is more than thresold", pp->dev, avglatency);
+        return DEFAULT_PRIORITY;
+    }
+
+    /* warn the user if the latency_interval set is smaller than (2 * standard deviation), or equal */
+    standard_deviation = calc_standard_deviation(path_latency, index, avglatency);
+    if (latency_interval <= (2 * standard_deviation))
+        condlog(3, "%s: args latency_interval set is smaller than 2 * standard deviation (%lld us), or equal",
+            pp->dev, standard_deviation);
+
+	rc = (int)(THRES_USEC_VALUE - (avglatency/(long long)latency_interval));
+    return rc;
+}
multipath/multipath.conf.5 +18 -0
index 5939688..3dd0d77 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I path_latency
+Generate the path priority based on a latency algorithm.
+Requires prio_args keyword.
+.TP
 .I datacore
 .\" XXX
 ???. Requires prio_args keyword.
@@ -333,6 +337,20 @@ these values can be looked up through sysfs or by running \fImultipathd show pat
 "%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.*
 .RE
 .TP 12
+.I path_latency
+Needs a value of the form
+\fI"<latency_interval>|<io_num>"\fR
+.RS
+.TP 8
+.I latency_interval
+The interval values of average latency between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 60000], us [1, 60000000],
+For example: If latency_interval=10ms, the paths will be grouped in priority groups with path latency 0-10ms, 10-20ms, 20-30ms, etc..
+.TP
+.I io_num
+The number of read IOs sent to the current path continuously, used to calculate the average path latency. Valid Values: Integer, [10, 200].
+.RE
+.TP 12
 .I alua
 If \fIexclusive_pref_bit\fR is set, paths with the \fIpreferred path\fR bit
 set will always be in their own path group.
author
#9 lore raw

Re: [PATCH] multipath-tools:Prioritizer based on a latency algorithm

In reply to Yang Feng

Hello Martin,

How about this patch?
Look forward your replies.
Thank you very much.
Best regards!

Quoted 461 lines · Yang Feng

Hello Martin,

Firstly, thank you very much for your comments.
And find my replys and the up-to-date patch.

Best regards!

Quoted 2 lines

Please think about the name once again. Maybe you should call it "io_latency" or "path_latency" instead of "delayedpath"?

OK, as the following patch.

Quoted 6 lines

Hm, I can't see a lot of difference in the parsing code wrt the previous version. IMO it's still non-straightforward and hard to comprehend. Maybe I didn't express myself clearly enough. Here is how I'd code this:

Quoted 8 lines

1. Verify that the string starts with a digit. Error if it does not.
2. Parse the delay interval using strtoul().
3. The "end" pointer of strtoul() points to the unit, which has to be
"s", "ms" or "us". Verify, and set the unit accordingly.
4. Verify that the next character is '|', and that it's followed by a
digit.
5. Parse the number with strtoul()
6. Verify that there's no garbage at the end of the string.

Thank you , as the following patch.

Quoted 5 lines

Please follow the https://en.wikipedia.org/wiki/Robustness_principle.
If a user enters "1500ms" here, the parsing will silently fail, and with it the whole prio algorithm. This will cause user confusion. Please don't do this

Thank you , as the following patch.

Quoted 6 lines

However please consider lowering the upper bound, I kind of doubt that 1000 IOs will finish quickly. More often than not, a lot of paths will appear at the same time (e.g. if a port of a storage array is enabled) and we'll have to send 1000 IOs to each one.

OK, the upper bound lower to 200, as the following patch.

Quoted 28 lines
Quoted 13 lines
libmultipath/prioritizers/path_latency.c new file +13 -0
+    while (temp-- > 0)
+    {
+        (void)clock_gettime(CLOCK_MONOTONIC, &tv);
+        before = timeval_to_us(&tv);		
+
+        if (do_readsector0(pp->fd, timeout) == 2)
+        {
+            condlog(0, "%s: path down", pp->dev);
+            return -1;
+        }
+
+        (void)clock_gettime(CLOCK_MONOTONIC, &tv);
+        after = timeval_to_us(&tv);

libmultipath/prioritizers/path_latency.c

Quoted 10 lines
libmultipath/prioritizers/path_latency.c new file +10 -0
+
+        delay = after - before;
+    	
+        min = (min <= delay) ? min : delay;
+        max = (max >= delay) ? max : delay;
+
+        toldelay += delay;
+    }
+
+    toldelay -= min + max;

Why are you doing this? If you want to discard "extreme" values, this is probably not sufficient. If cons == 3, this will have the effect to use a single measurement rather than an average, is that intended?

Quoted 6 lines

Btw, as you are doing statistics here anyway, you may want to calculate the estimate of the standard deviation and warn the user if the delay_interval is smaller than, say, 2 * standard deviation.

Please consider printing a message with the measured value at debug level 3 or higher.

OK, as the following patch.

Quoted 6 lines
Quoted 4 lines

"%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.* .RE
.TP 12
+.I delayed

should be "delayedpath" here?

OK, as the following patch.

Quoted 12 lines
Quoted 9 lines
multipath/multipath.conf.5 +6 -0
+Needs a value of the form
+\fI"<delay_interval|cons_num>"\fR
+.RS
+.TP 8
+.I delay_interval
+The interval values of average IO-time-delay between two different

neighbour ranks of path priority, used to partition different priority ranks.

It might be good to give an example here, like this:

Quoted 2 lines

"If delay_interval=10ms, the paths will be grouped in priority groups with path latency 0-10ms, 10-20ms, 20-30ms, etc."

OK, as the following patch.>

---

Quoted 1 line

From 58d718fdd34550bd9c4a32c6e9a87099c1e45a9f Mon Sep 17 00:00:00 2001

From: Yang Feng <[email protected]>
Date: Fri, 19 May 2017 16:09:07 +0800
Subject: [PATCH] libmultipath/prioritizers: Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a latency algorithm. And the latency algorithm is dependent on the following arguments(latency_interval and io_num). The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path continuously, the IOs' average latency can be calculated.
2. According to the average latency of each path and the weight value "latency_interval", the priority "rc" of each path can be provided.

latency_interval latency_interval latency_interval latency_interval
|------------------|------------------|------------------|...|------------------| | priority rank 1 | priority rank 2 | priority rank 3 |...| priority rank x | |------------------|------------------|------------------|...|------------------|
Priority Rank Partitioning

---
 libmultipath/prioritizers/Makefile       |   6 +-
 libmultipath/prioritizers/path_latency.c | 271 +++++++++++++++++++++++++++++++
 multipath/multipath.conf.5               |  18 ++
 3 files changed, 294 insertions(+), 1 deletion(-)
 create mode 100644 libmultipath/prioritizers/path_latency.c
libmultipath/prioritizers/Makefile +5 -1
index 36b42e4..d2f20f6 100644
@@ -18,13 +18,17 @@ LIBS = \
 	libpriorandom.so \
 	libpriordac.so \
 	libprioweightedpath.so \
-	libpriosysfs.so
+	libpriopath_latency.so \
+	libpriosysfs.so
 
 all: $(LIBS)
 
 libprioalua.so: alua.o alua_rtpg.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
+libpriopath_latency.so: path_latency.o  ../checkers/libsg.o
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^ -lm
+
 libprio%.so: %.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
libmultipath/prioritizers/path_latency.c new file +271 -0
new file mode 100644
index 0000000..a666b6c
@@ -0,0 +1,271 @@
+/*
+ * (C) Copyright HUAWEI Technology Corp. 2017, 2021   All Rights Reserved.
+ *
+ * main.c
+ *
+ * Prioritizer for device mapper multipath, where the corresponding priority
+ * values of specific paths are provided by a latency algorithm. And the
+ * latency algorithm is dependent on arguments.
+ *
+ * The principle of the algorithm as follows:
+ * 1. By sending a certain number "io_num" of read IOs to the current path
+ *    continuously, the IOs' average latency can be calculated.
+ * 2. According to the average latency of each path and the weight value
+ *    "latency_interval", the priority "rc" of each path can be provided.
+ *
+ * Author(s): Yang Feng <[email protected]>
+ *            Zou Ming <[email protected]>
+ *
+ * This file is released under the GPL.
+ */
+#include <stdio.h>
+#include <math.h>
+#include <ctype.h>
+#include <time.h>
+
+#include "debug.h"
+#include "prio.h"
+#include "structs.h"
+#include "../checkers/libsg.h"
+
+#define THRES_USEC_VALUE        120000000LL    /*unit: us, =120s*/
+
+#define MAX_IO_NUM              200
+#define MIN_IO_NUM              10
+
+#define MAX_LATENCY_INTERVAL    60            /*unit: s*/
+#define MIN_LATENCY_INTERVAL    1             /*unit: us, or ms, or s*/
+
+#define DEFAULT_PRIORITY        0
+
+#define MAX_CHAR_SIZE           30
+
+#define CHAR_USEC               "us"
+#define CHAR_MSEC               "ms"
+#define CHAR_SEC                "s"
+
+enum interval_type {
+    INTERVAL_USEC,
+    INTERVAL_MSEC,
+    INTERVAL_SEC,
+    INTERVAL_INVALID
+};
+
+/* interval_unit_str and interval_unit_type keep the same assignment sequence */
+static const char *interval_unit_str[MAX_CHAR_SIZE] = {
+    CHAR_USEC, CHAR_MSEC, CHAR_SEC
+};
+static const int interval_unit_type[] = {
+    INTERVAL_USEC, INTERVAL_MSEC, INTERVAL_SEC
+};
+
+#define USEC_PER_SEC      1000000LL
+#define USEC_PER_MSEC     1000LL
+#define USEC_PER_USEC     1LL
+
+static const int conversion_ratio[] = {
+    [INTERVAL_USEC]		= USEC_PER_USEC,
+    [INTERVAL_MSEC]     = USEC_PER_MSEC,
+    [INTERVAL_SEC]		= USEC_PER_SEC,
+    [INTERVAL_INVALID]	= 0
+};
+
+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 >> 10);
+}
+
+static int do_readsector0(int fd, unsigned int timeout)
+{
+	unsigned char buf[4096];
+	unsigned char sbuf[SENSE_BUFF_LEN];
+	int ret;
+
+	ret = sg_read(fd, &buf[0], 4096, &sbuf[0],
+		      SENSE_BUFF_LEN, timeout);
+
+	return ret;
+}
+
+int check_args_valid(int io_num, long long latency_interval, int type)
+{
+    if ((io_num < MIN_IO_NUM) || (io_num > MAX_IO_NUM))
+    {
+        condlog(0, "args io_num is more than the valid values range");
+        return 0;
+    }
+
+    /* s:[1, 60], ms:[1, 60000], us:[1, 60000000] */
+    if ((latency_interval < MIN_LATENCY_INTERVAL) || (latency_interval > (MAX_LATENCY_INTERVAL * USEC_PER_SEC / conversion_ratio[type])))
+    {
+        condlog(0, "args latency_interval is more than the valid values range");
+        return 0;
+    }
+
+    return 1;
+}
+
+static int get_interval_type(char *type)
+{
+    int index;
+
+    for (index = 0; index < sizeof(interval_unit_str)/MAX_CHAR_SIZE; index++)
+    {
+        if (strcmp(type, interval_unit_str[index]) == 0)
+        {
+            return interval_unit_type[index];
+        }
+    }
+
+    return INTERVAL_INVALID;
+}
+
+long long get_conversion_ratio(int type)
+{
+    return conversion_ratio[type];
+}
+
+/* In multipath.conf, args form: io_num|latency_interval. For example,
+*  args is "20|10ms", this function can get 20, 10.
+*/
+static int get_interval_and_ionum(char *args,
+                                        int *ionum,
+                                        long long *interval)
+{
+    char source[MAX_CHAR_SIZE];
+    char vertica = '|';
+    char *endstrbefore = NULL;
+    char *endstrafter = NULL;
+    int type;
+    unsigned int size = strlen(args);
+    long long ratio;
+
+    if ((args == NULL) || (ionum == NULL) || (interval == NULL))
+    {
+        condlog(0, "args string is NULL");
+        return 0;
+    }
+
+    if ((size < 1) || (size > MAX_CHAR_SIZE-1))
+    {
+        condlog(0, "args string's size is too long");
+        return 0;
+    }
+
+    memcpy(source, args, size+1);
+
+    if (!isdigit(source[0]))
+    {
+        condlog(0, "args io_num string's first char is not digit");
+        return 0;
+    }
+
+    *ionum = (int)strtoul(source, &endstrbefore, 10);
+    if (endstrbefore[0] != vertica)
+    {
+        condlog(0, "segmentation char is invalid");
+        return 0;
+    }
+
+    if (!isdigit(endstrbefore[1]))
+    {
+        condlog(0, "args latency_interval string's first char is not digit");
+        return 0;
+    }
+
+    *interval = (long long)strtol(&endstrbefore[1], &endstrafter, 10);
+    type = get_interval_type(endstrafter);
+    if (type == INTERVAL_INVALID)
+    {
+        condlog(0, "args latency_interval type is invalid");
+        return 0;
+    }
+
+    if (check_args_valid(*ionum, *interval, type) == 0)
+    {
+        return 0;
+    }
+
+	ratio = get_conversion_ratio(type);
+    *interval *= (long long)ratio;
+
+    return 1;
+}
+
+long long calc_standard_deviation(long long *path_latency, int size, long long avglatency)
+{
+    int index;
+    long long total = 0;
+
+    for (index = 0; index < size; index++)
+    {
+        total += (path_latency[index] - avglatency) * (path_latency[index] - avglatency);
+    }
+
+    total /= (size-1);
+
+    return (long long)sqrt((double)total);
+}
+
+int getprio (struct path *pp, char *args, unsigned int timeout)
+{
+    int rc, temp;
+    int index = 0;
+    int io_num;
+    long long latency_interval;
+    long long avglatency;
+    long long standard_deviation;
+    long long toldelay = 0;
+    long long before, after;
+    struct timespec tv;
+
+	if (pp->fd < 0)
+		return -1;
+
+    if (get_interval_and_ionum(args, &io_num, &latency_interval) == 0)
+    {
+        condlog(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)
+        {
+            condlog(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;
+    condlog(4, "%s: average latency is (%lld)", pp->dev, avglatency);
+
+    if (avglatency > THRES_USEC_VALUE)
+    {
+        condlog(0, "%s: average latency (%lld) is more than thresold", pp->dev, avglatency);
+        return DEFAULT_PRIORITY;
+    }
+
+    /* warn the user if the latency_interval set is smaller than (2 * standard deviation), or equal */
+    standard_deviation = calc_standard_deviation(path_latency, index, avglatency);
+    if (latency_interval <= (2 * standard_deviation))
+        condlog(3, "%s: args latency_interval set is smaller than 2 * standard deviation (%lld us), or equal",
+            pp->dev, standard_deviation);
+
+	rc = (int)(THRES_USEC_VALUE - (avglatency/(long long)latency_interval));
+    return rc;
+}
multipath/multipath.conf.5 +18 -0
index 5939688..3dd0d77 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I path_latency
+Generate the path priority based on a latency algorithm.
+Requires prio_args keyword.
+.TP
 .I datacore
 .\" XXX
 ???. Requires prio_args keyword.
@@ -333,6 +337,20 @@ these values can be looked up through sysfs or by running \fImultipathd show pat
 "%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.*
 .RE
 .TP 12
+.I path_latency
+Needs a value of the form
+\fI"<latency_interval>|<io_num>"\fR
+.RS
+.TP 8
+.I latency_interval
+The interval values of average latency between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 60000], us [1, 60000000],
+For example: If latency_interval=10ms, the paths will be grouped in priority groups with path latency 0-10ms, 10-20ms, 20-30ms, etc..
+.TP
+.I io_num
+The number of read IOs sent to the current path continuously, used to calculate the average path latency. Valid Values: Integer, [10, 200].
+.RE
+.TP 12
 .I alua
 If \fIexclusive_pref_bit\fR is set, paths with the \fIpreferred path\fR bit
 set will always be in their own path group.
#10 lore raw
In reply to Yang Feng
Quoted 2 lines · Yang Feng

Please find the up-to-date patch below:

First, one overall question. We have dynamic path selectors available to deal with paths that are just simply slower that other paths, but can still be used together. Is there specific hardware or a specific setup where this isn't good enough and we really need to seperate these paths into different pathgroups, but we can't find out deterministically how the groups should be set up? It just seems like there could be a less hacky solution to this problem, but perhaps there are some situations where this is truly the best option. I'm just wondering what those are.

other commenets inlined

Quoted 95 lines

---

Quoted 1 line

From 1a9426dfbad00b5dbefc7020603e40e8896e4869 Mon Sep 17 00:00:00 2001

From: Yang Feng <[email protected]>
Date: Mon, 15 May 2017 18:33:29 +0800
Subject: [PATCH] [dm-devel] [PATCH] multipath-tools:Prioritizer based on a time-delay algorithm
Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a time-delay algorithm. And the time-delay algorithm is dependent on the following arguments(delay_interval, cons_num). The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path continuously, the IOs' average delay can be calculated.
2. According to the average delay of each path and the weight value "delay_interval", the priority "rc" of each path can be provided.

delay_interval delay_interval delay_interval delay_interval
|---------------|---------------|---------------| |---------------|
|priority rank 1|priority rank 2|priority rank 3|...|priority rank x|
|---------------|---------------|---------------| |---------------|
Priority Rank Partitioning

---
 libmultipath/prioritizers/Makefile      |   6 +-
 libmultipath/prioritizers/delayedpath.c | 261 ++++++++++++++++++++++++++++++++
 libmultipath/prioritizers/delayedpath.h |  17 +++
 multipath/multipath.conf.5              |  19 +++
 4 files changed, 302 insertions(+), 1 deletion(-)
 create mode 100644 libmultipath/prioritizers/delayedpath.c
 create mode 100644 libmultipath/prioritizers/delayedpath.h
libmultipath/prioritizers/Makefile +5 -1
index 36b42e4..8df5234 100644
@@ -18,13 +18,17 @@ LIBS = \
 	libpriorandom.so \
 	libpriordac.so \
 	libprioweightedpath.so \
-	libpriosysfs.so
+	libpriodelayedpath.so \
+	libpriosysfs.so
 
 all: $(LIBS)
 
 libprioalua.so: alua.o alua_rtpg.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
+libpriodelayedpath.so: delayedpath.o  ../checkers/libsg.o
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
+
 libprio%.so: %.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
libmultipath/prioritizers/delayedpath.c new file +41 -0
new file mode 100644
index 0000000..0490e8d
@@ -0,0 +1,261 @@
+/*
+ * (C) Copyright HUAWEI Technology Corp. 2017, 2021   All Rights Reserved.
+ *
+ * main.c
+ *
+ * Prioritizer for device mapper multipath, where the corresponding priority
+ * values of specific paths are provided by a time-delay algorithm. And the
+ * time-delay algorithm is dependent on arguments.
+ *
+ * The principle of the algorithm as follows:
+ * 1. By sending a certain number "cons_num" of read IOs to the current path
+ *    continuously, the IOs' average delay can be calculated.
+ * 2. According to the average delay of each path and the weight value
+ *    "delay_interval", the priority "rc" of each path can be provided.
+ *
+ * Author(s): Yang Feng <[email protected]>
+ *            Zou Ming <[email protected]>
+ *
+ * This file is released under the GPL.
+ */
+#include <stdio.h>
+#include <ctype.h>
+#include <time.h>
+
+#include "debug.h"
+#include "prio.h"
+#include "structs.h"
+#include "../checkers/libsg.h"
+
+#include "delayedpath.h"
+
+#define THRES_USEC_VALUE        300000000LL    /*USEC, 300SEC*/
+#define DEFAULT_DELAY_INTERVAL  10             /*MSEC*/
+#define DEFAULT_CONS_NUM        20
+
+#define MAX_CHAR_SIZE           30
+
+#define CHAR_SEC                "s"
+#define CHAR_MSEC               "ms"
+#define CHAR_USEC               "us"
+

libmultipath/prioritizers/delayedpath.c line 51

Quoted 10 lines
libmultipath/prioritizers/delayedpath.c new file +10 -0
+enum interval_type {
+    INTERVAL_SEC,
+    INTERVAL_MSEC,
+    INTERVAL_USEC,
+    INTERVAL_INVALID
+};
+
+/* interval_unit_str and interval_unit_type keep the same assignment sequence */
+static const char interval_unit_str[][MAX_CHAR_SIZE] = {
+    CHAR_USEC, CHAR_MSEC, CHAR_SEC

This is a nit, but for constant strings, could you please use "char *var" instead of "char var[]", to be consistent with the rest of the multipath code.

Quoted 58 lines
libmultipath/prioritizers/delayedpath.c new file +58 -0
+};
+static const int interval_unit_type[] = {
+    INTERVAL_USEC, INTERVAL_MSEC, INTERVAL_SEC
+};
+
+static const int conversion_ratio[] = {
+	[INTERVAL_SEC]		= USEC_PER_SEC,
+	[INTERVAL_MSEC]	        = USEC_PER_MSEC,
+	[INTERVAL_USEC]		= USEC_PER_USEC,
+	[INTERVAL_INVALID]	= 0
+};
+
+
+static int do_readsector0(int fd, unsigned int timeout)
+{
+	unsigned char buf[4096];
+	unsigned char sbuf[SENSE_BUFF_LEN];
+	int ret;
+
+	ret = sg_read(fd, &buf[0], 4096, &sbuf[0],
+		      SENSE_BUFF_LEN, timeout);
+
+	return ret;
+}
+
+static int get_interval_type(char *source, char *type)
+{
+    char *p;
+    int size;
+    int i;
+
+    for (i = 0; i < sizeof(interval_unit_str)/MAX_CHAR_SIZE; i++)
+    {
+        size = strlen(interval_unit_str[i]);
+        p = strstr(source, interval_unit_str[i]);
+        if (p != NULL && p[size] == '|')
+        {
+            memcpy(type, interval_unit_str[i], size+1);
+            return interval_unit_type[i];
+        }
+    }
+
+    return INTERVAL_INVALID;
+}
+
+/* In multipath.conf, args form: delay_interval|cons_num. For example,
+*  args is "10ms|20", this function can get 10, ms, and 20.
+*/
+static int get_digit_and_type(char *args,
+                              int *interval,
+                              int *consnum,
+                              int *type)
+{
+    char typestr[MAX_CHAR_SIZE];
+    char source[MAX_CHAR_SIZE];
+    char vertica[] = "|";
+    char *tokenbefore = NULL;
+    char *tokenafter = NULL;

libmultipath/prioritizers/delayedpath.c line 119

Quoted 10 lines
libmultipath/prioritizers/delayedpath.c new file +10 -0
+    char *tmp = NULL;
+    unsigned int size = strlen(args);
+
+    if ((args == NULL) || (interval == NULL)
+        || (consnum == NULL) || (type == NULL))
+        return 0;
+
+    /* int type */
+    if ((size < 1) || (size > MAX_CHAR_SIZE-1))
+        return 0;

You should probably have log messages for these error returns.

libmultipath/prioritizers/delayedpath.c line 130

Quoted 11 lines
libmultipath/prioritizers/delayedpath.c new file +11 -0
+
+    memcpy(source, args, size+1);
+    if (strstr(source, vertica) == NULL)
+        return 0;
+
+    *type = get_interval_type(source, typestr);
+    if (*type == INTERVAL_INVALID)
+    {
+        condlog(0, "delay_interval type is invalid");
+        return 0;
+    }

I'm confused here. How do you get to use the default interval. Shouldn't you accept "20s|" and "|30" and as valid inputs that use the defaults for the part they don't specify.

Quoted 14 lines
libmultipath/prioritizers/delayedpath.c new file +14 -0
+    tokenbefore = strtok(source, vertica);
+    tokenafter = strtok(NULL, vertica);
+    typestr[1] = '\0';
+    tokenbefore = strtok(tokenbefore, typestr);
+    if ((tokenbefore == NULL) || (tokenafter == NULL))
+        return 0;
+
+    tmp = tokenbefore;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+        {
+            condlog(0, "delay_interval string include invalid char");
+            return 0;
+        }

libmultipath/prioritizers/delayedpath.c line 155

Quoted 10 lines
libmultipath/prioritizers/delayedpath.c new file +10 -0
+
+    tmp = tokenafter;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+        {
+            condlog(0, "cons_num string include invalid char");
+            return 0;
+        }
+
+    *interval = atoi(tokenbefore);

Why do you keep track of the type and the interval seperately? Can't you just find out the type, and use that to multiply the interval once you read it, and then just use that value, instead of keeping track of two values across multiple functions?

Quoted 55 lines
libmultipath/prioritizers/delayedpath.c new file +55 -0
+    *consnum = atoi(tokenafter);
+
+    return 1;
+}
+
+int check_args_valid(int delay_interval, int cons_num, int type)
+{
+    if (type == INTERVAL_SEC)
+    {
+        if ((delay_interval < 1) || (delay_interval > 60))
+        {
+            condlog(0, "delay_interval values is invalid");
+            return 0;
+        }
+    }
+    else if (type != INTERVAL_INVALID)
+    {
+        if ((delay_interval < 1) || (delay_interval >= 1000))
+        {
+            condlog(0, "delay_interval values is invalid");
+            return 0;
+        }
+    }
+
+    if ((cons_num < 3) || (cons_num > 1000))
+    {
+        condlog(0, "cons_num values is invalid");
+        return 0;
+    }
+
+    return 1;
+}
+
+int get_delay_pref_arg(char *args, int *delay_interval, int *cons_num, int *interval_type)
+{
+    if (get_digit_and_type(args, delay_interval, cons_num, interval_type) == 0)
+        return 0;
+
+    if (check_args_valid(*delay_interval, *cons_num, *interval_type) == 0)
+        return 0;
+
+    return 1;
+}
+
+long long get_conversion_ratio(int type)
+{
+    return conversion_ratio[type];
+}
+
+int getprio (struct path *pp, char *args, unsigned int timeout)
+{
+    int rc, delay_interval, cons_num, type, temp;
+    long long delay, avgdelay, ratio;
+    long long min = THRES_USEC_VALUE;
+    long long max = 0;

libmultipath/prioritizers/delayedpath.c line 220

Quoted 10 lines
libmultipath/prioritizers/delayedpath.c new file +10 -0
+    long long toldelay = 0;
+    long long before, after;
+    struct timespec tv;
+
+	if (pp->fd < 0)
+	    return -PRIO_NO_INFORMATION;
+
+    if (get_delay_pref_arg(args, &delay_interval, &cons_num, &type) == 0)
+    {
+        condlog(3, "%s: get delay arg fail", pp->dev);

Why use the word "fail" in this message? Not setting prio_args to get the defaults seems like a perfectly valid choice.

Quoted 47 lines
libmultipath/prioritizers/delayedpath.c new file +41 -0
+        delay_interval = DEFAULT_DELAY_INTERVAL;
+        cons_num = DEFAULT_CONS_NUM;
+        type = INTERVAL_MSEC;
+    }
+
+    temp = cons_num;
+    while (temp-- > 0)
+    {
+        (void)clock_gettime(CLOCK_MONOTONIC, &tv);
+        before = timeval_to_us(&tv);		
+
+        if (do_readsector0(pp->fd, timeout) == 2)
+        {
+            condlog(0, "%s: path down", pp->dev);
+            return -1;
+        }
+
+        (void)clock_gettime(CLOCK_MONOTONIC, &tv);
+        after = timeval_to_us(&tv);
+
+        delay = after - before;
+    	
+        min = (min <= delay) ? min : delay;
+        max = (max >= delay) ? max : delay;
+
+        toldelay += delay;
+    }
+
+    toldelay -= min + max;
+    avgdelay = toldelay/(long long)(cons_num - 2);
+    if (avgdelay > THRES_USEC_VALUE)
+    {
+        condlog(0, "%s: avgdelay is more than thresold", pp->dev);
+        return 1;
+    }
+
+	ratio = get_conversion_ratio(type);
+	rc = (int)(THRES_USEC_VALUE - (avgdelay/(((long long)delay_interval) * ratio)));
+
+    return rc;
+}
libmultipath/prioritizers/delayedpath.h new file +0 -0
new file mode 100644
index 0000000..d8213e9

libmultipath/prioritizers/delayedpath.h line 4

Quoted 4 lines
libmultipath/prioritizers/delayedpath.h new file +4 -0
@@ -0,0 +1,17 @@
+#ifndef _DELAYEDPATH_H
+#define _DELAYEDPATH_H
+
+#define PRIO_DELAYED_PATH "delayedpath"

In order for the rest of the code to refer to this prioritizer, this define should be in prio.h with the other prioritizer names, and as long as delayedpath.c includes prio.h, there's no need to put it in delayedpath.h.

libmultipath/prioritizers/delayedpath.h line 6

Quoted 2 lines
libmultipath/prioritizers/delayedpath.h new file +2 -0
+
+#define PRIO_NO_INFORMATION 5

The rest of the multipath code only cares if getprio returns a negative number of not. It doesn't check what the specific negative number is. I realize the the alua prioritizer returns a set of error codes, but they aren't used, or even usable in their present form. If we wanted to have better error reporting, we should set up a universal set of error codes in prio.h, and have all prioritizers use them, instead of having each prioritizer define its own error codes. There's no reason why your prioritizer needs to return this error code instead of -1.

libmultipath/prioritizers/delayedpath.h line 15

Quoted 9 lines
libmultipath/prioritizers/delayedpath.h new file +9 -0
+
+#define USEC_PER_SEC      1000000LL
+#define USEC_PER_MSEC     1000LL
+#define USEC_PER_USEC     1LL
+
+static inline long long timeval_to_us(const struct timespec *tv)
+{
+	return ((long long) tv->tv_sec * USEC_PER_SEC) + (tv->tv_nsec >> 10);
+}

No other file besides delayedpath.c will likely be including this .h file, so I don't see any purpose for these being defined here. In fact, I don't see why you can't just have a .c file without a .h file like the majority of prioritizers. I'm pretty sure that none of the prioritizers really need their own .h file.

multipath/multipath.conf.5 Generate a random priority between 1 and 10. · line 298

Quoted 12 lines
libmultipath/prioritizers/delayedpath.h new file +1 -0
+#endif
multipath/multipath.conf.5 +3 -0
index 5939688..f1e126e 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I delayedpath
+Generate the path priority based on a time-delay algorithm.
+Requires prio_args keyword.

Really it doesn't require prio_args if you want to use the default values, and should probably say so.

Quoted 13 lines
multipath/multipath.conf.5 +6 -0
+.TP
 .I datacore
 .\" XXX
 ???. Requires prio_args keyword.
@@ -333,6 +337,21 @@ these values can be looked up through sysfs or by running \fImultipathd show pat
 "%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.*
 .RE
 .TP 12
+.I delayed
+Needs a value of the form
+\fI"<delay_interval|cons_num>"\fR
+.RS
+.TP 8

multipath/multipath.conf.5 these values can be looked up through sysfs or by running \fImultipathd show pat · line 354

Quoted 10 lines
multipath/multipath.conf.5 +10 -0
+.I delay_interval
+The interval values of average IO-time-delay between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 1000), us [1, 1000),
+For example: 10s, or 100us, or 100ms. The default is: 10ms.
+.TP
+.I cons_num
+The number of read IOs sent to the current path continuously, used to calculate the average IO-time-delay. Valid Values: Integer, [3, 1000].
+For example: 30. The default is: 20.
+.RE
+.TP 12

Looking at the "weighted" prio_args definition just above your "delayed" definition, the pipe character "|" is being used to say that any of a set of options is allowed. Your definition has it being a literal character, but it's still inside the angle brackets that usually delineate a variable. perhaps "<delay_interval>|<io_num>" would be easier to understand, or even "[delayed_interval]|[io_num]" if you can omit these to use the defaults.

-Ben

Quoted 15 lines
multipath/multipath.conf.5 +0 -0
 .I alua
 If \fIexclusive_pref_bit\fR is set, paths with the \fIpreferred path\fR bit
 set will always be in their own path group.
Signature · 4 lines
--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
author
#11 lore raw

Hi Benjamin,

Thank you very much for your comments.
Please find my replys and the up-to-date patch.
Best regards!

Quoted 9 lines

First, one overall question. We have dynamic path selectors available to
deal with paths that are just simply slower that other paths, but can
still be used together. Is there specific hardware or a specific setup
where this isn't good enough and we really need to seperate these paths
into different pathgroups, but we can't find out deterministically how
the groups should be set up? It just seems like there could be a less
hacky solution to this problem, but perhaps there are some situations
where this is truly the best option. I'm just wondering what those are.1. In the Storage-Backup environment of HyperCluster,includs one storage array near

to the host and one remote storage array, and the two storage arrays have the same hardware.
The same LUN is writed or readed by the two storage arrays.
However, usually, the average latency of the paths of the remote storage array is much higher than the
near storage array's.
apparently, the prioritizer can be a good automatic solution.
And the current selectors don't solve it, IOs will send to the paths of the remote storage array, IOPS will be influenced unavoidably.
2. In the environment of single storage array, the prioritizer can automatically separate the paths who's latency is much higher, IOs will not send to this paths.
But the current selectors don't solve this problem, IOPS will be influenced unavoidably.

Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+
+/* interval_unit_str and interval_unit_type keep the same assignment sequence */
+static const char interval_unit_str[][MAX_CHAR_SIZE] = {
+    CHAR_USEC, CHAR_MSEC, CHAR_SEC

This is a nit, but for constant strings, could you please use "char *var" instead of "char var[]", to be consistent with the rest of the multipath code.

Thanks, as the following patch.

Quoted 9 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+    if ((args == NULL) || (interval == NULL)
+        || (consnum == NULL) || (type == NULL))
+        return 0;
+
+    /* int type */
+    if ((size < 1) || (size > MAX_CHAR_SIZE-1))
+        return 0;

You should probably have log messages for these error returns.

Thanks, as the following patch.

Quoted 7 lines
Quoted 7 lines
(quoted diff) +7 -0
+
+    memcpy(source, args, size+1);
+    if (strstr(source, vertica) == NULL)
+        return 0;
+
+    *type = get_interval_type(source, typestr);
+    if (*type == INTERVAL_INVALID)
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+    {
+        condlog(0, "delay_interval type is invalid");
+        return 0;
+    }

I'm confused here. How do you get to use the default interval. Shouldn't you accept "20s|" and "|30" and as valid inputs that use the defaults for the part they don't specify.

OK,the default arguments value is removed. If get inputs failed, return default priority "0". As the following patch.

Quoted 22 lines
Quoted 22 lines
(quoted diff) +21 -0
+    tokenbefore = strtok(source, vertica);
+    tokenafter = strtok(NULL, vertica);
+    typestr[1] = '\0';
+    tokenbefore = strtok(tokenbefore, typestr);
+    if ((tokenbefore == NULL) || (tokenafter == NULL))
+        return 0;
+
+    tmp = tokenbefore;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+        {
+            condlog(0, "delay_interval string include invalid char");
+            return 0;
+        }
+
+    tmp = tokenafter;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+        {
+            condlog(0, "cons_num string include invalid char");
+            return 0;
Quoted 8 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+        }
+
+    *interval = atoi(tokenbefore);

Why do you keep track of the type and the interval seperately? Can't you just find out the type, and use that to multiply the interval once you read it, and then just use that value, instead of keeping track of two values across multiple functions?

Thanks, as the following patch.

Quoted 10 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+
+	if (pp->fd < 0)
+	    return -PRIO_NO_INFORMATION;
+
+    if (get_delay_pref_arg(args, &delay_interval, &cons_num, &type) == 0)
+    {
+        condlog(3, "%s: get delay arg fail", pp->dev);

Why use the word "fail" in this message? Not setting prio_args to get the defaults seems like a perfectly valid choice.

The defaults are not used. Insteadly, return default priority "0". See below.

Quoted 7 lines
Quoted 7 lines
libmultipath/prioritizers/delayedpath.h new file +1 -0
new file mode 100644
index 0000000..d8213e9
@@ -0,0 +1,17 @@
+#ifndef _DELAYEDPATH_H
Quoted 8 lines

libmultipath/prioritizers/delayedpath.h line 4

Quoted 3 lines
libmultipath/prioritizers/delayedpath.h new file +3 -0
+#define _DELAYEDPATH_H
+
+#define PRIO_DELAYED_PATH "delayedpath"

In order for the rest of the code to refer to this prioritizer, this define should be in prio.h with the other prioritizer names, and as long as delayedpath.c includes prio.h, there's no need to put it in delayedpath.h.

OK, as the following patch.

Quoted 12 lines

libmultipath/prioritizers/delayedpath.h line 6

Quoted 3 lines
libmultipath/prioritizers/delayedpath.h new file +2 -0
+
+#define PRIO_NO_INFORMATION 5

The rest of the multipath code only cares if getprio returns a negative number of not. It doesn't check what the specific negative number is. I realize the the alua prioritizer returns a set of error codes, but they aren't used, or even usable in their present form. If we wanted to have better error reporting, we should set up a universal set of error codes in prio.h, and have all prioritizers use them, instead of having each prioritizer define its own error codes. There's no reason why your prioritizer needs to return this error code instead of -1.

OK, as the following patch.

Quoted 16 lines

libmultipath/prioritizers/delayedpath.h line 15

Quoted 10 lines
libmultipath/prioritizers/delayedpath.h new file +9 -0
+
+#define USEC_PER_SEC      1000000LL
+#define USEC_PER_MSEC     1000LL
+#define USEC_PER_USEC     1LL
+
+static inline long long timeval_to_us(const struct timespec *tv)
+{
+	return ((long long) tv->tv_sec * USEC_PER_SEC) + (tv->tv_nsec >> 10);
+}

No other file besides delayedpath.c will likely be including this .h file, so I don't see any purpose for these being defined here. In fact, I don't see why you can't just have a .c file without a .h file like the majority of prioritizers. I'm pretty sure that none of the prioritizers really need their own .h file.

OK, as the following patch.

Quoted 16 lines

multipath/multipath.conf.5 Generate a random priority between 1 and 10. · line 298

Quoted 13 lines
libmultipath/prioritizers/delayedpath.h new file +1 -0
+#endif
multipath/multipath.conf.5 +3 -0
index 5939688..f1e126e 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I delayedpath
+Generate the path priority based on a time-delay algorithm.
+Requires prio_args keyword.

Really it doesn't require prio_args if you want to use the default values, and should probably say so.

The default args is discarded, as the following patch.

Quoted 16 lines
Quoted 5 lines
multipath/multipath.conf.5 +5 -0
+.I delayed
+Needs a value of the form
+\fI"<delay_interval|cons_num>"\fR
+.RS
+.TP 8

multipath/multipath.conf.5 Generate a random priority between 1 and 10.

Quoted 10 lines
multipath/multipath.conf.5 +10 -0
+.I delay_interval
+The interval values of average IO-time-delay between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 1000), us [1, 1000),
+For example: 10s, or 100us, or 100ms. The default is: 10ms.
+.TP
+.I cons_num
+The number of read IOs sent to the current path continuously, used to calculate the average IO-time-delay. Valid Values: Integer, [3, 1000].
+For example: 30. The default is: 20.
+.RE
+.TP 12
Quoted 7 lines

Looking at the "weighted" prio_args definition just above your "delayed" definition, the pipe character "|" is being used to say that any of a set of options is allowed. Your definition has it being a literal character, but it's still inside the angle brackets that usually delineate a variable. perhaps "<delay_interval>|<io_num>" would be easier to understand, or even "[delayed_interval]|[io_num]" if you can omit these to use the defaults.

OK, as the following patch.
The up-to-date patch as follows:

---

Quoted 1 line

From 58d718fdd34550bd9c4a32c6e9a87099c1e45a9f Mon Sep 17 00:00:00 2001

From: Yang Feng <[email protected]>
Date: Fri, 19 May 2017 16:09:07 +0800
Subject: [PATCH] libmultipath/prioritizers: Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a latency algorithm. And the latency algorithm is dependent on the following arguments(latency_interval and io_num). The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path continuously, the IOs' average latency can be calculated.
2. According to the average latency of each path and the weight value "latency_interval", the priority "rc" of each path can be provided.

latency_interval latency_interval latency_interval latency_interval
|------------------|------------------|------------------|...|------------------| | priority rank 1 | priority rank 2 | priority rank 3 |...| priority rank x | |------------------|------------------|------------------|...|------------------|
Priority Rank Partitioning

---
 libmultipath/prioritizers/Makefile       |   6 +-
 libmultipath/prioritizers/path_latency.c | 271 +++++++++++++++++++++++++++++++
 multipath/multipath.conf.5               |  18 ++
 libmultipath/prio.h 			  |   1 +
 4 files changed, 295 insertions(+), 1 deletion(-)
 create mode 100644 libmultipath/prioritizers/path_latency.c
libmultipath/prioritizers/Makefile +5 -1
index 36b42e4..d2f20f6 100644
@@ -18,13 +18,17 @@ LIBS = \
 	libpriorandom.so \
 	libpriordac.so \
 	libprioweightedpath.so \
-	libpriosysfs.so
+	libpriopath_latency.so \
+	libpriosysfs.so
 
 all: $(LIBS)
 
 libprioalua.so: alua.o alua_rtpg.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
+libpriopath_latency.so: path_latency.o  ../checkers/libsg.o
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^ -lm
+
 libprio%.so: %.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
libmultipath/prioritizers/path_latency.c new file +271 -0

271 lines not on this page · raw message

multipath/multipath.conf.5 +18 -0
index 5939688..3dd0d77 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I path_latency
+Generate the path priority based on a latency algorithm.
+Requires prio_args keyword.
+.TP
 .I datacore
 .\" XXX
 ???. Requires prio_args keyword.
@@ -333,6 +337,20 @@ these values can be looked up through sysfs or by running \fImultipathd show pat
 "%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.*
 .RE
 .TP 12
+.I path_latency
+Needs a value of the form
+\fI"<latency_interval>|<io_num>"\fR
+.RS
+.TP 8
+.I latency_interval
+The interval values of average latency between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 60000], us [1, 60000000],
+For example: If latency_interval=10ms, the paths will be grouped in priority groups with path latency 0-10ms, 10-20ms, 20-30ms, etc..
+.TP
+.I io_num
+The number of read IOs sent to the current path continuously, used to calculate the average path latency. Valid Values: Integer, [10, 200].
+.RE
+.TP 12
 .I alua
 If \fIexclusive_pref_bit\fR is set, paths with the \fIpreferred path\fR bit
 set will always be in their own path group.
libmultipath/prio.h +1 -0
index 0193c52..c97fe39 100644
@@ -29,6 +29,7 @@ struct path;
 #define PRIO_RDAC		"rdac"
 #define PRIO_WEIGHTED_PATH	"weightedpath"
 #define PRIO_SYSFS		"sysfs"
+#define PRIO_PATH_LATENCY	"path_latency"
 
 /*
  * Value used to mark the fact prio was not defined
Signature · 4 lines
--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
author
#12 lore raw

Hello Benjamin and Christophe,

How about this patch?
Thanks a lot.
Best.

Quoted 562 lines · Yang Feng

Hi Benjamin,

Thank you very much for your comments.
Please find my replys and the up-to-date patch.
Best regards!

Quoted 9 lines

First, one overall question. We have dynamic path selectors available to
deal with paths that are just simply slower that other paths, but can
still be used together. Is there specific hardware or a specific setup
where this isn't good enough and we really need to seperate these paths
into different pathgroups, but we can't find out deterministically how
the groups should be set up? It just seems like there could be a less
hacky solution to this problem, but perhaps there are some situations
where this is truly the best option. I'm just wondering what those are.1. In the Storage-Backup environment of HyperCluster,includs one storage array near

to the host and one remote storage array, and the two storage arrays have the same hardware.
The same LUN is writed or readed by the two storage arrays.
However, usually, the average latency of the paths of the remote storage array is much higher than the
near storage array's.
apparently, the prioritizer can be a good automatic solution.
And the current selectors don't solve it, IOs will send to the paths of the remote storage array, IOPS will be influenced unavoidably.
2. In the environment of single storage array, the prioritizer can automatically separate the paths who's latency is much higher, IOs will not send to this paths.
But the current selectors don't solve this problem, IOPS will be influenced unavoidably.

Quoted 8 lines

libmultipath/prioritizers/path_latency.c line 56

Quoted 4 lines
libmultipath/prioritizers/path_latency.c new file +4 -0
+
+/* interval_unit_str and interval_unit_type keep the same assignment sequence */
+static const char interval_unit_str[][MAX_CHAR_SIZE] = {
+    CHAR_USEC, CHAR_MSEC, CHAR_SEC

This is a nit, but for constant strings, could you please use "char *var" instead of "char var[]", to be consistent with the rest of the multipath code.

Thanks, as the following patch.

Quoted 9 lines

libmultipath/prioritizers/path_latency.c line 154

Quoted 7 lines
libmultipath/prioritizers/path_latency.c new file +7 -0
+    if ((args == NULL) || (interval == NULL)
+        || (consnum == NULL) || (type == NULL))
+        return 0;
+
+    /* int type */
+    if ((size < 1) || (size > MAX_CHAR_SIZE-1))
+        return 0;

You should probably have log messages for these error returns.

Thanks, as the following patch.

Quoted 7 lines
Quoted 7 lines
libmultipath/prioritizers/path_latency.c new file +7 -0
+
+    memcpy(source, args, size+1);
+    if (strstr(source, vertica) == NULL)
+        return 0;
+
+    *type = get_interval_type(source, typestr);
+    if (*type == INTERVAL_INVALID)
Quoted 8 lines

libmultipath/prioritizers/path_latency.c line 98

Quoted 4 lines
libmultipath/prioritizers/path_latency.c new file +4 -0
+    {
+        condlog(0, "delay_interval type is invalid");
+        return 0;
+    }

I'm confused here. How do you get to use the default interval. Shouldn't you accept "20s|" and "|30" and as valid inputs that use the defaults for the part they don't specify.

OK,the default arguments value is removed. If get inputs failed, return default priority "0". As the following patch.

Quoted 22 lines
Quoted 22 lines
(quoted diff) +21 -0
+    tokenbefore = strtok(source, vertica);
+    tokenafter = strtok(NULL, vertica);
+    typestr[1] = '\0';
+    tokenbefore = strtok(tokenbefore, typestr);
+    if ((tokenbefore == NULL) || (tokenafter == NULL))
+        return 0;
+
+    tmp = tokenbefore;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+        {
+            condlog(0, "delay_interval string include invalid char");
+            return 0;
+        }
+
+    tmp = tokenafter;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+        {
+            condlog(0, "cons_num string include invalid char");
+            return 0;
Quoted 8 lines

libmultipath/prioritizers/path_latency.c

Quoted 3 lines
libmultipath/prioritizers/path_latency.c new file +3 -0
+        }
+
+    *interval = atoi(tokenbefore);

Why do you keep track of the type and the interval seperately? Can't you just find out the type, and use that to multiply the interval once you read it, and then just use that value, instead of keeping track of two values across multiple functions?

Thanks, as the following patch.

Quoted 10 lines

libmultipath/prioritizers/path_latency.c

Quoted 7 lines
libmultipath/prioritizers/path_latency.c new file +7 -0
+
+	if (pp->fd < 0)
+	    return -PRIO_NO_INFORMATION;
+
+    if (get_delay_pref_arg(args, &delay_interval, &cons_num, &type) == 0)
+    {
+        condlog(3, "%s: get delay arg fail", pp->dev);

Why use the word "fail" in this message? Not setting prio_args to get the defaults seems like a perfectly valid choice.

The defaults are not used. Insteadly, return default priority "0". See below.

Quoted 7 lines
Quoted 7 lines
libmultipath/prioritizers/delayedpath.h new file +1 -0
new file mode 100644
index 0000000..d8213e9
@@ -0,0 +1,17 @@
+#ifndef _DELAYEDPATH_H
Quoted 8 lines

libmultipath/prioritizers/delayedpath.h line 4

Quoted 3 lines
libmultipath/prioritizers/delayedpath.h new file +3 -0
+#define _DELAYEDPATH_H
+
+#define PRIO_DELAYED_PATH "delayedpath"

In order for the rest of the code to refer to this prioritizer, this define should be in prio.h with the other prioritizer names, and as long as delayedpath.c includes prio.h, there's no need to put it in delayedpath.h.

OK, as the following patch.

Quoted 12 lines

libmultipath/prioritizers/delayedpath.h line 6

Quoted 3 lines
libmultipath/prioritizers/delayedpath.h new file +2 -0
+
+#define PRIO_NO_INFORMATION 5

The rest of the multipath code only cares if getprio returns a negative number of not. It doesn't check what the specific negative number is. I realize the the alua prioritizer returns a set of error codes, but they aren't used, or even usable in their present form. If we wanted to have better error reporting, we should set up a universal set of error codes in prio.h, and have all prioritizers use them, instead of having each prioritizer define its own error codes. There's no reason why your prioritizer needs to return this error code instead of -1.

OK, as the following patch.

Quoted 16 lines

libmultipath/prioritizers/delayedpath.h line 15

Quoted 10 lines
libmultipath/prioritizers/delayedpath.h new file +9 -0
+
+#define USEC_PER_SEC      1000000LL
+#define USEC_PER_MSEC     1000LL
+#define USEC_PER_USEC     1LL
+
+static inline long long timeval_to_us(const struct timespec *tv)
+{
+	return ((long long) tv->tv_sec * USEC_PER_SEC) + (tv->tv_nsec >> 10);
+}

No other file besides delayedpath.c will likely be including this .h file, so I don't see any purpose for these being defined here. In fact, I don't see why you can't just have a .c file without a .h file like the majority of prioritizers. I'm pretty sure that none of the prioritizers really need their own .h file.

OK, as the following patch.

Quoted 16 lines

multipath/multipath.conf.5 Generate a random priority between 1 and 10. · line 298

Quoted 13 lines
libmultipath/prioritizers/delayedpath.h new file +1 -0
+#endif
multipath/multipath.conf.5 +3 -0
index 5939688..f1e126e 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I delayedpath
+Generate the path priority based on a time-delay algorithm.
+Requires prio_args keyword.

Really it doesn't require prio_args if you want to use the default values, and should probably say so.

The default args is discarded, as the following patch.

Quoted 16 lines
Quoted 5 lines
multipath/multipath.conf.5 +5 -0
+.I delayed
+Needs a value of the form
+\fI"<delay_interval|cons_num>"\fR
+.RS
+.TP 8

multipath/multipath.conf.5 Generate a random priority between 1 and 10.

Quoted 10 lines
multipath/multipath.conf.5 +10 -0
+.I delay_interval
+The interval values of average IO-time-delay between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 1000), us [1, 1000),
+For example: 10s, or 100us, or 100ms. The default is: 10ms.
+.TP
+.I cons_num
+The number of read IOs sent to the current path continuously, used to calculate the average IO-time-delay. Valid Values: Integer, [3, 1000].
+For example: 30. The default is: 20.
+.RE
+.TP 12
Quoted 7 lines

Looking at the "weighted" prio_args definition just above your "delayed" definition, the pipe character "|" is being used to say that any of a set of options is allowed. Your definition has it being a literal character, but it's still inside the angle brackets that usually delineate a variable. perhaps "<delay_interval>|<io_num>" would be easier to understand, or even "[delayed_interval]|[io_num]" if you can omit these to use the defaults.

OK, as the following patch.
The up-to-date patch as follows:

---

Quoted 1 line

From 58d718fdd34550bd9c4a32c6e9a87099c1e45a9f Mon Sep 17 00:00:00 2001

From: Yang Feng <[email protected]>
Date: Fri, 19 May 2017 16:09:07 +0800
Subject: [PATCH] libmultipath/prioritizers: Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a latency algorithm. And the latency algorithm is dependent on the following arguments(latency_interval and io_num). The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path continuously, the IOs' average latency can be calculated.
2. According to the average latency of each path and the weight value "latency_interval", the priority "rc" of each path can be provided.

latency_interval latency_interval latency_interval latency_interval
|------------------|------------------|------------------|...|------------------| | priority rank 1 | priority rank 2 | priority rank 3 |...| priority rank x | |------------------|------------------|------------------|...|------------------|
Priority Rank Partitioning

---
 libmultipath/prioritizers/Makefile       |   6 +-
 libmultipath/prioritizers/path_latency.c | 271 +++++++++++++++++++++++++++++++
 multipath/multipath.conf.5               |  18 ++
 libmultipath/prio.h 			  |   1 +
 4 files changed, 295 insertions(+), 1 deletion(-)
 create mode 100644 libmultipath/prioritizers/path_latency.c
libmultipath/prioritizers/Makefile +5 -1
index 36b42e4..d2f20f6 100644
@@ -18,13 +18,17 @@ LIBS = \
 	libpriorandom.so \
 	libpriordac.so \
 	libprioweightedpath.so \
-	libpriosysfs.so
+	libpriopath_latency.so \
+	libpriosysfs.so
 
 all: $(LIBS)
 
 libprioalua.so: alua.o alua_rtpg.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
+libpriopath_latency.so: path_latency.o  ../checkers/libsg.o
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^ -lm
+
 libprio%.so: %.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
libmultipath/prioritizers/path_latency.c new file +271 -0
new file mode 100644
index 0000000..a666b6c
@@ -0,0 +1,271 @@
+/*
+ * (C) Copyright HUAWEI Technology Corp. 2017, 2021   All Rights Reserved.
+ *
+ * main.c
+ *
+ * Prioritizer for device mapper multipath, where the corresponding priority
+ * values of specific paths are provided by a latency algorithm. And the
+ * latency algorithm is dependent on arguments.
+ *
+ * The principle of the algorithm as follows:
+ * 1. By sending a certain number "io_num" of read IOs to the current path
+ *    continuously, the IOs' average latency can be calculated.
+ * 2. According to the average latency of each path and the weight value
+ *    "latency_interval", the priority "rc" of each path can be provided.
+ *
+ * Author(s): Yang Feng <[email protected]>
+ *            Zou Ming <[email protected]>
+ *
+ * This file is released under the GPL.
+ */
+#include <stdio.h>
+#include <math.h>
+#include <ctype.h>
+#include <time.h>
+
+#include "debug.h"
+#include "prio.h"
+#include "structs.h"
+#include "../checkers/libsg.h"
+
+#define THRES_USEC_VALUE        120000000LL    /*unit: us, =120s*/
+
+#define MAX_IO_NUM              200
+#define MIN_IO_NUM              10
+
+#define MAX_LATENCY_INTERVAL    60            /*unit: s*/
+#define MIN_LATENCY_INTERVAL    1             /*unit: us, or ms, or s*/
+
+#define DEFAULT_PRIORITY        0
+
+#define MAX_CHAR_SIZE           30
+
+#define CHAR_USEC               "us"
+#define CHAR_MSEC               "ms"
+#define CHAR_SEC                "s"
+
+enum interval_type {
+    INTERVAL_USEC,
+    INTERVAL_MSEC,
+    INTERVAL_SEC,
+    INTERVAL_INVALID
+};
+
+/* interval_unit_str and interval_unit_type keep the same assignment sequence */
+static const char *interval_unit_str[MAX_CHAR_SIZE] = {
+    CHAR_USEC, CHAR_MSEC, CHAR_SEC
+};
+static const int interval_unit_type[] = {
+    INTERVAL_USEC, INTERVAL_MSEC, INTERVAL_SEC
+};
+
+#define USEC_PER_SEC      1000000LL
+#define USEC_PER_MSEC     1000LL
+#define USEC_PER_USEC     1LL
+
+static const int conversion_ratio[] = {
+    [INTERVAL_USEC]		= USEC_PER_USEC,
+    [INTERVAL_MSEC]     = USEC_PER_MSEC,
+    [INTERVAL_SEC]		= USEC_PER_SEC,
+    [INTERVAL_INVALID]	= 0
+};
+
+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 >> 10);
+}
+
+static int do_readsector0(int fd, unsigned int timeout)
+{
+	unsigned char buf[4096];
+	unsigned char sbuf[SENSE_BUFF_LEN];
+	int ret;
+
+	ret = sg_read(fd, &buf[0], 4096, &sbuf[0],
+		      SENSE_BUFF_LEN, timeout);
+
+	return ret;
+}
+
+int check_args_valid(int io_num, long long latency_interval, int type)
+{
+    if ((io_num < MIN_IO_NUM) || (io_num > MAX_IO_NUM))
+    {
+        condlog(0, "args io_num is more than the valid values range");
+        return 0;
+    }
+
+    /* s:[1, 60], ms:[1, 60000], us:[1, 60000000] */
+    if ((latency_interval < MIN_LATENCY_INTERVAL) || (latency_interval > (MAX_LATENCY_INTERVAL * USEC_PER_SEC / conversion_ratio[type])))
+    {
+        condlog(0, "args latency_interval is more than the valid values range");
+        return 0;
+    }
+
+    return 1;
+}
+
+static int get_interval_type(char *type)
+{
+    int index;
+
+    for (index = 0; index < sizeof(interval_unit_str)/MAX_CHAR_SIZE; index++)
+    {
+        if (strcmp(type, interval_unit_str[index]) == 0)
+        {
+            return interval_unit_type[index];
+        }
+    }
+
+    return INTERVAL_INVALID;
+}
+
+long long get_conversion_ratio(int type)
+{
+    return conversion_ratio[type];
+}
+
+/* In multipath.conf, args form: io_num|latency_interval. For example,
+*  args is "20|10ms", this function can get 20, 10.
+*/
+static int get_interval_and_ionum(char *args,
+                                        int *ionum,
+                                        long long *interval)
+{
+    char source[MAX_CHAR_SIZE];
+    char vertica = '|';
+    char *endstrbefore = NULL;
+    char *endstrafter = NULL;
+    int type;
+    unsigned int size = strlen(args);
+    long long ratio;
+
+    if ((args == NULL) || (ionum == NULL) || (interval == NULL))
+    {
+        condlog(0, "args string is NULL");
+        return 0;
+    }
+
+    if ((size < 1) || (size > MAX_CHAR_SIZE-1))
+    {
+        condlog(0, "args string's size is too long");
+        return 0;
+    }
+
+    memcpy(source, args, size+1);
+
+    if (!isdigit(source[0]))
+    {
+        condlog(0, "args io_num string's first char is not digit");
+        return 0;
+    }
+
+    *ionum = (int)strtoul(source, &endstrbefore, 10);
+    if (endstrbefore[0] != vertica)
+    {
+        condlog(0, "segmentation char is invalid");
+        return 0;
+    }
+
+    if (!isdigit(endstrbefore[1]))
+    {
+        condlog(0, "args latency_interval string's first char is not digit");
+        return 0;
+    }
+
+    *interval = (long long)strtol(&endstrbefore[1], &endstrafter, 10);
+    type = get_interval_type(endstrafter);
+    if (type == INTERVAL_INVALID)
+    {
+        condlog(0, "args latency_interval type is invalid");
+        return 0;
+    }
+
+    if (check_args_valid(*ionum, *interval, type) == 0)
+    {
+        return 0;
+    }
+
+	ratio = get_conversion_ratio(type);
+    *interval *= (long long)ratio;
+
+    return 1;
+}
+
+long long calc_standard_deviation(long long *path_latency, int size, long long avglatency)
+{
+    int index;
+    long long total = 0;
+
+    for (index = 0; index < size; index++)
+    {
+        total += (path_latency[index] - avglatency) * (path_latency[index] - avglatency);
+    }
+
+    total /= (size-1);
+
+    return (long long)sqrt((double)total);
+}
+
+int getprio (struct path *pp, char *args, unsigned int timeout)
+{
+    int rc, temp;
+    int index = 0;
+    int io_num;
+    long long latency_interval;
+    long long avglatency;
+    long long standard_deviation;
+    long long toldelay = 0;
+    long long before, after;
+    struct timespec tv;
+
+	if (pp->fd < 0)
+		return -1;
+
+    if (get_interval_and_ionum(args, &io_num, &latency_interval) == 0)
+    {
+        condlog(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)
+        {
+            condlog(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;
+    condlog(4, "%s: average latency is (%lld)", pp->dev, avglatency);
+
+    if (avglatency > THRES_USEC_VALUE)
+    {
+        condlog(0, "%s: average latency (%lld) is more than thresold", pp->dev, avglatency);
+        return DEFAULT_PRIORITY;
+    }
+
+    /* warn the user if the latency_interval set is smaller than (2 * standard deviation), or equal */
+    standard_deviation = calc_standard_deviation(path_latency, index, avglatency);
+    if (latency_interval <= (2 * standard_deviation))
+        condlog(3, "%s: args latency_interval set is smaller than 2 * standard deviation (%lld us), or equal",
+            pp->dev, standard_deviation);
+
+	rc = (int)(THRES_USEC_VALUE - (avglatency/(long long)latency_interval));
+    return rc;
+}
multipath/multipath.conf.5 +18 -0
index 5939688..3dd0d77 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I path_latency
+Generate the path priority based on a latency algorithm.
+Requires prio_args keyword.
+.TP
 .I datacore
 .\" XXX
 ???. Requires prio_args keyword.
@@ -333,6 +337,20 @@ these values can be looked up through sysfs or by running \fImultipathd show pat
 "%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.*
 .RE
 .TP 12
+.I path_latency
+Needs a value of the form
+\fI"<latency_interval>|<io_num>"\fR
+.RS
+.TP 8
+.I latency_interval
+The interval values of average latency between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 60000], us [1, 60000000],
+For example: If latency_interval=10ms, the paths will be grouped in priority groups with path latency 0-10ms, 10-20ms, 20-30ms, etc..
+.TP
+.I io_num
+The number of read IOs sent to the current path continuously, used to calculate the average path latency. Valid Values: Integer, [10, 200].
+.RE
+.TP 12
 .I alua
 If \fIexclusive_pref_bit\fR is set, paths with the \fIpreferred path\fR bit
 set will always be in their own path group.
libmultipath/prio.h +0 -0
index 0193c52..c97fe39 100644

libmultipath/prio.h struct path; · line 35

Quoted 7 lines
libmultipath/prio.h +1 -0
@@ -29,6 +29,7 @@ struct path;
 #define PRIO_RDAC		"rdac"
 #define PRIO_WEIGHTED_PATH	"weightedpath"
 #define PRIO_SYSFS		"sysfs"
+#define PRIO_PATH_LATENCY	"path_latency"
 
 /*
  * Value used to mark the fact prio was not defined

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel

author
#13 lore raw
In reply to Yang Feng

Hello Benjamin,

How about this patch?
Look forward your replies.
Thank you very much.
Best regards!

Quoted 562 lines · Yang Feng

Hi Benjamin,

Thank you very much for your comments.
Please find my replys and the up-to-date patch.
Best regards!

Quoted 9 lines

First, one overall question. We have dynamic path selectors available to
deal with paths that are just simply slower that other paths, but can
still be used together. Is there specific hardware or a specific setup
where this isn't good enough and we really need to seperate these paths
into different pathgroups, but we can't find out deterministically how
the groups should be set up? It just seems like there could be a less
hacky solution to this problem, but perhaps there are some situations
where this is truly the best option. I'm just wondering what those are.1. In the Storage-Backup environment of HyperCluster,includs one storage array near

to the host and one remote storage array, and the two storage arrays have the same hardware.
The same LUN is writed or readed by the two storage arrays.
However, usually, the average latency of the paths of the remote storage array is much higher than the
near storage array's.
apparently, the prioritizer can be a good automatic solution.
And the current selectors don't solve it, IOs will send to the paths of the remote storage array, IOPS will be influenced unavoidably.
2. In the environment of single storage array, the prioritizer can automatically separate the paths who's latency is much higher, IOs will not send to this paths.
But the current selectors don't solve this problem, IOPS will be influenced unavoidably.

Quoted 8 lines

libmultipath/prioritizers/path_latency.c line 56

Quoted 4 lines
libmultipath/prioritizers/path_latency.c new file +4 -0
+
+/* interval_unit_str and interval_unit_type keep the same assignment sequence */
+static const char interval_unit_str[][MAX_CHAR_SIZE] = {
+    CHAR_USEC, CHAR_MSEC, CHAR_SEC

This is a nit, but for constant strings, could you please use "char *var" instead of "char var[]", to be consistent with the rest of the multipath code.

Thanks, as the following patch.

Quoted 9 lines

libmultipath/prioritizers/path_latency.c line 154

Quoted 7 lines
libmultipath/prioritizers/path_latency.c new file +7 -0
+    if ((args == NULL) || (interval == NULL)
+        || (consnum == NULL) || (type == NULL))
+        return 0;
+
+    /* int type */
+    if ((size < 1) || (size > MAX_CHAR_SIZE-1))
+        return 0;

You should probably have log messages for these error returns.

Thanks, as the following patch.

Quoted 7 lines
Quoted 7 lines
libmultipath/prioritizers/path_latency.c new file +7 -0
+
+    memcpy(source, args, size+1);
+    if (strstr(source, vertica) == NULL)
+        return 0;
+
+    *type = get_interval_type(source, typestr);
+    if (*type == INTERVAL_INVALID)
Quoted 8 lines

libmultipath/prioritizers/path_latency.c line 98

Quoted 4 lines
libmultipath/prioritizers/path_latency.c new file +4 -0
+    {
+        condlog(0, "delay_interval type is invalid");
+        return 0;
+    }

I'm confused here. How do you get to use the default interval. Shouldn't you accept "20s|" and "|30" and as valid inputs that use the defaults for the part they don't specify.

OK,the default arguments value is removed. If get inputs failed, return default priority "0". As the following patch.

Quoted 22 lines
Quoted 22 lines
(quoted diff) +21 -0
+    tokenbefore = strtok(source, vertica);
+    tokenafter = strtok(NULL, vertica);
+    typestr[1] = '\0';
+    tokenbefore = strtok(tokenbefore, typestr);
+    if ((tokenbefore == NULL) || (tokenafter == NULL))
+        return 0;
+
+    tmp = tokenbefore;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+        {
+            condlog(0, "delay_interval string include invalid char");
+            return 0;
+        }
+
+    tmp = tokenafter;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+        {
+            condlog(0, "cons_num string include invalid char");
+            return 0;
Quoted 8 lines

libmultipath/prioritizers/path_latency.c

Quoted 3 lines
libmultipath/prioritizers/path_latency.c new file +3 -0
+        }
+
+    *interval = atoi(tokenbefore);

Why do you keep track of the type and the interval seperately? Can't you just find out the type, and use that to multiply the interval once you read it, and then just use that value, instead of keeping track of two values across multiple functions?

Thanks, as the following patch.

Quoted 10 lines

libmultipath/prioritizers/path_latency.c

Quoted 7 lines
libmultipath/prioritizers/path_latency.c new file +7 -0
+
+	if (pp->fd < 0)
+	    return -PRIO_NO_INFORMATION;
+
+    if (get_delay_pref_arg(args, &delay_interval, &cons_num, &type) == 0)
+    {
+        condlog(3, "%s: get delay arg fail", pp->dev);

Why use the word "fail" in this message? Not setting prio_args to get the defaults seems like a perfectly valid choice.

The defaults are not used. Insteadly, return default priority "0". See below.

Quoted 7 lines
Quoted 7 lines
libmultipath/prioritizers/delayedpath.h new file +1 -0
new file mode 100644
index 0000000..d8213e9
@@ -0,0 +1,17 @@
+#ifndef _DELAYEDPATH_H
Quoted 8 lines

libmultipath/prioritizers/delayedpath.h line 4

Quoted 3 lines
libmultipath/prioritizers/delayedpath.h new file +3 -0
+#define _DELAYEDPATH_H
+
+#define PRIO_DELAYED_PATH "delayedpath"

In order for the rest of the code to refer to this prioritizer, this define should be in prio.h with the other prioritizer names, and as long as delayedpath.c includes prio.h, there's no need to put it in delayedpath.h.

OK, as the following patch.

Quoted 12 lines

libmultipath/prioritizers/delayedpath.h line 6

Quoted 3 lines
libmultipath/prioritizers/delayedpath.h new file +2 -0
+
+#define PRIO_NO_INFORMATION 5

The rest of the multipath code only cares if getprio returns a negative number of not. It doesn't check what the specific negative number is. I realize the the alua prioritizer returns a set of error codes, but they aren't used, or even usable in their present form. If we wanted to have better error reporting, we should set up a universal set of error codes in prio.h, and have all prioritizers use them, instead of having each prioritizer define its own error codes. There's no reason why your prioritizer needs to return this error code instead of -1.

OK, as the following patch.

Quoted 16 lines

libmultipath/prioritizers/delayedpath.h line 15

Quoted 10 lines
libmultipath/prioritizers/delayedpath.h new file +9 -0
+
+#define USEC_PER_SEC      1000000LL
+#define USEC_PER_MSEC     1000LL
+#define USEC_PER_USEC     1LL
+
+static inline long long timeval_to_us(const struct timespec *tv)
+{
+	return ((long long) tv->tv_sec * USEC_PER_SEC) + (tv->tv_nsec >> 10);
+}

No other file besides delayedpath.c will likely be including this .h file, so I don't see any purpose for these being defined here. In fact, I don't see why you can't just have a .c file without a .h file like the majority of prioritizers. I'm pretty sure that none of the prioritizers really need their own .h file.

OK, as the following patch.

Quoted 16 lines

multipath/multipath.conf.5 Generate a random priority between 1 and 10. · line 298

Quoted 13 lines
libmultipath/prioritizers/delayedpath.h new file +1 -0
+#endif
multipath/multipath.conf.5 +3 -0
index 5939688..f1e126e 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I delayedpath
+Generate the path priority based on a time-delay algorithm.
+Requires prio_args keyword.

Really it doesn't require prio_args if you want to use the default values, and should probably say so.

The default args is discarded, as the following patch.

Quoted 16 lines
Quoted 5 lines
multipath/multipath.conf.5 +5 -0
+.I delayed
+Needs a value of the form
+\fI"<delay_interval|cons_num>"\fR
+.RS
+.TP 8

multipath/multipath.conf.5 Generate a random priority between 1 and 10.

Quoted 10 lines
multipath/multipath.conf.5 +10 -0
+.I delay_interval
+The interval values of average IO-time-delay between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 1000), us [1, 1000),
+For example: 10s, or 100us, or 100ms. The default is: 10ms.
+.TP
+.I cons_num
+The number of read IOs sent to the current path continuously, used to calculate the average IO-time-delay. Valid Values: Integer, [3, 1000].
+For example: 30. The default is: 20.
+.RE
+.TP 12
Quoted 7 lines

Looking at the "weighted" prio_args definition just above your "delayed" definition, the pipe character "|" is being used to say that any of a set of options is allowed. Your definition has it being a literal character, but it's still inside the angle brackets that usually delineate a variable. perhaps "<delay_interval>|<io_num>" would be easier to understand, or even "[delayed_interval]|[io_num]" if you can omit these to use the defaults.

OK, as the following patch.
The up-to-date patch as follows:

---

Quoted 1 line

From 58d718fdd34550bd9c4a32c6e9a87099c1e45a9f Mon Sep 17 00:00:00 2001

From: Yang Feng <[email protected]>
Date: Fri, 19 May 2017 16:09:07 +0800
Subject: [PATCH] libmultipath/prioritizers: Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a latency algorithm. And the latency algorithm is dependent on the following arguments(latency_interval and io_num). The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path continuously, the IOs' average latency can be calculated.
2. According to the average latency of each path and the weight value "latency_interval", the priority "rc" of each path can be provided.

latency_interval latency_interval latency_interval latency_interval
|------------------|------------------|------------------|...|------------------| | priority rank 1 | priority rank 2 | priority rank 3 |...| priority rank x | |------------------|------------------|------------------|...|------------------|
Priority Rank Partitioning

---
 libmultipath/prioritizers/Makefile       |   6 +-
 libmultipath/prioritizers/path_latency.c | 271 +++++++++++++++++++++++++++++++
 multipath/multipath.conf.5               |  18 ++
 libmultipath/prio.h 			  |   1 +
 4 files changed, 295 insertions(+), 1 deletion(-)
 create mode 100644 libmultipath/prioritizers/path_latency.c
libmultipath/prioritizers/Makefile +5 -1
index 36b42e4..d2f20f6 100644
@@ -18,13 +18,17 @@ LIBS = \
 	libpriorandom.so \
 	libpriordac.so \
 	libprioweightedpath.so \
-	libpriosysfs.so
+	libpriopath_latency.so \
+	libpriosysfs.so
 
 all: $(LIBS)
 
 libprioalua.so: alua.o alua_rtpg.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
+libpriopath_latency.so: path_latency.o  ../checkers/libsg.o
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^ -lm
+
 libprio%.so: %.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
libmultipath/prioritizers/path_latency.c new file +271 -0
new file mode 100644
index 0000000..a666b6c
@@ -0,0 +1,271 @@
+/*
+ * (C) Copyright HUAWEI Technology Corp. 2017, 2021   All Rights Reserved.
+ *
+ * main.c
+ *
+ * Prioritizer for device mapper multipath, where the corresponding priority
+ * values of specific paths are provided by a latency algorithm. And the
+ * latency algorithm is dependent on arguments.
+ *
+ * The principle of the algorithm as follows:
+ * 1. By sending a certain number "io_num" of read IOs to the current path
+ *    continuously, the IOs' average latency can be calculated.
+ * 2. According to the average latency of each path and the weight value
+ *    "latency_interval", the priority "rc" of each path can be provided.
+ *
+ * Author(s): Yang Feng <[email protected]>
+ *            Zou Ming <[email protected]>
+ *
+ * This file is released under the GPL.
+ */
+#include <stdio.h>
+#include <math.h>
+#include <ctype.h>
+#include <time.h>
+
+#include "debug.h"
+#include "prio.h"
+#include "structs.h"
+#include "../checkers/libsg.h"
+
+#define THRES_USEC_VALUE        120000000LL    /*unit: us, =120s*/
+
+#define MAX_IO_NUM              200
+#define MIN_IO_NUM              10
+
+#define MAX_LATENCY_INTERVAL    60            /*unit: s*/
+#define MIN_LATENCY_INTERVAL    1             /*unit: us, or ms, or s*/
+
+#define DEFAULT_PRIORITY        0
+
+#define MAX_CHAR_SIZE           30
+
+#define CHAR_USEC               "us"
+#define CHAR_MSEC               "ms"
+#define CHAR_SEC                "s"
+
+enum interval_type {
+    INTERVAL_USEC,
+    INTERVAL_MSEC,
+    INTERVAL_SEC,
+    INTERVAL_INVALID
+};
+
+/* interval_unit_str and interval_unit_type keep the same assignment sequence */
+static const char *interval_unit_str[MAX_CHAR_SIZE] = {
+    CHAR_USEC, CHAR_MSEC, CHAR_SEC
+};
+static const int interval_unit_type[] = {
+    INTERVAL_USEC, INTERVAL_MSEC, INTERVAL_SEC
+};
+
+#define USEC_PER_SEC      1000000LL
+#define USEC_PER_MSEC     1000LL
+#define USEC_PER_USEC     1LL
+
+static const int conversion_ratio[] = {
+    [INTERVAL_USEC]		= USEC_PER_USEC,
+    [INTERVAL_MSEC]     = USEC_PER_MSEC,
+    [INTERVAL_SEC]		= USEC_PER_SEC,
+    [INTERVAL_INVALID]	= 0
+};
+
+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 >> 10);
+}
+
+static int do_readsector0(int fd, unsigned int timeout)
+{
+	unsigned char buf[4096];
+	unsigned char sbuf[SENSE_BUFF_LEN];
+	int ret;
+
+	ret = sg_read(fd, &buf[0], 4096, &sbuf[0],
+		      SENSE_BUFF_LEN, timeout);
+
+	return ret;
+}
+
+int check_args_valid(int io_num, long long latency_interval, int type)
+{
+    if ((io_num < MIN_IO_NUM) || (io_num > MAX_IO_NUM))
+    {
+        condlog(0, "args io_num is more than the valid values range");
+        return 0;
+    }
+
+    /* s:[1, 60], ms:[1, 60000], us:[1, 60000000] */
+    if ((latency_interval < MIN_LATENCY_INTERVAL) || (latency_interval > (MAX_LATENCY_INTERVAL * USEC_PER_SEC / conversion_ratio[type])))
+    {
+        condlog(0, "args latency_interval is more than the valid values range");
+        return 0;
+    }
+
+    return 1;
+}
+
+static int get_interval_type(char *type)
+{
+    int index;
+
+    for (index = 0; index < sizeof(interval_unit_str)/MAX_CHAR_SIZE; index++)
+    {
+        if (strcmp(type, interval_unit_str[index]) == 0)
+        {
+            return interval_unit_type[index];
+        }
+    }
+
+    return INTERVAL_INVALID;
+}
+
+long long get_conversion_ratio(int type)
+{
+    return conversion_ratio[type];
+}
+
+/* In multipath.conf, args form: io_num|latency_interval. For example,
+*  args is "20|10ms", this function can get 20, 10.
+*/
+static int get_interval_and_ionum(char *args,
+                                        int *ionum,
+                                        long long *interval)
+{
+    char source[MAX_CHAR_SIZE];
+    char vertica = '|';
+    char *endstrbefore = NULL;
+    char *endstrafter = NULL;
+    int type;
+    unsigned int size = strlen(args);
+    long long ratio;
+
+    if ((args == NULL) || (ionum == NULL) || (interval == NULL))
+    {
+        condlog(0, "args string is NULL");
+        return 0;
+    }
+
+    if ((size < 1) || (size > MAX_CHAR_SIZE-1))
+    {
+        condlog(0, "args string's size is too long");
+        return 0;
+    }
+
+    memcpy(source, args, size+1);
+
+    if (!isdigit(source[0]))
+    {
+        condlog(0, "args io_num string's first char is not digit");
+        return 0;
+    }
+
+    *ionum = (int)strtoul(source, &endstrbefore, 10);
+    if (endstrbefore[0] != vertica)
+    {
+        condlog(0, "segmentation char is invalid");
+        return 0;
+    }
+
+    if (!isdigit(endstrbefore[1]))
+    {
+        condlog(0, "args latency_interval string's first char is not digit");
+        return 0;
+    }
+
+    *interval = (long long)strtol(&endstrbefore[1], &endstrafter, 10);
+    type = get_interval_type(endstrafter);
+    if (type == INTERVAL_INVALID)
+    {
+        condlog(0, "args latency_interval type is invalid");
+        return 0;
+    }
+
+    if (check_args_valid(*ionum, *interval, type) == 0)
+    {
+        return 0;
+    }
+
+	ratio = get_conversion_ratio(type);
+    *interval *= (long long)ratio;
+
+    return 1;
+}
+
+long long calc_standard_deviation(long long *path_latency, int size, long long avglatency)
+{
+    int index;
+    long long total = 0;
+
+    for (index = 0; index < size; index++)
+    {
+        total += (path_latency[index] - avglatency) * (path_latency[index] - avglatency);
+    }
+
+    total /= (size-1);
+
+    return (long long)sqrt((double)total);
+}
+
+int getprio (struct path *pp, char *args, unsigned int timeout)
+{
+    int rc, temp;
+    int index = 0;
+    int io_num;
+    long long latency_interval;
+    long long avglatency;
+    long long standard_deviation;
+    long long toldelay = 0;
+    long long before, after;
+    struct timespec tv;
+
+	if (pp->fd < 0)
+		return -1;
+
+    if (get_interval_and_ionum(args, &io_num, &latency_interval) == 0)
+    {
+        condlog(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)
+        {
+            condlog(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;
+    condlog(4, "%s: average latency is (%lld)", pp->dev, avglatency);
+
+    if (avglatency > THRES_USEC_VALUE)
+    {
+        condlog(0, "%s: average latency (%lld) is more than thresold", pp->dev, avglatency);
+        return DEFAULT_PRIORITY;
+    }
+
+    /* warn the user if the latency_interval set is smaller than (2 * standard deviation), or equal */
+    standard_deviation = calc_standard_deviation(path_latency, index, avglatency);
+    if (latency_interval <= (2 * standard_deviation))
+        condlog(3, "%s: args latency_interval set is smaller than 2 * standard deviation (%lld us), or equal",
+            pp->dev, standard_deviation);
+
+	rc = (int)(THRES_USEC_VALUE - (avglatency/(long long)latency_interval));
+    return rc;
+}
multipath/multipath.conf.5 +18 -0
index 5939688..3dd0d77 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I path_latency
+Generate the path priority based on a latency algorithm.
+Requires prio_args keyword.
+.TP
 .I datacore
 .\" XXX
 ???. Requires prio_args keyword.
@@ -333,6 +337,20 @@ these values can be looked up through sysfs or by running \fImultipathd show pat
 "%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.*
 .RE
 .TP 12
+.I path_latency
+Needs a value of the form
+\fI"<latency_interval>|<io_num>"\fR
+.RS
+.TP 8
+.I latency_interval
+The interval values of average latency between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 60000], us [1, 60000000],
+For example: If latency_interval=10ms, the paths will be grouped in priority groups with path latency 0-10ms, 10-20ms, 20-30ms, etc..
+.TP
+.I io_num
+The number of read IOs sent to the current path continuously, used to calculate the average path latency. Valid Values: Integer, [10, 200].
+.RE
+.TP 12
 .I alua
 If \fIexclusive_pref_bit\fR is set, paths with the \fIpreferred path\fR bit
 set will always be in their own path group.
libmultipath/prio.h +0 -0
index 0193c52..c97fe39 100644

libmultipath/prio.h struct path; · line 35

Quoted 7 lines
libmultipath/prio.h +1 -0
@@ -29,6 +29,7 @@ struct path;
 #define PRIO_RDAC		"rdac"
 #define PRIO_WEIGHTED_PATH	"weightedpath"
 #define PRIO_SYSFS		"sysfs"
+#define PRIO_PATH_LATENCY	"path_latency"
 
 /*
  * Value used to mark the fact prio was not defined

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel

author
#14 lore raw

Re: [PATCH] multipath-tools:Prioritizer based on a latency algorithm

In reply to Yang Feng

Hello Benjamin,

Sorry for reply's word format error, and fixed now. Thank you very much for your comments. Please find my replys and the up-to-date patch.

Look forward your reply.
Best regards!

Quoted 9 lines

First, one overall question. We have dynamic path selectors available to deal with paths that are just simply slower that other paths, but can still be used together. Is there specific hardware or a specific setup where this isn't good enough and we really need to seperate these paths into different pathgroups, but we can't find out deterministically how the groups should be set up? It just seems like there could be a less hacky solution to this problem, but perhaps there are some situations where this is truly the best option. I'm just wondering what those are.

1. In the Storage-Backup environment of HyperCluster, includes one storage array near
to the host and one remote storage array, and the two storage arrays have the same hardware.
The same LUN is writed or readed by the two storage arrays.
However, usually, the average latency of the paths of the remote storage array is much higher than the
near storage array's.
apparently, the prioritizer can be a good automatic solution.
And the current selectors don't solve it, IOs will send to the paths of the remote storage array, IOPS will be influenced unavoidably.
2. In the environment of single storage array, the prioritizer can automatically separate the paths who's latency is much higher, IOs will not send to this paths.
But the current selectors don't solve this problem, IOPS will be influenced unavoidably.

Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+
+/* interval_unit_str and interval_unit_type keep the same assignment sequence */
+static const char interval_unit_str[][MAX_CHAR_SIZE] = {
+    CHAR_USEC, CHAR_MSEC, CHAR_SEC

This is a nit, but for constant strings, could you please use "char *var" instead of "char var[]", to be consistent with the rest of the multipath code.

Thanks, as the following patch.

Quoted 9 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+    if ((args == NULL) || (interval == NULL)
+        || (consnum == NULL) || (type == NULL))
+        return 0;
+
+    /* int type */
+    if ((size < 1) || (size > MAX_CHAR_SIZE-1))
+        return 0;

You should probably have log messages for these error returns.

Thanks, as the following patch.

Quoted 7 lines
Quoted 7 lines
(quoted diff) +7 -0
+
+    memcpy(source, args, size+1);
+    if (strstr(source, vertica) == NULL)
+        return 0;
+
+    *type = get_interval_type(source, typestr);
+    if (*type == INTERVAL_INVALID)
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+    {
+        condlog(0, "delay_interval type is invalid");
+        return 0;
+    }

I'm confused here. How do you get to use the default interval. Shouldn't you accept "20s|" and "|30" and as valid inputs that use the defaults for the part they don't specify.

OK,the default arguments value is removed. If get inputs failed, return default priority "0". As the following patch.

Quoted 22 lines
Quoted 22 lines
(quoted diff) +21 -0
+    tokenbefore = strtok(source, vertica);
+    tokenafter = strtok(NULL, vertica);
+    typestr[1] = '\0';
+    tokenbefore = strtok(tokenbefore, typestr);
+    if ((tokenbefore == NULL) || (tokenafter == NULL))
+        return 0;
+
+    tmp = tokenbefore;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+        {
+            condlog(0, "delay_interval string include invalid char");
+            return 0;
+        }
+
+    tmp = tokenafter;
+    while (*tmp != '\0')
+        if (!isdigit(*tmp++))
+        {
+            condlog(0, "cons_num string include invalid char");
+            return 0;
Quoted 8 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+        }
+
+    *interval = atoi(tokenbefore);

Why do you keep track of the type and the interval seperately? Can't you just find out the type, and use that to multiply the interval once you read it, and then just use that value, instead of keeping track of two values across multiple functions?

Thanks, as the following patch.

Quoted 10 lines

Review

Quoted 7 lines
(quoted diff) +7 -0
+
+	if (pp->fd < 0)
+	    return -PRIO_NO_INFORMATION;
+
+    if (get_delay_pref_arg(args, &delay_interval, &cons_num, &type) == 0)
+    {
+        condlog(3, "%s: get delay arg fail", pp->dev);

Why use the word "fail" in this message? Not setting prio_args to get the defaults seems like a perfectly valid choice.

The defaults are not used. Insteadly, return default priority "0". See below.

Quoted 7 lines
Quoted 7 lines
libmultipath/prioritizers/delayedpath.h new file +1 -0
new file mode 100644
index 0000000..d8213e9
@@ -0,0 +1,17 @@
+#ifndef _DELAYEDPATH_H
Quoted 8 lines

libmultipath/prioritizers/delayedpath.h line 4

Quoted 3 lines
libmultipath/prioritizers/delayedpath.h new file +3 -0
+#define _DELAYEDPATH_H
+
+#define PRIO_DELAYED_PATH "delayedpath"

In order for the rest of the code to refer to this prioritizer, this define should be in prio.h with the other prioritizer names, and as long as delayedpath.c includes prio.h, there's no need to put it in delayedpath.h.

OK, as the following patch.

Quoted 12 lines

libmultipath/prioritizers/delayedpath.h line 6

Quoted 3 lines
libmultipath/prioritizers/delayedpath.h new file +2 -0
+
+#define PRIO_NO_INFORMATION 5

The rest of the multipath code only cares if getprio returns a negative number of not. It doesn't check what the specific negative number is. I realize the the alua prioritizer returns a set of error codes, but they aren't used, or even usable in their present form. If we wanted to have better error reporting, we should set up a universal set of error codes in prio.h, and have all prioritizers use them, instead of having each prioritizer define its own error codes. There's no reason why your prioritizer needs to return this error code instead of -1.

OK, as the following patch.

Quoted 16 lines

libmultipath/prioritizers/delayedpath.h line 15

Quoted 10 lines
libmultipath/prioritizers/delayedpath.h new file +9 -0
+
+#define USEC_PER_SEC      1000000LL
+#define USEC_PER_MSEC     1000LL
+#define USEC_PER_USEC     1LL
+
+static inline long long timeval_to_us(const struct timespec *tv)
+{
+	return ((long long) tv->tv_sec * USEC_PER_SEC) + (tv->tv_nsec >> 10);
+}

No other file besides delayedpath.c will likely be including this .h file, so I don't see any purpose for these being defined here. In fact, I don't see why you can't just have a .c file without a .h file like the majority of prioritizers. I'm pretty sure that none of the prioritizers really need their own .h file.

OK, as the following patch.

Quoted 16 lines

multipath/multipath.conf.5 Generate a random priority between 1 and 10. · line 298

Quoted 13 lines
libmultipath/prioritizers/delayedpath.h new file +1 -0
+#endif
multipath/multipath.conf.5 +3 -0
index 5939688..f1e126e 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I delayedpath
+Generate the path priority based on a time-delay algorithm.
+Requires prio_args keyword.

Really it doesn't require prio_args if you want to use the default values, and should probably say so.

The default args is discarded, as the following patch.

Quoted 16 lines
Quoted 5 lines
multipath/multipath.conf.5 +5 -0
+.I delayed
+Needs a value of the form
+\fI"<delay_interval|cons_num>"\fR
+.RS
+.TP 8

multipath/multipath.conf.5 Generate a random priority between 1 and 10.

Quoted 10 lines
multipath/multipath.conf.5 +10 -0
+.I delay_interval
+The interval values of average IO-time-delay between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 1000), us [1, 1000),
+For example: 10s, or 100us, or 100ms. The default is: 10ms.
+.TP
+.I cons_num
+The number of read IOs sent to the current path continuously, used to calculate the average IO-time-delay. Valid Values: Integer, [3, 1000].
+For example: 30. The default is: 20.
+.RE
+.TP 12
Quoted 7 lines

Looking at the "weighted" prio_args definition just above your "delayed" definition, the pipe character "|" is being used to say that any of a set of options is allowed. Your definition has it being a literal character, but it's still inside the angle brackets that usually delineate a variable. perhaps "<delay_interval>|<io_num>" would be easier to understand, or even "[delayed_interval]|[io_num]" if you can omit these to use the defaults.

OK, as the following patch.
The up-to-date patch as follows:

---

Quoted 1 line

From 58d718fdd34550bd9c4a32c6e9a87099c1e45a9f Mon Sep 17 00:00:00 2001

From: Yang Feng <[email protected]>
Date: Fri, 19 May 2017 16:09:07 +0800
Subject: [PATCH] libmultipath/prioritizers: Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a latency algorithm. And the latency algorithm is dependent on the following arguments(latency_interval and io_num). The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path continuously, the IOs' average latency can be calculated.
2. According to the average latency of each path and the weight value "latency_interval", the priority "rc" of each path can be provided.

latency_interval latency_interval latency_interval latency_interval
|------------------|------------------|------------------|...|------------------| | priority rank 1 | priority rank 2 | priority rank 3 |...| priority rank x | |------------------|------------------|------------------|...|------------------|
Priority Rank Partitioning

---
 libmultipath/prioritizers/Makefile       |   6 +-
 libmultipath/prioritizers/path_latency.c | 271 +++++++++++++++++++++++++++++++
 multipath/multipath.conf.5               |  18 ++
 libmultipath/prio.h 			  |   1 +
 4 files changed, 295 insertions(+), 1 deletion(-)
 create mode 100644 libmultipath/prioritizers/path_latency.c
libmultipath/prioritizers/Makefile +5 -1
index 36b42e4..d2f20f6 100644
@@ -18,13 +18,17 @@ LIBS = \
 	libpriorandom.so \
 	libpriordac.so \
 	libprioweightedpath.so \
-	libpriosysfs.so
+	libpriopath_latency.so \
+	libpriosysfs.so
 
 all: $(LIBS)
 
 libprioalua.so: alua.o alua_rtpg.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
+libpriopath_latency.so: path_latency.o  ../checkers/libsg.o
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^ -lm
+
 libprio%.so: %.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
libmultipath/prioritizers/path_latency.c new file +271 -0

271 lines not on this page · raw message

multipath/multipath.conf.5 +18 -0
index 5939688..3dd0d77 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I path_latency
+Generate the path priority based on a latency algorithm.
+Requires prio_args keyword.
+.TP
 .I datacore
 .\" XXX
 ???. Requires prio_args keyword.
@@ -333,6 +337,20 @@ these values can be looked up through sysfs or by running \fImultipathd show pat
 "%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.*
 .RE
 .TP 12
+.I path_latency
+Needs a value of the form
+\fI"<latency_interval>|<io_num>"\fR
+.RS
+.TP 8
+.I latency_interval
+The interval values of average latency between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 60000], us [1, 60000000],
+For example: If latency_interval=10ms, the paths will be grouped in priority groups with path latency 0-10ms, 10-20ms, 20-30ms, etc..
+.TP
+.I io_num
+The number of read IOs sent to the current path continuously, used to calculate the average path latency. Valid Values: Integer, [10, 200].
+.RE
+.TP 12
 .I alua
 If \fIexclusive_pref_bit\fR is set, paths with the \fIpreferred path\fR bit
 set will always be in their own path group.
libmultipath/prio.h +1 -0
index 0193c52..c97fe39 100644
@@ -29,6 +29,7 @@ struct path;
 #define PRIO_RDAC		"rdac"
 #define PRIO_WEIGHTED_PATH	"weightedpath"
 #define PRIO_SYSFS		"sysfs"
+#define PRIO_PATH_LATENCY	"path_latency"
 
 /*
  * Value used to mark the fact prio was not defined
Signature · 4 lines
--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
author
#15 lore raw

Re: [PATCH] multipath-tools:Prioritizer based on a latency algorithm

Hello Christophe,

Our technology team is working on a open source contribution for multipath. We think that this patch is essential to isolate the high latency paths and avoid high fluctuation of io performance, particularly, for the Storage-Backup environment of HyperCluster.

Look forward your reply very much.
Respects and regards!

1. In the Storage-Backup environment of HyperCluster, includes one storage array near to the host and one remote storage array, and the two storage arrays have the same hardware. The same LUN is writed or readed by the two storage arrays.However, usually, the average latency of the paths of the remote storage array is much higher than the near storage array's. Apparently, the prioritizer can be a good automatic solution. And the current selectors don't solve it, IOs will send to the paths of the remote storage array, IOPS will be influenced unavoidably.
2. In the environment of single storage array, the prioritizer can automatically separate the paths who's latency is much higher, IOs will not send to this paths. But the current selectors don't solve this problem, io performance will be influenced unavoidably.

The up-to-date patch as follows:
---
From 58d718fdd34550bd9c4a32c6e9a87099c1e45a9f Mon Sep 17 00:00:00 2001
From: Yang Feng <[email protected]>
Date: Fri, 19 May 2017 16:09:07 +0800
Subject: [PATCH] libmultipath/prioritizers: Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a latency algorithm. And the latency algorithm is dependent on the following arguments(latency_interval and io_num). The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path continuously, the IOs' average latency can be calculated.
2. According to the average latency of each path and the weight value "latency_interval", the priority "rc" of each path can be provided.

latency_interval latency_interval latency_interval latency_interval
|------------------|------------------|------------------|...|------------------| | priority rank 1 | priority rank 2 | priority rank 3 |...| priority rank x | |------------------|------------------|------------------|...|------------------|
Priority Rank Partitioning

---
 libmultipath/prioritizers/Makefile       |   6 +-
 libmultipath/prioritizers/path_latency.c | 271 +++++++++++++++++++++++++++++++
 multipath/multipath.conf.5               |  18 ++
 libmultipath/prio.h 			  |   1 +
 4 files changed, 295 insertions(+), 1 deletion(-)
 create mode 100644 libmultipath/prioritizers/path_latency.c
libmultipath/prioritizers/Makefile +5 -1
index 36b42e4..d2f20f6 100644
@@ -18,13 +18,17 @@ LIBS = \
 	libpriorandom.so \
 	libpriordac.so \
 	libprioweightedpath.so \
-	libpriosysfs.so
+	libpriopath_latency.so \
+	libpriosysfs.so
 
 all: $(LIBS)
 
 libprioalua.so: alua.o alua_rtpg.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
+libpriopath_latency.so: path_latency.o  ../checkers/libsg.o
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^ -lm
+
 libprio%.so: %.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
libmultipath/prioritizers/path_latency.c new file +271 -0

271 lines not on this page · raw message

multipath/multipath.conf.5 +18 -0
index 5939688..3dd0d77 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I path_latency
+Generate the path priority based on a latency algorithm.
+Requires prio_args keyword.
+.TP
 .I datacore
 .\" XXX
 ???. Requires prio_args keyword.
@@ -333,6 +337,20 @@ these values can be looked up through sysfs or by running \fImultipathd show pat
 "%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.*
 .RE
 .TP 12
+.I path_latency
+Needs a value of the form
+\fI"<latency_interval>|<io_num>"\fR
+.RS
+.TP 8
+.I latency_interval
+The interval values of average latency between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 60000], us [1, 60000000],
+For example: If latency_interval=10ms, the paths will be grouped in priority groups with path latency 0-10ms, 10-20ms, 20-30ms, etc..
+.TP
+.I io_num
+The number of read IOs sent to the current path continuously, used to calculate the average path latency. Valid Values: Integer, [10, 200].
+.RE
+.TP 12
 .I alua
 If \fIexclusive_pref_bit\fR is set, paths with the \fIpreferred path\fR bit
 set will always be in their own path group.
libmultipath/prio.h +1 -0
index 0193c52..c97fe39 100644
@@ -29,6 +29,7 @@ struct path;
 #define PRIO_RDAC		"rdac"
 #define PRIO_WEIGHTED_PATH	"weightedpath"
 #define PRIO_SYSFS		"sysfs"
+#define PRIO_PATH_LATENCY	"path_latency"
 
 /*
  * Value used to mark the fact prio was not defined
#16 lore raw

Re: [PATCH] multipath-tools:Prioritizer based on a latency algorithm

Quoted 1 line · Yang Feng

Hello Christophe,

ACK

-Ben

Quoted 398 lines

Our technology team is working on a open source contribution for multipath. We think that this patch is essential to isolate the high latency paths and avoid high fluctuation of io performance, particularly, for the Storage-Backup environment of HyperCluster.

Look forward your reply very much.
Respects and regards!

1. In the Storage-Backup environment of HyperCluster, includes one storage array near to the host and one remote storage array, and the two storage arrays have the same hardware. The same LUN is writed or readed by the two storage arrays.However, usually, the average latency of the paths of the remote storage array is much higher than the near storage array's. Apparently, the prioritizer can be a good automatic solution. And the current selectors don't solve it, IOs will send to the paths of the remote storage array, IOPS will be influenced unavoidably.
2. In the environment of single storage array, the prioritizer can automatically separate the paths who's latency is much higher, IOs will not send to this paths. But the current selectors don't solve this problem, io performance will be influenced unavoidably.

The up-to-date patch as follows:
---

Quoted 1 line

From 58d718fdd34550bd9c4a32c6e9a87099c1e45a9f Mon Sep 17 00:00:00 2001

From: Yang Feng <[email protected]>
Date: Fri, 19 May 2017 16:09:07 +0800
Subject: [PATCH] libmultipath/prioritizers: Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a latency algorithm. And the latency algorithm is dependent on the following arguments(latency_interval and io_num). The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path continuously, the IOs' average latency can be calculated.
2. According to the average latency of each path and the weight value "latency_interval", the priority "rc" of each path can be provided.

latency_interval latency_interval latency_interval latency_interval
|------------------|------------------|------------------|...|------------------| | priority rank 1 | priority rank 2 | priority rank 3 |...| priority rank x | |------------------|------------------|------------------|...|------------------|
Priority Rank Partitioning

---
 libmultipath/prioritizers/Makefile       |   6 +-
 libmultipath/prioritizers/path_latency.c | 271 +++++++++++++++++++++++++++++++
 multipath/multipath.conf.5               |  18 ++
 libmultipath/prio.h 			  |   1 +
 4 files changed, 295 insertions(+), 1 deletion(-)
 create mode 100644 libmultipath/prioritizers/path_latency.c
libmultipath/prioritizers/Makefile +5 -1
index 36b42e4..d2f20f6 100644
@@ -18,13 +18,17 @@ LIBS = \
 	libpriorandom.so \
 	libpriordac.so \
 	libprioweightedpath.so \
-	libpriosysfs.so
+	libpriopath_latency.so \
+	libpriosysfs.so
 
 all: $(LIBS)
 
 libprioalua.so: alua.o alua_rtpg.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
+libpriopath_latency.so: path_latency.o  ../checkers/libsg.o
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^ -lm
+
 libprio%.so: %.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
libmultipath/prioritizers/path_latency.c new file +271 -0
new file mode 100644
index 0000000..a666b6c
@@ -0,0 +1,271 @@
+/*
+ * (C) Copyright HUAWEI Technology Corp. 2017, 2021   All Rights Reserved.
+ *
+ * main.c
+ *
+ * Prioritizer for device mapper multipath, where the corresponding priority
+ * values of specific paths are provided by a latency algorithm. And the
+ * latency algorithm is dependent on arguments.
+ *
+ * The principle of the algorithm as follows:
+ * 1. By sending a certain number "io_num" of read IOs to the current path
+ *    continuously, the IOs' average latency can be calculated.
+ * 2. According to the average latency of each path and the weight value
+ *    "latency_interval", the priority "rc" of each path can be provided.
+ *
+ * Author(s): Yang Feng <[email protected]>
+ *            Zou Ming <[email protected]>
+ *
+ * This file is released under the GPL.
+ */
+#include <stdio.h>
+#include <math.h>
+#include <ctype.h>
+#include <time.h>
+
+#include "debug.h"
+#include "prio.h"
+#include "structs.h"
+#include "../checkers/libsg.h"
+
+#define THRES_USEC_VALUE        120000000LL    /*unit: us, =120s*/
+
+#define MAX_IO_NUM              200
+#define MIN_IO_NUM              10
+
+#define MAX_LATENCY_INTERVAL    60            /*unit: s*/
+#define MIN_LATENCY_INTERVAL    1             /*unit: us, or ms, or s*/
+
+#define DEFAULT_PRIORITY        0
+
+#define MAX_CHAR_SIZE           30
+
+#define CHAR_USEC               "us"
+#define CHAR_MSEC               "ms"
+#define CHAR_SEC                "s"
+
+enum interval_type {
+    INTERVAL_USEC,
+    INTERVAL_MSEC,
+    INTERVAL_SEC,
+    INTERVAL_INVALID
+};
+
+/* interval_unit_str and interval_unit_type keep the same assignment sequence */
+static const char *interval_unit_str[MAX_CHAR_SIZE] = {
+    CHAR_USEC, CHAR_MSEC, CHAR_SEC
+};
+static const int interval_unit_type[] = {
+    INTERVAL_USEC, INTERVAL_MSEC, INTERVAL_SEC
+};
+
+#define USEC_PER_SEC      1000000LL
+#define USEC_PER_MSEC     1000LL
+#define USEC_PER_USEC     1LL
+
+static const int conversion_ratio[] = {
+    [INTERVAL_USEC]		= USEC_PER_USEC,
+    [INTERVAL_MSEC]     = USEC_PER_MSEC,
+    [INTERVAL_SEC]		= USEC_PER_SEC,
+    [INTERVAL_INVALID]	= 0
+};
+
+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 >> 10);
+}
+
+static int do_readsector0(int fd, unsigned int timeout)
+{
+	unsigned char buf[4096];
+	unsigned char sbuf[SENSE_BUFF_LEN];
+	int ret;
+
+	ret = sg_read(fd, &buf[0], 4096, &sbuf[0],
+		      SENSE_BUFF_LEN, timeout);
+
+	return ret;
+}
+
+int check_args_valid(int io_num, long long latency_interval, int type)
+{
+    if ((io_num < MIN_IO_NUM) || (io_num > MAX_IO_NUM))
+    {
+        condlog(0, "args io_num is more than the valid values range");
+        return 0;
+    }
+
+    /* s:[1, 60], ms:[1, 60000], us:[1, 60000000] */
+    if ((latency_interval < MIN_LATENCY_INTERVAL) || (latency_interval > (MAX_LATENCY_INTERVAL * USEC_PER_SEC / conversion_ratio[type])))
+    {
+        condlog(0, "args latency_interval is more than the valid values range");
+        return 0;
+    }
+
+    return 1;
+}
+
+static int get_interval_type(char *type)
+{
+    int index;
+
+    for (index = 0; index < sizeof(interval_unit_str)/MAX_CHAR_SIZE; index++)
+    {
+        if (strcmp(type, interval_unit_str[index]) == 0)
+        {
+            return interval_unit_type[index];
+        }
+    }
+
+    return INTERVAL_INVALID;
+}
+
+long long get_conversion_ratio(int type)
+{
+    return conversion_ratio[type];
+}
+
+/* In multipath.conf, args form: io_num|latency_interval. For example,
+*  args is "20|10ms", this function can get 20, 10.
+*/
+static int get_interval_and_ionum(char *args,
+                                        int *ionum,
+                                        long long *interval)
+{
+    char source[MAX_CHAR_SIZE];
+    char vertica = '|';
+    char *endstrbefore = NULL;
+    char *endstrafter = NULL;
+    int type;
+    unsigned int size = strlen(args);
+    long long ratio;
+
+    if ((args == NULL) || (ionum == NULL) || (interval == NULL))
+    {
+        condlog(0, "args string is NULL");
+        return 0;
+    }
+
+    if ((size < 1) || (size > MAX_CHAR_SIZE-1))
+    {
+        condlog(0, "args string's size is too long");
+        return 0;
+    }
+
+    memcpy(source, args, size+1);
+
+    if (!isdigit(source[0]))
+    {
+        condlog(0, "args io_num string's first char is not digit");
+        return 0;
+    }
+
+    *ionum = (int)strtoul(source, &endstrbefore, 10);
+    if (endstrbefore[0] != vertica)
+    {
+        condlog(0, "segmentation char is invalid");
+        return 0;
+    }
+
+    if (!isdigit(endstrbefore[1]))
+    {
+        condlog(0, "args latency_interval string's first char is not digit");
+        return 0;
+    }
+
+    *interval = (long long)strtol(&endstrbefore[1], &endstrafter, 10);
+    type = get_interval_type(endstrafter);
+    if (type == INTERVAL_INVALID)
+    {
+        condlog(0, "args latency_interval type is invalid");
+        return 0;
+    }
+
+    if (check_args_valid(*ionum, *interval, type) == 0)
+    {
+        return 0;
+    }
+
+	ratio = get_conversion_ratio(type);
+    *interval *= (long long)ratio;
+
+    return 1;
+}
+
+long long calc_standard_deviation(long long *path_latency, int size, long long avglatency)
+{
+    int index;
+    long long total = 0;
+
+    for (index = 0; index < size; index++)
+    {
+        total += (path_latency[index] - avglatency) * (path_latency[index] - avglatency);
+    }
+
+    total /= (size-1);
+
+    return (long long)sqrt((double)total);
+}
+
+int getprio (struct path *pp, char *args, unsigned int timeout)
+{
+    int rc, temp;
+    int index = 0;
+    int io_num;
+    long long latency_interval;
+    long long avglatency;
+    long long standard_deviation;
+    long long toldelay = 0;
+    long long before, after;
+    struct timespec tv;
+
+	if (pp->fd < 0)
+		return -1;
+
+    if (get_interval_and_ionum(args, &io_num, &latency_interval) == 0)
+    {
+        condlog(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)
+        {
+            condlog(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;
+    condlog(4, "%s: average latency is (%lld)", pp->dev, avglatency);
+
+    if (avglatency > THRES_USEC_VALUE)
+    {
+        condlog(0, "%s: average latency (%lld) is more than thresold", pp->dev, avglatency);
+        return DEFAULT_PRIORITY;
+    }
+
+    /* warn the user if the latency_interval set is smaller than (2 * standard deviation), or equal */
+    standard_deviation = calc_standard_deviation(path_latency, index, avglatency);
+    if (latency_interval <= (2 * standard_deviation))
+        condlog(3, "%s: args latency_interval set is smaller than 2 * standard deviation (%lld us), or equal",
+            pp->dev, standard_deviation);
+
+	rc = (int)(THRES_USEC_VALUE - (avglatency/(long long)latency_interval));
+    return rc;
+}
multipath/multipath.conf.5 +18 -0
index 5939688..3dd0d77 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I path_latency
+Generate the path priority based on a latency algorithm.
+Requires prio_args keyword.
+.TP
 .I datacore
 .\" XXX
 ???. Requires prio_args keyword.
@@ -333,6 +337,20 @@ these values can be looked up through sysfs or by running \fImultipathd show pat
 "%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.*
 .RE
 .TP 12
+.I path_latency
+Needs a value of the form
+\fI"<latency_interval>|<io_num>"\fR
+.RS
+.TP 8
+.I latency_interval
+The interval values of average latency between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 60000], us [1, 60000000],
+For example: If latency_interval=10ms, the paths will be grouped in priority groups with path latency 0-10ms, 10-20ms, 20-30ms, etc..
+.TP
+.I io_num
+The number of read IOs sent to the current path continuously, used to calculate the average path latency. Valid Values: Integer, [10, 200].
+.RE
+.TP 12
 .I alua
 If \fIexclusive_pref_bit\fR is set, paths with the \fIpreferred path\fR bit
 set will always be in their own path group.
libmultipath/prio.h +1 -0
index 0193c52..c97fe39 100644
@@ -29,6 +29,7 @@ struct path;
 #define PRIO_RDAC		"rdac"
 #define PRIO_WEIGHTED_PATH	"weightedpath"
 #define PRIO_SYSFS		"sysfs"
+#define PRIO_PATH_LATENCY	"path_latency"
 
 /*
  * Value used to mark the fact prio was not defined
Signature · 4 lines
--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
author
#18 lore raw

PATCH v2 1/1 multipath-tools: Prioritizer based on a latency algorithm

In reply to Yang Feng

1. In the Storage-Backup environment of HyperCluster, includes one storage array near to the host and one remote storage array, and the two storage arrays have the same hardware. The same LUN is writed or readed by the two storage arrays. However, usually, the average latency of the paths of the remote storage array is much higher than the near storage array's. Apparently, the prioritizer can be a good automatic solution. And the current selectors don't solve it, IOs will send to the paths of the remote storage array, IOPS will be influenced unavoidably.

2. In the environment of single storage array, the prioritizer can automatically separate the paths who's latency is much higher, IOs will not send to this paths. But the current selectors don't solve this problem, IOPS will be influenced unavoidably.

The value of "MIN_IO_NUM" is set 2 from 10, the patch v2 as follows:
---
Subject: [PATCH] libmultipath/prioritizers: Prioritizer for device mapper multipath, where the corresponding priority values of specific paths are provided by a latency algorithm. And the latency algorithm is dependent on the following arguments(latency_interval and io_num). The principle of the algorithm is illustrated as follows:
1. By sending a certain number "cons_num" of read IOs to the current path continuously, the IOs' average latency can be calculated.
2. According to the average latency of each path and the weight value "latency_interval", the priority "rc" of each path can be provided.

latency_interval latency_interval latency_interval latency_interval
|------------------|------------------|------------------|...|------------------| | priority rank 1 | priority rank 2 | priority rank 3 |...| priority rank x | |------------------|------------------|------------------|...|------------------|
Priority Rank Partitioning

---
 libmultipath/prioritizers/Makefile       |   6 +-
 libmultipath/prioritizers/path_latency.c | 271 +++++++++++++++++++++++++++++++
 multipath/multipath.conf.5               |  18 ++
 libmultipath/prio.h 			  |   1 +
 4 files changed, 295 insertions(+), 1 deletion(-)
 create mode 100644 libmultipath/prioritizers/path_latency.c
libmultipath/prioritizers/Makefile +5 -1
index 36b42e4..d2f20f6 100644
@@ -18,13 +18,17 @@ LIBS = \
 	libpriorandom.so \
 	libpriordac.so \
 	libprioweightedpath.so \
-	libpriosysfs.so
+	libpriopath_latency.so \
+	libpriosysfs.so
 
 all: $(LIBS)
 
 libprioalua.so: alua.o alua_rtpg.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
+libpriopath_latency.so: path_latency.o  ../checkers/libsg.o
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^ -lm
+
 libprio%.so: %.o
 	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -o $@ $^
 
libmultipath/prioritizers/path_latency.c new file +271 -0

271 lines not on this page · raw message

multipath/multipath.conf.5 +18 -0
index 5939688..3dd0d77 100644
@@ -293,6 +293,10 @@ Generate a random priority between 1 and 10.
 Generate the path priority based on the regular expression and the
 priority provided as argument. Requires prio_args keyword.
 .TP
+.I path_latency
+Generate the path priority based on a latency algorithm.
+Requires prio_args keyword.
+.TP
 .I datacore
 .\" XXX
 ???. Requires prio_args keyword.
@@ -333,6 +337,20 @@ these values can be looked up through sysfs or by running \fImultipathd show pat
 "%N:%R:%n:%r"\fR. For example: 0x200100e08ba0aea0:0x210100e08ba0aea0:.*:.* , .*:.*:iqn.2009-10.com.redhat.msp.lab.ask-06:.*
 .RE
 .TP 12
+.I path_latency
+Needs a value of the form
+\fI"<latency_interval>|<io_num>"\fR
+.RS
+.TP 8
+.I latency_interval
+The interval values of average latency between two different neighbour ranks of path priority, used to partition different priority ranks.
+Form: XXs, or XXXus, or XXXms. Unit: Second, or Microsecond, or Millisecond. Valid Values: Integer, s [1, 60], ms [1, 60000], us [1, 60000000],
+For example: If latency_interval=10ms, the paths will be grouped in priority groups with path latency 0-10ms, 10-20ms, 20-30ms, etc..
+.TP
+.I io_num
+The number of read IOs sent to the current path continuously, used to calculate the average path latency. Valid Values: Integer, [2, 200].
+.RE
+.TP 12
 .I alua
 If \fIexclusive_pref_bit\fR is set, paths with the \fIpreferred path\fR bit
 set will always be in their own path group.
libmultipath/prio.h +1 -0
index 0193c52..c97fe39 100644
@@ -29,6 +29,7 @@ struct path;
 #define PRIO_RDAC		"rdac"
 #define PRIO_WEIGHTED_PATH	"weightedpath"
 #define PRIO_SYSFS		"sysfs"
+#define PRIO_PATH_LATENCY	"path_latency"
 
 /*
  * Value used to mark the fact prio was not defined