]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ProfStats.cc
Bug 3610: peername_regex ACL
[thirdparty/squid.git] / src / ProfStats.cc
CommitLineData
88bfe092 1
2/*
262a0e14 3 * $Id$
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.
26ac0430 24 *
88bfe092 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.
26ac0430 29 *
88bfe092 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
f7f3304a 36#include "squid-old.h"
88bfe092 37
32d002cb 38#if USE_XPROF_STATS
a98bcbee 39
a98bcbee 40#include "event.h"
8822ebee 41#include "mgr/Registration.h"
a98bcbee 42#include "SquidMath.h"
e6ccf245 43#include "Store.h"
88bfe092 44
45/* Private stuff */
46
47#define MAX_SORTLIST 200
48
49static hrtime_t xprof_delta = 0;
50static hrtime_t xprof_start_t = 0;
51static hrtime_t xprof_verystart = 0;
52static hrtime_t xprof_average_delta = 0;
53static int xprof_events = 0;
54static int xprof_inited = 0;
55static xprof_stats_data Totals;
56
57static TimersArray *xprof_stats_avg1sec = NULL;
58static TimersArray *xprof_stats_avg5sec = NULL;
59static TimersArray *xprof_stats_avg30sec = NULL;
60static TimersArray *xprof_stats_avg1min = NULL;
61static TimersArray *xprof_stats_avg5min = NULL;
62static TimersArray *xprof_stats_avg30min = NULL;
63static TimersArray *xprof_stats_avg1hour = NULL;
64static TimersArray *xprof_stats_avg5hour = NULL;
65static TimersArray *xprof_stats_avg24hour = NULL;
66
67static xprof_stats_node *sortlist[XPROF_LAST + 2];
43ae1d95 68static void xprof_summary(StoreEntry * sentry);
88bfe092 69
70static void
71xprof_reset(xprof_stats_data * head)
72{
73 head->summ = 0;
74 head->count = 0;
75 head->delta = 0;
76 head->best = XP_NOBEST;
77 head->worst = 0;
78 head->start = 0;
79 head->stop = 0;
80}
81
82static void
83xprof_move(xprof_stats_data * head, xprof_stats_data * hist)
84{
85 memcpy(hist, head, sizeof(xprof_stats_data));
86}
87
88static int
89xprof_comp(xprof_stats_node ** ii, xprof_stats_node ** jj)
90{
91 if ((*ii)->hist.summ < (*jj)->hist.summ)
62e76326 92 return (1);
93
88bfe092 94 if ((*ii)->hist.summ > (*jj)->hist.summ)
62e76326 95 return (-1);
88bfe092 96
97 return (0);
98}
99
100static void
101xprof_sorthist(TimersArray * xprof_list)
102{
5db6bf73 103 for (int i = 0; i < XPROF_LAST; ++i) {
62e76326 104 sortlist[i] = xprof_list[i];
88bfe092 105 }
62e76326 106
1e6e2c8a 107 qsort(&sortlist[XPROF_PROF_UNACCOUNTED+1], XPROF_LAST - XPROF_PROF_UNACCOUNTED+1, sizeof(xprof_stats_node *), (QS *) xprof_comp);
88bfe092 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,
a98bcbee 124 Math::doublePercent((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
5db6bf73 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
5db6bf73 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{
8822ebee 271 Mgr::RegisterAction("cpu_profile", "CPU Profiling Stats", xprof_summary, 0, 1);
6b7d87bb
FC
272}
273
26ac0430 274// FIXME:
6852be71
FC
275// this gets colled once per event. This doesn't seem to make much sense,
276// does it?
88bfe092 277static hrtime_t now;
278static void
279xprof_Init(void)
280{
281 if (xprof_inited)
62e76326 282 return;
88bfe092 283
284 xprof_delta = xprof_verystart = xprof_start_t = now;
285
286 xprof_inited = 1;
6852be71
FC
287
288 xprofRegisterWithCacheManager(); //moved here so it's not double-init'ed
62ee09ca 289}
62e76326 290
88bfe092 291void
292xprof_event(void *data)
293{
294 now = get_tick();
295 xprof_Init();
296 xprof_delta = now - xprof_start_t;
297 xprof_start_t = now;
5db6bf73 298 ++xprof_events;
88bfe092 299
300 if (!xprof_average_delta)
62e76326 301 xprof_average_delta = xprof_delta;
302
88bfe092 303 if (xprof_average_delta > (xprof_delta >> 1))
62e76326 304 xprof_average_delta = xprof_average_delta - (xprof_average_delta >> 8) + (xprof_delta >> 8);
88bfe092 305
88bfe092 306 xprof_chk_overhead(2);
62e76326 307
88bfe092 308 xprof_average(&xprof_stats_avg24hour, 24 * 3600);
62e76326 309
88bfe092 310 xprof_average(&xprof_stats_avg5hour, 5 * 3600);
62e76326 311
88bfe092 312 xprof_average(&xprof_stats_avg1hour, 3600);
62e76326 313
88bfe092 314 xprof_average(&xprof_stats_avg30min, 1800);
62e76326 315
88bfe092 316 xprof_average(&xprof_stats_avg5min, 300);
62e76326 317
88bfe092 318 xprof_average(&xprof_stats_avg1min, 60);
62e76326 319
88bfe092 320 xprof_average(&xprof_stats_avg30sec, 30);
62e76326 321
88bfe092 322 xprof_average(&xprof_stats_avg5sec, 5);
62e76326 323
88bfe092 324 xprof_average(&xprof_stats_avg1sec, 1);
62e76326 325
88bfe092 326 xprof_chk_overhead(30);
62e76326 327
88bfe092 328 eventAdd("cpuProfiling", xprof_event, NULL, 1.0, 1);
329}
330
331#endif /* USE_XPROF_STATS */