stats.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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", vers->counts[j]);
  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_DATA(inode));
  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. {
  72. const struct svc_program *prog = statp->program;
  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. vers = prog->pg_vers[i];
  90. if (!vers)
  91. continue;
  92. seq_printf(seq, "proc%d %u", i, vers->vs_nproc);
  93. for (j = 0; j < vers->vs_nproc; j++)
  94. seq_printf(seq, " %u", vers->vs_count[j]);
  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. /* kernel API: om_ops must never become larger than om_ntrans */
  142. op_metrics->om_ntrans += max(req->rq_ntrans, 1);
  143. op_metrics->om_timeouts += task->tk_timeouts;
  144. op_metrics->om_bytes_sent += req->rq_xmit_bytes_sent;
  145. op_metrics->om_bytes_recv += req->rq_reply_bytes_recvd;
  146. if (ktime_to_ns(req->rq_xtime)) {
  147. delta = ktime_sub(req->rq_xtime, task->tk_start);
  148. op_metrics->om_queue = ktime_add(op_metrics->om_queue, delta);
  149. }
  150. op_metrics->om_rtt = ktime_add(op_metrics->om_rtt, req->rq_rtt);
  151. delta = ktime_sub(now, task->tk_start);
  152. op_metrics->om_execute = ktime_add(op_metrics->om_execute, delta);
  153. spin_unlock(&op_metrics->om_lock);
  154. }
  155. EXPORT_SYMBOL_GPL(rpc_count_iostats_metrics);
  156. /**
  157. * rpc_count_iostats - tally up per-task stats
  158. * @task: completed rpc_task
  159. * @stats: array of stat structures
  160. *
  161. * Uses the statidx from @task
  162. */
  163. void rpc_count_iostats(const struct rpc_task *task, struct rpc_iostats *stats)
  164. {
  165. rpc_count_iostats_metrics(task,
  166. &stats[task->tk_msg.rpc_proc->p_statidx]);
  167. }
  168. EXPORT_SYMBOL_GPL(rpc_count_iostats);
  169. static void _print_name(struct seq_file *seq, unsigned int op,
  170. const struct rpc_procinfo *procs)
  171. {
  172. if (procs[op].p_name)
  173. seq_printf(seq, "\t%12s: ", procs[op].p_name);
  174. else if (op == 0)
  175. seq_printf(seq, "\t NULL: ");
  176. else
  177. seq_printf(seq, "\t%12u: ", op);
  178. }
  179. void rpc_print_iostats(struct seq_file *seq, struct rpc_clnt *clnt)
  180. {
  181. struct rpc_iostats *stats = clnt->cl_metrics;
  182. struct rpc_xprt *xprt;
  183. unsigned int op, maxproc = clnt->cl_maxproc;
  184. if (!stats)
  185. return;
  186. seq_printf(seq, "\tRPC iostats version: %s ", RPC_IOSTATS_VERS);
  187. seq_printf(seq, "p/v: %u/%u (%s)\n",
  188. clnt->cl_prog, clnt->cl_vers, clnt->cl_program->name);
  189. rcu_read_lock();
  190. xprt = rcu_dereference(clnt->cl_xprt);
  191. if (xprt)
  192. xprt->ops->print_stats(xprt, seq);
  193. rcu_read_unlock();
  194. seq_printf(seq, "\tper-op statistics\n");
  195. for (op = 0; op < maxproc; op++) {
  196. struct rpc_iostats *metrics = &stats[op];
  197. _print_name(seq, op, clnt->cl_procinfo);
  198. seq_printf(seq, "%lu %lu %lu %Lu %Lu %Lu %Lu %Lu\n",
  199. metrics->om_ops,
  200. metrics->om_ntrans,
  201. metrics->om_timeouts,
  202. metrics->om_bytes_sent,
  203. metrics->om_bytes_recv,
  204. ktime_to_ms(metrics->om_queue),
  205. ktime_to_ms(metrics->om_rtt),
  206. ktime_to_ms(metrics->om_execute));
  207. }
  208. }
  209. EXPORT_SYMBOL_GPL(rpc_print_iostats);
  210. /*
  211. * Register/unregister RPC proc files
  212. */
  213. static inline struct proc_dir_entry *
  214. do_register(struct net *net, const char *name, void *data,
  215. const struct file_operations *fops)
  216. {
  217. struct sunrpc_net *sn;
  218. dprintk("RPC: registering /proc/net/rpc/%s\n", name);
  219. sn = net_generic(net, sunrpc_net_id);
  220. return proc_create_data(name, 0, sn->proc_net_rpc, fops, data);
  221. }
  222. struct proc_dir_entry *
  223. rpc_proc_register(struct net *net, struct rpc_stat *statp)
  224. {
  225. return do_register(net, statp->program->name, statp, &rpc_proc_fops);
  226. }
  227. EXPORT_SYMBOL_GPL(rpc_proc_register);
  228. void
  229. rpc_proc_unregister(struct net *net, const char *name)
  230. {
  231. struct sunrpc_net *sn;
  232. sn = net_generic(net, sunrpc_net_id);
  233. remove_proc_entry(name, sn->proc_net_rpc);
  234. }
  235. EXPORT_SYMBOL_GPL(rpc_proc_unregister);
  236. struct proc_dir_entry *
  237. svc_proc_register(struct net *net, struct svc_stat *statp, const struct file_operations *fops)
  238. {
  239. return do_register(net, statp->program->pg_name, statp, fops);
  240. }
  241. EXPORT_SYMBOL_GPL(svc_proc_register);
  242. void
  243. svc_proc_unregister(struct net *net, const char *name)
  244. {
  245. struct sunrpc_net *sn;
  246. sn = net_generic(net, sunrpc_net_id);
  247. remove_proc_entry(name, sn->proc_net_rpc);
  248. }
  249. EXPORT_SYMBOL_GPL(svc_proc_unregister);
  250. int rpc_proc_init(struct net *net)
  251. {
  252. struct sunrpc_net *sn;
  253. dprintk("RPC: registering /proc/net/rpc\n");
  254. sn = net_generic(net, sunrpc_net_id);
  255. sn->proc_net_rpc = proc_mkdir("rpc", net->proc_net);
  256. if (sn->proc_net_rpc == NULL)
  257. return -ENOMEM;
  258. return 0;
  259. }
  260. void rpc_proc_exit(struct net *net)
  261. {
  262. dprintk("RPC: unregistering /proc/net/rpc\n");
  263. remove_proc_entry("rpc", net->proc_net);
  264. }