1 // SPDX-License-Identifier: GPL-2.0
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 only,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License version 2 for more details (a copy is included
15 * in the LICENSE file that accompanied this code).
17 * You should have received a copy of the GNU General Public License
18 * version 2 along with this program; If not, see
19 * http://www.gnu.org/licenses/gpl-2.0.html
24 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Use is subject to license terms.
27 * Copyright (c) 2010, 2017, Intel Corporation.
30 * This file is part of Lustre, http://www.lustre.org/
31 * Lustre is a trademark of Sun Microsystems, Inc.
33 * lustre/ldlm/ldlm_pool.c
35 * Author: Yury Umanets <umka@clusterfs.com>
39 * Idea of this code is rather simple. Each second, for each server namespace
40 * we have SLV - server lock volume which is calculated on current number of
41 * granted locks, grant speed for past period, etc - that is, locking load.
42 * This SLV number may be thought as a flow definition for simplicity. It is
43 * sent to clients with each occasion to let them know what is current load
44 * situation on the server. By default, at the beginning, SLV on server is
45 * set max value which is calculated as the following: allow to one client
46 * have all locks of limit ->pl_limit for 10h.
48 * Next, on clients, number of cached locks is not limited artificially in any
49 * way as it was before. Instead, client calculates CLV, that is, client lock
50 * volume for each lock and compares it with last SLV from the server. CLV is
51 * calculated as the number of locks in LRU * lock live time in seconds. If
52 * CLV > SLV - lock is canceled.
54 * Client has LVF, that is, lock volume factor which regulates how much
55 * sensitive client should be about last SLV from server. The higher LVF is the
56 * more locks will be canceled on client. Default value for it is 1. Setting LVF
57 * to 2 means that client will cancel locks 2 times faster.
59 * Locks on a client will be canceled more intensively in these cases:
60 * (1) if SLV is smaller, that is, load is higher on the server;
61 * (2) client has a lot of locks (the more locks are held by client, the bigger
62 * chances that some of them should be canceled);
63 * (3) client has old locks (taken some time ago);
65 * Thus, according to flow paradigm that we use for better understanding SLV,
66 * CLV is the volume of particle in flow described by SLV. According to this,
67 * if flow is getting thinner, more and more particles become outside of it and
68 * as particles are locks, they should be canceled.
70 * General idea of this belongs to Vitaly Fertman (vitaly@clusterfs.com).
71 * Andreas Dilger (adilger@clusterfs.com) proposed few nice ideas like using
72 * LVF and many cleanups. Flow definition to allow more easy understanding of
73 * the logic belongs to Nikita Danilov (nikita@clusterfs.com) as well as many
74 * cleanups and fixes. And design and implementation are done by Yury Umanets
75 * (umka@clusterfs.com).
77 * Glossary for terms used:
79 * pl_limit - Number of allowed locks in pool. Applies to server and client
82 * pl_granted - Number of granted locks (calculated);
83 * pl_grant_rate - Number of granted locks for last T (calculated);
84 * pl_cancel_rate - Number of canceled locks for last T (calculated);
85 * pl_grant_speed - Grant speed (GR - CR) for last T (calculated);
86 * pl_grant_plan - Planned number of granted locks for next T (calculated);
87 * pl_server_lock_volume - Current server lock volume (calculated);
89 * As it may be seen from list above, we have few possible tunables which may
90 * affect behavior much. They all may be modified via sysfs. However, they also
91 * give a possibility for constructing few pre-defined behavior policies. If
92 * none of predefines is suitable for a working pattern being used, new one may
93 * be "constructed" via sysfs tunables.
96 #define DEBUG_SUBSYSTEM S_LDLM
98 #include <lustre_dlm.h>
99 #include <cl_object.h>
100 #include <obd_class.h>
101 #include <obd_support.h>
102 #include "ldlm_internal.h"
105 * 50 ldlm locks for 1MB of RAM.
107 #define LDLM_POOL_HOST_L ((NUM_CACHEPAGES >> (20 - PAGE_SHIFT)) * 50)
110 * Maximal possible grant step plan in %.
112 #define LDLM_POOL_MAX_GSP (30)
115 * Minimal possible grant step plan in %.
117 #define LDLM_POOL_MIN_GSP (1)
120 * This controls the speed of reaching LDLM_POOL_MAX_GSP
121 * with increasing thread period.
123 #define LDLM_POOL_GSP_STEP_SHIFT (2)
126 * LDLM_POOL_GSP% of all locks is default GP.
128 #define LDLM_POOL_GP(L) (((L) * LDLM_POOL_MAX_GSP) / 100)
131 * Max age for locks on clients.
133 #define LDLM_POOL_MAX_AGE (36000)
136 * The granularity of SLV calculation.
138 #define LDLM_POOL_SLV_SHIFT (10)
140 static inline u64 dru(u64 val, u32 shift, int round_up)
142 return (val + (round_up ? (1 << shift) - 1 : 0)) >> shift;
145 static inline u64 ldlm_pool_slv_max(u32 L)
148 * Allow to have all locks for 1 client for 10 hrs.
149 * Formula is the following: limit * 10h / 1 client.
151 u64 lim = (u64)L * LDLM_POOL_MAX_AGE / 1;
155 static inline u64 ldlm_pool_slv_min(u32 L)
161 LDLM_POOL_FIRST_STAT = 0,
162 LDLM_POOL_GRANTED_STAT = LDLM_POOL_FIRST_STAT,
163 LDLM_POOL_GRANT_STAT,
164 LDLM_POOL_CANCEL_STAT,
165 LDLM_POOL_GRANT_RATE_STAT,
166 LDLM_POOL_CANCEL_RATE_STAT,
167 LDLM_POOL_GRANT_PLAN_STAT,
169 LDLM_POOL_SHRINK_REQTD_STAT,
170 LDLM_POOL_SHRINK_FREED_STAT,
171 LDLM_POOL_RECALC_STAT,
172 LDLM_POOL_TIMING_STAT,
177 * Calculates suggested grant_step in % of available locks for passed
178 * @period. This is later used in grant_plan calculations.
180 static inline int ldlm_pool_t2gsp(unsigned int t)
183 * This yields 1% grant step for anything below LDLM_POOL_GSP_STEP
184 * and up to 30% for anything higher than LDLM_POOL_GSP_STEP.
186 * How this will affect execution is the following:
188 * - for thread period 1s we will have grant_step 1% which good from
189 * pov of taking some load off from server and push it out to clients.
190 * This is like that because 1% for grant_step means that server will
191 * not allow clients to get lots of locks in short period of time and
192 * keep all old locks in their caches. Clients will always have to
193 * get some locks back if they want to take some new;
195 * - for thread period 10s (which is default) we will have 23% which
196 * means that clients will have enough of room to take some new locks
197 * without getting some back. All locks from this 23% which were not
198 * taken by clients in current period will contribute in SLV growing.
199 * SLV growing means more locks cached on clients until limit or grant
202 return LDLM_POOL_MAX_GSP -
203 ((LDLM_POOL_MAX_GSP - LDLM_POOL_MIN_GSP) >>
204 (t >> LDLM_POOL_GSP_STEP_SHIFT));
208 * Recalculates next stats on passed @pl.
210 * \pre ->pl_lock is locked.
212 static void ldlm_pool_recalc_stats(struct ldlm_pool *pl, timeout_t period)
214 int grant_plan = pl->pl_grant_plan;
215 u64 slv = pl->pl_server_lock_volume;
216 int granted = atomic_read(&pl->pl_granted);
217 int grant_rate = atomic_read(&pl->pl_grant_rate) / period;
218 int cancel_rate = atomic_read(&pl->pl_cancel_rate) / period;
220 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_SLV_STAT,
222 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
224 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
226 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
228 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
233 * Sets SLV and Limit from container_of(pl, struct ldlm_namespace,
234 * ns_pool)->ns_obd tp passed @pl.
236 static void ldlm_cli_pool_pop_slv(struct ldlm_pool *pl)
238 struct obd_device *obd;
241 * Get new SLV and Limit from obd which is updated with coming
244 obd = container_of(pl, struct ldlm_namespace,
246 read_lock(&obd->obd_pool_lock);
247 pl->pl_server_lock_volume = obd->obd_pool_slv;
248 atomic_set(&pl->pl_limit, obd->obd_pool_limit);
249 read_unlock(&obd->obd_pool_lock);
253 * Recalculates client size pool @pl according to current SLV and Limit.
255 static int ldlm_cli_pool_recalc(struct ldlm_pool *pl)
257 timeout_t recalc_interval_sec;
260 recalc_interval_sec = ktime_get_seconds() - pl->pl_recalc_time;
261 if (recalc_interval_sec < pl->pl_recalc_period)
264 spin_lock(&pl->pl_lock);
266 * Check if we need to recalc lists now.
268 recalc_interval_sec = ktime_get_seconds() - pl->pl_recalc_time;
269 if (recalc_interval_sec < pl->pl_recalc_period) {
270 spin_unlock(&pl->pl_lock);
276 ldlm_pool_recalc_grant_plan(pl);
278 pl->pl_recalc_time = ktime_get_real_seconds();
279 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
280 recalc_interval_sec);
281 spin_unlock(&pl->pl_lock);
284 ldlm_pool_recalc_grant_plan(pl);
286 pl->pl_recalc_time = ktime_get_seconds();
287 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
288 recalc_interval_sec);
289 spin_unlock(&pl->pl_lock);
294 static int ldlm_cli_pool_recalc(struct ldlm_pool *pl)
296 time64_t recalc_interval_sec;
300 static int ldlm_cli_pool_recalc(struct ldlm_pool *pl)
302 timeout_t recalc_interval_sec;
308 * Make sure that pool knows last SLV and Limit from obd.
310 ldlm_cli_pool_pop_slv(pl);
312 spin_unlock(&pl->pl_lock);
316 recalc_interval_sec = ktime_get_real_seconds() - pl->pl_recalc_time;
317 if (recalc_interval_sec < pl->pl_recalc_period)
323 recalc_interval_sec = ktime_get_seconds() - pl->pl_recalc_time;
324 if (recalc_interval_sec < pl->pl_recalc_period)
331 * Check if we need to recalc lists now.
333 recalc_interval_sec = ktime_get_real_seconds() - pl->pl_recalc_time;
334 if (recalc_interval_sec < pl->pl_recalc_period) {
335 spin_unlock(&pl->pl_lock);
338 * Check if we need to recalc lists now.
340 recalc_interval_sec = ktime_get_seconds() - pl->pl_recalc_time;
341 if (recalc_interval_sec < pl->pl_recalc_period) {
342 spin_unlock(&pl->pl_lock);
346 * In the time of canceling locks on client we do not need to maintain
347 * sharp timing, we only want to cancel locks asap according to new SLV.
348 * It may be called when SLV has changed much, this is why we do not
349 * take into account pl->pl_recalc_time here.
351 ret = ldlm_cancel_lru(container_of(pl, struct ldlm_namespace, ns_pool),
354 spin_lock(&pl->pl_lock);
356 * Time of LRU resizing might be longer than period,
357 * so update after LRU resizing rather than before it.
359 pl->pl_recalc_time = ktime_get_seconds();
360 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
361 recalc_interval_sec);
362 spin_unlock(&pl->pl_lock);
367 * This function is main entry point for memory pressure handling on client
368 * side. Main goal of this function is to cancel some number of locks on
369 * passed @pl according to @nr and @gfp_mask.
371 static int ldlm_cli_pool_shrink(struct ldlm_pool *pl,
372 int nr, gfp_t gfp_mask)
374 struct ldlm_namespace *ns;
377 ns = container_of(pl, struct ldlm_namespace, ns_pool);
380 * Do not cancel locks in case lru resize is disabled for this ns.
382 if (!ns_connect_lru_resize(ns))
386 * Make sure that pool knows last SLV and Limit from obd.
388 spin_lock(&pl->pl_lock);
389 ldlm_cli_pool_pop_slv(pl);
390 spin_unlock(&pl->pl_lock);
392 spin_lock(&ns->ns_lock);
393 unused = ns->ns_nr_unused;
394 spin_unlock(&ns->ns_lock);
397 return (unused / 100) * sysctl_vfs_cache_pressure;
399 return ldlm_cancel_lru(ns, nr, LCF_ASYNC, 0);
402 static const struct ldlm_pool_ops ldlm_cli_pool_ops = {
403 .po_recalc = ldlm_cli_pool_recalc,
404 .po_shrink = ldlm_cli_pool_shrink
408 * Pool recalc wrapper. Will call either client or server pool recalc callback
409 * depending what pool @pl is used.
411 * \retval time in seconds for the next recalc of this pool
414 static int ldlm_pool_recalc(struct ldlm_pool *pl)
416 u32 recalc_interval_sec;
418 time64_t ldlm_pool_recalc(struct ldlm_pool *pl)
420 time64_t recalc_interval_sec;
422 time64_t ldlm_pool_recalc(struct ldlm_pool *pl)
424 timeout_t recalc_interval_sec;
428 recalc_interval_sec = ktime_get_seconds() - pl->pl_recalc_time;
429 if (recalc_interval_sec > 0) {
430 spin_lock(&pl->pl_lock);
432 recalc_interval_sec = ktime_get_real_seconds() -
435 recalc_interval_sec = ktime_get_real_seconds() -
438 recalc_interval_sec = ktime_get_seconds() - pl->pl_recalc_time;
441 if (recalc_interval_sec > 0) {
443 * Update pool statistics every recalc interval.
445 ldlm_pool_recalc_stats(pl, recalc_interval_sec);
448 * Zero out all rates and speed for the last period.
450 atomic_set(&pl->pl_grant_rate, 0);
451 atomic_set(&pl->pl_cancel_rate, 0);
453 spin_unlock(&pl->pl_lock);
456 if (pl->pl_ops->po_recalc) {
457 count = pl->pl_ops->po_recalc(pl);
458 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_RECALC_STAT,
463 recalc_interval_sec = pl->pl_recalc_time - ktime_get_real_seconds() +
464 pl->pl_recalc_period;
465 if (recalc_interval_sec <= 0) {
466 /* DEBUG: should be re-removed after LU-4536 is fixed */
468 "%s: Negative interval(%ld), too short period(%ld)\n",
469 pl->pl_name, (long)recalc_interval_sec,
470 (long)pl->pl_recalc_period);
472 /* Prevent too frequent recalculation. */
473 recalc_interval_sec = 1;
476 return recalc_interval_sec;
478 recalc_interval_sec = pl->pl_recalc_time - ktime_get_real_seconds() +
479 pl->pl_recalc_period;
480 if (recalc_interval_sec <= 0) {
481 /* DEBUG: should be re-removed after LU-4536 is fixed */
482 CDEBUG(D_DLMTRACE, "%s: Negative interval(%lld), too short period(%lld)\n",
483 pl->pl_name, recalc_interval_sec,
484 (s64)pl->pl_recalc_period);
486 /* Prevent too frequent recalculation. */
487 recalc_interval_sec = 1;
490 return recalc_interval_sec;
492 return pl->pl_recalc_time + pl->pl_recalc_period;
497 * Pool shrink wrapper. Will call either client or server pool recalc callback
498 * depending what pool pl is used. When nr == 0, just return the number of
499 * freeable locks. Otherwise, return the number of canceled locks.
501 static int ldlm_pool_shrink(struct ldlm_pool *pl, int nr, gfp_t gfp_mask)
505 if (pl->pl_ops->po_shrink) {
506 cancel = pl->pl_ops->po_shrink(pl, nr, gfp_mask);
508 lprocfs_counter_add(pl->pl_stats,
509 LDLM_POOL_SHRINK_REQTD_STAT,
511 lprocfs_counter_add(pl->pl_stats,
512 LDLM_POOL_SHRINK_FREED_STAT,
515 "%s: request to shrink %d locks, shrunk %d\n",
516 pl->pl_name, nr, cancel);
522 static int lprocfs_pool_state_seq_show(struct seq_file *m, void *unused)
524 int granted, grant_rate, cancel_rate;
525 int grant_speed, lvf;
526 struct ldlm_pool *pl = m->private;
531 spin_lock(&pl->pl_lock);
532 slv = pl->pl_server_lock_volume;
533 clv = pl->pl_client_lock_volume;
534 limit = atomic_read(&pl->pl_limit);
535 granted = atomic_read(&pl->pl_granted);
536 period = ktime_get_seconds() - pl->pl_recalc_time;
539 grant_rate = atomic_read(&pl->pl_grant_rate) / period;
540 cancel_rate = atomic_read(&pl->pl_cancel_rate) / period;
541 grant_speed = grant_rate - cancel_rate;
542 lvf = atomic_read(&pl->pl_lock_volume_factor);
543 spin_unlock(&pl->pl_lock);
545 seq_printf(m, "LDLM pool state (%s):\n"
549 pl->pl_name, slv, clv, (lvf * 100) >> 8);
551 seq_printf(m, " GR: %d\n CR: %d\n GS: %d\n"
553 grant_rate, cancel_rate, grant_speed,
559 LDEBUGFS_SEQ_FOPS_RO(lprocfs_pool_state);
561 static ssize_t grant_speed_show(struct kobject *kobj, struct attribute *attr,
564 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,
569 spin_lock(&pl->pl_lock);
570 /* serialize with ldlm_pool_recalc */
571 period = ktime_get_seconds() - pl->pl_recalc_time;
574 grant_speed = (atomic_read(&pl->pl_grant_rate) -
575 atomic_read(&pl->pl_cancel_rate)) / period;
576 spin_unlock(&pl->pl_lock);
577 return sprintf(buf, "%d\n", grant_speed);
579 LUSTRE_RO_ATTR(grant_speed);
581 LDLM_POOL_SYSFS_READER_SHOW(grant_plan, int);
582 LUSTRE_RO_ATTR(grant_plan);
584 LDLM_POOL_SYSFS_READER_SHOW(recalc_period, int);
585 LDLM_POOL_SYSFS_WRITER_STORE(recalc_period, int);
586 LUSTRE_RW_ATTR(recalc_period);
588 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(server_lock_volume, u64);
589 LUSTRE_RO_ATTR(server_lock_volume);
591 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(client_lock_volume, u64);
592 LUSTRE_RO_ATTR(client_lock_volume);
594 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(limit, atomic);
595 LDLM_POOL_SYSFS_WRITER_NOLOCK_STORE(limit, atomic);
596 LUSTRE_RW_ATTR(limit);
598 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(granted, atomic);
599 LUSTRE_RO_ATTR(granted);
601 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(cancel_rate, atomic);
602 LUSTRE_RO_ATTR(cancel_rate);
604 LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(grant_rate, atomic);
605 LUSTRE_RO_ATTR(grant_rate);
607 static ssize_t lock_volume_factor_show(struct kobject *kobj,
608 struct attribute *attr,
611 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool, pl_kobj);
614 tmp = (atomic_read(&pl->pl_lock_volume_factor) * 100) >> 8;
615 return sprintf(buf, "%lu\n", tmp);
618 static ssize_t lock_volume_factor_store(struct kobject *kobj,
619 struct attribute *attr,
623 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool, pl_kobj);
627 rc = kstrtoul(buffer, 10, &tmp);
632 tmp = (tmp << 8) / 100;
633 atomic_set(&pl->pl_lock_volume_factor, tmp);
638 LUSTRE_RW_ATTR(lock_volume_factor);
640 static ssize_t recalc_time_show(struct kobject *kobj,
641 struct attribute *attr,
644 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool, pl_kobj);
646 return snprintf(buf, PAGE_SIZE, "%llu\n",
647 ktime_get_seconds() - pl->pl_recalc_time);
649 LUSTRE_RO_ATTR(recalc_time);
651 /* These are for pools in /sys/fs/lustre/ldlm/namespaces/.../pool */
652 static struct attribute *ldlm_pl_attrs[] = {
653 &lustre_attr_grant_speed.attr,
654 &lustre_attr_grant_plan.attr,
655 &lustre_attr_recalc_period.attr,
656 &lustre_attr_server_lock_volume.attr,
657 &lustre_attr_client_lock_volume.attr,
658 &lustre_attr_recalc_time.attr,
659 &lustre_attr_limit.attr,
660 &lustre_attr_granted.attr,
661 &lustre_attr_cancel_rate.attr,
662 &lustre_attr_grant_rate.attr,
663 &lustre_attr_lock_volume_factor.attr,
667 static void ldlm_pl_release(struct kobject *kobj)
669 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,
671 complete(&pl->pl_kobj_unregister);
674 static struct kobj_type ldlm_pl_ktype = {
675 .default_attrs = ldlm_pl_attrs,
676 .sysfs_ops = &lustre_sysfs_ops,
677 .release = ldlm_pl_release,
680 static int ldlm_pool_sysfs_init(struct ldlm_pool *pl)
682 struct ldlm_namespace *ns = container_of(pl, struct ldlm_namespace,
686 init_completion(&pl->pl_kobj_unregister);
687 err = kobject_init_and_add(&pl->pl_kobj, &ldlm_pl_ktype, &ns->ns_kobj,
693 static int ldlm_pool_debugfs_init(struct ldlm_pool *pl)
695 struct ldlm_namespace *ns = container_of(pl, struct ldlm_namespace,
697 struct dentry *debugfs_ns_parent;
698 struct ldebugfs_vars pool_vars[2];
701 debugfs_ns_parent = ns->ns_debugfs_entry;
702 if (IS_ERR_OR_NULL(debugfs_ns_parent)) {
703 CERROR("%s: debugfs entry is not initialized\n",
708 pl->pl_debugfs_entry = debugfs_create_dir("pool", debugfs_ns_parent);
710 memset(pool_vars, 0, sizeof(pool_vars));
712 ldlm_add_var(&pool_vars[0], pl->pl_debugfs_entry, "state", pl,
713 &lprocfs_pool_state_fops);
715 pl->pl_stats = lprocfs_alloc_stats(LDLM_POOL_LAST_STAT -
716 LDLM_POOL_FIRST_STAT, 0);
722 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
723 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
725 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_STAT,
726 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
728 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_STAT,
729 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
731 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
732 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
733 "grant_rate", "locks/s");
734 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
735 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
736 "cancel_rate", "locks/s");
737 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
738 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
739 "grant_plan", "locks/s");
740 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SLV_STAT,
741 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
743 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_REQTD_STAT,
744 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
745 "shrink_request", "locks");
746 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_FREED_STAT,
747 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
748 "shrink_freed", "locks");
749 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_RECALC_STAT,
750 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
751 "recalc_freed", "locks");
752 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_TIMING_STAT,
753 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
754 "recalc_timing", "sec");
755 debugfs_create_file("stats", 0644, pl->pl_debugfs_entry, pl->pl_stats,
756 &ldebugfs_stats_seq_fops);
762 static void ldlm_pool_sysfs_fini(struct ldlm_pool *pl)
764 kobject_put(&pl->pl_kobj);
765 wait_for_completion(&pl->pl_kobj_unregister);
768 static void ldlm_pool_debugfs_fini(struct ldlm_pool *pl)
771 lprocfs_free_stats(&pl->pl_stats);
774 debugfs_remove_recursive(pl->pl_debugfs_entry);
777 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
778 int idx, enum ldlm_side client)
782 spin_lock_init(&pl->pl_lock);
783 atomic_set(&pl->pl_granted, 0);
784 pl->pl_recalc_time = ktime_get_seconds();
785 atomic_set(&pl->pl_lock_volume_factor, 1 << 8);
787 atomic_set(&pl->pl_grant_rate, 0);
788 atomic_set(&pl->pl_cancel_rate, 0);
789 pl->pl_grant_plan = LDLM_POOL_GP(LDLM_POOL_HOST_L);
791 snprintf(pl->pl_name, sizeof(pl->pl_name), "ldlm-pool-%s-%d",
792 ldlm_ns_name(ns), idx);
794 atomic_set(&pl->pl_limit, 1);
795 pl->pl_server_lock_volume = 0;
796 pl->pl_ops = &ldlm_cli_pool_ops;
797 pl->pl_recalc_period = LDLM_POOL_CLI_DEF_RECALC_PERIOD;
798 pl->pl_client_lock_volume = 0;
799 rc = ldlm_pool_debugfs_init(pl);
803 rc = ldlm_pool_sysfs_init(pl);
807 CDEBUG(D_DLMTRACE, "Lock pool %s is initialized\n", pl->pl_name);
812 void ldlm_pool_fini(struct ldlm_pool *pl)
814 ldlm_pool_sysfs_fini(pl);
815 ldlm_pool_debugfs_fini(pl);
818 * Pool should not be used after this point. We can't free it here as
819 * it lives in struct ldlm_namespace, but still interested in catching
820 * any abnormal using cases.
822 POISON(pl, 0x5a, sizeof(*pl));
826 * Add new taken ldlm lock @lock into pool @pl accounting.
828 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
831 * FLOCK locks are special in a sense that they are almost never
832 * cancelled, instead special kind of lock is used to drop them.
833 * also there is no LRU for flock locks, so no point in tracking
836 if (lock->l_resource->lr_type == LDLM_FLOCK)
839 atomic_inc(&pl->pl_granted);
840 atomic_inc(&pl->pl_grant_rate);
841 lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_GRANT_STAT);
843 * Do not do pool recalc for client side as all locks which
844 * potentially may be canceled has already been packed into
845 * enqueue/cancel rpc. Also we do not want to run out of stack
846 * with too long call paths.
851 * Remove ldlm lock @lock from pool @pl accounting.
853 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
856 * Filter out FLOCK locks. Read above comment in ldlm_pool_add().
858 if (lock->l_resource->lr_type == LDLM_FLOCK)
861 LASSERT(atomic_read(&pl->pl_granted) > 0);
862 atomic_dec(&pl->pl_granted);
863 atomic_inc(&pl->pl_cancel_rate);
865 lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_CANCEL_STAT);
869 * Returns current @pl SLV.
871 * \pre ->pl_lock is not locked.
873 u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
877 spin_lock(&pl->pl_lock);
878 slv = pl->pl_server_lock_volume;
879 spin_unlock(&pl->pl_lock);
884 * Sets passed @clv to @pl.
886 * \pre ->pl_lock is not locked.
888 void ldlm_pool_set_clv(struct ldlm_pool *pl, u64 clv)
890 spin_lock(&pl->pl_lock);
891 pl->pl_client_lock_volume = clv;
892 spin_unlock(&pl->pl_lock);
896 * Returns current LVF from @pl.
898 u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
900 return atomic_read(&pl->pl_lock_volume_factor);
903 static int ldlm_pool_granted(struct ldlm_pool *pl)
905 return atomic_read(&pl->pl_granted);
909 * count locks from all namespaces (if possible). Returns number of
912 static unsigned long ldlm_pools_count(enum ldlm_side client, gfp_t gfp_mask)
914 unsigned long total = 0;
916 struct ldlm_namespace *ns;
917 struct ldlm_namespace *ns_old = NULL; /* loop detection */
919 if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
923 * Find out how many resources we may release.
925 for (nr_ns = ldlm_namespace_nr_read(client);
926 nr_ns > 0; nr_ns--) {
927 mutex_lock(ldlm_namespace_lock(client));
928 if (list_empty(ldlm_namespace_list(client))) {
929 mutex_unlock(ldlm_namespace_lock(client));
932 ns = ldlm_namespace_first_locked(client);
935 mutex_unlock(ldlm_namespace_lock(client));
939 if (ldlm_ns_empty(ns)) {
940 ldlm_namespace_move_to_inactive_locked(ns, client);
941 mutex_unlock(ldlm_namespace_lock(client));
948 ldlm_namespace_get(ns);
949 ldlm_namespace_move_to_active_locked(ns, client);
950 mutex_unlock(ldlm_namespace_lock(client));
951 total += ldlm_pool_shrink(&ns->ns_pool, 0, gfp_mask);
952 ldlm_namespace_put(ns);
958 static unsigned long ldlm_pools_scan(enum ldlm_side client, int nr,
961 unsigned long freed = 0;
963 struct ldlm_namespace *ns;
965 if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
969 * Shrink at least ldlm_namespace_nr_read(client) namespaces.
971 for (tmp = nr_ns = ldlm_namespace_nr_read(client);
973 int cancel, nr_locks;
976 * Do not call shrink under ldlm_namespace_lock(client)
978 mutex_lock(ldlm_namespace_lock(client));
979 if (list_empty(ldlm_namespace_list(client))) {
980 mutex_unlock(ldlm_namespace_lock(client));
983 ns = ldlm_namespace_first_locked(client);
984 ldlm_namespace_get(ns);
985 ldlm_namespace_move_to_active_locked(ns, client);
986 mutex_unlock(ldlm_namespace_lock(client));
988 nr_locks = ldlm_pool_granted(&ns->ns_pool);
990 * We use to shrink propotionally but with new shrinker API,
991 * we lost the total number of freeable locks.
993 cancel = 1 + min_t(int, nr_locks, nr / nr_ns);
994 freed += ldlm_pool_shrink(&ns->ns_pool, cancel, gfp_mask);
995 ldlm_namespace_put(ns);
998 * we only decrease the SLV in server pools shrinker, return
999 * SHRINK_STOP to kernel to avoid needless loop. LU-1128
1004 static unsigned long ldlm_pools_cli_count(struct shrinker *s,
1005 struct shrink_control *sc)
1007 return ldlm_pools_count(LDLM_NAMESPACE_CLIENT, sc->gfp_mask);
1010 static unsigned long ldlm_pools_cli_scan(struct shrinker *s,
1011 struct shrink_control *sc)
1013 return ldlm_pools_scan(LDLM_NAMESPACE_CLIENT, sc->nr_to_scan,
1017 static void ldlm_pools_recalc(struct work_struct *ws);
1018 static DECLARE_DELAYED_WORK(ldlm_recalc_pools, ldlm_pools_recalc);
1020 static void ldlm_pools_recalc(struct work_struct *ws)
1022 enum ldlm_side client = LDLM_NAMESPACE_CLIENT;
1023 struct ldlm_namespace *ns;
1024 struct ldlm_namespace *ns_old = NULL;
1025 /* seconds of sleep if no active namespaces */
1027 time64_t time = LDLM_POOL_CLI_DEF_RECALC_PERIOD;
1029 time64_t delay = side == LDLM_NAMESPACE_SERVER ?
1030 LDLM_POOL_SRV_DEF_RECALC_PERIOD :
1031 LDLM_POOL_CLI_DEF_RECALC_PERIOD;
1033 time64_t delay = ktime_get_seconds() +
1034 (side == LDLM_NAMESPACE_SERVER ?
1035 LDLM_POOL_SRV_DEF_RECALC_PERIOD :
1036 LDLM_POOL_CLI_DEF_RECALC_PERIOD);
1041 * Recalc at least ldlm_namespace_nr_read(client) namespaces.
1043 for (nr = ldlm_namespace_nr_read(client); nr > 0; nr--) {
1046 * Lock the list, get first @ns in the list, getref, move it
1047 * to the tail, unlock and call pool recalc. This way we avoid
1048 * calling recalc under @ns lock what is really good as we get
1049 * rid of potential deadlock on client nodes when canceling
1050 * locks synchronously.
1052 mutex_lock(ldlm_namespace_lock(client));
1053 if (list_empty(ldlm_namespace_list(client))) {
1054 mutex_unlock(ldlm_namespace_lock(client));
1057 ns = ldlm_namespace_first_locked(client);
1059 if (ns_old == ns) { /* Full pass complete */
1060 mutex_unlock(ldlm_namespace_lock(client));
1064 /* We got an empty namespace, need to move it back to inactive
1066 * The race with parallel resource creation is fine:
1067 * - If they do namespace_get before our check, we fail the
1068 * check and they move this item to the end of the list anyway
1069 * - If we do the check and then they do namespace_get, then
1070 * we move the namespace to inactive and they will move
1071 * it back to active (synchronised by the lock, so no clash
1074 if (ldlm_ns_empty(ns)) {
1075 ldlm_namespace_move_to_inactive_locked(ns, client);
1076 mutex_unlock(ldlm_namespace_lock(client));
1083 spin_lock(&ns->ns_lock);
1085 * skip ns which is being freed, and we don't want to increase
1086 * its refcount again, not even temporarily. bz21519 & LU-499.
1088 if (ns->ns_stopping) {
1092 ldlm_namespace_get(ns);
1094 spin_unlock(&ns->ns_lock);
1096 ldlm_namespace_move_to_active_locked(ns, client);
1097 mutex_unlock(ldlm_namespace_lock(client));
1100 * After setup is done - recalc the pool.
1103 time64_t ttime = ldlm_pool_recalc(&ns->ns_pool);
1108 ldlm_namespace_put(ns);
1112 /* Wake up the blocking threads from time to time. */
1113 ldlm_bl_thread_wakeup();
1115 delay -= ktime_get_seconds();
1117 /* Prevent too frequent recalculation. */
1118 CDEBUG(D_DLMTRACE, "Negative interval(%lld)\n", delay);
1122 schedule_delayed_work(&ldlm_recalc_pools, time * HZ);
1125 static int ldlm_pools_thread_start(void)
1129 schedule_delayed_work(&ldlm_recalc_pools, 0);
1135 static void ldlm_pools_thread_stop(void)
1138 DEF_SHRINKER_VAR(shcvar, ldlm_pools_cli_shrink,
1139 ldlm_pools_cli_count, ldlm_pools_cli_scan);
1141 schedule_delayed_work(&ldlm_pools_recalc_work,
1142 LDLM_POOL_CLI_DEF_RECALC_PERIOD);
1143 ldlm_pools_srv_shrinker = set_shrinker(DEFAULT_SEEKS, &shsvar);
1145 DEF_SHRINKER_VAR(shcvar, ldlm_pools_cli_shrink,
1146 ldlm_pools_cli_count, ldlm_pools_cli_scan);
1148 #ifdef HAVE_SERVER_SUPPORT
1149 delay = min(LDLM_POOL_SRV_DEF_RECALC_PERIOD,
1150 LDLM_POOL_CLI_DEF_RECALC_PERIOD);
1152 delay = LDLM_POOL_CLI_DEF_RECALC_PERIOD;
1155 schedule_delayed_work(&ldlm_pools_recalc_work, delay);
1156 ldlm_pools_srv_shrinker = set_shrinker(DEFAULT_SEEKS, &shsvar);
1158 cancel_delayed_work_sync(&ldlm_recalc_pools);
1161 static struct shrinker ldlm_pools_cli_shrinker = {
1162 .count_objects = ldlm_pools_cli_count,
1163 .scan_objects = ldlm_pools_cli_scan,
1164 .seeks = DEFAULT_SEEKS,
1167 int ldlm_pools_init(void)
1171 rc = ldlm_pools_thread_start();
1173 rc = register_shrinker(&ldlm_pools_cli_shrinker);
1178 void ldlm_pools_fini(void)
1180 unregister_shrinker(&ldlm_pools_cli_shrinker);
1182 ldlm_pools_thread_stop();