blob: 9711a155bc50071995402518d1ddd56a78b24a2a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/stats.c
3 *
4 * procfs-based user access to generic RPC statistics. The stats files
5 * reside in /proc/net/rpc.
6 *
7 * The read routines assume that the buffer passed in is just big enough.
8 * If you implement an RPC service that has its own stats routine which
9 * appends the generic RPC stats, make sure you don't exceed the PAGE_SIZE
10 * limit.
11 *
12 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
13 */
14
15#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <linux/init.h>
19#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/proc_fs.h>
21#include <linux/seq_file.h>
22#include <linux/sunrpc/clnt.h>
23#include <linux/sunrpc/svcsock.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050024#include <linux/sunrpc/metrics.h>
Trond Myklebust2446ab62012-03-01 17:00:56 -050025#include <linux/rcupdate.h>
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +040026
27#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#define RPCDBG_FACILITY RPCDBG_MISC
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/*
32 * Get RPC client stats
33 */
34static int rpc_proc_show(struct seq_file *seq, void *v) {
35 const struct rpc_stat *statp = seq->private;
36 const struct rpc_program *prog = statp->program;
Chuck Leverea339d42007-10-26 13:32:56 -040037 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070040 "net %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 statp->netcnt,
42 statp->netudpcnt,
43 statp->nettcpcnt,
44 statp->nettcpconn);
45 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070046 "rpc %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 statp->rpccnt,
48 statp->rpcretrans,
49 statp->rpcauthrefresh);
50
51 for (i = 0; i < prog->nrvers; i++) {
52 const struct rpc_version *vers = prog->version[i];
53 if (!vers)
54 continue;
Max Kellermann49e31cb2005-09-06 15:18:03 -070055 seq_printf(seq, "proc%u %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 vers->number, vers->nrprocs);
57 for (j = 0; j < vers->nrprocs; j++)
Max Kellermann49e31cb2005-09-06 15:18:03 -070058 seq_printf(seq, " %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 vers->procs[j].p_count);
60 seq_putc(seq, '\n');
61 }
62 return 0;
63}
64
65static int rpc_proc_open(struct inode *inode, struct file *file)
66{
Al Virod9dda782013-03-31 18:16:14 -040067 return single_open(file, rpc_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Arjan van de Venda7071d2007-02-12 00:55:36 -080070static const struct file_operations rpc_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 .owner = THIS_MODULE,
72 .open = rpc_proc_open,
73 .read = seq_read,
74 .llseek = seq_lseek,
75 .release = single_release,
76};
77
78/*
79 * Get RPC server stats
80 */
81void svc_seq_show(struct seq_file *seq, const struct svc_stat *statp) {
82 const struct svc_program *prog = statp->program;
83 const struct svc_procedure *proc;
84 const struct svc_version *vers;
Chuck Leverea339d42007-10-26 13:32:56 -040085 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070088 "net %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 statp->netcnt,
90 statp->netudpcnt,
91 statp->nettcpcnt,
92 statp->nettcpconn);
93 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070094 "rpc %u %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 statp->rpccnt,
96 statp->rpcbadfmt+statp->rpcbadauth+statp->rpcbadclnt,
97 statp->rpcbadfmt,
98 statp->rpcbadauth,
99 statp->rpcbadclnt);
100
101 for (i = 0; i < prog->pg_nvers; i++) {
102 if (!(vers = prog->pg_vers[i]) || !(proc = vers->vs_proc))
103 continue;
Max Kellermann49e31cb2005-09-06 15:18:03 -0700104 seq_printf(seq, "proc%d %u", i, vers->vs_nproc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 for (j = 0; j < vers->vs_nproc; j++, proc++)
Max Kellermann49e31cb2005-09-06 15:18:03 -0700106 seq_printf(seq, " %u", proc->pc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 seq_putc(seq, '\n');
108 }
109}
Trond Myklebust24c37672008-12-23 16:30:12 -0500110EXPORT_SYMBOL_GPL(svc_seq_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Chuck Lever11c556b2006-03-20 13:44:22 -0500112/**
113 * rpc_alloc_iostats - allocate an rpc_iostats structure
114 * @clnt: RPC program, version, and xprt
115 *
116 */
117struct rpc_iostats *rpc_alloc_iostats(struct rpc_clnt *clnt)
118{
Chuck Leveredef1292014-11-08 20:15:09 -0500119 struct rpc_iostats *stats;
120 int i;
121
122 stats = kcalloc(clnt->cl_maxproc, sizeof(*stats), GFP_KERNEL);
123 if (stats) {
124 for (i = 0; i < clnt->cl_maxproc; i++)
125 spin_lock_init(&stats[i].om_lock);
126 }
127 return stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500128}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400129EXPORT_SYMBOL_GPL(rpc_alloc_iostats);
Chuck Lever11c556b2006-03-20 13:44:22 -0500130
131/**
132 * rpc_free_iostats - release an rpc_iostats structure
133 * @stats: doomed rpc_iostats structure
134 *
135 */
136void rpc_free_iostats(struct rpc_iostats *stats)
137{
138 kfree(stats);
139}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400140EXPORT_SYMBOL_GPL(rpc_free_iostats);
Chuck Lever11c556b2006-03-20 13:44:22 -0500141
142/**
143 * rpc_count_iostats - tally up per-task stats
144 * @task: completed rpc_task
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500145 * @stats: array of stat structures
Chuck Lever11c556b2006-03-20 13:44:22 -0500146 */
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500147void rpc_count_iostats(const struct rpc_task *task, struct rpc_iostats *stats)
Chuck Lever11c556b2006-03-20 13:44:22 -0500148{
149 struct rpc_rqst *req = task->tk_rqstp;
Chuck Lever11c556b2006-03-20 13:44:22 -0500150 struct rpc_iostats *op_metrics;
Chuck Leveredef1292014-11-08 20:15:09 -0500151 ktime_t delta, now;
Chuck Lever11c556b2006-03-20 13:44:22 -0500152
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500153 if (!stats || !req)
Chuck Lever11c556b2006-03-20 13:44:22 -0500154 return;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400155
Chuck Leveredef1292014-11-08 20:15:09 -0500156 now = ktime_get();
Chuck Levercc0175c2006-03-20 13:44:22 -0500157 op_metrics = &stats[task->tk_msg.rpc_proc->p_statidx];
Chuck Lever11c556b2006-03-20 13:44:22 -0500158
Chuck Leveredef1292014-11-08 20:15:09 -0500159 spin_lock(&op_metrics->om_lock);
160
Chuck Lever11c556b2006-03-20 13:44:22 -0500161 op_metrics->om_ops++;
162 op_metrics->om_ntrans += req->rq_ntrans;
163 op_metrics->om_timeouts += task->tk_timeouts;
164
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400165 op_metrics->om_bytes_sent += req->rq_xmit_bytes_sent;
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -0400166 op_metrics->om_bytes_recv += req->rq_reply_bytes_recvd;
Chuck Lever11c556b2006-03-20 13:44:22 -0500167
Chuck Leverff839972010-05-07 13:34:47 -0400168 delta = ktime_sub(req->rq_xtime, task->tk_start);
169 op_metrics->om_queue = ktime_add(op_metrics->om_queue, delta);
Chuck Lever11c556b2006-03-20 13:44:22 -0500170
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400171 op_metrics->om_rtt = ktime_add(op_metrics->om_rtt, req->rq_rtt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500172
Chuck Leveredef1292014-11-08 20:15:09 -0500173 delta = ktime_sub(now, task->tk_start);
Chuck Leverff839972010-05-07 13:34:47 -0400174 op_metrics->om_execute = ktime_add(op_metrics->om_execute, delta);
Chuck Leveredef1292014-11-08 20:15:09 -0500175
176 spin_unlock(&op_metrics->om_lock);
Chuck Lever11c556b2006-03-20 13:44:22 -0500177}
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500178EXPORT_SYMBOL_GPL(rpc_count_iostats);
Chuck Lever11c556b2006-03-20 13:44:22 -0500179
Adrian Bunkec535ce2006-04-18 13:21:50 -0400180static void _print_name(struct seq_file *seq, unsigned int op,
181 struct rpc_procinfo *procs)
Chuck Levercc0175c2006-03-20 13:44:22 -0500182{
183 if (procs[op].p_name)
184 seq_printf(seq, "\t%12s: ", procs[op].p_name);
185 else if (op == 0)
186 seq_printf(seq, "\t NULL: ");
187 else
188 seq_printf(seq, "\t%12u: ", op);
189}
190
Chuck Lever11c556b2006-03-20 13:44:22 -0500191void rpc_print_iostats(struct seq_file *seq, struct rpc_clnt *clnt)
192{
193 struct rpc_iostats *stats = clnt->cl_metrics;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500194 struct rpc_xprt *xprt;
Chuck Lever11c556b2006-03-20 13:44:22 -0500195 unsigned int op, maxproc = clnt->cl_maxproc;
196
197 if (!stats)
198 return;
199
200 seq_printf(seq, "\tRPC iostats version: %s ", RPC_IOSTATS_VERS);
201 seq_printf(seq, "p/v: %u/%u (%s)\n",
Trond Myklebust55909f22013-08-23 11:48:15 -0400202 clnt->cl_prog, clnt->cl_vers, clnt->cl_program->name);
Chuck Lever11c556b2006-03-20 13:44:22 -0500203
Trond Myklebust2446ab62012-03-01 17:00:56 -0500204 rcu_read_lock();
205 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500206 if (xprt)
207 xprt->ops->print_stats(xprt, seq);
Trond Myklebust2446ab62012-03-01 17:00:56 -0500208 rcu_read_unlock();
Chuck Lever11c556b2006-03-20 13:44:22 -0500209
210 seq_printf(seq, "\tper-op statistics\n");
211 for (op = 0; op < maxproc; op++) {
212 struct rpc_iostats *metrics = &stats[op];
Chuck Levercc0175c2006-03-20 13:44:22 -0500213 _print_name(seq, op, clnt->cl_procinfo);
Chuck Lever11c556b2006-03-20 13:44:22 -0500214 seq_printf(seq, "%lu %lu %lu %Lu %Lu %Lu %Lu %Lu\n",
215 metrics->om_ops,
216 metrics->om_ntrans,
217 metrics->om_timeouts,
218 metrics->om_bytes_sent,
219 metrics->om_bytes_recv,
Chuck Leverff839972010-05-07 13:34:47 -0400220 ktime_to_ms(metrics->om_queue),
221 ktime_to_ms(metrics->om_rtt),
222 ktime_to_ms(metrics->om_execute));
Chuck Lever11c556b2006-03-20 13:44:22 -0500223 }
224}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400225EXPORT_SYMBOL_GPL(rpc_print_iostats);
Chuck Lever11c556b2006-03-20 13:44:22 -0500226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/*
228 * Register/unregister RPC proc files
229 */
230static inline struct proc_dir_entry *
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300231do_register(struct net *net, const char *name, void *data,
232 const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400234 struct sunrpc_net *sn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400236 dprintk("RPC: registering /proc/net/rpc/%s\n", name);
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300237 sn = net_generic(net, sunrpc_net_id);
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400238 return proc_create_data(name, 0, sn->proc_net_rpc, fops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241struct proc_dir_entry *
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300242rpc_proc_register(struct net *net, struct rpc_stat *statp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300244 return do_register(net, statp->program->name, statp, &rpc_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400246EXPORT_SYMBOL_GPL(rpc_proc_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248void
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300249rpc_proc_unregister(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400251 struct sunrpc_net *sn;
252
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300253 sn = net_generic(net, sunrpc_net_id);
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400254 remove_proc_entry(name, sn->proc_net_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400256EXPORT_SYMBOL_GPL(rpc_proc_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258struct proc_dir_entry *
Stanislav Kinsbursky246590f2011-12-06 16:42:49 +0300259svc_proc_register(struct net *net, struct svc_stat *statp, const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Stanislav Kinsbursky246590f2011-12-06 16:42:49 +0300261 return do_register(net, statp->program->pg_name, statp, fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
Trond Myklebust24c37672008-12-23 16:30:12 -0500263EXPORT_SYMBOL_GPL(svc_proc_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265void
Stanislav Kinsbursky246590f2011-12-06 16:42:49 +0300266svc_proc_unregister(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400268 struct sunrpc_net *sn;
269
Stanislav Kinsbursky246590f2011-12-06 16:42:49 +0300270 sn = net_generic(net, sunrpc_net_id);
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400271 remove_proc_entry(name, sn->proc_net_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
Trond Myklebust24c37672008-12-23 16:30:12 -0500273EXPORT_SYMBOL_GPL(svc_proc_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400275int rpc_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400277 struct sunrpc_net *sn;
278
Chuck Lever46121cf2007-01-31 12:14:08 -0500279 dprintk("RPC: registering /proc/net/rpc\n");
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400280 sn = net_generic(net, sunrpc_net_id);
281 sn->proc_net_rpc = proc_mkdir("rpc", net->proc_net);
282 if (sn->proc_net_rpc == NULL)
283 return -ENOMEM;
284
285 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400288void rpc_proc_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Chuck Lever46121cf2007-01-31 12:14:08 -0500290 dprintk("RPC: unregistering /proc/net/rpc\n");
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400291 remove_proc_entry("rpc", net->proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293