stats.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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/sched.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. #define RPCDBG_FACILITY RPCDBG_MISC
  24. struct proc_dir_entry *proc_net_rpc = NULL;
  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. 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 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. 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. /**
  98. * rpc_alloc_iostats - allocate an rpc_iostats structure
  99. * @clnt: RPC program, version, and xprt
  100. *
  101. */
  102. struct rpc_iostats *rpc_alloc_iostats(struct rpc_clnt *clnt)
  103. {
  104. unsigned int ops = clnt->cl_maxproc;
  105. size_t size = ops * sizeof(struct rpc_iostats);
  106. struct rpc_iostats *new;
  107. new = kmalloc(size, GFP_KERNEL);
  108. if (new)
  109. memset(new, 0 , size);
  110. return new;
  111. }
  112. EXPORT_SYMBOL(rpc_alloc_iostats);
  113. /**
  114. * rpc_free_iostats - release an rpc_iostats structure
  115. * @stats: doomed rpc_iostats structure
  116. *
  117. */
  118. void rpc_free_iostats(struct rpc_iostats *stats)
  119. {
  120. kfree(stats);
  121. }
  122. EXPORT_SYMBOL(rpc_free_iostats);
  123. /**
  124. * rpc_count_iostats - tally up per-task stats
  125. * @task: completed rpc_task
  126. *
  127. * Relies on the caller for serialization.
  128. */
  129. void rpc_count_iostats(struct rpc_task *task)
  130. {
  131. struct rpc_rqst *req = task->tk_rqstp;
  132. struct rpc_iostats *stats = task->tk_client->cl_metrics;
  133. struct rpc_iostats *op_metrics;
  134. long rtt, execute, queue;
  135. if (!stats || !req)
  136. return;
  137. op_metrics = &stats[task->tk_msg.rpc_proc->p_statidx];
  138. op_metrics->om_ops++;
  139. op_metrics->om_ntrans += req->rq_ntrans;
  140. op_metrics->om_timeouts += task->tk_timeouts;
  141. op_metrics->om_bytes_sent += task->tk_bytes_sent;
  142. op_metrics->om_bytes_recv += req->rq_received;
  143. queue = (long)req->rq_xtime - task->tk_start;
  144. if (queue < 0)
  145. queue = -queue;
  146. op_metrics->om_queue += queue;
  147. rtt = task->tk_rtt;
  148. if (rtt < 0)
  149. rtt = -rtt;
  150. op_metrics->om_rtt += rtt;
  151. execute = (long)jiffies - task->tk_start;
  152. if (execute < 0)
  153. execute = -execute;
  154. op_metrics->om_execute += execute;
  155. }
  156. static void _print_name(struct seq_file *seq, unsigned int op,
  157. struct rpc_procinfo *procs)
  158. {
  159. if (procs[op].p_name)
  160. seq_printf(seq, "\t%12s: ", procs[op].p_name);
  161. else if (op == 0)
  162. seq_printf(seq, "\t NULL: ");
  163. else
  164. seq_printf(seq, "\t%12u: ", op);
  165. }
  166. #define MILLISECS_PER_JIFFY (1000 / HZ)
  167. void rpc_print_iostats(struct seq_file *seq, struct rpc_clnt *clnt)
  168. {
  169. struct rpc_iostats *stats = clnt->cl_metrics;
  170. struct rpc_xprt *xprt = clnt->cl_xprt;
  171. unsigned int op, maxproc = clnt->cl_maxproc;
  172. if (!stats)
  173. return;
  174. seq_printf(seq, "\tRPC iostats version: %s ", RPC_IOSTATS_VERS);
  175. seq_printf(seq, "p/v: %u/%u (%s)\n",
  176. clnt->cl_prog, clnt->cl_vers, clnt->cl_protname);
  177. if (xprt)
  178. xprt->ops->print_stats(xprt, seq);
  179. seq_printf(seq, "\tper-op statistics\n");
  180. for (op = 0; op < maxproc; op++) {
  181. struct rpc_iostats *metrics = &stats[op];
  182. _print_name(seq, op, clnt->cl_procinfo);
  183. seq_printf(seq, "%lu %lu %lu %Lu %Lu %Lu %Lu %Lu\n",
  184. metrics->om_ops,
  185. metrics->om_ntrans,
  186. metrics->om_timeouts,
  187. metrics->om_bytes_sent,
  188. metrics->om_bytes_recv,
  189. metrics->om_queue * MILLISECS_PER_JIFFY,
  190. metrics->om_rtt * MILLISECS_PER_JIFFY,
  191. metrics->om_execute * MILLISECS_PER_JIFFY);
  192. }
  193. }
  194. EXPORT_SYMBOL(rpc_print_iostats);
  195. /*
  196. * Register/unregister RPC proc files
  197. */
  198. static inline struct proc_dir_entry *
  199. do_register(const char *name, void *data, const struct file_operations *fops)
  200. {
  201. struct proc_dir_entry *ent;
  202. rpc_proc_init();
  203. dprintk("RPC: registering /proc/net/rpc/%s\n", name);
  204. ent = create_proc_entry(name, 0, proc_net_rpc);
  205. if (ent) {
  206. ent->proc_fops = fops;
  207. ent->data = data;
  208. }
  209. return ent;
  210. }
  211. struct proc_dir_entry *
  212. rpc_proc_register(struct rpc_stat *statp)
  213. {
  214. return do_register(statp->program->name, statp, &rpc_proc_fops);
  215. }
  216. void
  217. rpc_proc_unregister(const char *name)
  218. {
  219. remove_proc_entry(name, proc_net_rpc);
  220. }
  221. struct proc_dir_entry *
  222. svc_proc_register(struct svc_stat *statp, const struct file_operations *fops)
  223. {
  224. return do_register(statp->program->pg_name, statp, fops);
  225. }
  226. void
  227. svc_proc_unregister(const char *name)
  228. {
  229. remove_proc_entry(name, proc_net_rpc);
  230. }
  231. void
  232. rpc_proc_init(void)
  233. {
  234. dprintk("RPC: registering /proc/net/rpc\n");
  235. if (!proc_net_rpc) {
  236. struct proc_dir_entry *ent;
  237. ent = proc_mkdir("rpc", proc_net);
  238. if (ent) {
  239. ent->owner = THIS_MODULE;
  240. proc_net_rpc = ent;
  241. }
  242. }
  243. }
  244. void
  245. rpc_proc_exit(void)
  246. {
  247. dprintk("RPC: unregistering /proc/net/rpc\n");
  248. if (proc_net_rpc) {
  249. proc_net_rpc = NULL;
  250. remove_proc_entry("net/rpc", NULL);
  251. }
  252. }