4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/lvfs/lvfs_lib.c
38 * Lustre filesystem abstraction routines
40 * Author: Andreas Dilger <adilger@clusterfs.com>
42 #include <linux/module.h>
43 #include <lustre_lib.h>
44 #include <lprocfs_status.h>
47 void lprocfs_counter_add(struct lprocfs_stats *stats, int idx, long amount)
49 struct lprocfs_counter *percpu_cntr;
50 struct lprocfs_counter_header *header;
52 unsigned long flags = 0;
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);
63 header = &stats->ls_cnt_header[idx];
64 percpu_cntr = lprocfs_stats_counter_get(stats, smp_id, idx);
65 percpu_cntr->lc_count++;
67 if (header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
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().
74 * Only obd_memory uses LPROCFS_STATS_FLAG_IRQ_SAFE
75 * flag, because it needs accurate counting lest memory leak
76 * check reports error.
79 (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
80 percpu_cntr->lc_sum_irq += amount;
82 percpu_cntr->lc_sum += amount;
84 if (header->lc_config & LPROCFS_CNTR_STDDEV)
85 percpu_cntr->lc_sumsquare += (__s64)amount * amount;
86 if (amount < percpu_cntr->lc_min)
87 percpu_cntr->lc_min = amount;
88 if (amount > percpu_cntr->lc_max)
89 percpu_cntr->lc_max = amount;
91 lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID, &flags);
93 EXPORT_SYMBOL(lprocfs_counter_add);
95 void lprocfs_counter_sub(struct lprocfs_stats *stats, int idx, long amount)
97 struct lprocfs_counter *percpu_cntr;
98 struct lprocfs_counter_header *header;
100 unsigned long flags = 0;
105 /* With per-client stats, statistics are allocated only for
106 * single CPU area, so the smp_id should be 0 always. */
107 smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID, &flags);
111 header = &stats->ls_cnt_header[idx];
112 percpu_cntr = lprocfs_stats_counter_get(stats, smp_id, idx);
113 if (header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
115 * Sometimes we use RCU callbacks to free memory which calls
116 * lprocfs_counter_sub(), and RCU callbacks may execute in
117 * softirq context - right now that's the only case we're in
118 * softirq context here, use separate counter for that.
121 * Only obd_memory uses LPROCFS_STATS_FLAG_IRQ_SAFE
122 * flag, because it needs accurate counting lest memory leak
123 * check reports error.
125 if (in_interrupt() &&
126 (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
127 percpu_cntr->lc_sum_irq -= amount;
129 percpu_cntr->lc_sum -= amount;
131 lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID, &flags);
133 EXPORT_SYMBOL(lprocfs_counter_sub);
135 int lprocfs_stats_alloc_one(struct lprocfs_stats *stats, unsigned int cpuid)
137 struct lprocfs_counter *cntr;
138 unsigned int percpusize;
140 unsigned long flags = 0;
143 LASSERT(stats->ls_percpu[cpuid] == NULL);
144 LASSERT((stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) == 0);
146 percpusize = lprocfs_stats_counter_size(stats);
147 LIBCFS_ALLOC_ATOMIC(stats->ls_percpu[cpuid], percpusize);
148 if (stats->ls_percpu[cpuid] != NULL) {
150 if (unlikely(stats->ls_biggest_alloc_num <= cpuid)) {
151 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE)
152 spin_lock_irqsave(&stats->ls_lock, flags);
154 spin_lock(&stats->ls_lock);
155 if (stats->ls_biggest_alloc_num <= cpuid)
156 stats->ls_biggest_alloc_num = cpuid + 1;
157 if (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) {
158 spin_unlock_irqrestore(&stats->ls_lock, flags);
160 spin_unlock(&stats->ls_lock);
163 /* initialize the ls_percpu[cpuid] non-zero counter */
164 for (i = 0; i < stats->ls_num; ++i) {
165 cntr = lprocfs_stats_counter_get(stats, cpuid, i);
166 cntr->lc_min = LC_MIN_INIT;
172 EXPORT_SYMBOL(lprocfs_stats_alloc_one);