]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ProfStats.cc
First step of MinGw configure support.
[thirdparty/squid.git] / src / ProfStats.cc
CommitLineData
88bfe092 1
2/*
43ae1d95 3 * $Id: ProfStats.cc,v 1.5 2003/03/10 04:56:36 robertc Exp $
88bfe092 4 *
5 * DEBUG: section 81 CPU Profiling Routines
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"
37
38#ifdef USE_XPROF_STATS
e6ccf245 39#include "Store.h"
88bfe092 40
41/* Private stuff */
42
43#define MAX_SORTLIST 200
44
45static hrtime_t xprof_delta = 0;
46static hrtime_t xprof_start_t = 0;
47static hrtime_t xprof_verystart = 0;
48static hrtime_t xprof_average_delta = 0;
49static int xprof_events = 0;
50static int xprof_inited = 0;
51static xprof_stats_data Totals;
52
53static TimersArray *xprof_stats_avg1sec = NULL;
54static TimersArray *xprof_stats_avg5sec = NULL;
55static TimersArray *xprof_stats_avg30sec = NULL;
56static TimersArray *xprof_stats_avg1min = NULL;
57static TimersArray *xprof_stats_avg5min = NULL;
58static TimersArray *xprof_stats_avg30min = NULL;
59static TimersArray *xprof_stats_avg1hour = NULL;
60static TimersArray *xprof_stats_avg5hour = NULL;
61static TimersArray *xprof_stats_avg24hour = NULL;
62
63static xprof_stats_node *sortlist[XPROF_LAST + 2];
43ae1d95 64static void xprof_summary(StoreEntry * sentry);
88bfe092 65
66static void
67xprof_reset(xprof_stats_data * head)
68{
69 head->summ = 0;
70 head->count = 0;
71 head->delta = 0;
72 head->best = XP_NOBEST;
73 head->worst = 0;
74 head->start = 0;
75 head->stop = 0;
76}
77
78static void
79xprof_move(xprof_stats_data * head, xprof_stats_data * hist)
80{
81 memcpy(hist, head, sizeof(xprof_stats_data));
82}
83
84static int
85xprof_comp(xprof_stats_node ** ii, xprof_stats_node ** jj)
86{
87 if ((*ii)->hist.summ < (*jj)->hist.summ)
62e76326 88 return (1);
89
88bfe092 90 if ((*ii)->hist.summ > (*jj)->hist.summ)
62e76326 91 return (-1);
88bfe092 92
93 return (0);
94}
95
96static void
97xprof_sorthist(TimersArray * xprof_list)
98{
99 int i;
100
101 for (i = 0; i < XPROF_LAST; i++) {
62e76326 102 sortlist[i] = xprof_list[i];
88bfe092 103 }
62e76326 104
88bfe092 105 qsort(&sortlist[XPROF_hash_lookup], XPROF_LAST - XPROF_hash_lookup, sizeof(xprof_stats_node *), (QS *) xprof_comp);
106}
107
108static double time_frame;
109
110static void
111xprof_show_item(StoreEntry * sentry, const char *name, xprof_stats_data * hist)
112{
113 storeAppendPrintf(sentry,
62e76326 114 "%s\t %llu\t %llu\t %llu\t %llu\t %llu\t %.2f\t %6.3f\t\n",
115 name,
116 hist->count,
117 hist->summ,
118 (hist->best != XP_NOBEST ? hist->best : 0),
119 hist->count ? hist->summ / hist->count : 0,
120 hist->worst,
121 hist->count / time_frame,
122 dpercent((double) hist->summ, (double) hist->delta));
88bfe092 123}
124
125static void
43ae1d95 126xprof_summary_item(StoreEntry * sentry, char const *descr, TimersArray * list)
88bfe092 127{
128 int i;
129 xprof_stats_node **hist;
130 xprof_stats_data *show;
131 xprof_reset(&Totals);
132 xprof_sorthist(list);
133 hist = &sortlist[0];
134
135 show = &hist[0]->hist;
62e76326 136
88bfe092 137 if (!hist[0]->hist.delta)
62e76326 138 show = &hist[0]->accu;
88bfe092 139
140 time_frame = (double) show->delta / (double) xprof_average_delta;
141
142 storeAppendPrintf(sentry, "\n%s:", descr);
62e76326 143
88bfe092 144 storeAppendPrintf(sentry, " (Cumulated time: %llu, %.2f sec)\n",
62e76326 145 show->delta,
146 time_frame
147 );
148
88bfe092 149 storeAppendPrintf(sentry,
62e76326 150 "Probe Name\t Events\t cumulated time \t best case \t average \t worst case\t Rate / sec \t %% in int\n");
88bfe092 151
152 for (i = 0; i < XPROF_LAST; i++) {
62e76326 153 if (!hist[i]->name)
154 continue;
155
156 show = &hist[i]->hist;
157
158 if (!show->count)
159 continue;
160
161 xprof_show_item(sentry, hist[i]->name, show);
162
163 Totals.count += show->count;
164
165 Totals.summ += show->summ;
166
167 Totals.best += (show->best != XP_NOBEST ? show->best : 0);
168
169 Totals.worst += show->worst;
170
171 Totals.delta = (show->delta > Totals.delta ? show->delta : Totals.delta);
88bfe092 172 }
62e76326 173
88bfe092 174 xprof_show_item(sentry, "TOTALS", &Totals);
175}
176
177static void
178xprof_average(TimersArray ** list, int secs)
179{
180 int i;
181 TimersArray *head = xprof_Timers;
182 TimersArray *hist;
183 hrtime_t now;
184 hrtime_t keep;
185 int doavg = (xprof_events % secs);
186
187 if (!*list)
62e76326 188 *list = (TimersArray *)xcalloc(XPROF_LAST, sizeof(xprof_stats_node));
88bfe092 189
190 hist = *list;
62e76326 191
88bfe092 192 now = get_tick();
193
194 for (i = 0; i < XPROF_LAST; i++) {
62e76326 195 hist[i]->name = head[i]->name;
196 hist[i]->accu.summ += head[i]->accu.summ;
197 hist[i]->accu.count += head[i]->accu.count; /* accumulate multisec */
198
199 if (!hist[i]->accu.best)
200 hist[i]->accu.best = head[i]->accu.best;
201
202 if (hist[i]->accu.best > head[i]->accu.best)
203 hist[i]->accu.best = head[i]->accu.best;
204
205 if (hist[i]->accu.worst < head[i]->accu.worst)
206 hist[i]->accu.worst = head[i]->accu.worst;
207
208 hist[i]->accu.delta += xprof_delta;
209
210 if (!doavg) {
211 /* we have X seconds accumulated */
212 xprof_move(&hist[i]->accu, &hist[i]->hist);
213 xprof_reset(&hist[i]->accu);
214
215 hist[i]->accu.start = now;
216 }
217
218 /* reset 0sec counters */
219 if (secs == 1) {
220 keep = head[i]->accu.start;
221 xprof_move(&head[i]->accu, &head[i]->hist);
222 xprof_reset(&head[i]->accu);
223 hist[i]->accu.delta = 0;
224 head[i]->accu.start = keep;
225 }
88bfe092 226 }
227}
228
229void
230xprof_summary(StoreEntry * sentry)
231{
232 hrtime_t now = get_tick();
233
234 storeAppendPrintf(sentry, "CPU Profiling Statistics:\n");
235 storeAppendPrintf(sentry,
62e76326 236 " (CPU times are in arbitrary units, most probably in CPU clock ticks)\n");
88bfe092 237 storeAppendPrintf(sentry,
62e76326 238 "Probe Name\t Event Count\t last Interval \t Avg Interval \t since squid start \t (since system boot) \n");
88bfe092 239 storeAppendPrintf(sentry, "Total\t %lu\t %llu \t %llu \t %llu \t %llu\n",
62e76326 240 (long unsigned) xprof_events,
241 xprof_delta,
242 xprof_average_delta,
243 now - xprof_verystart,
244 now);
88bfe092 245
246 xprof_summary_item(sentry, "Last 1 sec averages", xprof_stats_avg1sec);
247 xprof_summary_item(sentry, "Last 5 sec averages", xprof_stats_avg5sec);
248 xprof_summary_item(sentry, "Last 30 sec averages", xprof_stats_avg30sec);
249 xprof_summary_item(sentry, "Last 1 min averages", xprof_stats_avg1min);
250 xprof_summary_item(sentry, "Last 5 min averages", xprof_stats_avg5min);
251 xprof_summary_item(sentry, "Last 30 min averages", xprof_stats_avg30min);
252 xprof_summary_item(sentry, "Last 1 hour averages", xprof_stats_avg1hour);
253 xprof_summary_item(sentry, "Last 5 hour averages", xprof_stats_avg5hour);
254 xprof_summary_item(sentry, "Last 24 hour averages", xprof_stats_avg24hour);
255}
256
257static inline void
258xprof_chk_overhead(int samples)
259{
260 while (samples--) {
62e76326 261 PROF_start(PROF_OVERHEAD);
262 PROF_stop(PROF_OVERHEAD);
88bfe092 263 }
264}
265
266static hrtime_t now;
267static void
268xprof_Init(void)
269{
270 if (xprof_inited)
62e76326 271 return;
88bfe092 272
273 xprof_delta = xprof_verystart = xprof_start_t = now;
274
275 xprof_inited = 1;
62e76326 276
88bfe092 277 cachemgrRegister("cpu_profile", "CPU Profiling Stats", xprof_summary, 0, 1);
278}
279
280void
281xprof_event(void *data)
282{
283 now = get_tick();
284 xprof_Init();
285 xprof_delta = now - xprof_start_t;
286 xprof_start_t = now;
287 xprof_events++;
288
289 if (!xprof_average_delta)
62e76326 290 xprof_average_delta = xprof_delta;
291
88bfe092 292 if (xprof_average_delta > (xprof_delta >> 1))
62e76326 293 xprof_average_delta = xprof_average_delta - (xprof_average_delta >> 8) + (xprof_delta >> 8);
88bfe092 294
295 xprof_nesting++;
62e76326 296
88bfe092 297 xprof_chk_overhead(2);
62e76326 298
88bfe092 299 xprof_average(&xprof_stats_avg24hour, 24 * 3600);
62e76326 300
88bfe092 301 xprof_average(&xprof_stats_avg5hour, 5 * 3600);
62e76326 302
88bfe092 303 xprof_average(&xprof_stats_avg1hour, 3600);
62e76326 304
88bfe092 305 xprof_average(&xprof_stats_avg30min, 1800);
62e76326 306
88bfe092 307 xprof_average(&xprof_stats_avg5min, 300);
62e76326 308
88bfe092 309 xprof_average(&xprof_stats_avg1min, 60);
62e76326 310
88bfe092 311 xprof_average(&xprof_stats_avg30sec, 30);
62e76326 312
88bfe092 313 xprof_average(&xprof_stats_avg5sec, 5);
62e76326 314
88bfe092 315 xprof_average(&xprof_stats_avg1sec, 1);
62e76326 316
88bfe092 317 xprof_chk_overhead(30);
62e76326 318
88bfe092 319 xprof_nesting--;
320
321 eventAdd("cpuProfiling", xprof_event, NULL, 1.0, 1);
322}
323
324#endif /* USE_XPROF_STATS */