xref: /openbmc/linux/fs/nfsd/stats.c (revision 7e043a80)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * procfs-based user access to knfsd statistics
4  *
5  * /proc/net/rpc/nfsd
6  *
7  * Format:
8  *	rc <hits> <misses> <nocache>
9  *			Statistsics for the reply cache
10  *	fh <stale> <deprecated filehandle cache stats>
11  *			statistics for filehandle lookup
12  *	io <bytes-read> <bytes-written>
13  *			statistics for IO throughput
14  *	th <threads> <deprecated thread usage histogram stats>
15  *			number of threads
16  *	ra <deprecated ra-cache stats>
17  *
18  *	plus generic RPC stats (see net/sunrpc/stats.c)
19  *
20  * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
21  */
22 
23 #include <linux/seq_file.h>
24 #include <linux/module.h>
25 #include <linux/sunrpc/stats.h>
26 #include <net/net_namespace.h>
27 
28 #include "nfsd.h"
29 
30 struct nfsd_stats	nfsdstats;
31 struct svc_stat		nfsd_svcstats = {
32 	.program	= &nfsd_program,
33 };
34 
35 static int nfsd_show(struct seq_file *seq, void *v)
36 {
37 	int i;
38 
39 	seq_printf(seq, "rc %lld %lld %lld\nfh %lld 0 0 0 0\nio %lld %lld\n",
40 		   percpu_counter_sum_positive(&nfsdstats.counter[NFSD_STATS_RC_HITS]),
41 		   percpu_counter_sum_positive(&nfsdstats.counter[NFSD_STATS_RC_MISSES]),
42 		   percpu_counter_sum_positive(&nfsdstats.counter[NFSD_STATS_RC_NOCACHE]),
43 		   percpu_counter_sum_positive(&nfsdstats.counter[NFSD_STATS_FH_STALE]),
44 		   percpu_counter_sum_positive(&nfsdstats.counter[NFSD_STATS_IO_READ]),
45 		   percpu_counter_sum_positive(&nfsdstats.counter[NFSD_STATS_IO_WRITE]));
46 
47 	/* thread usage: */
48 	seq_printf(seq, "th %u 0", atomic_read(&nfsdstats.th_cnt));
49 
50 	/* deprecated thread usage histogram stats */
51 	for (i = 0; i < 10; i++)
52 		seq_puts(seq, " 0.000");
53 
54 	/* deprecated ra-cache stats */
55 	seq_puts(seq, "\nra 0 0 0 0 0 0 0 0 0 0 0 0\n");
56 
57 	/* show my rpc info */
58 	svc_seq_show(seq, &nfsd_svcstats);
59 
60 #ifdef CONFIG_NFSD_V4
61 	/* Show count for individual nfsv4 operations */
62 	/* Writing operation numbers 0 1 2 also for maintaining uniformity */
63 	seq_printf(seq,"proc4ops %u", LAST_NFS4_OP + 1);
64 	for (i = 0; i <= LAST_NFS4_OP; i++) {
65 		seq_printf(seq, " %lld",
66 			   percpu_counter_sum_positive(&nfsdstats.counter[NFSD_STATS_NFS4_OP(i)]));
67 	}
68 
69 	seq_putc(seq, '\n');
70 #endif
71 
72 	return 0;
73 }
74 
75 DEFINE_PROC_SHOW_ATTRIBUTE(nfsd);
76 
77 int nfsd_percpu_counters_init(struct percpu_counter counters[], int num)
78 {
79 	int i, err = 0;
80 
81 	for (i = 0; !err && i < num; i++)
82 		err = percpu_counter_init(&counters[i], 0, GFP_KERNEL);
83 
84 	if (!err)
85 		return 0;
86 
87 	for (; i > 0; i--)
88 		percpu_counter_destroy(&counters[i-1]);
89 
90 	return err;
91 }
92 
93 void nfsd_percpu_counters_reset(struct percpu_counter counters[], int num)
94 {
95 	int i;
96 
97 	for (i = 0; i < num; i++)
98 		percpu_counter_set(&counters[i], 0);
99 }
100 
101 void nfsd_percpu_counters_destroy(struct percpu_counter counters[], int num)
102 {
103 	int i;
104 
105 	for (i = 0; i < num; i++)
106 		percpu_counter_destroy(&counters[i]);
107 }
108 
109 static int nfsd_stat_counters_init(void)
110 {
111 	return nfsd_percpu_counters_init(nfsdstats.counter, NFSD_STATS_COUNTERS_NUM);
112 }
113 
114 static void nfsd_stat_counters_destroy(void)
115 {
116 	nfsd_percpu_counters_destroy(nfsdstats.counter, NFSD_STATS_COUNTERS_NUM);
117 }
118 
119 int nfsd_stat_init(void)
120 {
121 	int err;
122 
123 	err = nfsd_stat_counters_init();
124 	if (err)
125 		return err;
126 
127 	svc_proc_register(&init_net, &nfsd_svcstats, &nfsd_proc_ops);
128 
129 	return 0;
130 }
131 
132 void nfsd_stat_shutdown(void)
133 {
134 	nfsd_stat_counters_destroy();
135 	svc_proc_unregister(&init_net, "nfsd");
136 }
137