Linux-libre 4.15.7-gnu
[librecmc/linux-libre.git] / drivers / staging / lustre / lustre / obdclass / lprocfs_counters.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * GPL HEADER START
4  *
5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 only,
9  * as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License version 2 for more details (a copy is included
15  * in the LICENSE file that accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License
18  * version 2 along with this program; If not, see
19  *
20  * http://www.gnu.org/licenses/gpl-2.0.html
21  *
22  * GPL HEADER END
23  */
24 /*
25  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
26  * Use is subject to license terms.
27  *
28  * Copyright (c) 2012, 2013, Intel Corporation.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/obdclass/lprocfs_counters.c
35  *
36  * Lustre lprocfs counter routines
37  *
38  * Author: Andreas Dilger <andreas.dilger@intel.com>
39  */
40
41 #include <linux/module.h>
42 #include <lprocfs_status.h>
43 #include <obd_support.h>
44
45 void lprocfs_counter_add(struct lprocfs_stats *stats, int idx, long amount)
46 {
47         struct lprocfs_counter          *percpu_cntr;
48         struct lprocfs_counter_header   *header;
49         int                             smp_id;
50         unsigned long                   flags = 0;
51
52         if (!stats)
53                 return;
54
55         LASSERTF(0 <= idx && idx < stats->ls_num,
56                  "idx %d, ls_num %hu\n", idx, stats->ls_num);
57
58         /* With per-client stats, statistics are allocated only for
59          * single CPU area, so the smp_id should be 0 always.
60          */
61         smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID, &flags);
62         if (smp_id < 0)
63                 return;
64
65         header = &stats->ls_cnt_header[idx];
66         percpu_cntr = lprocfs_stats_counter_get(stats, smp_id, idx);
67         percpu_cntr->lc_count++;
68
69         if (header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
70                 /*
71                  * lprocfs_counter_add() can be called in interrupt context,
72                  * as memory allocation could trigger memory shrinker call
73                  * ldlm_pool_shrink(), which calls lprocfs_counter_add().
74                  * LU-1727.
75                  *
76                  */
77                 if (in_interrupt() &&
78                     (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
79                         percpu_cntr->lc_sum_irq += amount;
80                 else
81                         percpu_cntr->lc_sum += amount;
82
83                 if (header->lc_config & LPROCFS_CNTR_STDDEV)
84                         percpu_cntr->lc_sumsquare += (__s64)amount * amount;
85                 if (amount < percpu_cntr->lc_min)
86                         percpu_cntr->lc_min = amount;
87                 if (amount > percpu_cntr->lc_max)
88                         percpu_cntr->lc_max = amount;
89         }
90         lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID, &flags);
91 }
92 EXPORT_SYMBOL(lprocfs_counter_add);
93
94 void lprocfs_counter_sub(struct lprocfs_stats *stats, int idx, long amount)
95 {
96         struct lprocfs_counter          *percpu_cntr;
97         struct lprocfs_counter_header   *header;
98         int                             smp_id;
99         unsigned long                   flags = 0;
100
101         if (!stats)
102                 return;
103
104         LASSERTF(0 <= idx && idx < stats->ls_num,
105                  "idx %d, ls_num %hu\n", idx, stats->ls_num);
106
107         /* With per-client stats, statistics are allocated only for
108          * single CPU area, so the smp_id should be 0 always.
109          */
110         smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID, &flags);
111         if (smp_id < 0)
112                 return;
113
114         header = &stats->ls_cnt_header[idx];
115         percpu_cntr = lprocfs_stats_counter_get(stats, smp_id, idx);
116         if (header->lc_config & LPROCFS_CNTR_AVGMINMAX) {
117                 /*
118                  * Sometimes we use RCU callbacks to free memory which calls
119                  * lprocfs_counter_sub(), and RCU callbacks may execute in
120                  * softirq context - right now that's the only case we're in
121                  * softirq context here, use separate counter for that.
122                  * bz20650.
123                  *
124                  */
125                 if (in_interrupt() &&
126                     (stats->ls_flags & LPROCFS_STATS_FLAG_IRQ_SAFE) != 0)
127                         percpu_cntr->lc_sum_irq -= amount;
128                 else
129                         percpu_cntr->lc_sum -= amount;
130         }
131         lprocfs_stats_unlock(stats, LPROCFS_GET_SMP_ID, &flags);
132 }
133 EXPORT_SYMBOL(lprocfs_counter_sub);