Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / lustre / obdclass / lprocfs_counters.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  *
19  * http://www.gnu.org/licenses/gpl-2.0.html
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
25  * Use is subject to license terms.
26  *
27  * Copyright (c) 2012, 2013, Intel Corporation.
28  */
29 /*
30  * This file is part of Lustre, http://www.lustre.org/
31  *
32  * lustre/obdclass/lprocfs_counters.c
33  *
34  * Lustre lprocfs counter routines
35  *
36  * Author: Andreas Dilger <andreas.dilger@intel.com>
37  */
38 #include <linux/module.h>
39 #include <lustre_lib.h>
40 #include <lprocfs_status.h>
41
42 #ifdef CONFIG_PROC_FS
43 void lprocfs_counter_add(struct lprocfs_stats *stats, int idx, long amount)
44 {
45         struct lprocfs_counter *percpu_cntr;
46         struct lprocfs_counter_header *header;
47         int smp_id;
48         unsigned long flags = 0;
49         struct obd_histogram *hist;
50
51         if (stats == NULL)
52                 return;
53
54         LASSERTF(0 <= idx && idx < stats->ls_num,
55                  "idx %d, ls_num %hu\n", idx, stats->ls_num);
56
57         /* With per-client stats, statistics are allocated only for
58          * single CPU area, so the smp_id should be 0 always. */
59         smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID, &flags);
60         if (smp_id < 0)
61                 return;
62
63         header = &stats->ls_cnt_header[idx];
64         percpu_cntr = lprocfs_stats_counter_get(stats, smp_id, idx);
65         percpu_cntr->lc_count++;
66
67         if (header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
68                 /*
69                  * lprocfs_counter_add() can be called in interrupt context,
70                  * as memory allocation could trigger memory shrinker call
71                  * ldlm_pool_shrink(), which calls lprocfs_counter_add().
72                  * LU-1727.
73                  */
74                 percpu_cntr->lc_sum += amount;
75
76                 if (header->lc_config & LPROCFS_CNTR_STDDEV)
77                         percpu_cntr->lc_sumsquare += (__s64)amount * amount;
78                 if (amount < percpu_cntr->lc_min)
79                         percpu_cntr->lc_min = amount;
80                 if (amount > percpu_cntr->lc_max)
81                         percpu_cntr->lc_max = amount;
82         }
83         /* no counter in interrupt has historgram for now */
84         hist = stats->ls_cnt_header[idx].lc_hist;
85         if (hist != NULL) {
86                 unsigned int val = 0;
87
88                 if (likely(amount != 0))
89                         val = min(fls(amount - 1), OBD_HIST_MAX - 1);
90                 spin_lock(&hist->oh_lock);
91                 hist->oh_buckets[val]++;
92                 spin_unlock(&hist->oh_lock);
93         }
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         LASSERTF(0 <= idx && idx < stats->ls_num,
110                  "idx %d, ls_num %hu\n", idx, stats->ls_num);
111
112         /* With per-client stats, statistics are allocated only for
113          * single CPU area, so the smp_id should be 0 always. */
114         smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID, &flags);
115         if (smp_id < 0)
116                 return;
117
118         header = &stats->ls_cnt_header[idx];
119         percpu_cntr = lprocfs_stats_counter_get(stats, smp_id, idx);
120         if (header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
121                 /*
122                  * Sometimes we use RCU callbacks to free memory which calls
123                  * lprocfs_counter_sub(), and RCU callbacks may execute in
124                  * softirq context - right now that's the only case we're in
125                  * softirq context here, use separate counter for that.
126                  * bz20650.
127                  */
128                 percpu_cntr->lc_sum -= amount;
129         }
130         lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID, &flags);
131 }
132 EXPORT_SYMBOL(lprocfs_counter_sub);
133 #endif  /* CONFIG_PROC_FS */