stats.c 6.8 KB

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