1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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 sensitive
58 * client should be about last SLV from server. The higher LVF is the more locks
59 * will be canceled on client. Default value for it is 1. Setting LVF to 2 means
60 * 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). Andreas
74 * Dilger (adilger@clusterfs.com) proposed few nice ideas like using LVF and many
75 * cleanups. Flow definition to allow more easy understanding of the logic belongs
76 * to Nikita Danilov (nikita@clusterfs.com) as well as many cleanups and fixes.
77 * And design and implementation are done by Yury Umanets (umka@clusterfs.com).
79 * Glossary for terms used:
81 * pl_limit - Number of allowed locks in pool. Applies to server and client
84 * pl_granted - Number of granted locks (calculated);
85 * pl_grant_rate - Number of granted locks for last T (calculated);
86 * pl_cancel_rate - Number of canceled locks for last T (calculated);
87 * pl_grant_speed - Grant speed (GR - CR) for last T (calculated);
88 * pl_grant_plan - Planned number of granted locks for next T (calculated);
90 * pl_grant_step - Grant plan step, that is how ->pl_grant_plan
91 * will change in next T (tunable);
93 * pl_server_lock_volume - Current server lock volume (calculated);
95 * As it may be seen from list above, we have few possible tunables which may
96 * affect behavior much. They all may be modified via proc. However, they also
97 * give a possibility for constructing few pre-defined behavior policies. If
98 * none of predefines is suitable for a working pattern being used, new one may
99 * be "constructed" via proc tunables.
102 #define DEBUG_SUBSYSTEM S_LDLM
105 # include <lustre_dlm.h>
107 # include <liblustre.h>
110 #include <obd_class.h>
111 #include <obd_support.h>
112 #include "ldlm_internal.h"
114 #ifdef HAVE_LRU_RESIZE_SUPPORT
117 * 50 ldlm locks for 1MB of RAM.
119 #define LDLM_POOL_HOST_L ((num_physpages >> (20 - CFS_PAGE_SHIFT)) * 50)
122 * Default step in % for grant plan.
124 #define LDLM_POOL_GSP (10)
127 * LDLM_POOL_GSP% of all locks is default GP.
129 #define LDLM_POOL_GP(L) (((L) * LDLM_POOL_GSP) / 100)
132 * Max age for locks on clients.
134 #define LDLM_POOL_MAX_AGE (36000)
137 extern cfs_proc_dir_entry_t *ldlm_ns_proc_dir;
140 #define avg(src, add) \
141 ((src) = ((src) + (add)) / 2)
143 static inline __u64 dru(__u64 val, __u32 div)
145 __u64 ret = val + (div - 1);
150 static inline __u64 ldlm_pool_slv_max(__u32 L)
153 * Allow to have all locks for 1 client for 10 hrs.
154 * Formula is the following: limit * 10h / 1 client.
156 __u64 lim = L * LDLM_POOL_MAX_AGE / 1;
160 static inline __u64 ldlm_pool_slv_min(__u32 L)
166 LDLM_POOL_FIRST_STAT = 0,
167 LDLM_POOL_GRANTED_STAT = LDLM_POOL_FIRST_STAT,
168 LDLM_POOL_GRANT_STAT,
169 LDLM_POOL_CANCEL_STAT,
170 LDLM_POOL_GRANT_RATE_STAT,
171 LDLM_POOL_CANCEL_RATE_STAT,
172 LDLM_POOL_GRANT_PLAN_STAT,
174 LDLM_POOL_SHRINK_REQTD_STAT,
175 LDLM_POOL_SHRINK_FREED_STAT,
176 LDLM_POOL_RECALC_STAT,
177 LDLM_POOL_TIMING_STAT,
181 static inline struct ldlm_namespace *ldlm_pl2ns(struct ldlm_pool *pl)
183 return container_of(pl, struct ldlm_namespace, ns_pool);
187 * Recalculates next grant limit on passed \a pl.
189 * \pre ->pl_lock is locked.
191 static inline void ldlm_pool_recalc_grant_plan(struct ldlm_pool *pl)
193 int granted, grant_step, limit;
195 limit = ldlm_pool_get_limit(pl);
196 granted = atomic_read(&pl->pl_granted);
198 grant_step = ((limit - granted) * pl->pl_grant_step) / 100;
199 pl->pl_grant_plan = granted + grant_step;
203 * Recalculates next SLV on passed \a pl.
205 * \pre ->pl_lock is locked.
207 static inline void ldlm_pool_recalc_slv(struct ldlm_pool *pl)
209 int grant_usage, granted, grant_plan;
210 __u64 slv, slv_factor;
213 slv = pl->pl_server_lock_volume;
214 grant_plan = pl->pl_grant_plan;
215 limit = ldlm_pool_get_limit(pl);
216 granted = atomic_read(&pl->pl_granted);
218 grant_usage = limit - (granted - grant_plan);
219 if (grant_usage <= 0)
223 * Find out SLV change factor which is the ratio of grant usage
224 * from limit. SLV changes as fast as the ratio of grant plan
225 * consumtion. The more locks from grant plan are not consumed
226 * by clients in last interval (idle time), the faster grows
227 * SLV. And the opposite, the more grant plan is over-consumed
228 * (load time) the faster drops SLV.
230 slv_factor = (grant_usage * 100) / limit;
231 if (2 * abs(granted - limit) > limit) {
232 slv_factor *= slv_factor;
233 slv_factor = dru(slv_factor, 100);
235 slv = slv * slv_factor;
238 if (slv > ldlm_pool_slv_max(limit)) {
239 slv = ldlm_pool_slv_max(limit);
240 } else if (slv < ldlm_pool_slv_min(limit)) {
241 slv = ldlm_pool_slv_min(limit);
244 pl->pl_server_lock_volume = slv;
248 * Recalculates next stats on passed \a pl.
250 * \pre ->pl_lock is locked.
252 static inline void ldlm_pool_recalc_stats(struct ldlm_pool *pl)
254 int grant_plan = pl->pl_grant_plan;
255 __u64 slv = pl->pl_server_lock_volume;
256 int granted = atomic_read(&pl->pl_granted);
257 int grant_rate = atomic_read(&pl->pl_grant_rate);
258 int cancel_rate = atomic_read(&pl->pl_cancel_rate);
260 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_SLV_STAT,
262 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
264 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
266 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
268 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
273 * Sets current SLV into obd accessible via ldlm_pl2ns(pl)->ns_obd.
275 static void ldlm_srv_pool_push_slv(struct ldlm_pool *pl)
277 struct obd_device *obd;
280 * Set new SLV in obd field for using it later without accessing the
281 * pool. This is required to avoid race between sending reply to client
282 * with new SLV and cleanup server stack in which we can't guarantee
283 * that namespace is still alive. We know only that obd is alive as
284 * long as valid export is alive.
286 obd = ldlm_pl2ns(pl)->ns_obd;
287 LASSERT(obd != NULL);
288 write_lock(&obd->obd_pool_lock);
289 obd->obd_pool_slv = pl->pl_server_lock_volume;
290 write_unlock(&obd->obd_pool_lock);
294 * Recalculates all pool fields on passed \a pl.
296 * \pre ->pl_lock is not locked.
298 static int ldlm_srv_pool_recalc(struct ldlm_pool *pl)
300 time_t recalc_interval_sec;
303 spin_lock(&pl->pl_lock);
304 recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
305 if (recalc_interval_sec > 0) {
309 ldlm_pool_recalc_stats(pl);
312 * Recalc SLV after last period. This should be done
313 * _before_ recalculating new grant plan.
315 ldlm_pool_recalc_slv(pl);
318 * Make sure that pool informed obd of last SLV changes.
320 ldlm_srv_pool_push_slv(pl);
323 * Update grant_plan for new period.
325 ldlm_pool_recalc_grant_plan(pl);
328 * Zero out all rates and speed for the last period.
330 atomic_set(&pl->pl_grant_rate, 0);
331 atomic_set(&pl->pl_cancel_rate, 0);
332 atomic_set(&pl->pl_grant_speed, 0);
333 pl->pl_recalc_time = cfs_time_current_sec();
334 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
335 recalc_interval_sec);
337 spin_unlock(&pl->pl_lock);
342 * This function is used on server side as main entry point for memory
343 * preasure handling. It decreases SLV on \a pl according to passed
344 * \a nr and \a gfp_mask.
346 * Our goal here is to decrease SLV such a way that clients hold \a nr
347 * locks smaller in next 10h.
349 static int ldlm_srv_pool_shrink(struct ldlm_pool *pl,
350 int nr, unsigned int gfp_mask)
356 * VM is asking how many entries may be potentially freed.
359 RETURN(atomic_read(&pl->pl_granted));
362 * Client already canceled locks but server is already in shrinker
363 * and can't cancel anything. Let's catch this race.
365 if (atomic_read(&pl->pl_granted) == 0)
368 spin_lock(&pl->pl_lock);
371 * We want shrinker to possibly cause cancelation of @nr locks from
372 * clients or grant approximately @nr locks smaller next intervals.
374 * This is why we decresed SLV by @nr. This effect will only be as
375 * long as one re-calc interval (1s these days) and this should be
376 * enough to pass this decreased SLV to all clients. On next recalc
377 * interval pool will either increase SLV if locks load is not high
378 * or will keep on same level or even decrease again, thus, shrinker
379 * decreased SLV will affect next recalc intervals and this way will
380 * make locking load lower.
382 if (nr < pl->pl_server_lock_volume) {
383 pl->pl_server_lock_volume = pl->pl_server_lock_volume - nr;
385 limit = ldlm_pool_get_limit(pl);
386 pl->pl_server_lock_volume = ldlm_pool_slv_min(limit);
390 * Make sure that pool informed obd of last SLV changes.
392 ldlm_srv_pool_push_slv(pl);
393 spin_unlock(&pl->pl_lock);
396 * We did not really free any memory here so far, it only will be
397 * freed later may be, so that we return 0 to not confuse VM.
403 * Setup server side pool \a pl with passed \a limit.
405 static int ldlm_srv_pool_setup(struct ldlm_pool *pl, int limit)
407 struct obd_device *obd;
410 obd = ldlm_pl2ns(pl)->ns_obd;
411 LASSERT(obd != NULL && obd != LP_POISON);
412 LASSERT(obd->obd_type != LP_POISON);
413 write_lock(&obd->obd_pool_lock);
414 obd->obd_pool_limit = limit;
415 write_unlock(&obd->obd_pool_lock);
417 ldlm_pool_set_limit(pl, limit);
422 * Sets SLV and Limit from ldlm_pl2ns(pl)->ns_obd tp passed \a pl.
424 static void ldlm_cli_pool_pop_slv(struct ldlm_pool *pl)
426 struct obd_device *obd;
429 * Get new SLV and Limit from obd which is updated with comming
432 obd = ldlm_pl2ns(pl)->ns_obd;
433 LASSERT(obd != NULL);
434 read_lock(&obd->obd_pool_lock);
435 pl->pl_server_lock_volume = obd->obd_pool_slv;
436 ldlm_pool_set_limit(pl, obd->obd_pool_limit);
437 read_unlock(&obd->obd_pool_lock);
441 * Recalculates client sise pool \a pl according to current SLV and Limit.
443 static int ldlm_cli_pool_recalc(struct ldlm_pool *pl)
445 time_t recalc_interval_sec;
448 spin_lock(&pl->pl_lock);
451 * Make sure that pool knows last SLV and Limit from obd.
453 ldlm_cli_pool_pop_slv(pl);
455 recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
456 if (recalc_interval_sec > 0) {
458 * Update statistics only every T.
460 ldlm_pool_recalc_stats(pl);
463 * Zero out grant/cancel rates and speed for last period.
465 atomic_set(&pl->pl_grant_rate, 0);
466 atomic_set(&pl->pl_cancel_rate, 0);
467 atomic_set(&pl->pl_grant_speed, 0);
468 pl->pl_recalc_time = cfs_time_current_sec();
469 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
470 recalc_interval_sec);
472 spin_unlock(&pl->pl_lock);
475 * Do not cancel locks in case lru resize is disabled for this ns.
477 if (!ns_connect_lru_resize(ldlm_pl2ns(pl)))
481 * In the time of canceling locks on client we do not need to maintain
482 * sharp timing, we only want to cancel locks asap according to new SLV.
483 * It may be called when SLV has changed much, this is why we do not
484 * take into account pl->pl_recalc_time here.
486 RETURN(ldlm_cancel_lru(ldlm_pl2ns(pl), 0, LDLM_ASYNC,
491 * This function is main entry point for memory preasure handling on client side.
492 * Main goal of this function is to cancel some number of locks on passed \a pl
493 * according to \a nr and \a gfp_mask.
495 static int ldlm_cli_pool_shrink(struct ldlm_pool *pl,
496 int nr, unsigned int gfp_mask)
501 * Do not cancel locks in case lru resize is disabled for this ns.
503 if (!ns_connect_lru_resize(ldlm_pl2ns(pl)))
507 * Make sure that pool knows last SLV and Limit from obd.
509 ldlm_cli_pool_pop_slv(pl);
512 * Find out how many locks may be released according to shrink
516 RETURN(ldlm_cancel_lru_estimate(ldlm_pl2ns(pl), 0, 0,
517 LDLM_CANCEL_SHRINK));
520 * Cancel @nr locks accoding to shrink policy.
522 RETURN(ldlm_cancel_lru(ldlm_pl2ns(pl), nr, LDLM_SYNC,
523 LDLM_CANCEL_SHRINK));
526 struct ldlm_pool_ops ldlm_srv_pool_ops = {
527 .po_recalc = ldlm_srv_pool_recalc,
528 .po_shrink = ldlm_srv_pool_shrink,
529 .po_setup = ldlm_srv_pool_setup
532 struct ldlm_pool_ops ldlm_cli_pool_ops = {
533 .po_recalc = ldlm_cli_pool_recalc,
534 .po_shrink = ldlm_cli_pool_shrink
538 * Pool recalc wrapper. Will call either client or server pool recalc callback
539 * depending what pool \a pl is used.
541 int ldlm_pool_recalc(struct ldlm_pool *pl)
545 if (pl->pl_ops->po_recalc != NULL) {
546 count = pl->pl_ops->po_recalc(pl);
547 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_RECALC_STAT,
553 EXPORT_SYMBOL(ldlm_pool_recalc);
556 * Pool shrink wrapper. Will call either client or server pool recalc callback
557 * depending what pool \a pl is used.
559 int ldlm_pool_shrink(struct ldlm_pool *pl, int nr,
560 unsigned int gfp_mask)
564 if (pl->pl_ops->po_shrink != NULL) {
565 cancel = pl->pl_ops->po_shrink(pl, nr, gfp_mask);
567 lprocfs_counter_add(pl->pl_stats,
568 LDLM_POOL_SHRINK_REQTD_STAT,
570 lprocfs_counter_add(pl->pl_stats,
571 LDLM_POOL_SHRINK_FREED_STAT,
573 CDEBUG(D_DLMTRACE, "%s: request to shrink %d locks, "
574 "shrunk %d\n", pl->pl_name, nr, cancel);
579 EXPORT_SYMBOL(ldlm_pool_shrink);
582 * Pool setup wrapper. Will call either client or server pool recalc callback
583 * depending what pool \a pl is used.
585 * Sets passed \a limit into pool \a pl.
587 int ldlm_pool_setup(struct ldlm_pool *pl, int limit)
590 if (pl->pl_ops->po_setup != NULL)
591 RETURN(pl->pl_ops->po_setup(pl, limit));
594 EXPORT_SYMBOL(ldlm_pool_setup);
597 static int lprocfs_rd_pool_state(char *page, char **start, off_t off,
598 int count, int *eof, void *data)
600 int granted, grant_rate, cancel_rate, grant_step;
601 int nr = 0, grant_speed, grant_plan;
602 struct ldlm_pool *pl = data;
606 spin_lock(&pl->pl_lock);
607 slv = pl->pl_server_lock_volume;
608 clv = pl->pl_client_lock_volume;
609 limit = ldlm_pool_get_limit(pl);
610 grant_plan = pl->pl_grant_plan;
611 grant_step = pl->pl_grant_step;
612 granted = atomic_read(&pl->pl_granted);
613 grant_rate = atomic_read(&pl->pl_grant_rate);
614 grant_speed = atomic_read(&pl->pl_grant_speed);
615 cancel_rate = atomic_read(&pl->pl_cancel_rate);
616 spin_unlock(&pl->pl_lock);
618 nr += snprintf(page + nr, count - nr, "LDLM pool state (%s):\n",
620 nr += snprintf(page + nr, count - nr, " SLV: "LPU64"\n", slv);
621 nr += snprintf(page + nr, count - nr, " CLV: "LPU64"\n", clv);
623 nr += snprintf(page + nr, count - nr, " LVF: %d\n",
624 atomic_read(&pl->pl_lock_volume_factor));
626 nr += snprintf(page + nr, count - nr, " GSP: %d%%\n",
628 nr += snprintf(page + nr, count - nr, " GP: %d\n",
630 nr += snprintf(page + nr, count - nr, " GR: %d\n",
632 nr += snprintf(page + nr, count - nr, " CR: %d\n",
634 nr += snprintf(page + nr, count - nr, " GS: %d\n",
636 nr += snprintf(page + nr, count - nr, " G: %d\n",
638 nr += snprintf(page + nr, count - nr, " L: %d\n",
643 LDLM_POOL_PROC_READER(grant_plan, int);
644 LDLM_POOL_PROC_READER(grant_step, int);
645 LDLM_POOL_PROC_WRITER(grant_step, int);
647 static int ldlm_pool_proc_init(struct ldlm_pool *pl)
649 struct ldlm_namespace *ns = ldlm_pl2ns(pl);
650 struct proc_dir_entry *parent_ns_proc;
651 struct lprocfs_vars pool_vars[2];
652 char *var_name = NULL;
656 OBD_ALLOC(var_name, MAX_STRING_SIZE + 1);
660 parent_ns_proc = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
661 if (parent_ns_proc == NULL) {
662 CERROR("%s: proc entry is not initialized\n",
664 GOTO(out_free_name, rc = -EINVAL);
666 pl->pl_proc_dir = lprocfs_register("pool", parent_ns_proc,
668 if (IS_ERR(pl->pl_proc_dir)) {
669 CERROR("LProcFS failed in ldlm-pool-init\n");
670 rc = PTR_ERR(pl->pl_proc_dir);
671 GOTO(out_free_name, rc);
674 var_name[MAX_STRING_SIZE] = '\0';
675 memset(pool_vars, 0, sizeof(pool_vars));
676 pool_vars[0].name = var_name;
678 snprintf(var_name, MAX_STRING_SIZE, "server_lock_volume");
679 pool_vars[0].data = &pl->pl_server_lock_volume;
680 pool_vars[0].read_fptr = lprocfs_rd_u64;
681 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
683 snprintf(var_name, MAX_STRING_SIZE, "limit");
684 pool_vars[0].data = &pl->pl_limit;
685 pool_vars[0].read_fptr = lprocfs_rd_atomic;
686 pool_vars[0].write_fptr = lprocfs_wr_atomic;
687 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
689 snprintf(var_name, MAX_STRING_SIZE, "granted");
690 pool_vars[0].data = &pl->pl_granted;
691 pool_vars[0].read_fptr = lprocfs_rd_atomic;
692 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
694 snprintf(var_name, MAX_STRING_SIZE, "grant_speed");
695 pool_vars[0].data = &pl->pl_grant_speed;
696 pool_vars[0].read_fptr = lprocfs_rd_atomic;
697 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
699 snprintf(var_name, MAX_STRING_SIZE, "cancel_rate");
700 pool_vars[0].data = &pl->pl_cancel_rate;
701 pool_vars[0].read_fptr = lprocfs_rd_atomic;
702 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
704 snprintf(var_name, MAX_STRING_SIZE, "grant_rate");
705 pool_vars[0].data = &pl->pl_grant_rate;
706 pool_vars[0].read_fptr = lprocfs_rd_atomic;
707 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
709 snprintf(var_name, MAX_STRING_SIZE, "grant_plan");
710 pool_vars[0].data = pl;
711 pool_vars[0].read_fptr = lprocfs_rd_grant_plan;
712 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
714 snprintf(var_name, MAX_STRING_SIZE, "grant_step");
715 pool_vars[0].data = pl;
716 pool_vars[0].read_fptr = lprocfs_rd_grant_step;
717 if (ns_is_server(ns))
718 pool_vars[0].write_fptr = lprocfs_wr_grant_step;
719 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
721 snprintf(var_name, MAX_STRING_SIZE, "lock_volume_factor");
722 pool_vars[0].data = &pl->pl_lock_volume_factor;
723 pool_vars[0].read_fptr = lprocfs_rd_atomic;
724 pool_vars[0].write_fptr = lprocfs_wr_atomic;
725 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
727 snprintf(var_name, MAX_STRING_SIZE, "state");
728 pool_vars[0].data = pl;
729 pool_vars[0].read_fptr = lprocfs_rd_pool_state;
730 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
732 pl->pl_stats = lprocfs_alloc_stats(LDLM_POOL_LAST_STAT -
733 LDLM_POOL_FIRST_STAT, 0);
735 GOTO(out_free_name, rc = -ENOMEM);
737 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
738 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
740 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_STAT,
741 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
743 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_STAT,
744 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
746 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
747 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
748 "grant_rate", "locks/s");
749 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
750 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
751 "cancel_rate", "locks/s");
752 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
753 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
754 "grant_plan", "locks/s");
755 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SLV_STAT,
756 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
758 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_REQTD_STAT,
759 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
760 "shrink_request", "locks");
761 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_FREED_STAT,
762 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
763 "shrink_freed", "locks");
764 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_RECALC_STAT,
765 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
766 "recalc_freed", "locks");
767 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_TIMING_STAT,
768 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
769 "recalc_timing", "sec");
770 lprocfs_register_stats(pl->pl_proc_dir, "stats", pl->pl_stats);
774 OBD_FREE(var_name, MAX_STRING_SIZE + 1);
778 static void ldlm_pool_proc_fini(struct ldlm_pool *pl)
780 if (pl->pl_stats != NULL) {
781 lprocfs_free_stats(&pl->pl_stats);
784 if (pl->pl_proc_dir != NULL) {
785 lprocfs_remove(&pl->pl_proc_dir);
786 pl->pl_proc_dir = NULL;
789 #else /* !__KERNEL__*/
790 #define ldlm_pool_proc_init(pl) (0)
791 #define ldlm_pool_proc_fini(pl) while (0) {}
794 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
795 int idx, ldlm_side_t client)
800 spin_lock_init(&pl->pl_lock);
801 atomic_set(&pl->pl_granted, 0);
802 pl->pl_recalc_time = cfs_time_current_sec();
803 atomic_set(&pl->pl_lock_volume_factor, 1);
805 atomic_set(&pl->pl_grant_rate, 0);
806 atomic_set(&pl->pl_cancel_rate, 0);
807 atomic_set(&pl->pl_grant_speed, 0);
808 pl->pl_grant_step = LDLM_POOL_GSP;
809 pl->pl_grant_plan = LDLM_POOL_GP(LDLM_POOL_HOST_L);
811 snprintf(pl->pl_name, sizeof(pl->pl_name), "ldlm-pool-%s-%d",
814 if (client == LDLM_NAMESPACE_SERVER) {
815 pl->pl_ops = &ldlm_srv_pool_ops;
816 ldlm_pool_set_limit(pl, LDLM_POOL_HOST_L);
817 pl->pl_server_lock_volume = ldlm_pool_slv_max(LDLM_POOL_HOST_L);
819 pl->pl_server_lock_volume = 1;
820 ldlm_pool_set_limit(pl, 1);
821 pl->pl_ops = &ldlm_cli_pool_ops;
823 pl->pl_client_lock_volume = 0;
824 rc = ldlm_pool_proc_init(pl);
828 CDEBUG(D_DLMTRACE, "Lock pool %s is initialized\n", pl->pl_name);
832 EXPORT_SYMBOL(ldlm_pool_init);
834 void ldlm_pool_fini(struct ldlm_pool *pl)
837 ldlm_pool_proc_fini(pl);
840 * Pool should not be used after this point. We can't free it here as
841 * it lives in struct ldlm_namespace, but still interested in catching
842 * any abnormal using cases.
844 POISON(pl, 0x5a, sizeof(*pl));
847 EXPORT_SYMBOL(ldlm_pool_fini);
850 * Add new taken ldlm lock \a lock into pool \a pl accounting.
852 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
855 * FLOCK locks are special in a sense that they are almost never
856 * cancelled, instead special kind of lock is used to drop them.
857 * also there is no LRU for flock locks, so no point in tracking
860 if (lock->l_resource->lr_type == LDLM_FLOCK)
865 atomic_inc(&pl->pl_granted);
866 atomic_inc(&pl->pl_grant_rate);
867 atomic_inc(&pl->pl_grant_speed);
869 lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_GRANT_STAT);
872 * Do not do pool recalc for client side as all locks which
873 * potentially may be canceled has already been packed into
874 * enqueue/cancel rpc. Also we do not want to run out of stack
875 * with too long call paths.
877 if (ns_is_server(ldlm_pl2ns(pl)))
878 ldlm_pool_recalc(pl);
881 EXPORT_SYMBOL(ldlm_pool_add);
884 * Remove ldlm lock \a lock from pool \a pl accounting.
886 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
889 * Filter out FLOCK locks. Read above comment in ldlm_pool_add().
891 if (lock->l_resource->lr_type == LDLM_FLOCK)
894 LASSERT(atomic_read(&pl->pl_granted) > 0);
895 atomic_dec(&pl->pl_granted);
896 atomic_inc(&pl->pl_cancel_rate);
897 atomic_dec(&pl->pl_grant_speed);
899 lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_CANCEL_STAT);
901 if (ns_is_server(ldlm_pl2ns(pl)))
902 ldlm_pool_recalc(pl);
905 EXPORT_SYMBOL(ldlm_pool_del);
908 * Returns current \a pl SLV.
910 * \pre ->pl_lock is not locked.
912 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
915 spin_lock(&pl->pl_lock);
916 slv = pl->pl_server_lock_volume;
917 spin_unlock(&pl->pl_lock);
920 EXPORT_SYMBOL(ldlm_pool_get_slv);
923 * Sets passed \a slv to \a pl.
925 * \pre ->pl_lock is not locked.
927 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv)
929 spin_lock(&pl->pl_lock);
930 pl->pl_server_lock_volume = slv;
931 spin_unlock(&pl->pl_lock);
933 EXPORT_SYMBOL(ldlm_pool_set_slv);
936 * Returns current \a pl CLV.
938 * \pre ->pl_lock is not locked.
940 __u64 ldlm_pool_get_clv(struct ldlm_pool *pl)
943 spin_lock(&pl->pl_lock);
944 slv = pl->pl_client_lock_volume;
945 spin_unlock(&pl->pl_lock);
948 EXPORT_SYMBOL(ldlm_pool_get_clv);
951 * Sets passed \a clv to \a pl.
953 * \pre ->pl_lock is not locked.
955 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv)
957 spin_lock(&pl->pl_lock);
958 pl->pl_client_lock_volume = clv;
959 spin_unlock(&pl->pl_lock);
961 EXPORT_SYMBOL(ldlm_pool_set_clv);
964 * Returns current \a pl limit.
966 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
968 return atomic_read(&pl->pl_limit);
970 EXPORT_SYMBOL(ldlm_pool_get_limit);
973 * Sets passed \a limit to \a pl.
975 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
977 atomic_set(&pl->pl_limit, limit);
979 EXPORT_SYMBOL(ldlm_pool_set_limit);
982 * Returns current LVF from \a pl.
984 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
986 return atomic_read(&pl->pl_lock_volume_factor);
988 EXPORT_SYMBOL(ldlm_pool_get_lvf);
991 static int ldlm_pool_granted(struct ldlm_pool *pl)
993 return atomic_read(&pl->pl_granted);
996 static struct ptlrpc_thread *ldlm_pools_thread;
997 static struct shrinker *ldlm_pools_srv_shrinker;
998 static struct shrinker *ldlm_pools_cli_shrinker;
999 static struct completion ldlm_pools_comp;
1001 void ldlm_pools_wakeup(void)
1004 if (ldlm_pools_thread == NULL)
1006 ldlm_pools_thread->t_flags |= SVC_EVENT;
1007 cfs_waitq_signal(&ldlm_pools_thread->t_ctl_waitq);
1010 EXPORT_SYMBOL(ldlm_pools_wakeup);
1013 * Cancel \a nr locks from all namespaces (if possible). Returns number of
1014 * cached locks after shrink is finished. All namespaces are asked to
1015 * cancel approximately equal amount of locks to keep balancing.
1017 static int ldlm_pools_shrink(ldlm_side_t client, int nr,
1018 unsigned int gfp_mask)
1020 int total = 0, cached = 0, nr_ns;
1021 struct ldlm_namespace *ns;
1023 if (nr != 0 && !(gfp_mask & __GFP_FS))
1026 CDEBUG(D_DLMTRACE, "Request to shrink %d %s locks from all pools\n",
1027 nr, client == LDLM_NAMESPACE_CLIENT ? "client" : "server");
1030 * Find out how many resources we may release.
1032 for (nr_ns = atomic_read(ldlm_namespace_nr(client));
1035 mutex_down(ldlm_namespace_lock(client));
1036 if (list_empty(ldlm_namespace_list(client))) {
1037 mutex_up(ldlm_namespace_lock(client));
1040 ns = ldlm_namespace_first_locked(client);
1041 ldlm_namespace_get(ns);
1042 ldlm_namespace_move_locked(ns, client);
1043 mutex_up(ldlm_namespace_lock(client));
1044 total += ldlm_pool_shrink(&ns->ns_pool, 0, gfp_mask);
1045 ldlm_namespace_put(ns, 1);
1048 if (nr == 0 || total == 0)
1052 * Shrink at least ldlm_namespace_nr(client) namespaces.
1054 for (nr_ns = atomic_read(ldlm_namespace_nr(client));
1057 int cancel, nr_locks;
1060 * Do not call shrink under ldlm_namespace_lock(client)
1062 mutex_down(ldlm_namespace_lock(client));
1063 if (list_empty(ldlm_namespace_list(client))) {
1064 mutex_up(ldlm_namespace_lock(client));
1066 * If list is empty, we can't return any @cached > 0,
1067 * that probably would cause needless shrinker
1073 ns = ldlm_namespace_first_locked(client);
1074 ldlm_namespace_get(ns);
1075 ldlm_namespace_move_locked(ns, client);
1076 mutex_up(ldlm_namespace_lock(client));
1078 nr_locks = ldlm_pool_granted(&ns->ns_pool);
1079 cancel = 1 + nr_locks * nr / total;
1080 ldlm_pool_shrink(&ns->ns_pool, cancel, gfp_mask);
1081 cached += ldlm_pool_granted(&ns->ns_pool);
1082 ldlm_namespace_put(ns, 1);
1087 static int ldlm_pools_srv_shrink(int nr, unsigned int gfp_mask)
1089 return ldlm_pools_shrink(LDLM_NAMESPACE_SERVER, nr, gfp_mask);
1092 static int ldlm_pools_cli_shrink(int nr, unsigned int gfp_mask)
1094 return ldlm_pools_shrink(LDLM_NAMESPACE_CLIENT, nr, gfp_mask);
1097 void ldlm_pools_recalc(ldlm_side_t client)
1099 __u32 nr_l = 0, nr_p = 0, l;
1100 struct ldlm_namespace *ns;
1104 * No need to setup pool limit for client pools.
1106 if (client == LDLM_NAMESPACE_SERVER) {
1108 * Check all modest namespaces first.
1110 mutex_down(ldlm_namespace_lock(client));
1111 list_for_each_entry(ns, ldlm_namespace_list(client),
1114 if (ns->ns_appetite != LDLM_NAMESPACE_MODEST)
1117 l = ldlm_pool_granted(&ns->ns_pool);
1122 * Set the modest pools limit equal to their avg granted
1125 l += dru(l * LDLM_POOLS_MODEST_MARGIN, 100);
1126 ldlm_pool_setup(&ns->ns_pool, l);
1132 * Make sure that modest namespaces did not eat more that 2/3
1135 if (nr_l >= 2 * (LDLM_POOL_HOST_L / 3)) {
1136 CWARN("\"Modest\" pools eat out 2/3 of server locks "
1137 "limit (%d of %lu). This means that you have too "
1138 "many clients for this amount of server RAM. "
1139 "Upgrade server!\n", nr_l, LDLM_POOL_HOST_L);
1144 * The rest is given to greedy namespaces.
1146 list_for_each_entry(ns, ldlm_namespace_list(client),
1149 if (!equal && ns->ns_appetite != LDLM_NAMESPACE_GREEDY)
1154 * In the case 2/3 locks are eaten out by
1155 * modest pools, we re-setup equal limit
1158 l = LDLM_POOL_HOST_L /
1159 atomic_read(ldlm_namespace_nr(client));
1162 * All the rest of greedy pools will have
1163 * all locks in equal parts.
1165 l = (LDLM_POOL_HOST_L - nr_l) /
1166 (atomic_read(ldlm_namespace_nr(client)) -
1169 ldlm_pool_setup(&ns->ns_pool, l);
1171 mutex_up(ldlm_namespace_lock(client));
1175 * Recalc at least ldlm_namespace_nr(client) namespaces.
1177 for (nr = atomic_read(ldlm_namespace_nr(client)); nr > 0; nr--) {
1179 * Lock the list, get first @ns in the list, getref, move it
1180 * to the tail, unlock and call pool recalc. This way we avoid
1181 * calling recalc under @ns lock what is really good as we get
1182 * rid of potential deadlock on client nodes when canceling
1183 * locks synchronously.
1185 mutex_down(ldlm_namespace_lock(client));
1186 if (list_empty(ldlm_namespace_list(client))) {
1187 mutex_up(ldlm_namespace_lock(client));
1190 ns = ldlm_namespace_first_locked(client);
1191 ldlm_namespace_get(ns);
1192 ldlm_namespace_move_locked(ns, client);
1193 mutex_up(ldlm_namespace_lock(client));
1196 * After setup is done - recalc the pool.
1198 ldlm_pool_recalc(&ns->ns_pool);
1199 ldlm_namespace_put(ns, 1);
1202 EXPORT_SYMBOL(ldlm_pools_recalc);
1204 static int ldlm_pools_thread_main(void *arg)
1206 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
1207 char *t_name = "ldlm_poold";
1210 cfs_daemonize(t_name);
1211 thread->t_flags = SVC_RUNNING;
1212 cfs_waitq_signal(&thread->t_ctl_waitq);
1214 CDEBUG(D_DLMTRACE, "%s: pool thread starting, process %d\n",
1215 t_name, cfs_curproc_pid());
1218 struct l_wait_info lwi;
1221 * Recal all pools on this tick.
1223 ldlm_pools_recalc(LDLM_NAMESPACE_SERVER);
1224 ldlm_pools_recalc(LDLM_NAMESPACE_CLIENT);
1227 * Wait until the next check time, or until we're
1230 lwi = LWI_TIMEOUT(cfs_time_seconds(LDLM_POOLS_THREAD_PERIOD),
1232 l_wait_event(thread->t_ctl_waitq, (thread->t_flags &
1233 (SVC_STOPPING|SVC_EVENT)),
1236 if (thread->t_flags & SVC_STOPPING) {
1237 thread->t_flags &= ~SVC_STOPPING;
1239 } else if (thread->t_flags & SVC_EVENT) {
1240 thread->t_flags &= ~SVC_EVENT;
1244 thread->t_flags = SVC_STOPPED;
1245 cfs_waitq_signal(&thread->t_ctl_waitq);
1247 CDEBUG(D_DLMTRACE, "%s: pool thread exiting, process %d\n",
1248 t_name, cfs_curproc_pid());
1250 complete_and_exit(&ldlm_pools_comp, 0);
1253 static int ldlm_pools_thread_start(void)
1255 struct l_wait_info lwi = { 0 };
1259 if (ldlm_pools_thread != NULL)
1262 OBD_ALLOC_PTR(ldlm_pools_thread);
1263 if (ldlm_pools_thread == NULL)
1266 init_completion(&ldlm_pools_comp);
1267 cfs_waitq_init(&ldlm_pools_thread->t_ctl_waitq);
1270 * CLONE_VM and CLONE_FILES just avoid a needless copy, because we
1271 * just drop the VM and FILES in ptlrpc_daemonize() right away.
1273 rc = cfs_kernel_thread(ldlm_pools_thread_main, ldlm_pools_thread,
1274 CLONE_VM | CLONE_FILES);
1276 CERROR("Can't start pool thread, error %d\n",
1278 OBD_FREE(ldlm_pools_thread, sizeof(*ldlm_pools_thread));
1279 ldlm_pools_thread = NULL;
1282 l_wait_event(ldlm_pools_thread->t_ctl_waitq,
1283 (ldlm_pools_thread->t_flags & SVC_RUNNING), &lwi);
1287 static void ldlm_pools_thread_stop(void)
1291 if (ldlm_pools_thread == NULL) {
1296 ldlm_pools_thread->t_flags = SVC_STOPPING;
1297 cfs_waitq_signal(&ldlm_pools_thread->t_ctl_waitq);
1300 * Make sure that pools thread is finished before freeing @thread.
1301 * This fixes possible race and oops due to accessing freed memory
1304 wait_for_completion(&ldlm_pools_comp);
1305 OBD_FREE_PTR(ldlm_pools_thread);
1306 ldlm_pools_thread = NULL;
1310 int ldlm_pools_init(void)
1315 rc = ldlm_pools_thread_start();
1317 ldlm_pools_srv_shrinker = set_shrinker(DEFAULT_SEEKS,
1318 ldlm_pools_srv_shrink);
1319 ldlm_pools_cli_shrinker = set_shrinker(DEFAULT_SEEKS,
1320 ldlm_pools_cli_shrink);
1324 EXPORT_SYMBOL(ldlm_pools_init);
1326 void ldlm_pools_fini(void)
1328 if (ldlm_pools_srv_shrinker != NULL) {
1329 remove_shrinker(ldlm_pools_srv_shrinker);
1330 ldlm_pools_srv_shrinker = NULL;
1332 if (ldlm_pools_cli_shrinker != NULL) {
1333 remove_shrinker(ldlm_pools_cli_shrinker);
1334 ldlm_pools_cli_shrinker = NULL;
1336 ldlm_pools_thread_stop();
1338 EXPORT_SYMBOL(ldlm_pools_fini);
1339 #endif /* __KERNEL__ */
1341 #else /* !HAVE_LRU_RESIZE_SUPPORT */
1342 int ldlm_pool_setup(struct ldlm_pool *pl, int limit)
1346 EXPORT_SYMBOL(ldlm_pool_setup);
1348 int ldlm_pool_recalc(struct ldlm_pool *pl)
1352 EXPORT_SYMBOL(ldlm_pool_recalc);
1354 int ldlm_pool_shrink(struct ldlm_pool *pl,
1355 int nr, unsigned int gfp_mask)
1359 EXPORT_SYMBOL(ldlm_pool_shrink);
1361 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
1362 int idx, ldlm_side_t client)
1366 EXPORT_SYMBOL(ldlm_pool_init);
1368 void ldlm_pool_fini(struct ldlm_pool *pl)
1372 EXPORT_SYMBOL(ldlm_pool_fini);
1374 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
1378 EXPORT_SYMBOL(ldlm_pool_add);
1380 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
1384 EXPORT_SYMBOL(ldlm_pool_del);
1386 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
1390 EXPORT_SYMBOL(ldlm_pool_get_slv);
1392 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv)
1396 EXPORT_SYMBOL(ldlm_pool_set_slv);
1398 __u64 ldlm_pool_get_clv(struct ldlm_pool *pl)
1402 EXPORT_SYMBOL(ldlm_pool_get_clv);
1404 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv)
1408 EXPORT_SYMBOL(ldlm_pool_set_clv);
1410 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
1414 EXPORT_SYMBOL(ldlm_pool_get_limit);
1416 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
1420 EXPORT_SYMBOL(ldlm_pool_set_limit);
1422 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
1426 EXPORT_SYMBOL(ldlm_pool_get_lvf);
1428 int ldlm_pools_init(void)
1432 EXPORT_SYMBOL(ldlm_pools_init);
1434 void ldlm_pools_fini(void)
1438 EXPORT_SYMBOL(ldlm_pools_fini);
1440 void ldlm_pools_wakeup(void)
1444 EXPORT_SYMBOL(ldlm_pools_wakeup);
1446 void ldlm_pools_recalc(ldlm_side_t client)
1450 EXPORT_SYMBOL(ldlm_pools_recalc);
1451 #endif /* HAVE_LRU_RESIZE_SUPPORT */