Ignore *.exe
[awesomized/libmemcached] / clients / ms_stats.c
1 /*
2 * File: ms_stats.h
3 * Author: Mingqiang Zhuang
4 *
5 * Created on March 25, 2009
6 *
7 * (c) Copyright 2009, Schooner Information Technology, Inc.
8 * http://www.schoonerinfotech.com/
9 *
10 */
11
12 #include "config.h"
13
14 #include <inttypes.h>
15 #include "ms_stats.h"
16
17 #define array_size(x) (sizeof(x) / sizeof((x)[0]))
18
19 static int ms_local_log2(uint64_t value);
20 static uint64_t ms_get_events(ms_stat_t *stat);
21
22
23 /**
24 * get the index of local log2 array
25 *
26 * @param value
27 *
28 * @return return the index of local log2 array
29 */
30 static int ms_local_log2(uint64_t value)
31 {
32 int result= 0;
33
34 while (result <= 63 && ((uint64_t)1 << result) < value)
35 {
36 result++;
37 }
38
39 return result;
40 } /* ms_local_log2 */
41
42
43 /**
44 * initialize statistic structure
45 *
46 * @param stat, pointer of the statistic structure
47 * @param name, name of the statistic
48 */
49 void ms_init_stats(ms_stat_t *stat, const char *name)
50 {
51 memset(stat, 0, sizeof(*stat));
52
53 stat->name= (char *)name;
54 stat->min_time= (uint64_t)-1;
55 stat->max_time= 0;
56 stat->period_min_time= (uint64_t)-1;
57 stat->period_max_time= 0;
58 stat->log_product= 0;
59 stat->total_time= 0;
60 stat->pre_total_time= 0;
61 stat->squares= 0;
62 stat->pre_squares= 0;
63 stat->pre_events= 0;
64 stat->pre_log_product= 0;
65 stat->get_miss= 0;
66 stat->pre_get_miss= 0;
67 } /* ms_init_stats */
68
69
70 /**
71 * record one event
72 *
73 * @param stat, pointer of the statistic structure
74 * @param total_time, response time of the command
75 * @param get_miss, whether it gets miss
76 */
77 void ms_record_event(ms_stat_t *stat, uint64_t total_time, int get_miss)
78 {
79 stat->total_time+= total_time;
80
81 if (total_time < stat->min_time)
82 {
83 stat->min_time= total_time;
84 }
85
86 if (total_time > stat->max_time)
87 {
88 stat->max_time= total_time;
89 }
90
91 if (total_time < stat->period_min_time)
92 {
93 stat->period_min_time= total_time;
94 }
95
96 if (total_time > stat->period_max_time)
97 {
98 stat->period_max_time= total_time;
99 }
100
101 if (get_miss)
102 {
103 stat->get_miss++;
104 }
105
106 stat->dist[ms_local_log2(total_time)]++;
107 stat->squares+= (double)(total_time * total_time);
108
109 if (total_time != 0)
110 {
111 stat->log_product+= log((double)total_time);
112 }
113 } /* ms_record_event */
114
115
116 /**
117 * get the events count
118 *
119 * @param stat, pointer of the statistic structure
120 *
121 * @return total events recorded
122 */
123 static uint64_t ms_get_events(ms_stat_t *stat)
124 {
125 uint64_t events= 0;
126
127 for (uint32_t i= 0; i < array_size(stat->dist); i++)
128 {
129 events+= stat->dist[i];
130 }
131
132 return events;
133 } /* ms_get_events */
134
135
136 /**
137 * dump the statistics
138 *
139 * @param stat, pointer of the statistic structure
140 */
141 void ms_dump_stats(ms_stat_t *stat)
142 {
143 uint64_t events= 0;
144 int max_non_zero= 0;
145 int min_non_zero= 0;
146 double average= 0;
147
148 for (uint32_t i= 0; i < array_size(stat->dist); i++)
149 {
150 events+= stat->dist[i];
151 if (stat->dist[i] != 0)
152 {
153 max_non_zero= (int)i;
154 }
155 }
156
157 if (events == 0)
158 {
159 return;
160 }
161 average= (double)(stat->total_time / events);
162
163 printf("%s Statistics (%lld events)\n", stat->name, (long long)events);
164 printf(" Min: %8lld\n", (long long)stat->min_time);
165 printf(" Max: %8lld\n", (long long)stat->max_time);
166 printf(" Avg: %8lld\n", (long long)(stat->total_time / events));
167 printf(" Geo: %8.2lf\n", exp(stat->log_product / (double)events));
168
169 if (events > 1)
170 {
171 printf(" Std: %8.2lf\n",
172 sqrt((stat->squares - (double)events * average
173 * average) / ((double)events - 1)));
174 }
175 printf(" Log2 Dist:");
176
177 for (int i= 0; i <= max_non_zero - 4; i+= 4)
178 {
179 if ((stat->dist[i + 0] != 0)
180 || (stat->dist[i + 1] != 0)
181 || (stat->dist[i + 2] != 0)
182 || (stat->dist[i + 3] != 0))
183 {
184 min_non_zero= i;
185 break;
186 }
187 }
188
189 for (int i= min_non_zero; i <= max_non_zero; i++)
190 {
191 if ((i % 4) == 0)
192 {
193 printf("\n %2d:", (int)i);
194 }
195 printf(" %6" PRIu64 , stat->dist[i]);
196 }
197
198 printf("\n\n");
199 } /* ms_dump_stats */
200
201
202 /**
203 * dump the format statistics
204 *
205 * @param stat, pointer of the statistic structure
206 * @param run_time, the total run time
207 * @param freq, statistic frequency
208 * @param obj_size, average object size
209 */
210 void ms_dump_format_stats(ms_stat_t *stat,
211 int run_time,
212 int freq,
213 int obj_size)
214 {
215 uint64_t events= 0;
216 double global_average= 0;
217 uint64_t global_tps= 0;
218 double global_rate= 0;
219 double global_std= 0;
220 double global_log= 0;
221
222 uint64_t diff_time= 0;
223 uint64_t diff_events= 0;
224 double diff_squares= 0;
225 double diff_log_product= 0;
226 double period_average= 0;
227 uint64_t period_tps= 0;
228 double period_rate= 0;
229 double period_std= 0;
230 double period_log= 0;
231
232 if ((events= ms_get_events(stat)) == 0)
233 {
234 return;
235 }
236
237 global_average= (double)(stat->total_time / events);
238 global_tps= events / (uint64_t)run_time;
239 global_rate= (double)events * obj_size / 1024 / 1024 / run_time;
240 global_std= sqrt((stat->squares - (double)events * global_average
241 * global_average) / (double)(events - 1));
242 global_log= exp(stat->log_product / (double)events);
243
244 diff_time= stat->total_time - stat->pre_total_time;
245 diff_events= events - stat->pre_events;
246 if (diff_events >= 1)
247 {
248 period_average= (double)(diff_time / diff_events);
249 period_tps= diff_events / (uint64_t)freq;
250 period_rate= (double)diff_events * obj_size / 1024 / 1024 / freq;
251 diff_squares= (double)stat->squares - (double)stat->pre_squares;
252 period_std= sqrt((diff_squares - (double)diff_events * period_average
253 * period_average) / (double)(diff_events - 1));
254 diff_log_product= stat->log_product - stat->pre_log_product;
255 period_log= exp(diff_log_product / (double)diff_events);
256 }
257
258 printf("%s Statistics\n", stat->name);
259 printf("%-8s %-8s %-12s %-12s %-10s %-10s %-8s %-10s %-10s %-10s %-10s\n",
260 "Type",
261 "Time(s)",
262 "Ops",
263 "TPS(ops/s)",
264 "Net(M/s)",
265 "Get_miss",
266 "Min(us)",
267 "Max(us)",
268 "Avg(us)",
269 "Std_dev",
270 "Geo_dist");
271
272 printf(
273 "%-8s %-8d %-12llu %-12lld %-10.1f %-10lld %-8lld %-10lld %-10lld %-10.2f %.2f\n",
274 "Period",
275 freq,
276 (long long)diff_events,
277 (long long)period_tps,
278 global_rate,
279 (long long)(stat->get_miss - stat->pre_get_miss),
280 (long long)stat->period_min_time,
281 (long long)stat->period_max_time,
282 (long long)period_average,
283 period_std,
284 period_log);
285
286 printf(
287 "%-8s %-8d %-12llu %-12lld %-10.1f %-10lld %-8lld %-10lld %-10lld %-10.2f %.2f\n\n",
288 "Global",
289 run_time,
290 (long long)events,
291 (long long)global_tps,
292 period_rate,
293 (long long)stat->get_miss,
294 (long long)stat->min_time,
295 (long long)stat->max_time,
296 (long long)global_average,
297 global_std,
298 global_log);
299
300 stat->pre_events= events;
301 stat->pre_squares= (uint64_t)stat->squares;
302 stat->pre_total_time= stat->total_time;
303 stat->pre_log_product= stat->log_product;
304 stat->period_min_time= (uint64_t)-1;
305 stat->period_max_time= 0;
306 stat->pre_get_miss= stat->get_miss;
307 } /* ms_dump_format_stats */