stats.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. int collectstats = 1;
  5. /* keep in sync with dat.h:/NStat */
  6. Statdesc statdesc[NStat] =
  7. {
  8. { "rpc total", },
  9. { "rpc reads", },
  10. { "rpc reads ok", },
  11. { "rpc reads failed", },
  12. { "rpc read bytes", },
  13. { "rpc read time", },
  14. { "rpc read cached", },
  15. { "rpc read cached time", },
  16. { "rpc read uncached", },
  17. { "rpc read uncached time "},
  18. { "rpc writes", },
  19. { "rpc writes new", },
  20. { "rpc writes old", },
  21. { "rpc writes failed", },
  22. { "rpc write bytes", },
  23. { "rpc write time", },
  24. { "rpc write new time", },
  25. { "rpc write old time", },
  26. { "lump cache hits", },
  27. { "lump cache misses", },
  28. { "lump cache reads", },
  29. { "lump cache writes", },
  30. { "lump cache size", },
  31. { "lump cache stall", },
  32. { "lump cache read time", },
  33. { "disk cache hits", },
  34. { "disk cache misses", },
  35. { "disk cache lookups", },
  36. { "disk cache reads", },
  37. { "disk cache writes", },
  38. { "disk cache dirty", },
  39. { "disk cache size", },
  40. { "disk cache flushes", },
  41. { "disk cache stalls", },
  42. { "disk cache lookup time", },
  43. { "disk block stalls", },
  44. { "lump stalls", },
  45. { "index cache hits", },
  46. { "index cache misses", },
  47. { "index cache reads", },
  48. { "index cache writes", },
  49. { "index cache fills", },
  50. { "index cache prefetches", },
  51. { "index cache dirty", },
  52. { "index cache size", },
  53. { "index cache flushes", },
  54. { "index cache stalls", },
  55. { "index cache read time", },
  56. { "index cache lookups" },
  57. { "index cache summary hits" },
  58. { "index cache summary prefetches" },
  59. { "bloom filter hits", },
  60. { "bloom filter misses", },
  61. { "bloom filter false misses", },
  62. { "bloom filter lookups", },
  63. { "bloom filter ones", },
  64. { "bloom filter bits", },
  65. { "arena block reads", },
  66. { "arena block read bytes", },
  67. { "arena block writes", },
  68. { "arena block write bytes", },
  69. { "isect block reads", },
  70. { "isect block read bytes", },
  71. { "isect block writes", },
  72. { "isect block write bytes", },
  73. { "sum reads", },
  74. { "sum read bytes", },
  75. { "cig loads" },
  76. { "cig load time" },
  77. };
  78. QLock statslock;
  79. Stats stats;
  80. Stats *stathist;
  81. int nstathist;
  82. ulong statind;
  83. ulong stattime;
  84. void
  85. statsproc(void *v)
  86. {
  87. USED(v);
  88. for(;;){
  89. stats.now = time(0);
  90. stathist[stattime%nstathist] = stats;
  91. stattime++;
  92. sleep(1000);
  93. }
  94. }
  95. void
  96. statsinit(void)
  97. {
  98. nstathist = 90000;
  99. stathist = MKNZ(Stats, nstathist);
  100. vtproc(statsproc, nil);
  101. }
  102. void
  103. setstat(int index, long val)
  104. {
  105. qlock(&statslock);
  106. stats.n[index] = val;
  107. qunlock(&statslock);
  108. }
  109. void
  110. addstat(int index, int inc)
  111. {
  112. if(!collectstats)
  113. return;
  114. qlock(&statslock);
  115. stats.n[index] += inc;
  116. qunlock(&statslock);
  117. }
  118. void
  119. addstat2(int index, int inc, int index1, int inc1)
  120. {
  121. if(!collectstats)
  122. return;
  123. qlock(&statslock);
  124. stats.n[index] += inc;
  125. stats.n[index1] += inc1;
  126. qunlock(&statslock);
  127. }
  128. void
  129. printstats(void)
  130. {
  131. }
  132. void
  133. binstats(long (*fn)(Stats *s0, Stats *s1, void *arg), void *arg,
  134. long t0, long t1, Statbin *bin, int nbin)
  135. {
  136. long xt0, t, te, v;
  137. int i, j, lo, hi, m;
  138. vlong tot;
  139. Statbin *b;
  140. t = stats.now;
  141. /* negative times mean relative to now. */
  142. if(t0 <= 0)
  143. t0 += t;
  144. if(t1 <= 0)
  145. t1 += t;
  146. /* ten minute range if none given */
  147. if(t1 <= t0)
  148. t0 = t1 - 60*10;
  149. if(0) fprint(2, "stats %ld-%ld\n", t0, t1);
  150. /* binary search to find t0-1 or close */
  151. lo = stattime;
  152. hi = stattime+nstathist;
  153. while(lo+1 < hi){
  154. m = (lo+hi)/2;
  155. if(stathist[m%nstathist].now >= t0)
  156. hi = m;
  157. else
  158. lo = m;
  159. }
  160. xt0 = stathist[lo%nstathist].now;
  161. if(xt0 >= t1){
  162. /* no samples */
  163. memset(bin, 0, nbin*sizeof bin[0]);
  164. return;
  165. }
  166. hi = stattime+nstathist;
  167. j = lo+1;
  168. for(i=0; i<nbin; i++){
  169. te = t0 + (t1-t0)*i/nbin;
  170. b = &bin[i];
  171. memset(b, 0, sizeof *b);
  172. tot = 0;
  173. for(; j<hi && stathist[j%nstathist].now<te; j++){
  174. v = fn(&stathist[(j-1)%nstathist], &stathist[j%nstathist], arg);
  175. if(b->nsamp==0 || v < b->min)
  176. b->min = v;
  177. if(b->nsamp==0 || v > b->max)
  178. b->max = v;
  179. tot += v;
  180. b->nsamp++;
  181. }
  182. if(b->nsamp)
  183. b->avg = tot / b->nsamp;
  184. if(b->nsamp==0 && i>0)
  185. *b = bin[i-1];
  186. }
  187. }