]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ProfStats.cc
Merged from trunk.
[thirdparty/squid.git] / src / ProfStats.cc
CommitLineData
88bfe092 1
2/*
507d0a78 3 * $Id: ProfStats.cc,v 1.10 2007/04/25 11:30:18 adrian Exp $
88bfe092 4 *
507d0a78 5 * DEBUG: section 81 CPU Profiling Routines
88bfe092 6 * AUTHOR: Andres Kroonmaa
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * the Regents of the University of California. Please see the
16 * COPYRIGHT file for full details. Squid incorporates software
17 * developed and/or copyrighted by other sources. Please see the
18 * CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36#include "squid.h"
a553a5a3 37#include "event.h"
62ee09ca 38#include "CacheManager.h"
88bfe092 39
40#ifdef USE_XPROF_STATS
e6ccf245 41#include "Store.h"
88bfe092 42
43/* Private stuff */
44
45#define MAX_SORTLIST 200
46
47static hrtime_t xprof_delta = 0;
48static hrtime_t xprof_start_t = 0;
49static hrtime_t xprof_verystart = 0;
50static hrtime_t xprof_average_delta = 0;
51static int xprof_events = 0;
52static int xprof_inited = 0;
53static xprof_stats_data Totals;
54
55static TimersArray *xprof_stats_avg1sec = NULL;
56static TimersArray *xprof_stats_avg5sec = NULL;
57static TimersArray *xprof_stats_avg30sec = NULL;
58static TimersArray *xprof_stats_avg1min = NULL;
59static TimersArray *xprof_stats_avg5min = NULL;
60static TimersArray *xprof_stats_avg30min = NULL;
61static TimersArray *xprof_stats_avg1hour = NULL;
62static TimersArray *xprof_stats_avg5hour = NULL;
63static TimersArray *xprof_stats_avg24hour = NULL;
64
65static xprof_stats_node *sortlist[XPROF_LAST + 2];
43ae1d95 66static void xprof_summary(StoreEntry * sentry);
88bfe092 67
68static void
69xprof_reset(xprof_stats_data * head)
70{
71 head->summ = 0;
72 head->count = 0;
73 head->delta = 0;
74 head->best = XP_NOBEST;
75 head->worst = 0;
76 head->start = 0;
77 head->stop = 0;
78}
79
80static void
81xprof_move(xprof_stats_data * head, xprof_stats_data * hist)
82{
83 memcpy(hist, head, sizeof(xprof_stats_data));
84}
85
86static int
87xprof_comp(xprof_stats_node ** ii, xprof_stats_node ** jj)
88{
89 if ((*ii)->hist.summ < (*jj)->hist.summ)
62e76326 90 return (1);
91
88bfe092 92 if ((*ii)->hist.summ > (*jj)->hist.summ)
62e76326 93 return (-1);
88bfe092 94
95 return (0);
96}
97
98static void
99xprof_sorthist(TimersArray * xprof_list)
100{
101 int i;
102
103 for (i = 0; i < XPROF_LAST; i++) {
62e76326 104 sortlist[i] = xprof_list[i];
88bfe092 105 }
62e76326 106
88bfe092 107 qsort(&sortlist[XPROF_hash_lookup], XPROF_LAST - XPROF_hash_lookup, sizeof(xprof_stats_node *), (QS *) xprof_comp);
108}
109
110static double time_frame;
111
112static void
113xprof_show_item(StoreEntry * sentry, const char *name, xprof_stats_data * hist)
114{
115 storeAppendPrintf(sentry,
e2b35d08 116 "%s\t %" PRIu64 "\t %" PRIu64 "\t %" PRIu64 "\t %" PRIu64 "\t %" PRIu64 "\t %.2f\t %6.3f\t\n",
62e76326 117 name,
118 hist->count,
119 hist->summ,
120 (hist->best != XP_NOBEST ? hist->best : 0),
121 hist->count ? hist->summ / hist->count : 0,
122 hist->worst,
123 hist->count / time_frame,
124 dpercent((double) hist->summ, (double) hist->delta));
88bfe092 125}
126
127static void
43ae1d95 128xprof_summary_item(StoreEntry * sentry, char const *descr, TimersArray * list)
88bfe092 129{
130 int i;
131 xprof_stats_node **hist;
132 xprof_stats_data *show;
133 xprof_reset(&Totals);
134 xprof_sorthist(list);
135 hist = &sortlist[0];
136
137 show = &hist[0]->hist;
62e76326 138
88bfe092 139 if (!hist[0]->hist.delta)
62e76326 140 show = &hist[0]->accu;
88bfe092 141
142 time_frame = (double) show->delta / (double) xprof_average_delta;
143
144 storeAppendPrintf(sentry, "\n%s:", descr);
62e76326 145
e2b35d08 146 storeAppendPrintf(sentry, " (Cumulated time: %" PRIu64 ", %.2f sec)\n",
62e76326 147 show->delta,
148 time_frame
149 );
150
88bfe092 151 storeAppendPrintf(sentry,
62e76326 152 "Probe Name\t Events\t cumulated time \t best case \t average \t worst case\t Rate / sec \t %% in int\n");
88bfe092 153
154 for (i = 0; i < XPROF_LAST; i++) {
62e76326 155 if (!hist[i]->name)
156 continue;
157
158 show = &hist[i]->hist;
159
160 if (!show->count)
161 continue;
162
163 xprof_show_item(sentry, hist[i]->name, show);
164
165 Totals.count += show->count;
166
167 Totals.summ += show->summ;
168
169 Totals.best += (show->best != XP_NOBEST ? show->best : 0);
170
171 Totals.worst += show->worst;
172
173 Totals.delta = (show->delta > Totals.delta ? show->delta : Totals.delta);
88bfe092 174 }
62e76326 175
88bfe092 176 xprof_show_item(sentry, "TOTALS", &Totals);
177}
178
179static void
180xprof_average(TimersArray ** list, int secs)
181{
182 int i;
183 TimersArray *head = xprof_Timers;
184 TimersArray *hist;
185 hrtime_t now;
186 hrtime_t keep;
187 int doavg = (xprof_events % secs);
188
189 if (!*list)
62e76326 190 *list = (TimersArray *)xcalloc(XPROF_LAST, sizeof(xprof_stats_node));
88bfe092 191
192 hist = *list;
62e76326 193
88bfe092 194 now = get_tick();
195
196 for (i = 0; i < XPROF_LAST; i++) {
62e76326 197 hist[i]->name = head[i]->name;
198 hist[i]->accu.summ += head[i]->accu.summ;
199 hist[i]->accu.count += head[i]->accu.count; /* accumulate multisec */
200
201 if (!hist[i]->accu.best)
202 hist[i]->accu.best = head[i]->accu.best;
203
204 if (hist[i]->accu.best > head[i]->accu.best)
205 hist[i]->accu.best = head[i]->accu.best;
206
207 if (hist[i]->accu.worst < head[i]->accu.worst)
208 hist[i]->accu.worst = head[i]->accu.worst;
209
210 hist[i]->accu.delta += xprof_delta;
211
212 if (!doavg) {
213 /* we have X seconds accumulated */
214 xprof_move(&hist[i]->accu, &hist[i]->hist);
215 xprof_reset(&hist[i]->accu);
216
217 hist[i]->accu.start = now;
218 }
219
220 /* reset 0sec counters */
221 if (secs == 1) {
222 keep = head[i]->accu.start;
223 xprof_move(&head[i]->accu, &head[i]->hist);
224 xprof_reset(&head[i]->accu);
225 hist[i]->accu.delta = 0;
226 head[i]->accu.start = keep;
227 }
88bfe092 228 }
229}
230
231void
232xprof_summary(StoreEntry * sentry)
233{
234 hrtime_t now = get_tick();
235
236 storeAppendPrintf(sentry, "CPU Profiling Statistics:\n");
237 storeAppendPrintf(sentry,
62e76326 238 " (CPU times are in arbitrary units, most probably in CPU clock ticks)\n");
88bfe092 239 storeAppendPrintf(sentry,
62e76326 240 "Probe Name\t Event Count\t last Interval \t Avg Interval \t since squid start \t (since system boot) \n");
e2b35d08 241 storeAppendPrintf(sentry, "Total\t %lu\t %" PRIu64 " \t %" PRIu64 " \t %" PRIu64 " \t %" PRIu64 "\n",
62e76326 242 (long unsigned) xprof_events,
243 xprof_delta,
244 xprof_average_delta,
245 now - xprof_verystart,
246 now);
88bfe092 247
248 xprof_summary_item(sentry, "Last 1 sec averages", xprof_stats_avg1sec);
249 xprof_summary_item(sentry, "Last 5 sec averages", xprof_stats_avg5sec);
250 xprof_summary_item(sentry, "Last 30 sec averages", xprof_stats_avg30sec);
251 xprof_summary_item(sentry, "Last 1 min averages", xprof_stats_avg1min);
252 xprof_summary_item(sentry, "Last 5 min averages", xprof_stats_avg5min);
253 xprof_summary_item(sentry, "Last 30 min averages", xprof_stats_avg30min);
254 xprof_summary_item(sentry, "Last 1 hour averages", xprof_stats_avg1hour);
255 xprof_summary_item(sentry, "Last 5 hour averages", xprof_stats_avg5hour);
256 xprof_summary_item(sentry, "Last 24 hour averages", xprof_stats_avg24hour);
257}
258
259static inline void
260xprof_chk_overhead(int samples)
261{
262 while (samples--) {
62e76326 263 PROF_start(PROF_OVERHEAD);
264 PROF_stop(PROF_OVERHEAD);
88bfe092 265 }
266}
267
6b7d87bb
FC
268static void
269xprofRegisterWithCacheManager(void)
270{
271 CacheManager::GetInstance()->
272 registerAction("cpu_profile", "CPU Profiling Stats", xprof_summary, 0, 1);
273}
274
6852be71
FC
275// FIXME:
276// this gets colled once per event. This doesn't seem to make much sense,
277// does it?
88bfe092 278static hrtime_t now;
279static void
280xprof_Init(void)
281{
282 if (xprof_inited)
62e76326 283 return;
88bfe092 284
285 xprof_delta = xprof_verystart = xprof_start_t = now;
286
287 xprof_inited = 1;
6852be71
FC
288
289 xprofRegisterWithCacheManager(); //moved here so it's not double-init'ed
62ee09ca 290}
62e76326 291
88bfe092 292void
293xprof_event(void *data)
294{
295 now = get_tick();
296 xprof_Init();
297 xprof_delta = now - xprof_start_t;
298 xprof_start_t = now;
299 xprof_events++;
300
301 if (!xprof_average_delta)
62e76326 302 xprof_average_delta = xprof_delta;
303
88bfe092 304 if (xprof_average_delta > (xprof_delta >> 1))
62e76326 305 xprof_average_delta = xprof_average_delta - (xprof_average_delta >> 8) + (xprof_delta >> 8);
88bfe092 306
88bfe092 307 xprof_chk_overhead(2);
62e76326 308
88bfe092 309 xprof_average(&xprof_stats_avg24hour, 24 * 3600);
62e76326 310
88bfe092 311 xprof_average(&xprof_stats_avg5hour, 5 * 3600);
62e76326 312
88bfe092 313 xprof_average(&xprof_stats_avg1hour, 3600);
62e76326 314
88bfe092 315 xprof_average(&xprof_stats_avg30min, 1800);
62e76326 316
88bfe092 317 xprof_average(&xprof_stats_avg5min, 300);
62e76326 318
88bfe092 319 xprof_average(&xprof_stats_avg1min, 60);
62e76326 320
88bfe092 321 xprof_average(&xprof_stats_avg30sec, 30);
62e76326 322
88bfe092 323 xprof_average(&xprof_stats_avg5sec, 5);
62e76326 324
88bfe092 325 xprof_average(&xprof_stats_avg1sec, 1);
62e76326 326
88bfe092 327 xprof_chk_overhead(30);
62e76326 328
88bfe092 329 eventAdd("cpuProfiling", xprof_event, NULL, 1.0, 1);
330}
331
332#endif /* USE_XPROF_STATS */