Whamcloud - gitweb
04f87baf7b62f68f6404185c4dec68bbdbbeb63f
[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  * Copyright (c) 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/lvfs/lvfs_lib.c
37  *
38  * Lustre filesystem abstraction routines
39  *
40  * Author: Andreas Dilger <adilger@clusterfs.com>
41  */
42 #ifdef __KERNEL__
43 #include <linux/module.h>
44 #else
45 #include <liblustre.h>
46 #endif
47 #include <lustre_lib.h>
48 #include <lprocfs_status.h>
49
50 unsigned int obd_alloc_fail_rate = 0;
51
52 int obd_alloc_fail(const void *ptr, const char *name, const char *type,
53                    size_t size, const char *file, int line)
54 {
55         if (ptr == NULL ||
56             (cfs_rand() & OBD_ALLOC_FAIL_MASK) < obd_alloc_fail_rate) {
57                 CERROR("%s%salloc of %s ("LPU64" bytes) failed at %s:%d\n",
58                        ptr ? "force " :"", type, name, (__u64)size, file,
59                        line);
60                 CERROR(LPU64" total bytes and "LPU64" total pages "
61                        "("LPU64" bytes) allocated by Lustre, "
62                        "%d total bytes by LNET\n",
63                        obd_memory_sum(),
64                        obd_pages_sum() << CFS_PAGE_SHIFT,
65                        obd_pages_sum(),
66                        cfs_atomic_read(&libcfs_kmemory));
67                 return 1;
68         }
69         return 0;
70 }
71 EXPORT_SYMBOL(obd_alloc_fail);
72
73 #ifdef LPROCFS
74 void lprocfs_counter_add(struct lprocfs_stats *stats, int idx, long amount)
75 {
76         struct lprocfs_counter          *percpu_cntr;
77         struct lprocfs_counter_header   *header;
78         int                             smp_id;
79         unsigned long                   flags = 0;
80
81         if (stats == NULL)
82                 return;
83
84         LASSERT(ergo((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) == 0,
85                      !cfs_in_interrupt()));
86
87         /* With per-client stats, statistics are allocated only for
88          * single CPU area, so the smp_id should be 0 always. */
89         smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID, &flags);
90         if (smp_id < 0)
91                 return;
92
93         header = &stats->ls_cnt_header[idx];
94         percpu_cntr = lprocfs_stats_counter_get(stats, smp_id, idx);
95         percpu_cntr->lc_count++;
96
97         if (header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
98                 /*
99                  * lprocfs_counter_add() can be called in interrupt context,
100                  * as memory allocation could trigger memory shrinker call
101                  * ldlm_pool_shrink(), which calls lprocfs_counter_add().
102                  * LU-1727.
103                  */
104                 if (cfs_in_interrupt())
105                         percpu_cntr->lc_sum_irq += amount;
106                 else
107                         percpu_cntr->lc_sum += amount;
108
109                 if (header->lc_config & LPROCFS_CNTR_STDDEV)
110                         percpu_cntr->lc_sumsquare += (__s64)amount * amount;
111                 if (amount < percpu_cntr->lc_min)
112                         percpu_cntr->lc_min = amount;
113                 if (amount > percpu_cntr->lc_max)
114                         percpu_cntr->lc_max = amount;
115         }
116         lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID, &flags);
117 }
118 EXPORT_SYMBOL(lprocfs_counter_add);
119
120 void lprocfs_counter_sub(struct lprocfs_stats *stats, int idx, long amount)
121 {
122         struct lprocfs_counter          *percpu_cntr;
123         struct lprocfs_counter_header   *header;
124         int                             smp_id;
125         unsigned long                   flags = 0;
126
127         if (stats == NULL)
128                 return;
129
130         LASSERT(ergo((stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) == 0,
131                      !cfs_in_interrupt()));
132
133         /* With per-client stats, statistics are allocated only for
134          * single CPU area, so the smp_id should be 0 always. */
135         smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID, &flags);
136         if (smp_id < 0)
137                 return;
138
139         header = &stats->ls_cnt_header[idx];
140         percpu_cntr = lprocfs_stats_counter_get(stats, smp_id, idx);
141         if (header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
142                 /*
143                  * Sometimes we use RCU callbacks to free memory which calls
144                  * lprocfs_counter_sub(), and RCU callbacks may execute in
145                  * softirq context - right now that's the only case we're in
146                  * softirq context here, use separate counter for that.
147                  * bz20650.
148                  */
149                 if (cfs_in_interrupt())
150                         percpu_cntr->lc_sum_irq -= amount;
151                 else
152                         percpu_cntr->lc_sum -= amount;
153         }
154         lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID, &flags);
155 }
156 EXPORT_SYMBOL(lprocfs_counter_sub);
157
158 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
159 {
160         struct lprocfs_counter  *cntr;
161         unsigned int            percpusize;
162         int                     rc = -ENOMEM;
163         unsigned long           flags = 0;
164         int                     i;
165
166         LASSERT(stats->ls_percpu[cpuid] == NULL);
167         LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
168
169         percpusize = lprocfs_stats_counter_size(stats);
170         LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
171         if (stats->ls_percpu[cpuid] != NULL) {
172                 rc = 0;
173                 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
174                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
175                                 spin_lock_irqsave(&stats->ls_lock, flags);
176                         else
177                                 spin_lock(&stats->ls_lock);
178                         if (stats->ls_biggest_alloc_num <= cpuid)
179                                 stats->ls_biggest_alloc_num = cpuid + 1;
180                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
181                                 spin_unlock_irqrestore(&stats->ls_lock, flags);
182                         } else {
183                                 spin_unlock(&stats->ls_lock);
184                         }
185                 }
186                 /* initialize the ls_percpu[cpuid] non-zero counter */
187                 for (i = 0; i < stats->ls_num; ++i) {
188                         cntr = lprocfs_stats_counter_get(stats, cpuid, i);
189                         cntr->lc_min = LC_MIN_INIT;
190                 }
191         }
192
193         return rc;
194 }
195 EXPORT_SYMBOL(lprocfs_stats_alloc_one);
196 #endif  /* LPROCFS */
197
198 EXPORT_SYMBOL(obd_alloc_fail_rate);