stats.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 <trace/events/sunrpc.h>
  25. #include "netns.h"
  26. #define RPCDBG_FACILITY RPCDBG_MISC
  27. /*
  28. * Get RPC client stats
  29. */
  30. static int rpc_proc_show(struct seq_file *seq, void *v) {
  31. const struct rpc_stat *statp = seq->private;
  32. const struct rpc_program *prog = statp->program;
  33. unsigned int i, j;
  34. seq_printf(seq,
  35. "net %u %u %u %u\n",
  36. statp->netcnt,
  37. statp->netudpcnt,
  38. statp->nettcpcnt,
  39. statp->nettcpconn);
  40. seq_printf(seq,
  41. "rpc %u %u %u\n",
  42. statp->rpccnt,
  43. statp->rpcretrans,
  44. statp->rpcauthrefresh);
  45. for (i = 0; i < prog->nrvers; i++) {
  46. const struct rpc_version *vers = prog->version[i];
  47. if (!vers)
  48. continue;
  49. seq_printf(seq, "proc%u %u",
  50. vers->number, vers->nrprocs);
  51. for (j = 0; j < vers->nrprocs; j++)
  52. seq_printf(seq, " %u", vers->counts[j]);
  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. {
  73. const struct svc_program *prog = statp->program;
  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. vers = prog->pg_vers[i];
  91. if (!vers)
  92. continue;
  93. seq_printf(seq, "proc%d %u", i, vers->vs_nproc);
  94. for (j = 0; j < vers->vs_nproc; j++)
  95. seq_printf(seq, " %u", vers->vs_count[j]);
  96. seq_putc(seq, '\n');
  97. }
  98. }
  99. EXPORT_SYMBOL_GPL(svc_seq_show);
  100. /**
  101. * rpc_alloc_iostats - allocate an rpc_iostats structure
  102. * @clnt: RPC program, version, and xprt
  103. *
  104. */
  105. struct rpc_iostats *rpc_alloc_iostats(struct rpc_clnt *clnt)
  106. {
  107. struct rpc_iostats *stats;
  108. int i;
  109. stats = kcalloc(clnt->cl_maxproc, sizeof(*stats), GFP_KERNEL);
  110. if (stats) {
  111. for (i = 0; i < clnt->cl_maxproc; i++)
  112. spin_lock_init(&stats[i].om_lock);
  113. }
  114. return stats;
  115. }
  116. EXPORT_SYMBOL_GPL(rpc_alloc_iostats);
  117. /**
  118. * rpc_free_iostats - release an rpc_iostats structure
  119. * @stats: doomed rpc_iostats structure
  120. *
  121. */
  122. void rpc_free_iostats(struct rpc_iostats *stats)
  123. {
  124. kfree(stats);
  125. }
  126. EXPORT_SYMBOL_GPL(rpc_free_iostats);
  127. /**
  128. * rpc_count_iostats_metrics - tally up per-task stats
  129. * @task: completed rpc_task
  130. * @op_metrics: stat structure for OP that will accumulate stats from @task
  131. */
  132. void rpc_count_iostats_metrics(const struct rpc_task *task,
  133. struct rpc_iostats *op_metrics)
  134. {
  135. struct rpc_rqst *req = task->tk_rqstp;
  136. ktime_t backlog, execute, now;
  137. if (!op_metrics || !req)
  138. return;
  139. now = ktime_get();
  140. spin_lock(&op_metrics->om_lock);
  141. op_metrics->om_ops++;
  142. /* kernel API: om_ops must never become larger than om_ntrans */
  143. op_metrics->om_ntrans += max(req->rq_ntrans, 1);
  144. op_metrics->om_timeouts += task->tk_timeouts;
  145. op_metrics->om_bytes_sent += req->rq_xmit_bytes_sent;
  146. op_metrics->om_bytes_recv += req->rq_reply_bytes_recvd;
  147. backlog = 0;
  148. if (ktime_to_ns(req->rq_xtime)) {
  149. backlog = ktime_sub(req->rq_xtime, task->tk_start);
  150. op_metrics->om_queue = ktime_add(op_metrics->om_queue, backlog);
  151. }
  152. op_metrics->om_rtt = ktime_add(op_metrics->om_rtt, req->rq_rtt);
  153. execute = ktime_sub(now, task->tk_start);
  154. op_metrics->om_execute = ktime_add(op_metrics->om_execute, execute);
  155. spin_unlock(&op_metrics->om_lock);
  156. trace_rpc_stats_latency(req->rq_task, backlog, req->rq_rtt, execute);
  157. }
  158. EXPORT_SYMBOL_GPL(rpc_count_iostats_metrics);
  159. /**
  160. * rpc_count_iostats - tally up per-task stats
  161. * @task: completed rpc_task
  162. * @stats: array of stat structures
  163. *
  164. * Uses the statidx from @task
  165. */
  166. void rpc_count_iostats(const struct rpc_task *task, struct rpc_iostats *stats)
  167. {
  168. rpc_count_iostats_metrics(task,
  169. &stats[task->tk_msg.rpc_proc->p_statidx]);
  170. }
  171. EXPORT_SYMBOL_GPL(rpc_count_iostats);
  172. static void _print_name(struct seq_file *seq, unsigned int op,
  173. const struct rpc_procinfo *procs)
  174. {
  175. if (procs[op].p_name)
  176. seq_printf(seq, "\t%12s: ", procs[op].p_name);
  177. else if (op == 0)
  178. seq_printf(seq, "\t NULL: ");
  179. else
  180. seq_printf(seq, "\t%12u: ", op);
  181. }
  182. static void _add_rpc_iostats(struct rpc_iostats *a, struct rpc_iostats *b)
  183. {
  184. a->om_ops += b->om_ops;
  185. a->om_ntrans += b->om_ntrans;
  186. a->om_timeouts += b->om_timeouts;
  187. a->om_bytes_sent += b->om_bytes_sent;
  188. a->om_bytes_recv += b->om_bytes_recv;
  189. a->om_queue = ktime_add(a->om_queue, b->om_queue);
  190. a->om_rtt = ktime_add(a->om_rtt, b->om_rtt);
  191. a->om_execute = ktime_add(a->om_execute, b->om_execute);
  192. }
  193. static void _print_rpc_iostats(struct seq_file *seq, struct rpc_iostats *stats,
  194. int op, const struct rpc_procinfo *procs)
  195. {
  196. _print_name(seq, op, procs);
  197. seq_printf(seq, "%lu %lu %lu %Lu %Lu %Lu %Lu %Lu\n",
  198. stats->om_ops,
  199. stats->om_ntrans,
  200. stats->om_timeouts,
  201. stats->om_bytes_sent,
  202. stats->om_bytes_recv,
  203. ktime_to_ms(stats->om_queue),
  204. ktime_to_ms(stats->om_rtt),
  205. ktime_to_ms(stats->om_execute));
  206. }
  207. void rpc_clnt_show_stats(struct seq_file *seq, struct rpc_clnt *clnt)
  208. {
  209. struct rpc_xprt *xprt;
  210. unsigned int op, maxproc = clnt->cl_maxproc;
  211. if (!clnt->cl_metrics)
  212. return;
  213. seq_printf(seq, "\tRPC iostats version: %s ", RPC_IOSTATS_VERS);
  214. seq_printf(seq, "p/v: %u/%u (%s)\n",
  215. clnt->cl_prog, clnt->cl_vers, clnt->cl_program->name);
  216. rcu_read_lock();
  217. xprt = rcu_dereference(clnt->cl_xprt);
  218. if (xprt)
  219. xprt->ops->print_stats(xprt, seq);
  220. rcu_read_unlock();
  221. seq_printf(seq, "\tper-op statistics\n");
  222. for (op = 0; op < maxproc; op++) {
  223. struct rpc_iostats stats = {};
  224. struct rpc_clnt *next = clnt;
  225. do {
  226. _add_rpc_iostats(&stats, &next->cl_metrics[op]);
  227. if (next == next->cl_parent)
  228. break;
  229. next = next->cl_parent;
  230. } while (next);
  231. _print_rpc_iostats(seq, &stats, op, clnt->cl_procinfo);
  232. }
  233. }
  234. EXPORT_SYMBOL_GPL(rpc_clnt_show_stats);
  235. /*
  236. * Register/unregister RPC proc files
  237. */
  238. static inline struct proc_dir_entry *
  239. do_register(struct net *net, const char *name, void *data,
  240. const struct file_operations *fops)
  241. {
  242. struct sunrpc_net *sn;
  243. dprintk("RPC: registering /proc/net/rpc/%s\n", name);
  244. sn = net_generic(net, sunrpc_net_id);
  245. return proc_create_data(name, 0, sn->proc_net_rpc, fops, data);
  246. }
  247. struct proc_dir_entry *
  248. rpc_proc_register(struct net *net, struct rpc_stat *statp)
  249. {
  250. return do_register(net, statp->program->name, statp, &rpc_proc_fops);
  251. }
  252. EXPORT_SYMBOL_GPL(rpc_proc_register);
  253. void
  254. rpc_proc_unregister(struct net *net, const char *name)
  255. {
  256. struct sunrpc_net *sn;
  257. sn = net_generic(net, sunrpc_net_id);
  258. remove_proc_entry(name, sn->proc_net_rpc);
  259. }
  260. EXPORT_SYMBOL_GPL(rpc_proc_unregister);
  261. struct proc_dir_entry *
  262. svc_proc_register(struct net *net, struct svc_stat *statp, const struct file_operations *fops)
  263. {
  264. return do_register(net, statp->program->pg_name, statp, fops);
  265. }
  266. EXPORT_SYMBOL_GPL(svc_proc_register);
  267. void
  268. svc_proc_unregister(struct net *net, const char *name)
  269. {
  270. struct sunrpc_net *sn;
  271. sn = net_generic(net, sunrpc_net_id);
  272. remove_proc_entry(name, sn->proc_net_rpc);
  273. }
  274. EXPORT_SYMBOL_GPL(svc_proc_unregister);
  275. int rpc_proc_init(struct net *net)
  276. {
  277. struct sunrpc_net *sn;
  278. dprintk("RPC: registering /proc/net/rpc\n");
  279. sn = net_generic(net, sunrpc_net_id);
  280. sn->proc_net_rpc = proc_mkdir("rpc", net->proc_net);
  281. if (sn->proc_net_rpc == NULL)
  282. return -ENOMEM;
  283. return 0;
  284. }
  285. void rpc_proc_exit(struct net *net)
  286. {
  287. dprintk("RPC: unregistering /proc/net/rpc\n");
  288. remove_proc_entry("rpc", net->proc_net);
  289. }