Whamcloud - gitweb
LU-499 grant/cancel_rate are static when OST is idle
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011 Whamcloud, Inc.
33  *
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * lustre/ldlm/ldlm_pool.c
40  *
41  * Author: Yury Umanets <umka@clusterfs.com>
42  */
43
44 /*
45  * Idea of this code is rather simple. Each second, for each server namespace
46  * we have SLV - server lock volume which is calculated on current number of
47  * granted locks, grant speed for past period, etc - that is, locking load.
48  * This SLV number may be thought as a flow definition for simplicity. It is
49  * sent to clients with each occasion to let them know what is current load
50  * situation on the server. By default, at the beginning, SLV on server is
51  * set max value which is calculated as the following: allow to one client
52  * have all locks of limit ->pl_limit for 10h.
53  *
54  * Next, on clients, number of cached locks is not limited artificially in any
55  * way as it was before. Instead, client calculates CLV, that is, client lock
56  * volume for each lock and compares it with last SLV from the server. CLV is
57  * calculated as the number of locks in LRU * lock live time in seconds. If
58  * CLV > SLV - lock is canceled.
59  *
60  * Client has LVF, that is, lock volume factor which regulates how much sensitive
61  * client should be about last SLV from server. The higher LVF is the more locks
62  * will be canceled on client. Default value for it is 1. Setting LVF to 2 means
63  * that client will cancel locks 2 times faster.
64  *
65  * Locks on a client will be canceled more intensively in these cases:
66  * (1) if SLV is smaller, that is, load is higher on the server;
67  * (2) client has a lot of locks (the more locks are held by client, the bigger
68  *     chances that some of them should be canceled);
69  * (3) client has old locks (taken some time ago);
70  *
71  * Thus, according to flow paradigm that we use for better understanding SLV,
72  * CLV is the volume of particle in flow described by SLV. According to this,
73  * if flow is getting thinner, more and more particles become outside of it and
74  * as particles are locks, they should be canceled.
75  *
76  * General idea of this belongs to Vitaly Fertman (vitaly@clusterfs.com). Andreas
77  * Dilger (adilger@clusterfs.com) proposed few nice ideas like using LVF and many
78  * cleanups. Flow definition to allow more easy understanding of the logic belongs
79  * to Nikita Danilov (nikita@clusterfs.com) as well as many cleanups and fixes.
80  * And design and implementation are done by Yury Umanets (umka@clusterfs.com).
81  *
82  * Glossary for terms used:
83  *
84  * pl_limit - Number of allowed locks in pool. Applies to server and client
85  * side (tunable);
86  *
87  * pl_granted - Number of granted locks (calculated);
88  * pl_grant_rate - Number of granted locks for last T (calculated);
89  * pl_cancel_rate - Number of canceled locks for last T (calculated);
90  * pl_grant_speed - Grant speed (GR - CR) for last T (calculated);
91  * pl_grant_plan - Planned number of granted locks for next T (calculated);
92  * pl_server_lock_volume - Current server lock volume (calculated);
93  *
94  * As it may be seen from list above, we have few possible tunables which may
95  * affect behavior much. They all may be modified via proc. However, they also
96  * give a possibility for constructing few pre-defined behavior policies. If
97  * none of predefines is suitable for a working pattern being used, new one may
98  * be "constructed" via proc tunables.
99  */
100
101 #define DEBUG_SUBSYSTEM S_LDLM
102
103 #ifdef __KERNEL__
104 # include <lustre_dlm.h>
105 #else
106 # include <liblustre.h>
107 #endif
108
109 #include <cl_object.h>
110
111 #include <obd_class.h>
112 #include <obd_support.h>
113 #include "ldlm_internal.h"
114
115 #ifdef HAVE_LRU_RESIZE_SUPPORT
116
117 /*
118  * 50 ldlm locks for 1MB of RAM.
119  */
120 #define LDLM_POOL_HOST_L ((CFS_NUM_CACHEPAGES >> (20 - CFS_PAGE_SHIFT)) * 50)
121
122 /*
123  * Maximal possible grant step plan in %.
124  */
125 #define LDLM_POOL_MAX_GSP (30)
126
127 /*
128  * Minimal possible grant step plan in %.
129  */
130 #define LDLM_POOL_MIN_GSP (1)
131
132 /*
133  * This controls the speed of reaching LDLM_POOL_MAX_GSP
134  * with increasing thread period.
135  */
136 #define LDLM_POOL_GSP_STEP_SHIFT (2)
137
138 /*
139  * LDLM_POOL_GSP% of all locks is default GP.
140  */
141 #define LDLM_POOL_GP(L)   (((L) * LDLM_POOL_MAX_GSP) / 100)
142
143 /*
144  * Max age for locks on clients.
145  */
146 #define LDLM_POOL_MAX_AGE (36000)
147
148 /*
149  * The granularity of SLV calculation.
150  */
151 #define LDLM_POOL_SLV_SHIFT (10)
152
153 #ifdef __KERNEL__
154 extern cfs_proc_dir_entry_t *ldlm_ns_proc_dir;
155 #endif
156
157 static inline __u64 dru(__u64 val, __u32 shift, int round_up)
158 {
159         return (val + (round_up ? (1 << shift) - 1 : 0)) >> shift;
160 }
161
162 static inline __u64 ldlm_pool_slv_max(__u32 L)
163 {
164         /*
165          * Allow to have all locks for 1 client for 10 hrs.
166          * Formula is the following: limit * 10h / 1 client.
167          */
168         __u64 lim = (__u64)L *  LDLM_POOL_MAX_AGE / 1;
169         return lim;
170 }
171
172 static inline __u64 ldlm_pool_slv_min(__u32 L)
173 {
174         return 1;
175 }
176
177 enum {
178         LDLM_POOL_FIRST_STAT = 0,
179         LDLM_POOL_GRANTED_STAT = LDLM_POOL_FIRST_STAT,
180         LDLM_POOL_GRANT_STAT,
181         LDLM_POOL_CANCEL_STAT,
182         LDLM_POOL_GRANT_RATE_STAT,
183         LDLM_POOL_CANCEL_RATE_STAT,
184         LDLM_POOL_GRANT_PLAN_STAT,
185         LDLM_POOL_SLV_STAT,
186         LDLM_POOL_SHRINK_REQTD_STAT,
187         LDLM_POOL_SHRINK_FREED_STAT,
188         LDLM_POOL_RECALC_STAT,
189         LDLM_POOL_TIMING_STAT,
190         LDLM_POOL_LAST_STAT
191 };
192
193 static inline struct ldlm_namespace *ldlm_pl2ns(struct ldlm_pool *pl)
194 {
195         return container_of(pl, struct ldlm_namespace, ns_pool);
196 }
197
198 /**
199  * Calculates suggested grant_step in % of available locks for passed
200  * \a period. This is later used in grant_plan calculations.
201  */
202 static inline int ldlm_pool_t2gsp(unsigned int t)
203 {
204         /*
205          * This yields 1% grant step for anything below LDLM_POOL_GSP_STEP
206          * and up to 30% for anything higher than LDLM_POOL_GSP_STEP.
207          *
208          * How this will affect execution is the following:
209          *
210          * - for thread period 1s we will have grant_step 1% which good from
211          * pov of taking some load off from server and push it out to clients.
212          * This is like that because 1% for grant_step means that server will
213          * not allow clients to get lots of locks in short period of time and
214          * keep all old locks in their caches. Clients will always have to
215          * get some locks back if they want to take some new;
216          *
217          * - for thread period 10s (which is default) we will have 23% which
218          * means that clients will have enough of room to take some new locks
219          * without getting some back. All locks from this 23% which were not
220          * taken by clients in current period will contribute in SLV growing.
221          * SLV growing means more locks cached on clients until limit or grant
222          * plan is reached.
223          */
224         return LDLM_POOL_MAX_GSP -
225                 ((LDLM_POOL_MAX_GSP - LDLM_POOL_MIN_GSP) >>
226                  (t >> LDLM_POOL_GSP_STEP_SHIFT));
227 }
228
229 /**
230  * Recalculates next grant limit on passed \a pl.
231  *
232  * \pre ->pl_lock is locked.
233  */
234 static void ldlm_pool_recalc_grant_plan(struct ldlm_pool *pl)
235 {
236         int granted, grant_step, limit;
237
238         limit = ldlm_pool_get_limit(pl);
239         granted = cfs_atomic_read(&pl->pl_granted);
240
241         grant_step = ldlm_pool_t2gsp(pl->pl_recalc_period);
242         grant_step = ((limit - granted) * grant_step) / 100;
243         pl->pl_grant_plan = granted + grant_step;
244         limit = (limit * 5) >> 2;
245         if (pl->pl_grant_plan > limit)
246                 pl->pl_grant_plan = limit;
247 }
248
249 /**
250  * Recalculates next SLV on passed \a pl.
251  *
252  * \pre ->pl_lock is locked.
253  */
254 static void ldlm_pool_recalc_slv(struct ldlm_pool *pl)
255 {
256         int granted;
257         int grant_plan;
258         int round_up;
259         __u64 slv;
260         __u64 slv_factor;
261         __u64 grant_usage;
262         __u32 limit;
263
264         slv = pl->pl_server_lock_volume;
265         grant_plan = pl->pl_grant_plan;
266         limit = ldlm_pool_get_limit(pl);
267         granted = cfs_atomic_read(&pl->pl_granted);
268         round_up = granted < limit;
269
270         grant_usage = max_t(int, limit - (granted - grant_plan), 1);
271
272         /*
273          * Find out SLV change factor which is the ratio of grant usage
274          * from limit. SLV changes as fast as the ratio of grant plan
275          * consumption. The more locks from grant plan are not consumed
276          * by clients in last interval (idle time), the faster grows
277          * SLV. And the opposite, the more grant plan is over-consumed
278          * (load time) the faster drops SLV.
279          */
280         slv_factor = (grant_usage << LDLM_POOL_SLV_SHIFT);
281         do_div(slv_factor, limit);
282         slv = slv * slv_factor;
283         slv = dru(slv, LDLM_POOL_SLV_SHIFT, round_up);
284
285         if (slv > ldlm_pool_slv_max(limit)) {
286                 slv = ldlm_pool_slv_max(limit);
287         } else if (slv < ldlm_pool_slv_min(limit)) {
288                 slv = ldlm_pool_slv_min(limit);
289         }
290
291         pl->pl_server_lock_volume = slv;
292 }
293
294 /**
295  * Recalculates next stats on passed \a pl.
296  *
297  * \pre ->pl_lock is locked.
298  */
299 static void ldlm_pool_recalc_stats(struct ldlm_pool *pl)
300 {
301         int grant_plan = pl->pl_grant_plan;
302         __u64 slv = pl->pl_server_lock_volume;
303         int granted = cfs_atomic_read(&pl->pl_granted);
304         int grant_rate = cfs_atomic_read(&pl->pl_grant_rate);
305         int cancel_rate = cfs_atomic_read(&pl->pl_cancel_rate);
306
307         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_SLV_STAT,
308                             slv);
309         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
310                             granted);
311         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
312                             grant_rate);
313         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
314                             grant_plan);
315         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
316                             cancel_rate);
317 }
318
319 /**
320  * Sets current SLV into obd accessible via ldlm_pl2ns(pl)->ns_obd.
321  */
322 static void ldlm_srv_pool_push_slv(struct ldlm_pool *pl)
323 {
324         struct obd_device *obd;
325
326         /*
327          * Set new SLV in obd field for using it later without accessing the
328          * pool. This is required to avoid race between sending reply to client
329          * with new SLV and cleanup server stack in which we can't guarantee
330          * that namespace is still alive. We know only that obd is alive as
331          * long as valid export is alive.
332          */
333         obd = ldlm_pl2ns(pl)->ns_obd;
334         LASSERT(obd != NULL);
335         cfs_write_lock(&obd->obd_pool_lock);
336         obd->obd_pool_slv = pl->pl_server_lock_volume;
337         cfs_write_unlock(&obd->obd_pool_lock);
338 }
339
340 /**
341  * Recalculates all pool fields on passed \a pl.
342  *
343  * \pre ->pl_lock is not locked.
344  */
345 static int ldlm_srv_pool_recalc(struct ldlm_pool *pl)
346 {
347         time_t recalc_interval_sec;
348         ENTRY;
349
350         recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
351         if (recalc_interval_sec < pl->pl_recalc_period)
352                 RETURN(0);
353
354         cfs_spin_lock(&pl->pl_lock);
355         recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
356         if (recalc_interval_sec < pl->pl_recalc_period) {
357                 cfs_spin_unlock(&pl->pl_lock);
358                 RETURN(0);
359         }
360         /*
361          * Recalc SLV after last period. This should be done
362          * _before_ recalculating new grant plan.
363          */
364         ldlm_pool_recalc_slv(pl);
365
366         /*
367          * Make sure that pool informed obd of last SLV changes.
368          */
369         ldlm_srv_pool_push_slv(pl);
370
371         /*
372          * Update grant_plan for new period.
373          */
374         ldlm_pool_recalc_grant_plan(pl);
375
376         pl->pl_recalc_time = cfs_time_current_sec();
377         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
378                             recalc_interval_sec);
379         cfs_spin_unlock(&pl->pl_lock);
380         RETURN(0);
381 }
382
383 /**
384  * This function is used on server side as main entry point for memory
385  * pressure handling. It decreases SLV on \a pl according to passed
386  * \a nr and \a gfp_mask.
387  *
388  * Our goal here is to decrease SLV such a way that clients hold \a nr
389  * locks smaller in next 10h.
390  */
391 static int ldlm_srv_pool_shrink(struct ldlm_pool *pl,
392                                 int nr, unsigned int gfp_mask)
393 {
394         __u32 limit;
395
396         /*
397          * VM is asking how many entries may be potentially freed.
398          */
399         if (nr == 0)
400                 return cfs_atomic_read(&pl->pl_granted);
401
402         /*
403          * Client already canceled locks but server is already in shrinker
404          * and can't cancel anything. Let's catch this race.
405          */
406         if (cfs_atomic_read(&pl->pl_granted) == 0)
407                 RETURN(0);
408
409         cfs_spin_lock(&pl->pl_lock);
410
411         /*
412          * We want shrinker to possibly cause cancellation of @nr locks from
413          * clients or grant approximately @nr locks smaller next intervals.
414          *
415          * This is why we decreased SLV by @nr. This effect will only be as
416          * long as one re-calc interval (1s these days) and this should be
417          * enough to pass this decreased SLV to all clients. On next recalc
418          * interval pool will either increase SLV if locks load is not high
419          * or will keep on same level or even decrease again, thus, shrinker
420          * decreased SLV will affect next recalc intervals and this way will
421          * make locking load lower.
422          */
423         if (nr < pl->pl_server_lock_volume) {
424                 pl->pl_server_lock_volume = pl->pl_server_lock_volume - nr;
425         } else {
426                 limit = ldlm_pool_get_limit(pl);
427                 pl->pl_server_lock_volume = ldlm_pool_slv_min(limit);
428         }
429
430         /*
431          * Make sure that pool informed obd of last SLV changes.
432          */
433         ldlm_srv_pool_push_slv(pl);
434         cfs_spin_unlock(&pl->pl_lock);
435
436         /*
437          * We did not really free any memory here so far, it only will be
438          * freed later may be, so that we return 0 to not confuse VM.
439          */
440         return 0;
441 }
442
443 /**
444  * Setup server side pool \a pl with passed \a limit.
445  */
446 static int ldlm_srv_pool_setup(struct ldlm_pool *pl, int limit)
447 {
448         struct obd_device *obd;
449
450         obd = ldlm_pl2ns(pl)->ns_obd;
451         LASSERT(obd != NULL && obd != LP_POISON);
452         LASSERT(obd->obd_type != LP_POISON);
453         cfs_write_lock(&obd->obd_pool_lock);
454         obd->obd_pool_limit = limit;
455         cfs_write_unlock(&obd->obd_pool_lock);
456
457         ldlm_pool_set_limit(pl, limit);
458         return 0;
459 }
460
461 /**
462  * Sets SLV and Limit from ldlm_pl2ns(pl)->ns_obd tp passed \a pl.
463  */
464 static void ldlm_cli_pool_pop_slv(struct ldlm_pool *pl)
465 {
466         struct obd_device *obd;
467
468         /*
469          * Get new SLV and Limit from obd which is updated with coming
470          * RPCs.
471          */
472         obd = ldlm_pl2ns(pl)->ns_obd;
473         LASSERT(obd != NULL);
474         cfs_read_lock(&obd->obd_pool_lock);
475         pl->pl_server_lock_volume = obd->obd_pool_slv;
476         ldlm_pool_set_limit(pl, obd->obd_pool_limit);
477         cfs_read_unlock(&obd->obd_pool_lock);
478 }
479
480 /**
481  * Recalculates client size pool \a pl according to current SLV and Limit.
482  */
483 static int ldlm_cli_pool_recalc(struct ldlm_pool *pl)
484 {
485         time_t recalc_interval_sec;
486         ENTRY;
487
488         recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
489         if (recalc_interval_sec < pl->pl_recalc_period)
490                 RETURN(0);
491
492         cfs_spin_lock(&pl->pl_lock);
493         /*
494          * Check if we need to recalc lists now.
495          */
496         recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
497         if (recalc_interval_sec < pl->pl_recalc_period) {
498                 cfs_spin_unlock(&pl->pl_lock);
499                 RETURN(0);
500         }
501
502         /*
503          * Make sure that pool knows last SLV and Limit from obd.
504          */
505         ldlm_cli_pool_pop_slv(pl);
506
507         pl->pl_recalc_time = cfs_time_current_sec();
508         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
509                             recalc_interval_sec);
510         cfs_spin_unlock(&pl->pl_lock);
511
512         /*
513          * Do not cancel locks in case lru resize is disabled for this ns.
514          */
515         if (!ns_connect_lru_resize(ldlm_pl2ns(pl)))
516                 RETURN(0);
517
518         /*
519          * In the time of canceling locks on client we do not need to maintain
520          * sharp timing, we only want to cancel locks asap according to new SLV.
521          * It may be called when SLV has changed much, this is why we do not
522          * take into account pl->pl_recalc_time here.
523          */
524         RETURN(ldlm_cancel_lru(ldlm_pl2ns(pl), 0, LDLM_SYNC, 
525                                LDLM_CANCEL_LRUR));
526 }
527
528 /**
529  * This function is main entry point for memory pressure handling on client
530  * side.  Main goal of this function is to cancel some number of locks on
531  * passed \a pl according to \a nr and \a gfp_mask.
532  */
533 static int ldlm_cli_pool_shrink(struct ldlm_pool *pl,
534                                 int nr, unsigned int gfp_mask)
535 {
536         struct ldlm_namespace *ns;
537         int canceled = 0, unused;
538
539         ns = ldlm_pl2ns(pl);
540
541         /*
542          * Do not cancel locks in case lru resize is disabled for this ns.
543          */
544         if (!ns_connect_lru_resize(ns))
545                 RETURN(0);
546
547         /*
548          * Make sure that pool knows last SLV and Limit from obd.
549          */
550         ldlm_cli_pool_pop_slv(pl);
551
552         cfs_spin_lock(&ns->ns_lock);
553         unused = ns->ns_nr_unused;
554         cfs_spin_unlock(&ns->ns_lock);
555         
556         if (nr) {
557                 canceled = ldlm_cancel_lru(ns, nr, LDLM_ASYNC,
558                                            LDLM_CANCEL_SHRINK);
559         }
560 #ifdef __KERNEL__
561         /*
562          * Return the number of potentially reclaimable locks.
563          */
564         return ((unused - canceled) / 100) * sysctl_vfs_cache_pressure;
565 #else
566         return unused - canceled;
567 #endif
568 }
569
570 struct ldlm_pool_ops ldlm_srv_pool_ops = {
571         .po_recalc = ldlm_srv_pool_recalc,
572         .po_shrink = ldlm_srv_pool_shrink,
573         .po_setup  = ldlm_srv_pool_setup
574 };
575
576 struct ldlm_pool_ops ldlm_cli_pool_ops = {
577         .po_recalc = ldlm_cli_pool_recalc,
578         .po_shrink = ldlm_cli_pool_shrink
579 };
580
581 /**
582  * Pool recalc wrapper. Will call either client or server pool recalc callback
583  * depending what pool \a pl is used.
584  */
585 int ldlm_pool_recalc(struct ldlm_pool *pl)
586 {
587         time_t recalc_interval_sec;
588         int count;
589
590         recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
591         if (recalc_interval_sec <= 0)
592                 goto recalc;
593
594         cfs_spin_lock(&pl->pl_lock);
595         recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
596         if (recalc_interval_sec > 0) {
597                 /*
598                  * Update pool statistics every 1s.
599                  */
600                 ldlm_pool_recalc_stats(pl);
601
602                 /*
603                  * Zero out all rates and speed for the last period.
604                  */
605                 cfs_atomic_set(&pl->pl_grant_rate, 0);
606                 cfs_atomic_set(&pl->pl_cancel_rate, 0);
607         }
608         cfs_spin_unlock(&pl->pl_lock);
609
610  recalc:
611         if (pl->pl_ops->po_recalc != NULL) {
612                 count = pl->pl_ops->po_recalc(pl);
613                 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_RECALC_STAT,
614                                     count);
615                 return count;
616         }
617
618         return 0;
619 }
620 EXPORT_SYMBOL(ldlm_pool_recalc);
621
622 /**
623  * Pool shrink wrapper. Will call either client or server pool recalc callback
624  * depending what pool \a pl is used.
625  */
626 int ldlm_pool_shrink(struct ldlm_pool *pl, int nr,
627                      unsigned int gfp_mask)
628 {
629         int cancel = 0;
630
631         if (pl->pl_ops->po_shrink != NULL) {
632                 cancel = pl->pl_ops->po_shrink(pl, nr, gfp_mask);
633                 if (nr > 0) {
634                         lprocfs_counter_add(pl->pl_stats,
635                                             LDLM_POOL_SHRINK_REQTD_STAT,
636                                             nr);
637                         lprocfs_counter_add(pl->pl_stats,
638                                             LDLM_POOL_SHRINK_FREED_STAT,
639                                             cancel);
640                         CDEBUG(D_DLMTRACE, "%s: request to shrink %d locks, "
641                                "shrunk %d\n", pl->pl_name, nr, cancel);
642                 }
643         }
644         return cancel;
645 }
646 EXPORT_SYMBOL(ldlm_pool_shrink);
647
648 /**
649  * Pool setup wrapper. Will call either client or server pool recalc callback
650  * depending what pool \a pl is used.
651  *
652  * Sets passed \a limit into pool \a pl.
653  */
654 int ldlm_pool_setup(struct ldlm_pool *pl, int limit)
655 {
656         if (pl->pl_ops->po_setup != NULL)
657                 return(pl->pl_ops->po_setup(pl, limit));
658         return 0;
659 }
660 EXPORT_SYMBOL(ldlm_pool_setup);
661
662 #ifdef __KERNEL__
663 static int lprocfs_rd_pool_state(char *page, char **start, off_t off,
664                                  int count, int *eof, void *data)
665 {
666         int granted, grant_rate, cancel_rate, grant_step;
667         int nr = 0, grant_speed, grant_plan, lvf;
668         struct ldlm_pool *pl = data;
669         __u64 slv, clv;
670         __u32 limit;
671
672         cfs_spin_lock(&pl->pl_lock);
673         slv = pl->pl_server_lock_volume;
674         clv = pl->pl_client_lock_volume;
675         limit = ldlm_pool_get_limit(pl);
676         grant_plan = pl->pl_grant_plan;
677         granted = cfs_atomic_read(&pl->pl_granted);
678         grant_rate = cfs_atomic_read(&pl->pl_grant_rate);
679         cancel_rate = cfs_atomic_read(&pl->pl_cancel_rate);
680         grant_speed = grant_rate - cancel_rate;
681         lvf = cfs_atomic_read(&pl->pl_lock_volume_factor);
682         grant_step = ldlm_pool_t2gsp(pl->pl_recalc_period);
683         cfs_spin_unlock(&pl->pl_lock);
684
685         nr += snprintf(page + nr, count - nr, "LDLM pool state (%s):\n",
686                        pl->pl_name);
687         nr += snprintf(page + nr, count - nr, "  SLV: "LPU64"\n", slv);
688         nr += snprintf(page + nr, count - nr, "  CLV: "LPU64"\n", clv);
689         nr += snprintf(page + nr, count - nr, "  LVF: %d\n", lvf);
690
691         if (ns_is_server(ldlm_pl2ns(pl))) {
692                 nr += snprintf(page + nr, count - nr, "  GSP: %d%%\n",
693                                grant_step);
694                 nr += snprintf(page + nr, count - nr, "  GP:  %d\n",
695                                grant_plan);
696         }
697         nr += snprintf(page + nr, count - nr, "  GR:  %d\n",
698                        grant_rate);
699         nr += snprintf(page + nr, count - nr, "  CR:  %d\n",
700                        cancel_rate);
701         nr += snprintf(page + nr, count - nr, "  GS:  %d\n",
702                        grant_speed);
703         nr += snprintf(page + nr, count - nr, "  G:   %d\n",
704                        granted);
705         nr += snprintf(page + nr, count - nr, "  L:   %d\n",
706                        limit);
707         return nr;
708 }
709
710 static int lprocfs_rd_grant_speed(char *page, char **start, off_t off,
711                                   int count, int *eof, void *data)
712 {
713         struct ldlm_pool *pl = data;
714         int               grant_speed;
715
716         cfs_spin_lock(&pl->pl_lock);
717         /* serialize with ldlm_pool_recalc */
718         grant_speed = cfs_atomic_read(&pl->pl_grant_rate) -
719                       cfs_atomic_read(&pl->pl_cancel_rate);
720         cfs_spin_unlock(&pl->pl_lock);
721         return lprocfs_rd_uint(page, start, off, count, eof, &grant_speed);
722 }
723
724 LDLM_POOL_PROC_READER(grant_plan, int);
725 LDLM_POOL_PROC_READER(recalc_period, int);
726 LDLM_POOL_PROC_WRITER(recalc_period, int);
727
728 static int ldlm_pool_proc_init(struct ldlm_pool *pl)
729 {
730         struct ldlm_namespace *ns = ldlm_pl2ns(pl);
731         struct proc_dir_entry *parent_ns_proc;
732         struct lprocfs_vars pool_vars[2];
733         char *var_name = NULL;
734         int rc = 0;
735         ENTRY;
736
737         OBD_ALLOC(var_name, MAX_STRING_SIZE + 1);
738         if (!var_name)
739                 RETURN(-ENOMEM);
740
741         parent_ns_proc = lprocfs_srch(ldlm_ns_proc_dir,
742                                       ldlm_ns_name(ns));
743         if (parent_ns_proc == NULL) {
744                 CERROR("%s: proc entry is not initialized\n",
745                        ldlm_ns_name(ns));
746                 GOTO(out_free_name, rc = -EINVAL);
747         }
748         pl->pl_proc_dir = lprocfs_register("pool", parent_ns_proc,
749                                            NULL, NULL);
750         if (IS_ERR(pl->pl_proc_dir)) {
751                 CERROR("LProcFS failed in ldlm-pool-init\n");
752                 rc = PTR_ERR(pl->pl_proc_dir);
753                 GOTO(out_free_name, rc);
754         }
755
756         var_name[MAX_STRING_SIZE] = '\0';
757         memset(pool_vars, 0, sizeof(pool_vars));
758         pool_vars[0].name = var_name;
759
760         snprintf(var_name, MAX_STRING_SIZE, "server_lock_volume");
761         pool_vars[0].data = &pl->pl_server_lock_volume;
762         pool_vars[0].read_fptr = lprocfs_rd_u64;
763         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
764
765         snprintf(var_name, MAX_STRING_SIZE, "limit");
766         pool_vars[0].data = &pl->pl_limit;
767         pool_vars[0].read_fptr = lprocfs_rd_atomic;
768         pool_vars[0].write_fptr = lprocfs_wr_atomic;
769         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
770
771         snprintf(var_name, MAX_STRING_SIZE, "granted");
772         pool_vars[0].data = &pl->pl_granted;
773         pool_vars[0].read_fptr = lprocfs_rd_atomic;
774         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
775
776         snprintf(var_name, MAX_STRING_SIZE, "grant_speed");
777         pool_vars[0].data = pl;
778         pool_vars[0].read_fptr = lprocfs_rd_grant_speed;
779         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
780
781         snprintf(var_name, MAX_STRING_SIZE, "cancel_rate");
782         pool_vars[0].data = &pl->pl_cancel_rate;
783         pool_vars[0].read_fptr = lprocfs_rd_atomic;
784         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
785
786         snprintf(var_name, MAX_STRING_SIZE, "grant_rate");
787         pool_vars[0].data = &pl->pl_grant_rate;
788         pool_vars[0].read_fptr = lprocfs_rd_atomic;
789         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
790
791         snprintf(var_name, MAX_STRING_SIZE, "grant_plan");
792         pool_vars[0].data = pl;
793         pool_vars[0].read_fptr = lprocfs_rd_grant_plan;
794         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
795
796         snprintf(var_name, MAX_STRING_SIZE, "recalc_period");
797         pool_vars[0].data = pl;
798         pool_vars[0].read_fptr = lprocfs_rd_recalc_period;
799         pool_vars[0].write_fptr = lprocfs_wr_recalc_period;
800         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
801
802         snprintf(var_name, MAX_STRING_SIZE, "lock_volume_factor");
803         pool_vars[0].data = &pl->pl_lock_volume_factor;
804         pool_vars[0].read_fptr = lprocfs_rd_atomic;
805         pool_vars[0].write_fptr = lprocfs_wr_atomic;
806         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
807
808         snprintf(var_name, MAX_STRING_SIZE, "state");
809         pool_vars[0].data = pl;
810         pool_vars[0].read_fptr = lprocfs_rd_pool_state;
811         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
812
813         pl->pl_stats = lprocfs_alloc_stats(LDLM_POOL_LAST_STAT -
814                                            LDLM_POOL_FIRST_STAT, 0);
815         if (!pl->pl_stats)
816                 GOTO(out_free_name, rc = -ENOMEM);
817
818         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
819                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
820                              "granted", "locks");
821         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_STAT,
822                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
823                              "grant", "locks");
824         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_STAT,
825                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
826                              "cancel", "locks");
827         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
828                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
829                              "grant_rate", "locks/s");
830         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
831                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
832                              "cancel_rate", "locks/s");
833         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
834                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
835                              "grant_plan", "locks/s");
836         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SLV_STAT,
837                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
838                              "slv", "slv");
839         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_REQTD_STAT,
840                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
841                              "shrink_request", "locks");
842         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_FREED_STAT,
843                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
844                              "shrink_freed", "locks");
845         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_RECALC_STAT,
846                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
847                              "recalc_freed", "locks");
848         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_TIMING_STAT,
849                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
850                              "recalc_timing", "sec");
851         lprocfs_register_stats(pl->pl_proc_dir, "stats", pl->pl_stats);
852
853         EXIT;
854 out_free_name:
855         OBD_FREE(var_name, MAX_STRING_SIZE + 1);
856         return rc;
857 }
858
859 static void ldlm_pool_proc_fini(struct ldlm_pool *pl)
860 {
861         if (pl->pl_stats != NULL) {
862                 lprocfs_free_stats(&pl->pl_stats);
863                 pl->pl_stats = NULL;
864         }
865         if (pl->pl_proc_dir != NULL) {
866                 lprocfs_remove(&pl->pl_proc_dir);
867                 pl->pl_proc_dir = NULL;
868         }
869 }
870 #else /* !__KERNEL__*/
871 #define ldlm_pool_proc_init(pl) (0)
872 #define ldlm_pool_proc_fini(pl) while (0) {}
873 #endif
874
875 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
876                    int idx, ldlm_side_t client)
877 {
878         int rc;
879         ENTRY;
880
881         cfs_spin_lock_init(&pl->pl_lock);
882         cfs_atomic_set(&pl->pl_granted, 0);
883         pl->pl_recalc_time = cfs_time_current_sec();
884         cfs_atomic_set(&pl->pl_lock_volume_factor, 1);
885
886         cfs_atomic_set(&pl->pl_grant_rate, 0);
887         cfs_atomic_set(&pl->pl_cancel_rate, 0);
888         pl->pl_grant_plan = LDLM_POOL_GP(LDLM_POOL_HOST_L);
889
890         snprintf(pl->pl_name, sizeof(pl->pl_name), "ldlm-pool-%s-%d",
891                  ldlm_ns_name(ns), idx);
892
893         if (client == LDLM_NAMESPACE_SERVER) {
894                 pl->pl_ops = &ldlm_srv_pool_ops;
895                 ldlm_pool_set_limit(pl, LDLM_POOL_HOST_L);
896                 pl->pl_recalc_period = LDLM_POOL_SRV_DEF_RECALC_PERIOD;
897                 pl->pl_server_lock_volume = ldlm_pool_slv_max(LDLM_POOL_HOST_L);
898         } else {
899                 ldlm_pool_set_limit(pl, 1);
900                 pl->pl_server_lock_volume = 0;
901                 pl->pl_ops = &ldlm_cli_pool_ops;
902                 pl->pl_recalc_period = LDLM_POOL_CLI_DEF_RECALC_PERIOD;
903         }
904         pl->pl_client_lock_volume = 0;
905         rc = ldlm_pool_proc_init(pl);
906         if (rc)
907                 RETURN(rc);
908
909         CDEBUG(D_DLMTRACE, "Lock pool %s is initialized\n", pl->pl_name);
910
911         RETURN(rc);
912 }
913 EXPORT_SYMBOL(ldlm_pool_init);
914
915 void ldlm_pool_fini(struct ldlm_pool *pl)
916 {
917         ENTRY;
918         ldlm_pool_proc_fini(pl);
919
920         /*
921          * Pool should not be used after this point. We can't free it here as
922          * it lives in struct ldlm_namespace, but still interested in catching
923          * any abnormal using cases.
924          */
925         POISON(pl, 0x5a, sizeof(*pl));
926         EXIT;
927 }
928 EXPORT_SYMBOL(ldlm_pool_fini);
929
930 /**
931  * Add new taken ldlm lock \a lock into pool \a pl accounting.
932  */
933 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
934 {
935         /*
936          * FLOCK locks are special in a sense that they are almost never
937          * cancelled, instead special kind of lock is used to drop them.
938          * also there is no LRU for flock locks, so no point in tracking
939          * them anyway.
940          */
941         if (lock->l_resource->lr_type == LDLM_FLOCK)
942                 return;
943
944         cfs_atomic_inc(&pl->pl_granted);
945         cfs_atomic_inc(&pl->pl_grant_rate);
946         lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_GRANT_STAT);
947         /*
948          * Do not do pool recalc for client side as all locks which
949          * potentially may be canceled has already been packed into
950          * enqueue/cancel rpc. Also we do not want to run out of stack
951          * with too long call paths.
952          */
953         if (ns_is_server(ldlm_pl2ns(pl)))
954                 ldlm_pool_recalc(pl);
955 }
956 EXPORT_SYMBOL(ldlm_pool_add);
957
958 /**
959  * Remove ldlm lock \a lock from pool \a pl accounting.
960  */
961 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
962 {
963         /*
964          * Filter out FLOCK locks. Read above comment in ldlm_pool_add().
965          */
966         if (lock->l_resource->lr_type == LDLM_FLOCK)
967                 return;
968
969         LASSERT(cfs_atomic_read(&pl->pl_granted) > 0);
970         cfs_atomic_dec(&pl->pl_granted);
971         cfs_atomic_inc(&pl->pl_cancel_rate);
972
973         lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_CANCEL_STAT);
974
975         if (ns_is_server(ldlm_pl2ns(pl)))
976                 ldlm_pool_recalc(pl);
977 }
978 EXPORT_SYMBOL(ldlm_pool_del);
979
980 /**
981  * Returns current \a pl SLV.
982  *
983  * \pre ->pl_lock is not locked.
984  */
985 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
986 {
987         __u64 slv;
988         cfs_spin_lock(&pl->pl_lock);
989         slv = pl->pl_server_lock_volume;
990         cfs_spin_unlock(&pl->pl_lock);
991         return slv;
992 }
993 EXPORT_SYMBOL(ldlm_pool_get_slv);
994
995 /**
996  * Sets passed \a slv to \a pl.
997  *
998  * \pre ->pl_lock is not locked.
999  */
1000 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv)
1001 {
1002         cfs_spin_lock(&pl->pl_lock);
1003         pl->pl_server_lock_volume = slv;
1004         cfs_spin_unlock(&pl->pl_lock);
1005 }
1006 EXPORT_SYMBOL(ldlm_pool_set_slv);
1007
1008 /**
1009  * Returns current \a pl CLV.
1010  *
1011  * \pre ->pl_lock is not locked.
1012  */
1013 __u64 ldlm_pool_get_clv(struct ldlm_pool *pl)
1014 {
1015         __u64 slv;
1016         cfs_spin_lock(&pl->pl_lock);
1017         slv = pl->pl_client_lock_volume;
1018         cfs_spin_unlock(&pl->pl_lock);
1019         return slv;
1020 }
1021 EXPORT_SYMBOL(ldlm_pool_get_clv);
1022
1023 /**
1024  * Sets passed \a clv to \a pl.
1025  *
1026  * \pre ->pl_lock is not locked.
1027  */
1028 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv)
1029 {
1030         cfs_spin_lock(&pl->pl_lock);
1031         pl->pl_client_lock_volume = clv;
1032         cfs_spin_unlock(&pl->pl_lock);
1033 }
1034 EXPORT_SYMBOL(ldlm_pool_set_clv);
1035
1036 /**
1037  * Returns current \a pl limit.
1038  */
1039 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
1040 {
1041         return cfs_atomic_read(&pl->pl_limit);
1042 }
1043 EXPORT_SYMBOL(ldlm_pool_get_limit);
1044
1045 /**
1046  * Sets passed \a limit to \a pl.
1047  */
1048 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
1049 {
1050         cfs_atomic_set(&pl->pl_limit, limit);
1051 }
1052 EXPORT_SYMBOL(ldlm_pool_set_limit);
1053
1054 /**
1055  * Returns current LVF from \a pl.
1056  */
1057 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
1058 {
1059         return cfs_atomic_read(&pl->pl_lock_volume_factor);
1060 }
1061 EXPORT_SYMBOL(ldlm_pool_get_lvf);
1062
1063 #ifdef __KERNEL__
1064 static int ldlm_pool_granted(struct ldlm_pool *pl)
1065 {
1066         return cfs_atomic_read(&pl->pl_granted);
1067 }
1068
1069 static struct ptlrpc_thread *ldlm_pools_thread;
1070 static struct cfs_shrinker *ldlm_pools_srv_shrinker;
1071 static struct cfs_shrinker *ldlm_pools_cli_shrinker;
1072 static cfs_completion_t ldlm_pools_comp;
1073
1074 /*
1075  * Cancel \a nr locks from all namespaces (if possible). Returns number of
1076  * cached locks after shrink is finished. All namespaces are asked to
1077  * cancel approximately equal amount of locks to keep balancing.
1078  */
1079 static int ldlm_pools_shrink(ldlm_side_t client, int nr,
1080                              unsigned int gfp_mask)
1081 {
1082         int total = 0, cached = 0, nr_ns;
1083         struct ldlm_namespace *ns;
1084         void *cookie;
1085
1086         if (client == LDLM_NAMESPACE_CLIENT && nr != 0 &&
1087             !(gfp_mask & __GFP_FS))
1088                 return -1;
1089
1090         CDEBUG(D_DLMTRACE, "Request to shrink %d %s locks from all pools\n",
1091                nr, client == LDLM_NAMESPACE_CLIENT ? "client" : "server");
1092
1093         cookie = cl_env_reenter();
1094
1095         /*
1096          * Find out how many resources we may release.
1097          */
1098         for (nr_ns = cfs_atomic_read(ldlm_namespace_nr(client));
1099              nr_ns > 0; nr_ns--)
1100         {
1101                 cfs_mutex_down(ldlm_namespace_lock(client));
1102                 if (cfs_list_empty(ldlm_namespace_list(client))) {
1103                         cfs_mutex_up(ldlm_namespace_lock(client));
1104                         cl_env_reexit(cookie);
1105                         return 0;
1106                 }
1107                 ns = ldlm_namespace_first_locked(client);
1108                 ldlm_namespace_get(ns);
1109                 ldlm_namespace_move_locked(ns, client);
1110                 cfs_mutex_up(ldlm_namespace_lock(client));
1111                 total += ldlm_pool_shrink(&ns->ns_pool, 0, gfp_mask);
1112                 ldlm_namespace_put(ns);
1113         }
1114
1115         if (nr == 0 || total == 0) {
1116                 cl_env_reexit(cookie);
1117                 return total;
1118         }
1119
1120         /*
1121          * Shrink at least ldlm_namespace_nr(client) namespaces.
1122          */
1123         for (nr_ns = cfs_atomic_read(ldlm_namespace_nr(client));
1124              nr_ns > 0; nr_ns--)
1125         {
1126                 int cancel, nr_locks;
1127
1128                 /*
1129                  * Do not call shrink under ldlm_namespace_lock(client)
1130                  */
1131                 cfs_mutex_down(ldlm_namespace_lock(client));
1132                 if (cfs_list_empty(ldlm_namespace_list(client))) {
1133                         cfs_mutex_up(ldlm_namespace_lock(client));
1134                         /*
1135                          * If list is empty, we can't return any @cached > 0,
1136                          * that probably would cause needless shrinker
1137                          * call.
1138                          */
1139                         cached = 0;
1140                         break;
1141                 }
1142                 ns = ldlm_namespace_first_locked(client);
1143                 ldlm_namespace_get(ns);
1144                 ldlm_namespace_move_locked(ns, client);
1145                 cfs_mutex_up(ldlm_namespace_lock(client));
1146
1147                 nr_locks = ldlm_pool_granted(&ns->ns_pool);
1148                 cancel = 1 + nr_locks * nr / total;
1149                 ldlm_pool_shrink(&ns->ns_pool, cancel, gfp_mask);
1150                 cached += ldlm_pool_granted(&ns->ns_pool);
1151                 ldlm_namespace_put(ns);
1152         }
1153         cl_env_reexit(cookie);
1154         return cached;
1155 }
1156
1157 static int ldlm_pools_srv_shrink(SHRINKER_FIRST_ARG int nr_to_scan,
1158                                  unsigned int gfp_mask)
1159 {
1160         return ldlm_pools_shrink(LDLM_NAMESPACE_SERVER, nr_to_scan, gfp_mask);
1161 }
1162
1163 static int ldlm_pools_cli_shrink(SHRINKER_FIRST_ARG int nr_to_scan,
1164                                  unsigned int gfp_mask)
1165 {
1166         return ldlm_pools_shrink(LDLM_NAMESPACE_CLIENT, nr_to_scan, gfp_mask);
1167 }
1168
1169 void ldlm_pools_recalc(ldlm_side_t client)
1170 {
1171         __u32 nr_l = 0, nr_p = 0, l;
1172         struct ldlm_namespace *ns;
1173         int nr, equal = 0;
1174
1175         /*
1176          * No need to setup pool limit for client pools.
1177          */
1178         if (client == LDLM_NAMESPACE_SERVER) {
1179                 /*
1180                  * Check all modest namespaces first.
1181                  */
1182                 cfs_mutex_down(ldlm_namespace_lock(client));
1183                 cfs_list_for_each_entry(ns, ldlm_namespace_list(client),
1184                                         ns_list_chain)
1185                 {
1186                         if (ns->ns_appetite != LDLM_NAMESPACE_MODEST)
1187                                 continue;
1188
1189                         l = ldlm_pool_granted(&ns->ns_pool);
1190                         if (l == 0)
1191                                 l = 1;
1192
1193                         /*
1194                          * Set the modest pools limit equal to their avg granted
1195                          * locks + ~6%.
1196                          */
1197                         l += dru(l, LDLM_POOLS_MODEST_MARGIN_SHIFT, 0);
1198                         ldlm_pool_setup(&ns->ns_pool, l);
1199                         nr_l += l;
1200                         nr_p++;
1201                 }
1202
1203                 /*
1204                  * Make sure that modest namespaces did not eat more that 2/3
1205                  * of limit.
1206                  */
1207                 if (nr_l >= 2 * (LDLM_POOL_HOST_L / 3)) {
1208                         CWARN("\"Modest\" pools eat out 2/3 of server locks "
1209                               "limit (%d of %lu). This means that you have too "
1210                               "many clients for this amount of server RAM. "
1211                               "Upgrade server!\n", nr_l, LDLM_POOL_HOST_L);
1212                         equal = 1;
1213                 }
1214
1215                 /*
1216                  * The rest is given to greedy namespaces.
1217                  */
1218                 cfs_list_for_each_entry(ns, ldlm_namespace_list(client),
1219                                         ns_list_chain)
1220                 {
1221                         if (!equal && ns->ns_appetite != LDLM_NAMESPACE_GREEDY)
1222                                 continue;
1223
1224                         if (equal) {
1225                                 /*
1226                                  * In the case 2/3 locks are eaten out by
1227                                  * modest pools, we re-setup equal limit
1228                                  * for _all_ pools.
1229                                  */
1230                                 l = LDLM_POOL_HOST_L /
1231                                         cfs_atomic_read(
1232                                                 ldlm_namespace_nr(client));
1233                         } else {
1234                                 /*
1235                                  * All the rest of greedy pools will have
1236                                  * all locks in equal parts.
1237                                  */
1238                                 l = (LDLM_POOL_HOST_L - nr_l) /
1239                                         (cfs_atomic_read(
1240                                                 ldlm_namespace_nr(client)) -
1241                                          nr_p);
1242                         }
1243                         ldlm_pool_setup(&ns->ns_pool, l);
1244                 }
1245                 cfs_mutex_up(ldlm_namespace_lock(client));
1246         }
1247
1248         /*
1249          * Recalc at least ldlm_namespace_nr(client) namespaces.
1250          */
1251         for (nr = cfs_atomic_read(ldlm_namespace_nr(client)); nr > 0; nr--) {
1252                 int     skip;
1253                 /*
1254                  * Lock the list, get first @ns in the list, getref, move it
1255                  * to the tail, unlock and call pool recalc. This way we avoid
1256                  * calling recalc under @ns lock what is really good as we get
1257                  * rid of potential deadlock on client nodes when canceling
1258                  * locks synchronously.
1259                  */
1260                 cfs_mutex_down(ldlm_namespace_lock(client));
1261                 if (cfs_list_empty(ldlm_namespace_list(client))) {
1262                         cfs_mutex_up(ldlm_namespace_lock(client));
1263                         break;
1264                 }
1265                 ns = ldlm_namespace_first_locked(client);
1266
1267                 cfs_spin_lock(&ns->ns_lock);
1268                 /*
1269                  * skip ns which is being freed, and we don't want to increase
1270                  * its refcount again, not even temporarily. bz21519 & LU-499.
1271                  */
1272                 if (ns->ns_stopping) {
1273                         skip = 1;
1274                 } else {
1275                         skip = 0;
1276                         ldlm_namespace_get(ns);
1277                 }
1278                 cfs_spin_unlock(&ns->ns_lock);
1279
1280                 ldlm_namespace_move_locked(ns, client);
1281                 cfs_mutex_up(ldlm_namespace_lock(client));
1282
1283                 /*
1284                  * After setup is done - recalc the pool.
1285                  */
1286                 if (!skip) {
1287                         ldlm_pool_recalc(&ns->ns_pool);
1288                         ldlm_namespace_put(ns);
1289                 }
1290         }
1291 }
1292 EXPORT_SYMBOL(ldlm_pools_recalc);
1293
1294 static int ldlm_pools_thread_main(void *arg)
1295 {
1296         struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
1297         char *t_name = "ldlm_poold";
1298         ENTRY;
1299
1300         cfs_daemonize(t_name);
1301         thread->t_flags = SVC_RUNNING;
1302         cfs_waitq_signal(&thread->t_ctl_waitq);
1303
1304         CDEBUG(D_DLMTRACE, "%s: pool thread starting, process %d\n",
1305                t_name, cfs_curproc_pid());
1306
1307         while (1) {
1308                 struct l_wait_info lwi;
1309
1310                 /*
1311                  * Recal all pools on this tick.
1312                  */
1313                 ldlm_pools_recalc(LDLM_NAMESPACE_SERVER);
1314                 ldlm_pools_recalc(LDLM_NAMESPACE_CLIENT);
1315
1316                 /*
1317                  * Wait until the next check time, or until we're
1318                  * stopped.
1319                  */
1320                 lwi = LWI_TIMEOUT(cfs_time_seconds(LDLM_POOLS_THREAD_PERIOD),
1321                                   NULL, NULL);
1322                 l_wait_event(thread->t_ctl_waitq, (thread->t_flags &
1323                                                    (SVC_STOPPING|SVC_EVENT)),
1324                              &lwi);
1325
1326                 if (thread->t_flags & SVC_STOPPING) {
1327                         thread->t_flags &= ~SVC_STOPPING;
1328                         break;
1329                 } else if (thread->t_flags & SVC_EVENT) {
1330                         thread->t_flags &= ~SVC_EVENT;
1331                 }
1332         }
1333
1334         thread->t_flags = SVC_STOPPED;
1335         cfs_waitq_signal(&thread->t_ctl_waitq);
1336
1337         CDEBUG(D_DLMTRACE, "%s: pool thread exiting, process %d\n",
1338                t_name, cfs_curproc_pid());
1339
1340         cfs_complete_and_exit(&ldlm_pools_comp, 0);
1341 }
1342
1343 static int ldlm_pools_thread_start(void)
1344 {
1345         struct l_wait_info lwi = { 0 };
1346         int rc;
1347         ENTRY;
1348
1349         if (ldlm_pools_thread != NULL)
1350                 RETURN(-EALREADY);
1351
1352         OBD_ALLOC_PTR(ldlm_pools_thread);
1353         if (ldlm_pools_thread == NULL)
1354                 RETURN(-ENOMEM);
1355
1356         cfs_init_completion(&ldlm_pools_comp);
1357         cfs_waitq_init(&ldlm_pools_thread->t_ctl_waitq);
1358
1359         /*
1360          * CLONE_VM and CLONE_FILES just avoid a needless copy, because we
1361          * just drop the VM and FILES in cfs_daemonize() right away.
1362          */
1363         rc = cfs_create_thread(ldlm_pools_thread_main, ldlm_pools_thread,
1364                                CFS_DAEMON_FLAGS);
1365         if (rc < 0) {
1366                 CERROR("Can't start pool thread, error %d\n",
1367                        rc);
1368                 OBD_FREE(ldlm_pools_thread, sizeof(*ldlm_pools_thread));
1369                 ldlm_pools_thread = NULL;
1370                 RETURN(rc);
1371         }
1372         l_wait_event(ldlm_pools_thread->t_ctl_waitq,
1373                      (ldlm_pools_thread->t_flags & SVC_RUNNING), &lwi);
1374         RETURN(0);
1375 }
1376
1377 static void ldlm_pools_thread_stop(void)
1378 {
1379         ENTRY;
1380
1381         if (ldlm_pools_thread == NULL) {
1382                 EXIT;
1383                 return;
1384         }
1385
1386         ldlm_pools_thread->t_flags = SVC_STOPPING;
1387         cfs_waitq_signal(&ldlm_pools_thread->t_ctl_waitq);
1388
1389         /*
1390          * Make sure that pools thread is finished before freeing @thread.
1391          * This fixes possible race and oops due to accessing freed memory
1392          * in pools thread.
1393          */
1394         cfs_wait_for_completion(&ldlm_pools_comp);
1395         OBD_FREE_PTR(ldlm_pools_thread);
1396         ldlm_pools_thread = NULL;
1397         EXIT;
1398 }
1399
1400 int ldlm_pools_init(void)
1401 {
1402         int rc;
1403         ENTRY;
1404
1405         rc = ldlm_pools_thread_start();
1406         if (rc == 0) {
1407                 ldlm_pools_srv_shrinker =
1408                         cfs_set_shrinker(CFS_DEFAULT_SEEKS,
1409                                          ldlm_pools_srv_shrink);
1410                 ldlm_pools_cli_shrinker =
1411                         cfs_set_shrinker(CFS_DEFAULT_SEEKS,
1412                                          ldlm_pools_cli_shrink);
1413         }
1414         RETURN(rc);
1415 }
1416 EXPORT_SYMBOL(ldlm_pools_init);
1417
1418 void ldlm_pools_fini(void)
1419 {
1420         if (ldlm_pools_srv_shrinker != NULL) {
1421                 cfs_remove_shrinker(ldlm_pools_srv_shrinker);
1422                 ldlm_pools_srv_shrinker = NULL;
1423         }
1424         if (ldlm_pools_cli_shrinker != NULL) {
1425                 cfs_remove_shrinker(ldlm_pools_cli_shrinker);
1426                 ldlm_pools_cli_shrinker = NULL;
1427         }
1428         ldlm_pools_thread_stop();
1429 }
1430 EXPORT_SYMBOL(ldlm_pools_fini);
1431 #endif /* __KERNEL__ */
1432
1433 #else /* !HAVE_LRU_RESIZE_SUPPORT */
1434 int ldlm_pool_setup(struct ldlm_pool *pl, int limit)
1435 {
1436         return 0;
1437 }
1438 EXPORT_SYMBOL(ldlm_pool_setup);
1439
1440 int ldlm_pool_recalc(struct ldlm_pool *pl)
1441 {
1442         return 0;
1443 }
1444 EXPORT_SYMBOL(ldlm_pool_recalc);
1445
1446 int ldlm_pool_shrink(struct ldlm_pool *pl,
1447                      int nr, unsigned int gfp_mask)
1448 {
1449         return 0;
1450 }
1451 EXPORT_SYMBOL(ldlm_pool_shrink);
1452
1453 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
1454                    int idx, ldlm_side_t client)
1455 {
1456         return 0;
1457 }
1458 EXPORT_SYMBOL(ldlm_pool_init);
1459
1460 void ldlm_pool_fini(struct ldlm_pool *pl)
1461 {
1462         return;
1463 }
1464 EXPORT_SYMBOL(ldlm_pool_fini);
1465
1466 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
1467 {
1468         return;
1469 }
1470 EXPORT_SYMBOL(ldlm_pool_add);
1471
1472 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
1473 {
1474         return;
1475 }
1476 EXPORT_SYMBOL(ldlm_pool_del);
1477
1478 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
1479 {
1480         return 1;
1481 }
1482 EXPORT_SYMBOL(ldlm_pool_get_slv);
1483
1484 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv)
1485 {
1486         return;
1487 }
1488 EXPORT_SYMBOL(ldlm_pool_set_slv);
1489
1490 __u64 ldlm_pool_get_clv(struct ldlm_pool *pl)
1491 {
1492         return 1;
1493 }
1494 EXPORT_SYMBOL(ldlm_pool_get_clv);
1495
1496 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv)
1497 {
1498         return;
1499 }
1500 EXPORT_SYMBOL(ldlm_pool_set_clv);
1501
1502 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
1503 {
1504         return 0;
1505 }
1506 EXPORT_SYMBOL(ldlm_pool_get_limit);
1507
1508 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
1509 {
1510         return;
1511 }
1512 EXPORT_SYMBOL(ldlm_pool_set_limit);
1513
1514 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
1515 {
1516         return 0;
1517 }
1518 EXPORT_SYMBOL(ldlm_pool_get_lvf);
1519
1520 int ldlm_pools_init(void)
1521 {
1522         return 0;
1523 }
1524 EXPORT_SYMBOL(ldlm_pools_init);
1525
1526 void ldlm_pools_fini(void)
1527 {
1528         return;
1529 }
1530 EXPORT_SYMBOL(ldlm_pools_fini);
1531
1532 void ldlm_pools_recalc(ldlm_side_t client)
1533 {
1534         return;
1535 }
1536 EXPORT_SYMBOL(ldlm_pools_recalc);
1537 #endif /* HAVE_LRU_RESIZE_SUPPORT */