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