stats.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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_DATA(inode));
  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. struct rpc_iostats *stats;
  107. int i;
  108. stats = kcalloc(clnt->cl_maxproc, sizeof(*stats), GFP_KERNEL);
  109. if (stats) {
  110. for (i = 0; i < clnt->cl_maxproc; i++)
  111. spin_lock_init(&stats[i].om_lock);
  112. }
  113. return stats;
  114. }
  115. EXPORT_SYMBOL_GPL(rpc_alloc_iostats);
  116. /**
  117. * rpc_free_iostats - release an rpc_iostats structure
  118. * @stats: doomed rpc_iostats structure
  119. *
  120. */
  121. void rpc_free_iostats(struct rpc_iostats *stats)
  122. {
  123. kfree(stats);
  124. }
  125. EXPORT_SYMBOL_GPL(rpc_free_iostats);
  126. /**
  127. * rpc_count_iostats_metrics - tally up per-task stats
  128. * @task: completed rpc_task
  129. * @op_metrics: stat structure for OP that will accumulate stats from @task
  130. */
  131. void rpc_count_iostats_metrics(const struct rpc_task *task,
  132. struct rpc_iostats *op_metrics)
  133. {
  134. struct rpc_rqst *req = task->tk_rqstp;
  135. ktime_t delta, now;
  136. if (!op_metrics || !req)
  137. return;
  138. now = ktime_get();
  139. spin_lock(&op_metrics->om_lock);
  140. op_metrics->om_ops++;
  141. op_metrics->om_ntrans += req->rq_ntrans;
  142. op_metrics->om_timeouts += task->tk_timeouts;
  143. op_metrics->om_bytes_sent += req->rq_xmit_bytes_sent;
  144. op_metrics->om_bytes_recv += req->rq_reply_bytes_recvd;
  145. delta = ktime_sub(req->rq_xtime, task->tk_start);
  146. op_metrics->om_queue = ktime_add(op_metrics->om_queue, delta);
  147. op_metrics->om_rtt = ktime_add(op_metrics->om_rtt, req->rq_rtt);
  148. delta = ktime_sub(now, task->tk_start);
  149. op_metrics->om_execute = ktime_add(op_metrics->om_execute, delta);
  150. spin_unlock(&op_metrics->om_lock);
  151. }
  152. EXPORT_SYMBOL_GPL(rpc_count_iostats_metrics);
  153. /**
  154. * rpc_count_iostats - tally up per-task stats
  155. * @task: completed rpc_task
  156. * @stats: array of stat structures
  157. *
  158. * Uses the statidx from @task
  159. */
  160. void rpc_count_iostats(const struct rpc_task *task, struct rpc_iostats *stats)
  161. {
  162. rpc_count_iostats_metrics(task,
  163. &stats[task->tk_msg.rpc_proc->p_statidx]);
  164. }
  165. EXPORT_SYMBOL_GPL(rpc_count_iostats);
  166. static void _print_name(struct seq_file *seq, unsigned int op,
  167. struct rpc_procinfo *procs)
  168. {
  169. if (procs[op].p_name)
  170. seq_printf(seq, "\t%12s: ", procs[op].p_name);
  171. else if (op == 0)
  172. seq_printf(seq, "\t NULL: ");
  173. else
  174. seq_printf(seq, "\t%12u: ", op);
  175. }
  176. void rpc_print_iostats(struct seq_file *seq, struct rpc_clnt *clnt)
  177. {
  178. struct rpc_iostats *stats = clnt->cl_metrics;
  179. struct rpc_xprt *xprt;
  180. unsigned int op, maxproc = clnt->cl_maxproc;
  181. if (!stats)
  182. return;
  183. seq_printf(seq, "\tRPC iostats version: %s ", RPC_IOSTATS_VERS);
  184. seq_printf(seq, "p/v: %u/%u (%s)\n",
  185. clnt->cl_prog, clnt->cl_vers, clnt->cl_program->name);
  186. rcu_read_lock();
  187. xprt = rcu_dereference(clnt->cl_xprt);
  188. if (xprt)
  189. xprt->ops->print_stats(xprt, seq);
  190. rcu_read_unlock();
  191. seq_printf(seq, "\tper-op statistics\n");
  192. for (op = 0; op < maxproc; op++) {
  193. struct rpc_iostats *metrics = &stats[op];
  194. _print_name(seq, op, clnt->cl_procinfo);
  195. seq_printf(seq, "%lu %lu %lu %Lu %Lu %Lu %Lu %Lu\n",
  196. metrics->om_ops,
  197. metrics->om_ntrans,
  198. metrics->om_timeouts,
  199. metrics->om_bytes_sent,
  200. metrics->om_bytes_recv,
  201. ktime_to_ms(metrics->om_queue),
  202. ktime_to_ms(metrics->om_rtt),
  203. ktime_to_ms(metrics->om_execute));
  204. }
  205. }
  206. EXPORT_SYMBOL_GPL(rpc_print_iostats);
  207. /*
  208. * Register/unregister RPC proc files
  209. */
  210. static inline struct proc_dir_entry *
  211. do_register(struct net *net, const char *name, void *data,
  212. const struct file_operations *fops)
  213. {
  214. struct sunrpc_net *sn;
  215. dprintk("RPC: registering /proc/net/rpc/%s\n", name);
  216. sn = net_generic(net, sunrpc_net_id);
  217. return proc_create_data(name, 0, sn->proc_net_rpc, fops, data);
  218. }
  219. struct proc_dir_entry *
  220. rpc_proc_register(struct net *net, struct rpc_stat *statp)
  221. {
  222. return do_register(net, statp->program->name, statp, &rpc_proc_fops);
  223. }
  224. EXPORT_SYMBOL_GPL(rpc_proc_register);
  225. void
  226. rpc_proc_unregister(struct net *net, const char *name)
  227. {
  228. struct sunrpc_net *sn;
  229. sn = net_generic(net, sunrpc_net_id);
  230. remove_proc_entry(name, sn->proc_net_rpc);
  231. }
  232. EXPORT_SYMBOL_GPL(rpc_proc_unregister);
  233. struct proc_dir_entry *
  234. svc_proc_register(struct net *net, struct svc_stat *statp, const struct file_operations *fops)
  235. {
  236. return do_register(net, statp->program->pg_name, statp, fops);
  237. }
  238. EXPORT_SYMBOL_GPL(svc_proc_register);
  239. void
  240. svc_proc_unregister(struct net *net, const char *name)
  241. {
  242. struct sunrpc_net *sn;
  243. sn = net_generic(net, sunrpc_net_id);
  244. remove_proc_entry(name, sn->proc_net_rpc);
  245. }
  246. EXPORT_SYMBOL_GPL(svc_proc_unregister);
  247. int rpc_proc_init(struct net *net)
  248. {
  249. struct sunrpc_net *sn;
  250. dprintk("RPC: registering /proc/net/rpc\n");
  251. sn = net_generic(net, sunrpc_net_id);
  252. sn->proc_net_rpc = proc_mkdir("rpc", net->proc_net);
  253. if (sn->proc_net_rpc == NULL)
  254. return -ENOMEM;
  255. return 0;
  256. }
  257. void rpc_proc_exit(struct net *net)
  258. {
  259. dprintk("RPC: unregistering /proc/net/rpc\n");
  260. remove_proc_entry("rpc", net->proc_net);
  261. }