1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/lvfs/lvfs_lib.c
38 * Lustre filesystem abstraction routines
40 * Author: Andreas Dilger <adilger@clusterfs.com>
43 #include <linux/module.h>
44 #include <linux/random.h>
46 #include <liblustre.h>
48 #include <lustre_lib.h>
49 #include <lprocfs_status.h>
51 __u64 obd_max_pages = 0;
52 __u64 obd_max_alloc = 0;
55 struct lprocfs_stats *obd_memory = NULL;
56 spinlock_t obd_updatemax_lock = SPIN_LOCK_UNLOCKED;
57 /* refine later and change to seqlock or simlar from libcfs */
63 unsigned int obd_fail_val = 0;
64 unsigned int obd_fail_loc = 0;
65 unsigned int obd_alloc_fail_rate = 0;
67 int obd_alloc_fail(const void *ptr, const char *name, const char *type,
68 size_t size, const char *file, int line)
71 (ll_rand() & OBD_ALLOC_FAIL_MASK) < obd_alloc_fail_rate) {
72 CERROR("%s%salloc of %s ("LPU64" bytes) failed at %s:%d\n",
73 ptr ? "force " :"", type, name, (__u64)size, file,
75 CERROR(LPU64" total bytes and "LPU64" total pages "
76 "("LPU64" bytes) allocated by Lustre, "
77 "%d total bytes by LNET\n",
79 obd_pages_sum() << CFS_PAGE_SHIFT,
81 atomic_read(&libcfs_kmemory));
86 EXPORT_SYMBOL(obd_alloc_fail);
89 void obd_update_maxusage()
93 max1 = obd_pages_sum();
94 max2 = obd_memory_sum();
96 spin_lock(&obd_updatemax_lock);
97 if (max1 > obd_max_pages)
99 if (max2 > obd_max_alloc)
100 obd_max_alloc = max2;
101 spin_unlock(&obd_updatemax_lock);
104 __u64 obd_memory_max(void)
108 spin_lock(&obd_updatemax_lock);
110 spin_unlock(&obd_updatemax_lock);
115 __u64 obd_pages_max(void)
119 spin_lock(&obd_updatemax_lock);
121 spin_unlock(&obd_updatemax_lock);
126 EXPORT_SYMBOL(obd_update_maxusage);
127 EXPORT_SYMBOL(obd_pages_max);
128 EXPORT_SYMBOL(obd_memory_max);
129 EXPORT_SYMBOL(obd_memory);
134 __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
135 enum lprocfs_fields_flags field)
143 centry = atomic_read(&lc->lc_cntl.la_entry);
146 case LPROCFS_FIELDS_FLAGS_CONFIG:
149 case LPROCFS_FIELDS_FLAGS_SUM:
150 ret = lc->lc_sum + lc->lc_sum_irq;
152 case LPROCFS_FIELDS_FLAGS_MIN:
155 case LPROCFS_FIELDS_FLAGS_MAX:
158 case LPROCFS_FIELDS_FLAGS_AVG:
159 ret = (lc->lc_max - lc->lc_min)/2;
161 case LPROCFS_FIELDS_FLAGS_SUMSQUARE:
162 ret = lc->lc_sumsquare;
164 case LPROCFS_FIELDS_FLAGS_COUNT:
170 } while (centry != atomic_read(&lc->lc_cntl.la_entry) &&
171 centry != atomic_read(&lc->lc_cntl.la_exit));
175 EXPORT_SYMBOL(lprocfs_read_helper);
177 void lprocfs_counter_add(struct lprocfs_stats *stats, int idx,
180 struct lprocfs_counter *percpu_cntr;
186 /* With per-client stats, statistics are allocated only for
187 * single CPU area, so the smp_id should be 0 always. */
188 smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID);
190 percpu_cntr = &(stats->ls_percpu[smp_id]->lp_cntr[idx]);
191 atomic_inc(&percpu_cntr->lc_cntl.la_entry);
192 percpu_cntr->lc_count++;
194 if (percpu_cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) {
195 /* see comment in lprocfs_counter_sub */
196 LASSERT(!cfs_in_interrupt());
198 percpu_cntr->lc_sum += amount;
199 if (percpu_cntr->lc_config & LPROCFS_CNTR_STDDEV)
200 percpu_cntr->lc_sumsquare += (__u64)amount * amount;
201 if (amount < percpu_cntr->lc_min)
202 percpu_cntr->lc_min = amount;
203 if (amount > percpu_cntr->lc_max)
204 percpu_cntr->lc_max = amount;
206 atomic_inc(&percpu_cntr->lc_cntl.la_exit);
207 lprocfs_stats_unlock(stats);
209 EXPORT_SYMBOL(lprocfs_counter_add);
211 void lprocfs_counter_sub(struct lprocfs_stats *stats, int idx,
214 struct lprocfs_counter *percpu_cntr;
220 /* With per-client stats, statistics are allocated only for
221 * single CPU area, so the smp_id should be 0 always. */
222 smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID);
224 percpu_cntr = &(stats->ls_percpu[smp_id]->lp_cntr[idx]);
225 atomic_inc(&percpu_cntr->lc_cntl.la_entry);
226 if (percpu_cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) {
228 * currently lprocfs_count_add() can only be called in thread
229 * context; sometimes we use RCU callbacks to free memory
230 * which calls lprocfs_counter_sub(), and RCU callbacks may
231 * execute in softirq context - right now that's the only case
232 * we're in softirq context here, use separate counter for that.
235 if (cfs_in_interrupt())
236 percpu_cntr->lc_sum_irq -= amount;
238 percpu_cntr->lc_sum -= amount;
240 atomic_inc(&percpu_cntr->lc_cntl.la_exit);
241 lprocfs_stats_unlock(stats);
243 EXPORT_SYMBOL(lprocfs_counter_sub);
246 EXPORT_SYMBOL(obd_fail_loc);
247 EXPORT_SYMBOL(obd_alloc_fail_rate);
248 EXPORT_SYMBOL(obd_fail_val);
250 int obd_fail_check(__u32 id)
252 static int count = 0;
253 if (likely((obd_fail_loc & OBD_FAIL_MASK_LOC) !=
254 (id & OBD_FAIL_MASK_LOC)))
257 if ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE)) ==
258 (OBD_FAILED | OBD_FAIL_ONCE)) {
259 count = 0; /* paranoia */
263 if (obd_fail_loc & OBD_FAIL_RAND) {
264 if (obd_fail_val < 2)
266 if (ll_rand() % obd_fail_val > 0)
270 if (obd_fail_loc & OBD_FAIL_SKIP) {
272 if (count < obd_fail_val)
277 /* Overridden by FAIL_ONCE */
278 if (obd_fail_loc & OBD_FAIL_SOME) {
280 if (count >= obd_fail_val) {
282 /* Don't fail anymore */
283 obd_fail_loc |= OBD_FAIL_ONCE;
287 obd_fail_loc |= OBD_FAILED;
288 /* Handle old checks that OR in this */
289 if (id & OBD_FAIL_ONCE)
290 obd_fail_loc |= OBD_FAIL_ONCE;
294 EXPORT_SYMBOL(obd_fail_check);