blob: f68aa46c9dd7125bee03506a9a3e4914939770ae [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
Chuck Lever40bf7eb2018-03-16 10:33:49 -040027#include <trace/events/sunrpc.h>
28
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +040029#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#define RPCDBG_FACILITY RPCDBG_MISC
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
34 * Get RPC client stats
35 */
36static int rpc_proc_show(struct seq_file *seq, void *v) {
37 const struct rpc_stat *statp = seq->private;
38 const struct rpc_program *prog = statp->program;
Chuck Leverea339d42007-10-26 13:32:56 -040039 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070042 "net %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 statp->netcnt,
44 statp->netudpcnt,
45 statp->nettcpcnt,
46 statp->nettcpconn);
47 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070048 "rpc %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 statp->rpccnt,
50 statp->rpcretrans,
51 statp->rpcauthrefresh);
52
53 for (i = 0; i < prog->nrvers; i++) {
54 const struct rpc_version *vers = prog->version[i];
55 if (!vers)
56 continue;
Max Kellermann49e31cb2005-09-06 15:18:03 -070057 seq_printf(seq, "proc%u %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 vers->number, vers->nrprocs);
59 for (j = 0; j < vers->nrprocs; j++)
Christoph Hellwig1c5876d2017-05-08 23:27:10 +020060 seq_printf(seq, " %u", vers->counts[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 seq_putc(seq, '\n');
62 }
63 return 0;
64}
65
66static int rpc_proc_open(struct inode *inode, struct file *file)
67{
Al Virod9dda782013-03-31 18:16:14 -040068 return single_open(file, rpc_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
Arjan van de Venda7071d2007-02-12 00:55:36 -080071static const struct file_operations rpc_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 .owner = THIS_MODULE,
73 .open = rpc_proc_open,
74 .read = seq_read,
75 .llseek = seq_lseek,
76 .release = single_release,
77};
78
79/*
80 * Get RPC server stats
81 */
Christoph Hellwig7fd38af2017-05-08 23:40:27 +020082void svc_seq_show(struct seq_file *seq, const struct svc_stat *statp)
83{
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 const struct svc_program *prog = statp->program;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 const struct svc_version *vers;
Chuck Leverea339d42007-10-26 13:32:56 -040086 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070089 "net %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 statp->netcnt,
91 statp->netudpcnt,
92 statp->nettcpcnt,
93 statp->nettcpconn);
94 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070095 "rpc %u %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 statp->rpccnt,
97 statp->rpcbadfmt+statp->rpcbadauth+statp->rpcbadclnt,
98 statp->rpcbadfmt,
99 statp->rpcbadauth,
100 statp->rpcbadclnt);
101
102 for (i = 0; i < prog->pg_nvers; i++) {
Christoph Hellwig7fd38af2017-05-08 23:40:27 +0200103 vers = prog->pg_vers[i];
104 if (!vers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 continue;
Max Kellermann49e31cb2005-09-06 15:18:03 -0700106 seq_printf(seq, "proc%d %u", i, vers->vs_nproc);
Christoph Hellwig7fd38af2017-05-08 23:40:27 +0200107 for (j = 0; j < vers->vs_nproc; j++)
108 seq_printf(seq, " %u", vers->vs_count[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 seq_putc(seq, '\n');
110 }
111}
Trond Myklebust24c37672008-12-23 16:30:12 -0500112EXPORT_SYMBOL_GPL(svc_seq_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Chuck Lever11c556b2006-03-20 13:44:22 -0500114/**
115 * rpc_alloc_iostats - allocate an rpc_iostats structure
116 * @clnt: RPC program, version, and xprt
117 *
118 */
119struct rpc_iostats *rpc_alloc_iostats(struct rpc_clnt *clnt)
120{
Chuck Leveredef1292014-11-08 20:15:09 -0500121 struct rpc_iostats *stats;
122 int i;
123
124 stats = kcalloc(clnt->cl_maxproc, sizeof(*stats), GFP_KERNEL);
125 if (stats) {
126 for (i = 0; i < clnt->cl_maxproc; i++)
127 spin_lock_init(&stats[i].om_lock);
128 }
129 return stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500130}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400131EXPORT_SYMBOL_GPL(rpc_alloc_iostats);
Chuck Lever11c556b2006-03-20 13:44:22 -0500132
133/**
134 * rpc_free_iostats - release an rpc_iostats structure
135 * @stats: doomed rpc_iostats structure
136 *
137 */
138void rpc_free_iostats(struct rpc_iostats *stats)
139{
140 kfree(stats);
141}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400142EXPORT_SYMBOL_GPL(rpc_free_iostats);
Chuck Lever11c556b2006-03-20 13:44:22 -0500143
144/**
Weston Andros Adamson840210f2014-06-24 10:59:52 -0400145 * rpc_count_iostats_metrics - tally up per-task stats
Chuck Lever11c556b2006-03-20 13:44:22 -0500146 * @task: completed rpc_task
Weston Andros Adamson840210f2014-06-24 10:59:52 -0400147 * @op_metrics: stat structure for OP that will accumulate stats from @task
Chuck Lever11c556b2006-03-20 13:44:22 -0500148 */
Weston Andros Adamson840210f2014-06-24 10:59:52 -0400149void rpc_count_iostats_metrics(const struct rpc_task *task,
150 struct rpc_iostats *op_metrics)
Chuck Lever11c556b2006-03-20 13:44:22 -0500151{
152 struct rpc_rqst *req = task->tk_rqstp;
Chuck Lever40bf7eb2018-03-16 10:33:49 -0400153 ktime_t backlog, execute, now;
Chuck Lever11c556b2006-03-20 13:44:22 -0500154
Weston Andros Adamson840210f2014-06-24 10:59:52 -0400155 if (!op_metrics || !req)
Chuck Lever11c556b2006-03-20 13:44:22 -0500156 return;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400157
Chuck Leveredef1292014-11-08 20:15:09 -0500158 now = ktime_get();
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++;
Chuck Leverae095312016-11-29 10:52:32 -0500162 /* kernel API: om_ops must never become larger than om_ntrans */
163 op_metrics->om_ntrans += max(req->rq_ntrans, 1);
Chuck Lever11c556b2006-03-20 13:44:22 -0500164 op_metrics->om_timeouts += task->tk_timeouts;
165
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400166 op_metrics->om_bytes_sent += req->rq_xmit_bytes_sent;
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -0400167 op_metrics->om_bytes_recv += req->rq_reply_bytes_recvd;
Chuck Lever11c556b2006-03-20 13:44:22 -0500168
Chuck Lever40bf7eb2018-03-16 10:33:49 -0400169 backlog = 0;
Chuck Leverae095312016-11-29 10:52:32 -0500170 if (ktime_to_ns(req->rq_xtime)) {
Chuck Lever40bf7eb2018-03-16 10:33:49 -0400171 backlog = ktime_sub(req->rq_xtime, task->tk_start);
172 op_metrics->om_queue = ktime_add(op_metrics->om_queue, backlog);
Chuck Leverae095312016-11-29 10:52:32 -0500173 }
Chuck Lever40bf7eb2018-03-16 10:33:49 -0400174
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400175 op_metrics->om_rtt = ktime_add(op_metrics->om_rtt, req->rq_rtt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500176
Chuck Lever40bf7eb2018-03-16 10:33:49 -0400177 execute = ktime_sub(now, task->tk_start);
178 op_metrics->om_execute = ktime_add(op_metrics->om_execute, execute);
Chuck Leveredef1292014-11-08 20:15:09 -0500179
180 spin_unlock(&op_metrics->om_lock);
Chuck Lever40bf7eb2018-03-16 10:33:49 -0400181
182 trace_rpc_stats_latency(req->rq_task, backlog, req->rq_rtt, execute);
Chuck Lever11c556b2006-03-20 13:44:22 -0500183}
Weston Andros Adamson840210f2014-06-24 10:59:52 -0400184EXPORT_SYMBOL_GPL(rpc_count_iostats_metrics);
185
186/**
187 * rpc_count_iostats - tally up per-task stats
188 * @task: completed rpc_task
189 * @stats: array of stat structures
190 *
191 * Uses the statidx from @task
192 */
193void rpc_count_iostats(const struct rpc_task *task, struct rpc_iostats *stats)
194{
195 rpc_count_iostats_metrics(task,
196 &stats[task->tk_msg.rpc_proc->p_statidx]);
197}
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500198EXPORT_SYMBOL_GPL(rpc_count_iostats);
Chuck Lever11c556b2006-03-20 13:44:22 -0500199
Adrian Bunkec535ce2006-04-18 13:21:50 -0400200static void _print_name(struct seq_file *seq, unsigned int op,
Christoph Hellwig499b4982017-05-12 15:36:49 +0200201 const struct rpc_procinfo *procs)
Chuck Levercc0175c2006-03-20 13:44:22 -0500202{
203 if (procs[op].p_name)
204 seq_printf(seq, "\t%12s: ", procs[op].p_name);
205 else if (op == 0)
206 seq_printf(seq, "\t NULL: ");
207 else
208 seq_printf(seq, "\t%12u: ", op);
209}
210
Chuck Lever11c556b2006-03-20 13:44:22 -0500211void rpc_print_iostats(struct seq_file *seq, struct rpc_clnt *clnt)
212{
213 struct rpc_iostats *stats = clnt->cl_metrics;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500214 struct rpc_xprt *xprt;
Chuck Lever11c556b2006-03-20 13:44:22 -0500215 unsigned int op, maxproc = clnt->cl_maxproc;
216
217 if (!stats)
218 return;
219
220 seq_printf(seq, "\tRPC iostats version: %s ", RPC_IOSTATS_VERS);
221 seq_printf(seq, "p/v: %u/%u (%s)\n",
Trond Myklebust55909f22013-08-23 11:48:15 -0400222 clnt->cl_prog, clnt->cl_vers, clnt->cl_program->name);
Chuck Lever11c556b2006-03-20 13:44:22 -0500223
Trond Myklebust2446ab62012-03-01 17:00:56 -0500224 rcu_read_lock();
225 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500226 if (xprt)
227 xprt->ops->print_stats(xprt, seq);
Trond Myklebust2446ab62012-03-01 17:00:56 -0500228 rcu_read_unlock();
Chuck Lever11c556b2006-03-20 13:44:22 -0500229
230 seq_printf(seq, "\tper-op statistics\n");
231 for (op = 0; op < maxproc; op++) {
232 struct rpc_iostats *metrics = &stats[op];
Chuck Levercc0175c2006-03-20 13:44:22 -0500233 _print_name(seq, op, clnt->cl_procinfo);
Chuck Lever11c556b2006-03-20 13:44:22 -0500234 seq_printf(seq, "%lu %lu %lu %Lu %Lu %Lu %Lu %Lu\n",
235 metrics->om_ops,
236 metrics->om_ntrans,
237 metrics->om_timeouts,
238 metrics->om_bytes_sent,
239 metrics->om_bytes_recv,
Chuck Leverff839972010-05-07 13:34:47 -0400240 ktime_to_ms(metrics->om_queue),
241 ktime_to_ms(metrics->om_rtt),
242 ktime_to_ms(metrics->om_execute));
Chuck Lever11c556b2006-03-20 13:44:22 -0500243 }
244}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400245EXPORT_SYMBOL_GPL(rpc_print_iostats);
Chuck Lever11c556b2006-03-20 13:44:22 -0500246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/*
248 * Register/unregister RPC proc files
249 */
250static inline struct proc_dir_entry *
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300251do_register(struct net *net, const char *name, void *data,
252 const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400254 struct sunrpc_net *sn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400256 dprintk("RPC: registering /proc/net/rpc/%s\n", name);
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300257 sn = net_generic(net, sunrpc_net_id);
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400258 return proc_create_data(name, 0, sn->proc_net_rpc, fops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261struct proc_dir_entry *
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300262rpc_proc_register(struct net *net, struct rpc_stat *statp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300264 return do_register(net, statp->program->name, statp, &rpc_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400266EXPORT_SYMBOL_GPL(rpc_proc_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268void
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300269rpc_proc_unregister(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400271 struct sunrpc_net *sn;
272
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300273 sn = net_generic(net, sunrpc_net_id);
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400274 remove_proc_entry(name, sn->proc_net_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400276EXPORT_SYMBOL_GPL(rpc_proc_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278struct proc_dir_entry *
Stanislav Kinsbursky246590f2011-12-06 16:42:49 +0300279svc_proc_register(struct net *net, struct svc_stat *statp, const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Stanislav Kinsbursky246590f2011-12-06 16:42:49 +0300281 return do_register(net, statp->program->pg_name, statp, fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
Trond Myklebust24c37672008-12-23 16:30:12 -0500283EXPORT_SYMBOL_GPL(svc_proc_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285void
Stanislav Kinsbursky246590f2011-12-06 16:42:49 +0300286svc_proc_unregister(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400288 struct sunrpc_net *sn;
289
Stanislav Kinsbursky246590f2011-12-06 16:42:49 +0300290 sn = net_generic(net, sunrpc_net_id);
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400291 remove_proc_entry(name, sn->proc_net_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
Trond Myklebust24c37672008-12-23 16:30:12 -0500293EXPORT_SYMBOL_GPL(svc_proc_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400295int rpc_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400297 struct sunrpc_net *sn;
298
Chuck Lever46121cf2007-01-31 12:14:08 -0500299 dprintk("RPC: registering /proc/net/rpc\n");
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400300 sn = net_generic(net, sunrpc_net_id);
301 sn->proc_net_rpc = proc_mkdir("rpc", net->proc_net);
302 if (sn->proc_net_rpc == NULL)
303 return -ENOMEM;
304
305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400308void rpc_proc_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Chuck Lever46121cf2007-01-31 12:14:08 -0500310 dprintk("RPC: unregistering /proc/net/rpc\n");
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400311 remove_proc_entry("rpc", net->proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313