1 // SPDX-License-Identifier: GPL-2.0-only << 2 /* 1 /* 3 * linux/net/sunrpc/stats.c 2 * linux/net/sunrpc/stats.c 4 * 3 * 5 * procfs-based user access to generic RPC sta 4 * procfs-based user access to generic RPC statistics. The stats files 6 * reside in /proc/net/rpc. 5 * reside in /proc/net/rpc. 7 * 6 * 8 * The read routines assume that the buffer pa 7 * The read routines assume that the buffer passed in is just big enough. 9 * If you implement an RPC service that has it 8 * If you implement an RPC service that has its own stats routine which 10 * appends the generic RPC stats, make sure yo 9 * appends the generic RPC stats, make sure you don't exceed the PAGE_SIZE 11 * limit. 10 * limit. 12 * 11 * 13 * Copyright (C) 1995, 1996, 1997 Olaf Kirch < 12 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de> 14 */ 13 */ 15 14 16 #include <linux/module.h> 15 #include <linux/module.h> 17 #include <linux/slab.h> 16 #include <linux/slab.h> 18 17 19 #include <linux/init.h> 18 #include <linux/init.h> 20 #include <linux/kernel.h> 19 #include <linux/kernel.h> 21 #include <linux/proc_fs.h> 20 #include <linux/proc_fs.h> 22 #include <linux/seq_file.h> 21 #include <linux/seq_file.h> 23 #include <linux/sunrpc/clnt.h> 22 #include <linux/sunrpc/clnt.h> 24 #include <linux/sunrpc/svcsock.h> 23 #include <linux/sunrpc/svcsock.h> 25 #include <linux/sunrpc/metrics.h> 24 #include <linux/sunrpc/metrics.h> 26 #include <linux/rcupdate.h> 25 #include <linux/rcupdate.h> 27 26 28 #include <trace/events/sunrpc.h> << 29 << 30 #include "netns.h" 27 #include "netns.h" 31 28 32 #define RPCDBG_FACILITY RPCDBG_MISC 29 #define RPCDBG_FACILITY RPCDBG_MISC 33 30 34 /* 31 /* 35 * Get RPC client stats 32 * Get RPC client stats 36 */ 33 */ 37 static int rpc_proc_show(struct seq_file *seq, 34 static int rpc_proc_show(struct seq_file *seq, void *v) { 38 const struct rpc_stat *statp = seq-> 35 const struct rpc_stat *statp = seq->private; 39 const struct rpc_program *prog = statp 36 const struct rpc_program *prog = statp->program; 40 unsigned int i, j; 37 unsigned int i, j; 41 38 42 seq_printf(seq, 39 seq_printf(seq, 43 "net %u %u %u %u\n", 40 "net %u %u %u %u\n", 44 statp->netcnt, 41 statp->netcnt, 45 statp->netudpcnt, 42 statp->netudpcnt, 46 statp->nettcpcnt, 43 statp->nettcpcnt, 47 statp->nettcpconn); 44 statp->nettcpconn); 48 seq_printf(seq, 45 seq_printf(seq, 49 "rpc %u %u %u\n", 46 "rpc %u %u %u\n", 50 statp->rpccnt, 47 statp->rpccnt, 51 statp->rpcretrans, 48 statp->rpcretrans, 52 statp->rpcauthrefresh) 49 statp->rpcauthrefresh); 53 50 54 for (i = 0; i < prog->nrvers; i++) { 51 for (i = 0; i < prog->nrvers; i++) { 55 const struct rpc_version *vers 52 const struct rpc_version *vers = prog->version[i]; 56 if (!vers) 53 if (!vers) 57 continue; 54 continue; 58 seq_printf(seq, "proc%u %u", 55 seq_printf(seq, "proc%u %u", 59 vers-> 56 vers->number, vers->nrprocs); 60 for (j = 0; j < vers->nrprocs; 57 for (j = 0; j < vers->nrprocs; j++) 61 seq_printf(seq, " %u", 58 seq_printf(seq, " %u", vers->counts[j]); 62 seq_putc(seq, '\n'); 59 seq_putc(seq, '\n'); 63 } 60 } 64 return 0; 61 return 0; 65 } 62 } 66 63 67 static int rpc_proc_open(struct inode *inode, 64 static int rpc_proc_open(struct inode *inode, struct file *file) 68 { 65 { 69 return single_open(file, rpc_proc_show !! 66 return single_open(file, rpc_proc_show, PDE_DATA(inode)); 70 } 67 } 71 68 72 static const struct proc_ops rpc_proc_ops = { !! 69 static const struct file_operations rpc_proc_fops = { 73 .proc_open = rpc_proc_open, !! 70 .owner = THIS_MODULE, 74 .proc_read = seq_read, !! 71 .open = rpc_proc_open, 75 .proc_lseek = seq_lseek, !! 72 .read = seq_read, 76 .proc_release = single_release, !! 73 .llseek = seq_lseek, >> 74 .release = single_release, 77 }; 75 }; 78 76 79 /* 77 /* 80 * Get RPC server stats 78 * Get RPC server stats 81 */ 79 */ 82 void svc_seq_show(struct seq_file *seq, const 80 void svc_seq_show(struct seq_file *seq, const struct svc_stat *statp) 83 { 81 { 84 const struct svc_program *prog = statp 82 const struct svc_program *prog = statp->program; 85 const struct svc_version *vers; 83 const struct svc_version *vers; 86 unsigned int i, j, k; !! 84 unsigned int i, j; 87 unsigned long count; << 88 85 89 seq_printf(seq, 86 seq_printf(seq, 90 "net %u %u %u %u\n", 87 "net %u %u %u %u\n", 91 statp->netcnt, 88 statp->netcnt, 92 statp->netudpcnt, 89 statp->netudpcnt, 93 statp->nettcpcnt, 90 statp->nettcpcnt, 94 statp->nettcpconn); 91 statp->nettcpconn); 95 seq_printf(seq, 92 seq_printf(seq, 96 "rpc %u %u %u %u %u\n", 93 "rpc %u %u %u %u %u\n", 97 statp->rpccnt, 94 statp->rpccnt, 98 statp->rpcbadfmt+statp 95 statp->rpcbadfmt+statp->rpcbadauth+statp->rpcbadclnt, 99 statp->rpcbadfmt, 96 statp->rpcbadfmt, 100 statp->rpcbadauth, 97 statp->rpcbadauth, 101 statp->rpcbadclnt); 98 statp->rpcbadclnt); 102 99 103 for (i = 0; i < prog->pg_nvers; i++) { 100 for (i = 0; i < prog->pg_nvers; i++) { 104 vers = prog->pg_vers[i]; 101 vers = prog->pg_vers[i]; 105 if (!vers) 102 if (!vers) 106 continue; 103 continue; 107 seq_printf(seq, "proc%d %u", i 104 seq_printf(seq, "proc%d %u", i, vers->vs_nproc); 108 for (j = 0; j < vers->vs_nproc !! 105 for (j = 0; j < vers->vs_nproc; j++) 109 count = 0; !! 106 seq_printf(seq, " %u", vers->vs_count[j]); 110 for_each_possible_cpu( << 111 count += per_c << 112 seq_printf(seq, " %lu" << 113 } << 114 seq_putc(seq, '\n'); 107 seq_putc(seq, '\n'); 115 } 108 } 116 } 109 } 117 EXPORT_SYMBOL_GPL(svc_seq_show); 110 EXPORT_SYMBOL_GPL(svc_seq_show); 118 111 119 /** 112 /** 120 * rpc_alloc_iostats - allocate an rpc_iostats 113 * rpc_alloc_iostats - allocate an rpc_iostats structure 121 * @clnt: RPC program, version, and xprt 114 * @clnt: RPC program, version, and xprt 122 * 115 * 123 */ 116 */ 124 struct rpc_iostats *rpc_alloc_iostats(struct r 117 struct rpc_iostats *rpc_alloc_iostats(struct rpc_clnt *clnt) 125 { 118 { 126 struct rpc_iostats *stats; 119 struct rpc_iostats *stats; 127 int i; 120 int i; 128 121 129 stats = kcalloc(clnt->cl_maxproc, size 122 stats = kcalloc(clnt->cl_maxproc, sizeof(*stats), GFP_KERNEL); 130 if (stats) { 123 if (stats) { 131 for (i = 0; i < clnt->cl_maxpr 124 for (i = 0; i < clnt->cl_maxproc; i++) 132 spin_lock_init(&stats[ 125 spin_lock_init(&stats[i].om_lock); 133 } 126 } 134 return stats; 127 return stats; 135 } 128 } 136 EXPORT_SYMBOL_GPL(rpc_alloc_iostats); 129 EXPORT_SYMBOL_GPL(rpc_alloc_iostats); 137 130 138 /** 131 /** 139 * rpc_free_iostats - release an rpc_iostats s 132 * rpc_free_iostats - release an rpc_iostats structure 140 * @stats: doomed rpc_iostats structure 133 * @stats: doomed rpc_iostats structure 141 * 134 * 142 */ 135 */ 143 void rpc_free_iostats(struct rpc_iostats *stat 136 void rpc_free_iostats(struct rpc_iostats *stats) 144 { 137 { 145 kfree(stats); 138 kfree(stats); 146 } 139 } 147 EXPORT_SYMBOL_GPL(rpc_free_iostats); 140 EXPORT_SYMBOL_GPL(rpc_free_iostats); 148 141 149 /** 142 /** 150 * rpc_count_iostats_metrics - tally up per-ta 143 * rpc_count_iostats_metrics - tally up per-task stats 151 * @task: completed rpc_task 144 * @task: completed rpc_task 152 * @op_metrics: stat structure for OP that wil 145 * @op_metrics: stat structure for OP that will accumulate stats from @task 153 */ 146 */ 154 void rpc_count_iostats_metrics(const struct rp 147 void rpc_count_iostats_metrics(const struct rpc_task *task, 155 struct rpc_iost 148 struct rpc_iostats *op_metrics) 156 { 149 { 157 struct rpc_rqst *req = task->tk_rqstp; 150 struct rpc_rqst *req = task->tk_rqstp; 158 ktime_t backlog, execute, now; !! 151 ktime_t delta, now; 159 152 160 if (!op_metrics || !req) 153 if (!op_metrics || !req) 161 return; 154 return; 162 155 163 now = ktime_get(); 156 now = ktime_get(); 164 spin_lock(&op_metrics->om_lock); 157 spin_lock(&op_metrics->om_lock); 165 158 166 op_metrics->om_ops++; 159 op_metrics->om_ops++; 167 /* kernel API: om_ops must never becom 160 /* kernel API: om_ops must never become larger than om_ntrans */ 168 op_metrics->om_ntrans += max(req->rq_n 161 op_metrics->om_ntrans += max(req->rq_ntrans, 1); 169 op_metrics->om_timeouts += task->tk_ti 162 op_metrics->om_timeouts += task->tk_timeouts; 170 163 171 op_metrics->om_bytes_sent += req->rq_x 164 op_metrics->om_bytes_sent += req->rq_xmit_bytes_sent; 172 op_metrics->om_bytes_recv += req->rq_r 165 op_metrics->om_bytes_recv += req->rq_reply_bytes_recvd; 173 166 174 backlog = 0; << 175 if (ktime_to_ns(req->rq_xtime)) { 167 if (ktime_to_ns(req->rq_xtime)) { 176 backlog = ktime_sub(req->rq_xt !! 168 delta = ktime_sub(req->rq_xtime, task->tk_start); 177 op_metrics->om_queue = ktime_a !! 169 op_metrics->om_queue = ktime_add(op_metrics->om_queue, delta); 178 } 170 } 179 << 180 op_metrics->om_rtt = ktime_add(op_metr 171 op_metrics->om_rtt = ktime_add(op_metrics->om_rtt, req->rq_rtt); 181 172 182 execute = ktime_sub(now, task->tk_star !! 173 delta = ktime_sub(now, task->tk_start); 183 op_metrics->om_execute = ktime_add(op_ !! 174 op_metrics->om_execute = ktime_add(op_metrics->om_execute, delta); 184 if (task->tk_status < 0) << 185 op_metrics->om_error_status++; << 186 175 187 spin_unlock(&op_metrics->om_lock); 176 spin_unlock(&op_metrics->om_lock); 188 << 189 trace_rpc_stats_latency(req->rq_task, << 190 } 177 } 191 EXPORT_SYMBOL_GPL(rpc_count_iostats_metrics); 178 EXPORT_SYMBOL_GPL(rpc_count_iostats_metrics); 192 179 193 /** 180 /** 194 * rpc_count_iostats - tally up per-task stats 181 * rpc_count_iostats - tally up per-task stats 195 * @task: completed rpc_task 182 * @task: completed rpc_task 196 * @stats: array of stat structures 183 * @stats: array of stat structures 197 * 184 * 198 * Uses the statidx from @task 185 * Uses the statidx from @task 199 */ 186 */ 200 void rpc_count_iostats(const struct rpc_task * 187 void rpc_count_iostats(const struct rpc_task *task, struct rpc_iostats *stats) 201 { 188 { 202 rpc_count_iostats_metrics(task, 189 rpc_count_iostats_metrics(task, 203 &stats[task- 190 &stats[task->tk_msg.rpc_proc->p_statidx]); 204 } 191 } 205 EXPORT_SYMBOL_GPL(rpc_count_iostats); 192 EXPORT_SYMBOL_GPL(rpc_count_iostats); 206 193 207 static void _print_name(struct seq_file *seq, 194 static void _print_name(struct seq_file *seq, unsigned int op, 208 const struct rpc_proci 195 const struct rpc_procinfo *procs) 209 { 196 { 210 if (procs[op].p_name) 197 if (procs[op].p_name) 211 seq_printf(seq, "\t%12s: ", pr 198 seq_printf(seq, "\t%12s: ", procs[op].p_name); 212 else if (op == 0) 199 else if (op == 0) 213 seq_printf(seq, "\t NUL 200 seq_printf(seq, "\t NULL: "); 214 else 201 else 215 seq_printf(seq, "\t%12u: ", op 202 seq_printf(seq, "\t%12u: ", op); 216 } 203 } 217 204 218 static void _add_rpc_iostats(struct rpc_iostat !! 205 void rpc_print_iostats(struct seq_file *seq, struct rpc_clnt *clnt) 219 { << 220 a->om_ops += b->om_ops; << 221 a->om_ntrans += b->om_ntrans; << 222 a->om_timeouts += b->om_timeouts; << 223 a->om_bytes_sent += b->om_bytes_sent; << 224 a->om_bytes_recv += b->om_bytes_recv; << 225 a->om_queue = ktime_add(a->om_queue, b << 226 a->om_rtt = ktime_add(a->om_rtt, b->om << 227 a->om_execute = ktime_add(a->om_execut << 228 a->om_error_status += b->om_error_stat << 229 } << 230 << 231 static void _print_rpc_iostats(struct seq_file << 232 int op, const s << 233 { << 234 _print_name(seq, op, procs); << 235 seq_printf(seq, "%lu %lu %lu %llu %llu << 236 stats->om_ops, << 237 stats->om_ntrans, << 238 stats->om_timeouts, << 239 stats->om_bytes_sent, << 240 stats->om_bytes_recv, << 241 ktime_to_ms(stats->om_queue << 242 ktime_to_ms(stats->om_rtt), << 243 ktime_to_ms(stats->om_execu << 244 stats->om_error_status); << 245 } << 246 << 247 static int do_print_stats(struct rpc_clnt *cln << 248 { << 249 struct seq_file *seq = seqv; << 250 << 251 xprt->ops->print_stats(xprt, seq); << 252 return 0; << 253 } << 254 << 255 void rpc_clnt_show_stats(struct seq_file *seq, << 256 { 206 { >> 207 struct rpc_iostats *stats = clnt->cl_metrics; >> 208 struct rpc_xprt *xprt; 257 unsigned int op, maxproc = clnt->cl_ma 209 unsigned int op, maxproc = clnt->cl_maxproc; 258 210 259 if (!clnt->cl_metrics) !! 211 if (!stats) 260 return; 212 return; 261 213 262 seq_printf(seq, "\tRPC iostats version 214 seq_printf(seq, "\tRPC iostats version: %s ", RPC_IOSTATS_VERS); 263 seq_printf(seq, "p/v: %u/%u (%s)\n", 215 seq_printf(seq, "p/v: %u/%u (%s)\n", 264 clnt->cl_prog, clnt->c 216 clnt->cl_prog, clnt->cl_vers, clnt->cl_program->name); 265 217 266 rpc_clnt_iterate_for_each_xprt(clnt, d !! 218 rcu_read_lock(); >> 219 xprt = rcu_dereference(clnt->cl_xprt); >> 220 if (xprt) >> 221 xprt->ops->print_stats(xprt, seq); >> 222 rcu_read_unlock(); 267 223 268 seq_printf(seq, "\tper-op statistics\n 224 seq_printf(seq, "\tper-op statistics\n"); 269 for (op = 0; op < maxproc; op++) { 225 for (op = 0; op < maxproc; op++) { 270 struct rpc_iostats stats = {}; !! 226 struct rpc_iostats *metrics = &stats[op]; 271 struct rpc_clnt *next = clnt; !! 227 _print_name(seq, op, clnt->cl_procinfo); 272 do { !! 228 seq_printf(seq, "%lu %lu %lu %Lu %Lu %Lu %Lu %Lu\n", 273 _add_rpc_iostats(&stat !! 229 metrics->om_ops, 274 if (next == next->cl_p !! 230 metrics->om_ntrans, 275 break; !! 231 metrics->om_timeouts, 276 next = next->cl_parent !! 232 metrics->om_bytes_sent, 277 } while (next); !! 233 metrics->om_bytes_recv, 278 _print_rpc_iostats(seq, &stats !! 234 ktime_to_ms(metrics->om_queue), >> 235 ktime_to_ms(metrics->om_rtt), >> 236 ktime_to_ms(metrics->om_execute)); 279 } 237 } 280 } 238 } 281 EXPORT_SYMBOL_GPL(rpc_clnt_show_stats); !! 239 EXPORT_SYMBOL_GPL(rpc_print_iostats); 282 240 283 /* 241 /* 284 * Register/unregister RPC proc files 242 * Register/unregister RPC proc files 285 */ 243 */ 286 static inline struct proc_dir_entry * 244 static inline struct proc_dir_entry * 287 do_register(struct net *net, const char *name, 245 do_register(struct net *net, const char *name, void *data, 288 const struct proc_ops *proc_ops) !! 246 const struct file_operations *fops) 289 { 247 { 290 struct sunrpc_net *sn; 248 struct sunrpc_net *sn; 291 249 292 dprintk("RPC: registering /proc/ 250 dprintk("RPC: registering /proc/net/rpc/%s\n", name); 293 sn = net_generic(net, sunrpc_net_id); 251 sn = net_generic(net, sunrpc_net_id); 294 return proc_create_data(name, 0, sn->p !! 252 return proc_create_data(name, 0, sn->proc_net_rpc, fops, data); 295 } 253 } 296 254 297 struct proc_dir_entry * 255 struct proc_dir_entry * 298 rpc_proc_register(struct net *net, struct rpc_ 256 rpc_proc_register(struct net *net, struct rpc_stat *statp) 299 { 257 { 300 return do_register(net, statp->program !! 258 return do_register(net, statp->program->name, statp, &rpc_proc_fops); 301 } 259 } 302 EXPORT_SYMBOL_GPL(rpc_proc_register); 260 EXPORT_SYMBOL_GPL(rpc_proc_register); 303 261 304 void 262 void 305 rpc_proc_unregister(struct net *net, const cha 263 rpc_proc_unregister(struct net *net, const char *name) 306 { 264 { 307 struct sunrpc_net *sn; 265 struct sunrpc_net *sn; 308 266 309 sn = net_generic(net, sunrpc_net_id); 267 sn = net_generic(net, sunrpc_net_id); 310 remove_proc_entry(name, sn->proc_net_r 268 remove_proc_entry(name, sn->proc_net_rpc); 311 } 269 } 312 EXPORT_SYMBOL_GPL(rpc_proc_unregister); 270 EXPORT_SYMBOL_GPL(rpc_proc_unregister); 313 271 314 struct proc_dir_entry * 272 struct proc_dir_entry * 315 svc_proc_register(struct net *net, struct svc_ !! 273 svc_proc_register(struct net *net, struct svc_stat *statp, const struct file_operations *fops) 316 { 274 { 317 return do_register(net, statp->program !! 275 return do_register(net, statp->program->pg_name, statp, fops); 318 } 276 } 319 EXPORT_SYMBOL_GPL(svc_proc_register); 277 EXPORT_SYMBOL_GPL(svc_proc_register); 320 278 321 void 279 void 322 svc_proc_unregister(struct net *net, const cha 280 svc_proc_unregister(struct net *net, const char *name) 323 { 281 { 324 struct sunrpc_net *sn; 282 struct sunrpc_net *sn; 325 283 326 sn = net_generic(net, sunrpc_net_id); 284 sn = net_generic(net, sunrpc_net_id); 327 remove_proc_entry(name, sn->proc_net_r 285 remove_proc_entry(name, sn->proc_net_rpc); 328 } 286 } 329 EXPORT_SYMBOL_GPL(svc_proc_unregister); 287 EXPORT_SYMBOL_GPL(svc_proc_unregister); 330 288 331 int rpc_proc_init(struct net *net) 289 int rpc_proc_init(struct net *net) 332 { 290 { 333 struct sunrpc_net *sn; 291 struct sunrpc_net *sn; 334 292 335 dprintk("RPC: registering /proc/ 293 dprintk("RPC: registering /proc/net/rpc\n"); 336 sn = net_generic(net, sunrpc_net_id); 294 sn = net_generic(net, sunrpc_net_id); 337 sn->proc_net_rpc = proc_mkdir("rpc", n 295 sn->proc_net_rpc = proc_mkdir("rpc", net->proc_net); 338 if (sn->proc_net_rpc == NULL) 296 if (sn->proc_net_rpc == NULL) 339 return -ENOMEM; 297 return -ENOMEM; 340 298 341 return 0; 299 return 0; 342 } 300 } 343 301 344 void rpc_proc_exit(struct net *net) 302 void rpc_proc_exit(struct net *net) 345 { 303 { 346 dprintk("RPC: unregistering /pro 304 dprintk("RPC: unregistering /proc/net/rpc\n"); 347 remove_proc_entry("rpc", net->proc_net 305 remove_proc_entry("rpc", net->proc_net); 348 } 306 } >> 307 349 308
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.