stats.c 6.9 KB

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