Whamcloud - gitweb
31c2d512fa6bca9bd5c7a1bbb7bd3d022818a533
[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 #ifdef LPROCFS
51 void lprocfs_counter_add(struct lprocfs_stats *stats, int idx, long amount)
52 {
53         struct lprocfs_counter          *percpu_cntr;
54         struct lprocfs_counter_header   *header;
55         int                             smp_id;
56         unsigned long                   flags = 0;
57
58         if (stats == NULL)
59                 return;
60
61         /* With per-client stats, statistics are allocated only for
62          * single CPU area, so the smp_id should be 0 always. */
63         smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID, &flags);
64         if (smp_id < 0)
65                 return;
66
67         header = &stats->ls_cnt_header[idx];
68         percpu_cntr = lprocfs_stats_counter_get(stats, smp_id, idx);
69         percpu_cntr->lc_count++;
70
71         if (header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
72                 /*
73                  * lprocfs_counter_add() can be called in interrupt context,
74                  * as memory allocation could trigger memory shrinker call
75                  * ldlm_pool_shrink(), which calls lprocfs_counter_add().
76                  * LU-1727.
77                  *
78                  * Only obd_memory uses LPROCFS_STATS_FLAG_IRQ_SAFE
79                  * flag, because it needs accurate counting lest memory leak
80                  * check reports error.
81                  */
82                 if (cfs_in_interrupt() &&
83                     (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
84                         percpu_cntr->lc_sum_irq += amount;
85                 else
86                         percpu_cntr->lc_sum += amount;
87
88                 if (header->lc_config & LPROCFS_CNTR_STDDEV)
89                         percpu_cntr->lc_sumsquare += (__s64)amount * amount;
90                 if (amount < percpu_cntr->lc_min)
91                         percpu_cntr->lc_min = amount;
92                 if (amount > percpu_cntr->lc_max)
93                         percpu_cntr->lc_max = amount;
94         }
95         lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID, &flags);
96 }
97 EXPORT_SYMBOL(lprocfs_counter_add);
98
99 void lprocfs_counter_sub(struct lprocfs_stats *stats, int idx, long amount)
100 {
101         struct lprocfs_counter          *percpu_cntr;
102         struct lprocfs_counter_header   *header;
103         int                             smp_id;
104         unsigned long                   flags = 0;
105
106         if (stats == NULL)
107                 return;
108
109         /* With per-client stats, statistics are allocated only for
110          * single CPU area, so the smp_id should be 0 always. */
111         smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID, &flags);
112         if (smp_id < 0)
113                 return;
114
115         header = &stats->ls_cnt_header[idx];
116         percpu_cntr = lprocfs_stats_counter_get(stats, smp_id, idx);
117         if (header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
118                 /*
119                  * Sometimes we use RCU callbacks to free memory which calls
120                  * lprocfs_counter_sub(), and RCU callbacks may execute in
121                  * softirq context - right now that's the only case we're in
122                  * softirq context here, use separate counter for that.
123                  * bz20650.
124                  *
125                  * Only obd_memory uses LPROCFS_STATS_FLAG_IRQ_SAFE
126                  * flag, because it needs accurate counting lest memory leak
127                  * check reports error.
128                  */
129                 if (cfs_in_interrupt() &&
130                     (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
131                         percpu_cntr->lc_sum_irq -= amount;
132                 else
133                         percpu_cntr->lc_sum -= amount;
134         }
135         lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID, &flags);
136 }
137 EXPORT_SYMBOL(lprocfs_counter_sub);
138
139 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
140 {
141         struct lprocfs_counter  *cntr;
142         unsigned int            percpusize;
143         int                     rc = -ENOMEM;
144         unsigned long           flags = 0;
145         int                     i;
146
147         LASSERT(stats->ls_percpu[cpuid] == NULL);
148         LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
149
150         percpusize = lprocfs_stats_counter_size(stats);
151         LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
152         if (stats->ls_percpu[cpuid] != NULL) {
153                 rc = 0;
154                 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
155                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
156                                 spin_lock_irqsave(&stats->ls_lock, flags);
157                         else
158                                 spin_lock(&stats->ls_lock);
159                         if (stats->ls_biggest_alloc_num <= cpuid)
160                                 stats->ls_biggest_alloc_num = cpuid + 1;
161                         if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
162                                 spin_unlock_irqrestore(&stats->ls_lock, flags);
163                         } else {
164                                 spin_unlock(&stats->ls_lock);
165                         }
166                 }
167                 /* initialize the ls_percpu[cpuid] non-zero counter */
168                 for (i = 0; i < stats->ls_num; ++i) {
169                         cntr = lprocfs_stats_counter_get(stats, cpuid, i);
170                         cntr->lc_min = LC_MIN_INIT;
171                 }
172         }
173
174         return rc;
175 }
176 EXPORT_SYMBOL(lprocfs_stats_alloc_one);
177 #endif  /* LPROCFS */