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
19 * http://www.gnu.org/licenses/gpl-2.0.html
24 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Use is subject to license terms.
27 * Copyright (c) 2012, 2013, Intel Corporation.
30 * This file is part of Lustre, http://www.lustre.org/
31 * Lustre is a trademark of Sun Microsystems, Inc.
33 * lustre/obdclass/lprocfs_counters.c
35 * Lustre lprocfs counter routines
37 * Author: Andreas Dilger <andreas.dilger@intel.com>
40 #include <linux/module.h>
41 #include "../include/lprocfs_status.h"
42 #include "../include/obd_support.h"
44 void lprocfs_counter_add(struct lprocfs_stats *stats, int idx, long amount)
46 struct lprocfs_counter *percpu_cntr;
47 struct lprocfs_counter_header *header;
49 unsigned long flags = 0;
54 LASSERTF(0 <= idx && idx < stats->ls_num,
55 "idx %d, ls_num %hu\n", idx, stats->ls_num);
57 /* With per-client stats, statistics are allocated only for
58 * single CPU area, so the smp_id should be 0 always.
60 smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID, &flags);
64 header = &stats->ls_cnt_header[idx];
65 percpu_cntr = lprocfs_stats_counter_get(stats, smp_id, idx);
66 percpu_cntr->lc_count++;
68 if (header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
70 * lprocfs_counter_add() can be called in interrupt context,
71 * as memory allocation could trigger memory shrinker call
72 * ldlm_pool_shrink(), which calls lprocfs_counter_add().
77 (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
78 percpu_cntr->lc_sum_irq += amount;
80 percpu_cntr->lc_sum += amount;
82 if (header->lc_config & LPROCFS_CNTR_STDDEV)
83 percpu_cntr->lc_sumsquare += (__s64)amount * amount;
84 if (amount < percpu_cntr->lc_min)
85 percpu_cntr->lc_min = amount;
86 if (amount > percpu_cntr->lc_max)
87 percpu_cntr->lc_max = amount;
89 lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID, &flags);
91 EXPORT_SYMBOL(lprocfs_counter_add);
93 void lprocfs_counter_sub(struct lprocfs_stats *stats, int idx, long amount)
95 struct lprocfs_counter *percpu_cntr;
96 struct lprocfs_counter_header *header;
98 unsigned long flags = 0;
103 LASSERTF(0 <= idx && idx < stats->ls_num,
104 "idx %d, ls_num %hu\n", idx, stats->ls_num);
106 /* With per-client stats, statistics are allocated only for
107 * single CPU area, so the smp_id should be 0 always.
109 smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID, &flags);
113 header = &stats->ls_cnt_header[idx];
114 percpu_cntr = lprocfs_stats_counter_get(stats, smp_id, idx);
115 if (header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
117 * Sometimes we use RCU callbacks to free memory which calls
118 * lprocfs_counter_sub(), and RCU callbacks may execute in
119 * softirq context - right now that's the only case we're in
120 * softirq context here, use separate counter for that.
124 if (in_interrupt() &&
125 (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
126 percpu_cntr->lc_sum_irq -= amount;
128 percpu_cntr->lc_sum -= amount;
130 lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID, &flags);
132 EXPORT_SYMBOL(lprocfs_counter_sub);