Whamcloud - gitweb
LU-6283 ptlrpc: Implement NRS Delay Policy
[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  * lprocfs interface
366  */
367
368 #ifdef CONFIG_PROC_FS
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 LPROC_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 LPROC_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 LPROC_SEQ_FOPS(ptlrpc_lprocfs_nrs_delay_pct);
778
779 static int nrs_delay_lprocfs_init(struct ptlrpc_service *svc)
780 {
781         struct lprocfs_vars nrs_delay_lprocfs_vars[] = {
782                 { .name         = "nrs_delay_min",
783                   .fops         = &ptlrpc_lprocfs_nrs_delay_min_fops,
784                   .data         = svc },
785                 { .name         = "nrs_delay_max",
786                   .fops         = &ptlrpc_lprocfs_nrs_delay_max_fops,
787                   .data         = svc },
788                 { .name         = "nrs_delay_pct",
789                   .fops         = &ptlrpc_lprocfs_nrs_delay_pct_fops,
790                   .data         = svc },
791                 { NULL }
792         };
793
794         if (svc->srv_procroot == NULL)
795                 return 0;
796
797         return lprocfs_add_vars(svc->srv_procroot, nrs_delay_lprocfs_vars,
798                                 NULL);
799 }
800
801 static void nrs_delay_lprocfs_fini(struct ptlrpc_service *svc)
802 {
803         if (svc->srv_procroot == NULL)
804                 return;
805
806         lprocfs_remove_proc_entry("nrs_delay_min", svc->srv_procroot);
807         lprocfs_remove_proc_entry("nrs_delay_max", svc->srv_procroot);
808         lprocfs_remove_proc_entry("nrs_delay_pct", svc->srv_procroot);
809 }
810
811 #endif /* CONFIG_PROC_FS */
812
813 /**
814  * Delay policy operations
815  */
816 static const struct ptlrpc_nrs_pol_ops nrs_delay_ops = {
817         .op_policy_start        = nrs_delay_start,
818         .op_policy_stop         = nrs_delay_stop,
819         .op_policy_ctl          = nrs_delay_ctl,
820         .op_res_get             = nrs_delay_res_get,
821         .op_req_get             = nrs_delay_req_get,
822         .op_req_enqueue         = nrs_delay_req_add,
823         .op_req_dequeue         = nrs_delay_req_del,
824         .op_req_stop            = nrs_delay_req_stop,
825 #ifdef CONFIG_PROC_FS
826         .op_lprocfs_init        = nrs_delay_lprocfs_init,
827         .op_lprocfs_fini        = nrs_delay_lprocfs_fini,
828 #endif
829 };
830
831 /**
832  * Delay policy configuration
833  */
834 struct ptlrpc_nrs_pol_conf nrs_conf_delay = {
835         .nc_name                = NRS_POL_NAME_DELAY,
836         .nc_ops                 = &nrs_delay_ops,
837         .nc_compat              = nrs_policy_compat_all,
838 };
839
840 /** @} delay */
841
842 /** @} nrs */