Merge lp:~tangent-org/libmemcached/1.0-build/ Build: jenkins-Libmemcached-187
[m6w6/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 "mem_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 double period_average= 0;
223 uint64_t period_tps= 0;
224 double period_rate= 0;
225 double period_std= 0;
226 double period_log= 0;
227
228 if ((events= ms_get_events(stat)) == 0)
229 {
230 return;
231 }
232
233 global_average= (double)(stat->total_time / events);
234 global_tps= events / (uint64_t)run_time;
235 global_rate= (double)events * obj_size / 1024 / 1024 / run_time;
236 global_std= sqrt((stat->squares - (double)events * global_average
237 * global_average) / (double)(events - 1));
238 global_log= exp(stat->log_product / (double)events);
239
240 uint64_t diff_time= stat->total_time - stat->pre_total_time;
241 uint64_t diff_events= events - stat->pre_events;
242 if (diff_events >= 1)
243 {
244 period_average= (double)(diff_time / diff_events);
245 period_tps= diff_events / (uint64_t)freq;
246 period_rate= (double)diff_events * obj_size / 1024 / 1024 / freq;
247 double diff_squares= (double)stat->squares - (double)stat->pre_squares;
248 period_std= sqrt((diff_squares - (double)diff_events * period_average
249 * period_average) / (double)(diff_events - 1));
250 double diff_log_product= stat->log_product - stat->pre_log_product;
251 period_log= exp(diff_log_product / (double)diff_events);
252 }
253
254 printf("%s Statistics\n", stat->name);
255 printf("%-8s %-8s %-12s %-12s %-10s %-10s %-8s %-10s %-10s %-10s %-10s\n",
256 "Type",
257 "Time(s)",
258 "Ops",
259 "TPS(ops/s)",
260 "Net(M/s)",
261 "Get_miss",
262 "Min(us)",
263 "Max(us)",
264 "Avg(us)",
265 "Std_dev",
266 "Geo_dist");
267
268 printf(
269 "%-8s %-8d %-12llu %-12lld %-10.1f %-10lld %-8lld %-10lld %-10lld %-10.2f %.2f\n",
270 "Period",
271 freq,
272 (long long)diff_events,
273 (long long)period_tps,
274 global_rate,
275 (long long)(stat->get_miss - stat->pre_get_miss),
276 (long long)stat->period_min_time,
277 (long long)stat->period_max_time,
278 (long long)period_average,
279 period_std,
280 period_log);
281
282 printf(
283 "%-8s %-8d %-12llu %-12lld %-10.1f %-10lld %-8lld %-10lld %-10lld %-10.2f %.2f\n\n",
284 "Global",
285 run_time,
286 (long long)events,
287 (long long)global_tps,
288 period_rate,
289 (long long)stat->get_miss,
290 (long long)stat->min_time,
291 (long long)stat->max_time,
292 (long long)global_average,
293 global_std,
294 global_log);
295
296 stat->pre_events= events;
297 stat->pre_squares= (uint64_t)stat->squares;
298 stat->pre_total_time= stat->total_time;
299 stat->pre_log_product= stat->log_product;
300 stat->period_min_time= (uint64_t)-1;
301 stat->period_max_time= 0;
302 stat->pre_get_miss= stat->get_miss;
303 } /* ms_dump_format_stats */