blob: 80df89d957ba02dce1b977ec19f96b51d2c657b0 [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>
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +040025
26#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#define RPCDBG_FACILITY RPCDBG_MISC
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
31 * Get RPC client stats
32 */
33static int rpc_proc_show(struct seq_file *seq, void *v) {
34 const struct rpc_stat *statp = seq->private;
35 const struct rpc_program *prog = statp->program;
Chuck Leverea339d42007-10-26 13:32:56 -040036 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070039 "net %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 statp->netcnt,
41 statp->netudpcnt,
42 statp->nettcpcnt,
43 statp->nettcpconn);
44 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070045 "rpc %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 statp->rpccnt,
47 statp->rpcretrans,
48 statp->rpcauthrefresh);
49
50 for (i = 0; i < prog->nrvers; i++) {
51 const struct rpc_version *vers = prog->version[i];
52 if (!vers)
53 continue;
Max Kellermann49e31cb2005-09-06 15:18:03 -070054 seq_printf(seq, "proc%u %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 vers->number, vers->nrprocs);
56 for (j = 0; j < vers->nrprocs; j++)
Max Kellermann49e31cb2005-09-06 15:18:03 -070057 seq_printf(seq, " %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 vers->procs[j].p_count);
59 seq_putc(seq, '\n');
60 }
61 return 0;
62}
63
64static int rpc_proc_open(struct inode *inode, struct file *file)
65{
66 return single_open(file, rpc_proc_show, PDE(inode)->data);
67}
68
Arjan van de Venda7071d2007-02-12 00:55:36 -080069static const struct file_operations rpc_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 .owner = THIS_MODULE,
71 .open = rpc_proc_open,
72 .read = seq_read,
73 .llseek = seq_lseek,
74 .release = single_release,
75};
76
77/*
78 * Get RPC server stats
79 */
80void svc_seq_show(struct seq_file *seq, const struct svc_stat *statp) {
81 const struct svc_program *prog = statp->program;
82 const struct svc_procedure *proc;
83 const struct svc_version *vers;
Chuck Leverea339d42007-10-26 13:32:56 -040084 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070087 "net %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 statp->netcnt,
89 statp->netudpcnt,
90 statp->nettcpcnt,
91 statp->nettcpconn);
92 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070093 "rpc %u %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 statp->rpccnt,
95 statp->rpcbadfmt+statp->rpcbadauth+statp->rpcbadclnt,
96 statp->rpcbadfmt,
97 statp->rpcbadauth,
98 statp->rpcbadclnt);
99
100 for (i = 0; i < prog->pg_nvers; i++) {
101 if (!(vers = prog->pg_vers[i]) || !(proc = vers->vs_proc))
102 continue;
Max Kellermann49e31cb2005-09-06 15:18:03 -0700103 seq_printf(seq, "proc%d %u", i, vers->vs_nproc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 for (j = 0; j < vers->vs_nproc; j++, proc++)
Max Kellermann49e31cb2005-09-06 15:18:03 -0700105 seq_printf(seq, " %u", proc->pc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 seq_putc(seq, '\n');
107 }
108}
Trond Myklebust24c37672008-12-23 16:30:12 -0500109EXPORT_SYMBOL_GPL(svc_seq_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Chuck Lever11c556b2006-03-20 13:44:22 -0500111/**
112 * rpc_alloc_iostats - allocate an rpc_iostats structure
113 * @clnt: RPC program, version, and xprt
114 *
115 */
116struct rpc_iostats *rpc_alloc_iostats(struct rpc_clnt *clnt)
117{
Jesper Juhl94f58df2010-11-07 22:11:34 +0100118 return kcalloc(clnt->cl_maxproc, sizeof(struct rpc_iostats), GFP_KERNEL);
Chuck Lever11c556b2006-03-20 13:44:22 -0500119}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400120EXPORT_SYMBOL_GPL(rpc_alloc_iostats);
Chuck Lever11c556b2006-03-20 13:44:22 -0500121
122/**
123 * rpc_free_iostats - release an rpc_iostats structure
124 * @stats: doomed rpc_iostats structure
125 *
126 */
127void rpc_free_iostats(struct rpc_iostats *stats)
128{
129 kfree(stats);
130}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400131EXPORT_SYMBOL_GPL(rpc_free_iostats);
Chuck Lever11c556b2006-03-20 13:44:22 -0500132
133/**
134 * rpc_count_iostats - tally up per-task stats
135 * @task: completed rpc_task
136 *
137 * Relies on the caller for serialization.
138 */
139void rpc_count_iostats(struct rpc_task *task)
140{
141 struct rpc_rqst *req = task->tk_rqstp;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400142 struct rpc_iostats *stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500143 struct rpc_iostats *op_metrics;
Chuck Leverff839972010-05-07 13:34:47 -0400144 ktime_t delta;
Chuck Lever11c556b2006-03-20 13:44:22 -0500145
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400146 if (!task->tk_client || !task->tk_client->cl_metrics || !req)
Chuck Lever11c556b2006-03-20 13:44:22 -0500147 return;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400148
149 stats = task->tk_client->cl_metrics;
Chuck Levercc0175c2006-03-20 13:44:22 -0500150 op_metrics = &stats[task->tk_msg.rpc_proc->p_statidx];
Chuck Lever11c556b2006-03-20 13:44:22 -0500151
152 op_metrics->om_ops++;
153 op_metrics->om_ntrans += req->rq_ntrans;
154 op_metrics->om_timeouts += task->tk_timeouts;
155
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400156 op_metrics->om_bytes_sent += req->rq_xmit_bytes_sent;
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -0400157 op_metrics->om_bytes_recv += req->rq_reply_bytes_recvd;
Chuck Lever11c556b2006-03-20 13:44:22 -0500158
Chuck Leverff839972010-05-07 13:34:47 -0400159 delta = ktime_sub(req->rq_xtime, task->tk_start);
160 op_metrics->om_queue = ktime_add(op_metrics->om_queue, delta);
Chuck Lever11c556b2006-03-20 13:44:22 -0500161
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400162 op_metrics->om_rtt = ktime_add(op_metrics->om_rtt, req->rq_rtt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500163
Chuck Leverff839972010-05-07 13:34:47 -0400164 delta = ktime_sub(ktime_get(), task->tk_start);
165 op_metrics->om_execute = ktime_add(op_metrics->om_execute, delta);
Chuck Lever11c556b2006-03-20 13:44:22 -0500166}
167
Adrian Bunkec535ce2006-04-18 13:21:50 -0400168static void _print_name(struct seq_file *seq, unsigned int op,
169 struct rpc_procinfo *procs)
Chuck Levercc0175c2006-03-20 13:44:22 -0500170{
171 if (procs[op].p_name)
172 seq_printf(seq, "\t%12s: ", procs[op].p_name);
173 else if (op == 0)
174 seq_printf(seq, "\t NULL: ");
175 else
176 seq_printf(seq, "\t%12u: ", op);
177}
178
Chuck Lever11c556b2006-03-20 13:44:22 -0500179void rpc_print_iostats(struct seq_file *seq, struct rpc_clnt *clnt)
180{
181 struct rpc_iostats *stats = clnt->cl_metrics;
182 struct rpc_xprt *xprt = clnt->cl_xprt;
183 unsigned int op, maxproc = clnt->cl_maxproc;
184
185 if (!stats)
186 return;
187
188 seq_printf(seq, "\tRPC iostats version: %s ", RPC_IOSTATS_VERS);
189 seq_printf(seq, "p/v: %u/%u (%s)\n",
190 clnt->cl_prog, clnt->cl_vers, clnt->cl_protname);
191
192 if (xprt)
193 xprt->ops->print_stats(xprt, seq);
194
195 seq_printf(seq, "\tper-op statistics\n");
196 for (op = 0; op < maxproc; op++) {
197 struct rpc_iostats *metrics = &stats[op];
Chuck Levercc0175c2006-03-20 13:44:22 -0500198 _print_name(seq, op, clnt->cl_procinfo);
Chuck Lever11c556b2006-03-20 13:44:22 -0500199 seq_printf(seq, "%lu %lu %lu %Lu %Lu %Lu %Lu %Lu\n",
200 metrics->om_ops,
201 metrics->om_ntrans,
202 metrics->om_timeouts,
203 metrics->om_bytes_sent,
204 metrics->om_bytes_recv,
Chuck Leverff839972010-05-07 13:34:47 -0400205 ktime_to_ms(metrics->om_queue),
206 ktime_to_ms(metrics->om_rtt),
207 ktime_to_ms(metrics->om_execute));
Chuck Lever11c556b2006-03-20 13:44:22 -0500208 }
209}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400210EXPORT_SYMBOL_GPL(rpc_print_iostats);
Chuck Lever11c556b2006-03-20 13:44:22 -0500211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*
213 * Register/unregister RPC proc files
214 */
215static inline struct proc_dir_entry *
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800216do_register(const char *name, void *data, const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400218 struct sunrpc_net *sn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400220 dprintk("RPC: registering /proc/net/rpc/%s\n", name);
221 sn = net_generic(&init_net, sunrpc_net_id);
222 return proc_create_data(name, 0, sn->proc_net_rpc, fops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225struct proc_dir_entry *
226rpc_proc_register(struct rpc_stat *statp)
227{
228 return do_register(statp->program->name, statp, &rpc_proc_fops);
229}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400230EXPORT_SYMBOL_GPL(rpc_proc_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232void
233rpc_proc_unregister(const char *name)
234{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400235 struct sunrpc_net *sn;
236
237 sn = net_generic(&init_net, sunrpc_net_id);
238 remove_proc_entry(name, sn->proc_net_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400240EXPORT_SYMBOL_GPL(rpc_proc_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242struct proc_dir_entry *
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800243svc_proc_register(struct svc_stat *statp, const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 return do_register(statp->program->pg_name, statp, fops);
246}
Trond Myklebust24c37672008-12-23 16:30:12 -0500247EXPORT_SYMBOL_GPL(svc_proc_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249void
250svc_proc_unregister(const char *name)
251{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400252 struct sunrpc_net *sn;
253
254 sn = net_generic(&init_net, sunrpc_net_id);
255 remove_proc_entry(name, sn->proc_net_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
Trond Myklebust24c37672008-12-23 16:30:12 -0500257EXPORT_SYMBOL_GPL(svc_proc_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400259int rpc_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400261 struct sunrpc_net *sn;
262
Chuck Lever46121cf2007-01-31 12:14:08 -0500263 dprintk("RPC: registering /proc/net/rpc\n");
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400264 sn = net_generic(net, sunrpc_net_id);
265 sn->proc_net_rpc = proc_mkdir("rpc", net->proc_net);
266 if (sn->proc_net_rpc == NULL)
267 return -ENOMEM;
268
269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400272void rpc_proc_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Chuck Lever46121cf2007-01-31 12:14:08 -0500274 dprintk("RPC: unregistering /proc/net/rpc\n");
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400275 remove_proc_entry("rpc", net->proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277