4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2010, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ldlm/ldlm_pool.c
38 * Author: Yury Umanets <umka@clusterfs.com>
42 * Idea of this code is rather simple. Each second, for each server namespace
43 * we have SLV - server lock volume which is calculated on current number of
44 * granted locks, grant speed for past period, etc - that is, locking load.
45 * This SLV number may be thought as a flow definition for simplicity. It is
46 * sent to clients with each occasion to let them know what is current load
47 * situation on the server. By default, at the beginning, SLV on server is
48 * set max value which is calculated as the following: allow to one client
49 * have all locks of limit ->pl_limit for 10h.
51 * Next, on clients, number of cached locks is not limited artificially in any
52 * way as it was before. Instead, client calculates CLV, that is, client lock
53 * volume for each lock and compares it with last SLV from the server. CLV is
54 * calculated as the number of locks in LRU * lock live time in seconds. If
55 * CLV > SLV - lock is canceled.
57 * Client has LVF, that is, lock volume factor which regulates how much
58 * sensitive client should be about last SLV from server. The higher LVF is the
59 * more locks will be canceled on client. Default value for it is 1. Setting LVF
60 * to 2 means that client will cancel locks 2 times faster.
62 * Locks on a client will be canceled more intensively in these cases:
63 * (1) if SLV is smaller, that is, load is higher on the server;
64 * (2) client has a lot of locks (the more locks are held by client, the bigger
65 * chances that some of them should be canceled);
66 * (3) client has old locks (taken some time ago);
68 * Thus, according to flow paradigm that we use for better understanding SLV,
69 * CLV is the volume of particle in flow described by SLV. According to this,
70 * if flow is getting thinner, more and more particles become outside of it and
71 * as particles are locks, they should be canceled.
73 * General idea of this belongs to Vitaly Fertman (vitaly@clusterfs.com).
74 * Andreas Dilger (adilger@clusterfs.com) proposed few nice ideas like using
75 * LVF and many cleanups. Flow definition to allow more easy understanding of
76 * the logic belongs to Nikita Danilov (nikita@clusterfs.com) as well as many
77 * cleanups and fixes. And design and implementation are done by Yury Umanets
78 * (umka@clusterfs.com).
80 * Glossary for terms used:
82 * pl_limit - Number of allowed locks in pool. Applies to server and client
85 * pl_granted - Number of granted locks (calculated);
86 * pl_grant_rate - Number of granted locks for last T (calculated);
87 * pl_cancel_rate - Number of canceled locks for last T (calculated);
88 * pl_grant_speed - Grant speed (GR - CR) for last T (calculated);
89 * pl_grant_plan - Planned number of granted locks for next T (calculated);
90 * pl_server_lock_volume - Current server lock volume (calculated);
92 * As it may be seen from list above, we have few possible tunables which may
93 * affect behavior much. They all may be modified via proc. However, they also
94 * give a possibility for constructing few pre-defined behavior policies. If
95 * none of predefines is suitable for a working pattern being used, new one may
96 * be "constructed" via proc tunables.
99 #define DEBUG_SUBSYSTEM S_LDLM
101 #include "../include/lustre_dlm.h"
102 #include "../include/cl_object.h"
103 #include "../include/obd_class.h"
104 #include "../include/obd_support.h"
105 #include "ldlm_internal.h"
109 * 50 ldlm locks for 1MB of RAM.
111 #define LDLM_POOL_HOST_L ((NUM_CACHEPAGES >> (20 - PAGE_CACHE_SHIFT)) * 50)
114 * Maximal possible grant step plan in %.
116 #define LDLM_POOL_MAX_GSP (30)
119 * Minimal possible grant step plan in %.
121 #define LDLM_POOL_MIN_GSP (1)
124 * This controls the speed of reaching LDLM_POOL_MAX_GSP
125 * with increasing thread period.
127 #define LDLM_POOL_GSP_STEP_SHIFT (2)
130 * LDLM_POOL_GSP% of all locks is default GP.
132 #define LDLM_POOL_GP(L) (((L) * LDLM_POOL_MAX_GSP) / 100)
135 * Max age for locks on clients.
137 #define LDLM_POOL_MAX_AGE (36000)
140 * The granularity of SLV calculation.
142 #define LDLM_POOL_SLV_SHIFT (10)
144 static inline __u64 dru(__u64 val, __u32 shift, int round_up)
146 return (val + (round_up ? (1 << shift) - 1 : 0)) >> shift;
149 static inline __u64 ldlm_pool_slv_max(__u32 L)
152 * Allow to have all locks for 1 client for 10 hrs.
153 * Formula is the following: limit * 10h / 1 client.
155 __u64 lim = (__u64)L * LDLM_POOL_MAX_AGE / 1;
159 static inline __u64 ldlm_pool_slv_min(__u32 L)
165 LDLM_POOL_FIRST_STAT = 0,
166 LDLM_POOL_GRANTED_STAT = LDLM_POOL_FIRST_STAT,
167 LDLM_POOL_GRANT_STAT,
168 LDLM_POOL_CANCEL_STAT,
169 LDLM_POOL_GRANT_RATE_STAT,
170 LDLM_POOL_CANCEL_RATE_STAT,
171 LDLM_POOL_GRANT_PLAN_STAT,
173 LDLM_POOL_SHRINK_REQTD_STAT,
174 LDLM_POOL_SHRINK_FREED_STAT,
175 LDLM_POOL_RECALC_STAT,
176 LDLM_POOL_TIMING_STAT,
180 static inline struct ldlm_namespace *ldlm_pl2ns(struct ldlm_pool *pl)
182 return container_of(pl, struct ldlm_namespace, ns_pool);
186 * Calculates suggested grant_step in % of available locks for passed
187 * \a period. This is later used in grant_plan calculations.
189 static inline int ldlm_pool_t2gsp(unsigned int t)
192 * This yields 1% grant step for anything below LDLM_POOL_GSP_STEP
193 * and up to 30% for anything higher than LDLM_POOL_GSP_STEP.
195 * How this will affect execution is the following:
197 * - for thread period 1s we will have grant_step 1% which good from
198 * pov of taking some load off from server and push it out to clients.
199 * This is like that because 1% for grant_step means that server will
200 * not allow clients to get lots of locks in short period of time and
201 * keep all old locks in their caches. Clients will always have to
202 * get some locks back if they want to take some new;
204 * - for thread period 10s (which is default) we will have 23% which
205 * means that clients will have enough of room to take some new locks
206 * without getting some back. All locks from this 23% which were not
207 * taken by clients in current period will contribute in SLV growing.
208 * SLV growing means more locks cached on clients until limit or grant
211 return LDLM_POOL_MAX_GSP -
212 ((LDLM_POOL_MAX_GSP - LDLM_POOL_MIN_GSP) >>
213 (t >> LDLM_POOL_GSP_STEP_SHIFT));
217 * Recalculates next grant limit on passed \a pl.
219 * \pre ->pl_lock is locked.
221 static void ldlm_pool_recalc_grant_plan(struct ldlm_pool *pl)
223 int granted, grant_step, limit;
225 limit = ldlm_pool_get_limit(pl);
226 granted = atomic_read(&pl->pl_granted);
228 grant_step = ldlm_pool_t2gsp(pl->pl_recalc_period);
229 grant_step = ((limit - granted) * grant_step) / 100;
230 pl->pl_grant_plan = granted + grant_step;
231 limit = (limit * 5) >> 2;
232 if (pl->pl_grant_plan > limit)
233 pl->pl_grant_plan = limit;
237 * Recalculates next SLV on passed \a pl.
239 * \pre ->pl_lock is locked.
241 static void ldlm_pool_recalc_slv(struct ldlm_pool *pl)
251 slv = pl->pl_server_lock_volume;
252 grant_plan = pl->pl_grant_plan;
253 limit = ldlm_pool_get_limit(pl);
254 granted = atomic_read(&pl->pl_granted);
255 round_up = granted < limit;
257 grant_usage = max_t(int, limit - (granted - grant_plan), 1);
260 * Find out SLV change factor which is the ratio of grant usage
261 * from limit. SLV changes as fast as the ratio of grant plan
262 * consumption. The more locks from grant plan are not consumed
263 * by clients in last interval (idle time), the faster grows
264 * SLV. And the opposite, the more grant plan is over-consumed
265 * (load time) the faster drops SLV.
267 slv_factor = grant_usage << LDLM_POOL_SLV_SHIFT;
268 do_div(slv_factor, limit);
269 slv = slv * slv_factor;
270 slv = dru(slv, LDLM_POOL_SLV_SHIFT, round_up);
272 if (slv > ldlm_pool_slv_max(limit))
273 slv = ldlm_pool_slv_max(limit);
274 else if (slv < ldlm_pool_slv_min(limit))
275 slv = ldlm_pool_slv_min(limit);
277 pl->pl_server_lock_volume = slv;
281 * Recalculates next stats on passed \a pl.
283 * \pre ->pl_lock is locked.
285 static void ldlm_pool_recalc_stats(struct ldlm_pool *pl)
287 int grant_plan = pl->pl_grant_plan;
288 __u64 slv = pl->pl_server_lock_volume;
289 int granted = atomic_read(&pl->pl_granted);
290 int grant_rate = atomic_read(&pl->pl_grant_rate);
291 int cancel_rate = atomic_read(&pl->pl_cancel_rate);
293 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_SLV_STAT,
295 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
297 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
299 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
301 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
306 * Sets current SLV into obd accessible via ldlm_pl2ns(pl)->ns_obd.
308 static void ldlm_srv_pool_push_slv(struct ldlm_pool *pl)
310 struct obd_device *obd;
313 * Set new SLV in obd field for using it later without accessing the
314 * pool. This is required to avoid race between sending reply to client
315 * with new SLV and cleanup server stack in which we can't guarantee
316 * that namespace is still alive. We know only that obd is alive as
317 * long as valid export is alive.
319 obd = ldlm_pl2ns(pl)->ns_obd;
320 LASSERT(obd != NULL);
321 write_lock(&obd->obd_pool_lock);
322 obd->obd_pool_slv = pl->pl_server_lock_volume;
323 write_unlock(&obd->obd_pool_lock);
327 * Recalculates all pool fields on passed \a pl.
329 * \pre ->pl_lock is not locked.
331 static int ldlm_srv_pool_recalc(struct ldlm_pool *pl)
333 time_t recalc_interval_sec;
335 recalc_interval_sec = get_seconds() - pl->pl_recalc_time;
336 if (recalc_interval_sec < pl->pl_recalc_period)
339 spin_lock(&pl->pl_lock);
340 recalc_interval_sec = get_seconds() - pl->pl_recalc_time;
341 if (recalc_interval_sec < pl->pl_recalc_period) {
342 spin_unlock(&pl->pl_lock);
346 * Recalc SLV after last period. This should be done
347 * _before_ recalculating new grant plan.
349 ldlm_pool_recalc_slv(pl);
352 * Make sure that pool informed obd of last SLV changes.
354 ldlm_srv_pool_push_slv(pl);
357 * Update grant_plan for new period.
359 ldlm_pool_recalc_grant_plan(pl);
361 pl->pl_recalc_time = get_seconds();
362 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
363 recalc_interval_sec);
364 spin_unlock(&pl->pl_lock);
369 * This function is used on server side as main entry point for memory
370 * pressure handling. It decreases SLV on \a pl according to passed
371 * \a nr and \a gfp_mask.
373 * Our goal here is to decrease SLV such a way that clients hold \a nr
374 * locks smaller in next 10h.
376 static int ldlm_srv_pool_shrink(struct ldlm_pool *pl,
377 int nr, gfp_t gfp_mask)
382 * VM is asking how many entries may be potentially freed.
385 return atomic_read(&pl->pl_granted);
388 * Client already canceled locks but server is already in shrinker
389 * and can't cancel anything. Let's catch this race.
391 if (atomic_read(&pl->pl_granted) == 0)
394 spin_lock(&pl->pl_lock);
397 * We want shrinker to possibly cause cancellation of @nr locks from
398 * clients or grant approximately @nr locks smaller next intervals.
400 * This is why we decreased SLV by @nr. This effect will only be as
401 * long as one re-calc interval (1s these days) and this should be
402 * enough to pass this decreased SLV to all clients. On next recalc
403 * interval pool will either increase SLV if locks load is not high
404 * or will keep on same level or even decrease again, thus, shrinker
405 * decreased SLV will affect next recalc intervals and this way will
406 * make locking load lower.
408 if (nr < pl->pl_server_lock_volume) {
409 pl->pl_server_lock_volume = pl->pl_server_lock_volume - nr;
411 limit = ldlm_pool_get_limit(pl);
412 pl->pl_server_lock_volume = ldlm_pool_slv_min(limit);
416 * Make sure that pool informed obd of last SLV changes.
418 ldlm_srv_pool_push_slv(pl);
419 spin_unlock(&pl->pl_lock);
422 * We did not really free any memory here so far, it only will be
423 * freed later may be, so that we return 0 to not confuse VM.
429 * Setup server side pool \a pl with passed \a limit.
431 static int ldlm_srv_pool_setup(struct ldlm_pool *pl, int limit)
433 struct obd_device *obd;
435 obd = ldlm_pl2ns(pl)->ns_obd;
436 LASSERT(obd != NULL && obd != LP_POISON);
437 LASSERT(obd->obd_type != LP_POISON);
438 write_lock(&obd->obd_pool_lock);
439 obd->obd_pool_limit = limit;
440 write_unlock(&obd->obd_pool_lock);
442 ldlm_pool_set_limit(pl, limit);
447 * Sets SLV and Limit from ldlm_pl2ns(pl)->ns_obd tp passed \a pl.
449 static void ldlm_cli_pool_pop_slv(struct ldlm_pool *pl)
451 struct obd_device *obd;
454 * Get new SLV and Limit from obd which is updated with coming
457 obd = ldlm_pl2ns(pl)->ns_obd;
458 LASSERT(obd != NULL);
459 read_lock(&obd->obd_pool_lock);
460 pl->pl_server_lock_volume = obd->obd_pool_slv;
461 ldlm_pool_set_limit(pl, obd->obd_pool_limit);
462 read_unlock(&obd->obd_pool_lock);
466 * Recalculates client size pool \a pl according to current SLV and Limit.
468 static int ldlm_cli_pool_recalc(struct ldlm_pool *pl)
470 time_t recalc_interval_sec;
473 recalc_interval_sec = get_seconds() - pl->pl_recalc_time;
474 if (recalc_interval_sec < pl->pl_recalc_period)
477 spin_lock(&pl->pl_lock);
479 * Check if we need to recalc lists now.
481 recalc_interval_sec = get_seconds() - pl->pl_recalc_time;
482 if (recalc_interval_sec < pl->pl_recalc_period) {
483 spin_unlock(&pl->pl_lock);
488 * Make sure that pool knows last SLV and Limit from obd.
490 ldlm_cli_pool_pop_slv(pl);
492 spin_unlock(&pl->pl_lock);
495 * Do not cancel locks in case lru resize is disabled for this ns.
497 if (!ns_connect_lru_resize(ldlm_pl2ns(pl))) {
503 * In the time of canceling locks on client we do not need to maintain
504 * sharp timing, we only want to cancel locks asap according to new SLV.
505 * It may be called when SLV has changed much, this is why we do not
506 * take into account pl->pl_recalc_time here.
508 ret = ldlm_cancel_lru(ldlm_pl2ns(pl), 0, LCF_ASYNC, LDLM_CANCEL_LRUR);
511 spin_lock(&pl->pl_lock);
513 * Time of LRU resizing might be longer than period,
514 * so update after LRU resizing rather than before it.
516 pl->pl_recalc_time = get_seconds();
517 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
518 recalc_interval_sec);
519 spin_unlock(&pl->pl_lock);
524 * This function is main entry point for memory pressure handling on client
525 * side. Main goal of this function is to cancel some number of locks on
526 * passed \a pl according to \a nr and \a gfp_mask.
528 static int ldlm_cli_pool_shrink(struct ldlm_pool *pl,
529 int nr, gfp_t gfp_mask)
531 struct ldlm_namespace *ns;
537 * Do not cancel locks in case lru resize is disabled for this ns.
539 if (!ns_connect_lru_resize(ns))
543 * Make sure that pool knows last SLV and Limit from obd.
545 ldlm_cli_pool_pop_slv(pl);
547 spin_lock(&ns->ns_lock);
548 unused = ns->ns_nr_unused;
549 spin_unlock(&ns->ns_lock);
552 return (unused / 100) * sysctl_vfs_cache_pressure;
554 return ldlm_cancel_lru(ns, nr, LCF_ASYNC, LDLM_CANCEL_SHRINK);
557 static const struct ldlm_pool_ops ldlm_srv_pool_ops = {
558 .po_recalc = ldlm_srv_pool_recalc,
559 .po_shrink = ldlm_srv_pool_shrink,
560 .po_setup = ldlm_srv_pool_setup
563 static const struct ldlm_pool_ops ldlm_cli_pool_ops = {
564 .po_recalc = ldlm_cli_pool_recalc,
565 .po_shrink = ldlm_cli_pool_shrink
569 * Pool recalc wrapper. Will call either client or server pool recalc callback
570 * depending what pool \a pl is used.
572 int ldlm_pool_recalc(struct ldlm_pool *pl)
574 time_t recalc_interval_sec;
577 recalc_interval_sec = get_seconds() - pl->pl_recalc_time;
578 if (recalc_interval_sec <= 0)
581 spin_lock(&pl->pl_lock);
582 if (recalc_interval_sec > 0) {
584 * Update pool statistics every 1s.
586 ldlm_pool_recalc_stats(pl);
589 * Zero out all rates and speed for the last period.
591 atomic_set(&pl->pl_grant_rate, 0);
592 atomic_set(&pl->pl_cancel_rate, 0);
594 spin_unlock(&pl->pl_lock);
597 if (pl->pl_ops->po_recalc != NULL) {
598 count = pl->pl_ops->po_recalc(pl);
599 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_RECALC_STAT,
602 recalc_interval_sec = pl->pl_recalc_time - get_seconds() +
603 pl->pl_recalc_period;
604 if (recalc_interval_sec <= 0) {
605 /* Prevent too frequent recalculation. */
606 CDEBUG(D_DLMTRACE, "Negative interval(%ld), "
607 "too short period(%ld)",
609 pl->pl_recalc_period);
610 recalc_interval_sec = 1;
613 return recalc_interval_sec;
617 * Pool shrink wrapper. Will call either client or server pool recalc callback
618 * depending what pool pl is used. When nr == 0, just return the number of
619 * freeable locks. Otherwise, return the number of canceled locks.
621 int ldlm_pool_shrink(struct ldlm_pool *pl, int nr,
626 if (pl->pl_ops->po_shrink != NULL) {
627 cancel = pl->pl_ops->po_shrink(pl, nr, gfp_mask);
629 lprocfs_counter_add(pl->pl_stats,
630 LDLM_POOL_SHRINK_REQTD_STAT,
632 lprocfs_counter_add(pl->pl_stats,
633 LDLM_POOL_SHRINK_FREED_STAT,
635 CDEBUG(D_DLMTRACE, "%s: request to shrink %d locks, shrunk %d\n",
636 pl->pl_name, nr, cancel);
641 EXPORT_SYMBOL(ldlm_pool_shrink);
644 * Pool setup wrapper. Will call either client or server pool recalc callback
645 * depending what pool \a pl is used.
647 * Sets passed \a limit into pool \a pl.
649 int ldlm_pool_setup(struct ldlm_pool *pl, int limit)
651 if (pl->pl_ops->po_setup != NULL)
652 return pl->pl_ops->po_setup(pl, limit);
655 EXPORT_SYMBOL(ldlm_pool_setup);
657 #if defined(CONFIG_PROC_FS)
658 static int lprocfs_pool_state_seq_show(struct seq_file *m, void *unused)
660 int granted, grant_rate, cancel_rate, grant_step;
661 int grant_speed, grant_plan, lvf;
662 struct ldlm_pool *pl = m->private;
666 spin_lock(&pl->pl_lock);
667 slv = pl->pl_server_lock_volume;
668 clv = pl->pl_client_lock_volume;
669 limit = ldlm_pool_get_limit(pl);
670 grant_plan = pl->pl_grant_plan;
671 granted = atomic_read(&pl->pl_granted);
672 grant_rate = atomic_read(&pl->pl_grant_rate);
673 cancel_rate = atomic_read(&pl->pl_cancel_rate);
674 grant_speed = grant_rate - cancel_rate;
675 lvf = atomic_read(&pl->pl_lock_volume_factor);
676 grant_step = ldlm_pool_t2gsp(pl->pl_recalc_period);
677 spin_unlock(&pl->pl_lock);
679 seq_printf(m, "LDLM pool state (%s):\n"
683 pl->pl_name, slv, clv, lvf);
685 if (ns_is_server(ldlm_pl2ns(pl))) {
686 seq_printf(m, " GSP: %d%%\n"
688 grant_step, grant_plan);
690 seq_printf(m, " GR: %d\n CR: %d\n GS: %d\n"
692 grant_rate, cancel_rate, grant_speed,
697 LPROC_SEQ_FOPS_RO(lprocfs_pool_state);
699 static int lprocfs_grant_speed_seq_show(struct seq_file *m, void *unused)
701 struct ldlm_pool *pl = m->private;
704 spin_lock(&pl->pl_lock);
705 /* serialize with ldlm_pool_recalc */
706 grant_speed = atomic_read(&pl->pl_grant_rate) -
707 atomic_read(&pl->pl_cancel_rate);
708 spin_unlock(&pl->pl_lock);
709 return lprocfs_rd_uint(m, &grant_speed);
712 LDLM_POOL_PROC_READER_SEQ_SHOW(grant_plan, int);
713 LPROC_SEQ_FOPS_RO(lprocfs_grant_plan);
715 LDLM_POOL_PROC_READER_SEQ_SHOW(recalc_period, int);
716 LDLM_POOL_PROC_WRITER(recalc_period, int);
717 static ssize_t lprocfs_recalc_period_seq_write(struct file *file,
718 const char __user *buf,
719 size_t len, loff_t *off)
721 struct seq_file *seq = file->private_data;
723 return lprocfs_wr_recalc_period(file, buf, len, seq->private);
725 LPROC_SEQ_FOPS(lprocfs_recalc_period);
727 LPROC_SEQ_FOPS_RO_TYPE(ldlm_pool, u64);
728 LPROC_SEQ_FOPS_RO_TYPE(ldlm_pool, atomic);
729 LPROC_SEQ_FOPS_RW_TYPE(ldlm_pool_rw, atomic);
731 LPROC_SEQ_FOPS_RO(lprocfs_grant_speed);
733 #define LDLM_POOL_ADD_VAR(name, var, ops) \
735 snprintf(var_name, MAX_STRING_SIZE, #name); \
736 pool_vars[0].data = var; \
737 pool_vars[0].fops = ops; \
738 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, NULL);\
741 static int ldlm_pool_proc_init(struct ldlm_pool *pl)
743 struct ldlm_namespace *ns = ldlm_pl2ns(pl);
744 struct proc_dir_entry *parent_ns_proc;
745 struct lprocfs_vars pool_vars[2];
746 char *var_name = NULL;
749 OBD_ALLOC(var_name, MAX_STRING_SIZE + 1);
753 parent_ns_proc = ns->ns_proc_dir_entry;
754 if (parent_ns_proc == NULL) {
755 CERROR("%s: proc entry is not initialized\n",
760 pl->pl_proc_dir = lprocfs_register("pool", parent_ns_proc,
762 if (IS_ERR(pl->pl_proc_dir)) {
763 CERROR("LProcFS failed in ldlm-pool-init\n");
764 rc = PTR_ERR(pl->pl_proc_dir);
765 pl->pl_proc_dir = NULL;
769 var_name[MAX_STRING_SIZE] = '\0';
770 memset(pool_vars, 0, sizeof(pool_vars));
771 pool_vars[0].name = var_name;
773 LDLM_POOL_ADD_VAR("server_lock_volume", &pl->pl_server_lock_volume,
774 &ldlm_pool_u64_fops);
775 LDLM_POOL_ADD_VAR("limit", &pl->pl_limit, &ldlm_pool_rw_atomic_fops);
776 LDLM_POOL_ADD_VAR("granted", &pl->pl_granted, &ldlm_pool_atomic_fops);
777 LDLM_POOL_ADD_VAR("grant_speed", pl, &lprocfs_grant_speed_fops);
778 LDLM_POOL_ADD_VAR("cancel_rate", &pl->pl_cancel_rate,
779 &ldlm_pool_atomic_fops);
780 LDLM_POOL_ADD_VAR("grant_rate", &pl->pl_grant_rate,
781 &ldlm_pool_atomic_fops);
782 LDLM_POOL_ADD_VAR("grant_plan", pl, &lprocfs_grant_plan_fops);
783 LDLM_POOL_ADD_VAR("recalc_period", pl, &lprocfs_recalc_period_fops);
784 LDLM_POOL_ADD_VAR("lock_volume_factor", &pl->pl_lock_volume_factor,
785 &ldlm_pool_rw_atomic_fops);
786 LDLM_POOL_ADD_VAR("state", pl, &lprocfs_pool_state_fops);
788 pl->pl_stats = lprocfs_alloc_stats(LDLM_POOL_LAST_STAT -
789 LDLM_POOL_FIRST_STAT, 0);
795 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
796 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
798 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_STAT,
799 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
801 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_STAT,
802 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
804 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
805 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
806 "grant_rate", "locks/s");
807 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
808 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
809 "cancel_rate", "locks/s");
810 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
811 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
812 "grant_plan", "locks/s");
813 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SLV_STAT,
814 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
816 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_REQTD_STAT,
817 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
818 "shrink_request", "locks");
819 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_FREED_STAT,
820 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
821 "shrink_freed", "locks");
822 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_RECALC_STAT,
823 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
824 "recalc_freed", "locks");
825 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_TIMING_STAT,
826 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
827 "recalc_timing", "sec");
828 rc = lprocfs_register_stats(pl->pl_proc_dir, "stats", pl->pl_stats);
831 OBD_FREE(var_name, MAX_STRING_SIZE + 1);
835 static void ldlm_pool_proc_fini(struct ldlm_pool *pl)
837 if (pl->pl_stats != NULL) {
838 lprocfs_free_stats(&pl->pl_stats);
841 if (pl->pl_proc_dir != NULL) {
842 lprocfs_remove(&pl->pl_proc_dir);
843 pl->pl_proc_dir = NULL;
846 #else /* !CONFIG_PROC_FS */
847 static int ldlm_pool_proc_init(struct ldlm_pool *pl)
852 static void ldlm_pool_proc_fini(struct ldlm_pool *pl) {}
853 #endif /* CONFIG_PROC_FS */
855 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
856 int idx, ldlm_side_t client)
860 spin_lock_init(&pl->pl_lock);
861 atomic_set(&pl->pl_granted, 0);
862 pl->pl_recalc_time = get_seconds();
863 atomic_set(&pl->pl_lock_volume_factor, 1);
865 atomic_set(&pl->pl_grant_rate, 0);
866 atomic_set(&pl->pl_cancel_rate, 0);
867 pl->pl_grant_plan = LDLM_POOL_GP(LDLM_POOL_HOST_L);
869 snprintf(pl->pl_name, sizeof(pl->pl_name), "ldlm-pool-%s-%d",
870 ldlm_ns_name(ns), idx);
872 if (client == LDLM_NAMESPACE_SERVER) {
873 pl->pl_ops = &ldlm_srv_pool_ops;
874 ldlm_pool_set_limit(pl, LDLM_POOL_HOST_L);
875 pl->pl_recalc_period = LDLM_POOL_SRV_DEF_RECALC_PERIOD;
876 pl->pl_server_lock_volume = ldlm_pool_slv_max(LDLM_POOL_HOST_L);
878 ldlm_pool_set_limit(pl, 1);
879 pl->pl_server_lock_volume = 0;
880 pl->pl_ops = &ldlm_cli_pool_ops;
881 pl->pl_recalc_period = LDLM_POOL_CLI_DEF_RECALC_PERIOD;
883 pl->pl_client_lock_volume = 0;
884 rc = ldlm_pool_proc_init(pl);
888 CDEBUG(D_DLMTRACE, "Lock pool %s is initialized\n", pl->pl_name);
892 EXPORT_SYMBOL(ldlm_pool_init);
894 void ldlm_pool_fini(struct ldlm_pool *pl)
896 ldlm_pool_proc_fini(pl);
899 * Pool should not be used after this point. We can't free it here as
900 * it lives in struct ldlm_namespace, but still interested in catching
901 * any abnormal using cases.
903 POISON(pl, 0x5a, sizeof(*pl));
905 EXPORT_SYMBOL(ldlm_pool_fini);
908 * Add new taken ldlm lock \a lock into pool \a pl accounting.
910 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
913 * FLOCK locks are special in a sense that they are almost never
914 * cancelled, instead special kind of lock is used to drop them.
915 * also there is no LRU for flock locks, so no point in tracking
918 if (lock->l_resource->lr_type == LDLM_FLOCK)
921 atomic_inc(&pl->pl_granted);
922 atomic_inc(&pl->pl_grant_rate);
923 lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_GRANT_STAT);
925 * Do not do pool recalc for client side as all locks which
926 * potentially may be canceled has already been packed into
927 * enqueue/cancel rpc. Also we do not want to run out of stack
928 * with too long call paths.
930 if (ns_is_server(ldlm_pl2ns(pl)))
931 ldlm_pool_recalc(pl);
933 EXPORT_SYMBOL(ldlm_pool_add);
936 * Remove ldlm lock \a lock from pool \a pl accounting.
938 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
941 * Filter out FLOCK locks. Read above comment in ldlm_pool_add().
943 if (lock->l_resource->lr_type == LDLM_FLOCK)
946 LASSERT(atomic_read(&pl->pl_granted) > 0);
947 atomic_dec(&pl->pl_granted);
948 atomic_inc(&pl->pl_cancel_rate);
950 lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_CANCEL_STAT);
952 if (ns_is_server(ldlm_pl2ns(pl)))
953 ldlm_pool_recalc(pl);
955 EXPORT_SYMBOL(ldlm_pool_del);
958 * Returns current \a pl SLV.
960 * \pre ->pl_lock is not locked.
962 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
966 spin_lock(&pl->pl_lock);
967 slv = pl->pl_server_lock_volume;
968 spin_unlock(&pl->pl_lock);
971 EXPORT_SYMBOL(ldlm_pool_get_slv);
974 * Sets passed \a slv to \a pl.
976 * \pre ->pl_lock is not locked.
978 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv)
980 spin_lock(&pl->pl_lock);
981 pl->pl_server_lock_volume = slv;
982 spin_unlock(&pl->pl_lock);
984 EXPORT_SYMBOL(ldlm_pool_set_slv);
987 * Returns current \a pl CLV.
989 * \pre ->pl_lock is not locked.
991 __u64 ldlm_pool_get_clv(struct ldlm_pool *pl)
995 spin_lock(&pl->pl_lock);
996 slv = pl->pl_client_lock_volume;
997 spin_unlock(&pl->pl_lock);
1000 EXPORT_SYMBOL(ldlm_pool_get_clv);
1003 * Sets passed \a clv to \a pl.
1005 * \pre ->pl_lock is not locked.
1007 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv)
1009 spin_lock(&pl->pl_lock);
1010 pl->pl_client_lock_volume = clv;
1011 spin_unlock(&pl->pl_lock);
1013 EXPORT_SYMBOL(ldlm_pool_set_clv);
1016 * Returns current \a pl limit.
1018 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
1020 return atomic_read(&pl->pl_limit);
1022 EXPORT_SYMBOL(ldlm_pool_get_limit);
1025 * Sets passed \a limit to \a pl.
1027 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
1029 atomic_set(&pl->pl_limit, limit);
1031 EXPORT_SYMBOL(ldlm_pool_set_limit);
1034 * Returns current LVF from \a pl.
1036 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
1038 return atomic_read(&pl->pl_lock_volume_factor);
1040 EXPORT_SYMBOL(ldlm_pool_get_lvf);
1042 static int ldlm_pool_granted(struct ldlm_pool *pl)
1044 return atomic_read(&pl->pl_granted);
1047 static struct ptlrpc_thread *ldlm_pools_thread;
1048 static struct completion ldlm_pools_comp;
1051 * count locks from all namespaces (if possible). Returns number of
1054 static unsigned long ldlm_pools_count(ldlm_side_t client, gfp_t gfp_mask)
1056 int total = 0, nr_ns;
1057 struct ldlm_namespace *ns;
1058 struct ldlm_namespace *ns_old = NULL; /* loop detection */
1061 if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
1064 CDEBUG(D_DLMTRACE, "Request to count %s locks from all pools\n",
1065 client == LDLM_NAMESPACE_CLIENT ? "client" : "server");
1067 cookie = cl_env_reenter();
1070 * Find out how many resources we may release.
1072 for (nr_ns = ldlm_namespace_nr_read(client);
1073 nr_ns > 0; nr_ns--) {
1074 mutex_lock(ldlm_namespace_lock(client));
1075 if (list_empty(ldlm_namespace_list(client))) {
1076 mutex_unlock(ldlm_namespace_lock(client));
1077 cl_env_reexit(cookie);
1080 ns = ldlm_namespace_first_locked(client);
1083 mutex_unlock(ldlm_namespace_lock(client));
1087 if (ldlm_ns_empty(ns)) {
1088 ldlm_namespace_move_to_inactive_locked(ns, client);
1089 mutex_unlock(ldlm_namespace_lock(client));
1096 ldlm_namespace_get(ns);
1097 ldlm_namespace_move_to_active_locked(ns, client);
1098 mutex_unlock(ldlm_namespace_lock(client));
1099 total += ldlm_pool_shrink(&ns->ns_pool, 0, gfp_mask);
1100 ldlm_namespace_put(ns);
1103 cl_env_reexit(cookie);
1107 static unsigned long ldlm_pools_scan(ldlm_side_t client, int nr, gfp_t gfp_mask)
1109 unsigned long freed = 0;
1111 struct ldlm_namespace *ns;
1114 if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
1117 cookie = cl_env_reenter();
1120 * Shrink at least ldlm_namespace_nr_read(client) namespaces.
1122 for (tmp = nr_ns = ldlm_namespace_nr_read(client);
1124 int cancel, nr_locks;
1127 * Do not call shrink under ldlm_namespace_lock(client)
1129 mutex_lock(ldlm_namespace_lock(client));
1130 if (list_empty(ldlm_namespace_list(client))) {
1131 mutex_unlock(ldlm_namespace_lock(client));
1134 ns = ldlm_namespace_first_locked(client);
1135 ldlm_namespace_get(ns);
1136 ldlm_namespace_move_to_active_locked(ns, client);
1137 mutex_unlock(ldlm_namespace_lock(client));
1139 nr_locks = ldlm_pool_granted(&ns->ns_pool);
1141 * We use to shrink propotionally but with new shrinker API,
1142 * we lost the total number of freeable locks.
1144 cancel = 1 + min_t(int, nr_locks, nr / nr_ns);
1145 freed += ldlm_pool_shrink(&ns->ns_pool, cancel, gfp_mask);
1146 ldlm_namespace_put(ns);
1148 cl_env_reexit(cookie);
1150 * we only decrease the SLV in server pools shrinker, return
1151 * SHRINK_STOP to kernel to avoid needless loop. LU-1128
1153 return (client == LDLM_NAMESPACE_SERVER) ? SHRINK_STOP : freed;
1156 static unsigned long ldlm_pools_srv_count(struct shrinker *s,
1157 struct shrink_control *sc)
1159 return ldlm_pools_count(LDLM_NAMESPACE_SERVER, sc->gfp_mask);
1162 static unsigned long ldlm_pools_srv_scan(struct shrinker *s,
1163 struct shrink_control *sc)
1165 return ldlm_pools_scan(LDLM_NAMESPACE_SERVER, sc->nr_to_scan,
1169 static unsigned long ldlm_pools_cli_count(struct shrinker *s,
1170 struct shrink_control *sc)
1172 return ldlm_pools_count(LDLM_NAMESPACE_CLIENT, sc->gfp_mask);
1175 static unsigned long ldlm_pools_cli_scan(struct shrinker *s,
1176 struct shrink_control *sc)
1178 return ldlm_pools_scan(LDLM_NAMESPACE_CLIENT, sc->nr_to_scan,
1182 int ldlm_pools_recalc(ldlm_side_t client)
1184 __u32 nr_l = 0, nr_p = 0, l;
1185 struct ldlm_namespace *ns;
1186 struct ldlm_namespace *ns_old = NULL;
1188 int time = 50; /* seconds of sleep if no active namespaces */
1191 * No need to setup pool limit for client pools.
1193 if (client == LDLM_NAMESPACE_SERVER) {
1195 * Check all modest namespaces first.
1197 mutex_lock(ldlm_namespace_lock(client));
1198 list_for_each_entry(ns, ldlm_namespace_list(client),
1200 if (ns->ns_appetite != LDLM_NAMESPACE_MODEST)
1203 l = ldlm_pool_granted(&ns->ns_pool);
1208 * Set the modest pools limit equal to their avg granted
1211 l += dru(l, LDLM_POOLS_MODEST_MARGIN_SHIFT, 0);
1212 ldlm_pool_setup(&ns->ns_pool, l);
1218 * Make sure that modest namespaces did not eat more that 2/3
1221 if (nr_l >= 2 * (LDLM_POOL_HOST_L / 3)) {
1222 CWARN("\"Modest\" pools eat out 2/3 of server locks limit (%d of %lu). This means that you have too many clients for this amount of server RAM. Upgrade server!\n",
1223 nr_l, LDLM_POOL_HOST_L);
1228 * The rest is given to greedy namespaces.
1230 list_for_each_entry(ns, ldlm_namespace_list(client),
1232 if (!equal && ns->ns_appetite != LDLM_NAMESPACE_GREEDY)
1237 * In the case 2/3 locks are eaten out by
1238 * modest pools, we re-setup equal limit
1241 l = LDLM_POOL_HOST_L /
1242 ldlm_namespace_nr_read(client);
1245 * All the rest of greedy pools will have
1246 * all locks in equal parts.
1248 l = (LDLM_POOL_HOST_L - nr_l) /
1249 (ldlm_namespace_nr_read(client) -
1252 ldlm_pool_setup(&ns->ns_pool, l);
1254 mutex_unlock(ldlm_namespace_lock(client));
1258 * Recalc at least ldlm_namespace_nr_read(client) namespaces.
1260 for (nr = ldlm_namespace_nr_read(client); nr > 0; nr--) {
1263 * Lock the list, get first @ns in the list, getref, move it
1264 * to the tail, unlock and call pool recalc. This way we avoid
1265 * calling recalc under @ns lock what is really good as we get
1266 * rid of potential deadlock on client nodes when canceling
1267 * locks synchronously.
1269 mutex_lock(ldlm_namespace_lock(client));
1270 if (list_empty(ldlm_namespace_list(client))) {
1271 mutex_unlock(ldlm_namespace_lock(client));
1274 ns = ldlm_namespace_first_locked(client);
1276 if (ns_old == ns) { /* Full pass complete */
1277 mutex_unlock(ldlm_namespace_lock(client));
1281 /* We got an empty namespace, need to move it back to inactive
1283 * The race with parallel resource creation is fine:
1284 * - If they do namespace_get before our check, we fail the
1285 * check and they move this item to the end of the list anyway
1286 * - If we do the check and then they do namespace_get, then
1287 * we move the namespace to inactive and they will move
1288 * it back to active (synchronised by the lock, so no clash
1291 if (ldlm_ns_empty(ns)) {
1292 ldlm_namespace_move_to_inactive_locked(ns, client);
1293 mutex_unlock(ldlm_namespace_lock(client));
1300 spin_lock(&ns->ns_lock);
1302 * skip ns which is being freed, and we don't want to increase
1303 * its refcount again, not even temporarily. bz21519 & LU-499.
1305 if (ns->ns_stopping) {
1309 ldlm_namespace_get(ns);
1311 spin_unlock(&ns->ns_lock);
1313 ldlm_namespace_move_to_active_locked(ns, client);
1314 mutex_unlock(ldlm_namespace_lock(client));
1317 * After setup is done - recalc the pool.
1320 int ttime = ldlm_pool_recalc(&ns->ns_pool);
1325 ldlm_namespace_put(ns);
1330 EXPORT_SYMBOL(ldlm_pools_recalc);
1332 static int ldlm_pools_thread_main(void *arg)
1334 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
1337 thread_set_flags(thread, SVC_RUNNING);
1338 wake_up(&thread->t_ctl_waitq);
1340 CDEBUG(D_DLMTRACE, "%s: pool thread starting, process %d\n",
1341 "ldlm_poold", current_pid());
1344 struct l_wait_info lwi;
1347 * Recal all pools on this tick.
1349 s_time = ldlm_pools_recalc(LDLM_NAMESPACE_SERVER);
1350 c_time = ldlm_pools_recalc(LDLM_NAMESPACE_CLIENT);
1353 * Wait until the next check time, or until we're
1356 lwi = LWI_TIMEOUT(cfs_time_seconds(min(s_time, c_time)),
1358 l_wait_event(thread->t_ctl_waitq,
1359 thread_is_stopping(thread) ||
1360 thread_is_event(thread),
1363 if (thread_test_and_clear_flags(thread, SVC_STOPPING))
1366 thread_test_and_clear_flags(thread, SVC_EVENT);
1369 thread_set_flags(thread, SVC_STOPPED);
1370 wake_up(&thread->t_ctl_waitq);
1372 CDEBUG(D_DLMTRACE, "%s: pool thread exiting, process %d\n",
1373 "ldlm_poold", current_pid());
1375 complete_and_exit(&ldlm_pools_comp, 0);
1378 static int ldlm_pools_thread_start(void)
1380 struct l_wait_info lwi = { 0 };
1381 struct task_struct *task;
1383 if (ldlm_pools_thread != NULL)
1386 OBD_ALLOC_PTR(ldlm_pools_thread);
1387 if (ldlm_pools_thread == NULL)
1390 init_completion(&ldlm_pools_comp);
1391 init_waitqueue_head(&ldlm_pools_thread->t_ctl_waitq);
1393 task = kthread_run(ldlm_pools_thread_main, ldlm_pools_thread,
1396 CERROR("Can't start pool thread, error %ld\n", PTR_ERR(task));
1397 OBD_FREE(ldlm_pools_thread, sizeof(*ldlm_pools_thread));
1398 ldlm_pools_thread = NULL;
1399 return PTR_ERR(task);
1401 l_wait_event(ldlm_pools_thread->t_ctl_waitq,
1402 thread_is_running(ldlm_pools_thread), &lwi);
1406 static void ldlm_pools_thread_stop(void)
1408 if (ldlm_pools_thread == NULL)
1411 thread_set_flags(ldlm_pools_thread, SVC_STOPPING);
1412 wake_up(&ldlm_pools_thread->t_ctl_waitq);
1415 * Make sure that pools thread is finished before freeing @thread.
1416 * This fixes possible race and oops due to accessing freed memory
1419 wait_for_completion(&ldlm_pools_comp);
1420 OBD_FREE_PTR(ldlm_pools_thread);
1421 ldlm_pools_thread = NULL;
1424 static struct shrinker ldlm_pools_srv_shrinker = {
1425 .count_objects = ldlm_pools_srv_count,
1426 .scan_objects = ldlm_pools_srv_scan,
1427 .seeks = DEFAULT_SEEKS,
1430 static struct shrinker ldlm_pools_cli_shrinker = {
1431 .count_objects = ldlm_pools_cli_count,
1432 .scan_objects = ldlm_pools_cli_scan,
1433 .seeks = DEFAULT_SEEKS,
1436 int ldlm_pools_init(void)
1440 rc = ldlm_pools_thread_start();
1442 register_shrinker(&ldlm_pools_srv_shrinker);
1443 register_shrinker(&ldlm_pools_cli_shrinker);
1447 EXPORT_SYMBOL(ldlm_pools_init);
1449 void ldlm_pools_fini(void)
1451 unregister_shrinker(&ldlm_pools_srv_shrinker);
1452 unregister_shrinker(&ldlm_pools_cli_shrinker);
1453 ldlm_pools_thread_stop();
1455 EXPORT_SYMBOL(ldlm_pools_fini);