4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/lvfs/lvfs_lib.c
36 * Lustre filesystem abstraction routines
38 * Author: Andreas Dilger <adilger@clusterfs.com>
41 #include <linux/module.h>
43 #include <liblustre.h>
45 #include <lustre_lib.h>
46 #include <lprocfs_status.h>
48 unsigned int obd_alloc_fail_rate = 0;
50 int obd_alloc_fail(const void *ptr, const char *name, const char *type,
51 size_t size, const char *file, int line)
54 (cfs_rand() & OBD_ALLOC_FAIL_MASK) < obd_alloc_fail_rate) {
55 CERROR("%s%salloc of %s ("LPU64" bytes) failed at %s:%d\n",
56 ptr ? "force " :"", type, name, (__u64)size, file,
58 CERROR(LPU64" total bytes and "LPU64" total pages "
59 "("LPU64" bytes) allocated by Lustre, "
60 "%d total bytes by LNET\n",
62 obd_pages_sum() << CFS_PAGE_SHIFT,
64 cfs_atomic_read(&libcfs_kmemory));
69 EXPORT_SYMBOL(obd_alloc_fail);
72 void lprocfs_counter_add(struct lprocfs_stats *stats, int idx,
75 struct lprocfs_counter *percpu_cntr;
77 unsigned long flags = 0;
82 /* With per-client stats, statistics are allocated only for
83 * single CPU area, so the smp_id should be 0 always. */
84 smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID, &flags);
88 percpu_cntr = &(stats->ls_percpu[smp_id]->lp_cntr[idx]);
89 if (!(stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU))
90 cfs_atomic_inc(&percpu_cntr->lc_cntl.la_entry);
91 percpu_cntr->lc_count++;
93 if (percpu_cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) {
94 /* see comment in lprocfs_counter_sub */
95 LASSERT(!cfs_in_interrupt());
97 percpu_cntr->lc_sum += amount;
98 if (percpu_cntr->lc_config & LPROCFS_CNTR_STDDEV)
99 percpu_cntr->lc_sumsquare += (__s64)amount * amount;
100 if (amount < percpu_cntr->lc_min)
101 percpu_cntr->lc_min = amount;
102 if (amount > percpu_cntr->lc_max)
103 percpu_cntr->lc_max = amount;
105 if (!(stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU))
106 cfs_atomic_inc(&percpu_cntr->lc_cntl.la_exit);
107 lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID, &flags);
109 EXPORT_SYMBOL(lprocfs_counter_add);
111 void lprocfs_counter_sub(struct lprocfs_stats *stats, int idx, long amount)
113 struct lprocfs_counter *percpu_cntr;
115 unsigned long flags = 0;
120 /* With per-client stats, statistics are allocated only for
121 * single CPU area, so the smp_id should be 0 always. */
122 smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID, &flags);
126 percpu_cntr = &(stats->ls_percpu[smp_id]->lp_cntr[idx]);
127 if (!(stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU))
128 cfs_atomic_inc(&percpu_cntr->lc_cntl.la_entry);
129 if (percpu_cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) {
131 * currently lprocfs_count_add() can only be called in thread
132 * context; sometimes we use RCU callbacks to free memory
133 * which calls lprocfs_counter_sub(), and RCU callbacks may
134 * execute in softirq context - right now that's the only case
135 * we're in softirq context here, use separate counter for that.
138 if (cfs_in_interrupt())
139 percpu_cntr->lc_sum_irq -= amount;
141 percpu_cntr->lc_sum -= amount;
143 if (!(stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU))
144 cfs_atomic_inc(&percpu_cntr->lc_cntl.la_exit);
145 lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID, &flags);
147 EXPORT_SYMBOL(lprocfs_counter_sub);
149 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int idx)
151 unsigned int percpusize;
154 /* the 1st percpu entry was statically allocated in
155 * lprocfs_alloc_stats() */
156 LASSERT(idx != 0 && stats->ls_percpu[0] != NULL);
157 LASSERT(stats->ls_percpu[idx] == NULL);
158 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
160 percpusize = CFS_L1_CACHE_ALIGN(offsetof(struct lprocfs_percpu,
161 lp_cntr[stats->ls_num]));
162 OBD_ALLOC_GFP(stats->ls_percpu[idx], percpusize, CFS_ALLOC_ATOMIC);
163 if (stats->ls_percpu[idx] != NULL) {
165 if (unlikely(stats->ls_biggest_alloc_num <= idx))
166 stats->ls_biggest_alloc_num = idx + 1;
168 /* initialize the ls_percpu[idx] by copying the 0th template
170 memcpy(stats->ls_percpu[idx], stats->ls_percpu[0],
176 EXPORT_SYMBOL(lprocfs_stats_alloc_one);
179 EXPORT_SYMBOL(obd_alloc_fail_rate);