1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/net/sunrpc/stats.c
5 * procfs-based user access to generic RPC statistics. The stats files
6 * reside in /proc/net/rpc.
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
13 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
16 #include <linux/module.h>
17 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/proc_fs.h>
22 #include <linux/seq_file.h>
23 #include <linux/sunrpc/clnt.h>
24 #include <linux/sunrpc/svcsock.h>
25 #include <linux/sunrpc/metrics.h>
26 #include <linux/rcupdate.h>
28 #include <trace/events/sunrpc.h>
32 #define RPCDBG_FACILITY RPCDBG_MISC
35 * Get RPC client stats
37 static 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
;
52 statp
->rpcauthrefresh
);
54 for (i
= 0; i
< prog
->nrvers
; i
++) {
55 const struct rpc_version
*vers
= prog
->version
[i
];
58 seq_printf(seq
, "proc%u %u",
59 vers
->number
, vers
->nrprocs
);
60 for (j
= 0; j
< vers
->nrprocs
; j
++)
61 seq_printf(seq
, " %u", vers
->counts
[j
]);
67 static int rpc_proc_open(struct inode
*inode
, struct file
*file
)
69 return single_open(file
, rpc_proc_show
, PDE_DATA(inode
));
72 static const struct file_operations rpc_proc_fops
= {
74 .open
= rpc_proc_open
,
77 .release
= single_release
,
81 * Get RPC server stats
83 void svc_seq_show(struct seq_file
*seq
, const struct svc_stat
*statp
)
85 const struct svc_program
*prog
= statp
->program
;
86 const struct svc_version
*vers
;
96 "rpc %u %u %u %u %u\n",
98 statp
->rpcbadfmt
+statp
->rpcbadauth
+statp
->rpcbadclnt
,
103 for (i
= 0; i
< prog
->pg_nvers
; i
++) {
104 vers
= prog
->pg_vers
[i
];
107 seq_printf(seq
, "proc%d %u", i
, vers
->vs_nproc
);
108 for (j
= 0; j
< vers
->vs_nproc
; j
++)
109 seq_printf(seq
, " %u", vers
->vs_count
[j
]);
113 EXPORT_SYMBOL_GPL(svc_seq_show
);
116 * rpc_alloc_iostats - allocate an rpc_iostats structure
117 * @clnt: RPC program, version, and xprt
120 struct rpc_iostats
*rpc_alloc_iostats(struct rpc_clnt
*clnt
)
122 struct rpc_iostats
*stats
;
125 stats
= kcalloc(clnt
->cl_maxproc
, sizeof(*stats
), GFP_KERNEL
);
127 for (i
= 0; i
< clnt
->cl_maxproc
; i
++)
128 spin_lock_init(&stats
[i
].om_lock
);
132 EXPORT_SYMBOL_GPL(rpc_alloc_iostats
);
135 * rpc_free_iostats - release an rpc_iostats structure
136 * @stats: doomed rpc_iostats structure
139 void rpc_free_iostats(struct rpc_iostats
*stats
)
143 EXPORT_SYMBOL_GPL(rpc_free_iostats
);
146 * rpc_count_iostats_metrics - tally up per-task stats
147 * @task: completed rpc_task
148 * @op_metrics: stat structure for OP that will accumulate stats from @task
150 void rpc_count_iostats_metrics(const struct rpc_task
*task
,
151 struct rpc_iostats
*op_metrics
)
153 struct rpc_rqst
*req
= task
->tk_rqstp
;
154 ktime_t backlog
, execute
, now
;
156 if (!op_metrics
|| !req
)
160 spin_lock(&op_metrics
->om_lock
);
162 op_metrics
->om_ops
++;
163 /* kernel API: om_ops must never become larger than om_ntrans */
164 op_metrics
->om_ntrans
+= max(req
->rq_ntrans
, 1);
165 op_metrics
->om_timeouts
+= task
->tk_timeouts
;
167 op_metrics
->om_bytes_sent
+= req
->rq_xmit_bytes_sent
;
168 op_metrics
->om_bytes_recv
+= req
->rq_reply_bytes_recvd
;
171 if (ktime_to_ns(req
->rq_xtime
)) {
172 backlog
= ktime_sub(req
->rq_xtime
, task
->tk_start
);
173 op_metrics
->om_queue
= ktime_add(op_metrics
->om_queue
, backlog
);
176 op_metrics
->om_rtt
= ktime_add(op_metrics
->om_rtt
, req
->rq_rtt
);
178 execute
= ktime_sub(now
, task
->tk_start
);
179 op_metrics
->om_execute
= ktime_add(op_metrics
->om_execute
, execute
);
180 if (task
->tk_status
< 0)
181 op_metrics
->om_error_status
++;
183 spin_unlock(&op_metrics
->om_lock
);
185 trace_rpc_stats_latency(req
->rq_task
, backlog
, req
->rq_rtt
, execute
);
187 EXPORT_SYMBOL_GPL(rpc_count_iostats_metrics
);
190 * rpc_count_iostats - tally up per-task stats
191 * @task: completed rpc_task
192 * @stats: array of stat structures
194 * Uses the statidx from @task
196 void rpc_count_iostats(const struct rpc_task
*task
, struct rpc_iostats
*stats
)
198 rpc_count_iostats_metrics(task
,
199 &stats
[task
->tk_msg
.rpc_proc
->p_statidx
]);
201 EXPORT_SYMBOL_GPL(rpc_count_iostats
);
203 static void _print_name(struct seq_file
*seq
, unsigned int op
,
204 const struct rpc_procinfo
*procs
)
206 if (procs
[op
].p_name
)
207 seq_printf(seq
, "\t%12s: ", procs
[op
].p_name
);
209 seq_printf(seq
, "\t NULL: ");
211 seq_printf(seq
, "\t%12u: ", op
);
214 static void _add_rpc_iostats(struct rpc_iostats
*a
, struct rpc_iostats
*b
)
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
);
224 a
->om_error_status
+= b
->om_error_status
;
227 static void _print_rpc_iostats(struct seq_file
*seq
, struct rpc_iostats
*stats
,
228 int op
, const struct rpc_procinfo
*procs
)
230 _print_name(seq
, op
, procs
);
231 seq_printf(seq
, "%lu %lu %lu %llu %llu %llu %llu %llu %lu\n",
235 stats
->om_bytes_sent
,
236 stats
->om_bytes_recv
,
237 ktime_to_ms(stats
->om_queue
),
238 ktime_to_ms(stats
->om_rtt
),
239 ktime_to_ms(stats
->om_execute
),
240 stats
->om_error_status
);
243 static int do_print_stats(struct rpc_clnt
*clnt
, struct rpc_xprt
*xprt
, void *seqv
)
245 struct seq_file
*seq
= seqv
;
247 xprt
->ops
->print_stats(xprt
, seq
);
251 void rpc_clnt_show_stats(struct seq_file
*seq
, struct rpc_clnt
*clnt
)
253 unsigned int op
, maxproc
= clnt
->cl_maxproc
;
255 if (!clnt
->cl_metrics
)
258 seq_printf(seq
, "\tRPC iostats version: %s ", RPC_IOSTATS_VERS
);
259 seq_printf(seq
, "p/v: %u/%u (%s)\n",
260 clnt
->cl_prog
, clnt
->cl_vers
, clnt
->cl_program
->name
);
262 rpc_clnt_iterate_for_each_xprt(clnt
, do_print_stats
, seq
);
264 seq_printf(seq
, "\tper-op statistics\n");
265 for (op
= 0; op
< maxproc
; op
++) {
266 struct rpc_iostats stats
= {};
267 struct rpc_clnt
*next
= clnt
;
269 _add_rpc_iostats(&stats
, &next
->cl_metrics
[op
]);
270 if (next
== next
->cl_parent
)
272 next
= next
->cl_parent
;
274 _print_rpc_iostats(seq
, &stats
, op
, clnt
->cl_procinfo
);
277 EXPORT_SYMBOL_GPL(rpc_clnt_show_stats
);
280 * Register/unregister RPC proc files
282 static inline struct proc_dir_entry
*
283 do_register(struct net
*net
, const char *name
, void *data
,
284 const struct file_operations
*fops
)
286 struct sunrpc_net
*sn
;
288 dprintk("RPC: registering /proc/net/rpc/%s\n", name
);
289 sn
= net_generic(net
, sunrpc_net_id
);
290 return proc_create_data(name
, 0, sn
->proc_net_rpc
, fops
, data
);
293 struct proc_dir_entry
*
294 rpc_proc_register(struct net
*net
, struct rpc_stat
*statp
)
296 return do_register(net
, statp
->program
->name
, statp
, &rpc_proc_fops
);
298 EXPORT_SYMBOL_GPL(rpc_proc_register
);
301 rpc_proc_unregister(struct net
*net
, const char *name
)
303 struct sunrpc_net
*sn
;
305 sn
= net_generic(net
, sunrpc_net_id
);
306 remove_proc_entry(name
, sn
->proc_net_rpc
);
308 EXPORT_SYMBOL_GPL(rpc_proc_unregister
);
310 struct proc_dir_entry
*
311 svc_proc_register(struct net
*net
, struct svc_stat
*statp
, const struct file_operations
*fops
)
313 return do_register(net
, statp
->program
->pg_name
, statp
, fops
);
315 EXPORT_SYMBOL_GPL(svc_proc_register
);
318 svc_proc_unregister(struct net
*net
, const char *name
)
320 struct sunrpc_net
*sn
;
322 sn
= net_generic(net
, sunrpc_net_id
);
323 remove_proc_entry(name
, sn
->proc_net_rpc
);
325 EXPORT_SYMBOL_GPL(svc_proc_unregister
);
327 int rpc_proc_init(struct net
*net
)
329 struct sunrpc_net
*sn
;
331 dprintk("RPC: registering /proc/net/rpc\n");
332 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
)
340 void rpc_proc_exit(struct net
*net
)
342 dprintk("RPC: unregistering /proc/net/rpc\n");
343 remove_proc_entry("rpc", net
->proc_net
);