1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2007 Cluster File Systems, Inc.
5 * Author: Yury Umanets <umka@clusterfs.com>
7 * This file is part of the Lustre file system, http://www.lustre.org
8 * Lustre is a trademark of Cluster File Systems, Inc.
10 * You may have signed or agreed to another license before downloading
11 * this software. If so, you are bound by the terms and conditions
12 * of that agreement, and the following does not apply to you. See the
13 * LICENSE file included with this distribution for more information.
15 * If you did not agree to a different license, then this copy of Lustre
16 * is open source software; you can redistribute it and/or modify it
17 * under the terms of version 2 of the GNU General Public License as
18 * published by the Free Software Foundation.
20 * In either case, Lustre is distributed in the hope that it will be
21 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * license text for more details.
26 /* Idea of this code is rather simple. Each second, for each server namespace
27 * we have SLV - server lock volume which is calculated on current number of
28 * granted locks, grant speed for past period, etc - that is, locking load.
29 * This SLV number may be thought as a flow definition for simplicity. It is
30 * sent to clients with each occasion to let them know what is current load
31 * situation on the server. By default, at the beginning, SLV on server is
32 * set max value which is calculated as the following: allow to one client
33 * have all locks of limit ->pl_limit for 10h.
35 * Next, on clients, number of cached locks is not limited artificially in any
36 * way as it was before. Instead, client calculates CLV, that is, client lock
37 * volume for each lock and compares it with last SLV from the server. CLV is
38 * calculated as the number of locks in LRU * lock live time in seconds. If
39 * CLV > SLV - lock is canceled.
41 * Client has LVF, that is, lock volume factor which regulates how much sensitive
42 * client should be about last SLV from server. The higher LVF is the more locks
43 * will be canceled on client. Default value for it is 1. Setting LVF to 2 means
44 * that client will cancel locks 2 times faster.
46 * Locks on a client will be canceled more intensively in these cases:
47 * (1) if SLV is smaller, that is, load is higher on the server;
48 * (2) client has a lot of locks (the more locks are held by client, the bigger
49 * chances that some of them should be canceled);
50 * (3) client has old locks (taken some time ago);
52 * Thus, according to flow paradigm that we use for better understanding SLV,
53 * CLV is the volume of particle in flow described by SLV. According to this,
54 * if flow is getting thinner, more and more particles become outside of it and
55 * as particles are locks, they should be canceled.
57 * General idea of this belongs to Vitaly Fertman (vitaly@clusterfs.com). Andreas
58 * Dilger (adilger@clusterfs.com) proposed few nice ideas like using LVF and many
59 * cleanups. Flow definition to allow more easy understanding of the logic belongs
60 * to Nikita Danilov (nikita@clusterfs.com) as well as many cleanups and fixes.
61 * And design and implementation are done by Yury Umanets (umka@clusterfs.com).
63 * Glossary for terms used:
65 * pl_limit - Number of allowed locks in pool. Applies to server and client
68 * pl_granted - Number of granted locks (calculated);
69 * pl_grant_rate - Number of granted locks for last T (calculated);
70 * pl_cancel_rate - Number of canceled locks for last T (calculated);
71 * pl_grant_speed - Grant speed (GR - CR) for last T (calculated);
72 * pl_grant_plan - Planned number of granted locks for next T (calculated);
74 * pl_grant_step - Grant plan step, that is how ->pl_grant_plan
75 * will change in next T (tunable);
77 * pl_server_lock_volume - Current server lock volume (calculated);
79 * As it may be seen from list above, we have few possible tunables which may
80 * affect behavior much. They all may be modified via proc. However, they also
81 * give a possibility for constructing few pre-defined behavior policies. If
82 * none of predefines is suitable for a working pattern being used, new one may
83 * be "constructed" via proc tunables.
86 #define DEBUG_SUBSYSTEM S_LDLM
89 # include <lustre_dlm.h>
91 # include <liblustre.h>
92 # include <libcfs/kp30.h>
95 #include <obd_class.h>
96 #include <obd_support.h>
97 #include "ldlm_internal.h"
99 #ifdef HAVE_LRU_RESIZE_SUPPORT
101 /* 50 ldlm locks for 1MB of RAM. */
102 #define LDLM_POOL_HOST_L ((num_physpages >> (20 - PAGE_SHIFT)) * 50)
104 /* Default step in % for grant plan. */
105 #define LDLM_POOL_GSP (5)
107 /* LDLM_POOL_GSP% of all locks is default GP. */
108 #define LDLM_POOL_GP(L) ((L) * LDLM_POOL_GSP / 100)
110 /* Max age for locks on clients. */
111 #define LDLM_POOL_MAX_AGE (36000)
114 extern cfs_proc_dir_entry_t *ldlm_ns_proc_dir;
117 #define avg(src, add) \
118 ((src) = ((src) + (add)) / 2)
120 static inline __u64 dru(__u64 val, __u32 div)
122 __u64 ret = val + (div - 1);
127 static inline __u64 ldlm_pool_slv_max(__u32 L)
129 /* Allow to have all locks for 1 client for 10 hrs.
130 * Formula is the following: limit * 10h / 1 client. */
131 __u64 lim = L * LDLM_POOL_MAX_AGE / 1;
135 static inline __u64 ldlm_pool_slv_min(__u32 L)
141 LDLM_POOL_FIRST_STAT = 0,
142 LDLM_POOL_GRANTED_STAT = LDLM_POOL_FIRST_STAT,
143 LDLM_POOL_GRANT_STAT,
144 LDLM_POOL_CANCEL_STAT,
145 LDLM_POOL_GRANT_RATE_STAT,
146 LDLM_POOL_CANCEL_RATE_STAT,
147 LDLM_POOL_GRANT_PLAN_STAT,
149 LDLM_POOL_SHRINK_REQTD_STAT,
150 LDLM_POOL_SHRINK_FREED_STAT,
151 LDLM_POOL_RECALC_STAT,
155 static inline struct ldlm_namespace *ldlm_pl2ns(struct ldlm_pool *pl)
157 return container_of(pl, struct ldlm_namespace, ns_pool);
160 /* Should be called under ->pl_lock taken */
161 static inline void ldlm_pool_recalc_grant_plan(struct ldlm_pool *pl)
163 int grant_plan, granted;
166 limit = ldlm_pool_get_limit(pl);
167 granted = atomic_read(&pl->pl_granted);
169 grant_plan = granted + ((limit - granted) *
170 atomic_read(&pl->pl_grant_step)) / 100;
171 atomic_set(&pl->pl_grant_plan, grant_plan);
174 /* Should be called under ->pl_lock taken */
175 static inline void ldlm_pool_recalc_slv(struct ldlm_pool *pl)
177 int slv_factor, granted, grant_plan;
181 slv = ldlm_pool_get_slv(pl);
182 limit = ldlm_pool_get_limit(pl);
183 granted = atomic_read(&pl->pl_granted);
184 grant_plan = atomic_read(&pl->pl_grant_plan);
186 if ((slv_factor = limit - (granted - grant_plan)) <= 0)
189 slv = (slv * ((slv_factor * 100) / limit));
192 if (slv > ldlm_pool_slv_max(limit)) {
193 slv = ldlm_pool_slv_max(limit);
194 } else if (slv < ldlm_pool_slv_min(limit)) {
195 slv = ldlm_pool_slv_min(limit);
198 ldlm_pool_set_slv(pl, slv);
201 static inline void ldlm_pool_recalc_stats(struct ldlm_pool *pl)
203 __u64 slv = ldlm_pool_get_slv(pl);
204 __u32 granted = atomic_read(&pl->pl_granted);
205 __u32 grant_rate = atomic_read(&pl->pl_grant_rate);
206 __u32 grant_plan = atomic_read(&pl->pl_grant_plan);
207 __u32 cancel_rate = atomic_read(&pl->pl_cancel_rate);
209 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_SLV_STAT,
211 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
213 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
215 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
217 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
221 static int ldlm_srv_pool_recalc(struct ldlm_pool *pl)
223 time_t recalc_interval_sec;
226 spin_lock(&pl->pl_lock);
227 recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
228 if (recalc_interval_sec > 0) {
229 /* Update statistics */
230 ldlm_pool_recalc_stats(pl);
232 /* Recalc SLV after last period. This should be done
233 * _before_ recalculating new grant plan. */
234 ldlm_pool_recalc_slv(pl);
236 /* Update grant_plan for new period. */
237 ldlm_pool_recalc_grant_plan(pl);
239 /* Zero out all rates and speed for the last period. */
240 atomic_set(&pl->pl_grant_rate, 0);
241 atomic_set(&pl->pl_cancel_rate, 0);
242 atomic_set(&pl->pl_grant_speed, 0);
243 pl->pl_recalc_time = cfs_time_current_sec();
245 spin_unlock(&pl->pl_lock);
249 /* Our goal here is to decrease SLV the way to make a client hold
250 * @nr locks smaller in next 10h. */
251 static int ldlm_srv_pool_shrink(struct ldlm_pool *pl,
252 int nr, unsigned int gfp_mask)
257 /* VM is asking how many entries may be potentially freed. */
259 RETURN(atomic_read(&pl->pl_granted));
261 /* Client already canceled locks but server is already in shrinker
262 * and can't cancel anything. Let's catch this race. */
263 if (atomic_read(&pl->pl_granted) == 0)
266 spin_lock(&pl->pl_lock);
268 /* We want shrinker to possibly cause cancelation of @nr locks from
269 * clients or grant approximately @nr locks smaller next intervals.
271 * This is why we decresed SLV by @nr. This effect will only be as
272 * long as one re-calc interval (1s these days) and this should be
273 * enough to pass this decreased SLV to all clients. On next recalc
274 * interval pool will either increase SLV if locks load is not high
275 * or will keep on same level or even decrease again, thus, shrinker
276 * decreased SLV will affect next recalc intervals and this way will
277 * make locking load lower. */
278 if (nr < ldlm_pool_get_slv(pl)) {
279 ldlm_pool_set_slv(pl, ldlm_pool_get_slv(pl) - nr);
281 limit = ldlm_pool_get_limit(pl);
282 ldlm_pool_set_slv(pl, ldlm_pool_slv_min(limit));
284 spin_unlock(&pl->pl_lock);
286 /* We did not really free any memory here so far, it only will be
287 * freed later may be, so that we return 0 to not confuse VM. */
291 static int ldlm_srv_pool_setup(struct ldlm_pool *pl, int limit)
294 ldlm_pool_set_limit(pl, limit);
298 static int ldlm_cli_pool_recalc(struct ldlm_pool *pl)
300 time_t recalc_interval_sec;
303 spin_lock(&pl->pl_lock);
305 recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
306 if (recalc_interval_sec > 0) {
307 /* Update statistics only every T */
308 ldlm_pool_recalc_stats(pl);
310 /* Zero out grant/cancel rates and speed for last period. */
311 atomic_set(&pl->pl_grant_rate, 0);
312 atomic_set(&pl->pl_cancel_rate, 0);
313 atomic_set(&pl->pl_grant_speed, 0);
314 pl->pl_recalc_time = cfs_time_current_sec();
316 spin_unlock(&pl->pl_lock);
318 /* Do not cancel locks in case lru resize is disabled for this ns */
319 if (!ns_connect_lru_resize(ldlm_pl2ns(pl)))
322 /* In the time of canceling locks on client we do not need to maintain
323 * sharp timing, we only want to cancel locks asap according to new SLV.
324 * This may be called when SLV has changed much, this is why we do not
325 * take into account pl->pl_recalc_time here. */
326 RETURN(ldlm_cancel_lru(ldlm_pl2ns(pl), 0, LDLM_ASYNC,
330 static int ldlm_cli_pool_shrink(struct ldlm_pool *pl,
331 int nr, unsigned int gfp_mask)
335 /* Do not cancel locks in case lru resize is disabled for this ns */
336 if (!ns_connect_lru_resize(ldlm_pl2ns(pl)))
339 /* Find out how many locks may be released according to shrink
342 RETURN(ldlm_cancel_lru_local(ldlm_pl2ns(pl), NULL, 0,
343 0, 0, LDLM_CANCEL_SHRINK));
345 /* Cancel @nr locks accoding to shrink policy */
346 RETURN(ldlm_cancel_lru(ldlm_pl2ns(pl), nr, LDLM_SYNC,
347 LDLM_CANCEL_SHRINK));
350 struct ldlm_pool_ops ldlm_srv_pool_ops = {
351 .po_recalc = ldlm_srv_pool_recalc,
352 .po_shrink = ldlm_srv_pool_shrink,
353 .po_setup = ldlm_srv_pool_setup
356 struct ldlm_pool_ops ldlm_cli_pool_ops = {
357 .po_recalc = ldlm_cli_pool_recalc,
358 .po_shrink = ldlm_cli_pool_shrink
361 int ldlm_pool_recalc(struct ldlm_pool *pl)
365 if (pl->pl_ops->po_recalc != NULL && pool_recalc_enabled(pl)) {
366 count = pl->pl_ops->po_recalc(pl);
367 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_RECALC_STAT,
373 EXPORT_SYMBOL(ldlm_pool_recalc);
375 int ldlm_pool_shrink(struct ldlm_pool *pl, int nr,
376 unsigned int gfp_mask)
380 if (pl->pl_ops->po_shrink != NULL && pool_shrink_enabled(pl)) {
381 cancel = pl->pl_ops->po_shrink(pl, nr, gfp_mask);
383 lprocfs_counter_add(pl->pl_stats,
384 LDLM_POOL_SHRINK_REQTD_STAT,
386 lprocfs_counter_add(pl->pl_stats,
387 LDLM_POOL_SHRINK_FREED_STAT,
389 CDEBUG(D_DLMTRACE, "%s: request to shrink %d locks, "
390 "shrunk %d\n", pl->pl_name, nr, cancel);
395 EXPORT_SYMBOL(ldlm_pool_shrink);
397 /* The purpose of this function is to re-setup limit and maximal allowed
398 * slv according to the passed limit. */
399 int ldlm_pool_setup(struct ldlm_pool *pl, int limit)
402 if (pl->pl_ops->po_setup != NULL)
403 RETURN(pl->pl_ops->po_setup(pl, limit));
406 EXPORT_SYMBOL(ldlm_pool_setup);
409 static int lprocfs_rd_pool_state(char *page, char **start, off_t off,
410 int count, int *eof, void *data)
412 __u32 granted, grant_rate, cancel_rate, grant_step;
413 int nr = 0, grant_speed, grant_plan;
414 struct ldlm_pool *pl = data;
418 spin_lock(&pl->pl_lock);
419 slv = ldlm_pool_get_slv(pl);
420 limit = ldlm_pool_get_limit(pl);
421 granted = atomic_read(&pl->pl_granted);
422 grant_rate = atomic_read(&pl->pl_grant_rate);
423 grant_plan = atomic_read(&pl->pl_grant_plan);
424 grant_step = atomic_read(&pl->pl_grant_step);
425 grant_speed = atomic_read(&pl->pl_grant_speed);
426 cancel_rate = atomic_read(&pl->pl_cancel_rate);
427 spin_unlock(&pl->pl_lock);
429 nr += snprintf(page + nr, count - nr, "LDLM pool state (%s):\n",
431 nr += snprintf(page + nr, count - nr, " SLV: "LPU64"\n", slv);
433 nr += snprintf(page + nr, count - nr, " LVF: %d\n",
434 atomic_read(&pl->pl_lock_volume_factor));
436 nr += snprintf(page + nr, count - nr, " GSP: %d%%\n",
438 nr += snprintf(page + nr, count - nr, " GP: %d\n",
440 nr += snprintf(page + nr, count - nr, " GR: %d\n",
442 nr += snprintf(page + nr, count - nr, " CR: %d\n",
444 nr += snprintf(page + nr, count - nr, " GS: %d\n",
446 nr += snprintf(page + nr, count - nr, " G: %d\n",
448 nr += snprintf(page + nr, count - nr, " L: %d\n",
453 static int ldlm_pool_proc_init(struct ldlm_pool *pl)
455 struct ldlm_namespace *ns = ldlm_pl2ns(pl);
456 struct proc_dir_entry *parent_ns_proc;
457 struct lprocfs_vars pool_vars[2];
458 char *var_name = NULL;
462 OBD_ALLOC(var_name, MAX_STRING_SIZE + 1);
466 parent_ns_proc = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
467 if (parent_ns_proc == NULL) {
468 CERROR("%s: proc entry is not initialized\n",
470 GOTO(out_free_name, rc = -EINVAL);
472 pl->pl_proc_dir = lprocfs_register("pool", parent_ns_proc,
474 if (IS_ERR(pl->pl_proc_dir)) {
475 CERROR("LProcFS failed in ldlm-pool-init\n");
476 rc = PTR_ERR(pl->pl_proc_dir);
477 GOTO(out_free_name, rc);
480 var_name[MAX_STRING_SIZE] = '\0';
481 memset(pool_vars, 0, sizeof(pool_vars));
482 pool_vars[0].name = var_name;
484 snprintf(var_name, MAX_STRING_SIZE, "server_lock_volume");
485 pool_vars[0].data = &pl->pl_server_lock_volume;
486 pool_vars[0].read_fptr = lprocfs_rd_u64;
487 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
489 snprintf(var_name, MAX_STRING_SIZE, "limit");
490 pool_vars[0].data = &pl->pl_limit;
491 pool_vars[0].read_fptr = lprocfs_rd_atomic;
492 pool_vars[0].write_fptr = lprocfs_wr_atomic;
493 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
495 snprintf(var_name, MAX_STRING_SIZE, "granted");
496 pool_vars[0].data = &pl->pl_granted;
497 pool_vars[0].read_fptr = lprocfs_rd_atomic;
498 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
500 snprintf(var_name, MAX_STRING_SIZE, "control");
501 pool_vars[0].data = &pl->pl_control;
502 pool_vars[0].read_fptr = lprocfs_rd_uint;
503 pool_vars[0].write_fptr = lprocfs_wr_uint;
504 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
506 snprintf(var_name, MAX_STRING_SIZE, "grant_speed");
507 pool_vars[0].data = &pl->pl_grant_speed;
508 pool_vars[0].read_fptr = lprocfs_rd_atomic;
509 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
511 snprintf(var_name, MAX_STRING_SIZE, "cancel_rate");
512 pool_vars[0].data = &pl->pl_cancel_rate;
513 pool_vars[0].read_fptr = lprocfs_rd_atomic;
514 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
516 snprintf(var_name, MAX_STRING_SIZE, "grant_rate");
517 pool_vars[0].data = &pl->pl_grant_rate;
518 pool_vars[0].read_fptr = lprocfs_rd_atomic;
519 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
521 snprintf(var_name, MAX_STRING_SIZE, "grant_plan");
522 pool_vars[0].data = &pl->pl_grant_plan;
523 pool_vars[0].read_fptr = lprocfs_rd_atomic;
524 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
526 snprintf(var_name, MAX_STRING_SIZE, "grant_step");
527 pool_vars[0].data = &pl->pl_grant_step;
528 pool_vars[0].read_fptr = lprocfs_rd_atomic;
529 if (ns_is_server(ns))
530 pool_vars[0].write_fptr = lprocfs_wr_atomic;
531 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
533 snprintf(var_name, MAX_STRING_SIZE, "lock_volume_factor");
534 pool_vars[0].data = &pl->pl_lock_volume_factor;
535 pool_vars[0].read_fptr = lprocfs_rd_uint;
536 pool_vars[0].write_fptr = lprocfs_wr_uint;
537 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
539 snprintf(var_name, MAX_STRING_SIZE, "state");
540 pool_vars[0].data = pl;
541 pool_vars[0].read_fptr = lprocfs_rd_pool_state;
542 lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
544 pl->pl_stats = lprocfs_alloc_stats(LDLM_POOL_LAST_STAT -
545 LDLM_POOL_FIRST_STAT, 0);
547 GOTO(out_free_name, rc = -ENOMEM);
549 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
550 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
552 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_STAT, 0,
554 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_STAT, 0,
556 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
557 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
558 "grant_rate", "locks/s");
559 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
560 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
561 "cancel_rate", "locks/s");
562 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
563 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
564 "grant_plan", "locks/s");
565 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SLV_STAT,
566 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
568 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_REQTD_STAT,
569 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
570 "shrink_request", "locks");
571 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_FREED_STAT,
572 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
573 "shrink_freed", "locks");
574 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_RECALC_STAT,
575 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
576 "recalc_freed", "locks");
577 lprocfs_register_stats(pl->pl_proc_dir, "stats", pl->pl_stats);
581 OBD_FREE(var_name, MAX_STRING_SIZE + 1);
585 static void ldlm_pool_proc_fini(struct ldlm_pool *pl)
587 if (pl->pl_stats != NULL) {
588 lprocfs_free_stats(&pl->pl_stats);
591 if (pl->pl_proc_dir != NULL) {
592 lprocfs_remove(&pl->pl_proc_dir);
593 pl->pl_proc_dir = NULL;
596 #else /* !__KERNEL__*/
597 #define ldlm_pool_proc_init(pl) (0)
598 #define ldlm_pool_proc_fini(pl) while (0) {}
601 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
602 int idx, ldlm_side_t client)
607 spin_lock_init(&pl->pl_lock);
608 atomic_set(&pl->pl_granted, 0);
609 pl->pl_recalc_time = cfs_time_current_sec();
610 atomic_set(&pl->pl_lock_volume_factor, 1);
612 atomic_set(&pl->pl_grant_rate, 0);
613 atomic_set(&pl->pl_cancel_rate, 0);
614 atomic_set(&pl->pl_grant_speed, 0);
615 pl->pl_control = LDLM_POOL_CTL_FULL;
616 atomic_set(&pl->pl_grant_step, LDLM_POOL_GSP);
617 atomic_set(&pl->pl_grant_plan, LDLM_POOL_GP(LDLM_POOL_HOST_L));
619 snprintf(pl->pl_name, sizeof(pl->pl_name), "ldlm-pool-%s-%d",
622 if (client == LDLM_NAMESPACE_SERVER) {
623 pl->pl_ops = &ldlm_srv_pool_ops;
624 ldlm_pool_set_limit(pl, LDLM_POOL_HOST_L);
625 ldlm_pool_set_slv(pl, ldlm_pool_slv_max(LDLM_POOL_HOST_L));
627 ldlm_pool_set_slv(pl, 1);
628 ldlm_pool_set_limit(pl, 1);
629 pl->pl_ops = &ldlm_cli_pool_ops;
632 rc = ldlm_pool_proc_init(pl);
636 CDEBUG(D_DLMTRACE, "Lock pool %s is initialized\n", pl->pl_name);
640 EXPORT_SYMBOL(ldlm_pool_init);
642 void ldlm_pool_fini(struct ldlm_pool *pl)
645 ldlm_pool_proc_fini(pl);
649 EXPORT_SYMBOL(ldlm_pool_fini);
651 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
654 atomic_inc(&pl->pl_granted);
655 atomic_inc(&pl->pl_grant_rate);
656 atomic_inc(&pl->pl_grant_speed);
658 lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_GRANT_STAT);
660 /* Do not do pool recalc for client side as all locks which
661 * potentially may be canceled has already been packed into
662 * enqueue/cancel rpc. Also we do not want to run out of stack
663 * with too long call paths. */
664 if (ns_is_server(ldlm_pl2ns(pl)))
665 ldlm_pool_recalc(pl);
668 EXPORT_SYMBOL(ldlm_pool_add);
670 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
673 LASSERT(atomic_read(&pl->pl_granted) > 0);
674 atomic_dec(&pl->pl_granted);
675 atomic_inc(&pl->pl_cancel_rate);
676 atomic_dec(&pl->pl_grant_speed);
678 lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_CANCEL_STAT);
680 if (ns_is_server(ldlm_pl2ns(pl)))
681 ldlm_pool_recalc(pl);
684 EXPORT_SYMBOL(ldlm_pool_del);
686 /* ->pl_lock should be taken. */
687 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
689 return pl->pl_server_lock_volume;
691 EXPORT_SYMBOL(ldlm_pool_get_slv);
693 /* ->pl_lock should be taken. */
694 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv)
696 pl->pl_server_lock_volume = slv;
698 EXPORT_SYMBOL(ldlm_pool_set_slv);
700 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
702 return atomic_read(&pl->pl_limit);
704 EXPORT_SYMBOL(ldlm_pool_get_limit);
706 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
708 atomic_set(&pl->pl_limit, limit);
710 EXPORT_SYMBOL(ldlm_pool_set_limit);
712 /* Server side is only enabled for kernel space for now. */
714 static int ldlm_pool_granted(struct ldlm_pool *pl)
716 return atomic_read(&pl->pl_granted);
719 static struct ptlrpc_thread *ldlm_pools_thread;
720 static struct shrinker *ldlm_pools_srv_shrinker;
721 static struct shrinker *ldlm_pools_cli_shrinker;
722 static struct completion ldlm_pools_comp;
724 void ldlm_pools_wakeup(void)
727 if (ldlm_pools_thread == NULL)
729 ldlm_pools_thread->t_flags |= SVC_EVENT;
730 cfs_waitq_signal(&ldlm_pools_thread->t_ctl_waitq);
733 EXPORT_SYMBOL(ldlm_pools_wakeup);
735 /* Cancel @nr locks from all namespaces (if possible). Returns number of
736 * cached locks after shrink is finished. All namespaces are asked to
737 * cancel approximately equal amount of locks. */
738 static int ldlm_pools_shrink(ldlm_side_t client, int nr,
739 unsigned int gfp_mask)
741 int total = 0, cached = 0, nr_ns;
742 struct ldlm_namespace *ns;
744 if (nr != 0 && !(gfp_mask & __GFP_FS))
747 CDEBUG(D_DLMTRACE, "request to shrink %d %s locks from all pools\n",
748 nr, client == LDLM_NAMESPACE_CLIENT ? "client" : "server");
750 /* Find out how many resources we may release. */
751 for (nr_ns = atomic_read(ldlm_namespace_nr(client));
754 mutex_down(ldlm_namespace_lock(client));
755 if (list_empty(ldlm_namespace_list(client))) {
756 mutex_up(ldlm_namespace_lock(client));
759 ns = ldlm_namespace_first(client);
760 ldlm_namespace_get(ns);
761 ldlm_namespace_move(ns, client);
762 mutex_up(ldlm_namespace_lock(client));
763 total += ldlm_pool_shrink(&ns->ns_pool, 0, gfp_mask);
764 ldlm_namespace_put(ns, 1);
767 if (nr == 0 || total == 0)
770 /* Shrink at least ldlm_namespace_nr(client) namespaces. */
771 for (nr_ns = atomic_read(ldlm_namespace_nr(client));
774 int cancel, nr_locks;
776 /* Do not call shrink under ldlm_namespace_lock(client) */
777 mutex_down(ldlm_namespace_lock(client));
778 if (list_empty(ldlm_namespace_list(client))) {
779 mutex_up(ldlm_namespace_lock(client));
780 /* If list is empty, we can't return any @cached > 0,
781 * that probably would cause needless shrinker
786 ns = ldlm_namespace_first(client);
787 ldlm_namespace_get(ns);
788 ldlm_namespace_move(ns, client);
789 mutex_up(ldlm_namespace_lock(client));
791 nr_locks = ldlm_pool_granted(&ns->ns_pool);
792 cancel = 1 + nr_locks * nr / total;
793 ldlm_pool_shrink(&ns->ns_pool, cancel, gfp_mask);
794 cached += ldlm_pool_granted(&ns->ns_pool);
795 ldlm_namespace_put(ns, 1);
800 static int ldlm_pools_srv_shrink(int nr, unsigned int gfp_mask)
802 return ldlm_pools_shrink(LDLM_NAMESPACE_SERVER, nr, gfp_mask);
805 static int ldlm_pools_cli_shrink(int nr, unsigned int gfp_mask)
807 return ldlm_pools_shrink(LDLM_NAMESPACE_CLIENT, nr, gfp_mask);
810 void ldlm_pools_recalc(ldlm_side_t client)
812 __u32 nr_l = 0, nr_p = 0, l;
813 struct ldlm_namespace *ns;
816 /* No need to setup pool limit for client pools. */
817 if (client == LDLM_NAMESPACE_SERVER) {
818 /* Check all modest namespaces first. */
819 mutex_down(ldlm_namespace_lock(client));
820 list_for_each_entry(ns, ldlm_namespace_list(client),
823 if (ns->ns_appetite != LDLM_NAMESPACE_MODEST)
826 l = ldlm_pool_granted(&ns->ns_pool);
830 /* Set the modest pools limit equal to their avg granted
832 l += dru(l * LDLM_POOLS_MODEST_MARGIN, 100);
833 ldlm_pool_setup(&ns->ns_pool, l);
838 /* Make sure that modest namespaces did not eat more that 2/3
840 if (nr_l >= 2 * (LDLM_POOL_HOST_L / 3)) {
841 CWARN("\"Modest\" pools eat out 2/3 of server locks "
842 "limit (%d of %lu). This means that you have too "
843 "many clients for this amount of server RAM. "
844 "Upgrade server!\n", nr_l, LDLM_POOL_HOST_L);
848 /* The rest is given to greedy namespaces. */
849 list_for_each_entry(ns, ldlm_namespace_list(client),
852 if (!equal && ns->ns_appetite != LDLM_NAMESPACE_GREEDY)
856 /* In the case 2/3 locks are eaten out by
857 * modest pools, we re-setup equal limit
858 * for _all_ pools. */
859 l = LDLM_POOL_HOST_L /
860 atomic_read(ldlm_namespace_nr(client));
862 /* All the rest of greedy pools will have
863 * all locks in equal parts.*/
864 l = (LDLM_POOL_HOST_L - nr_l) /
865 (atomic_read(ldlm_namespace_nr(client)) -
868 ldlm_pool_setup(&ns->ns_pool, l);
870 mutex_up(ldlm_namespace_lock(client));
873 /* Recalc at least ldlm_namespace_nr(client) namespaces. */
874 for (nr = atomic_read(ldlm_namespace_nr(client)); nr > 0; nr--) {
875 /* Lock the list, get first @ns in the list, getref, move it
876 * to the tail, unlock and call pool recalc. This way we avoid
877 * calling recalc under @ns lock what is really good as we get
878 * rid of potential deadlock on client nodes when canceling
879 * locks synchronously. */
880 mutex_down(ldlm_namespace_lock(client));
881 if (list_empty(ldlm_namespace_list(client))) {
882 mutex_up(ldlm_namespace_lock(client));
885 ns = ldlm_namespace_first(client);
886 ldlm_namespace_get(ns);
887 ldlm_namespace_move(ns, client);
888 mutex_up(ldlm_namespace_lock(client));
890 /* After setup is done - recalc the pool. */
891 ldlm_pool_recalc(&ns->ns_pool);
892 ldlm_namespace_put(ns, 1);
895 EXPORT_SYMBOL(ldlm_pools_recalc);
897 static int ldlm_pools_thread_main(void *arg)
899 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
900 char *t_name = "ldlm_poold";
903 cfs_daemonize(t_name);
904 thread->t_flags = SVC_RUNNING;
905 cfs_waitq_signal(&thread->t_ctl_waitq);
907 CDEBUG(D_DLMTRACE, "%s: pool thread starting, process %d\n",
908 t_name, cfs_curproc_pid());
911 struct l_wait_info lwi;
913 /* Recal all pools on this tick. */
914 ldlm_pools_recalc(LDLM_NAMESPACE_SERVER);
915 ldlm_pools_recalc(LDLM_NAMESPACE_CLIENT);
917 /* Wait until the next check time, or until we're
919 lwi = LWI_TIMEOUT(cfs_time_seconds(LDLM_POOLS_THREAD_PERIOD),
921 l_wait_event(thread->t_ctl_waitq, (thread->t_flags &
922 (SVC_STOPPING|SVC_EVENT)),
925 if (thread->t_flags & SVC_STOPPING) {
926 thread->t_flags &= ~SVC_STOPPING;
928 } else if (thread->t_flags & SVC_EVENT) {
929 thread->t_flags &= ~SVC_EVENT;
933 thread->t_flags = SVC_STOPPED;
934 cfs_waitq_signal(&thread->t_ctl_waitq);
936 CDEBUG(D_DLMTRACE, "%s: pool thread exiting, process %d\n",
937 t_name, cfs_curproc_pid());
939 complete_and_exit(&ldlm_pools_comp, 0);
942 static int ldlm_pools_thread_start(void)
944 struct l_wait_info lwi = { 0 };
948 if (ldlm_pools_thread != NULL)
951 OBD_ALLOC_PTR(ldlm_pools_thread);
952 if (ldlm_pools_thread == NULL)
955 init_completion(&ldlm_pools_comp);
956 cfs_waitq_init(&ldlm_pools_thread->t_ctl_waitq);
958 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
959 * just drop the VM and FILES in ptlrpc_daemonize() right away. */
960 rc = cfs_kernel_thread(ldlm_pools_thread_main, ldlm_pools_thread,
961 CLONE_VM | CLONE_FILES);
963 CERROR("Can't start pool thread, error %d\n",
965 OBD_FREE(ldlm_pools_thread, sizeof(*ldlm_pools_thread));
966 ldlm_pools_thread = NULL;
969 l_wait_event(ldlm_pools_thread->t_ctl_waitq,
970 (ldlm_pools_thread->t_flags & SVC_RUNNING), &lwi);
974 static void ldlm_pools_thread_stop(void)
978 if (ldlm_pools_thread == NULL) {
983 ldlm_pools_thread->t_flags = SVC_STOPPING;
984 cfs_waitq_signal(&ldlm_pools_thread->t_ctl_waitq);
986 /* Make sure that pools thread is finished before freeing @thread.
987 * This fixes possible race and oops due to accessing freed memory
988 * in pools thread. */
989 wait_for_completion(&ldlm_pools_comp);
990 OBD_FREE_PTR(ldlm_pools_thread);
991 ldlm_pools_thread = NULL;
995 int ldlm_pools_init(void)
1000 rc = ldlm_pools_thread_start();
1002 ldlm_pools_srv_shrinker = set_shrinker(DEFAULT_SEEKS,
1003 ldlm_pools_srv_shrink);
1004 ldlm_pools_cli_shrinker = set_shrinker(DEFAULT_SEEKS,
1005 ldlm_pools_cli_shrink);
1009 EXPORT_SYMBOL(ldlm_pools_init);
1011 void ldlm_pools_fini(void)
1013 if (ldlm_pools_srv_shrinker != NULL) {
1014 remove_shrinker(ldlm_pools_srv_shrinker);
1015 ldlm_pools_srv_shrinker = NULL;
1017 if (ldlm_pools_cli_shrinker != NULL) {
1018 remove_shrinker(ldlm_pools_cli_shrinker);
1019 ldlm_pools_cli_shrinker = NULL;
1021 ldlm_pools_thread_stop();
1023 EXPORT_SYMBOL(ldlm_pools_fini);
1024 #endif /* __KERNEL__ */
1026 #else /* !HAVE_LRU_RESIZE_SUPPORT */
1027 int ldlm_pool_setup(struct ldlm_pool *pl, __u32 limit)
1031 EXPORT_SYMBOL(ldlm_pool_setup);
1033 int ldlm_pool_recalc(struct ldlm_pool *pl)
1037 EXPORT_SYMBOL(ldlm_pool_recalc);
1039 int ldlm_pool_shrink(struct ldlm_pool *pl,
1040 int nr, unsigned int gfp_mask)
1044 EXPORT_SYMBOL(ldlm_pool_shrink);
1046 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
1047 int idx, ldlm_side_t client)
1051 EXPORT_SYMBOL(ldlm_pool_init);
1053 void ldlm_pool_fini(struct ldlm_pool *pl)
1057 EXPORT_SYMBOL(ldlm_pool_fini);
1059 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
1063 EXPORT_SYMBOL(ldlm_pool_add);
1065 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
1069 EXPORT_SYMBOL(ldlm_pool_del);
1071 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
1075 EXPORT_SYMBOL(ldlm_pool_get_slv);
1077 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv)
1081 EXPORT_SYMBOL(ldlm_pool_set_slv);
1083 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
1087 EXPORT_SYMBOL(ldlm_pool_get_limit);
1089 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
1093 EXPORT_SYMBOL(ldlm_pool_set_limit);
1095 int ldlm_pools_init(void)
1099 EXPORT_SYMBOL(ldlm_pools_init);
1101 void ldlm_pools_fini(void)
1105 EXPORT_SYMBOL(ldlm_pools_fini);
1107 void ldlm_pools_wakeup(void)
1111 EXPORT_SYMBOL(ldlm_pools_wakeup);
1113 void ldlm_pools_recalc(ldlm_side_t client)
1117 EXPORT_SYMBOL(ldlm_pools_recalc);
1118 #endif /* HAVE_LRU_RESIZE_SUPPORT */