libaio,io_uring: move common cmdprio_prep() code to cmdprio
[fio.git] / engines / cmdprio.c
1 /*
2  * IO priority handling helper functions common to the libaio and io_uring
3  * engines.
4  */
5
6 #include "cmdprio.h"
7
8 static int fio_cmdprio_bssplit_ddir(struct thread_options *to, void *cb_arg,
9                                     enum fio_ddir ddir, char *str, bool data)
10 {
11         struct cmdprio *cmdprio = cb_arg;
12         struct split split;
13         unsigned int i;
14
15         if (ddir == DDIR_TRIM)
16                 return 0;
17
18         memset(&split, 0, sizeof(split));
19
20         if (split_parse_ddir(to, &split, str, data, BSSPLIT_MAX))
21                 return 1;
22         if (!split.nr)
23                 return 0;
24
25         cmdprio->bssplit_nr[ddir] = split.nr;
26         cmdprio->bssplit[ddir] = malloc(split.nr * sizeof(struct bssplit));
27         if (!cmdprio->bssplit[ddir])
28                 return 1;
29
30         for (i = 0; i < split.nr; i++) {
31                 cmdprio->bssplit[ddir][i].bs = split.val1[i];
32                 if (split.val2[i] == -1U) {
33                         cmdprio->bssplit[ddir][i].perc = 0;
34                 } else {
35                         if (split.val2[i] > 100)
36                                 cmdprio->bssplit[ddir][i].perc = 100;
37                         else
38                                 cmdprio->bssplit[ddir][i].perc = split.val2[i];
39                 }
40         }
41
42         return 0;
43 }
44
45 int fio_cmdprio_bssplit_parse(struct thread_data *td, const char *input,
46                               struct cmdprio *cmdprio)
47 {
48         char *str, *p;
49         int i, ret = 0;
50
51         p = str = strdup(input);
52
53         strip_blank_front(&str);
54         strip_blank_end(str);
55
56         ret = str_split_parse(td, str, fio_cmdprio_bssplit_ddir, cmdprio, false);
57
58         if (parse_dryrun()) {
59                 for (i = 0; i < CMDPRIO_RWDIR_CNT; i++) {
60                         free(cmdprio->bssplit[i]);
61                         cmdprio->bssplit[i] = NULL;
62                         cmdprio->bssplit_nr[i] = 0;
63                 }
64         }
65
66         free(p);
67         return ret;
68 }
69
70 static int fio_cmdprio_percentage(struct cmdprio *cmdprio, struct io_u *io_u)
71 {
72         enum fio_ddir ddir = io_u->ddir;
73         unsigned int p = cmdprio->percentage[ddir];
74         int i;
75
76         /*
77          * If cmdprio_percentage option was specified, then use that
78          * percentage. Otherwise, use cmdprio_bssplit percentages depending
79          * on the IO size.
80          */
81         if (p)
82                 return p;
83
84         for (i = 0; i < cmdprio->bssplit_nr[ddir]; i++) {
85                 if (cmdprio->bssplit[ddir][i].bs == io_u->buflen)
86                         return cmdprio->bssplit[ddir][i].perc;
87         }
88
89         return 0;
90 }
91
92 /**
93  * fio_cmdprio_set_ioprio - Set an io_u ioprio according to cmdprio options
94  *
95  * Generates a random percentage value to determine if an io_u ioprio needs
96  * to be set. If the random percentage value is within the user specified
97  * percentage of I/Os that should use a cmdprio priority value (rather than
98  * the default priority), then this function updates the io_u with an ioprio
99  * value as defined by the cmdprio/cmdprio_class or cmdprio_bssplit options.
100  *
101  * Return true if the io_u ioprio was changed and false otherwise.
102  */
103 bool fio_cmdprio_set_ioprio(struct thread_data *td, struct cmdprio *cmdprio,
104                             struct io_u *io_u)
105 {
106         enum fio_ddir ddir = io_u->ddir;
107         unsigned int p = fio_cmdprio_percentage(cmdprio, io_u);
108         unsigned int cmdprio_value =
109                 ioprio_value(cmdprio->class[ddir], cmdprio->level[ddir]);
110
111         if (p && rand_between(&td->prio_state, 0, 99) < p) {
112                 io_u->ioprio = cmdprio_value;
113                 if (!td->ioprio || cmdprio_value < td->ioprio) {
114                         /*
115                          * The async IO priority is higher (has a lower value)
116                          * than the default priority (which is either 0 or the
117                          * value set by "prio" and "prioclass" options).
118                          */
119                         io_u->flags |= IO_U_F_HIGH_PRIO;
120                 }
121                 return true;
122         }
123
124         if (td->ioprio && td->ioprio < cmdprio_value) {
125                 /*
126                  * The IO will be executed with the default priority (which is
127                  * either 0 or the value set by "prio" and "prioclass options),
128                  * and this priority is higher (has a lower value) than the
129                  * async IO priority.
130                  */
131                 io_u->flags |= IO_U_F_HIGH_PRIO;
132         }
133
134         return false;
135 }
136
137 int fio_cmdprio_init(struct thread_data *td, struct cmdprio *cmdprio,
138                      bool *has_cmdprio)
139 {
140         struct thread_options *to = &td->o;
141         bool has_cmdprio_percentage = false;
142         bool has_cmdprio_bssplit = false;
143         int i;
144
145         /*
146          * If cmdprio_percentage/cmdprio_bssplit is set and cmdprio_class
147          * is not set, default to RT priority class.
148          */
149         for (i = 0; i < CMDPRIO_RWDIR_CNT; i++) {
150                 if (cmdprio->percentage[i]) {
151                         if (!cmdprio->class[i])
152                                 cmdprio->class[i] = IOPRIO_CLASS_RT;
153                         has_cmdprio_percentage = true;
154                 }
155                 if (cmdprio->bssplit_nr[i]) {
156                         if (!cmdprio->class[i])
157                                 cmdprio->class[i] = IOPRIO_CLASS_RT;
158                         has_cmdprio_bssplit = true;
159                 }
160         }
161
162         /*
163          * Check for option conflicts
164          */
165         if (has_cmdprio_percentage && has_cmdprio_bssplit) {
166                 log_err("%s: cmdprio_percentage and cmdprio_bssplit options "
167                         "are mutually exclusive\n",
168                         to->name);
169                 return 1;
170         }
171
172         *has_cmdprio = has_cmdprio_percentage || has_cmdprio_bssplit;
173
174         return 0;
175 }