devws.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. /*
  16. * reported times can be translated to a more readable format by
  17. * using something like:
  18. * awk '{printf("print(\"%s: %s times; %s us worst; %s ws total\");\nsrc(%s)\n",
  19. * $1, $3, $4, $5, $2); }' | acid ../k10/9k8cpu
  20. * on the wsdata file, after doing a sort +2nr on it.
  21. */
  22. enum{
  23. WSdirqid,
  24. WSdataqid,
  25. WSctlqid,
  26. };
  27. Dirtab Wstab[]={
  28. {".", {WSdirqid, 0, QTDIR},0, DMDIR|0550},
  29. {"wsdata", {WSdataqid}, 0, 0600},
  30. {"wsctl", {WSctlqid}, 0, 0600},
  31. };
  32. /*
  33. * waitstats functions are in taslock.c, because they use Locks but
  34. * callers in taslock.c must not call them to avoid
  35. * a loop.
  36. * This is only the user interface.
  37. */
  38. static char*
  39. collect(void)
  40. {
  41. extern Lock waitstatslk;
  42. char *buf, *s;
  43. int i, n;
  44. static char *wname[] = {
  45. [WSlock] = "lock",
  46. [WSqlock] = "qlock",
  47. [WSslock] = "slock",
  48. };
  49. n = waitstats.npcs * (strlen("slock") + 1 + 19 * 3 + 1) + 1;
  50. buf = smalloc(n);
  51. s = buf;
  52. lock(&waitstatslk);
  53. for(i = 0; i < NWstats; i++)
  54. if(waitstats.pcs[i] != 0)
  55. s = seprint(s, buf+n, "%s %#llx %d %#llu %#llu\n",
  56. wname[waitstats.type[i]],
  57. waitstats.pcs[i], waitstats.ns[i], waitstats.wait[i],
  58. waitstats.total[i]);
  59. unlock(&waitstatslk);
  60. if(s == buf + n)
  61. print("collect: fix devws.c, buffer was too short");
  62. return buf;
  63. }
  64. static Chan*
  65. wsattach(char *spec)
  66. {
  67. return devattach('W', spec);
  68. }
  69. static Walkqid*
  70. wswalk(Chan *c, Chan *nc, char **name, int nname)
  71. {
  72. return devwalk(c, nc, name, nname, Wstab, nelem(Wstab), devgen);
  73. }
  74. static int32_t
  75. wsstat(Chan *c, uint8_t *db, int32_t n)
  76. {
  77. return devstat(c, db, n, Wstab, nelem(Wstab), devgen);
  78. }
  79. static Chan*
  80. wsopen(Chan *c, int omode)
  81. {
  82. if(c->qid.type & QTDIR){
  83. if(omode != OREAD)
  84. error(Eperm);
  85. }
  86. c->mode = openmode(omode);
  87. c->flag |= COPEN;
  88. c->offset = 0;
  89. c->aux = nil;
  90. if(c->qid.path == WSdataqid)
  91. c->aux = collect();
  92. return c;
  93. }
  94. static void
  95. wsclose(Chan *c)
  96. {
  97. free(c->aux);
  98. }
  99. static int32_t
  100. wsread(Chan *c, void *va, int32_t n, int64_t off)
  101. {
  102. switch((int)c->qid.path){
  103. case WSdirqid:
  104. n = devdirread(c, va, n, Wstab, nelem(Wstab), devgen);
  105. break;
  106. case WSdataqid:
  107. n = readstr(off, va, n, c->aux);
  108. break;
  109. default:
  110. n = 0;
  111. }
  112. return n;
  113. }
  114. static int32_t
  115. wswrite(Chan *c, void *a, int32_t n, int64_t m)
  116. {
  117. char *buf;
  118. switch((int)(c->qid.path)){
  119. case WSctlqid:
  120. buf = smalloc(n + 1);
  121. memmove(buf, a, n);
  122. buf[n] = 0;
  123. if(n > 0 && buf[n-1] == '\n')
  124. buf[n-1] = 0;
  125. if(strcmp(buf, "clear") == 0){
  126. lockstats.locks = lockstats.glare = lockstats.inglare = 0;
  127. qlockstats.qlock = qlockstats.qlockq = 0;
  128. clearwaitstats();
  129. }else if(strcmp(buf, "start") == 0)
  130. startwaitstats(1);
  131. else if(strcmp(buf, "stop") == 0)
  132. startwaitstats(0);
  133. else{
  134. free(buf);
  135. error(Ebadctl);
  136. }
  137. free(buf);
  138. break;
  139. default:
  140. error(Ebadusefd);
  141. }
  142. return n;
  143. }
  144. Dev wsdevtab = {
  145. .dc = 'W',
  146. .name = "waitstats",
  147. .reset = devreset,
  148. .init = devinit,
  149. .shutdown = devshutdown,
  150. .attach = wsattach,
  151. .walk = wswalk,
  152. .stat = wsstat,
  153. .open = wsopen,
  154. .create = devcreate,
  155. .close = wsclose,
  156. .read = wsread,
  157. .bread = devbread,
  158. .write = wswrite,
  159. .bwrite = devbwrite,
  160. .remove = devremove,
  161. .wstat = devwstat,
  162. };