Whamcloud - gitweb
LU-10391 ptlrpc: change rq_peer to struct lnet_nid
[fs/lustre-release.git] / lustre / ptlrpc / nrs_delay.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * version 2 along with this program; If not, see
17  * http://www.gnu.org/licenses/gpl-2.0.html
18  *
19  * GPL HEADER END
20  */
21 /*
22  * Copyright (c) 2017, Cray Inc. All Rights Reserved.
23  *
24  * Copyright (c) 2017, Intel Corporation.
25  */
26 /*
27  * lustre/ptlrpc/nrs_delay.c
28  *
29  * Network Request Scheduler (NRS) Delay policy
30  *
31  * This policy will delay request handling for some configurable amount of
32  * time.
33  *
34  * Author: Chris Horn <hornc@cray.com>
35  */
36 /**
37  * \addtogoup nrs
38  * @{
39  */
40
41 #define DEBUG_SUBSYSTEM S_RPC
42
43 #include <linux/random.h>
44
45 #include <obd_support.h>
46 #include <obd_class.h>
47 #include "ptlrpc_internal.h"
48
49 /**
50  * \name delay
51  *
52  * The delay policy schedules RPCs so that they are only processed after some
53  * configurable amount of time (in seconds) has passed.
54  *
55  * The defaults were chosen arbitrarily.
56  *
57  * @{
58  */
59
60 #define NRS_POL_NAME_DELAY      "delay"
61
62 /* Default minimum delay in seconds. */
63 #define NRS_DELAY_MIN_DEFAULT   5
64 /* Default maximum delay, in seconds. */
65 #define NRS_DELAY_MAX_DEFAULT   300
66 /* Default percentage of delayed RPCs. */
67 #define NRS_DELAY_PCT_DEFAULT   100
68
69 /**
70  * Binary heap predicate.
71  *
72  * Elements are sorted according to the start time assigned to the requests
73  * upon enqueue. An element with an earlier start time is "less than" an
74  * element with a later start time.
75  *
76  * \retval 0 start_time(e1) > start_time(e2)
77  * \retval 1 start_time(e1) <= start_time(e2)
78  */
79 static int delay_req_compare(struct binheap_node *e1,
80                              struct binheap_node *e2)
81 {
82         struct ptlrpc_nrs_request *nrq1;
83         struct ptlrpc_nrs_request *nrq2;
84
85         nrq1 = container_of(e1, struct ptlrpc_nrs_request, nr_node);
86         nrq2 = container_of(e2, struct ptlrpc_nrs_request, nr_node);
87
88         return nrq1->nr_u.delay.req_start_time <=
89                nrq2->nr_u.delay.req_start_time;
90 }
91
92 static struct binheap_ops nrs_delay_heap_ops = {
93         .hop_enter      = NULL,
94         .hop_exit       = NULL,
95         .hop_compare    = delay_req_compare,
96 };
97
98 /**
99  * Is called before the policy transitions into
100  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STARTED; allocates and initializes
101  * the delay-specific private data structure.
102  *
103  * \param[in] policy The policy to start
104  * \param[in] Generic char buffer; unused in this policy
105  *
106  * \retval -ENOMEM OOM error
107  * \retval  0      success
108  *
109  * \see nrs_policy_register()
110  * \see nrs_policy_ctl()
111  */
112 static int nrs_delay_start(struct ptlrpc_nrs_policy *policy, char *arg)
113 {
114         struct nrs_delay_data *delay_data;
115
116         ENTRY;
117
118         OBD_CPT_ALLOC_PTR(delay_data, nrs_pol2cptab(policy),
119                           nrs_pol2cptid(policy));
120         if (delay_data == NULL)
121                 RETURN(-ENOMEM);
122
123         delay_data->delay_binheap = binheap_create(&nrs_delay_heap_ops,
124                                                        CBH_FLAG_ATOMIC_GROW,
125                                                        4096, NULL,
126                                                        nrs_pol2cptab(policy),
127                                                        nrs_pol2cptid(policy));
128
129         if (delay_data->delay_binheap == NULL) {
130                 OBD_FREE_PTR(delay_data);
131                 RETURN(-ENOMEM);
132         }
133
134         delay_data->min_delay = NRS_DELAY_MIN_DEFAULT;
135         delay_data->max_delay = NRS_DELAY_MAX_DEFAULT;
136         delay_data->delay_pct = NRS_DELAY_PCT_DEFAULT;
137
138         policy->pol_private = delay_data;
139
140         RETURN(0);
141 }
142
143 /**
144  * Is called before the policy transitions into
145  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED; deallocates the delay-specific
146  * private data structure.
147  *
148  * \param[in] policy The policy to stop
149  *
150  * \see nrs_policy_stop0()
151  */
152 static void nrs_delay_stop(struct ptlrpc_nrs_policy *policy)
153 {
154         struct nrs_delay_data *delay_data = policy->pol_private;
155
156         LASSERT(delay_data != NULL);
157         LASSERT(delay_data->delay_binheap != NULL);
158         LASSERT(binheap_is_empty(delay_data->delay_binheap));
159
160         binheap_destroy(delay_data->delay_binheap);
161
162         OBD_FREE_PTR(delay_data);
163 }
164
165 /**
166  * Is called for obtaining a delay policy resource.
167  *
168  * \param[in]  policy     The policy on which the request is being asked for
169  * \param[in]  nrq        The request for which resources are being taken
170  * \param[in]  parent     Parent resource, unused in this policy
171  * \param[out] resp       Resources references are placed in this array
172  * \param[in]  moving_req Signifies limited caller context; unused in this
173  *                        policy
174  *
175  * \retval 1 The delay policy only has a one-level resource hierarchy
176  *
177  * \see nrs_resource_get_safe()
178  */
179 static int nrs_delay_res_get(struct ptlrpc_nrs_policy *policy,
180                              struct ptlrpc_nrs_request *nrq,
181                              const struct ptlrpc_nrs_resource *parent,
182                              struct ptlrpc_nrs_resource **resp, bool moving_req)
183 {
184         /**
185          * Just return the resource embedded inside nrs_delay_data, and end this
186          * resource hierarchy reference request.
187          */
188         *resp = &((struct nrs_delay_data *)policy->pol_private)->delay_res;
189         return 1;
190 }
191
192 /**
193  * Called when getting a request from the delay policy for handling, or just
194  * peeking; removes the request from the policy when it is to be handled.
195  * Requests are only removed from this policy when their start time has
196  * passed.
197  *
198  * \param[in] policy The policy
199  * \param[in] peek   When set, signifies that we just want to examine the
200  *                   request, and not handle it, so the request is not removed
201  *                   from the policy.
202  * \param[in] force  Force the policy to return a request
203  *
204  * \retval The request to be handled
205  * \retval NULL no request available
206  *
207  * \see ptlrpc_nrs_req_get_nolock()
208  * \see nrs_request_get()
209  */
210 static
211 struct ptlrpc_nrs_request *nrs_delay_req_get(struct ptlrpc_nrs_policy *policy,
212                                              bool peek, bool force)
213 {
214         struct nrs_delay_data *delay_data = policy->pol_private;
215         struct binheap_node *node;
216         struct ptlrpc_nrs_request *nrq;
217
218         node = binheap_root(delay_data->delay_binheap);
219         nrq = unlikely(node == NULL) ? NULL :
220               container_of(node, struct ptlrpc_nrs_request, nr_node);
221
222         if (likely(nrq != NULL)) {
223                 if (!force &&
224                     ktime_get_real_seconds() < nrq->nr_u.delay.req_start_time)
225                         nrq = NULL;
226                 else if (likely(!peek))
227                         binheap_remove(delay_data->delay_binheap,
228                                            &nrq->nr_node);
229         }
230
231         return nrq;
232 }
233
234 /**
235  * Adds request \a nrq to a delay \a policy instance's set of queued requests
236  *
237  * A percentage (delay_pct) of incoming requests are delayed by this policy.
238  * If selected for delay a request start time is calculated. A start time
239  * is the current time plus a random offset in the range [min_delay, max_delay]
240  * The start time is recorded in the request, and is then used by
241  * delay_req_compare() to maintain a set of requests ordered by their start
242  * times.
243  *
244  * \param[in] policy The policy
245  * \param[in] nrq    The request to add
246  *
247  * \retval 0 request added
248  * \retval 1 request not added
249  *
250  */
251 static int nrs_delay_req_add(struct ptlrpc_nrs_policy *policy,
252                              struct ptlrpc_nrs_request *nrq)
253 {
254         struct nrs_delay_data *delay_data = policy->pol_private;
255
256         if (delay_data->delay_pct == 0 || /* Not delaying anything */
257             (delay_data->delay_pct != 100 &&
258              delay_data->delay_pct < prandom_u32_max(100)))
259                 return 1;
260
261         nrq->nr_u.delay.req_start_time = ktime_get_real_seconds() +
262                                          prandom_u32_max(delay_data->max_delay - delay_data->min_delay + 1) +
263                                          delay_data->min_delay;
264
265         return binheap_insert(delay_data->delay_binheap, &nrq->nr_node);
266 }
267
268 /**
269  * Removes request \a nrq from \a policy's list of queued requests.
270  *
271  * \param[in] policy The policy
272  * \param[in] nrq    The request to remove
273  */
274 static void nrs_delay_req_del(struct ptlrpc_nrs_policy *policy,
275                               struct ptlrpc_nrs_request *nrq)
276 {
277         struct nrs_delay_data *delay_data = policy->pol_private;
278
279         binheap_remove(delay_data->delay_binheap, &nrq->nr_node);
280 }
281
282 /**
283  * Prints a debug statement right before the request \a nrq stops being
284  * handled.
285  *
286  * \param[in] policy The policy handling the request
287  * \param[in] nrq    The request being handled
288  *
289  * \see ptlrpc_server_finish_request()
290  * \see ptlrpc_nrs_req_stop_nolock()
291  */
292 static void nrs_delay_req_stop(struct ptlrpc_nrs_policy *policy,
293                                struct ptlrpc_nrs_request *nrq)
294 {
295         struct ptlrpc_request *req = container_of(nrq, struct ptlrpc_request,
296                                                   rq_nrq);
297
298         DEBUG_REQ(D_RPCTRACE, req,
299                   "NRS: finished delayed request from %s after %llds",
300                   libcfs_idstr(&req->rq_peer),
301                   (s64)(nrq->nr_u.delay.req_start_time -
302                         req->rq_srv.sr_arrival_time.tv_sec));
303 }
304
305 /**
306  * Performs ctl functions specific to delay policy instances; similar to ioctl
307  *
308  * \param[in]     policy the policy instance
309  * \param[in]     opc    the opcode
310  * \param[in,out] arg    used for passing parameters and information
311  *
312  * \pre assert_spin_locked(&policy->pol_nrs->->nrs_lock)
313  * \post assert_spin_locked(&policy->pol_nrs->->nrs_lock)
314  *
315  * \retval 0   operation carried out successfully
316  * \retval -ve error
317  */
318 static int nrs_delay_ctl(struct ptlrpc_nrs_policy *policy,
319                          enum ptlrpc_nrs_ctl opc, void *arg)
320 {
321         struct nrs_delay_data *delay_data = policy->pol_private;
322         __u32 *val = (__u32 *)arg;
323
324         assert_spin_locked(&policy->pol_nrs->nrs_lock);
325
326         switch ((enum nrs_ctl_delay)opc) {
327         default:
328                 RETURN(-EINVAL);
329
330         case NRS_CTL_DELAY_RD_MIN:
331                 *val = delay_data->min_delay;
332                 break;
333
334         case NRS_CTL_DELAY_WR_MIN:
335                 if (*val > delay_data->max_delay)
336                         RETURN(-EINVAL);
337
338                 delay_data->min_delay = *val;
339                 break;
340
341         case NRS_CTL_DELAY_RD_MAX:
342                 *val = delay_data->max_delay;
343                 break;
344
345         case NRS_CTL_DELAY_WR_MAX:
346                 if (*val < delay_data->min_delay)
347                         RETURN(-EINVAL);
348
349                 delay_data->max_delay = *val;
350                 break;
351
352         case NRS_CTL_DELAY_RD_PCT:
353                 *val = delay_data->delay_pct;
354                 break;
355
356         case NRS_CTL_DELAY_WR_PCT:
357                 if (*val < 0 || *val > 100)
358                         RETURN(-EINVAL);
359
360                 delay_data->delay_pct = *val;
361                 break;
362         }
363         RETURN(0);
364 }
365
366 /**
367  * debugfs interface
368  */
369
370 /* nrs_delay_min and nrs_delay_max are bounded by these values */
371 #define LPROCFS_NRS_DELAY_LOWER_BOUND           0
372 #define LPROCFS_NRS_DELAY_UPPER_BOUND           65535
373
374 #define LPROCFS_NRS_DELAY_MIN_NAME              "delay_min:"
375 #define LPROCFS_NRS_DELAY_MIN_NAME_REG          "reg_delay_min:"
376 #define LPROCFS_NRS_DELAY_MIN_NAME_HP           "hp_delay_min:"
377
378 /**
379  * Max size of the nrs_delay_min seq_write buffer. Needs to be large enough
380  * to hold the string: "reg_min_delay:65535 hp_min_delay:65535"
381  */
382 #define LPROCFS_NRS_DELAY_MIN_SIZE                                             \
383         sizeof(LPROCFS_NRS_DELAY_MIN_NAME_REG                                  \
384                __stringify(LPROCFS_NRS_DELAY_UPPER_BOUND)                      \
385                " " LPROCFS_NRS_DELAY_MIN_NAME_HP                               \
386                __stringify(LPROCFS_NRS_DELAY_UPPER_BOUND))
387
388 #define LPROCFS_NRS_DELAY_MAX_NAME              "delay_max:"
389 #define LPROCFS_NRS_DELAY_MAX_NAME_REG          "reg_delay_max:"
390 #define LPROCFS_NRS_DELAY_MAX_NAME_HP           "hp_delay_max:"
391
392 /**
393  * Similar to LPROCFS_NRS_DELAY_MIN_SIZE above, but for the nrs_delay_max
394  * variable.
395  */
396 #define LPROCFS_NRS_DELAY_MAX_SIZE                                             \
397         sizeof(LPROCFS_NRS_DELAY_MAX_NAME_REG                                  \
398                __stringify(LPROCFS_NRS_DELAY_UPPER_BOUND)                      \
399                " " LPROCFS_NRS_DELAY_MAX_NAME_HP                               \
400                __stringify(LPROCFS_NRS_DELAY_UPPER_BOUND))
401
402 #define LPROCFS_NRS_DELAY_PCT_MIN_VAL           0
403 #define LPROCFS_NRS_DELAY_PCT_MAX_VAL           100
404 #define LPROCFS_NRS_DELAY_PCT_NAME              "delay_pct:"
405 #define LPROCFS_NRS_DELAY_PCT_NAME_REG          "reg_delay_pct:"
406 #define LPROCFS_NRS_DELAY_PCT_NAME_HP           "hp_delay_pct:"
407
408 /**
409  * Similar to LPROCFS_NRS_DELAY_MIN_SIZE above, but for the nrs_delay_pct
410  * variable.
411  */
412 #define LPROCFS_NRS_DELAY_PCT_SIZE                                             \
413         sizeof(LPROCFS_NRS_DELAY_PCT_NAME_REG                                  \
414                __stringify(LPROCFS_NRS_DELAY_PCT_MAX_VAL)                      \
415                " " LPROCFS_NRS_DELAY_PCT_NAME_HP                               \
416                __stringify(LPROCFS_NRS_DELAY_PCT_MAX_VAL))
417
418 /**
419  * Helper for delay's seq_write functions.
420  */
421 static ssize_t
422 lprocfs_nrs_delay_seq_write_common(const char __user *buffer,
423                                    unsigned int bufsize, size_t count,
424                                    const char *var_name, unsigned int min_val,
425                                    unsigned int max_val,
426                                    struct ptlrpc_service *svc, char *pol_name,
427                                    enum ptlrpc_nrs_ctl opc, bool single)
428 {
429         enum ptlrpc_nrs_queue_type queue = 0;
430         char *kernbuf;
431         char *val_str;
432         long unsigned int val_reg;
433         long unsigned int val_hp;
434         size_t count_copy;
435         int rc = 0;
436         char *tmp = NULL;
437         int tmpsize = 0;
438
439         if (count > bufsize - 1)
440                 return -EINVAL;
441
442         OBD_ALLOC(kernbuf, bufsize);
443         if (kernbuf == NULL)
444                 return -ENOMEM;
445
446         if (copy_from_user(kernbuf, buffer, count))
447                 GOTO(free_kernbuf, rc = -EFAULT);
448
449         tmpsize = strlen("reg_") + strlen(var_name) + 1;
450         OBD_ALLOC(tmp, tmpsize);
451         if (tmp == NULL)
452                 GOTO(free_tmp, rc = -ENOMEM);
453
454         /* look for "reg_<var_name>" in kernbuf */
455         snprintf(tmp, tmpsize, "reg_%s", var_name);
456         count_copy = count;
457         val_str = lprocfs_find_named_value(kernbuf, tmp, &count_copy);
458         if (val_str != kernbuf) {
459                 rc = kstrtoul(val_str, 10, &val_reg);
460                 if (rc != 0)
461                         GOTO(free_tmp, rc = -EINVAL);
462                 queue |= PTLRPC_NRS_QUEUE_REG;
463         }
464
465         /* look for "hp_<var_name>" in kernbuf */
466         snprintf(tmp, tmpsize, "hp_%s", var_name);
467         count_copy = count;
468         val_str = lprocfs_find_named_value(kernbuf, tmp, &count_copy);
469         if (val_str != kernbuf) {
470                 if (!nrs_svc_has_hp(svc))
471                         GOTO(free_tmp, rc = -ENODEV);
472
473                 rc = kstrtoul(val_str, 10, &val_hp);
474                 if (rc != 0)
475                         GOTO(free_tmp, rc = -EINVAL);
476                 queue |= PTLRPC_NRS_QUEUE_HP;
477         }
478
479         if (queue == 0) {
480                 if (!isdigit(kernbuf[0]))
481                         GOTO(free_tmp, rc = -EINVAL);
482
483                 rc = kstrtoul(kernbuf, 10, &val_reg);
484                 if (rc != 0)
485                         GOTO(free_tmp, rc = -EINVAL);
486
487                 queue = PTLRPC_NRS_QUEUE_REG;
488
489                 if (nrs_svc_has_hp(svc)) {
490                         queue |= PTLRPC_NRS_QUEUE_HP;
491                         val_hp = val_reg;
492                 }
493         }
494
495         if (queue & PTLRPC_NRS_QUEUE_REG) {
496                 if (val_reg > max_val || val_reg < min_val)
497                         GOTO(free_tmp, rc = -EINVAL);
498
499                 rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
500                                                pol_name, opc, single, &val_reg);
501                 if ((rc < 0 && rc != -ENODEV) ||
502                     (rc == -ENODEV && queue == PTLRPC_NRS_QUEUE_REG))
503                         GOTO(free_tmp, rc);
504         }
505
506         if (queue & PTLRPC_NRS_QUEUE_HP) {
507                 int rc2 = 0;
508                 if (val_hp > max_val || val_hp < min_val)
509                         GOTO(free_tmp, rc = -EINVAL);
510
511                 rc2 = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
512                                                 pol_name, opc, single, &val_hp);
513                 if ((rc2 < 0 && rc2 != -ENODEV) ||
514                     (rc2 == -ENODEV && queue == PTLRPC_NRS_QUEUE_HP))
515                         GOTO(free_tmp, rc = rc2);
516         }
517
518         /* If we've reached here then we want to return count */
519         rc = count;
520
521 free_tmp:
522         OBD_FREE(tmp, tmpsize);
523 free_kernbuf:
524         OBD_FREE(kernbuf, bufsize);
525
526         return rc;
527 }
528
529 /**
530  * Retrieves the value of the minimum delay for delay policy instances on both
531  * the regular and high-priority NRS head of a service, as long as a policy
532  * instance is not in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state;
533  */
534 static int
535 ptlrpc_lprocfs_nrs_delay_min_seq_show(struct seq_file *m, void *data)
536 {
537         struct ptlrpc_service *svc = m->private;
538         unsigned int min_delay;
539         int rc;
540
541         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
542                                        NRS_POL_NAME_DELAY,
543                                        NRS_CTL_DELAY_RD_MIN,
544                                        true, &min_delay);
545
546         if (rc == 0)
547                 seq_printf(m, LPROCFS_NRS_DELAY_MIN_NAME_REG"%-5d\n",
548                            min_delay);
549                 /**
550                  * Ignore -ENODEV as the regular NRS head's policy may be in
551                  * the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
552                  */
553         else if (rc != -ENODEV)
554                 return rc;
555
556         if (!nrs_svc_has_hp(svc))
557                 return 0;
558
559         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
560                                        NRS_POL_NAME_DELAY,
561                                        NRS_CTL_DELAY_RD_MIN,
562                                        true, &min_delay);
563         if (rc == 0)
564                 seq_printf(m, LPROCFS_NRS_DELAY_MIN_NAME_HP"%-5d\n",
565                            min_delay);
566                 /**
567                  * Ignore -ENODEV as the regular NRS head's policy may be in
568                  * the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
569                  */
570         else if (rc == -ENODEV)
571                 rc = 0;
572
573         return rc;
574 }
575
576 /**
577  * Sets the value of the minimum request delay for delay policy instances of a
578  * service. The user can set the minimum request delay for the regular or high
579  * priority NRS head individually by specifying each value, or both together in
580  * a single invocation.
581  *
582  * For example:
583  *
584  * lctl set_param *.*.*.nrs_delay_min=reg_delay_min:5, to set the regular
585  * request minimum delay on all PtlRPC services to 5 seconds
586  *
587  * lctl set_param *.*.*.nrs_delay_min=hp_delay_min:2, to set the high-priority
588  * request minimum delay on all PtlRPC services to 2 seconds, and
589  *
590  * lctl set_param *.*.ost_io.nrs_delay_min=8, to set both the regular and
591  * high priority request minimum delay of the ost_io service to 8 seconds.
592  */
593 static ssize_t
594 ptlrpc_lprocfs_nrs_delay_min_seq_write(struct file *file,
595                                        const char __user *buffer, size_t count,
596                                        loff_t *off)
597 {
598         struct seq_file *m = file->private_data;
599         struct ptlrpc_service *svc = m->private;
600
601         return lprocfs_nrs_delay_seq_write_common(buffer,
602                                                   LPROCFS_NRS_DELAY_MIN_SIZE,
603                                                   count,
604                                                   LPROCFS_NRS_DELAY_MIN_NAME,
605                                                   LPROCFS_NRS_DELAY_LOWER_BOUND,
606                                                   LPROCFS_NRS_DELAY_UPPER_BOUND,
607                                                   svc, NRS_POL_NAME_DELAY,
608                                                   NRS_CTL_DELAY_WR_MIN, false);
609 }
610 LDEBUGFS_SEQ_FOPS(ptlrpc_lprocfs_nrs_delay_min);
611
612 /**
613  * Retrieves the value of the maximum delay for delay policy instances on both
614  * the regular and high-priority NRS head of a service, as long as a policy
615  * instance is not in the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state;
616  */
617 static int
618 ptlrpc_lprocfs_nrs_delay_max_seq_show(struct seq_file *m, void *data)
619 {
620         struct ptlrpc_service *svc = m->private;
621         unsigned int max_delay;
622         int rc;
623
624         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
625                                        NRS_POL_NAME_DELAY,
626                                        NRS_CTL_DELAY_RD_MAX,
627                                        true, &max_delay);
628
629         if (rc == 0)
630                 seq_printf(m, LPROCFS_NRS_DELAY_MAX_NAME_REG"%-5d\n",
631                            max_delay);
632                 /**
633                  * Ignore -ENODEV as the regular NRS head's policy may be in
634                  * the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
635                  */
636         else if (rc != -ENODEV)
637                 return rc;
638
639         if (!nrs_svc_has_hp(svc))
640                 return 0;
641
642         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
643                                        NRS_POL_NAME_DELAY,
644                                        NRS_CTL_DELAY_RD_MAX,
645                                        true, &max_delay);
646         if (rc == 0)
647                 seq_printf(m, LPROCFS_NRS_DELAY_MAX_NAME_HP"%-5d\n",
648                            max_delay);
649                 /**
650                  * Ignore -ENODEV as the regular NRS head's policy may be in
651                  * the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
652                  */
653         else if (rc == -ENODEV)
654                 rc = 0;
655
656         return rc;
657 }
658
659 /**
660  * Sets the value of the maximum request delay for delay policy instances of a
661  * service. The user can set the maximum request delay for the regular or high
662  * priority NRS head individually by specifying each value, or both together in
663  * a single invocation.
664  *
665  * For example:
666  *
667  * lctl set_param *.*.*.nrs_delay_max=reg_delay_max:20, to set the regular
668  * request maximum delay on all PtlRPC services to 20 seconds
669  *
670  * lctl set_param *.*.*.nrs_delay_max=hp_delay_max:10, to set the high-priority
671  * request maximum delay on all PtlRPC services to 10 seconds, and
672  *
673  * lctl set_param *.*.ost_io.nrs_delay_max=35, to set both the regular and
674  * high priority request maximum delay of the ost_io service to 35 seconds.
675  */
676 static ssize_t
677 ptlrpc_lprocfs_nrs_delay_max_seq_write(struct file *file,
678                                        const char __user *buffer, size_t count,
679                                        loff_t *off)
680 {
681         struct seq_file *m = file->private_data;
682         struct ptlrpc_service *svc = m->private;
683
684         return lprocfs_nrs_delay_seq_write_common(buffer,
685                                                   LPROCFS_NRS_DELAY_MAX_SIZE,
686                                                   count,
687                                                   LPROCFS_NRS_DELAY_MAX_NAME,
688                                                   LPROCFS_NRS_DELAY_LOWER_BOUND,
689                                                   LPROCFS_NRS_DELAY_UPPER_BOUND,
690                                                   svc, NRS_POL_NAME_DELAY,
691                                                   NRS_CTL_DELAY_WR_MAX, false);
692 }
693 LDEBUGFS_SEQ_FOPS(ptlrpc_lprocfs_nrs_delay_max);
694
695 /**
696  * Retrieves the value of the percentage of requests which should be delayed
697  * for delay policy instances on both the regular and high-priority NRS head
698  * of a service, as long as a policy instance is not in the
699  * ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state;
700  */
701 static int
702 ptlrpc_lprocfs_nrs_delay_pct_seq_show(struct seq_file *m, void *data)
703 {
704         struct ptlrpc_service *svc = m->private;
705         unsigned int delay_pct;
706         int rc;
707
708         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_REG,
709                                        NRS_POL_NAME_DELAY,
710                                        NRS_CTL_DELAY_RD_PCT,
711                                        true, &delay_pct);
712
713         if (rc == 0)
714                 seq_printf(m, LPROCFS_NRS_DELAY_PCT_NAME_REG"%-3d\n",
715                            delay_pct);
716                 /**
717                  * Ignore -ENODEV as the regular NRS head's policy may be in
718                  * the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
719                  */
720         else if (rc != -ENODEV)
721                 return rc;
722
723         if (!nrs_svc_has_hp(svc))
724                 return 0;
725
726         rc = ptlrpc_nrs_policy_control(svc, PTLRPC_NRS_QUEUE_HP,
727                                        NRS_POL_NAME_DELAY,
728                                        NRS_CTL_DELAY_RD_PCT,
729                                        true, &delay_pct);
730         if (rc == 0)
731                 seq_printf(m, LPROCFS_NRS_DELAY_PCT_NAME_HP"%-3d\n",
732                            delay_pct);
733                 /**
734                  * Ignore -ENODEV as the regular NRS head's policy may be in
735                  * the ptlrpc_nrs_pol_state::NRS_POL_STATE_STOPPED state.
736                  */
737         else if (rc == -ENODEV)
738                 rc = 0;
739
740         return rc;
741 }
742
743 /**
744  * Sets the value of the percentage of requests to be delayed for delay policy
745  * instances of a service. The user can set the percentage for the regular or
746  * high-priority NRS head individually by specifying each value, or both
747  * together in a single invocation.
748  *
749  * For example:
750  *
751  * lctl set_param *.*.*.nrs_delay_pct=reg_delay_pct:5, to delay 5 percent of
752  * regular requests on all PtlRPC services
753  *
754  * lctl set_param *.*.*.nrs_delay_pct=hp_delay_pct:2, to delay 2 percent of
755  * high-priority requests on all PtlRPC services, and
756  *
757  * lctl set_param *.*.ost_io.nrs_delay_pct=8, to delay 8 percent of both
758  * regular and high-priority requests of the ost_io service.
759  */
760 static ssize_t
761 ptlrpc_lprocfs_nrs_delay_pct_seq_write(struct file *file,
762                                        const char __user *buffer, size_t count,
763                                        loff_t *off)
764 {
765         struct seq_file *m = file->private_data;
766         struct ptlrpc_service *svc = m->private;
767
768         return lprocfs_nrs_delay_seq_write_common(buffer,
769                                                   LPROCFS_NRS_DELAY_PCT_SIZE,
770                                                   count,
771                                                   LPROCFS_NRS_DELAY_PCT_NAME,
772                                                   LPROCFS_NRS_DELAY_PCT_MIN_VAL,
773                                                   LPROCFS_NRS_DELAY_PCT_MAX_VAL,
774                                                   svc, NRS_POL_NAME_DELAY,
775                                                   NRS_CTL_DELAY_WR_PCT, false);
776 }
777
778 LDEBUGFS_SEQ_FOPS(ptlrpc_lprocfs_nrs_delay_pct);
779
780 static int nrs_delay_lprocfs_init(struct ptlrpc_service *svc)
781 {
782         struct ldebugfs_vars nrs_delay_lprocfs_vars[] = {
783                 { .name         = "nrs_delay_min",
784                   .fops         = &ptlrpc_lprocfs_nrs_delay_min_fops,
785                   .data         = svc },
786                 { .name         = "nrs_delay_max",
787                   .fops         = &ptlrpc_lprocfs_nrs_delay_max_fops,
788                   .data         = svc },
789                 { .name         = "nrs_delay_pct",
790                   .fops         = &ptlrpc_lprocfs_nrs_delay_pct_fops,
791                   .data         = svc },
792                 { NULL }
793         };
794
795         if (!svc->srv_debugfs_entry)
796                 return 0;
797
798         ldebugfs_add_vars(svc->srv_debugfs_entry, nrs_delay_lprocfs_vars, NULL);
799
800         return 0;
801 }
802
803 /**
804  * Delay policy operations
805  */
806 static const struct ptlrpc_nrs_pol_ops nrs_delay_ops = {
807         .op_policy_start        = nrs_delay_start,
808         .op_policy_stop         = nrs_delay_stop,
809         .op_policy_ctl          = nrs_delay_ctl,
810         .op_res_get             = nrs_delay_res_get,
811         .op_req_get             = nrs_delay_req_get,
812         .op_req_enqueue         = nrs_delay_req_add,
813         .op_req_dequeue         = nrs_delay_req_del,
814         .op_req_stop            = nrs_delay_req_stop,
815         .op_lprocfs_init        = nrs_delay_lprocfs_init,
816 };
817
818 /**
819  * Delay policy configuration
820  */
821 struct ptlrpc_nrs_pol_conf nrs_conf_delay = {
822         .nc_name                = NRS_POL_NAME_DELAY,
823         .nc_ops                 = &nrs_delay_ops,
824         .nc_compat              = nrs_policy_compat_all,
825 };
826
827 /** @} delay */
828
829 /** @} nrs */