blob: 53746793eca29b67d1dc9fb2f217c6c259ea59f7 [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>
19#include <linux/sched.h>
20#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#define RPCDBG_FACILITY RPCDBG_MISC
27
28struct proc_dir_entry *proc_net_rpc = NULL;
29
30/*
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;
36 int i, j;
37
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
69static struct file_operations rpc_proc_fops = {
70 .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;
84 int i, j;
85
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}
109
Chuck Lever11c556b2006-03-20 13:44:22 -0500110/**
111 * rpc_alloc_iostats - allocate an rpc_iostats structure
112 * @clnt: RPC program, version, and xprt
113 *
114 */
115struct rpc_iostats *rpc_alloc_iostats(struct rpc_clnt *clnt)
116{
117 unsigned int ops = clnt->cl_maxproc;
118 size_t size = ops * sizeof(struct rpc_iostats);
119 struct rpc_iostats *new;
120
121 new = kmalloc(size, GFP_KERNEL);
122 if (new)
123 memset(new, 0 , size);
124 return new;
125}
126EXPORT_SYMBOL(rpc_alloc_iostats);
127
128/**
129 * rpc_free_iostats - release an rpc_iostats structure
130 * @stats: doomed rpc_iostats structure
131 *
132 */
133void rpc_free_iostats(struct rpc_iostats *stats)
134{
135 kfree(stats);
136}
137EXPORT_SYMBOL(rpc_free_iostats);
138
139/**
140 * rpc_count_iostats - tally up per-task stats
141 * @task: completed rpc_task
142 *
143 * Relies on the caller for serialization.
144 */
145void rpc_count_iostats(struct rpc_task *task)
146{
147 struct rpc_rqst *req = task->tk_rqstp;
148 struct rpc_iostats *stats = task->tk_client->cl_metrics;
149 struct rpc_iostats *op_metrics;
150 long rtt, execute, queue;
151
152 if (!stats || !req)
153 return;
Chuck Levercc0175c2006-03-20 13:44:22 -0500154 op_metrics = &stats[task->tk_msg.rpc_proc->p_statidx];
Chuck Lever11c556b2006-03-20 13:44:22 -0500155
156 op_metrics->om_ops++;
157 op_metrics->om_ntrans += req->rq_ntrans;
158 op_metrics->om_timeouts += task->tk_timeouts;
159
160 op_metrics->om_bytes_sent += task->tk_bytes_sent;
161 op_metrics->om_bytes_recv += req->rq_received;
162
163 queue = (long)req->rq_xtime - task->tk_start;
164 if (queue < 0)
165 queue = -queue;
166 op_metrics->om_queue += queue;
167
168 rtt = task->tk_rtt;
169 if (rtt < 0)
170 rtt = -rtt;
171 op_metrics->om_rtt += rtt;
172
173 execute = (long)jiffies - task->tk_start;
174 if (execute < 0)
175 execute = -execute;
176 op_metrics->om_execute += execute;
177}
178
Chuck Levercc0175c2006-03-20 13:44:22 -0500179void _print_name(struct seq_file *seq, unsigned int op, struct rpc_procinfo *procs)
180{
181 if (procs[op].p_name)
182 seq_printf(seq, "\t%12s: ", procs[op].p_name);
183 else if (op == 0)
184 seq_printf(seq, "\t NULL: ");
185 else
186 seq_printf(seq, "\t%12u: ", op);
187}
188
Chuck Lever11c556b2006-03-20 13:44:22 -0500189#define MILLISECS_PER_JIFFY (1000UL / HZ)
190
191void rpc_print_iostats(struct seq_file *seq, struct rpc_clnt *clnt)
192{
193 struct rpc_iostats *stats = clnt->cl_metrics;
194 struct rpc_xprt *xprt = clnt->cl_xprt;
195 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",
202 clnt->cl_prog, clnt->cl_vers, clnt->cl_protname);
203
204 if (xprt)
205 xprt->ops->print_stats(xprt, seq);
206
207 seq_printf(seq, "\tper-op statistics\n");
208 for (op = 0; op < maxproc; op++) {
209 struct rpc_iostats *metrics = &stats[op];
Chuck Levercc0175c2006-03-20 13:44:22 -0500210 _print_name(seq, op, clnt->cl_procinfo);
Chuck Lever11c556b2006-03-20 13:44:22 -0500211 seq_printf(seq, "%lu %lu %lu %Lu %Lu %Lu %Lu %Lu\n",
212 metrics->om_ops,
213 metrics->om_ntrans,
214 metrics->om_timeouts,
215 metrics->om_bytes_sent,
216 metrics->om_bytes_recv,
217 metrics->om_queue * MILLISECS_PER_JIFFY,
218 metrics->om_rtt * MILLISECS_PER_JIFFY,
219 metrics->om_execute * MILLISECS_PER_JIFFY);
220 }
221}
222EXPORT_SYMBOL(rpc_print_iostats);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/*
225 * Register/unregister RPC proc files
226 */
227static inline struct proc_dir_entry *
228do_register(const char *name, void *data, struct file_operations *fops)
229{
230 struct proc_dir_entry *ent;
231
232 rpc_proc_init();
233 dprintk("RPC: registering /proc/net/rpc/%s\n", name);
234
235 ent = create_proc_entry(name, 0, proc_net_rpc);
236 if (ent) {
237 ent->proc_fops = fops;
238 ent->data = data;
239 }
240 return ent;
241}
242
243struct proc_dir_entry *
244rpc_proc_register(struct rpc_stat *statp)
245{
246 return do_register(statp->program->name, statp, &rpc_proc_fops);
247}
248
249void
250rpc_proc_unregister(const char *name)
251{
252 remove_proc_entry(name, proc_net_rpc);
253}
254
255struct proc_dir_entry *
256svc_proc_register(struct svc_stat *statp, struct file_operations *fops)
257{
258 return do_register(statp->program->pg_name, statp, fops);
259}
260
261void
262svc_proc_unregister(const char *name)
263{
264 remove_proc_entry(name, proc_net_rpc);
265}
266
267void
268rpc_proc_init(void)
269{
270 dprintk("RPC: registering /proc/net/rpc\n");
271 if (!proc_net_rpc) {
272 struct proc_dir_entry *ent;
273 ent = proc_mkdir("rpc", proc_net);
274 if (ent) {
275 ent->owner = THIS_MODULE;
276 proc_net_rpc = ent;
277 }
278 }
279}
280
281void
282rpc_proc_exit(void)
283{
284 dprintk("RPC: unregistering /proc/net/rpc\n");
285 if (proc_net_rpc) {
286 proc_net_rpc = NULL;
287 remove_proc_entry("net/rpc", NULL);
288 }
289}
290