stats.c 6.5 KB

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