stats.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. { "bloom filter lookup time", },
  66. { "arena block reads", },
  67. { "arena block read bytes", },
  68. { "arena block writes", },
  69. { "arena block write bytes", },
  70. { "isect block reads", },
  71. { "isect block read bytes", },
  72. { "isect block writes", },
  73. { "isect block write bytes", },
  74. { "sum reads", },
  75. { "sum read bytes", },
  76. { "cig loads" },
  77. { "cig load time" },
  78. };
  79. QLock statslock;
  80. Stats stats;
  81. Stats *stathist;
  82. int nstathist;
  83. ulong statind;
  84. ulong stattime;
  85. void
  86. statsproc(void *v)
  87. {
  88. USED(v);
  89. for(;;){
  90. stats.now = time(0);
  91. stathist[stattime%nstathist] = stats;
  92. stattime++;
  93. sleep(1000);
  94. }
  95. }
  96. void
  97. statsinit(void)
  98. {
  99. nstathist = 90000;
  100. stathist = MKNZ(Stats, nstathist);
  101. vtproc(statsproc, nil);
  102. }
  103. void
  104. setstat(int index, long val)
  105. {
  106. qlock(&statslock);
  107. stats.n[index] = val;
  108. qunlock(&statslock);
  109. }
  110. void
  111. addstat(int index, int inc)
  112. {
  113. if(!collectstats)
  114. return;
  115. qlock(&statslock);
  116. stats.n[index] += inc;
  117. qunlock(&statslock);
  118. }
  119. void
  120. addstat2(int index, int inc, int index1, int inc1)
  121. {
  122. if(!collectstats)
  123. return;
  124. qlock(&statslock);
  125. stats.n[index] += inc;
  126. stats.n[index1] += inc1;
  127. qunlock(&statslock);
  128. }
  129. void
  130. printstats(void)
  131. {
  132. }
  133. void
  134. binstats(long (*fn)(Stats *s0, Stats *s1, void *arg), void *arg,
  135. long t0, long t1, Statbin *bin, int nbin)
  136. {
  137. long xt0, t, te, v;
  138. int i, j, lo, hi, m;
  139. vlong tot;
  140. Statbin *b;
  141. t = stats.now;
  142. /* negative times mean relative to now. */
  143. if(t0 <= 0)
  144. t0 += t;
  145. if(t1 <= 0)
  146. t1 += t;
  147. /* ten minute range if none given */
  148. if(t1 <= t0)
  149. t0 = t1 - 60*10;
  150. if(0) fprint(2, "stats %ld-%ld\n", t0, t1);
  151. /* binary search to find t0-1 or close */
  152. lo = stattime;
  153. hi = stattime+nstathist;
  154. while(lo+1 < hi){
  155. m = (lo+hi)/2;
  156. if(stathist[m%nstathist].now >= t0)
  157. hi = m;
  158. else
  159. lo = m;
  160. }
  161. xt0 = stathist[lo%nstathist].now;
  162. if(xt0 >= t1){
  163. /* no samples */
  164. memset(bin, 0, nbin*sizeof bin[0]);
  165. return;
  166. }
  167. hi = stattime+nstathist;
  168. j = lo+1;
  169. for(i=0; i<nbin; i++){
  170. te = t0 + (t1-t0)*i/nbin;
  171. b = &bin[i];
  172. memset(b, 0, sizeof *b);
  173. tot = 0;
  174. for(; j<hi && stathist[j%nstathist].now<te; j++){
  175. v = fn(&stathist[(j-1)%nstathist], &stathist[j%nstathist], arg);
  176. if(b->nsamp==0 || v < b->min)
  177. b->min = v;
  178. if(b->nsamp==0 || v > b->max)
  179. b->max = v;
  180. tot += v;
  181. b->nsamp++;
  182. }
  183. if(b->nsamp)
  184. b->avg = tot / b->nsamp;
  185. if(b->nsamp==0 && i>0)
  186. *b = bin[i-1];
  187. }
  188. }