Merge branch 'next-fixes-for-5.2-rc' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / net / sunrpc / stats.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
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>
5a0e3ad6 17#include <linux/slab.h>
1da177e4
LT
18
19#include <linux/init.h>
20#include <linux/kernel.h>
1da177e4
LT
21#include <linux/proc_fs.h>
22#include <linux/seq_file.h>
23#include <linux/sunrpc/clnt.h>
24#include <linux/sunrpc/svcsock.h>
11c556b3 25#include <linux/sunrpc/metrics.h>
2446ab60 26#include <linux/rcupdate.h>
1da177e4 27
40bf7eb3
CL
28#include <trace/events/sunrpc.h>
29
4f42d0d5 30#include "netns.h"
1da177e4 31
4f42d0d5 32#define RPCDBG_FACILITY RPCDBG_MISC
1da177e4
LT
33
34/*
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;
ea339d46 40 unsigned int i, j;
1da177e4
LT
41
42 seq_printf(seq,
49e31cba 43 "net %u %u %u %u\n",
1da177e4
LT
44 statp->netcnt,
45 statp->netudpcnt,
46 statp->nettcpcnt,
47 statp->nettcpconn);
48 seq_printf(seq,
49e31cba 49 "rpc %u %u %u\n",
1da177e4
LT
50 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;
49e31cba 58 seq_printf(seq, "proc%u %u",
1da177e4
LT
59 vers->number, vers->nrprocs);
60 for (j = 0; j < vers->nrprocs; j++)
1c5876dd 61 seq_printf(seq, " %u", vers->counts[j]);
1da177e4
LT
62 seq_putc(seq, '\n');
63 }
64 return 0;
65}
66
67static int rpc_proc_open(struct inode *inode, struct file *file)
68{
d9dda78b 69 return single_open(file, rpc_proc_show, PDE_DATA(inode));
1da177e4
LT
70}
71
da7071d7 72static const struct file_operations rpc_proc_fops = {
1da177e4
LT
73 .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 */
7fd38af9
CH
83void svc_seq_show(struct seq_file *seq, const struct svc_stat *statp)
84{
1da177e4 85 const struct svc_program *prog = statp->program;
1da177e4 86 const struct svc_version *vers;
ea339d46 87 unsigned int i, j;
1da177e4
LT
88
89 seq_printf(seq,
49e31cba 90 "net %u %u %u %u\n",
1da177e4
LT
91 statp->netcnt,
92 statp->netudpcnt,
93 statp->nettcpcnt,
94 statp->nettcpconn);
95 seq_printf(seq,
49e31cba 96 "rpc %u %u %u %u %u\n",
1da177e4
LT
97 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++) {
7fd38af9
CH
104 vers = prog->pg_vers[i];
105 if (!vers)
1da177e4 106 continue;
49e31cba 107 seq_printf(seq, "proc%d %u", i, vers->vs_nproc);
7fd38af9
CH
108 for (j = 0; j < vers->vs_nproc; j++)
109 seq_printf(seq, " %u", vers->vs_count[j]);
1da177e4
LT
110 seq_putc(seq, '\n');
111 }
112}
24c3767e 113EXPORT_SYMBOL_GPL(svc_seq_show);
1da177e4 114
11c556b3
CL
115/**
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{
edef1297
CL
122 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;
11c556b3 131}
e8914c65 132EXPORT_SYMBOL_GPL(rpc_alloc_iostats);
11c556b3
CL
133
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}
e8914c65 143EXPORT_SYMBOL_GPL(rpc_free_iostats);
11c556b3
CL
144
145/**
840210fc 146 * rpc_count_iostats_metrics - tally up per-task stats
11c556b3 147 * @task: completed rpc_task
840210fc 148 * @op_metrics: stat structure for OP that will accumulate stats from @task
11c556b3 149 */
840210fc
WAA
150void rpc_count_iostats_metrics(const struct rpc_task *task,
151 struct rpc_iostats *op_metrics)
11c556b3
CL
152{
153 struct rpc_rqst *req = task->tk_rqstp;
40bf7eb3 154 ktime_t backlog, execute, now;
11c556b3 155
840210fc 156 if (!op_metrics || !req)
11c556b3 157 return;
55ae1aab 158
edef1297 159 now = ktime_get();
edef1297
CL
160 spin_lock(&op_metrics->om_lock);
161
11c556b3 162 op_metrics->om_ops++;
ae09531d
CL
163 /* kernel API: om_ops must never become larger than om_ntrans */
164 op_metrics->om_ntrans += max(req->rq_ntrans, 1);
11c556b3
CL
165 op_metrics->om_timeouts += task->tk_timeouts;
166
d60dbb20 167 op_metrics->om_bytes_sent += req->rq_xmit_bytes_sent;
dd2b63d0 168 op_metrics->om_bytes_recv += req->rq_reply_bytes_recvd;
11c556b3 169
40bf7eb3 170 backlog = 0;
ae09531d 171 if (ktime_to_ns(req->rq_xtime)) {
40bf7eb3
CL
172 backlog = ktime_sub(req->rq_xtime, task->tk_start);
173 op_metrics->om_queue = ktime_add(op_metrics->om_queue, backlog);
ae09531d 174 }
40bf7eb3 175
d60dbb20 176 op_metrics->om_rtt = ktime_add(op_metrics->om_rtt, req->rq_rtt);
11c556b3 177
40bf7eb3
CL
178 execute = ktime_sub(now, task->tk_start);
179 op_metrics->om_execute = ktime_add(op_metrics->om_execute, execute);
edef1297
CL
180
181 spin_unlock(&op_metrics->om_lock);
40bf7eb3
CL
182
183 trace_rpc_stats_latency(req->rq_task, backlog, req->rq_rtt, execute);
11c556b3 184}
840210fc
WAA
185EXPORT_SYMBOL_GPL(rpc_count_iostats_metrics);
186
187/**
188 * rpc_count_iostats - tally up per-task stats
189 * @task: completed rpc_task
190 * @stats: array of stat structures
191 *
192 * Uses the statidx from @task
193 */
194void rpc_count_iostats(const struct rpc_task *task, struct rpc_iostats *stats)
195{
196 rpc_count_iostats_metrics(task,
197 &stats[task->tk_msg.rpc_proc->p_statidx]);
198}
0a702195 199EXPORT_SYMBOL_GPL(rpc_count_iostats);
11c556b3 200
ec535ce1 201static void _print_name(struct seq_file *seq, unsigned int op,
499b4988 202 const struct rpc_procinfo *procs)
cc0175c1
CL
203{
204 if (procs[op].p_name)
205 seq_printf(seq, "\t%12s: ", procs[op].p_name);
206 else if (op == 0)
207 seq_printf(seq, "\t NULL: ");
208 else
209 seq_printf(seq, "\t%12u: ", op);
210}
211
189e1955
DW
212static void _add_rpc_iostats(struct rpc_iostats *a, struct rpc_iostats *b)
213{
214 a->om_ops += b->om_ops;
215 a->om_ntrans += b->om_ntrans;
216 a->om_timeouts += b->om_timeouts;
217 a->om_bytes_sent += b->om_bytes_sent;
218 a->om_bytes_recv += b->om_bytes_recv;
219 a->om_queue = ktime_add(a->om_queue, b->om_queue);
220 a->om_rtt = ktime_add(a->om_rtt, b->om_rtt);
221 a->om_execute = ktime_add(a->om_execute, b->om_execute);
222}
223
acdce5fb
DW
224static void _print_rpc_iostats(struct seq_file *seq, struct rpc_iostats *stats,
225 int op, const struct rpc_procinfo *procs)
226{
227 _print_name(seq, op, procs);
228 seq_printf(seq, "%lu %lu %lu %Lu %Lu %Lu %Lu %Lu\n",
229 stats->om_ops,
230 stats->om_ntrans,
231 stats->om_timeouts,
232 stats->om_bytes_sent,
233 stats->om_bytes_recv,
234 ktime_to_ms(stats->om_queue),
235 ktime_to_ms(stats->om_rtt),
236 ktime_to_ms(stats->om_execute));
237}
238
016583d7 239void rpc_clnt_show_stats(struct seq_file *seq, struct rpc_clnt *clnt)
11c556b3 240{
2446ab60 241 struct rpc_xprt *xprt;
11c556b3
CL
242 unsigned int op, maxproc = clnt->cl_maxproc;
243
016583d7 244 if (!clnt->cl_metrics)
11c556b3
CL
245 return;
246
247 seq_printf(seq, "\tRPC iostats version: %s ", RPC_IOSTATS_VERS);
248 seq_printf(seq, "p/v: %u/%u (%s)\n",
55909f21 249 clnt->cl_prog, clnt->cl_vers, clnt->cl_program->name);
11c556b3 250
2446ab60
TM
251 rcu_read_lock();
252 xprt = rcu_dereference(clnt->cl_xprt);
11c556b3
CL
253 if (xprt)
254 xprt->ops->print_stats(xprt, seq);
2446ab60 255 rcu_read_unlock();
11c556b3
CL
256
257 seq_printf(seq, "\tper-op statistics\n");
258 for (op = 0; op < maxproc; op++) {
016583d7
DW
259 struct rpc_iostats stats = {};
260 struct rpc_clnt *next = clnt;
261 do {
262 _add_rpc_iostats(&stats, &next->cl_metrics[op]);
263 if (next == next->cl_parent)
264 break;
265 next = next->cl_parent;
266 } while (next);
267 _print_rpc_iostats(seq, &stats, op, clnt->cl_procinfo);
11c556b3
CL
268 }
269}
016583d7 270EXPORT_SYMBOL_GPL(rpc_clnt_show_stats);
11c556b3 271
1da177e4
LT
272/*
273 * Register/unregister RPC proc files
274 */
275static inline struct proc_dir_entry *
ec7652aa
SK
276do_register(struct net *net, const char *name, void *data,
277 const struct file_operations *fops)
1da177e4 278{
4f42d0d5 279 struct sunrpc_net *sn;
1da177e4 280
4f42d0d5 281 dprintk("RPC: registering /proc/net/rpc/%s\n", name);
ec7652aa 282 sn = net_generic(net, sunrpc_net_id);
4f42d0d5 283 return proc_create_data(name, 0, sn->proc_net_rpc, fops, data);
1da177e4
LT
284}
285
286struct proc_dir_entry *
ec7652aa 287rpc_proc_register(struct net *net, struct rpc_stat *statp)
1da177e4 288{
ec7652aa 289 return do_register(net, statp->program->name, statp, &rpc_proc_fops);
1da177e4 290}
e8914c65 291EXPORT_SYMBOL_GPL(rpc_proc_register);
1da177e4
LT
292
293void
ec7652aa 294rpc_proc_unregister(struct net *net, const char *name)
1da177e4 295{
4f42d0d5
PE
296 struct sunrpc_net *sn;
297
ec7652aa 298 sn = net_generic(net, sunrpc_net_id);
4f42d0d5 299 remove_proc_entry(name, sn->proc_net_rpc);
1da177e4 300}
e8914c65 301EXPORT_SYMBOL_GPL(rpc_proc_unregister);
1da177e4
LT
302
303struct proc_dir_entry *
246590f5 304svc_proc_register(struct net *net, struct svc_stat *statp, const struct file_operations *fops)
1da177e4 305{
246590f5 306 return do_register(net, statp->program->pg_name, statp, fops);
1da177e4 307}
24c3767e 308EXPORT_SYMBOL_GPL(svc_proc_register);
1da177e4
LT
309
310void
246590f5 311svc_proc_unregister(struct net *net, const char *name)
1da177e4 312{
4f42d0d5
PE
313 struct sunrpc_net *sn;
314
246590f5 315 sn = net_generic(net, sunrpc_net_id);
4f42d0d5 316 remove_proc_entry(name, sn->proc_net_rpc);
1da177e4 317}
24c3767e 318EXPORT_SYMBOL_GPL(svc_proc_unregister);
1da177e4 319
4f42d0d5 320int rpc_proc_init(struct net *net)
1da177e4 321{
4f42d0d5
PE
322 struct sunrpc_net *sn;
323
46121cf7 324 dprintk("RPC: registering /proc/net/rpc\n");
4f42d0d5
PE
325 sn = net_generic(net, sunrpc_net_id);
326 sn->proc_net_rpc = proc_mkdir("rpc", net->proc_net);
327 if (sn->proc_net_rpc == NULL)
328 return -ENOMEM;
329
330 return 0;
1da177e4
LT
331}
332
4f42d0d5 333void rpc_proc_exit(struct net *net)
1da177e4 334{
46121cf7 335 dprintk("RPC: unregistering /proc/net/rpc\n");
4f42d0d5 336 remove_proc_entry("rpc", net->proc_net);
1da177e4 337}