Whamcloud - gitweb
979de4773a805d954698801325193f8ab7d0d81b
[fs/lustre-release.git] / lustre / ldlm / ldlm_pool.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2007 Cluster File Systems, Inc.
5  *   Author: Yury Umanets <umka@clusterfs.com>
6  *
7  *   This file is part of the Lustre file system, http://www.lustre.org
8  *   Lustre is a trademark of Cluster File Systems, Inc.
9  *
10  *   You may have signed or agreed to another license before downloading
11  *   this software.  If so, you are bound by the terms and conditions
12  *   of that agreement, and the following does not apply to you.  See the
13  *   LICENSE file included with this distribution for more information.
14  *
15  *   If you did not agree to a different license, then this copy of Lustre
16  *   is open source software; you can redistribute it and/or modify it
17  *   under the terms of version 2 of the GNU General Public License as
18  *   published by the Free Software Foundation.
19  *
20  *   In either case, Lustre is distributed in the hope that it will be
21  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   license text for more details.
24  */
25
26 /* Idea of this code is rather simple. Each second, for each server namespace
27  * we have SLV - server lock volume which is calculated on current number of
28  * granted locks, grant speed for past period, etc - that is, locking load.
29  * This SLV number may be thought as a flow definition for simplicity. It is
30  * sent to clients with each occasion to let them know what is current load
31  * situation on the server. By default, at the beginning, SLV on server is
32  * set max value which is calculated as the following: allow to one client
33  * have all locks of limit ->pl_limit for 10h.
34  *
35  * Next, on clients, number of cached locks is not limited artificially in any
36  * way as it was before. Instead, client calculates CLV, that is, client lock
37  * volume for each lock and compares it with last SLV from the server. CLV is
38  * calculated as the number of locks in LRU * lock live time in seconds. If
39  * CLV > SLV - lock is canceled.
40  *
41  * Client has LVF, that is, lock volume factor which regulates how much sensitive
42  * client should be about last SLV from server. The higher LVF is the more locks
43  * will be canceled on client. Default value for it is 1. Setting LVF to 2 means
44  * that client will cancel locks 2 times faster.
45  *
46  * Locks on a client will be canceled more intensively in these cases:
47  * (1) if SLV is smaller, that is, load is higher on the server;
48  * (2) client has a lot of locks (the more locks are held by client, the bigger
49  *     chances that some of them should be canceled);
50  * (3) client has old locks (taken some time ago);
51  *
52  * Thus, according to flow paradigm that we use for better understanding SLV,
53  * CLV is the volume of particle in flow described by SLV. According to this,
54  * if flow is getting thinner, more and more particles become outside of it and
55  * as particles are locks, they should be canceled.
56  *
57  * General idea of this belongs to Vitaly Fertman (vitaly@clusterfs.com). Andreas
58  * Dilger (adilger@clusterfs.com) proposed few nice ideas like using LVF and many
59  * cleanups. Flow definition to allow more easy understanding of the logic belongs
60  * to Nikita Danilov (nikita@clusterfs.com) as well as many cleanups and fixes.
61  * And design and implementation are done by Yury Umanets (umka@clusterfs.com).
62  *
63  * Glossary for terms used:
64  *
65  * pl_limit - Number of allowed locks in pool. Applies to server and client
66  * side (tunable);
67  *
68  * pl_granted - Number of granted locks (calculated);
69  * pl_grant_rate - Number of granted locks for last T (calculated);
70  * pl_cancel_rate - Number of canceled locks for last T (calculated);
71  * pl_grant_speed - Grant speed (GR - CR) for last T (calculated);
72  * pl_grant_plan - Planned number of granted locks for next T (calculated);
73  *
74  * pl_grant_step - Grant plan step, that is how ->pl_grant_plan
75  * will change in next T (tunable);
76  *
77  * pl_server_lock_volume - Current server lock volume (calculated);
78  *
79  * As it may be seen from list above, we have few possible tunables which may
80  * affect behavior much. They all may be modified via proc. However, they also
81  * give a possibility for constructing few pre-defined behavior policies. If
82  * none of predefines is suitable for a working pattern being used, new one may
83  * be "constructed" via proc tunables.
84  */
85
86 #define DEBUG_SUBSYSTEM S_LDLM
87
88 #ifdef __KERNEL__
89 # include <lustre_dlm.h>
90 #else
91 # include <liblustre.h>
92 # include <libcfs/kp30.h>
93 #endif
94
95 #include <obd_class.h>
96 #include <obd_support.h>
97 #include "ldlm_internal.h"
98
99 #ifdef HAVE_LRU_RESIZE_SUPPORT
100
101 /* 50 ldlm locks for 1MB of RAM. */
102 #define LDLM_POOL_HOST_L ((num_physpages >> (20 - PAGE_SHIFT)) * 50)
103
104 /* Default step in % for grant plan. */
105 #define LDLM_POOL_GSP (5)
106
107 /* LDLM_POOL_GSP% of all locks is default GP. */
108 #define LDLM_POOL_GP(L)   ((L) * LDLM_POOL_GSP / 100)
109
110 /* Max age for locks on clients. */
111 #define LDLM_POOL_MAX_AGE (36000)
112
113 #ifdef __KERNEL__
114 extern cfs_proc_dir_entry_t *ldlm_ns_proc_dir;
115 #endif
116
117 #define avg(src, add) \
118         ((src) = ((src) + (add)) / 2)
119
120 static inline __u64 dru(__u64 val, __u32 div)
121 {
122         __u64 ret = val + (div - 1);
123         do_div(ret, div);
124         return ret;
125 }
126
127 static inline __u64 ldlm_pool_slv_max(__u32 L)
128 {
129         /* Allow to have all locks for 1 client for 10 hrs.
130          * Formula is the following: limit * 10h / 1 client. */
131         __u64 lim = L *  LDLM_POOL_MAX_AGE / 1;
132         return lim;
133 }
134
135 static inline __u64 ldlm_pool_slv_min(__u32 L)
136 {
137         return 1;
138 }
139
140 enum {
141         LDLM_POOL_FIRST_STAT = 0,
142         LDLM_POOL_GRANTED_STAT = LDLM_POOL_FIRST_STAT,
143         LDLM_POOL_GRANT_STAT,
144         LDLM_POOL_CANCEL_STAT,
145         LDLM_POOL_GRANT_RATE_STAT,
146         LDLM_POOL_CANCEL_RATE_STAT,
147         LDLM_POOL_GRANT_PLAN_STAT,
148         LDLM_POOL_SLV_STAT,
149         LDLM_POOL_SHRINK_REQTD_STAT,
150         LDLM_POOL_SHRINK_FREED_STAT,
151         LDLM_POOL_RECALC_STAT,
152         LDLM_POOL_LAST_STAT
153 };
154
155 static inline struct ldlm_namespace *ldlm_pl2ns(struct ldlm_pool *pl)
156 {
157         return container_of(pl, struct ldlm_namespace, ns_pool);
158 }
159
160 /* Should be called under ->pl_lock taken */
161 static inline void ldlm_pool_recalc_grant_plan(struct ldlm_pool *pl)
162 {
163         int grant_plan, granted;
164         __u32 limit;
165         
166         limit = ldlm_pool_get_limit(pl);
167         granted = atomic_read(&pl->pl_granted);
168
169         grant_plan = granted + ((limit - granted) *
170                 atomic_read(&pl->pl_grant_step)) / 100;
171         atomic_set(&pl->pl_grant_plan, grant_plan);
172 }
173
174 /* Should be called under ->pl_lock taken */
175 static inline void ldlm_pool_recalc_slv(struct ldlm_pool *pl)
176 {
177         int slv_factor, granted, grant_plan;
178         __u32 limit;
179         __u64 slv;
180
181         slv = ldlm_pool_get_slv(pl);
182         limit = ldlm_pool_get_limit(pl);
183         granted = atomic_read(&pl->pl_granted);
184         grant_plan = atomic_read(&pl->pl_grant_plan);
185
186         if ((slv_factor = limit - (granted - grant_plan)) <= 0)
187                 slv_factor = 1;
188
189         slv = (slv * ((slv_factor * 100) / limit));
190         slv = dru(slv, 100);
191
192         if (slv > ldlm_pool_slv_max(limit)) {
193                 slv = ldlm_pool_slv_max(limit);
194         } else if (slv < ldlm_pool_slv_min(limit)) {
195                 slv = ldlm_pool_slv_min(limit);
196         }
197
198         ldlm_pool_set_slv(pl, slv);
199 }
200
201 static inline void ldlm_pool_recalc_stats(struct ldlm_pool *pl)
202 {
203         __u64 slv = ldlm_pool_get_slv(pl);
204         __u32 granted = atomic_read(&pl->pl_granted);
205         __u32 grant_rate = atomic_read(&pl->pl_grant_rate);
206         __u32 grant_plan = atomic_read(&pl->pl_grant_plan);
207         __u32 cancel_rate = atomic_read(&pl->pl_cancel_rate);
208
209         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_SLV_STAT, 
210                             slv);
211         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
212                             granted);
213         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
214                             grant_rate);
215         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
216                             grant_plan);
217         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
218                             cancel_rate);
219 }
220
221 static int ldlm_srv_pool_recalc(struct ldlm_pool *pl)
222 {
223         time_t recalc_interval_sec;
224         ENTRY;
225
226         spin_lock(&pl->pl_lock);
227         recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
228         if (recalc_interval_sec > 0) {
229                 /* Update statistics */
230                 ldlm_pool_recalc_stats(pl);
231
232                 /* Recalc SLV after last period. This should be done
233                  * _before_ recalculating new grant plan. */
234                 ldlm_pool_recalc_slv(pl);
235
236                 /* Update grant_plan for new period. */
237                 ldlm_pool_recalc_grant_plan(pl);
238
239                 /* Zero out all rates and speed for the last period. */
240                 atomic_set(&pl->pl_grant_rate, 0);
241                 atomic_set(&pl->pl_cancel_rate, 0);
242                 atomic_set(&pl->pl_grant_speed, 0);
243                 pl->pl_recalc_time = cfs_time_current_sec();
244         }
245         spin_unlock(&pl->pl_lock);
246         RETURN(0);
247 }
248
249 /* Our goal here is to decrease SLV the way to make a client hold
250  * @nr locks smaller in next 10h. */
251 static int ldlm_srv_pool_shrink(struct ldlm_pool *pl,
252                                 int nr, unsigned int gfp_mask)
253 {
254         __u32 limit;
255         ENTRY;
256
257         /* VM is asking how many entries may be potentially freed. */
258         if (nr == 0)
259                 RETURN(atomic_read(&pl->pl_granted));
260
261         /* Client already canceled locks but server is already in shrinker
262          * and can't cancel anything. Let's catch this race. */
263         if (atomic_read(&pl->pl_granted) == 0)
264                 RETURN(0);
265
266         spin_lock(&pl->pl_lock);
267
268         /* We want shrinker to possibly cause cancelation of @nr locks from
269          * clients or grant approximately @nr locks smaller next intervals.
270          *
271          * This is why we decresed SLV by @nr. This effect will only be as
272          * long as one re-calc interval (1s these days) and this should be
273          * enough to pass this decreased SLV to all clients. On next recalc
274          * interval pool will either increase SLV if locks load is not high
275          * or will keep on same level or even decrease again, thus, shrinker
276          * decreased SLV will affect next recalc intervals and this way will
277          * make locking load lower. */
278         if (nr < ldlm_pool_get_slv(pl)) {
279                 ldlm_pool_set_slv(pl, ldlm_pool_get_slv(pl) - nr);
280         } else {
281                 limit = ldlm_pool_get_limit(pl);
282                 ldlm_pool_set_slv(pl, ldlm_pool_slv_min(limit));
283         }
284         spin_unlock(&pl->pl_lock);
285
286         /* We did not really free any memory here so far, it only will be
287          * freed later may be, so that we return 0 to not confuse VM. */
288         RETURN(0);
289 }
290
291 static int ldlm_srv_pool_setup(struct ldlm_pool *pl, int limit)
292 {
293         ENTRY;
294         ldlm_pool_set_limit(pl, limit);
295         RETURN(0);
296 }
297
298 static int ldlm_cli_pool_recalc(struct ldlm_pool *pl)
299 {
300         time_t recalc_interval_sec;
301         ENTRY;
302
303         spin_lock(&pl->pl_lock);
304
305         recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
306         if (recalc_interval_sec > 0) {
307                 /* Update statistics only every T */
308                 ldlm_pool_recalc_stats(pl);
309
310                 /* Zero out grant/cancel rates and speed for last period. */
311                 atomic_set(&pl->pl_grant_rate, 0);
312                 atomic_set(&pl->pl_cancel_rate, 0);
313                 atomic_set(&pl->pl_grant_speed, 0);
314                 pl->pl_recalc_time = cfs_time_current_sec();
315         }
316         spin_unlock(&pl->pl_lock);
317
318         /* Do not cancel locks in case lru resize is disabled for this ns */
319         if (!ns_connect_lru_resize(ldlm_pl2ns(pl)))
320                 RETURN(0);
321
322         /* In the time of canceling locks on client we do not need to maintain
323          * sharp timing, we only want to cancel locks asap according to new SLV.
324          * This may be called when SLV has changed much, this is why we do not
325          * take into account pl->pl_recalc_time here. */
326         RETURN(ldlm_cancel_lru(ldlm_pl2ns(pl), 0, LDLM_ASYNC, 
327                                LDLM_CANCEL_LRUR));
328 }
329
330 static int ldlm_cli_pool_shrink(struct ldlm_pool *pl,
331                                 int nr, unsigned int gfp_mask)
332 {
333         ENTRY;
334         
335         /* Do not cancel locks in case lru resize is disabled for this ns */
336         if (!ns_connect_lru_resize(ldlm_pl2ns(pl)))
337                 RETURN(0);
338
339         /* Find out how many locks may be released according to shrink 
340          * policy. */
341         if (nr == 0)
342                 RETURN(ldlm_cancel_lru_local(ldlm_pl2ns(pl), NULL, 0, 
343                                              0, LDLM_CANCEL_SHRINK));
344
345         /* Cancel @nr locks accoding to shrink policy */
346         RETURN(ldlm_cancel_lru(ldlm_pl2ns(pl), nr, LDLM_SYNC, 
347                                LDLM_CANCEL_SHRINK));
348 }
349
350 struct ldlm_pool_ops ldlm_srv_pool_ops = {
351         .po_recalc = ldlm_srv_pool_recalc,
352         .po_shrink = ldlm_srv_pool_shrink,
353         .po_setup  = ldlm_srv_pool_setup
354 };
355
356 struct ldlm_pool_ops ldlm_cli_pool_ops = {
357         .po_recalc = ldlm_cli_pool_recalc,
358         .po_shrink = ldlm_cli_pool_shrink
359 };
360
361 int ldlm_pool_recalc(struct ldlm_pool *pl)
362 {
363         int count;
364
365         if (pl->pl_ops->po_recalc != NULL && pool_recalc_enabled(pl)) {
366                 count = pl->pl_ops->po_recalc(pl);
367                 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_RECALC_STAT, 
368                                     count);
369                 return count;
370         }
371         return 0;
372 }
373 EXPORT_SYMBOL(ldlm_pool_recalc);
374
375 int ldlm_pool_shrink(struct ldlm_pool *pl, int nr,
376                      unsigned int gfp_mask)
377 {
378         int cancel = 0;
379         
380         if (pl->pl_ops->po_shrink != NULL && pool_shrink_enabled(pl)) {
381                 cancel = pl->pl_ops->po_shrink(pl, nr, gfp_mask);
382                 if (nr > 0) {
383                         lprocfs_counter_add(pl->pl_stats, 
384                                             LDLM_POOL_SHRINK_REQTD_STAT,
385                                             nr);
386                         lprocfs_counter_add(pl->pl_stats, 
387                                             LDLM_POOL_SHRINK_FREED_STAT,
388                                             cancel);
389                         CDEBUG(D_DLMTRACE, "%s: request to shrink %d locks, "
390                                "shrunk %d\n", pl->pl_name, nr, cancel);
391                 }
392         }
393         return cancel;
394 }
395 EXPORT_SYMBOL(ldlm_pool_shrink);
396
397 /* The purpose of this function is to re-setup limit and maximal allowed
398  * slv according to the passed limit. */
399 int ldlm_pool_setup(struct ldlm_pool *pl, int limit)
400 {
401         ENTRY;
402         if (pl->pl_ops->po_setup != NULL)
403                 RETURN(pl->pl_ops->po_setup(pl, limit));
404         RETURN(0);
405 }
406 EXPORT_SYMBOL(ldlm_pool_setup);
407
408 #ifdef __KERNEL__
409 static int lprocfs_rd_pool_state(char *page, char **start, off_t off,
410                                  int count, int *eof, void *data)
411 {
412         __u32 granted, grant_rate, cancel_rate, grant_step;
413         int nr = 0, grant_speed, grant_plan;
414         struct ldlm_pool *pl = data;
415         __u32 limit;
416         __u64 slv;
417
418         spin_lock(&pl->pl_lock);
419         slv = ldlm_pool_get_slv(pl);
420         limit = ldlm_pool_get_limit(pl);
421         granted = atomic_read(&pl->pl_granted);
422         grant_rate = atomic_read(&pl->pl_grant_rate);
423         grant_plan = atomic_read(&pl->pl_grant_plan);
424         grant_step = atomic_read(&pl->pl_grant_step);
425         grant_speed = atomic_read(&pl->pl_grant_speed);
426         cancel_rate = atomic_read(&pl->pl_cancel_rate);
427         spin_unlock(&pl->pl_lock);
428
429         nr += snprintf(page + nr, count - nr, "LDLM pool state (%s):\n",
430                        pl->pl_name);
431         nr += snprintf(page + nr, count - nr, "  SLV: "LPU64"\n", slv);
432
433         nr += snprintf(page + nr, count - nr, "  LVF: %d\n",
434                        atomic_read(&pl->pl_lock_volume_factor));
435
436         nr += snprintf(page + nr, count - nr, "  GSP: %d%%\n",
437                        grant_step);
438         nr += snprintf(page + nr, count - nr, "  GP:  %d\n",
439                        grant_plan);
440         nr += snprintf(page + nr, count - nr, "  GR:  %d\n",
441                        grant_rate);
442         nr += snprintf(page + nr, count - nr, "  CR:  %d\n",
443                        cancel_rate);
444         nr += snprintf(page + nr, count - nr, "  GS:  %d\n",
445                        grant_speed);
446         nr += snprintf(page + nr, count - nr, "  G:   %d\n",
447                        granted);
448         nr += snprintf(page + nr, count - nr, "  L:   %d\n",
449                        limit);
450         return nr;
451 }
452
453 static int ldlm_pool_proc_init(struct ldlm_pool *pl)
454 {
455         struct ldlm_namespace *ns = ldlm_pl2ns(pl);
456         struct proc_dir_entry *parent_ns_proc;
457         struct lprocfs_vars pool_vars[2];
458         char *var_name = NULL;
459         int rc = 0;
460         ENTRY;
461
462         OBD_ALLOC(var_name, MAX_STRING_SIZE + 1);
463         if (!var_name)
464                 RETURN(-ENOMEM);
465
466         parent_ns_proc = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
467         if (parent_ns_proc == NULL) {
468                 CERROR("%s: proc entry is not initialized\n",
469                        ns->ns_name);
470                 GOTO(out_free_name, rc = -EINVAL);
471         }
472         pl->pl_proc_dir = lprocfs_register("pool", parent_ns_proc,
473                                            NULL, NULL);
474         if (IS_ERR(pl->pl_proc_dir)) {
475                 CERROR("LProcFS failed in ldlm-pool-init\n");
476                 rc = PTR_ERR(pl->pl_proc_dir);
477                 GOTO(out_free_name, rc);
478         }
479
480         var_name[MAX_STRING_SIZE] = '\0';
481         memset(pool_vars, 0, sizeof(pool_vars));
482         pool_vars[0].name = var_name;
483
484         snprintf(var_name, MAX_STRING_SIZE, "server_lock_volume");
485         pool_vars[0].data = &pl->pl_server_lock_volume;
486         pool_vars[0].read_fptr = lprocfs_rd_u64;
487         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
488
489         snprintf(var_name, MAX_STRING_SIZE, "limit");
490         pool_vars[0].data = &pl->pl_limit;
491         pool_vars[0].read_fptr = lprocfs_rd_atomic;
492         pool_vars[0].write_fptr = lprocfs_wr_atomic;
493         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
494
495         snprintf(var_name, MAX_STRING_SIZE, "granted");
496         pool_vars[0].data = &pl->pl_granted;
497         pool_vars[0].read_fptr = lprocfs_rd_atomic;
498         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
499
500         snprintf(var_name, MAX_STRING_SIZE, "control");
501         pool_vars[0].data = &pl->pl_control;
502         pool_vars[0].read_fptr = lprocfs_rd_uint;
503         pool_vars[0].write_fptr = lprocfs_wr_uint;
504         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
505
506         snprintf(var_name, MAX_STRING_SIZE, "grant_speed");
507         pool_vars[0].data = &pl->pl_grant_speed;
508         pool_vars[0].read_fptr = lprocfs_rd_atomic;
509         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
510
511         snprintf(var_name, MAX_STRING_SIZE, "cancel_rate");
512         pool_vars[0].data = &pl->pl_cancel_rate;
513         pool_vars[0].read_fptr = lprocfs_rd_atomic;
514         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
515
516         snprintf(var_name, MAX_STRING_SIZE, "grant_rate");
517         pool_vars[0].data = &pl->pl_grant_rate;
518         pool_vars[0].read_fptr = lprocfs_rd_atomic;
519         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
520
521         snprintf(var_name, MAX_STRING_SIZE, "grant_plan");
522         pool_vars[0].data = &pl->pl_grant_plan;
523         pool_vars[0].read_fptr = lprocfs_rd_atomic;
524         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
525
526         snprintf(var_name, MAX_STRING_SIZE, "grant_step");
527         pool_vars[0].data = &pl->pl_grant_step;
528         pool_vars[0].read_fptr = lprocfs_rd_atomic;
529         if (ns_is_server(ns))
530                 pool_vars[0].write_fptr = lprocfs_wr_atomic;
531         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
532
533         snprintf(var_name, MAX_STRING_SIZE, "lock_volume_factor");
534         pool_vars[0].data = &pl->pl_lock_volume_factor;
535         pool_vars[0].read_fptr = lprocfs_rd_uint;
536         pool_vars[0].write_fptr = lprocfs_wr_uint;
537         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
538
539         snprintf(var_name, MAX_STRING_SIZE, "state");
540         pool_vars[0].data = pl;
541         pool_vars[0].read_fptr = lprocfs_rd_pool_state;
542         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
543
544         pl->pl_stats = lprocfs_alloc_stats(LDLM_POOL_LAST_STAT -
545                                            LDLM_POOL_FIRST_STAT, 0);
546         if (!pl->pl_stats)
547                 GOTO(out_free_name, rc = -ENOMEM);
548
549         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
550                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
551                              "granted", "locks");
552         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_STAT, 0,
553                              "grant", "locks");
554         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_STAT, 0,
555                              "cancel", "locks");
556         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
557                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
558                              "grant_rate", "locks/s");
559         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
560                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
561                              "cancel_rate", "locks/s");
562         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
563                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
564                              "grant_plan", "locks/s");
565         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SLV_STAT,
566                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
567                              "slv", "slv");
568         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_REQTD_STAT,
569                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
570                              "shrink_request", "locks");
571         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_FREED_STAT,
572                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
573                              "shrink_freed", "locks");
574         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_RECALC_STAT,
575                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
576                              "recalc_freed", "locks");
577         lprocfs_register_stats(pl->pl_proc_dir, "stats", pl->pl_stats);
578
579         EXIT;
580 out_free_name:
581         OBD_FREE(var_name, MAX_STRING_SIZE + 1);
582         return rc;
583 }
584
585 static void ldlm_pool_proc_fini(struct ldlm_pool *pl)
586 {
587         if (pl->pl_stats != NULL) {
588                 lprocfs_free_stats(&pl->pl_stats);
589                 pl->pl_stats = NULL;
590         }
591         if (pl->pl_proc_dir != NULL) {
592                 lprocfs_remove(&pl->pl_proc_dir);
593                 pl->pl_proc_dir = NULL;
594         }
595 }
596 #else /* !__KERNEL__*/
597 #define ldlm_pool_proc_init(pl) (0)
598 #define ldlm_pool_proc_fini(pl) while (0) {}
599 #endif
600
601 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
602                    int idx, ldlm_side_t client)
603 {
604         int rc;
605         ENTRY;
606
607         spin_lock_init(&pl->pl_lock);
608         atomic_set(&pl->pl_granted, 0);
609         pl->pl_recalc_time = cfs_time_current_sec();
610         atomic_set(&pl->pl_lock_volume_factor, 1);
611
612         atomic_set(&pl->pl_grant_rate, 0);
613         atomic_set(&pl->pl_cancel_rate, 0);
614         atomic_set(&pl->pl_grant_speed, 0);
615         pl->pl_control = LDLM_POOL_CTL_FULL;
616         atomic_set(&pl->pl_grant_step, LDLM_POOL_GSP);
617         atomic_set(&pl->pl_grant_plan, LDLM_POOL_GP(LDLM_POOL_HOST_L));
618
619         snprintf(pl->pl_name, sizeof(pl->pl_name), "ldlm-pool-%s-%d",
620                  ns->ns_name, idx);
621
622         if (client == LDLM_NAMESPACE_SERVER) {
623                 pl->pl_ops = &ldlm_srv_pool_ops;
624                 ldlm_pool_set_limit(pl, LDLM_POOL_HOST_L);
625                 ldlm_pool_set_slv(pl, ldlm_pool_slv_max(LDLM_POOL_HOST_L));
626         } else {
627                 ldlm_pool_set_slv(pl, 1);
628                 ldlm_pool_set_limit(pl, 1);
629                 pl->pl_ops = &ldlm_cli_pool_ops;
630         }
631
632         rc = ldlm_pool_proc_init(pl);
633         if (rc)
634                 RETURN(rc);
635
636         CDEBUG(D_DLMTRACE, "Lock pool %s is initialized\n", pl->pl_name);
637
638         RETURN(rc);
639 }
640 EXPORT_SYMBOL(ldlm_pool_init);
641
642 void ldlm_pool_fini(struct ldlm_pool *pl)
643 {
644         ENTRY;
645         ldlm_pool_proc_fini(pl);
646         pl->pl_ops = NULL;
647         EXIT;
648 }
649 EXPORT_SYMBOL(ldlm_pool_fini);
650
651 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
652 {
653         ENTRY;
654         atomic_inc(&pl->pl_granted);
655         atomic_inc(&pl->pl_grant_rate);
656         atomic_inc(&pl->pl_grant_speed);
657
658         lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_GRANT_STAT);
659  
660         /* Do not do pool recalc for client side as all locks which
661          * potentially may be canceled has already been packed into 
662          * enqueue/cancel rpc. Also we do not want to run out of stack
663          * with too long call paths. */
664         if (ns_is_server(ldlm_pl2ns(pl)))
665                 ldlm_pool_recalc(pl);
666         EXIT;
667 }
668 EXPORT_SYMBOL(ldlm_pool_add);
669
670 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
671 {
672         ENTRY;
673         LASSERT(atomic_read(&pl->pl_granted) > 0);
674         atomic_dec(&pl->pl_granted);
675         atomic_inc(&pl->pl_cancel_rate);
676         atomic_dec(&pl->pl_grant_speed);
677         
678         lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_CANCEL_STAT);
679
680         if (ns_is_server(ldlm_pl2ns(pl)))
681                 ldlm_pool_recalc(pl);
682         EXIT;
683 }
684 EXPORT_SYMBOL(ldlm_pool_del);
685
686 /* ->pl_lock should be taken. */
687 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
688 {
689         return pl->pl_server_lock_volume;
690 }
691 EXPORT_SYMBOL(ldlm_pool_get_slv);
692
693 /* ->pl_lock should be taken. */
694 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv)
695 {
696         pl->pl_server_lock_volume = slv;
697 }
698 EXPORT_SYMBOL(ldlm_pool_set_slv);
699
700 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
701 {
702         return atomic_read(&pl->pl_limit);
703 }
704 EXPORT_SYMBOL(ldlm_pool_get_limit);
705
706 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
707 {
708         atomic_set(&pl->pl_limit, limit);
709 }
710 EXPORT_SYMBOL(ldlm_pool_set_limit);
711
712 /* Server side is only enabled for kernel space for now. */
713 #ifdef __KERNEL__
714 static int ldlm_pool_granted(struct ldlm_pool *pl)
715 {
716         return atomic_read(&pl->pl_granted);
717 }
718
719 static struct ptlrpc_thread *ldlm_pools_thread;
720 static struct shrinker *ldlm_pools_srv_shrinker;
721 static struct shrinker *ldlm_pools_cli_shrinker;
722 static struct completion ldlm_pools_comp;
723
724 void ldlm_pools_wakeup(void)
725 {
726         ENTRY;
727         if (ldlm_pools_thread == NULL)
728                 return;
729         ldlm_pools_thread->t_flags |= SVC_EVENT;
730         cfs_waitq_signal(&ldlm_pools_thread->t_ctl_waitq);
731         EXIT;
732 }
733 EXPORT_SYMBOL(ldlm_pools_wakeup);
734
735 /* Cancel @nr locks from all namespaces (if possible). Returns number of
736  * cached locks after shrink is finished. All namespaces are asked to
737  * cancel approximately equal amount of locks. */
738 static int ldlm_pools_shrink(ldlm_side_t client, int nr, 
739                              unsigned int gfp_mask)
740 {
741         int total = 0, cached = 0, nr_ns;
742         struct ldlm_namespace *ns;
743
744         if (nr != 0 && !(gfp_mask & __GFP_FS))
745                 return -1;
746
747         CDEBUG(D_DLMTRACE, "request to shrink %d %s locks from all pools\n",
748                nr, client == LDLM_NAMESPACE_CLIENT ? "client" : "server");
749
750         /* Find out how many resources we may release. */
751         for (nr_ns = atomic_read(ldlm_namespace_nr(client)); 
752              nr_ns > 0; nr_ns--) 
753         {
754                 mutex_down(ldlm_namespace_lock(client));
755                 if (list_empty(ldlm_namespace_list(client))) {
756                         mutex_up(ldlm_namespace_lock(client));
757                         return 0;
758                 }
759                 ns = ldlm_namespace_first(client);
760                 ldlm_namespace_get(ns);
761                 ldlm_namespace_move(ns, client);
762                 mutex_up(ldlm_namespace_lock(client));
763                 total += ldlm_pool_shrink(&ns->ns_pool, 0, gfp_mask);
764                 ldlm_namespace_put(ns, 1);
765         }
766  
767         if (nr == 0 || total == 0)
768                 return total;
769
770         /* Shrink at least ldlm_namespace_nr(client) namespaces. */
771         for (nr_ns = atomic_read(ldlm_namespace_nr(client)); 
772              nr_ns > 0; nr_ns--) 
773         {
774                 int cancel, nr_locks;
775
776                 /* Do not call shrink under ldlm_namespace_lock(client) */
777                 mutex_down(ldlm_namespace_lock(client));
778                 if (list_empty(ldlm_namespace_list(client))) {
779                         mutex_up(ldlm_namespace_lock(client));
780                         /* If list is empty, we can't return any @cached > 0,
781                          * that probably would cause needless shrinker
782                          * call. */
783                         cached = 0;
784                         break;
785                 }
786                 ns = ldlm_namespace_first(client);
787                 ldlm_namespace_get(ns);
788                 ldlm_namespace_move(ns, client);
789                 mutex_up(ldlm_namespace_lock(client));
790                 
791                 nr_locks = ldlm_pool_granted(&ns->ns_pool);
792                 cancel = 1 + nr_locks * nr / total;
793                 ldlm_pool_shrink(&ns->ns_pool, cancel, gfp_mask);
794                 cached += ldlm_pool_granted(&ns->ns_pool);
795                 ldlm_namespace_put(ns, 1);
796         }
797         return cached;
798 }
799
800 static int ldlm_pools_srv_shrink(int nr, unsigned int gfp_mask)
801 {
802         return ldlm_pools_shrink(LDLM_NAMESPACE_SERVER, nr, gfp_mask);
803 }
804
805 static int ldlm_pools_cli_shrink(int nr, unsigned int gfp_mask)
806 {
807         return ldlm_pools_shrink(LDLM_NAMESPACE_CLIENT, nr, gfp_mask);
808 }
809
810 void ldlm_pools_recalc(ldlm_side_t client)
811 {
812         __u32 nr_l = 0, nr_p = 0, l;
813         struct ldlm_namespace *ns;
814         int nr, equal = 0;
815
816         /* No need to setup pool limit for client pools. */
817         if (client == LDLM_NAMESPACE_SERVER) {
818                 /* Check all modest namespaces first. */
819                 mutex_down(ldlm_namespace_lock(client));
820                 list_for_each_entry(ns, ldlm_namespace_list(client), 
821                                     ns_list_chain) 
822                 {
823                         if (ns->ns_appetite != LDLM_NAMESPACE_MODEST)
824                                 continue;
825
826                         l = ldlm_pool_granted(&ns->ns_pool);
827                         if (l == 0)
828                                 l = 1;
829
830                         /* Set the modest pools limit equal to their avg granted
831                          * locks + 5%. */
832                         l += dru(l * LDLM_POOLS_MODEST_MARGIN, 100);
833                         ldlm_pool_setup(&ns->ns_pool, l);
834                         nr_l += l;
835                         nr_p++;
836                 }
837
838                 /* Make sure that modest namespaces did not eat more that 2/3 
839                  * of limit */
840                 if (nr_l >= 2 * (LDLM_POOL_HOST_L / 3)) {
841                         CWARN("\"Modest\" pools eat out 2/3 of server locks "
842                               "limit (%d of %lu). This means that you have too "
843                               "many clients for this amount of server RAM. "
844                               "Upgrade server!\n", nr_l, LDLM_POOL_HOST_L);
845                         equal = 1;
846                 }
847
848                 /* The rest is given to greedy namespaces. */
849                 list_for_each_entry(ns, ldlm_namespace_list(client), 
850                                     ns_list_chain) 
851                 {
852                         if (!equal && ns->ns_appetite != LDLM_NAMESPACE_GREEDY)
853                                 continue;
854
855                         if (equal) {
856                                 /* In the case 2/3 locks are eaten out by
857                                  * modest pools, we re-setup equal limit
858                                  * for _all_ pools. */
859                                 l = LDLM_POOL_HOST_L /
860                                         atomic_read(ldlm_namespace_nr(client));
861                         } else {
862                                 /* All the rest of greedy pools will have
863                                  * all locks in equal parts.*/
864                                 l = (LDLM_POOL_HOST_L - nr_l) /
865                                         (atomic_read(ldlm_namespace_nr(client)) -
866                                          nr_p);
867                         }
868                         ldlm_pool_setup(&ns->ns_pool, l);
869                 }
870                 mutex_up(ldlm_namespace_lock(client));
871         }
872
873         /* Recalc at least ldlm_namespace_nr(client) namespaces. */
874         for (nr = atomic_read(ldlm_namespace_nr(client)); nr > 0; nr--) {
875                 /* Lock the list, get first @ns in the list, getref, move it
876                  * to the tail, unlock and call pool recalc. This way we avoid
877                  * calling recalc under @ns lock what is really good as we get
878                  * rid of potential deadlock on client nodes when canceling
879                  * locks synchronously. */
880                 mutex_down(ldlm_namespace_lock(client));
881                 if (list_empty(ldlm_namespace_list(client))) {
882                         mutex_up(ldlm_namespace_lock(client));
883                         break;
884                 }
885                 ns = ldlm_namespace_first(client);
886                 ldlm_namespace_get(ns);
887                 ldlm_namespace_move(ns, client);
888                 mutex_up(ldlm_namespace_lock(client));
889
890                 /* After setup is done - recalc the pool. */
891                 ldlm_pool_recalc(&ns->ns_pool);
892                 ldlm_namespace_put(ns, 1);
893         }
894 }
895 EXPORT_SYMBOL(ldlm_pools_recalc);
896
897 static int ldlm_pools_thread_main(void *arg)
898 {
899         struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
900         char *t_name = "ldlm_poold";
901         ENTRY;
902
903         cfs_daemonize(t_name);
904         thread->t_flags = SVC_RUNNING;
905         cfs_waitq_signal(&thread->t_ctl_waitq);
906
907         CDEBUG(D_DLMTRACE, "%s: pool thread starting, process %d\n",
908                t_name, cfs_curproc_pid());
909
910         while (1) {
911                 struct l_wait_info lwi;
912
913                 /* Recal all pools on this tick. */
914                 ldlm_pools_recalc(LDLM_NAMESPACE_SERVER);
915                 ldlm_pools_recalc(LDLM_NAMESPACE_CLIENT);
916                 
917                 /* Wait until the next check time, or until we're
918                  * stopped. */
919                 lwi = LWI_TIMEOUT(cfs_time_seconds(LDLM_POOLS_THREAD_PERIOD),
920                                   NULL, NULL);
921                 l_wait_event(thread->t_ctl_waitq, (thread->t_flags &
922                                                    (SVC_STOPPING|SVC_EVENT)),
923                              &lwi);
924
925                 if (thread->t_flags & SVC_STOPPING) {
926                         thread->t_flags &= ~SVC_STOPPING;
927                         break;
928                 } else if (thread->t_flags & SVC_EVENT) {
929                         thread->t_flags &= ~SVC_EVENT;
930                 }
931         }
932
933         thread->t_flags = SVC_STOPPED;
934         cfs_waitq_signal(&thread->t_ctl_waitq);
935
936         CDEBUG(D_DLMTRACE, "%s: pool thread exiting, process %d\n",
937                t_name, cfs_curproc_pid());
938
939         complete_and_exit(&ldlm_pools_comp, 0);
940 }
941
942 static int ldlm_pools_thread_start(void)
943 {
944         struct l_wait_info lwi = { 0 };
945         int rc;
946         ENTRY;
947
948         if (ldlm_pools_thread != NULL)
949                 RETURN(-EALREADY);
950
951         OBD_ALLOC_PTR(ldlm_pools_thread);
952         if (ldlm_pools_thread == NULL)
953                 RETURN(-ENOMEM);
954
955         init_completion(&ldlm_pools_comp);
956         cfs_waitq_init(&ldlm_pools_thread->t_ctl_waitq);
957
958         /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
959          * just drop the VM and FILES in ptlrpc_daemonize() right away. */
960         rc = cfs_kernel_thread(ldlm_pools_thread_main, ldlm_pools_thread,
961                                CLONE_VM | CLONE_FILES);
962         if (rc < 0) {
963                 CERROR("Can't start pool thread, error %d\n",
964                        rc);
965                 OBD_FREE(ldlm_pools_thread, sizeof(*ldlm_pools_thread));
966                 ldlm_pools_thread = NULL;
967                 RETURN(rc);
968         }
969         l_wait_event(ldlm_pools_thread->t_ctl_waitq,
970                      (ldlm_pools_thread->t_flags & SVC_RUNNING), &lwi);
971         RETURN(0);
972 }
973
974 static void ldlm_pools_thread_stop(void)
975 {
976         ENTRY;
977
978         if (ldlm_pools_thread == NULL) {
979                 EXIT;
980                 return;
981         }
982
983         ldlm_pools_thread->t_flags = SVC_STOPPING;
984         cfs_waitq_signal(&ldlm_pools_thread->t_ctl_waitq);
985
986         /* Make sure that pools thread is finished before freeing @thread.
987          * This fixes possible race and oops due to accessing freed memory
988          * in pools thread. */
989         wait_for_completion(&ldlm_pools_comp);
990         OBD_FREE_PTR(ldlm_pools_thread);
991         ldlm_pools_thread = NULL;
992         EXIT;
993 }
994
995 int ldlm_pools_init(void)
996 {
997         int rc;
998         ENTRY;
999
1000         rc = ldlm_pools_thread_start();
1001         if (rc == 0) {
1002                 ldlm_pools_srv_shrinker = set_shrinker(DEFAULT_SEEKS,
1003                                                        ldlm_pools_srv_shrink);
1004                 ldlm_pools_cli_shrinker = set_shrinker(DEFAULT_SEEKS,
1005                                                        ldlm_pools_cli_shrink);
1006         }
1007         RETURN(rc);
1008 }
1009 EXPORT_SYMBOL(ldlm_pools_init);
1010
1011 void ldlm_pools_fini(void)
1012 {
1013         if (ldlm_pools_srv_shrinker != NULL) {
1014                 remove_shrinker(ldlm_pools_srv_shrinker);
1015                 ldlm_pools_srv_shrinker = NULL;
1016         }
1017         if (ldlm_pools_cli_shrinker != NULL) {
1018                 remove_shrinker(ldlm_pools_cli_shrinker);
1019                 ldlm_pools_cli_shrinker = NULL;
1020         }
1021         ldlm_pools_thread_stop();
1022 }
1023 EXPORT_SYMBOL(ldlm_pools_fini);
1024 #endif /* __KERNEL__ */
1025
1026 #else /* !HAVE_LRU_RESIZE_SUPPORT */
1027 int ldlm_pool_setup(struct ldlm_pool *pl, __u32 limit)
1028 {
1029         return 0;
1030 }
1031 EXPORT_SYMBOL(ldlm_pool_setup);
1032
1033 int ldlm_pool_recalc(struct ldlm_pool *pl)
1034 {
1035         return 0;
1036 }
1037 EXPORT_SYMBOL(ldlm_pool_recalc);
1038
1039 int ldlm_pool_shrink(struct ldlm_pool *pl,
1040                      int nr, unsigned int gfp_mask)
1041 {
1042         return 0;
1043 }
1044 EXPORT_SYMBOL(ldlm_pool_shrink);
1045
1046 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
1047                    int idx, ldlm_side_t client)
1048 {
1049         return 0;
1050 }
1051 EXPORT_SYMBOL(ldlm_pool_init);
1052
1053 void ldlm_pool_fini(struct ldlm_pool *pl)
1054 {
1055         return;
1056 }
1057 EXPORT_SYMBOL(ldlm_pool_fini);
1058
1059 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
1060 {
1061         return;
1062 }
1063 EXPORT_SYMBOL(ldlm_pool_add);
1064
1065 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
1066 {
1067         return;
1068 }
1069 EXPORT_SYMBOL(ldlm_pool_del);
1070
1071 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
1072 {
1073         return 1;
1074 }
1075 EXPORT_SYMBOL(ldlm_pool_get_slv);
1076
1077 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv)
1078 {
1079         return;
1080 }
1081 EXPORT_SYMBOL(ldlm_pool_set_slv);
1082
1083 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
1084 {
1085         return 0;
1086 }
1087 EXPORT_SYMBOL(ldlm_pool_get_limit);
1088
1089 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
1090 {
1091         return;
1092 }
1093 EXPORT_SYMBOL(ldlm_pool_set_limit);
1094
1095 int ldlm_pools_init(void)
1096 {
1097         return 0;
1098 }
1099 EXPORT_SYMBOL(ldlm_pools_init);
1100
1101 void ldlm_pools_fini(void)
1102 {
1103         return;
1104 }
1105 EXPORT_SYMBOL(ldlm_pools_fini);
1106
1107 void ldlm_pools_wakeup(void)
1108 {
1109         return;
1110 }
1111 EXPORT_SYMBOL(ldlm_pools_wakeup);
1112
1113 void ldlm_pools_recalc(ldlm_side_t client)
1114 {
1115         return;
1116 }
1117 EXPORT_SYMBOL(ldlm_pools_recalc);
1118 #endif /* HAVE_LRU_RESIZE_SUPPORT */