staging/lustre: make ldebugfs_remove recursive
[deliverable/linux.git] / drivers / staging / lustre / lustre / ldlm / ldlm_pool.c
CommitLineData
d7e09d03
PT
1/*
2 * GPL HEADER START
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
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.
9 *
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).
15 *
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 *
24 * GPL HEADER END
25 */
26/*
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 *
30 * Copyright (c) 2010, 2012, Intel Corporation.
31 */
32/*
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
35 *
36 * lustre/ldlm/ldlm_pool.c
37 *
38 * Author: Yury Umanets <umka@clusterfs.com>
39 */
40
41/*
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.
50 *
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.
56 *
e7ddc48c
AR
57 * Client has LVF, that is, lock volume factor which regulates how much
58 * sensitive client should be about last SLV from server. The higher LVF is the
59 * more locks will be canceled on client. Default value for it is 1. Setting LVF
60 * to 2 means that client will cancel locks 2 times faster.
d7e09d03
PT
61 *
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);
67 *
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.
72 *
e7ddc48c
AR
73 * General idea of this belongs to Vitaly Fertman (vitaly@clusterfs.com).
74 * Andreas Dilger (adilger@clusterfs.com) proposed few nice ideas like using
75 * LVF and many cleanups. Flow definition to allow more easy understanding of
76 * the logic belongs to Nikita Danilov (nikita@clusterfs.com) as well as many
77 * cleanups and fixes. And design and implementation are done by Yury Umanets
78 * (umka@clusterfs.com).
d7e09d03
PT
79 *
80 * Glossary for terms used:
81 *
82 * pl_limit - Number of allowed locks in pool. Applies to server and client
83 * side (tunable);
84 *
85 * pl_granted - Number of granted locks (calculated);
86 * pl_grant_rate - Number of granted locks for last T (calculated);
87 * pl_cancel_rate - Number of canceled locks for last T (calculated);
88 * pl_grant_speed - Grant speed (GR - CR) for last T (calculated);
89 * pl_grant_plan - Planned number of granted locks for next T (calculated);
90 * pl_server_lock_volume - Current server lock volume (calculated);
91 *
92 * As it may be seen from list above, we have few possible tunables which may
f2825e03 93 * affect behavior much. They all may be modified via sysfs. However, they also
d7e09d03
PT
94 * give a possibility for constructing few pre-defined behavior policies. If
95 * none of predefines is suitable for a working pattern being used, new one may
f2825e03 96 * be "constructed" via sysfs tunables.
d7e09d03
PT
97 */
98
99#define DEBUG_SUBSYSTEM S_LDLM
100
e27db149
GKH
101#include "../include/lustre_dlm.h"
102#include "../include/cl_object.h"
103#include "../include/obd_class.h"
104#include "../include/obd_support.h"
d7e09d03
PT
105#include "ldlm_internal.h"
106
107
108/*
109 * 50 ldlm locks for 1MB of RAM.
110 */
111#define LDLM_POOL_HOST_L ((NUM_CACHEPAGES >> (20 - PAGE_CACHE_SHIFT)) * 50)
112
113/*
114 * Maximal possible grant step plan in %.
115 */
116#define LDLM_POOL_MAX_GSP (30)
117
118/*
119 * Minimal possible grant step plan in %.
120 */
121#define LDLM_POOL_MIN_GSP (1)
122
123/*
124 * This controls the speed of reaching LDLM_POOL_MAX_GSP
125 * with increasing thread period.
126 */
127#define LDLM_POOL_GSP_STEP_SHIFT (2)
128
129/*
130 * LDLM_POOL_GSP% of all locks is default GP.
131 */
132#define LDLM_POOL_GP(L) (((L) * LDLM_POOL_MAX_GSP) / 100)
133
134/*
135 * Max age for locks on clients.
136 */
137#define LDLM_POOL_MAX_AGE (36000)
138
139/*
140 * The granularity of SLV calculation.
141 */
142#define LDLM_POOL_SLV_SHIFT (10)
143
d7e09d03
PT
144static inline __u64 dru(__u64 val, __u32 shift, int round_up)
145{
146 return (val + (round_up ? (1 << shift) - 1 : 0)) >> shift;
147}
148
149static inline __u64 ldlm_pool_slv_max(__u32 L)
150{
151 /*
152 * Allow to have all locks for 1 client for 10 hrs.
153 * Formula is the following: limit * 10h / 1 client.
154 */
155 __u64 lim = (__u64)L * LDLM_POOL_MAX_AGE / 1;
156 return lim;
157}
158
159static inline __u64 ldlm_pool_slv_min(__u32 L)
160{
161 return 1;
162}
163
164enum {
165 LDLM_POOL_FIRST_STAT = 0,
166 LDLM_POOL_GRANTED_STAT = LDLM_POOL_FIRST_STAT,
167 LDLM_POOL_GRANT_STAT,
168 LDLM_POOL_CANCEL_STAT,
169 LDLM_POOL_GRANT_RATE_STAT,
170 LDLM_POOL_CANCEL_RATE_STAT,
171 LDLM_POOL_GRANT_PLAN_STAT,
172 LDLM_POOL_SLV_STAT,
173 LDLM_POOL_SHRINK_REQTD_STAT,
174 LDLM_POOL_SHRINK_FREED_STAT,
175 LDLM_POOL_RECALC_STAT,
176 LDLM_POOL_TIMING_STAT,
177 LDLM_POOL_LAST_STAT
178};
179
180static inline struct ldlm_namespace *ldlm_pl2ns(struct ldlm_pool *pl)
181{
182 return container_of(pl, struct ldlm_namespace, ns_pool);
183}
184
185/**
186 * Calculates suggested grant_step in % of available locks for passed
187 * \a period. This is later used in grant_plan calculations.
188 */
189static inline int ldlm_pool_t2gsp(unsigned int t)
190{
191 /*
192 * This yields 1% grant step for anything below LDLM_POOL_GSP_STEP
193 * and up to 30% for anything higher than LDLM_POOL_GSP_STEP.
194 *
195 * How this will affect execution is the following:
196 *
197 * - for thread period 1s we will have grant_step 1% which good from
198 * pov of taking some load off from server and push it out to clients.
199 * This is like that because 1% for grant_step means that server will
200 * not allow clients to get lots of locks in short period of time and
201 * keep all old locks in their caches. Clients will always have to
202 * get some locks back if they want to take some new;
203 *
204 * - for thread period 10s (which is default) we will have 23% which
205 * means that clients will have enough of room to take some new locks
206 * without getting some back. All locks from this 23% which were not
207 * taken by clients in current period will contribute in SLV growing.
208 * SLV growing means more locks cached on clients until limit or grant
209 * plan is reached.
210 */
211 return LDLM_POOL_MAX_GSP -
212 ((LDLM_POOL_MAX_GSP - LDLM_POOL_MIN_GSP) >>
213 (t >> LDLM_POOL_GSP_STEP_SHIFT));
214}
215
216/**
217 * Recalculates next grant limit on passed \a pl.
218 *
219 * \pre ->pl_lock is locked.
220 */
221static void ldlm_pool_recalc_grant_plan(struct ldlm_pool *pl)
222{
223 int granted, grant_step, limit;
224
225 limit = ldlm_pool_get_limit(pl);
226 granted = atomic_read(&pl->pl_granted);
227
228 grant_step = ldlm_pool_t2gsp(pl->pl_recalc_period);
229 grant_step = ((limit - granted) * grant_step) / 100;
230 pl->pl_grant_plan = granted + grant_step;
231 limit = (limit * 5) >> 2;
232 if (pl->pl_grant_plan > limit)
233 pl->pl_grant_plan = limit;
234}
235
236/**
237 * Recalculates next SLV on passed \a pl.
238 *
239 * \pre ->pl_lock is locked.
240 */
241static void ldlm_pool_recalc_slv(struct ldlm_pool *pl)
242{
243 int granted;
244 int grant_plan;
245 int round_up;
246 __u64 slv;
247 __u64 slv_factor;
248 __u64 grant_usage;
249 __u32 limit;
250
251 slv = pl->pl_server_lock_volume;
252 grant_plan = pl->pl_grant_plan;
253 limit = ldlm_pool_get_limit(pl);
254 granted = atomic_read(&pl->pl_granted);
255 round_up = granted < limit;
256
257 grant_usage = max_t(int, limit - (granted - grant_plan), 1);
258
259 /*
260 * Find out SLV change factor which is the ratio of grant usage
261 * from limit. SLV changes as fast as the ratio of grant plan
262 * consumption. The more locks from grant plan are not consumed
263 * by clients in last interval (idle time), the faster grows
264 * SLV. And the opposite, the more grant plan is over-consumed
265 * (load time) the faster drops SLV.
266 */
1d06bb4e 267 slv_factor = grant_usage << LDLM_POOL_SLV_SHIFT;
d7e09d03
PT
268 do_div(slv_factor, limit);
269 slv = slv * slv_factor;
270 slv = dru(slv, LDLM_POOL_SLV_SHIFT, round_up);
271
8d2ff65d 272 if (slv > ldlm_pool_slv_max(limit))
d7e09d03 273 slv = ldlm_pool_slv_max(limit);
8d2ff65d 274 else if (slv < ldlm_pool_slv_min(limit))
d7e09d03 275 slv = ldlm_pool_slv_min(limit);
d7e09d03
PT
276
277 pl->pl_server_lock_volume = slv;
278}
279
280/**
281 * Recalculates next stats on passed \a pl.
282 *
283 * \pre ->pl_lock is locked.
284 */
285static void ldlm_pool_recalc_stats(struct ldlm_pool *pl)
286{
287 int grant_plan = pl->pl_grant_plan;
288 __u64 slv = pl->pl_server_lock_volume;
289 int granted = atomic_read(&pl->pl_granted);
290 int grant_rate = atomic_read(&pl->pl_grant_rate);
291 int cancel_rate = atomic_read(&pl->pl_cancel_rate);
292
293 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_SLV_STAT,
294 slv);
295 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
296 granted);
297 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
298 grant_rate);
299 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
300 grant_plan);
301 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
302 cancel_rate);
303}
304
305/**
306 * Sets current SLV into obd accessible via ldlm_pl2ns(pl)->ns_obd.
307 */
308static void ldlm_srv_pool_push_slv(struct ldlm_pool *pl)
309{
310 struct obd_device *obd;
311
312 /*
313 * Set new SLV in obd field for using it later without accessing the
314 * pool. This is required to avoid race between sending reply to client
315 * with new SLV and cleanup server stack in which we can't guarantee
316 * that namespace is still alive. We know only that obd is alive as
317 * long as valid export is alive.
318 */
319 obd = ldlm_pl2ns(pl)->ns_obd;
320 LASSERT(obd != NULL);
321 write_lock(&obd->obd_pool_lock);
322 obd->obd_pool_slv = pl->pl_server_lock_volume;
323 write_unlock(&obd->obd_pool_lock);
324}
325
326/**
327 * Recalculates all pool fields on passed \a pl.
328 *
329 * \pre ->pl_lock is not locked.
330 */
331static int ldlm_srv_pool_recalc(struct ldlm_pool *pl)
332{
333 time_t recalc_interval_sec;
d7e09d03 334
7264b8a5 335 recalc_interval_sec = get_seconds() - pl->pl_recalc_time;
d7e09d03 336 if (recalc_interval_sec < pl->pl_recalc_period)
0a3bdb00 337 return 0;
d7e09d03
PT
338
339 spin_lock(&pl->pl_lock);
7264b8a5 340 recalc_interval_sec = get_seconds() - pl->pl_recalc_time;
d7e09d03
PT
341 if (recalc_interval_sec < pl->pl_recalc_period) {
342 spin_unlock(&pl->pl_lock);
0a3bdb00 343 return 0;
d7e09d03
PT
344 }
345 /*
346 * Recalc SLV after last period. This should be done
347 * _before_ recalculating new grant plan.
348 */
349 ldlm_pool_recalc_slv(pl);
350
351 /*
352 * Make sure that pool informed obd of last SLV changes.
353 */
354 ldlm_srv_pool_push_slv(pl);
355
356 /*
357 * Update grant_plan for new period.
358 */
359 ldlm_pool_recalc_grant_plan(pl);
360
7264b8a5 361 pl->pl_recalc_time = get_seconds();
d7e09d03
PT
362 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
363 recalc_interval_sec);
364 spin_unlock(&pl->pl_lock);
0a3bdb00 365 return 0;
d7e09d03
PT
366}
367
368/**
369 * This function is used on server side as main entry point for memory
370 * pressure handling. It decreases SLV on \a pl according to passed
371 * \a nr and \a gfp_mask.
372 *
373 * Our goal here is to decrease SLV such a way that clients hold \a nr
374 * locks smaller in next 10h.
375 */
376static int ldlm_srv_pool_shrink(struct ldlm_pool *pl,
5802572e 377 int nr, gfp_t gfp_mask)
d7e09d03
PT
378{
379 __u32 limit;
380
381 /*
382 * VM is asking how many entries may be potentially freed.
383 */
384 if (nr == 0)
385 return atomic_read(&pl->pl_granted);
386
387 /*
388 * Client already canceled locks but server is already in shrinker
389 * and can't cancel anything. Let's catch this race.
390 */
391 if (atomic_read(&pl->pl_granted) == 0)
0a3bdb00 392 return 0;
d7e09d03
PT
393
394 spin_lock(&pl->pl_lock);
395
396 /*
397 * We want shrinker to possibly cause cancellation of @nr locks from
398 * clients or grant approximately @nr locks smaller next intervals.
399 *
400 * This is why we decreased SLV by @nr. This effect will only be as
401 * long as one re-calc interval (1s these days) and this should be
402 * enough to pass this decreased SLV to all clients. On next recalc
403 * interval pool will either increase SLV if locks load is not high
404 * or will keep on same level or even decrease again, thus, shrinker
405 * decreased SLV will affect next recalc intervals and this way will
406 * make locking load lower.
407 */
408 if (nr < pl->pl_server_lock_volume) {
409 pl->pl_server_lock_volume = pl->pl_server_lock_volume - nr;
410 } else {
411 limit = ldlm_pool_get_limit(pl);
412 pl->pl_server_lock_volume = ldlm_pool_slv_min(limit);
413 }
414
415 /*
416 * Make sure that pool informed obd of last SLV changes.
417 */
418 ldlm_srv_pool_push_slv(pl);
419 spin_unlock(&pl->pl_lock);
420
421 /*
422 * We did not really free any memory here so far, it only will be
423 * freed later may be, so that we return 0 to not confuse VM.
424 */
425 return 0;
426}
427
428/**
429 * Setup server side pool \a pl with passed \a limit.
430 */
431static int ldlm_srv_pool_setup(struct ldlm_pool *pl, int limit)
432{
433 struct obd_device *obd;
434
435 obd = ldlm_pl2ns(pl)->ns_obd;
436 LASSERT(obd != NULL && obd != LP_POISON);
437 LASSERT(obd->obd_type != LP_POISON);
438 write_lock(&obd->obd_pool_lock);
439 obd->obd_pool_limit = limit;
440 write_unlock(&obd->obd_pool_lock);
441
442 ldlm_pool_set_limit(pl, limit);
443 return 0;
444}
445
446/**
447 * Sets SLV and Limit from ldlm_pl2ns(pl)->ns_obd tp passed \a pl.
448 */
449static void ldlm_cli_pool_pop_slv(struct ldlm_pool *pl)
450{
451 struct obd_device *obd;
452
453 /*
454 * Get new SLV and Limit from obd which is updated with coming
455 * RPCs.
456 */
457 obd = ldlm_pl2ns(pl)->ns_obd;
458 LASSERT(obd != NULL);
459 read_lock(&obd->obd_pool_lock);
460 pl->pl_server_lock_volume = obd->obd_pool_slv;
461 ldlm_pool_set_limit(pl, obd->obd_pool_limit);
462 read_unlock(&obd->obd_pool_lock);
463}
464
465/**
466 * Recalculates client size pool \a pl according to current SLV and Limit.
467 */
468static int ldlm_cli_pool_recalc(struct ldlm_pool *pl)
469{
470 time_t recalc_interval_sec;
4d2c7b30 471 int ret;
d7e09d03 472
7264b8a5 473 recalc_interval_sec = get_seconds() - pl->pl_recalc_time;
d7e09d03 474 if (recalc_interval_sec < pl->pl_recalc_period)
0a3bdb00 475 return 0;
d7e09d03
PT
476
477 spin_lock(&pl->pl_lock);
478 /*
479 * Check if we need to recalc lists now.
480 */
7264b8a5 481 recalc_interval_sec = get_seconds() - pl->pl_recalc_time;
d7e09d03
PT
482 if (recalc_interval_sec < pl->pl_recalc_period) {
483 spin_unlock(&pl->pl_lock);
0a3bdb00 484 return 0;
d7e09d03
PT
485 }
486
487 /*
488 * Make sure that pool knows last SLV and Limit from obd.
489 */
490 ldlm_cli_pool_pop_slv(pl);
491
d7e09d03
PT
492 spin_unlock(&pl->pl_lock);
493
494 /*
495 * Do not cancel locks in case lru resize is disabled for this ns.
496 */
4d2c7b30
LX
497 if (!ns_connect_lru_resize(ldlm_pl2ns(pl))) {
498 ret = 0;
499 goto out;
500 }
d7e09d03
PT
501
502 /*
503 * In the time of canceling locks on client we do not need to maintain
504 * sharp timing, we only want to cancel locks asap according to new SLV.
505 * It may be called when SLV has changed much, this is why we do not
506 * take into account pl->pl_recalc_time here.
507 */
4d2c7b30
LX
508 ret = ldlm_cancel_lru(ldlm_pl2ns(pl), 0, LCF_ASYNC, LDLM_CANCEL_LRUR);
509
510out:
511 spin_lock(&pl->pl_lock);
512 /*
513 * Time of LRU resizing might be longer than period,
514 * so update after LRU resizing rather than before it.
515 */
516 pl->pl_recalc_time = get_seconds();
517 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
518 recalc_interval_sec);
519 spin_unlock(&pl->pl_lock);
520 return ret;
d7e09d03
PT
521}
522
523/**
524 * This function is main entry point for memory pressure handling on client
525 * side. Main goal of this function is to cancel some number of locks on
526 * passed \a pl according to \a nr and \a gfp_mask.
527 */
528static int ldlm_cli_pool_shrink(struct ldlm_pool *pl,
5802572e 529 int nr, gfp_t gfp_mask)
d7e09d03
PT
530{
531 struct ldlm_namespace *ns;
cbc3769e 532 int unused;
d7e09d03
PT
533
534 ns = ldlm_pl2ns(pl);
535
536 /*
537 * Do not cancel locks in case lru resize is disabled for this ns.
538 */
539 if (!ns_connect_lru_resize(ns))
0a3bdb00 540 return 0;
d7e09d03
PT
541
542 /*
543 * Make sure that pool knows last SLV and Limit from obd.
544 */
545 ldlm_cli_pool_pop_slv(pl);
546
547 spin_lock(&ns->ns_lock);
548 unused = ns->ns_nr_unused;
549 spin_unlock(&ns->ns_lock);
550
cbc3769e
PT
551 if (nr == 0)
552 return (unused / 100) * sysctl_vfs_cache_pressure;
553 else
554 return ldlm_cancel_lru(ns, nr, LCF_ASYNC, LDLM_CANCEL_SHRINK);
d7e09d03
PT
555}
556
b9c98cfa 557static const struct ldlm_pool_ops ldlm_srv_pool_ops = {
d7e09d03
PT
558 .po_recalc = ldlm_srv_pool_recalc,
559 .po_shrink = ldlm_srv_pool_shrink,
560 .po_setup = ldlm_srv_pool_setup
561};
562
b9c98cfa 563static const struct ldlm_pool_ops ldlm_cli_pool_ops = {
d7e09d03
PT
564 .po_recalc = ldlm_cli_pool_recalc,
565 .po_shrink = ldlm_cli_pool_shrink
566};
567
568/**
569 * Pool recalc wrapper. Will call either client or server pool recalc callback
570 * depending what pool \a pl is used.
571 */
572int ldlm_pool_recalc(struct ldlm_pool *pl)
573{
574 time_t recalc_interval_sec;
575 int count;
576
7264b8a5 577 recalc_interval_sec = get_seconds() - pl->pl_recalc_time;
d7e09d03
PT
578 if (recalc_interval_sec <= 0)
579 goto recalc;
580
581 spin_lock(&pl->pl_lock);
d7e09d03
PT
582 if (recalc_interval_sec > 0) {
583 /*
584 * Update pool statistics every 1s.
585 */
586 ldlm_pool_recalc_stats(pl);
587
588 /*
589 * Zero out all rates and speed for the last period.
590 */
591 atomic_set(&pl->pl_grant_rate, 0);
592 atomic_set(&pl->pl_cancel_rate, 0);
593 }
594 spin_unlock(&pl->pl_lock);
595
596 recalc:
597 if (pl->pl_ops->po_recalc != NULL) {
598 count = pl->pl_ops->po_recalc(pl);
599 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_RECALC_STAT,
600 count);
d7e09d03 601 }
7264b8a5 602 recalc_interval_sec = pl->pl_recalc_time - get_seconds() +
3eface59 603 pl->pl_recalc_period;
4d2c7b30
LX
604 if (recalc_interval_sec <= 0) {
605 /* Prevent too frequent recalculation. */
606 CDEBUG(D_DLMTRACE, "Negative interval(%ld), "
607 "too short period(%ld)",
608 recalc_interval_sec,
609 pl->pl_recalc_period);
610 recalc_interval_sec = 1;
611 }
d7e09d03 612
3eface59 613 return recalc_interval_sec;
d7e09d03 614}
d7e09d03 615
cbc3769e 616/*
d7e09d03 617 * Pool shrink wrapper. Will call either client or server pool recalc callback
cbc3769e
PT
618 * depending what pool pl is used. When nr == 0, just return the number of
619 * freeable locks. Otherwise, return the number of canceled locks.
d7e09d03
PT
620 */
621int ldlm_pool_shrink(struct ldlm_pool *pl, int nr,
5802572e 622 gfp_t gfp_mask)
d7e09d03
PT
623{
624 int cancel = 0;
625
626 if (pl->pl_ops->po_shrink != NULL) {
627 cancel = pl->pl_ops->po_shrink(pl, nr, gfp_mask);
628 if (nr > 0) {
629 lprocfs_counter_add(pl->pl_stats,
630 LDLM_POOL_SHRINK_REQTD_STAT,
631 nr);
632 lprocfs_counter_add(pl->pl_stats,
633 LDLM_POOL_SHRINK_FREED_STAT,
634 cancel);
2d00bd17
JP
635 CDEBUG(D_DLMTRACE, "%s: request to shrink %d locks, shrunk %d\n",
636 pl->pl_name, nr, cancel);
d7e09d03
PT
637 }
638 }
639 return cancel;
640}
641EXPORT_SYMBOL(ldlm_pool_shrink);
642
643/**
644 * Pool setup wrapper. Will call either client or server pool recalc callback
645 * depending what pool \a pl is used.
646 *
647 * Sets passed \a limit into pool \a pl.
648 */
649int ldlm_pool_setup(struct ldlm_pool *pl, int limit)
650{
651 if (pl->pl_ops->po_setup != NULL)
e8291974 652 return pl->pl_ops->po_setup(pl, limit);
d7e09d03
PT
653 return 0;
654}
655EXPORT_SYMBOL(ldlm_pool_setup);
656
73bb1da6 657static int lprocfs_pool_state_seq_show(struct seq_file *m, void *unused)
d7e09d03
PT
658{
659 int granted, grant_rate, cancel_rate, grant_step;
73bb1da6
PT
660 int grant_speed, grant_plan, lvf;
661 struct ldlm_pool *pl = m->private;
d7e09d03
PT
662 __u64 slv, clv;
663 __u32 limit;
664
665 spin_lock(&pl->pl_lock);
666 slv = pl->pl_server_lock_volume;
667 clv = pl->pl_client_lock_volume;
668 limit = ldlm_pool_get_limit(pl);
669 grant_plan = pl->pl_grant_plan;
670 granted = atomic_read(&pl->pl_granted);
671 grant_rate = atomic_read(&pl->pl_grant_rate);
672 cancel_rate = atomic_read(&pl->pl_cancel_rate);
673 grant_speed = grant_rate - cancel_rate;
674 lvf = atomic_read(&pl->pl_lock_volume_factor);
675 grant_step = ldlm_pool_t2gsp(pl->pl_recalc_period);
676 spin_unlock(&pl->pl_lock);
677
73bb1da6 678 seq_printf(m, "LDLM pool state (%s):\n"
b0f5aad5
GKH
679 " SLV: %llu\n"
680 " CLV: %llu\n"
73bb1da6
PT
681 " LVF: %d\n",
682 pl->pl_name, slv, clv, lvf);
d7e09d03
PT
683
684 if (ns_is_server(ldlm_pl2ns(pl))) {
73bb1da6
PT
685 seq_printf(m, " GSP: %d%%\n"
686 " GP: %d\n",
687 grant_step, grant_plan);
d7e09d03 688 }
2c2b7c05
HM
689 seq_printf(m, " GR: %d\n CR: %d\n GS: %d\n"
690 " G: %d\n L: %d\n",
73bb1da6
PT
691 grant_rate, cancel_rate, grant_speed,
692 granted, limit);
693
694 return 0;
d7e09d03 695}
73bb1da6 696LPROC_SEQ_FOPS_RO(lprocfs_pool_state);
d7e09d03 697
24b8c88a
OD
698static ssize_t grant_speed_show(struct kobject *kobj, struct attribute *attr,
699 char *buf)
d7e09d03 700{
24b8c88a
OD
701 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,
702 pl_kobj);
703
d7e09d03
PT
704 int grant_speed;
705
706 spin_lock(&pl->pl_lock);
707 /* serialize with ldlm_pool_recalc */
708 grant_speed = atomic_read(&pl->pl_grant_rate) -
709 atomic_read(&pl->pl_cancel_rate);
710 spin_unlock(&pl->pl_lock);
24b8c88a 711 return sprintf(buf, "%d\n", grant_speed);
d7e09d03 712}
24b8c88a 713LUSTRE_RO_ATTR(grant_speed);
d7e09d03 714
24b8c88a
OD
715LDLM_POOL_SYSFS_READER_SHOW(grant_plan, int);
716LUSTRE_RO_ATTR(grant_plan);
73bb1da6 717
24b8c88a
OD
718LDLM_POOL_SYSFS_READER_SHOW(recalc_period, int);
719LDLM_POOL_SYSFS_WRITER_STORE(recalc_period, int);
720LUSTRE_RW_ATTR(recalc_period);
73bb1da6 721
24b8c88a
OD
722LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(server_lock_volume, u64);
723LUSTRE_RO_ATTR(server_lock_volume);
724
725LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(limit, atomic);
726LDLM_POOL_SYSFS_WRITER_NOLOCK_STORE(limit, atomic);
727LUSTRE_RW_ATTR(limit);
728
729LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(granted, atomic);
730LUSTRE_RO_ATTR(granted);
731
732LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(cancel_rate, atomic);
733LUSTRE_RO_ATTR(cancel_rate);
73bb1da6 734
24b8c88a
OD
735LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(grant_rate, atomic);
736LUSTRE_RO_ATTR(grant_rate);
73bb1da6 737
24b8c88a
OD
738LDLM_POOL_SYSFS_READER_NOLOCK_SHOW(lock_volume_factor, atomic);
739LDLM_POOL_SYSFS_WRITER_NOLOCK_STORE(lock_volume_factor, atomic);
740LUSTRE_RW_ATTR(lock_volume_factor);
73bb1da6
PT
741
742#define LDLM_POOL_ADD_VAR(name, var, ops) \
743 do { \
744 snprintf(var_name, MAX_STRING_SIZE, #name); \
745 pool_vars[0].data = var; \
746 pool_vars[0].fops = ops; \
700815d4 747 ldebugfs_add_vars(pl->pl_debugfs_entry, pool_vars, NULL);\
73bb1da6 748 } while (0)
d7e09d03 749
f2825e03
OD
750/* These are for pools in /sys/fs/lustre/ldlm/namespaces/.../pool */
751static struct attribute *ldlm_pl_attrs[] = {
24b8c88a
OD
752 &lustre_attr_grant_speed.attr,
753 &lustre_attr_grant_plan.attr,
754 &lustre_attr_recalc_period.attr,
755 &lustre_attr_server_lock_volume.attr,
756 &lustre_attr_limit.attr,
757 &lustre_attr_granted.attr,
758 &lustre_attr_cancel_rate.attr,
759 &lustre_attr_grant_rate.attr,
760 &lustre_attr_lock_volume_factor.attr,
f2825e03
OD
761 NULL,
762};
763
764static void ldlm_pl_release(struct kobject *kobj)
765{
766 struct ldlm_pool *pl = container_of(kobj, struct ldlm_pool,
767 pl_kobj);
768 complete(&pl->pl_kobj_unregister);
769}
770
771static struct kobj_type ldlm_pl_ktype = {
772 .default_attrs = ldlm_pl_attrs,
773 .sysfs_ops = &lustre_sysfs_ops,
774 .release = ldlm_pl_release,
775};
776
777static int ldlm_pool_sysfs_init(struct ldlm_pool *pl)
778{
779 struct ldlm_namespace *ns = ldlm_pl2ns(pl);
780 int err;
781
782 init_completion(&pl->pl_kobj_unregister);
783 err = kobject_init_and_add(&pl->pl_kobj, &ldlm_pl_ktype, &ns->ns_kobj,
784 "pool");
785
786 return err;
787}
788
700815d4 789static int ldlm_pool_debugfs_init(struct ldlm_pool *pl)
d7e09d03
PT
790{
791 struct ldlm_namespace *ns = ldlm_pl2ns(pl);
700815d4 792 struct dentry *debugfs_ns_parent;
d7e09d03
PT
793 struct lprocfs_vars pool_vars[2];
794 char *var_name = NULL;
795 int rc = 0;
d7e09d03 796
352f7891 797 var_name = kzalloc(MAX_STRING_SIZE + 1, GFP_NOFS);
d7e09d03 798 if (!var_name)
0a3bdb00 799 return -ENOMEM;
d7e09d03 800
700815d4
DE
801 debugfs_ns_parent = ns->ns_debugfs_entry;
802 if (IS_ERR_OR_NULL(debugfs_ns_parent)) {
803 CERROR("%s: debugfs entry is not initialized\n",
d7e09d03 804 ldlm_ns_name(ns));
d1c0d446
JL
805 rc = -EINVAL;
806 goto out_free_name;
d7e09d03 807 }
700815d4
DE
808 pl->pl_debugfs_entry = ldebugfs_register("pool", debugfs_ns_parent,
809 NULL, NULL);
810 if (IS_ERR(pl->pl_debugfs_entry)) {
811 CERROR("LdebugFS failed in ldlm-pool-init\n");
812 rc = PTR_ERR(pl->pl_debugfs_entry);
813 pl->pl_debugfs_entry = NULL;
d1c0d446 814 goto out_free_name;
d7e09d03
PT
815 }
816
817 var_name[MAX_STRING_SIZE] = '\0';
818 memset(pool_vars, 0, sizeof(pool_vars));
819 pool_vars[0].name = var_name;
820
700815d4 821 LDLM_POOL_ADD_VAR(state, pl, &lprocfs_pool_state_fops);
d7e09d03
PT
822
823 pl->pl_stats = lprocfs_alloc_stats(LDLM_POOL_LAST_STAT -
824 LDLM_POOL_FIRST_STAT, 0);
d1c0d446
JL
825 if (!pl->pl_stats) {
826 rc = -ENOMEM;
827 goto out_free_name;
828 }
d7e09d03
PT
829
830 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
831 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
832 "granted", "locks");
833 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_STAT,
834 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
835 "grant", "locks");
836 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_STAT,
837 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
838 "cancel", "locks");
839 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
840 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
841 "grant_rate", "locks/s");
842 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
843 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
844 "cancel_rate", "locks/s");
845 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
846 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
847 "grant_plan", "locks/s");
848 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SLV_STAT,
849 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
850 "slv", "slv");
851 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_REQTD_STAT,
852 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
853 "shrink_request", "locks");
854 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_FREED_STAT,
855 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
856 "shrink_freed", "locks");
857 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_RECALC_STAT,
858 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
859 "recalc_freed", "locks");
860 lprocfs_counter_init(pl->pl_stats, LDLM_POOL_TIMING_STAT,
861 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
862 "recalc_timing", "sec");
700815d4
DE
863 rc = ldebugfs_register_stats(pl->pl_debugfs_entry, "stats",
864 pl->pl_stats);
d7e09d03 865
d7e09d03 866out_free_name:
352f7891 867 kfree(var_name);
d7e09d03
PT
868 return rc;
869}
870
f2825e03
OD
871static void ldlm_pool_sysfs_fini(struct ldlm_pool *pl)
872{
873 kobject_put(&pl->pl_kobj);
874 wait_for_completion(&pl->pl_kobj_unregister);
875}
876
700815d4 877static void ldlm_pool_debugfs_fini(struct ldlm_pool *pl)
d7e09d03
PT
878{
879 if (pl->pl_stats != NULL) {
880 lprocfs_free_stats(&pl->pl_stats);
881 pl->pl_stats = NULL;
882 }
700815d4
DE
883 if (pl->pl_debugfs_entry != NULL) {
884 ldebugfs_remove(&pl->pl_debugfs_entry);
885 pl->pl_debugfs_entry = NULL;
d7e09d03
PT
886 }
887}
888
889int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
890 int idx, ldlm_side_t client)
891{
892 int rc;
d7e09d03
PT
893
894 spin_lock_init(&pl->pl_lock);
895 atomic_set(&pl->pl_granted, 0);
7264b8a5 896 pl->pl_recalc_time = get_seconds();
d7e09d03
PT
897 atomic_set(&pl->pl_lock_volume_factor, 1);
898
899 atomic_set(&pl->pl_grant_rate, 0);
900 atomic_set(&pl->pl_cancel_rate, 0);
901 pl->pl_grant_plan = LDLM_POOL_GP(LDLM_POOL_HOST_L);
902
903 snprintf(pl->pl_name, sizeof(pl->pl_name), "ldlm-pool-%s-%d",
904 ldlm_ns_name(ns), idx);
905
906 if (client == LDLM_NAMESPACE_SERVER) {
907 pl->pl_ops = &ldlm_srv_pool_ops;
908 ldlm_pool_set_limit(pl, LDLM_POOL_HOST_L);
909 pl->pl_recalc_period = LDLM_POOL_SRV_DEF_RECALC_PERIOD;
910 pl->pl_server_lock_volume = ldlm_pool_slv_max(LDLM_POOL_HOST_L);
911 } else {
912 ldlm_pool_set_limit(pl, 1);
913 pl->pl_server_lock_volume = 0;
914 pl->pl_ops = &ldlm_cli_pool_ops;
915 pl->pl_recalc_period = LDLM_POOL_CLI_DEF_RECALC_PERIOD;
916 }
917 pl->pl_client_lock_volume = 0;
700815d4 918 rc = ldlm_pool_debugfs_init(pl);
d7e09d03 919 if (rc)
0a3bdb00 920 return rc;
d7e09d03 921
f2825e03
OD
922 rc = ldlm_pool_sysfs_init(pl);
923 if (rc)
924 return rc;
925
d7e09d03
PT
926 CDEBUG(D_DLMTRACE, "Lock pool %s is initialized\n", pl->pl_name);
927
0a3bdb00 928 return rc;
d7e09d03
PT
929}
930EXPORT_SYMBOL(ldlm_pool_init);
931
932void ldlm_pool_fini(struct ldlm_pool *pl)
933{
f2825e03 934 ldlm_pool_sysfs_fini(pl);
700815d4 935 ldlm_pool_debugfs_fini(pl);
d7e09d03
PT
936
937 /*
938 * Pool should not be used after this point. We can't free it here as
939 * it lives in struct ldlm_namespace, but still interested in catching
940 * any abnormal using cases.
941 */
942 POISON(pl, 0x5a, sizeof(*pl));
d7e09d03
PT
943}
944EXPORT_SYMBOL(ldlm_pool_fini);
945
946/**
947 * Add new taken ldlm lock \a lock into pool \a pl accounting.
948 */
949void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
950{
951 /*
952 * FLOCK locks are special in a sense that they are almost never
953 * cancelled, instead special kind of lock is used to drop them.
954 * also there is no LRU for flock locks, so no point in tracking
955 * them anyway.
956 */
957 if (lock->l_resource->lr_type == LDLM_FLOCK)
958 return;
959
960 atomic_inc(&pl->pl_granted);
961 atomic_inc(&pl->pl_grant_rate);
962 lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_GRANT_STAT);
963 /*
964 * Do not do pool recalc for client side as all locks which
965 * potentially may be canceled has already been packed into
966 * enqueue/cancel rpc. Also we do not want to run out of stack
967 * with too long call paths.
968 */
969 if (ns_is_server(ldlm_pl2ns(pl)))
970 ldlm_pool_recalc(pl);
971}
972EXPORT_SYMBOL(ldlm_pool_add);
973
974/**
975 * Remove ldlm lock \a lock from pool \a pl accounting.
976 */
977void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
978{
979 /*
980 * Filter out FLOCK locks. Read above comment in ldlm_pool_add().
981 */
982 if (lock->l_resource->lr_type == LDLM_FLOCK)
983 return;
984
985 LASSERT(atomic_read(&pl->pl_granted) > 0);
986 atomic_dec(&pl->pl_granted);
987 atomic_inc(&pl->pl_cancel_rate);
988
989 lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_CANCEL_STAT);
990
991 if (ns_is_server(ldlm_pl2ns(pl)))
992 ldlm_pool_recalc(pl);
993}
994EXPORT_SYMBOL(ldlm_pool_del);
995
996/**
997 * Returns current \a pl SLV.
998 *
999 * \pre ->pl_lock is not locked.
1000 */
1001__u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
1002{
1003 __u64 slv;
902f3bb1 1004
d7e09d03
PT
1005 spin_lock(&pl->pl_lock);
1006 slv = pl->pl_server_lock_volume;
1007 spin_unlock(&pl->pl_lock);
1008 return slv;
1009}
1010EXPORT_SYMBOL(ldlm_pool_get_slv);
1011
1012/**
1013 * Sets passed \a slv to \a pl.
1014 *
1015 * \pre ->pl_lock is not locked.
1016 */
1017void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv)
1018{
1019 spin_lock(&pl->pl_lock);
1020 pl->pl_server_lock_volume = slv;
1021 spin_unlock(&pl->pl_lock);
1022}
1023EXPORT_SYMBOL(ldlm_pool_set_slv);
1024
1025/**
1026 * Returns current \a pl CLV.
1027 *
1028 * \pre ->pl_lock is not locked.
1029 */
1030__u64 ldlm_pool_get_clv(struct ldlm_pool *pl)
1031{
1032 __u64 slv;
902f3bb1 1033
d7e09d03
PT
1034 spin_lock(&pl->pl_lock);
1035 slv = pl->pl_client_lock_volume;
1036 spin_unlock(&pl->pl_lock);
1037 return slv;
1038}
1039EXPORT_SYMBOL(ldlm_pool_get_clv);
1040
1041/**
1042 * Sets passed \a clv to \a pl.
1043 *
1044 * \pre ->pl_lock is not locked.
1045 */
1046void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv)
1047{
1048 spin_lock(&pl->pl_lock);
1049 pl->pl_client_lock_volume = clv;
1050 spin_unlock(&pl->pl_lock);
1051}
1052EXPORT_SYMBOL(ldlm_pool_set_clv);
1053
1054/**
1055 * Returns current \a pl limit.
1056 */
1057__u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
1058{
1059 return atomic_read(&pl->pl_limit);
1060}
1061EXPORT_SYMBOL(ldlm_pool_get_limit);
1062
1063/**
1064 * Sets passed \a limit to \a pl.
1065 */
1066void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
1067{
1068 atomic_set(&pl->pl_limit, limit);
1069}
1070EXPORT_SYMBOL(ldlm_pool_set_limit);
1071
1072/**
1073 * Returns current LVF from \a pl.
1074 */
1075__u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
1076{
1077 return atomic_read(&pl->pl_lock_volume_factor);
1078}
1079EXPORT_SYMBOL(ldlm_pool_get_lvf);
1080
1081static int ldlm_pool_granted(struct ldlm_pool *pl)
1082{
1083 return atomic_read(&pl->pl_granted);
1084}
1085
1086static struct ptlrpc_thread *ldlm_pools_thread;
d7e09d03
PT
1087static struct completion ldlm_pools_comp;
1088
1089/*
cbc3769e
PT
1090 * count locks from all namespaces (if possible). Returns number of
1091 * cached locks.
d7e09d03 1092 */
5802572e 1093static unsigned long ldlm_pools_count(ldlm_side_t client, gfp_t gfp_mask)
d7e09d03 1094{
cbc3769e 1095 int total = 0, nr_ns;
d7e09d03 1096 struct ldlm_namespace *ns;
91a50030 1097 struct ldlm_namespace *ns_old = NULL; /* loop detection */
d7e09d03
PT
1098 void *cookie;
1099
cbc3769e
PT
1100 if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
1101 return 0;
d7e09d03 1102
cbc3769e
PT
1103 CDEBUG(D_DLMTRACE, "Request to count %s locks from all pools\n",
1104 client == LDLM_NAMESPACE_CLIENT ? "client" : "server");
d7e09d03
PT
1105
1106 cookie = cl_env_reenter();
1107
1108 /*
1109 * Find out how many resources we may release.
1110 */
91a50030 1111 for (nr_ns = ldlm_namespace_nr_read(client);
cbc3769e 1112 nr_ns > 0; nr_ns--) {
d7e09d03
PT
1113 mutex_lock(ldlm_namespace_lock(client));
1114 if (list_empty(ldlm_namespace_list(client))) {
1115 mutex_unlock(ldlm_namespace_lock(client));
1116 cl_env_reexit(cookie);
1117 return 0;
1118 }
1119 ns = ldlm_namespace_first_locked(client);
91a50030
OD
1120
1121 if (ns == ns_old) {
1122 mutex_unlock(ldlm_namespace_lock(client));
1123 break;
1124 }
1125
1126 if (ldlm_ns_empty(ns)) {
1127 ldlm_namespace_move_to_inactive_locked(ns, client);
1128 mutex_unlock(ldlm_namespace_lock(client));
1129 continue;
1130 }
1131
1132 if (ns_old == NULL)
1133 ns_old = ns;
1134
d7e09d03 1135 ldlm_namespace_get(ns);
91a50030 1136 ldlm_namespace_move_to_active_locked(ns, client);
d7e09d03
PT
1137 mutex_unlock(ldlm_namespace_lock(client));
1138 total += ldlm_pool_shrink(&ns->ns_pool, 0, gfp_mask);
1139 ldlm_namespace_put(ns);
1140 }
1141
cbc3769e
PT
1142 cl_env_reexit(cookie);
1143 return total;
1144}
1145
5802572e 1146static unsigned long ldlm_pools_scan(ldlm_side_t client, int nr, gfp_t gfp_mask)
cbc3769e
PT
1147{
1148 unsigned long freed = 0;
1149 int tmp, nr_ns;
1150 struct ldlm_namespace *ns;
1151 void *cookie;
1152
1153 if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
1154 return -1;
1155
1156 cookie = cl_env_reenter();
d7e09d03
PT
1157
1158 /*
cbc3769e 1159 * Shrink at least ldlm_namespace_nr_read(client) namespaces.
d7e09d03 1160 */
cbc3769e
PT
1161 for (tmp = nr_ns = ldlm_namespace_nr_read(client);
1162 tmp > 0; tmp--) {
d7e09d03
PT
1163 int cancel, nr_locks;
1164
1165 /*
1166 * Do not call shrink under ldlm_namespace_lock(client)
1167 */
1168 mutex_lock(ldlm_namespace_lock(client));
1169 if (list_empty(ldlm_namespace_list(client))) {
1170 mutex_unlock(ldlm_namespace_lock(client));
d7e09d03
PT
1171 break;
1172 }
1173 ns = ldlm_namespace_first_locked(client);
1174 ldlm_namespace_get(ns);
91a50030 1175 ldlm_namespace_move_to_active_locked(ns, client);
d7e09d03
PT
1176 mutex_unlock(ldlm_namespace_lock(client));
1177
1178 nr_locks = ldlm_pool_granted(&ns->ns_pool);
cbc3769e
PT
1179 /*
1180 * We use to shrink propotionally but with new shrinker API,
1181 * we lost the total number of freeable locks.
1182 */
1183 cancel = 1 + min_t(int, nr_locks, nr / nr_ns);
1184 freed += ldlm_pool_shrink(&ns->ns_pool, cancel, gfp_mask);
d7e09d03
PT
1185 ldlm_namespace_put(ns);
1186 }
1187 cl_env_reexit(cookie);
cbc3769e
PT
1188 /*
1189 * we only decrease the SLV in server pools shrinker, return
1190 * SHRINK_STOP to kernel to avoid needless loop. LU-1128
1191 */
1192 return (client == LDLM_NAMESPACE_SERVER) ? SHRINK_STOP : freed;
1193}
1194
e7ddc48c
AR
1195static unsigned long ldlm_pools_srv_count(struct shrinker *s,
1196 struct shrink_control *sc)
cbc3769e
PT
1197{
1198 return ldlm_pools_count(LDLM_NAMESPACE_SERVER, sc->gfp_mask);
d7e09d03
PT
1199}
1200
e7ddc48c
AR
1201static unsigned long ldlm_pools_srv_scan(struct shrinker *s,
1202 struct shrink_control *sc)
d7e09d03 1203{
cbc3769e
PT
1204 return ldlm_pools_scan(LDLM_NAMESPACE_SERVER, sc->nr_to_scan,
1205 sc->gfp_mask);
d7e09d03
PT
1206}
1207
e7ddc48c
AR
1208static unsigned long ldlm_pools_cli_count(struct shrinker *s,
1209 struct shrink_control *sc)
d7e09d03 1210{
cbc3769e
PT
1211 return ldlm_pools_count(LDLM_NAMESPACE_CLIENT, sc->gfp_mask);
1212}
1213
e7ddc48c
AR
1214static unsigned long ldlm_pools_cli_scan(struct shrinker *s,
1215 struct shrink_control *sc)
cbc3769e
PT
1216{
1217 return ldlm_pools_scan(LDLM_NAMESPACE_CLIENT, sc->nr_to_scan,
1218 sc->gfp_mask);
d7e09d03
PT
1219}
1220
3eface59 1221int ldlm_pools_recalc(ldlm_side_t client)
d7e09d03
PT
1222{
1223 __u32 nr_l = 0, nr_p = 0, l;
1224 struct ldlm_namespace *ns;
91a50030 1225 struct ldlm_namespace *ns_old = NULL;
d7e09d03 1226 int nr, equal = 0;
3eface59 1227 int time = 50; /* seconds of sleep if no active namespaces */
d7e09d03
PT
1228
1229 /*
1230 * No need to setup pool limit for client pools.
1231 */
1232 if (client == LDLM_NAMESPACE_SERVER) {
1233 /*
1234 * Check all modest namespaces first.
1235 */
1236 mutex_lock(ldlm_namespace_lock(client));
1237 list_for_each_entry(ns, ldlm_namespace_list(client),
9d0b2b7a 1238 ns_list_chain) {
d7e09d03
PT
1239 if (ns->ns_appetite != LDLM_NAMESPACE_MODEST)
1240 continue;
1241
1242 l = ldlm_pool_granted(&ns->ns_pool);
1243 if (l == 0)
1244 l = 1;
1245
1246 /*
1247 * Set the modest pools limit equal to their avg granted
1248 * locks + ~6%.
1249 */
1250 l += dru(l, LDLM_POOLS_MODEST_MARGIN_SHIFT, 0);
1251 ldlm_pool_setup(&ns->ns_pool, l);
1252 nr_l += l;
1253 nr_p++;
1254 }
1255
1256 /*
1257 * Make sure that modest namespaces did not eat more that 2/3
1258 * of limit.
1259 */
1260 if (nr_l >= 2 * (LDLM_POOL_HOST_L / 3)) {
2d00bd17
JP
1261 CWARN("\"Modest\" pools eat out 2/3 of server locks limit (%d of %lu). This means that you have too many clients for this amount of server RAM. Upgrade server!\n",
1262 nr_l, LDLM_POOL_HOST_L);
d7e09d03
PT
1263 equal = 1;
1264 }
1265
1266 /*
1267 * The rest is given to greedy namespaces.
1268 */
1269 list_for_each_entry(ns, ldlm_namespace_list(client),
e9e2fa69 1270 ns_list_chain) {
d7e09d03
PT
1271 if (!equal && ns->ns_appetite != LDLM_NAMESPACE_GREEDY)
1272 continue;
1273
1274 if (equal) {
1275 /*
1276 * In the case 2/3 locks are eaten out by
1277 * modest pools, we re-setup equal limit
1278 * for _all_ pools.
1279 */
1280 l = LDLM_POOL_HOST_L /
91a50030 1281 ldlm_namespace_nr_read(client);
d7e09d03
PT
1282 } else {
1283 /*
1284 * All the rest of greedy pools will have
1285 * all locks in equal parts.
1286 */
1287 l = (LDLM_POOL_HOST_L - nr_l) /
91a50030 1288 (ldlm_namespace_nr_read(client) -
d7e09d03
PT
1289 nr_p);
1290 }
1291 ldlm_pool_setup(&ns->ns_pool, l);
1292 }
1293 mutex_unlock(ldlm_namespace_lock(client));
1294 }
1295
1296 /*
cbc3769e 1297 * Recalc at least ldlm_namespace_nr_read(client) namespaces.
d7e09d03 1298 */
91a50030 1299 for (nr = ldlm_namespace_nr_read(client); nr > 0; nr--) {
d7e09d03
PT
1300 int skip;
1301 /*
1302 * Lock the list, get first @ns in the list, getref, move it
1303 * to the tail, unlock and call pool recalc. This way we avoid
1304 * calling recalc under @ns lock what is really good as we get
1305 * rid of potential deadlock on client nodes when canceling
1306 * locks synchronously.
1307 */
1308 mutex_lock(ldlm_namespace_lock(client));
1309 if (list_empty(ldlm_namespace_list(client))) {
1310 mutex_unlock(ldlm_namespace_lock(client));
1311 break;
1312 }
1313 ns = ldlm_namespace_first_locked(client);
1314
91a50030
OD
1315 if (ns_old == ns) { /* Full pass complete */
1316 mutex_unlock(ldlm_namespace_lock(client));
1317 break;
1318 }
1319
1320 /* We got an empty namespace, need to move it back to inactive
1321 * list.
1322 * The race with parallel resource creation is fine:
1323 * - If they do namespace_get before our check, we fail the
1324 * check and they move this item to the end of the list anyway
1325 * - If we do the check and then they do namespace_get, then
1326 * we move the namespace to inactive and they will move
1327 * it back to active (synchronised by the lock, so no clash
1328 * there).
1329 */
1330 if (ldlm_ns_empty(ns)) {
1331 ldlm_namespace_move_to_inactive_locked(ns, client);
1332 mutex_unlock(ldlm_namespace_lock(client));
1333 continue;
1334 }
1335
1336 if (ns_old == NULL)
1337 ns_old = ns;
1338
d7e09d03
PT
1339 spin_lock(&ns->ns_lock);
1340 /*
1341 * skip ns which is being freed, and we don't want to increase
1342 * its refcount again, not even temporarily. bz21519 & LU-499.
1343 */
1344 if (ns->ns_stopping) {
1345 skip = 1;
1346 } else {
1347 skip = 0;
1348 ldlm_namespace_get(ns);
1349 }
1350 spin_unlock(&ns->ns_lock);
1351
91a50030 1352 ldlm_namespace_move_to_active_locked(ns, client);
d7e09d03
PT
1353 mutex_unlock(ldlm_namespace_lock(client));
1354
1355 /*
1356 * After setup is done - recalc the pool.
1357 */
1358 if (!skip) {
3eface59
OD
1359 int ttime = ldlm_pool_recalc(&ns->ns_pool);
1360
1361 if (ttime < time)
1362 time = ttime;
1363
d7e09d03
PT
1364 ldlm_namespace_put(ns);
1365 }
1366 }
3eface59 1367 return time;
d7e09d03
PT
1368}
1369EXPORT_SYMBOL(ldlm_pools_recalc);
1370
1371static int ldlm_pools_thread_main(void *arg)
1372{
1373 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
3eface59 1374 int s_time, c_time;
d7e09d03
PT
1375
1376 thread_set_flags(thread, SVC_RUNNING);
1377 wake_up(&thread->t_ctl_waitq);
1378
1379 CDEBUG(D_DLMTRACE, "%s: pool thread starting, process %d\n",
1380 "ldlm_poold", current_pid());
1381
1382 while (1) {
1383 struct l_wait_info lwi;
1384
1385 /*
1386 * Recal all pools on this tick.
1387 */
3eface59
OD
1388 s_time = ldlm_pools_recalc(LDLM_NAMESPACE_SERVER);
1389 c_time = ldlm_pools_recalc(LDLM_NAMESPACE_CLIENT);
d7e09d03
PT
1390
1391 /*
1392 * Wait until the next check time, or until we're
1393 * stopped.
1394 */
3eface59 1395 lwi = LWI_TIMEOUT(cfs_time_seconds(min(s_time, c_time)),
d7e09d03
PT
1396 NULL, NULL);
1397 l_wait_event(thread->t_ctl_waitq,
1398 thread_is_stopping(thread) ||
1399 thread_is_event(thread),
1400 &lwi);
1401
1402 if (thread_test_and_clear_flags(thread, SVC_STOPPING))
1403 break;
71e8dd9a 1404 thread_test_and_clear_flags(thread, SVC_EVENT);
d7e09d03
PT
1405 }
1406
1407 thread_set_flags(thread, SVC_STOPPED);
1408 wake_up(&thread->t_ctl_waitq);
1409
1410 CDEBUG(D_DLMTRACE, "%s: pool thread exiting, process %d\n",
1411 "ldlm_poold", current_pid());
1412
1413 complete_and_exit(&ldlm_pools_comp, 0);
1414}
1415
1416static int ldlm_pools_thread_start(void)
1417{
1418 struct l_wait_info lwi = { 0 };
68b636b6 1419 struct task_struct *task;
d7e09d03
PT
1420
1421 if (ldlm_pools_thread != NULL)
0a3bdb00 1422 return -EALREADY;
d7e09d03 1423
352f7891 1424 ldlm_pools_thread = kzalloc(sizeof(*ldlm_pools_thread), GFP_NOFS);
94e67761 1425 if (!ldlm_pools_thread)
0a3bdb00 1426 return -ENOMEM;
d7e09d03
PT
1427
1428 init_completion(&ldlm_pools_comp);
1429 init_waitqueue_head(&ldlm_pools_thread->t_ctl_waitq);
1430
1431 task = kthread_run(ldlm_pools_thread_main, ldlm_pools_thread,
1432 "ldlm_poold");
1433 if (IS_ERR(task)) {
1434 CERROR("Can't start pool thread, error %ld\n", PTR_ERR(task));
352f7891 1435 kfree(ldlm_pools_thread);
d7e09d03 1436 ldlm_pools_thread = NULL;
0a3bdb00 1437 return PTR_ERR(task);
d7e09d03
PT
1438 }
1439 l_wait_event(ldlm_pools_thread->t_ctl_waitq,
1440 thread_is_running(ldlm_pools_thread), &lwi);
0a3bdb00 1441 return 0;
d7e09d03
PT
1442}
1443
1444static void ldlm_pools_thread_stop(void)
1445{
8d2ff65d 1446 if (ldlm_pools_thread == NULL)
d7e09d03 1447 return;
d7e09d03
PT
1448
1449 thread_set_flags(ldlm_pools_thread, SVC_STOPPING);
1450 wake_up(&ldlm_pools_thread->t_ctl_waitq);
1451
1452 /*
1453 * Make sure that pools thread is finished before freeing @thread.
1454 * This fixes possible race and oops due to accessing freed memory
1455 * in pools thread.
1456 */
1457 wait_for_completion(&ldlm_pools_comp);
352f7891 1458 kfree(ldlm_pools_thread);
d7e09d03 1459 ldlm_pools_thread = NULL;
d7e09d03
PT
1460}
1461
cbc3769e
PT
1462static struct shrinker ldlm_pools_srv_shrinker = {
1463 .count_objects = ldlm_pools_srv_count,
1464 .scan_objects = ldlm_pools_srv_scan,
1465 .seeks = DEFAULT_SEEKS,
1466};
1467
1468static struct shrinker ldlm_pools_cli_shrinker = {
1469 .count_objects = ldlm_pools_cli_count,
1470 .scan_objects = ldlm_pools_cli_scan,
1471 .seeks = DEFAULT_SEEKS,
1472};
1473
d7e09d03
PT
1474int ldlm_pools_init(void)
1475{
1476 int rc;
d7e09d03
PT
1477
1478 rc = ldlm_pools_thread_start();
1479 if (rc == 0) {
cbc3769e
PT
1480 register_shrinker(&ldlm_pools_srv_shrinker);
1481 register_shrinker(&ldlm_pools_cli_shrinker);
d7e09d03 1482 }
0a3bdb00 1483 return rc;
d7e09d03
PT
1484}
1485EXPORT_SYMBOL(ldlm_pools_init);
1486
1487void ldlm_pools_fini(void)
1488{
cbc3769e
PT
1489 unregister_shrinker(&ldlm_pools_srv_shrinker);
1490 unregister_shrinker(&ldlm_pools_cli_shrinker);
d7e09d03
PT
1491 ldlm_pools_thread_stop();
1492}
1493EXPORT_SYMBOL(ldlm_pools_fini);
This page took 0.389826 seconds and 5 git commands to generate.