blob: 7c74197c2ecfc67f1ccb50a7aa7bf5d68f17b90c [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/net/sunrpc/stats.c
4 *
5 * procfs-based user access to generic RPC statistics. The stats files
6 * reside in /proc/net/rpc.
7 *
8 * The read routines assume that the buffer passed in is just big enough.
9 * If you implement an RPC service that has its own stats routine which
10 * appends the generic RPC stats, make sure you don't exceed the PAGE_SIZE
11 * limit.
12 *
13 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
14 */
15
16#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/init.h>
20#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/proc_fs.h>
22#include <linux/seq_file.h>
23#include <linux/sunrpc/clnt.h>
24#include <linux/sunrpc/svcsock.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050025#include <linux/sunrpc/metrics.h>
Trond Myklebust2446ab62012-03-01 17:00:56 -050026#include <linux/rcupdate.h>
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +040027
Chuck Lever40bf7eb2018-03-16 10:33:49 -040028#include <trace/events/sunrpc.h>
29
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +040030#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#define RPCDBG_FACILITY RPCDBG_MISC
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
35 * Get RPC client stats
36 */
37static int rpc_proc_show(struct seq_file *seq, void *v) {
38 const struct rpc_stat *statp = seq->private;
39 const struct rpc_program *prog = statp->program;
Chuck Leverea339d42007-10-26 13:32:56 -040040 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070043 "net %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 statp->netcnt,
45 statp->netudpcnt,
46 statp->nettcpcnt,
47 statp->nettcpconn);
48 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070049 "rpc %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 statp->rpccnt,
51 statp->rpcretrans,
52 statp->rpcauthrefresh);
53
54 for (i = 0; i < prog->nrvers; i++) {
55 const struct rpc_version *vers = prog->version[i];
56 if (!vers)
57 continue;
Max Kellermann49e31cb2005-09-06 15:18:03 -070058 seq_printf(seq, "proc%u %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 vers->number, vers->nrprocs);
60 for (j = 0; j < vers->nrprocs; j++)
Christoph Hellwig1c5876d2017-05-08 23:27:10 +020061 seq_printf(seq, " %u", vers->counts[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 seq_putc(seq, '\n');
63 }
64 return 0;
65}
66
67static int rpc_proc_open(struct inode *inode, struct file *file)
68{
Al Virod9dda782013-03-31 18:16:14 -040069 return single_open(file, rpc_proc_show, PDE_DATA(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
Arjan van de Venda7071d2007-02-12 00:55:36 -080072static const struct file_operations rpc_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 .owner = THIS_MODULE,
74 .open = rpc_proc_open,
75 .read = seq_read,
76 .llseek = seq_lseek,
77 .release = single_release,
78};
79
80/*
81 * Get RPC server stats
82 */
Christoph Hellwig7fd38af2017-05-08 23:40:27 +020083void svc_seq_show(struct seq_file *seq, const struct svc_stat *statp)
84{
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 const struct svc_program *prog = statp->program;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 const struct svc_version *vers;
Chuck Leverea339d42007-10-26 13:32:56 -040087 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070090 "net %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 statp->netcnt,
92 statp->netudpcnt,
93 statp->nettcpcnt,
94 statp->nettcpconn);
95 seq_printf(seq,
Max Kellermann49e31cb2005-09-06 15:18:03 -070096 "rpc %u %u %u %u %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 statp->rpccnt,
98 statp->rpcbadfmt+statp->rpcbadauth+statp->rpcbadclnt,
99 statp->rpcbadfmt,
100 statp->rpcbadauth,
101 statp->rpcbadclnt);
102
103 for (i = 0; i < prog->pg_nvers; i++) {
Christoph Hellwig7fd38af2017-05-08 23:40:27 +0200104 vers = prog->pg_vers[i];
105 if (!vers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 continue;
Max Kellermann49e31cb2005-09-06 15:18:03 -0700107 seq_printf(seq, "proc%d %u", i, vers->vs_nproc);
Christoph Hellwig7fd38af2017-05-08 23:40:27 +0200108 for (j = 0; j < vers->vs_nproc; j++)
109 seq_printf(seq, " %u", vers->vs_count[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 seq_putc(seq, '\n');
111 }
112}
Trond Myklebust24c37672008-12-23 16:30:12 -0500113EXPORT_SYMBOL_GPL(svc_seq_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Chuck Lever11c556b2006-03-20 13:44:22 -0500115/**
116 * rpc_alloc_iostats - allocate an rpc_iostats structure
117 * @clnt: RPC program, version, and xprt
118 *
119 */
120struct rpc_iostats *rpc_alloc_iostats(struct rpc_clnt *clnt)
121{
Chuck Leveredef1292014-11-08 20:15:09 -0500122 struct rpc_iostats *stats;
123 int i;
124
125 stats = kcalloc(clnt->cl_maxproc, sizeof(*stats), GFP_KERNEL);
126 if (stats) {
127 for (i = 0; i < clnt->cl_maxproc; i++)
128 spin_lock_init(&stats[i].om_lock);
129 }
130 return stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500131}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400132EXPORT_SYMBOL_GPL(rpc_alloc_iostats);
Chuck Lever11c556b2006-03-20 13:44:22 -0500133
134/**
135 * rpc_free_iostats - release an rpc_iostats structure
136 * @stats: doomed rpc_iostats structure
137 *
138 */
139void rpc_free_iostats(struct rpc_iostats *stats)
140{
141 kfree(stats);
142}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400143EXPORT_SYMBOL_GPL(rpc_free_iostats);
Chuck Lever11c556b2006-03-20 13:44:22 -0500144
145/**
Weston Andros Adamson840210f2014-06-24 10:59:52 -0400146 * rpc_count_iostats_metrics - tally up per-task stats
Chuck Lever11c556b2006-03-20 13:44:22 -0500147 * @task: completed rpc_task
Weston Andros Adamson840210f2014-06-24 10:59:52 -0400148 * @op_metrics: stat structure for OP that will accumulate stats from @task
Chuck Lever11c556b2006-03-20 13:44:22 -0500149 */
Weston Andros Adamson840210f2014-06-24 10:59:52 -0400150void rpc_count_iostats_metrics(const struct rpc_task *task,
151 struct rpc_iostats *op_metrics)
Chuck Lever11c556b2006-03-20 13:44:22 -0500152{
153 struct rpc_rqst *req = task->tk_rqstp;
Chuck Lever40bf7eb2018-03-16 10:33:49 -0400154 ktime_t backlog, execute, now;
Chuck Lever11c556b2006-03-20 13:44:22 -0500155
Weston Andros Adamson840210f2014-06-24 10:59:52 -0400156 if (!op_metrics || !req)
Chuck Lever11c556b2006-03-20 13:44:22 -0500157 return;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400158
Chuck Leveredef1292014-11-08 20:15:09 -0500159 now = ktime_get();
Chuck Leveredef1292014-11-08 20:15:09 -0500160 spin_lock(&op_metrics->om_lock);
161
Chuck Lever11c556b2006-03-20 13:44:22 -0500162 op_metrics->om_ops++;
Chuck Leverae095312016-11-29 10:52:32 -0500163 /* kernel API: om_ops must never become larger than om_ntrans */
164 op_metrics->om_ntrans += max(req->rq_ntrans, 1);
Chuck Lever11c556b2006-03-20 13:44:22 -0500165 op_metrics->om_timeouts += task->tk_timeouts;
166
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400167 op_metrics->om_bytes_sent += req->rq_xmit_bytes_sent;
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -0400168 op_metrics->om_bytes_recv += req->rq_reply_bytes_recvd;
Chuck Lever11c556b2006-03-20 13:44:22 -0500169
Chuck Lever40bf7eb2018-03-16 10:33:49 -0400170 backlog = 0;
Chuck Leverae095312016-11-29 10:52:32 -0500171 if (ktime_to_ns(req->rq_xtime)) {
Chuck Lever40bf7eb2018-03-16 10:33:49 -0400172 backlog = ktime_sub(req->rq_xtime, task->tk_start);
173 op_metrics->om_queue = ktime_add(op_metrics->om_queue, backlog);
Chuck Leverae095312016-11-29 10:52:32 -0500174 }
Chuck Lever40bf7eb2018-03-16 10:33:49 -0400175
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400176 op_metrics->om_rtt = ktime_add(op_metrics->om_rtt, req->rq_rtt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500177
Chuck Lever40bf7eb2018-03-16 10:33:49 -0400178 execute = ktime_sub(now, task->tk_start);
179 op_metrics->om_execute = ktime_add(op_metrics->om_execute, execute);
Dave Wysochanskia3325182019-05-23 16:13:50 -0400180 if (task->tk_status < 0)
181 op_metrics->om_error_status++;
Chuck Leveredef1292014-11-08 20:15:09 -0500182
183 spin_unlock(&op_metrics->om_lock);
Chuck Lever40bf7eb2018-03-16 10:33:49 -0400184
185 trace_rpc_stats_latency(req->rq_task, backlog, req->rq_rtt, execute);
Chuck Lever11c556b2006-03-20 13:44:22 -0500186}
Weston Andros Adamson840210f2014-06-24 10:59:52 -0400187EXPORT_SYMBOL_GPL(rpc_count_iostats_metrics);
188
189/**
190 * rpc_count_iostats - tally up per-task stats
191 * @task: completed rpc_task
192 * @stats: array of stat structures
193 *
194 * Uses the statidx from @task
195 */
196void rpc_count_iostats(const struct rpc_task *task, struct rpc_iostats *stats)
197{
198 rpc_count_iostats_metrics(task,
199 &stats[task->tk_msg.rpc_proc->p_statidx]);
200}
Weston Andros Adamson0a702192012-02-17 13:15:24 -0500201EXPORT_SYMBOL_GPL(rpc_count_iostats);
Chuck Lever11c556b2006-03-20 13:44:22 -0500202
Adrian Bunkec535ce2006-04-18 13:21:50 -0400203static void _print_name(struct seq_file *seq, unsigned int op,
Christoph Hellwig499b4982017-05-12 15:36:49 +0200204 const struct rpc_procinfo *procs)
Chuck Levercc0175c2006-03-20 13:44:22 -0500205{
206 if (procs[op].p_name)
207 seq_printf(seq, "\t%12s: ", procs[op].p_name);
208 else if (op == 0)
209 seq_printf(seq, "\t NULL: ");
210 else
211 seq_printf(seq, "\t%12u: ", op);
212}
213
Dave Wysochanski189e1952018-07-10 14:59:24 -0400214static void _add_rpc_iostats(struct rpc_iostats *a, struct rpc_iostats *b)
215{
216 a->om_ops += b->om_ops;
217 a->om_ntrans += b->om_ntrans;
218 a->om_timeouts += b->om_timeouts;
219 a->om_bytes_sent += b->om_bytes_sent;
220 a->om_bytes_recv += b->om_bytes_recv;
221 a->om_queue = ktime_add(a->om_queue, b->om_queue);
222 a->om_rtt = ktime_add(a->om_rtt, b->om_rtt);
223 a->om_execute = ktime_add(a->om_execute, b->om_execute);
Dave Wysochanskia3325182019-05-23 16:13:50 -0400224 a->om_error_status += b->om_error_status;
Dave Wysochanski189e1952018-07-10 14:59:24 -0400225}
226
Dave Wysochanskiacdce5f2018-07-10 14:59:23 -0400227static void _print_rpc_iostats(struct seq_file *seq, struct rpc_iostats *stats,
228 int op, const struct rpc_procinfo *procs)
229{
230 _print_name(seq, op, procs);
Dave Wysochanskia3325182019-05-23 16:13:50 -0400231 seq_printf(seq, "%lu %lu %lu %llu %llu %llu %llu %llu %lu\n",
Dave Wysochanskiacdce5f2018-07-10 14:59:23 -0400232 stats->om_ops,
233 stats->om_ntrans,
234 stats->om_timeouts,
235 stats->om_bytes_sent,
236 stats->om_bytes_recv,
237 ktime_to_ms(stats->om_queue),
238 ktime_to_ms(stats->om_rtt),
Dave Wysochanskia3325182019-05-23 16:13:50 -0400239 ktime_to_ms(stats->om_execute),
240 stats->om_error_status);
Dave Wysochanskiacdce5f2018-07-10 14:59:23 -0400241}
242
NeilBrown10db5692019-05-30 10:41:28 +1000243static int do_print_stats(struct rpc_clnt *clnt, struct rpc_xprt *xprt, void *seqv)
244{
245 struct seq_file *seq = seqv;
246
247 xprt->ops->print_stats(xprt, seq);
248 return 0;
249}
250
Dave Wysochanski016583d2018-07-31 10:10:51 -0400251void rpc_clnt_show_stats(struct seq_file *seq, struct rpc_clnt *clnt)
Chuck Lever11c556b2006-03-20 13:44:22 -0500252{
Chuck Lever11c556b2006-03-20 13:44:22 -0500253 unsigned int op, maxproc = clnt->cl_maxproc;
254
Dave Wysochanski016583d2018-07-31 10:10:51 -0400255 if (!clnt->cl_metrics)
Chuck Lever11c556b2006-03-20 13:44:22 -0500256 return;
257
258 seq_printf(seq, "\tRPC iostats version: %s ", RPC_IOSTATS_VERS);
259 seq_printf(seq, "p/v: %u/%u (%s)\n",
Trond Myklebust55909f22013-08-23 11:48:15 -0400260 clnt->cl_prog, clnt->cl_vers, clnt->cl_program->name);
Chuck Lever11c556b2006-03-20 13:44:22 -0500261
NeilBrown10db5692019-05-30 10:41:28 +1000262 rpc_clnt_iterate_for_each_xprt(clnt, do_print_stats, seq);
Chuck Lever11c556b2006-03-20 13:44:22 -0500263
264 seq_printf(seq, "\tper-op statistics\n");
265 for (op = 0; op < maxproc; op++) {
Dave Wysochanski016583d2018-07-31 10:10:51 -0400266 struct rpc_iostats stats = {};
267 struct rpc_clnt *next = clnt;
268 do {
269 _add_rpc_iostats(&stats, &next->cl_metrics[op]);
270 if (next == next->cl_parent)
271 break;
272 next = next->cl_parent;
273 } while (next);
274 _print_rpc_iostats(seq, &stats, op, clnt->cl_procinfo);
Chuck Lever11c556b2006-03-20 13:44:22 -0500275 }
276}
Dave Wysochanski016583d2018-07-31 10:10:51 -0400277EXPORT_SYMBOL_GPL(rpc_clnt_show_stats);
Chuck Lever11c556b2006-03-20 13:44:22 -0500278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279/*
280 * Register/unregister RPC proc files
281 */
282static inline struct proc_dir_entry *
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300283do_register(struct net *net, const char *name, void *data,
284 const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400286 struct sunrpc_net *sn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400288 dprintk("RPC: registering /proc/net/rpc/%s\n", name);
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300289 sn = net_generic(net, sunrpc_net_id);
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400290 return proc_create_data(name, 0, sn->proc_net_rpc, fops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293struct proc_dir_entry *
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300294rpc_proc_register(struct net *net, struct rpc_stat *statp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300296 return do_register(net, statp->program->name, statp, &rpc_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400298EXPORT_SYMBOL_GPL(rpc_proc_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300void
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300301rpc_proc_unregister(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400303 struct sunrpc_net *sn;
304
Stanislav Kinsburskyec7652a2011-12-06 16:42:40 +0300305 sn = net_generic(net, sunrpc_net_id);
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400306 remove_proc_entry(name, sn->proc_net_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400308EXPORT_SYMBOL_GPL(rpc_proc_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310struct proc_dir_entry *
Stanislav Kinsbursky246590f2011-12-06 16:42:49 +0300311svc_proc_register(struct net *net, struct svc_stat *statp, const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Stanislav Kinsbursky246590f2011-12-06 16:42:49 +0300313 return do_register(net, statp->program->pg_name, statp, fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
Trond Myklebust24c37672008-12-23 16:30:12 -0500315EXPORT_SYMBOL_GPL(svc_proc_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317void
Stanislav Kinsbursky246590f2011-12-06 16:42:49 +0300318svc_proc_unregister(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400320 struct sunrpc_net *sn;
321
Stanislav Kinsbursky246590f2011-12-06 16:42:49 +0300322 sn = net_generic(net, sunrpc_net_id);
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400323 remove_proc_entry(name, sn->proc_net_rpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
Trond Myklebust24c37672008-12-23 16:30:12 -0500325EXPORT_SYMBOL_GPL(svc_proc_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400327int rpc_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400329 struct sunrpc_net *sn;
330
Chuck Lever46121cf2007-01-31 12:14:08 -0500331 dprintk("RPC: registering /proc/net/rpc\n");
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400332 sn = net_generic(net, sunrpc_net_id);
333 sn->proc_net_rpc = proc_mkdir("rpc", net->proc_net);
334 if (sn->proc_net_rpc == NULL)
335 return -ENOMEM;
336
337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400340void rpc_proc_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Chuck Lever46121cf2007-01-31 12:14:08 -0500342 dprintk("RPC: unregistering /proc/net/rpc\n");
Pavel Emelyanov4f42d0d2010-09-27 14:01:58 +0400343 remove_proc_entry("rpc", net->proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}