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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ldlm/ldlm_pool.c
34 * Author: Yury Umanets <umka@clusterfs.com>
38 * Idea of this code is rather simple. Each second, for each server namespace
39 * we have SLV - server lock volume which is calculated on current number of
40 * granted locks, grant speed for past period, etc - that is, locking load.
41 * This SLV number may be thought as a flow definition for simplicity. It is
42 * sent to clients with each occasion to let them know what is current load
43 * situation on the server. By default, at the beginning, SLV on server is
44 * set max value which is calculated as the following: allow to one client
45 * have all locks of limit ->pl_limit for 10h.
47 * Next, on clients, number of cached locks is not limited artificially in any
48 * way as it was before. Instead, client calculates CLV, that is, client lock
49 * volume for each lock and compares it with last SLV from the server. CLV is
50 * calculated as the number of locks in LRU * lock live time in seconds. If
51 * CLV > SLV - lock is canceled.
53 * Client has LVF, that is, lock volume factor which regulates how much
54 * sensitive client should be about last SLV from server. The higher LVF is the
55 * more locks will be canceled on client. Default value for it is 1. Setting LVF
56 * to 2 means that client will cancel locks 2 times faster.
58 * Locks on a client will be canceled more intensively in these cases:
59 * (1) if SLV is smaller, that is, load is higher on the server;
60 * (2) client has a lot of locks (the more locks are held by client, the bigger
61 * chances that some of them should be canceled);
62 * (3) client has old locks (taken some time ago);
64 * Thus, according to flow paradigm that we use for better understanding SLV,
65 * CLV is the volume of particle in flow described by SLV. According to this,
66 * if flow is getting thinner, more and more particles become outside of it and
67 * as particles are locks, they should be canceled.
69 * General idea of this belongs to Vitaly Fertman (vitaly@clusterfs.com).
70 * Andreas Dilger (adilger@clusterfs.com) proposed few nice ideas like using
71 * LVF and many cleanups. Flow definition to allow more easy understanding of
72 * the logic belongs to Nikita Danilov (nikita@clusterfs.com) as well as many
73 * cleanups and fixes. And design and implementation are done by Yury Umanets
74 * (umka@clusterfs.com).
76 * Glossary for terms used:
78 * pl_limit - Number of allowed locks in pool. Applies to server and client
81 * pl_granted - Number of granted locks (calculated);
82 * pl_grant_rate - Number of granted locks for last T (calculated);
83 * pl_cancel_rate - Number of canceled locks for last T (calculated);
84 * pl_grant_speed - Grant speed (GR - CR) for last T (calculated);
85 * pl_grant_plan - Planned number of granted locks for next T (calculated);
86 * pl_server_lock_volume - Current server lock volume (calculated);
88 * As it may be seen from list above, we have few possible tunables which may
89 * affect behavior much. They all may be modified via sysfs. However, they also
90 * give a possibility for constructing few pre-defined behavior policies. If
91 * none of predefines is suitable for a working pattern being used, new one may
92 * be "constructed" via sysfs tunables.
95 #define DEBUG_SUBSYSTEM S_LDLM
97 #include <lustre_dlm.h>
98 #include <cl_object.h>
99 #include <obd_class.h>
100 #include <obd_support.h>
101 #include "ldlm_internal.h"
104 * 50 ldlm locks for 1MB of RAM.
106 #define LDLM_POOL_HOST_L ((NUM_CACHEPAGES >> (20 - PAGE_SHIFT)) * 50)
109 * Maximal possible grant step plan in %.
111 #define LDLM_POOL_MAX_GSP (30)
114 * Minimal possible grant step plan in %.
116 #define LDLM_POOL_MIN_GSP (1)
119 * This controls the speed of reaching LDLM_POOL_MAX_GSP
120 * with increasing thread period.
122 #define LDLM_POOL_GSP_STEP_SHIFT (2)
125 * LDLM_POOL_GSP% of all locks is default GP.
127 #define LDLM_POOL_GP(L) (((L) * LDLM_POOL_MAX_GSP) / 100)
130 * Max age for locks on clients.
132 #define LDLM_POOL_MAX_AGE (36000)
135 * The granularity of SLV calculation.
137 #define LDLM_POOL_SLV_SHIFT (10)
139 static inline __u64 dru(__u64 val, __u32 shift, int round_up)
141 return (val + (round_up ? (1 << shift) - 1 : 0)) >> shift;
144 static inline __u64 ldlm_pool_slv_max(__u32 L)
147 * Allow to have all locks for 1 client for 10 hrs.
148 * Formula is the following: limit * 10h / 1 client.
150 __u64 lim = (__u64)L * LDLM_POOL_MAX_AGE / 1;
154 static inline __u64 ldlm_pool_slv_min(__u32 L)
160 LDLM_POOL_FIRST_STAT = 0,
161 LDLM_POOL_GRANTED_STAT = LDLM_POOL_FIRST_STAT,
162 LDLM_POOL_GRANT_STAT,
163 LDLM_POOL_CANCEL_STAT,
164 LDLM_POOL_GRANT_RATE_STAT,
165 LDLM_POOL_CANCEL_RATE_STAT,
166 LDLM_POOL_GRANT_PLAN_STAT,
168 LDLM_POOL_SHRINK_REQTD_STAT,
169 LDLM_POOL_SHRINK_FREED_STAT,
170 LDLM_POOL_RECALC_STAT,
171 LDLM_POOL_TIMING_STAT,
176 * Calculates suggested grant_step in % of available locks for passed
177 * \a period. This is later used in grant_plan calculations.
179 static inline int ldlm_pool_t2gsp(unsigned int t)
182 * This yields 1% grant step for anything below LDLM_POOL_GSP_STEP
183 * and up to 30% for anything higher than LDLM_POOL_GSP_STEP.
185 * How this will affect execution is the following:
187 * - for thread period 1s we will have grant_step 1% which good from
188 * pov of taking some load off from server and push it out to clients.
189 * This is like that because 1% for grant_step means that server will
190 * not allow clients to get lots of locks in short period of time and
191 * keep all old locks in their caches. Clients will always have to
192 * get some locks back if they want to take some new;
194 * - for thread period 10s (which is default) we will have 23% which
195 * means that clients will have enough of room to take some new locks
196 * without getting some back. All locks from this 23% which were not
197 * taken by clients in current period will contribute in SLV growing.
198 * SLV growing means more locks cached on clients until limit or grant
201 return LDLM_POOL_MAX_GSP -
202 ((LDLM_POOL_MAX_GSP - LDLM_POOL_MIN_GSP) >>
203 (t >> LDLM_POOL_GSP_STEP_SHIFT));
207 * Recalculates next stats on passed \a pl.
209 * \pre ->pl_lock is locked.
211 static void ldlm_pool_recalc_stats(struct ldlm_pool *pl)
213 int grant_plan = pl->pl_grant_plan;
214 __u64 slv = pl->pl_server_lock_volume;
215 int granted = atomic_read(&pl->pl_granted);
216 int grant_rate = atomic_read(&pl->pl_grant_rate);
217 int cancel_rate = atomic_read(&pl->pl_cancel_rate);
219 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_SLV_STAT,
221 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
223 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
225 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
227 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
232 * Sets SLV and Limit from container_of(pl, struct ldlm_namespace,
233 * ns_pool)->ns_obd tp passed \a pl.
235 static void ldlm_cli_pool_pop_slv(struct ldlm_pool *pl)
237 struct obd_device *obd;
240 * Get new SLV and Limit from obd which is updated with coming
243 obd = container_of(pl, struct ldlm_namespace,
245 read_lock(&obd->obd_pool_lock);
246 pl->pl_server_lock_volume = obd->obd_pool_slv;
247 atomic_set(&pl->pl_limit, obd->obd_pool_limit);
248 read_unlock(&obd->obd_pool_lock);
252 * Recalculates client size pool \a pl according to current SLV and Limit.
254 static int ldlm_cli_pool_recalc(struct ldlm_pool *pl)
256 time64_t recalc_interval_sec;
259 recalc_interval_sec = ktime_get_real_seconds() - pl->pl_recalc_time;
260 if (recalc_interval_sec < pl->pl_recalc_period)
263 spin_lock(&pl->pl_lock);
265 * Check if we need to recalc lists now.
267 recalc_interval_sec = ktime_get_real_seconds() - pl->pl_recalc_time;
268 if (recalc_interval_sec < pl->pl_recalc_period) {
269 spin_unlock(&pl->pl_lock);
274 * Make sure that pool knows last SLV and Limit from obd.
276 ldlm_cli_pool_pop_slv(pl);
278 spin_unlock(&pl->pl_lock);
281 * Do not cancel locks in case lru resize is disabled for this ns.
283 if (!ns_connect_lru_resize(container_of(pl, struct ldlm_namespace,
290 * In the time of canceling locks on client we do not need to maintain
291 * sharp timing, we only want to cancel locks asap according to new SLV.
292 * It may be called when SLV has changed much, this is why we do not
293 * take into account pl->pl_recalc_time here.
295 ret = ldlm_cancel_lru(container_of(pl, struct ldlm_namespace, ns_pool),
296 0, LCF_ASYNC, LDLM_LRU_FLAG_LRUR);
299 spin_lock(&pl->pl_lock);
301 * Time of LRU resizing might be longer than period,
302 * so update after LRU resizing rather than before it.
304 pl->pl_recalc_time = ktime_get_real_seconds();
305 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
306 recalc_interval_sec);
307 spin_unlock(&pl->pl_lock);
312 * This function is main entry point for memory pressure handling on client
313 * side. Main goal of this function is to cancel some number of locks on
314 * passed \a pl according to \a nr and \a gfp_mask.
316 static int ldlm_cli_pool_shrink(struct ldlm_pool *pl,
317 int nr, gfp_t gfp_mask)
319 struct ldlm_namespace *ns;
322 ns = container_of(pl, struct ldlm_namespace, ns_pool);
325 * Do not cancel locks in case lru resize is disabled for this ns.
327 if (!ns_connect_lru_resize(ns))
331 * Make sure that pool knows last SLV and Limit from obd.
333 ldlm_cli_pool_pop_slv(pl);
335 spin_lock(&ns->ns_lock);
336 unused = ns->ns_nr_unused;
337 spin_unlock(&ns->ns_lock);
340 return (unused / 100) * sysctl_vfs_cache_pressure;
342 return ldlm_cancel_lru(ns, nr, LCF_ASYNC, LDLM_LRU_FLAG_SHRINK);
345 static const struct ldlm_pool_ops ldlm_cli_pool_ops = {
346 .po_recalc = ldlm_cli_pool_recalc,
347 .po_shrink = ldlm_cli_pool_shrink
351 * Pool recalc wrapper. Will call either client or server pool recalc callback
352 * depending what pool \a pl is used.
354 static int ldlm_pool_recalc(struct ldlm_pool *pl)
356 u32 recalc_interval_sec;
359 recalc_interval_sec = ktime_get_real_seconds() - pl->pl_recalc_time;
360 if (recalc_interval_sec > 0) {
361 spin_lock(&pl->pl_lock);
362 recalc_interval_sec = ktime_get_real_seconds() - pl->pl_recalc_time;
364 if (recalc_interval_sec > 0) {
366 * Update pool statistics every 1s.
368 ldlm_pool_recalc_stats(pl);
371 * Zero out all rates and speed for the last period.
373 atomic_set(&pl->pl_grant_rate, 0);
374 atomic_set(&pl->pl_cancel_rate, 0);
376 spin_unlock(&pl->pl_lock);
379 if (pl->pl_ops->po_recalc) {
380 count = pl->pl_ops->po_recalc(pl);
381 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_RECALC_STAT,
385 recalc_interval_sec = pl->pl_recalc_time - ktime_get_real_seconds() +
386 pl->pl_recalc_period;
387 if (recalc_interval_sec <= 0) {
388 /* DEBUG: should be re-removed after LU-4536 is fixed */
390 "%s: Negative interval(%ld), too short period(%ld)\n",
391 pl->pl_name, (long)recalc_interval_sec,
392 (long)pl->pl_recalc_period);
394 /* Prevent too frequent recalculation. */
395 recalc_interval_sec = 1;
398 return recalc_interval_sec;
402 * Pool shrink wrapper. Will call either client or server pool recalc callback
403 * depending what pool pl is used. When nr == 0, just return the number of
404 * freeable locks. Otherwise, return the number of canceled locks.
406 static int ldlm_pool_shrink(struct ldlm_pool *pl, int nr, gfp_t gfp_mask)
410 if (pl->pl_ops->po_shrink) {
411 cancel = pl->pl_ops->po_shrink(pl, nr, gfp_mask);
413 lprocfs_counter_add(pl->pl_stats,
414 LDLM_POOL_SHRINK_REQTD_STAT,
416 lprocfs_counter_add(pl->pl_stats,
417 LDLM_POOL_SHRINK_FREED_STAT,
420 "%s: request to shrink %d locks, shrunk %d\n",
421 pl->pl_name, nr, cancel);
427 static int lprocfs_pool_state_seq_show(struct seq_file *m, void *unused)
429 int granted, grant_rate, cancel_rate;
430 int grant_speed, lvf;
431 struct ldlm_pool *pl = m->private;
435 spin_lock(&pl->pl_lock);
436 slv = pl->pl_server_lock_volume;
437 clv = pl->pl_client_lock_volume;
438 limit = atomic_read(&pl->pl_limit);
439 granted = atomic_read(&pl->pl_granted);
440 grant_rate = atomic_read(&pl->pl_grant_rate);
441 cancel_rate = atomic_read(&pl->pl_cancel_rate);
442 grant_speed = grant_rate - cancel_rate;
443 lvf = atomic_read(&pl->pl_lock_volume_factor);
444 spin_unlock(&pl->pl_lock);
446 seq_printf(m, "LDLM pool state (%s):\n"
450 pl->pl_name, slv, clv, lvf);
452 seq_printf(m, " GR: %d\n CR: %d\n GS: %d\n"
454 grant_rate, cancel_rate, grant_speed,
460 LPROC_SEQ_FOPS_RO(lprocfs_pool_state);
462 static ssize_t grant_speed_show(struct kobject *kobj, struct attribute *attr,
465 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,
470 spin_lock(&pl->pl_lock);
471 /* serialize with ldlm_pool_recalc */
472 grant_speed = atomic_read(&pl->pl_grant_rate) -
473 atomic_read(&pl->pl_cancel_rate);
474 spin_unlock(&pl->pl_lock);
475 return sprintf(buf, "%d\n", grant_speed);
477 LUSTRE_RO_ATTR(grant_speed);
479 LDLM_POOL_SYSFS_READER_SHOW(grant_plan, int);
480 LUSTRE_RO_ATTR(grant_plan);
482 LDLM_POOL_SYSFS_READER_SHOW(recalc_period, int);
483 LDLM_POOL_SYSFS_WRITER_STORE(recalc_period, int);
484 LUSTRE_RW_ATTR(recalc_period);
486 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(server_lock_volume, u64);
487 LUSTRE_RO_ATTR(server_lock_volume);
489 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(limit, atomic);
490 LDLM_POOL_SYSFS_WRITER_NOLOCK_STORE(limit, atomic);
491 LUSTRE_RW_ATTR(limit);
493 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(granted, atomic);
494 LUSTRE_RO_ATTR(granted);
496 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(cancel_rate, atomic);
497 LUSTRE_RO_ATTR(cancel_rate);
499 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(grant_rate, atomic);
500 LUSTRE_RO_ATTR(grant_rate);
502 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(lock_volume_factor, atomic);
503 LDLM_POOL_SYSFS_WRITER_NOLOCK_STORE(lock_volume_factor, atomic);
504 LUSTRE_RW_ATTR(lock_volume_factor);
506 #define LDLM_POOL_ADD_VAR(name, var, ops) \
508 snprintf(var_name, MAX_STRING_SIZE, #name); \
509 pool_vars[0].data = var; \
510 pool_vars[0].fops = ops; \
511 ldebugfs_add_vars(pl->pl_debugfs_entry, pool_vars, NULL);\
514 /* These are for pools in /sys/fs/lustre/ldlm/namespaces/.../pool */
515 static struct attribute *ldlm_pl_attrs[] = {
516 &lustre_attr_grant_speed.attr,
517 &lustre_attr_grant_plan.attr,
518 &lustre_attr_recalc_period.attr,
519 &lustre_attr_server_lock_volume.attr,
520 &lustre_attr_limit.attr,
521 &lustre_attr_granted.attr,
522 &lustre_attr_cancel_rate.attr,
523 &lustre_attr_grant_rate.attr,
524 &lustre_attr_lock_volume_factor.attr,
528 static void ldlm_pl_release(struct kobject *kobj)
530 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,
532 complete(&pl->pl_kobj_unregister);
535 static struct kobj_type ldlm_pl_ktype = {
536 .default_attrs = ldlm_pl_attrs,
537 .sysfs_ops = &lustre_sysfs_ops,
538 .release = ldlm_pl_release,
541 static int ldlm_pool_sysfs_init(struct ldlm_pool *pl)
543 struct ldlm_namespace *ns = container_of(pl, struct ldlm_namespace,
547 init_completion(&pl->pl_kobj_unregister);
548 err = kobject_init_and_add(&pl->pl_kobj, &ldlm_pl_ktype, &ns->ns_kobj,
554 static int ldlm_pool_debugfs_init(struct ldlm_pool *pl)
556 struct ldlm_namespace *ns = container_of(pl, struct ldlm_namespace,
558 struct dentry *debugfs_ns_parent;
559 struct lprocfs_vars pool_vars[2];
560 char *var_name = NULL;
563 var_name = kzalloc(MAX_STRING_SIZE + 1, GFP_NOFS);
567 debugfs_ns_parent = ns->ns_debugfs_entry;
568 if (IS_ERR_OR_NULL(debugfs_ns_parent)) {
569 CERROR("%s: debugfs entry is not initialized\n",
574 pl->pl_debugfs_entry = ldebugfs_register("pool", debugfs_ns_parent,
576 if (IS_ERR(pl->pl_debugfs_entry)) {
577 CERROR("LdebugFS failed in ldlm-pool-init\n");
578 rc = PTR_ERR(pl->pl_debugfs_entry);
579 pl->pl_debugfs_entry = NULL;
583 var_name[MAX_STRING_SIZE] = '\0';
584 memset(pool_vars, 0, sizeof(pool_vars));
585 pool_vars[0].name = var_name;
587 LDLM_POOL_ADD_VAR(state, pl, &lprocfs_pool_state_fops);
589 pl->pl_stats = lprocfs_alloc_stats(LDLM_POOL_LAST_STAT -
590 LDLM_POOL_FIRST_STAT, 0);
596 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
597 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
599 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_STAT,
600 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
602 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_STAT,
603 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
605 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
606 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
607 "grant_rate", "locks/s");
608 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
609 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
610 "cancel_rate", "locks/s");
611 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
612 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
613 "grant_plan", "locks/s");
614 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SLV_STAT,
615 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
617 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_REQTD_STAT,
618 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
619 "shrink_request", "locks");
620 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_FREED_STAT,
621 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
622 "shrink_freed", "locks");
623 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_RECALC_STAT,
624 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
625 "recalc_freed", "locks");
626 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_TIMING_STAT,
627 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
628 "recalc_timing", "sec");
629 rc = ldebugfs_register_stats(pl->pl_debugfs_entry, "stats",
637 static void ldlm_pool_sysfs_fini(struct ldlm_pool *pl)
639 kobject_put(&pl->pl_kobj);
640 wait_for_completion(&pl->pl_kobj_unregister);
643 static void ldlm_pool_debugfs_fini(struct ldlm_pool *pl)
646 lprocfs_free_stats(&pl->pl_stats);
649 if (pl->pl_debugfs_entry) {
650 ldebugfs_remove(&pl->pl_debugfs_entry);
651 pl->pl_debugfs_entry = NULL;
655 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
656 int idx, enum ldlm_side client)
660 spin_lock_init(&pl->pl_lock);
661 atomic_set(&pl->pl_granted, 0);
662 pl->pl_recalc_time = ktime_get_real_seconds();
663 atomic_set(&pl->pl_lock_volume_factor, 1);
665 atomic_set(&pl->pl_grant_rate, 0);
666 atomic_set(&pl->pl_cancel_rate, 0);
667 pl->pl_grant_plan = LDLM_POOL_GP(LDLM_POOL_HOST_L);
669 snprintf(pl->pl_name, sizeof(pl->pl_name), "ldlm-pool-%s-%d",
670 ldlm_ns_name(ns), idx);
672 atomic_set(&pl->pl_limit, 1);
673 pl->pl_server_lock_volume = 0;
674 pl->pl_ops = &ldlm_cli_pool_ops;
675 pl->pl_recalc_period = LDLM_POOL_CLI_DEF_RECALC_PERIOD;
676 pl->pl_client_lock_volume = 0;
677 rc = ldlm_pool_debugfs_init(pl);
681 rc = ldlm_pool_sysfs_init(pl);
685 CDEBUG(D_DLMTRACE, "Lock pool %s is initialized\n", pl->pl_name);
690 void ldlm_pool_fini(struct ldlm_pool *pl)
692 ldlm_pool_sysfs_fini(pl);
693 ldlm_pool_debugfs_fini(pl);
696 * Pool should not be used after this point. We can't free it here as
697 * it lives in struct ldlm_namespace, but still interested in catching
698 * any abnormal using cases.
700 POISON(pl, 0x5a, sizeof(*pl));
704 * Add new taken ldlm lock \a lock into pool \a pl accounting.
706 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
709 * FLOCK locks are special in a sense that they are almost never
710 * cancelled, instead special kind of lock is used to drop them.
711 * also there is no LRU for flock locks, so no point in tracking
714 if (lock->l_resource->lr_type == LDLM_FLOCK)
717 atomic_inc(&pl->pl_granted);
718 atomic_inc(&pl->pl_grant_rate);
719 lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_GRANT_STAT);
721 * Do not do pool recalc for client side as all locks which
722 * potentially may be canceled has already been packed into
723 * enqueue/cancel rpc. Also we do not want to run out of stack
724 * with too long call paths.
729 * Remove ldlm lock \a lock from pool \a pl accounting.
731 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
734 * Filter out FLOCK locks. Read above comment in ldlm_pool_add().
736 if (lock->l_resource->lr_type == LDLM_FLOCK)
739 LASSERT(atomic_read(&pl->pl_granted) > 0);
740 atomic_dec(&pl->pl_granted);
741 atomic_inc(&pl->pl_cancel_rate);
743 lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_CANCEL_STAT);
747 * Returns current \a pl SLV.
749 * \pre ->pl_lock is not locked.
751 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
755 spin_lock(&pl->pl_lock);
756 slv = pl->pl_server_lock_volume;
757 spin_unlock(&pl->pl_lock);
762 * Sets passed \a clv to \a pl.
764 * \pre ->pl_lock is not locked.
766 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv)
768 spin_lock(&pl->pl_lock);
769 pl->pl_client_lock_volume = clv;
770 spin_unlock(&pl->pl_lock);
774 * Returns current LVF from \a pl.
776 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
778 return atomic_read(&pl->pl_lock_volume_factor);
781 static int ldlm_pool_granted(struct ldlm_pool *pl)
783 return atomic_read(&pl->pl_granted);
786 static struct ptlrpc_thread *ldlm_pools_thread;
787 static struct completion ldlm_pools_comp;
790 * count locks from all namespaces (if possible). Returns number of
793 static unsigned long ldlm_pools_count(enum ldlm_side client, gfp_t gfp_mask)
795 unsigned long total = 0;
797 struct ldlm_namespace *ns;
798 struct ldlm_namespace *ns_old = NULL; /* loop detection */
800 if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
803 CDEBUG(D_DLMTRACE, "Request to count %s locks from all pools\n",
804 client == LDLM_NAMESPACE_CLIENT ? "client" : "server");
807 * Find out how many resources we may release.
809 for (nr_ns = ldlm_namespace_nr_read(client);
810 nr_ns > 0; nr_ns--) {
811 mutex_lock(ldlm_namespace_lock(client));
812 if (list_empty(ldlm_namespace_list(client))) {
813 mutex_unlock(ldlm_namespace_lock(client));
816 ns = ldlm_namespace_first_locked(client);
819 mutex_unlock(ldlm_namespace_lock(client));
823 if (ldlm_ns_empty(ns)) {
824 ldlm_namespace_move_to_inactive_locked(ns, client);
825 mutex_unlock(ldlm_namespace_lock(client));
832 ldlm_namespace_get(ns);
833 ldlm_namespace_move_to_active_locked(ns, client);
834 mutex_unlock(ldlm_namespace_lock(client));
835 total += ldlm_pool_shrink(&ns->ns_pool, 0, gfp_mask);
836 ldlm_namespace_put(ns);
842 static unsigned long ldlm_pools_scan(enum ldlm_side client, int nr,
845 unsigned long freed = 0;
847 struct ldlm_namespace *ns;
849 if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
853 * Shrink at least ldlm_namespace_nr_read(client) namespaces.
855 for (tmp = nr_ns = ldlm_namespace_nr_read(client);
857 int cancel, nr_locks;
860 * Do not call shrink under ldlm_namespace_lock(client)
862 mutex_lock(ldlm_namespace_lock(client));
863 if (list_empty(ldlm_namespace_list(client))) {
864 mutex_unlock(ldlm_namespace_lock(client));
867 ns = ldlm_namespace_first_locked(client);
868 ldlm_namespace_get(ns);
869 ldlm_namespace_move_to_active_locked(ns, client);
870 mutex_unlock(ldlm_namespace_lock(client));
872 nr_locks = ldlm_pool_granted(&ns->ns_pool);
874 * We use to shrink propotionally but with new shrinker API,
875 * we lost the total number of freeable locks.
877 cancel = 1 + min_t(int, nr_locks, nr / nr_ns);
878 freed += ldlm_pool_shrink(&ns->ns_pool, cancel, gfp_mask);
879 ldlm_namespace_put(ns);
882 * we only decrease the SLV in server pools shrinker, return
883 * SHRINK_STOP to kernel to avoid needless loop. LU-1128
888 static unsigned long ldlm_pools_cli_count(struct shrinker *s,
889 struct shrink_control *sc)
891 return ldlm_pools_count(LDLM_NAMESPACE_CLIENT, sc->gfp_mask);
894 static unsigned long ldlm_pools_cli_scan(struct shrinker *s,
895 struct shrink_control *sc)
897 return ldlm_pools_scan(LDLM_NAMESPACE_CLIENT, sc->nr_to_scan,
901 static int ldlm_pools_recalc(enum ldlm_side client)
903 struct ldlm_namespace *ns;
904 struct ldlm_namespace *ns_old = NULL;
905 /* seconds of sleep if no active namespaces */
906 int time = LDLM_POOL_CLI_DEF_RECALC_PERIOD;
910 * Recalc at least ldlm_namespace_nr_read(client) namespaces.
912 for (nr = ldlm_namespace_nr_read(client); nr > 0; nr--) {
915 * Lock the list, get first @ns in the list, getref, move it
916 * to the tail, unlock and call pool recalc. This way we avoid
917 * calling recalc under @ns lock what is really good as we get
918 * rid of potential deadlock on client nodes when canceling
919 * locks synchronously.
921 mutex_lock(ldlm_namespace_lock(client));
922 if (list_empty(ldlm_namespace_list(client))) {
923 mutex_unlock(ldlm_namespace_lock(client));
926 ns = ldlm_namespace_first_locked(client);
928 if (ns_old == ns) { /* Full pass complete */
929 mutex_unlock(ldlm_namespace_lock(client));
933 /* We got an empty namespace, need to move it back to inactive
935 * The race with parallel resource creation is fine:
936 * - If they do namespace_get before our check, we fail the
937 * check and they move this item to the end of the list anyway
938 * - If we do the check and then they do namespace_get, then
939 * we move the namespace to inactive and they will move
940 * it back to active (synchronised by the lock, so no clash
943 if (ldlm_ns_empty(ns)) {
944 ldlm_namespace_move_to_inactive_locked(ns, client);
945 mutex_unlock(ldlm_namespace_lock(client));
952 spin_lock(&ns->ns_lock);
954 * skip ns which is being freed, and we don't want to increase
955 * its refcount again, not even temporarily. bz21519 & LU-499.
957 if (ns->ns_stopping) {
961 ldlm_namespace_get(ns);
963 spin_unlock(&ns->ns_lock);
965 ldlm_namespace_move_to_active_locked(ns, client);
966 mutex_unlock(ldlm_namespace_lock(client));
969 * After setup is done - recalc the pool.
972 int ttime = ldlm_pool_recalc(&ns->ns_pool);
977 ldlm_namespace_put(ns);
981 /* Wake up the blocking threads from time to time. */
982 ldlm_bl_thread_wakeup();
987 static int ldlm_pools_thread_main(void *arg)
989 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
992 thread_set_flags(thread, SVC_RUNNING);
993 wake_up(&thread->t_ctl_waitq);
995 CDEBUG(D_DLMTRACE, "%s: pool thread starting, process %d\n",
996 "ldlm_poold", current_pid());
999 struct l_wait_info lwi;
1002 * Recal all pools on this tick.
1004 c_time = ldlm_pools_recalc(LDLM_NAMESPACE_CLIENT);
1007 * Wait until the next check time, or until we're
1010 lwi = LWI_TIMEOUT(cfs_time_seconds(c_time),
1012 l_wait_event(thread->t_ctl_waitq,
1013 thread_is_stopping(thread) ||
1014 thread_is_event(thread),
1017 if (thread_test_and_clear_flags(thread, SVC_STOPPING))
1019 thread_test_and_clear_flags(thread, SVC_EVENT);
1022 thread_set_flags(thread, SVC_STOPPED);
1023 wake_up(&thread->t_ctl_waitq);
1025 CDEBUG(D_DLMTRACE, "%s: pool thread exiting, process %d\n",
1026 "ldlm_poold", current_pid());
1028 complete_and_exit(&ldlm_pools_comp, 0);
1031 static int ldlm_pools_thread_start(void)
1033 struct l_wait_info lwi = { 0 };
1034 struct task_struct *task;
1036 if (ldlm_pools_thread)
1039 ldlm_pools_thread = kzalloc(sizeof(*ldlm_pools_thread), GFP_NOFS);
1040 if (!ldlm_pools_thread)
1043 init_completion(&ldlm_pools_comp);
1044 init_waitqueue_head(&ldlm_pools_thread->t_ctl_waitq);
1046 task = kthread_run(ldlm_pools_thread_main, ldlm_pools_thread,
1049 CERROR("Can't start pool thread, error %ld\n", PTR_ERR(task));
1050 kfree(ldlm_pools_thread);
1051 ldlm_pools_thread = NULL;
1052 return PTR_ERR(task);
1054 l_wait_event(ldlm_pools_thread->t_ctl_waitq,
1055 thread_is_running(ldlm_pools_thread), &lwi);
1059 static void ldlm_pools_thread_stop(void)
1061 if (!ldlm_pools_thread)
1064 thread_set_flags(ldlm_pools_thread, SVC_STOPPING);
1065 wake_up(&ldlm_pools_thread->t_ctl_waitq);
1068 * Make sure that pools thread is finished before freeing @thread.
1069 * This fixes possible race and oops due to accessing freed memory
1072 wait_for_completion(&ldlm_pools_comp);
1073 kfree(ldlm_pools_thread);
1074 ldlm_pools_thread = NULL;
1077 static struct shrinker ldlm_pools_cli_shrinker = {
1078 .count_objects = ldlm_pools_cli_count,
1079 .scan_objects = ldlm_pools_cli_scan,
1080 .seeks = DEFAULT_SEEKS,
1083 int ldlm_pools_init(void)
1087 rc = ldlm_pools_thread_start();
1089 register_shrinker(&ldlm_pools_cli_shrinker);
1094 void ldlm_pools_fini(void)
1096 if (ldlm_pools_thread)
1097 unregister_shrinker(&ldlm_pools_cli_shrinker);
1099 ldlm_pools_thread_stop();