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  * Lustre is a trademark of Sun Microsystems, Inc.
32  *
33  * lustre/obdclass/lprocfs_counters.c
34  *
35  * Lustre lprocfs counter routines
36  *
37  * Author: Andreas Dilger <andreas.dilger@intel.com>
38  */
39 
40 #include <linux/module.h>
41 #include "../include/lprocfs_status.h"
42 #include "../include/obd_support.h"
43 
44 struct lprocfs_stats *obd_memory = NULL;
45 EXPORT_SYMBOL(obd_memory);
46 
lprocfs_counter_add(struct lprocfs_stats * stats,int idx,long amount)47 void lprocfs_counter_add(struct lprocfs_stats *stats, int idx, long amount)
48 {
49 	struct lprocfs_counter		*percpu_cntr;
50 	struct lprocfs_counter_header	*header;
51 	int				smp_id;
52 	unsigned long			flags = 0;
53 
54 	if (stats == NULL)
55 		return;
56 
57 	LASSERTF(0 <= idx && idx < stats->ls_num,
58 		 "idx %d, ls_num %hu\n", idx, stats->ls_num);
59 
60 	/* With per-client stats, statistics are allocated only for
61 	 * single CPU area, so the smp_id should be 0 always. */
62 	smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID, &flags);
63 	if (smp_id < 0)
64 		return;
65 
66 	header = &stats->ls_cnt_header[idx];
67 	percpu_cntr = lprocfs_stats_counter_get(stats, smp_id, idx);
68 	percpu_cntr->lc_count++;
69 
70 	if (header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
71 		/*
72 		 * lprocfs_counter_add() can be called in interrupt context,
73 		 * as memory allocation could trigger memory shrinker call
74 		 * ldlm_pool_shrink(), which calls lprocfs_counter_add().
75 		 * LU-1727.
76 		 *
77 		 * Only obd_memory uses LPROCFS_STATS_FLAG_IRQ_SAFE
78 		 * flag, because it needs accurate counting lest memory leak
79 		 * check reports error.
80 		 */
81 		if (in_interrupt() &&
82 		    (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
83 			percpu_cntr->lc_sum_irq += amount;
84 		else
85 			percpu_cntr->lc_sum += amount;
86 
87 		if (header->lc_config & LPROCFS_CNTR_STDDEV)
88 			percpu_cntr->lc_sumsquare += (__s64)amount * amount;
89 		if (amount < percpu_cntr->lc_min)
90 			percpu_cntr->lc_min = amount;
91 		if (amount > percpu_cntr->lc_max)
92 			percpu_cntr->lc_max = amount;
93 	}
94 	lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID, &flags);
95 }
96 EXPORT_SYMBOL(lprocfs_counter_add);
97 
lprocfs_counter_sub(struct lprocfs_stats * stats,int idx,long amount)98 void lprocfs_counter_sub(struct lprocfs_stats *stats, int idx, long amount)
99 {
100 	struct lprocfs_counter		*percpu_cntr;
101 	struct lprocfs_counter_header	*header;
102 	int				smp_id;
103 	unsigned long			flags = 0;
104 
105 	if (stats == NULL)
106 		return;
107 
108 	LASSERTF(0 <= idx && idx < stats->ls_num,
109 		 "idx %d, ls_num %hu\n", idx, stats->ls_num);
110 
111 	/* With per-client stats, statistics are allocated only for
112 	 * single CPU area, so the smp_id should be 0 always. */
113 	smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID, &flags);
114 	if (smp_id < 0)
115 		return;
116 
117 	header = &stats->ls_cnt_header[idx];
118 	percpu_cntr = lprocfs_stats_counter_get(stats, smp_id, idx);
119 	if (header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
120 		/*
121 		 * Sometimes we use RCU callbacks to free memory which calls
122 		 * lprocfs_counter_sub(), and RCU callbacks may execute in
123 		 * softirq context - right now that's the only case we're in
124 		 * softirq context here, use separate counter for that.
125 		 * bz20650.
126 		 *
127 		 * Only obd_memory uses LPROCFS_STATS_FLAG_IRQ_SAFE
128 		 * flag, because it needs accurate counting lest memory leak
129 		 * check reports error.
130 		 */
131 		if (in_interrupt() &&
132 		    (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
133 			percpu_cntr->lc_sum_irq -= amount;
134 		else
135 			percpu_cntr->lc_sum -= amount;
136 	}
137 	lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID, &flags);
138 }
139 EXPORT_SYMBOL(lprocfs_counter_sub);
140