Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[fs/lustre-release.git] / lustre / lvfs / lvfs_lib.c
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 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/lvfs/lvfs_lib.c
35  *
36  * Lustre filesystem abstraction routines
37  *
38  * Author: Andreas Dilger <adilger@clusterfs.com>
39  */
40 #ifdef __KERNEL__
41 #include <linux/module.h>
42 #else
43 #include <liblustre.h>
44 #endif
45 #include <lustre_lib.h>
46 #include <lprocfs_status.h>
47
48 unsigned int obd_alloc_fail_rate = 0;
49
50 int obd_alloc_fail(const void *ptr, const char *name, const char *type,
51                    size_t size, const char *file, int line)
52 {
53         if (ptr == NULL ||
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,
57                        line);
58                 CERROR(LPU64" total bytes and "LPU64" total pages "
59                        "("LPU64" bytes) allocated by Lustre, "
60                        "%d total bytes by LNET\n",
61                        obd_memory_sum(),
62                        obd_pages_sum() << CFS_PAGE_SHIFT,
63                        obd_pages_sum(),
64                        cfs_atomic_read(&libcfs_kmemory));
65                 return 1;
66         }
67         return 0;
68 }
69 EXPORT_SYMBOL(obd_alloc_fail);
70
71 #ifdef LPROCFS
72 void lprocfs_counter_add(struct lprocfs_stats *stats, int idx,
73                                        long amount)
74 {
75         struct lprocfs_counter *percpu_cntr;
76         int smp_id;
77
78         if (stats == NULL)
79                 return;
80
81         /* With per-client stats, statistics are allocated only for
82          * single CPU area, so the smp_id should be 0 always. */
83         smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID);
84
85         percpu_cntr = &(stats->ls_percpu[smp_id]->lp_cntr[idx]);
86         if (!(stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU))
87                 cfs_atomic_inc(&percpu_cntr->lc_cntl.la_entry);
88         percpu_cntr->lc_count++;
89
90         if (percpu_cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) {
91                 /* see comment in lprocfs_counter_sub */
92                 LASSERT(!cfs_in_interrupt());
93
94                 percpu_cntr->lc_sum += amount;
95                 if (percpu_cntr->lc_config & LPROCFS_CNTR_STDDEV)
96                         percpu_cntr->lc_sumsquare += (__s64)amount * amount;
97                 if (amount < percpu_cntr->lc_min)
98                         percpu_cntr->lc_min = amount;
99                 if (amount > percpu_cntr->lc_max)
100                         percpu_cntr->lc_max = amount;
101         }
102         if (!(stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU))
103                 cfs_atomic_inc(&percpu_cntr->lc_cntl.la_exit);
104         lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID);
105 }
106 EXPORT_SYMBOL(lprocfs_counter_add);
107
108 void lprocfs_counter_sub(struct lprocfs_stats *stats, int idx,
109                                        long amount)
110 {
111         struct lprocfs_counter *percpu_cntr;
112         int smp_id;
113
114         if (stats == NULL)
115                 return;
116
117         /* With per-client stats, statistics are allocated only for
118          * single CPU area, so the smp_id should be 0 always. */
119         smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID);
120
121         percpu_cntr = &(stats->ls_percpu[smp_id]->lp_cntr[idx]);
122         if (!(stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU))
123                 cfs_atomic_inc(&percpu_cntr->lc_cntl.la_entry);
124         if (percpu_cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) {
125                 /*
126                  * currently lprocfs_count_add() can only be called in thread
127                  * context; sometimes we use RCU callbacks to free memory
128                  * which calls lprocfs_counter_sub(), and RCU callbacks may
129                  * execute in softirq context - right now that's the only case
130                  * we're in softirq context here, use separate counter for that.
131                  * bz20650.
132                  */
133                 if (cfs_in_interrupt())
134                         percpu_cntr->lc_sum_irq -= amount;
135                 else
136                         percpu_cntr->lc_sum -= amount;
137         }
138         if (!(stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU))
139                 cfs_atomic_inc(&percpu_cntr->lc_cntl.la_exit);
140         lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID);
141 }
142 EXPORT_SYMBOL(lprocfs_counter_sub);
143 #endif  /* LPROCFS */
144
145 EXPORT_SYMBOL(obd_alloc_fail_rate);