Blob


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