blob: 74ba7d443dfc06ec7162c2d7b1735bc2bfc67e06 [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>
16
17#include <linux/init.h>
18#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/proc_fs.h>
20#include <linux/seq_file.h>
21#include <linux/sunrpc/clnt.h>
22#include <linux/sunrpc/svcsock.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050023#include <linux/sunrpc/metrics.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#define RPCDBG_FACILITY RPCDBG_MISC
26
27struct proc_dir_entry *proc_net_rpc = NULL;
28
29/*
30 * Get RPC client stats
31 */
32static int rpc_proc_show(struct seq_file *seq, void *v) {
33 const struct rpc_stat *statp = seq->private;
34 const struct rpc_program *prog = statp->program;
35 int i, j;
36
37 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070038 "net %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 statp->netcnt,
40 statp->netudpcnt,
41 statp->nettcpcnt,
42 statp->nettcpconn);
43 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070044 "rpc %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 statp->rpccnt,
46 statp->rpcretrans,
47 statp->rpcauthrefresh);
48
49 for (i = 0; i < prog->nrvers; i++) {
50 const struct rpc_version *vers = prog->version[i];
51 if (!vers)
52 continue;
Max Kellermann49e31cb2005-09-06 15:18:03 -070053 seq_printf(seq, "proc%u %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 vers->number, vers->nrprocs);
55 for (j = 0; j < vers->nrprocs; j++)
Max Kellermann49e31cb2005-09-06 15:18:03 -070056 seq_printf(seq, " %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 vers->procs[j].p_count);
58 seq_putc(seq, '\n');
59 }
60 return 0;
61}
62
63static int rpc_proc_open(struct inode *inode, struct file *file)
64{
65 return single_open(file, rpc_proc_show, PDE(inode)->data);
66}
67
Arjan van de Venda7071d2007-02-12 00:55:36 -080068static const struct file_operations rpc_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 .owner = THIS_MODULE,
70 .open = rpc_proc_open,
71 .read = seq_read,
72 .llseek = seq_lseek,
73 .release = single_release,
74};
75
76/*
77 * Get RPC server stats
78 */
79void svc_seq_show(struct seq_file *seq, const struct svc_stat *statp) {
80 const struct svc_program *prog = statp->program;
81 const struct svc_procedure *proc;
82 const struct svc_version *vers;
83 int i, j;
84
85 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070086 "net %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 statp->netcnt,
88 statp->netudpcnt,
89 statp->nettcpcnt,
90 statp->nettcpconn);
91 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070092 "rpc %u %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 statp->rpccnt,
94 statp->rpcbadfmt+statp->rpcbadauth+statp->rpcbadclnt,
95 statp->rpcbadfmt,
96 statp->rpcbadauth,
97 statp->rpcbadclnt);
98
99 for (i = 0; i < prog->pg_nvers; i++) {
100 if (!(vers = prog->pg_vers[i]) || !(proc = vers->vs_proc))
101 continue;
Max Kellermann49e31cb2005-09-06 15:18:03 -0700102 seq_printf(seq, "proc%d %u", i, vers->vs_nproc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 for (j = 0; j < vers->vs_nproc; j++, proc++)
Max Kellermann49e31cb2005-09-06 15:18:03 -0700104 seq_printf(seq, " %u", proc->pc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 seq_putc(seq, '\n');
106 }
107}
108
Chuck Lever11c556b2006-03-20 13:44:22 -0500109/**
110 * rpc_alloc_iostats - allocate an rpc_iostats structure
111 * @clnt: RPC program, version, and xprt
112 *
113 */
114struct rpc_iostats *rpc_alloc_iostats(struct rpc_clnt *clnt)
115{
Chuck Lever11c556b2006-03-20 13:44:22 -0500116 struct rpc_iostats *new;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700117 new = kcalloc(clnt->cl_maxproc, sizeof(struct rpc_iostats), GFP_KERNEL);
Chuck Lever11c556b2006-03-20 13:44:22 -0500118 return new;
119}
120EXPORT_SYMBOL(rpc_alloc_iostats);
121
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}
131EXPORT_SYMBOL(rpc_free_iostats);
132
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;
142 struct rpc_iostats *stats = task->tk_client->cl_metrics;
143 struct rpc_iostats *op_metrics;
144 long rtt, execute, queue;
145
146 if (!stats || !req)
147 return;
Chuck Levercc0175c2006-03-20 13:44:22 -0500148 op_metrics = &stats[task->tk_msg.rpc_proc->p_statidx];
Chuck Lever11c556b2006-03-20 13:44:22 -0500149
150 op_metrics->om_ops++;
151 op_metrics->om_ntrans += req->rq_ntrans;
152 op_metrics->om_timeouts += task->tk_timeouts;
153
154 op_metrics->om_bytes_sent += task->tk_bytes_sent;
155 op_metrics->om_bytes_recv += req->rq_received;
156
157 queue = (long)req->rq_xtime - task->tk_start;
158 if (queue < 0)
159 queue = -queue;
160 op_metrics->om_queue += queue;
161
162 rtt = task->tk_rtt;
163 if (rtt < 0)
164 rtt = -rtt;
165 op_metrics->om_rtt += rtt;
166
167 execute = (long)jiffies - task->tk_start;
168 if (execute < 0)
169 execute = -execute;
170 op_metrics->om_execute += execute;
171}
172
Adrian Bunkec535ce152006-04-18 13:21:50 -0400173static void _print_name(struct seq_file *seq, unsigned int op,
174 struct rpc_procinfo *procs)
Chuck Levercc0175c2006-03-20 13:44:22 -0500175{
176 if (procs[op].p_name)
177 seq_printf(seq, "\t%12s: ", procs[op].p_name);
178 else if (op == 0)
179 seq_printf(seq, "\t NULL: ");
180 else
181 seq_printf(seq, "\t%12u: ", op);
182}
183
Chuck Lever5eb53f42006-03-20 13:44:42 -0500184#define MILLISECS_PER_JIFFY (1000 / HZ)
Chuck Lever11c556b2006-03-20 13:44:22 -0500185
186void rpc_print_iostats(struct seq_file *seq, struct rpc_clnt *clnt)
187{
188 struct rpc_iostats *stats = clnt->cl_metrics;
189 struct rpc_xprt *xprt = clnt->cl_xprt;
190 unsigned int op, maxproc = clnt->cl_maxproc;
191
192 if (!stats)
193 return;
194
195 seq_printf(seq, "\tRPC iostats version: %s ", RPC_IOSTATS_VERS);
196 seq_printf(seq, "p/v: %u/%u (%s)\n",
197 clnt->cl_prog, clnt->cl_vers, clnt->cl_protname);
198
199 if (xprt)
200 xprt->ops->print_stats(xprt, seq);
201
202 seq_printf(seq, "\tper-op statistics\n");
203 for (op = 0; op < maxproc; op++) {
204 struct rpc_iostats *metrics = &stats[op];
Chuck Levercc0175c2006-03-20 13:44:22 -0500205 _print_name(seq, op, clnt->cl_procinfo);
Chuck Lever11c556b2006-03-20 13:44:22 -0500206 seq_printf(seq, "%lu %lu %lu %Lu %Lu %Lu %Lu %Lu\n",
207 metrics->om_ops,
208 metrics->om_ntrans,
209 metrics->om_timeouts,
210 metrics->om_bytes_sent,
211 metrics->om_bytes_recv,
212 metrics->om_queue * MILLISECS_PER_JIFFY,
213 metrics->om_rtt * MILLISECS_PER_JIFFY,
214 metrics->om_execute * MILLISECS_PER_JIFFY);
215 }
216}
217EXPORT_SYMBOL(rpc_print_iostats);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/*
220 * Register/unregister RPC proc files
221 */
222static inline struct proc_dir_entry *
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800223do_register(const char *name, void *data, const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct proc_dir_entry *ent;
226
227 rpc_proc_init();
Chuck Lever46121cf2007-01-31 12:14:08 -0500228 dprintk("RPC: registering /proc/net/rpc/%s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 ent = create_proc_entry(name, 0, proc_net_rpc);
231 if (ent) {
232 ent->proc_fops = fops;
233 ent->data = data;
234 }
235 return ent;
236}
237
238struct proc_dir_entry *
239rpc_proc_register(struct rpc_stat *statp)
240{
241 return do_register(statp->program->name, statp, &rpc_proc_fops);
242}
243
244void
245rpc_proc_unregister(const char *name)
246{
247 remove_proc_entry(name, proc_net_rpc);
248}
249
250struct proc_dir_entry *
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800251svc_proc_register(struct svc_stat *statp, const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 return do_register(statp->program->pg_name, statp, fops);
254}
255
256void
257svc_proc_unregister(const char *name)
258{
259 remove_proc_entry(name, proc_net_rpc);
260}
261
262void
263rpc_proc_init(void)
264{
Chuck Lever46121cf2007-01-31 12:14:08 -0500265 dprintk("RPC: registering /proc/net/rpc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!proc_net_rpc) {
267 struct proc_dir_entry *ent;
268 ent = proc_mkdir("rpc", proc_net);
269 if (ent) {
270 ent->owner = THIS_MODULE;
271 proc_net_rpc = ent;
272 }
273 }
274}
275
276void
277rpc_proc_exit(void)
278{
Chuck Lever46121cf2007-01-31 12:14:08 -0500279 dprintk("RPC: unregistering /proc/net/rpc\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (proc_net_rpc) {
281 proc_net_rpc = NULL;
282 remove_proc_entry("net/rpc", NULL);
283 }
284}
285