1 /* 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> 16 #include <linux/slab.h> 17 18 #include <linux/init.h> 19 #include <linux/kernel.h> 20 #include <linux/proc_fs.h> 21 #include <linux/seq_file.h> 22 #include <linux/sunrpc/clnt.h> 23 #include <linux/sunrpc/svcsock.h> 24 #include <linux/sunrpc/metrics.h> 25 #include <linux/rcupdate.h> 26 27 #include "netns.h" 28 29 #define RPCDBG_FACILITY RPCDBG_MISC 30 31 /* 32 * Get RPC client stats 33 */ 34 static int rpc_proc_show(struct seq_file *seq, void *v) { 35 const struct rpc_stat *statp = seq->private; 36 const struct rpc_program *prog = statp->program; 37 unsigned int i, j; 38 39 seq_printf(seq, 40 "net %u %u %u %u\n", 41 statp->netcnt, 42 statp->netudpcnt, 43 statp->nettcpcnt, 44 statp->nettcpconn); 45 seq_printf(seq, 46 "rpc %u %u %u\n", 47 statp->rpccnt, 48 statp->rpcretrans, 49 statp->rpcauthrefresh); 50 51 for (i = 0; i < prog->nrvers; i++) { 52 const struct rpc_version *vers = prog->version[i]; 53 if (!vers) 54 continue; 55 seq_printf(seq, "proc%u %u", 56 vers->number, vers->nrprocs); 57 for (j = 0; j < vers->nrprocs; j++) 58 seq_printf(seq, " %u", 59 vers->procs[j].p_count); 60 seq_putc(seq, '\n'); 61 } 62 return 0; 63 } 64 65 static int rpc_proc_open(struct inode *inode, struct file *file) 66 { 67 return single_open(file, rpc_proc_show, PDE_DATA(inode)); 68 } 69 70 static const struct file_operations rpc_proc_fops = { 71 .owner = THIS_MODULE, 72 .open = rpc_proc_open, 73 .read = seq_read, 74 .llseek = seq_lseek, 75 .release = single_release, 76 }; 77 78 /* 79 * Get RPC server stats 80 */ 81 void svc_seq_show(struct seq_file *seq, const struct svc_stat *statp) { 82 const struct svc_program *prog = statp->program; 83 const struct svc_procedure *proc; 84 const struct svc_version *vers; 85 unsigned int i, j; 86 87 seq_printf(seq, 88 "net %u %u %u %u\n", 89 statp->netcnt, 90 statp->netudpcnt, 91 statp->nettcpcnt, 92 statp->nettcpconn); 93 seq_printf(seq, 94 "rpc %u %u %u %u %u\n", 95 statp->rpccnt, 96 statp->rpcbadfmt+statp->rpcbadauth+statp->rpcbadclnt, 97 statp->rpcbadfmt, 98 statp->rpcbadauth, 99 statp->rpcbadclnt); 100 101 for (i = 0; i < prog->pg_nvers; i++) { 102 if (!(vers = prog->pg_vers[i]) || !(proc = vers->vs_proc)) 103 continue; 104 seq_printf(seq, "proc%d %u", i, vers->vs_nproc); 105 for (j = 0; j < vers->vs_nproc; j++, proc++) 106 seq_printf(seq, " %u", proc->pc_count); 107 seq_putc(seq, '\n'); 108 } 109 } 110 EXPORT_SYMBOL_GPL(svc_seq_show); 111 112 /** 113 * rpc_alloc_iostats - allocate an rpc_iostats structure 114 * @clnt: RPC program, version, and xprt 115 * 116 */ 117 struct rpc_iostats *rpc_alloc_iostats(struct rpc_clnt *clnt) 118 { 119 struct rpc_iostats *stats; 120 int i; 121 122 stats = kcalloc(clnt->cl_maxproc, sizeof(*stats), GFP_KERNEL); 123 if (stats) { 124 for (i = 0; i < clnt->cl_maxproc; i++) 125 spin_lock_init(&stats[i].om_lock); 126 } 127 return stats; 128 } 129 EXPORT_SYMBOL_GPL(rpc_alloc_iostats); 130 131 /** 132 * rpc_free_iostats - release an rpc_iostats structure 133 * @stats: doomed rpc_iostats structure 134 * 135 */ 136 void rpc_free_iostats(struct rpc_iostats *stats) 137 { 138 kfree(stats); 139 } 140 EXPORT_SYMBOL_GPL(rpc_free_iostats); 141 142 /** 143 * rpc_count_iostats - tally up per-task stats 144 * @task: completed rpc_task 145 * @stats: array of stat structures 146 */ 147 void rpc_count_iostats(const struct rpc_task *task, struct rpc_iostats *stats) 148 { 149 struct rpc_rqst *req = task->tk_rqstp; 150 struct rpc_iostats *op_metrics; 151 ktime_t delta, now; 152 153 if (!stats || !req) 154 return; 155 156 now = ktime_get(); 157 op_metrics = &stats[task->tk_msg.rpc_proc->p_statidx]; 158 159 spin_lock(&op_metrics->om_lock); 160 161 op_metrics->om_ops++; 162 op_metrics->om_ntrans += req->rq_ntrans; 163 op_metrics->om_timeouts += task->tk_timeouts; 164 165 op_metrics->om_bytes_sent += req->rq_xmit_bytes_sent; 166 op_metrics->om_bytes_recv += req->rq_reply_bytes_recvd; 167 168 delta = ktime_sub(req->rq_xtime, task->tk_start); 169 op_metrics->om_queue = ktime_add(op_metrics->om_queue, delta); 170 171 op_metrics->om_rtt = ktime_add(op_metrics->om_rtt, req->rq_rtt); 172 173 delta = ktime_sub(now, task->tk_start); 174 op_metrics->om_execute = ktime_add(op_metrics->om_execute, delta); 175 176 spin_unlock(&op_metrics->om_lock); 177 } 178 EXPORT_SYMBOL_GPL(rpc_count_iostats); 179 180 static void _print_name(struct seq_file *seq, unsigned int op, 181 struct rpc_procinfo *procs) 182 { 183 if (procs[op].p_name) 184 seq_printf(seq, "\t%12s: ", procs[op].p_name); 185 else if (op == 0) 186 seq_printf(seq, "\t NULL: "); 187 else 188 seq_printf(seq, "\t%12u: ", op); 189 } 190 191 void rpc_print_iostats(struct seq_file *seq, struct rpc_clnt *clnt) 192 { 193 struct rpc_iostats *stats = clnt->cl_metrics; 194 struct rpc_xprt *xprt; 195 unsigned int op, maxproc = clnt->cl_maxproc; 196 197 if (!stats) 198 return; 199 200 seq_printf(seq, "\tRPC iostats version: %s ", RPC_IOSTATS_VERS); 201 seq_printf(seq, "p/v: %u/%u (%s)\n", 202 clnt->cl_prog, clnt->cl_vers, clnt->cl_program->name); 203 204 rcu_read_lock(); 205 xprt = rcu_dereference(clnt->cl_xprt); 206 if (xprt) 207 xprt->ops->print_stats(xprt, seq); 208 rcu_read_unlock(); 209 210 seq_printf(seq, "\tper-op statistics\n"); 211 for (op = 0; op < maxproc; op++) { 212 struct rpc_iostats *metrics = &stats[op]; 213 _print_name(seq, op, clnt->cl_procinfo); 214 seq_printf(seq, "%lu %lu %lu %Lu %Lu %Lu %Lu %Lu\n", 215 metrics->om_ops, 216 metrics->om_ntrans, 217 metrics->om_timeouts, 218 metrics->om_bytes_sent, 219 metrics->om_bytes_recv, 220 ktime_to_ms(metrics->om_queue), 221 ktime_to_ms(metrics->om_rtt), 222 ktime_to_ms(metrics->om_execute)); 223 } 224 } 225 EXPORT_SYMBOL_GPL(rpc_print_iostats); 226 227 /* 228 * Register/unregister RPC proc files 229 */ 230 static inline struct proc_dir_entry * 231 do_register(struct net *net, const char *name, void *data, 232 const struct file_operations *fops) 233 { 234 struct sunrpc_net *sn; 235 236 dprintk("RPC: registering /proc/net/rpc/%s\n", name); 237 sn = net_generic(net, sunrpc_net_id); 238 return proc_create_data(name, 0, sn->proc_net_rpc, fops, data); 239 } 240 241 struct proc_dir_entry * 242 rpc_proc_register(struct net *net, struct rpc_stat *statp) 243 { 244 return do_register(net, statp->program->name, statp, &rpc_proc_fops); 245 } 246 EXPORT_SYMBOL_GPL(rpc_proc_register); 247 248 void 249 rpc_proc_unregister(struct net *net, const char *name) 250 { 251 struct sunrpc_net *sn; 252 253 sn = net_generic(net, sunrpc_net_id); 254 remove_proc_entry(name, sn->proc_net_rpc); 255 } 256 EXPORT_SYMBOL_GPL(rpc_proc_unregister); 257 258 struct proc_dir_entry * 259 svc_proc_register(struct net *net, struct svc_stat *statp, const struct file_operations *fops) 260 { 261 return do_register(net, statp->program->pg_name, statp, fops); 262 } 263 EXPORT_SYMBOL_GPL(svc_proc_register); 264 265 void 266 svc_proc_unregister(struct net *net, const char *name) 267 { 268 struct sunrpc_net *sn; 269 270 sn = net_generic(net, sunrpc_net_id); 271 remove_proc_entry(name, sn->proc_net_rpc); 272 } 273 EXPORT_SYMBOL_GPL(svc_proc_unregister); 274 275 int rpc_proc_init(struct net *net) 276 { 277 struct sunrpc_net *sn; 278 279 dprintk("RPC: registering /proc/net/rpc\n"); 280 sn = net_generic(net, sunrpc_net_id); 281 sn->proc_net_rpc = proc_mkdir("rpc", net->proc_net); 282 if (sn->proc_net_rpc == NULL) 283 return -ENOMEM; 284 285 return 0; 286 } 287 288 void rpc_proc_exit(struct net *net) 289 { 290 dprintk("RPC: unregistering /proc/net/rpc\n"); 291 remove_proc_entry("rpc", net->proc_net); 292 } 293 294