]> git.ipfire.org Git - thirdparty/squid.git/blob - src/stat.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / stat.cc
1 /*
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 18 Cache Manager Statistics */
10
11 #include "squid.h"
12 #include "CacheDigest.h"
13 #include "CachePeer.h"
14 #include "client_side.h"
15 #include "client_side_request.h"
16 #include "comm/Connection.h"
17 #include "comm/Loops.h"
18 #include "event.h"
19 #include "fde.h"
20 #include "format/Token.h"
21 #include "globals.h"
22 #include "HttpRequest.h"
23 #include "IoStats.h"
24 #include "mem/Pool.h"
25 #include "mem_node.h"
26 #include "MemBuf.h"
27 #include "MemObject.h"
28 #include "mgr/CountersAction.h"
29 #include "mgr/FunAction.h"
30 #include "mgr/InfoAction.h"
31 #include "mgr/IntervalAction.h"
32 #include "mgr/IoAction.h"
33 #include "mgr/Registration.h"
34 #include "mgr/ServiceTimesAction.h"
35 #include "neighbors.h"
36 #include "PeerDigest.h"
37 #include "SquidConfig.h"
38 #include "SquidMath.h"
39 #include "SquidTime.h"
40 #include "stat.h"
41 #include "StatCounters.h"
42 #include "Store.h"
43 #include "store_digest.h"
44 #include "StoreClient.h"
45 #include "tools.h"
46 // for tvSubDsec() which should be in SquidTime.h
47 #include "util.h"
48 #if USE_AUTH
49 #include "auth/UserRequest.h"
50 #endif
51 #if USE_DELAY_POOLS
52 #include "DelayId.h"
53 #endif
54 #if USE_OPENSSL
55 #include "ssl/support.h"
56 #endif
57
58 /* these are included because they expose stats calls */
59 /* TODO: provide a self registration mechanism for those classes
60 * to use during static construction
61 */
62 #include "comm.h"
63 #include "StoreSearch.h"
64
65 #define DEBUG_OPENFD 1
66
67 typedef int STOBJFLT(const StoreEntry *);
68
69 class StatObjectsState
70 {
71 CBDATA_CLASS(StatObjectsState);
72
73 public:
74 StoreEntry *sentry;
75 STOBJFLT *filter;
76 StoreSearchPointer theSearch;
77 };
78
79 /* LOCALS */
80 static const char *describeStatuses(const StoreEntry *);
81 static const char *describeTimestamps(const StoreEntry *);
82 static void statAvgTick(void *notused);
83 static void statAvgDump(StoreEntry *, int minutes, int hours);
84 #if STAT_GRAPHS
85 static void statGraphDump(StoreEntry *);
86 #endif
87 static void statCountersInit(StatCounters *);
88 static void statCountersInitSpecial(StatCounters *);
89 static void statCountersClean(StatCounters *);
90 static void statCountersCopy(StatCounters * dest, const StatCounters * orig);
91 static double statPctileSvc(double, int, int);
92 static void statStoreEntry(MemBuf * mb, StoreEntry * e);
93 static double statCPUUsage(int minutes);
94 static OBJH stat_objects_get;
95 static OBJH stat_vmobjects_get;
96 #if DEBUG_OPENFD
97 static OBJH statOpenfdObj;
98 #endif
99 static EVH statObjects;
100 static OBJH statCountersDump;
101 static OBJH statPeerSelect;
102 static OBJH statDigestBlob;
103 static OBJH statUtilization;
104 static OBJH statCountersHistograms;
105 static OBJH statClientRequests;
106 void GetAvgStat(Mgr::IntervalActionData& stats, int minutes, int hours);
107 void DumpAvgStat(Mgr::IntervalActionData& stats, StoreEntry* sentry);
108 void GetInfo(Mgr::InfoActionData& stats);
109 void DumpInfo(Mgr::InfoActionData& stats, StoreEntry* sentry);
110 void DumpMallocStatistics(StoreEntry* sentry);
111 void GetCountersStats(Mgr::CountersActionData& stats);
112 void DumpCountersStats(Mgr::CountersActionData& stats, StoreEntry* sentry);
113 void GetServiceTimesStats(Mgr::ServiceTimesActionData& stats);
114 void DumpServiceTimesStats(Mgr::ServiceTimesActionData& stats, StoreEntry* sentry);
115 void GetIoStats(Mgr::IoActionData& stats);
116 void DumpIoStats(Mgr::IoActionData& stats, StoreEntry* sentry);
117
118 #if XMALLOC_STATISTICS
119 static void info_get_mallstat(int, int, int, void *);
120 static double xm_time;
121 static double xm_deltat;
122 #endif
123
124 StatCounters CountHist[N_COUNT_HIST];
125 static int NCountHist = 0;
126 static StatCounters CountHourHist[N_COUNT_HOUR_HIST];
127 static int NCountHourHist = 0;
128 CBDATA_CLASS_INIT(StatObjectsState);
129
130 extern unsigned int mem_pool_alloc_calls;
131 extern unsigned int mem_pool_free_calls;
132
133 static void
134 statUtilization(StoreEntry * e)
135 {
136 storeAppendPrintf(e, "Cache Utilisation:\n");
137 storeAppendPrintf(e, "\n");
138 storeAppendPrintf(e, "Last 5 minutes:\n");
139
140 if (NCountHist >= 5)
141 statAvgDump(e, 5, 0);
142 else
143 storeAppendPrintf(e, "(no values recorded yet)\n");
144
145 storeAppendPrintf(e, "\n");
146
147 storeAppendPrintf(e, "Last 15 minutes:\n");
148
149 if (NCountHist >= 15)
150 statAvgDump(e, 15, 0);
151 else
152 storeAppendPrintf(e, "(no values recorded yet)\n");
153
154 storeAppendPrintf(e, "\n");
155
156 storeAppendPrintf(e, "Last hour:\n");
157
158 if (NCountHist >= 60)
159 statAvgDump(e, 60, 0);
160 else
161 storeAppendPrintf(e, "(no values recorded yet)\n");
162
163 storeAppendPrintf(e, "\n");
164
165 storeAppendPrintf(e, "Last 8 hours:\n");
166
167 if (NCountHourHist >= 8)
168 statAvgDump(e, 0, 8);
169 else
170 storeAppendPrintf(e, "(no values recorded yet)\n");
171
172 storeAppendPrintf(e, "\n");
173
174 storeAppendPrintf(e, "Last day:\n");
175
176 if (NCountHourHist >= 24)
177 statAvgDump(e, 0, 24);
178 else
179 storeAppendPrintf(e, "(no values recorded yet)\n");
180
181 storeAppendPrintf(e, "\n");
182
183 storeAppendPrintf(e, "Last 3 days:\n");
184
185 if (NCountHourHist >= 72)
186 statAvgDump(e, 0, 72);
187 else
188 storeAppendPrintf(e, "(no values recorded yet)\n");
189
190 storeAppendPrintf(e, "\n");
191
192 storeAppendPrintf(e, "Totals since cache startup:\n");
193
194 statCountersDump(e);
195 }
196
197 void
198 GetIoStats(Mgr::IoActionData& stats)
199 {
200 int i;
201
202 stats.http_reads = IOStats.Http.reads;
203
204 for (i = 0; i < IoStats::histSize; ++i) {
205 stats.http_read_hist[i] = IOStats.Http.read_hist[i];
206 }
207
208 stats.ftp_reads = IOStats.Ftp.reads;
209
210 for (i = 0; i < IoStats::histSize; ++i) {
211 stats.ftp_read_hist[i] = IOStats.Ftp.read_hist[i];
212 }
213
214 stats.gopher_reads = IOStats.Gopher.reads;
215
216 for (i = 0; i < IoStats::histSize; ++i) {
217 stats.gopher_read_hist[i] = IOStats.Gopher.read_hist[i];
218 }
219 }
220
221 void
222 DumpIoStats(Mgr::IoActionData& stats, StoreEntry* sentry)
223 {
224 int i;
225
226 storeAppendPrintf(sentry, "HTTP I/O\n");
227 storeAppendPrintf(sentry, "number of reads: %.0f\n", stats.http_reads);
228 storeAppendPrintf(sentry, "Read Histogram:\n");
229
230 for (i = 0; i < IoStats::histSize; ++i) {
231 storeAppendPrintf(sentry, "%5d-%5d: %9.0f %2.0f%%\n",
232 i ? (1 << (i - 1)) + 1 : 1,
233 1 << i,
234 stats.http_read_hist[i],
235 Math::doublePercent(stats.http_read_hist[i], stats.http_reads));
236 }
237
238 storeAppendPrintf(sentry, "\n");
239 storeAppendPrintf(sentry, "FTP I/O\n");
240 storeAppendPrintf(sentry, "number of reads: %.0f\n", stats.ftp_reads);
241 storeAppendPrintf(sentry, "Read Histogram:\n");
242
243 for (i = 0; i < IoStats::histSize; ++i) {
244 storeAppendPrintf(sentry, "%5d-%5d: %9.0f %2.0f%%\n",
245 i ? (1 << (i - 1)) + 1 : 1,
246 1 << i,
247 stats.ftp_read_hist[i],
248 Math::doublePercent(stats.ftp_read_hist[i], stats.ftp_reads));
249 }
250
251 storeAppendPrintf(sentry, "\n");
252 storeAppendPrintf(sentry, "Gopher I/O\n");
253 storeAppendPrintf(sentry, "number of reads: %.0f\n", stats.gopher_reads);
254 storeAppendPrintf(sentry, "Read Histogram:\n");
255
256 for (i = 0; i < IoStats::histSize; ++i) {
257 storeAppendPrintf(sentry, "%5d-%5d: %9.0f %2.0f%%\n",
258 i ? (1 << (i - 1)) + 1 : 1,
259 1 << i,
260 stats.gopher_read_hist[i],
261 Math::doublePercent(stats.gopher_read_hist[i], stats.gopher_reads));
262 }
263
264 storeAppendPrintf(sentry, "\n");
265 }
266
267 static const char *
268 describeStatuses(const StoreEntry * entry)
269 {
270 LOCAL_ARRAY(char, buf, 256);
271 snprintf(buf, 256, "%-13s %-13s %-12s %-12s",
272 storeStatusStr[entry->store_status],
273 memStatusStr[entry->mem_status],
274 swapStatusStr[entry->swap_status],
275 pingStatusStr[entry->ping_status]);
276 return buf;
277 }
278
279 const char *
280 storeEntryFlags(const StoreEntry * entry)
281 {
282 LOCAL_ARRAY(char, buf, 256);
283 int flags = (int) entry->flags;
284 char *t;
285 buf[0] = '\0';
286
287 if (EBIT_TEST(flags, ENTRY_SPECIAL))
288 strcat(buf, "SPECIAL,");
289
290 if (EBIT_TEST(flags, ENTRY_REVALIDATE))
291 strcat(buf, "REVALIDATE,");
292
293 if (EBIT_TEST(flags, DELAY_SENDING))
294 strcat(buf, "DELAY_SENDING,");
295
296 if (EBIT_TEST(flags, RELEASE_REQUEST))
297 strcat(buf, "RELEASE_REQUEST,");
298
299 if (EBIT_TEST(flags, REFRESH_REQUEST))
300 strcat(buf, "REFRESH_REQUEST,");
301
302 if (EBIT_TEST(flags, ENTRY_DISPATCHED))
303 strcat(buf, "DISPATCHED,");
304
305 if (EBIT_TEST(flags, KEY_PRIVATE))
306 strcat(buf, "PRIVATE,");
307
308 if (EBIT_TEST(flags, ENTRY_FWD_HDR_WAIT))
309 strcat(buf, "FWD_HDR_WAIT,");
310
311 if (EBIT_TEST(flags, ENTRY_NEGCACHED))
312 strcat(buf, "NEGCACHED,");
313
314 if (EBIT_TEST(flags, ENTRY_VALIDATED))
315 strcat(buf, "VALIDATED,");
316
317 if (EBIT_TEST(flags, ENTRY_BAD_LENGTH))
318 strcat(buf, "BAD_LENGTH,");
319
320 if (EBIT_TEST(flags, ENTRY_ABORTED))
321 strcat(buf, "ABORTED,");
322
323 if ((t = strrchr(buf, ',')))
324 *t = '\0';
325
326 return buf;
327 }
328
329 static const char *
330 describeTimestamps(const StoreEntry * entry)
331 {
332 LOCAL_ARRAY(char, buf, 256);
333 snprintf(buf, 256, "LV:%-9d LU:%-9d LM:%-9d EX:%-9d",
334 (int) entry->timestamp,
335 (int) entry->lastref,
336 (int) entry->lastmod,
337 (int) entry->expires);
338 return buf;
339 }
340
341 static void
342 statStoreEntry(MemBuf * mb, StoreEntry * e)
343 {
344 MemObject *mem = e->mem_obj;
345 mb->Printf("KEY %s\n", e->getMD5Text());
346 mb->Printf("\t%s\n", describeStatuses(e));
347 mb->Printf("\t%s\n", storeEntryFlags(e));
348 mb->Printf("\t%s\n", describeTimestamps(e));
349 mb->Printf("\t%d locks, %d clients, %d refs\n",
350 (int) e->locks(),
351 storePendingNClients(e),
352 (int) e->refcount);
353 mb->Printf("\tSwap Dir %d, File %#08X\n",
354 e->swap_dirn, e->swap_filen);
355
356 if (mem != NULL)
357 mem->stat (mb);
358
359 mb->Printf("\n");
360 }
361
362 /* process objects list */
363 static void
364 statObjects(void *data)
365 {
366 StatObjectsState *state = static_cast<StatObjectsState *>(data);
367 StoreEntry *e;
368
369 if (state->theSearch->isDone()) {
370 if (UsingSmp())
371 storeAppendPrintf(state->sentry, "} by kid%d\n\n", KidIdentifier);
372 state->sentry->complete();
373 state->sentry->unlock("statObjects+isDone");
374 cbdataFree(state);
375 return;
376 } else if (EBIT_TEST(state->sentry->flags, ENTRY_ABORTED)) {
377 state->sentry->unlock("statObjects+aborted");
378 cbdataFree(state);
379 return;
380 } else if (state->sentry->checkDeferRead(-1)) {
381 state->sentry->flush();
382 eventAdd("statObjects", statObjects, state, 0.1, 1);
383 return;
384 }
385
386 state->sentry->buffer();
387 size_t statCount = 0;
388 MemBuf mb;
389 mb.init();
390
391 while (statCount++ < static_cast<size_t>(Config.Store.objectsPerBucket) && state->
392 theSearch->next()) {
393 e = state->theSearch->currentItem();
394
395 if (state->filter && 0 == state->filter(e))
396 continue;
397
398 statStoreEntry(&mb, e);
399 }
400
401 if (mb.size)
402 state->sentry->append(mb.buf, mb.size);
403 mb.clean();
404
405 eventAdd("statObjects", statObjects, state, 0.0, 1);
406 }
407
408 static void
409 statObjectsStart(StoreEntry * sentry, STOBJFLT * filter)
410 {
411 StatObjectsState *state = new StatObjectsState;
412 state->sentry = sentry;
413 state->filter = filter;
414
415 sentry->lock("statObjects");
416 state->theSearch = Store::Root().search(NULL, NULL);
417
418 eventAdd("statObjects", statObjects, state, 0.0, 1);
419 }
420
421 static void
422 stat_objects_get(StoreEntry * sentry)
423 {
424 statObjectsStart(sentry, NULL);
425 }
426
427 static int
428 statObjectsVmFilter(const StoreEntry * e)
429 {
430 return e->mem_obj ? 1 : 0;
431 }
432
433 static void
434 stat_vmobjects_get(StoreEntry * sentry)
435 {
436 statObjectsStart(sentry, statObjectsVmFilter);
437 }
438
439 #if DEBUG_OPENFD
440 static int
441 statObjectsOpenfdFilter(const StoreEntry * e)
442 {
443 if (e->mem_obj == NULL)
444 return 0;
445
446 if (e->mem_obj->swapout.sio == NULL)
447 return 0;
448
449 return 1;
450 }
451
452 static void
453 statOpenfdObj(StoreEntry * sentry)
454 {
455 statObjectsStart(sentry, statObjectsOpenfdFilter);
456 }
457
458 #endif
459
460 #if XMALLOC_STATISTICS
461 static void
462 info_get_mallstat(int size, int number, int oldnum, void *data)
463 {
464 StoreEntry *sentry = (StoreEntry *)data;
465
466 // format: "%12s %15s %6s %12s\n","Alloc Size","Count","Delta","Alloc/sec"
467 if (number > 0)
468 storeAppendPrintf(sentry, "%12d %15d %6d %.1f\n", size, number, number - oldnum, xdiv((number - oldnum), xm_deltat));
469 }
470
471 #endif
472
473 void
474 GetInfo(Mgr::InfoActionData& stats)
475 {
476
477 struct rusage rusage;
478 double cputime;
479 double runtime;
480 #if HAVE_MSTATS && HAVE_GNUMALLOC_H
481 struct mstats ms;
482 #endif
483
484 runtime = tvSubDsec(squid_start, current_time);
485
486 if (runtime == 0.0)
487 runtime = 1.0;
488
489 stats.squid_start = squid_start;
490
491 stats.current_time = current_time;
492
493 stats.client_http_clients = statCounter.client_http.clients;
494
495 stats.client_http_requests = statCounter.client_http.requests;
496
497 stats.icp_pkts_recv = statCounter.icp.pkts_recv;
498
499 stats.icp_pkts_sent = statCounter.icp.pkts_sent;
500
501 stats.icp_replies_queued = statCounter.icp.replies_queued;
502
503 #if USE_HTCP
504
505 stats.htcp_pkts_recv = statCounter.htcp.pkts_recv;
506
507 stats.htcp_pkts_sent = statCounter.htcp.pkts_sent;
508
509 #endif
510
511 stats.request_failure_ratio = request_failure_ratio;
512
513 stats.avg_client_http_requests = statCounter.client_http.requests / (runtime / 60.0);
514
515 stats.avg_icp_messages = (statCounter.icp.pkts_sent + statCounter.icp.pkts_recv) / (runtime / 60.0);
516
517 stats.select_loops = statCounter.select_loops;
518 stats.avg_loop_time = 1000.0 * runtime / statCounter.select_loops;
519
520 stats.request_hit_ratio5 = statRequestHitRatio(5);
521 stats.request_hit_ratio60 = statRequestHitRatio(60);
522
523 stats.byte_hit_ratio5 = statByteHitRatio(5);
524 stats.byte_hit_ratio60 = statByteHitRatio(60);
525
526 stats.request_hit_mem_ratio5 = statRequestHitMemoryRatio(5);
527 stats.request_hit_mem_ratio60 = statRequestHitMemoryRatio(60);
528
529 stats.request_hit_disk_ratio5 = statRequestHitDiskRatio(5);
530 stats.request_hit_disk_ratio60 = statRequestHitDiskRatio(60);
531
532 Store::Root().getStats(stats.store);
533
534 stats.unlink_requests = statCounter.unlink.requests;
535
536 stats.http_requests5 = statPctileSvc(0.5, 5, PCTILE_HTTP);
537 stats.http_requests60 = statPctileSvc(0.5, 60, PCTILE_HTTP);
538
539 stats.cache_misses5 = statPctileSvc(0.5, 5, PCTILE_MISS);
540 stats.cache_misses60 = statPctileSvc(0.5, 60, PCTILE_MISS);
541
542 stats.cache_hits5 = statPctileSvc(0.5, 5, PCTILE_HIT);
543 stats.cache_hits60 = statPctileSvc(0.5, 60, PCTILE_HIT);
544
545 stats.near_hits5 = statPctileSvc(0.5, 5, PCTILE_NH);
546 stats.near_hits60 = statPctileSvc(0.5, 60, PCTILE_NH);
547
548 stats.not_modified_replies5 = statPctileSvc(0.5, 5, PCTILE_NM);
549 stats.not_modified_replies60 = statPctileSvc(0.5, 60, PCTILE_NM);
550
551 stats.dns_lookups5 = statPctileSvc(0.5, 5, PCTILE_DNS);
552 stats.dns_lookups60 = statPctileSvc(0.5, 60, PCTILE_DNS);
553
554 stats.icp_queries5 = statPctileSvc(0.5, 5, PCTILE_ICP_QUERY);
555 stats.icp_queries60 = statPctileSvc(0.5, 60, PCTILE_ICP_QUERY);
556
557 squid_getrusage(&rusage);
558 cputime = rusage_cputime(&rusage);
559
560 stats.up_time = runtime;
561 stats.cpu_time = cputime;
562 stats.cpu_usage = Math::doublePercent(cputime, runtime);
563 stats.cpu_usage5 = statCPUUsage(5);
564 stats.cpu_usage60 = statCPUUsage(60);
565
566 stats.maxrss = rusage_maxrss(&rusage);
567
568 stats.page_faults = rusage_pagefaults(&rusage);
569
570 #if HAVE_MSTATS && HAVE_GNUMALLOC_H
571
572 ms = mstats();
573
574 stats.ms_bytes_total = ms.bytes_total;
575
576 stats.ms_bytes_free = ms.bytes_free;
577
578 #endif
579
580 stats.total_accounted = statMemoryAccounted();
581
582 {
583 MemPoolGlobalStats mp_stats;
584 memPoolGetGlobalStats(&mp_stats);
585 stats.gb_saved_count = mp_stats.TheMeter->gb_saved.count;
586 stats.gb_freed_count = mp_stats.TheMeter->gb_freed.count;
587 }
588
589 stats.max_fd = Squid_MaxFD;
590 stats.biggest_fd = Biggest_FD;
591 stats.number_fd = Number_FD;
592 stats.opening_fd = Opening_FD;
593 stats.num_fd_free = fdNFree();
594 stats.reserved_fd = RESERVED_FD;
595 }
596
597 void
598 DumpInfo(Mgr::InfoActionData& stats, StoreEntry* sentry)
599 {
600 storeAppendPrintf(sentry, "Squid Object Cache: Version %s\n",
601 version_string);
602
603 storeAppendPrintf(sentry, "Build Info: " SQUID_BUILD_INFO "\n");
604
605 #if _SQUID_WINDOWS_
606 if (WIN32_run_mode == _WIN_SQUID_RUN_MODE_SERVICE) {
607 storeAppendPrintf(sentry,"\nRunning as " SQUIDSBUFPH " Windows System Service on %s\n",
608 SQUIDSBUFPRINT(service_name), WIN32_OS_string);
609 storeAppendPrintf(sentry,"Service command line is: %s\n", WIN32_Service_Command_Line);
610 } else
611 storeAppendPrintf(sentry,"Running on %s\n",WIN32_OS_string);
612 #else
613 storeAppendPrintf(sentry,"Service Name: " SQUIDSBUFPH "\n", SQUIDSBUFPRINT(service_name));
614 #endif
615
616 storeAppendPrintf(sentry, "Start Time:\t%s\n",
617 mkrfc1123(stats.squid_start.tv_sec));
618
619 storeAppendPrintf(sentry, "Current Time:\t%s\n",
620 mkrfc1123(stats.current_time.tv_sec));
621
622 storeAppendPrintf(sentry, "Connection information for %s:\n",APP_SHORTNAME);
623
624 storeAppendPrintf(sentry, "\tNumber of clients accessing cache:\t%.0f\n",
625 stats.client_http_clients);
626
627 storeAppendPrintf(sentry, "\tNumber of HTTP requests received:\t%.0f\n",
628 stats.client_http_requests);
629
630 storeAppendPrintf(sentry, "\tNumber of ICP messages received:\t%.0f\n",
631 stats.icp_pkts_recv);
632
633 storeAppendPrintf(sentry, "\tNumber of ICP messages sent:\t%.0f\n",
634 stats.icp_pkts_sent);
635
636 storeAppendPrintf(sentry, "\tNumber of queued ICP replies:\t%.0f\n",
637 stats.icp_replies_queued);
638
639 #if USE_HTCP
640
641 storeAppendPrintf(sentry, "\tNumber of HTCP messages received:\t%.0f\n",
642 stats.htcp_pkts_recv);
643
644 storeAppendPrintf(sentry, "\tNumber of HTCP messages sent:\t%.0f\n",
645 stats.htcp_pkts_sent);
646
647 #endif
648
649 double fct = stats.count > 1 ? stats.count : 1.0;
650 storeAppendPrintf(sentry, "\tRequest failure ratio:\t%5.2f\n",
651 stats.request_failure_ratio / fct);
652
653 storeAppendPrintf(sentry, "\tAverage HTTP requests per minute since start:\t%.1f\n",
654 stats.avg_client_http_requests);
655
656 storeAppendPrintf(sentry, "\tAverage ICP messages per minute since start:\t%.1f\n",
657 stats.avg_icp_messages);
658
659 storeAppendPrintf(sentry, "\tSelect loop called: %.0f times, %0.3f ms avg\n",
660 stats.select_loops, stats.avg_loop_time / fct);
661
662 storeAppendPrintf(sentry, "Cache information for %s:\n",APP_SHORTNAME);
663
664 storeAppendPrintf(sentry, "\tHits as %% of all requests:\t5min: %3.1f%%, 60min: %3.1f%%\n",
665 stats.request_hit_ratio5 / fct,
666 stats.request_hit_ratio60 / fct);
667
668 storeAppendPrintf(sentry, "\tHits as %% of bytes sent:\t5min: %3.1f%%, 60min: %3.1f%%\n",
669 stats.byte_hit_ratio5 / fct,
670 stats.byte_hit_ratio60 / fct);
671
672 storeAppendPrintf(sentry, "\tMemory hits as %% of hit requests:\t5min: %3.1f%%, 60min: %3.1f%%\n",
673 stats.request_hit_mem_ratio5 / fct,
674 stats.request_hit_mem_ratio60 / fct);
675
676 storeAppendPrintf(sentry, "\tDisk hits as %% of hit requests:\t5min: %3.1f%%, 60min: %3.1f%%\n",
677 stats.request_hit_disk_ratio5 / fct,
678 stats.request_hit_disk_ratio60 / fct);
679
680 storeAppendPrintf(sentry, "\tStorage Swap size:\t%.0f KB\n",
681 stats.store.swap.size / 1024);
682
683 storeAppendPrintf(sentry, "\tStorage Swap capacity:\t%4.1f%% used, %4.1f%% free\n",
684 Math::doublePercent(stats.store.swap.size, stats.store.swap.capacity),
685 Math::doublePercent(stats.store.swap.available(), stats.store.swap.capacity));
686
687 storeAppendPrintf(sentry, "\tStorage Mem size:\t%.0f KB\n",
688 stats.store.mem.size / 1024);
689
690 storeAppendPrintf(sentry, "\tStorage Mem capacity:\t%4.1f%% used, %4.1f%% free\n",
691 Math::doublePercent(stats.store.mem.size, stats.store.mem.capacity),
692 Math::doublePercent(stats.store.mem.available(), stats.store.mem.capacity));
693
694 storeAppendPrintf(sentry, "\tMean Object Size:\t%0.2f KB\n",
695 stats.store.swap.meanObjectSize() / 1024);
696
697 storeAppendPrintf(sentry, "\tRequests given to unlinkd:\t%.0f\n",
698 stats.unlink_requests);
699
700 storeAppendPrintf(sentry, "Median Service Times (seconds) 5 min 60 min:\n");
701
702 fct = stats.count > 1 ? stats.count * 1000.0 : 1000.0;
703 storeAppendPrintf(sentry, "\tHTTP Requests (All): %8.5f %8.5f\n",
704 stats.http_requests5 / fct,
705 stats.http_requests60 / fct);
706
707 storeAppendPrintf(sentry, "\tCache Misses: %8.5f %8.5f\n",
708 stats.cache_misses5 / fct,
709 stats.cache_misses60 / fct);
710
711 storeAppendPrintf(sentry, "\tCache Hits: %8.5f %8.5f\n",
712 stats.cache_hits5 / fct,
713 stats.cache_hits60 / fct);
714
715 storeAppendPrintf(sentry, "\tNear Hits: %8.5f %8.5f\n",
716 stats.near_hits5 / fct,
717 stats.near_hits60 / fct);
718
719 storeAppendPrintf(sentry, "\tNot-Modified Replies: %8.5f %8.5f\n",
720 stats.not_modified_replies5 / fct,
721 stats.not_modified_replies60 / fct);
722
723 storeAppendPrintf(sentry, "\tDNS Lookups: %8.5f %8.5f\n",
724 stats.dns_lookups5 / fct,
725 stats.dns_lookups60 / fct);
726
727 fct = stats.count > 1 ? stats.count * 1000000.0 : 1000000.0;
728 storeAppendPrintf(sentry, "\tICP Queries: %8.5f %8.5f\n",
729 stats.icp_queries5 / fct,
730 stats.icp_queries60 / fct);
731
732 storeAppendPrintf(sentry, "Resource usage for %s:\n", APP_SHORTNAME);
733
734 storeAppendPrintf(sentry, "\tUP Time:\t%.3f seconds\n", stats.up_time);
735
736 storeAppendPrintf(sentry, "\tCPU Time:\t%.3f seconds\n", stats.cpu_time);
737
738 storeAppendPrintf(sentry, "\tCPU Usage:\t%.2f%%\n",
739 stats.cpu_usage);
740
741 storeAppendPrintf(sentry, "\tCPU Usage, 5 minute avg:\t%.2f%%\n",
742 stats.cpu_usage5);
743
744 storeAppendPrintf(sentry, "\tCPU Usage, 60 minute avg:\t%.2f%%\n",
745 stats.cpu_usage60);
746
747 storeAppendPrintf(sentry, "\tMaximum Resident Size: %.0f KB\n",
748 stats.maxrss);
749
750 storeAppendPrintf(sentry, "\tPage faults with physical i/o: %.0f\n",
751 stats.page_faults);
752
753 #if HAVE_MSTATS && HAVE_GNUMALLOC_H
754
755 storeAppendPrintf(sentry, "Memory usage for %s via mstats():\n",APP_SHORTNAME);
756
757 storeAppendPrintf(sentry, "\tTotal space in arena: %6.0f KB\n",
758 stats.ms_bytes_total / 1024);
759
760 storeAppendPrintf(sentry, "\tTotal free: %6.0f KB %.0f%%\n",
761 stats.ms_bytes_free / 1024,
762 Math::doublePercent(stats.ms_bytes_free, stats.ms_bytes_total));
763
764 #endif
765
766 storeAppendPrintf(sentry, "Memory accounted for:\n");
767 storeAppendPrintf(sentry, "\tTotal accounted: %6.0f KB\n",
768 stats.total_accounted / 1024);
769 {
770 MemPoolGlobalStats mp_stats;
771 memPoolGetGlobalStats(&mp_stats);
772 storeAppendPrintf(sentry, "\tmemPoolAlloc calls: %9.0f\n",
773 stats.gb_saved_count);
774 storeAppendPrintf(sentry, "\tmemPoolFree calls: %9.0f\n",
775 stats.gb_freed_count);
776 }
777
778 storeAppendPrintf(sentry, "File descriptor usage for %s:\n", APP_SHORTNAME);
779 storeAppendPrintf(sentry, "\tMaximum number of file descriptors: %4.0f\n",
780 stats.max_fd);
781 storeAppendPrintf(sentry, "\tLargest file desc currently in use: %4.0f\n",
782 stats.biggest_fd);
783 storeAppendPrintf(sentry, "\tNumber of file desc currently in use: %4.0f\n",
784 stats.number_fd);
785 storeAppendPrintf(sentry, "\tFiles queued for open: %4.0f\n",
786 stats.opening_fd);
787 storeAppendPrintf(sentry, "\tAvailable number of file descriptors: %4.0f\n",
788 stats.num_fd_free);
789 storeAppendPrintf(sentry, "\tReserved number of file descriptors: %4.0f\n",
790 stats.reserved_fd);
791 storeAppendPrintf(sentry, "\tStore Disk files open: %4.0f\n",
792 stats.store.swap.open_disk_fd);
793
794 storeAppendPrintf(sentry, "Internal Data Structures:\n");
795 storeAppendPrintf(sentry, "\t%6.0f StoreEntries\n",
796 stats.store.store_entry_count);
797 storeAppendPrintf(sentry, "\t%6.0f StoreEntries with MemObjects\n",
798 stats.store.mem_object_count);
799 storeAppendPrintf(sentry, "\t%6.0f Hot Object Cache Items\n",
800 stats.store.mem.count);
801 storeAppendPrintf(sentry, "\t%6.0f on-disk objects\n",
802 stats.store.swap.count);
803 }
804
805 void
806 DumpMallocStatistics(StoreEntry* sentry)
807 {
808 #if XMALLOC_STATISTICS
809
810 xm_deltat = current_dtime - xm_time;
811 xm_time = current_dtime;
812 storeAppendPrintf(sentry, "\nMemory allocation statistics\n");
813 storeAppendPrintf(sentry, "%12s %15s %6s %12s\n","Alloc Size","Count","Delta","Alloc/sec");
814 malloc_statistics(info_get_mallstat, sentry);
815 #endif
816 }
817
818 void
819 GetServiceTimesStats(Mgr::ServiceTimesActionData& stats)
820 {
821 for (int i = 0; i < Mgr::ServiceTimesActionData::seriesSize; ++i) {
822 double p = (i + 1) * 5 / 100.0;
823 stats.http_requests5[i] = statPctileSvc(p, 5, PCTILE_HTTP);
824 stats.http_requests60[i] = statPctileSvc(p, 60, PCTILE_HTTP);
825
826 stats.cache_misses5[i] = statPctileSvc(p, 5, PCTILE_MISS);
827 stats.cache_misses60[i] = statPctileSvc(p, 60, PCTILE_MISS);
828
829 stats.cache_hits5[i] = statPctileSvc(p, 5, PCTILE_HIT);
830 stats.cache_hits60[i] = statPctileSvc(p, 60, PCTILE_HIT);
831
832 stats.near_hits5[i] = statPctileSvc(p, 5, PCTILE_NH);
833 stats.near_hits60[i] = statPctileSvc(p, 60, PCTILE_NH);
834
835 stats.not_modified_replies5[i] = statPctileSvc(p, 5, PCTILE_NM);
836 stats.not_modified_replies60[i] = statPctileSvc(p, 60, PCTILE_NM);
837
838 stats.dns_lookups5[i] = statPctileSvc(p, 5, PCTILE_DNS);
839 stats.dns_lookups60[i] = statPctileSvc(p, 60, PCTILE_DNS);
840
841 stats.icp_queries5[i] = statPctileSvc(p, 5, PCTILE_ICP_QUERY);
842 stats.icp_queries60[i] = statPctileSvc(p, 60, PCTILE_ICP_QUERY);
843 }
844 }
845
846 void
847 DumpServiceTimesStats(Mgr::ServiceTimesActionData& stats, StoreEntry* sentry)
848 {
849 storeAppendPrintf(sentry, "Service Time Percentiles 5 min 60 min:\n");
850 double fct = stats.count > 1 ? stats.count * 1000.0 : 1000.0;
851 for (int i = 0; i < Mgr::ServiceTimesActionData::seriesSize; ++i) {
852 storeAppendPrintf(sentry, "\tHTTP Requests (All): %2d%% %8.5f %8.5f\n",
853 (i + 1) * 5,
854 stats.http_requests5[i] / fct,
855 stats.http_requests60[i] / fct);
856 }
857 for (int i = 0; i < Mgr::ServiceTimesActionData::seriesSize; ++i) {
858 storeAppendPrintf(sentry, "\tCache Misses: %2d%% %8.5f %8.5f\n",
859 (i + 1) * 5,
860 stats.cache_misses5[i] / fct,
861 stats.cache_misses60[i] / fct);
862 }
863 for (int i = 0; i < Mgr::ServiceTimesActionData::seriesSize; ++i) {
864 storeAppendPrintf(sentry, "\tCache Hits: %2d%% %8.5f %8.5f\n",
865 (i + 1) * 5,
866 stats.cache_hits5[i] / fct,
867 stats.cache_hits60[i] / fct);
868 }
869 for (int i = 0; i < Mgr::ServiceTimesActionData::seriesSize; ++i) {
870 storeAppendPrintf(sentry, "\tNear Hits: %2d%% %8.5f %8.5f\n",
871 (i + 1) * 5,
872 stats.near_hits5[i] / fct,
873 stats.near_hits60[i] / fct);
874 }
875 for (int i = 0; i < Mgr::ServiceTimesActionData::seriesSize; ++i) {
876 storeAppendPrintf(sentry, "\tNot-Modified Replies: %2d%% %8.5f %8.5f\n",
877 (i + 1) * 5,
878 stats.not_modified_replies5[i] / fct,
879 stats.not_modified_replies60[i] / fct);
880 }
881 for (int i = 0; i < Mgr::ServiceTimesActionData::seriesSize; ++i) {
882 storeAppendPrintf(sentry, "\tDNS Lookups: %2d%% %8.5f %8.5f\n",
883 (i + 1) * 5,
884 stats.dns_lookups5[i] / fct,
885 stats.dns_lookups60[i] / fct);
886 }
887 fct = stats.count > 1 ? stats.count * 1000000.0 : 1000000.0;
888 for (int i = 0; i < Mgr::ServiceTimesActionData::seriesSize; ++i) {
889 storeAppendPrintf(sentry, "\tICP Queries: %2d%% %8.5f %8.5f\n",
890 (i + 1) * 5,
891 stats.icp_queries5[i] / fct,
892 stats.icp_queries60[i] / fct);
893 }
894 }
895
896 static void
897 statAvgDump(StoreEntry * sentry, int minutes, int hours)
898 {
899 Mgr::IntervalActionData stats;
900 GetAvgStat(stats, minutes, hours);
901 DumpAvgStat(stats, sentry);
902 }
903
904 #define XAVG(X) (dt ? (double) (f->X - l->X) / dt : 0.0)
905 void
906 GetAvgStat(Mgr::IntervalActionData& stats, int minutes, int hours)
907 {
908 StatCounters *f;
909 StatCounters *l;
910 double dt;
911 double ct;
912 assert(N_COUNT_HIST > 1);
913 assert(minutes > 0 || hours > 0);
914 f = &CountHist[0];
915 l = f;
916
917 if (minutes > 0 && hours == 0) {
918 /* checking minute readings ... */
919
920 if (minutes > N_COUNT_HIST - 1)
921 minutes = N_COUNT_HIST - 1;
922
923 l = &CountHist[minutes];
924 } else if (minutes == 0 && hours > 0) {
925 /* checking hour readings ... */
926
927 if (hours > N_COUNT_HOUR_HIST - 1)
928 hours = N_COUNT_HOUR_HIST - 1;
929
930 l = &CountHourHist[hours];
931 } else {
932 debugs(18, DBG_IMPORTANT, "statAvgDump: Invalid args, minutes=" << minutes << ", hours=" << hours);
933 return;
934 }
935
936 dt = tvSubDsec(l->timestamp, f->timestamp);
937 ct = f->cputime - l->cputime;
938
939 stats.sample_start_time = l->timestamp;
940 stats.sample_end_time = f->timestamp;
941
942 stats.client_http_requests = XAVG(client_http.requests);
943 stats.client_http_hits = XAVG(client_http.hits);
944 stats.client_http_errors = XAVG(client_http.errors);
945 stats.client_http_kbytes_in = XAVG(client_http.kbytes_in.kb);
946 stats.client_http_kbytes_out = XAVG(client_http.kbytes_out.kb);
947
948 stats.client_http_all_median_svc_time = statHistDeltaMedian(l->client_http.allSvcTime,
949 f->client_http.allSvcTime) / 1000.0;
950 stats.client_http_miss_median_svc_time = statHistDeltaMedian(l->client_http.missSvcTime,
951 f->client_http.missSvcTime) / 1000.0;
952 stats.client_http_nm_median_svc_time = statHistDeltaMedian(l->client_http.nearMissSvcTime,
953 f->client_http.nearMissSvcTime) / 1000.0;
954 stats.client_http_nh_median_svc_time = statHistDeltaMedian(l->client_http.nearHitSvcTime,
955 f->client_http.nearHitSvcTime) / 1000.0;
956 stats.client_http_hit_median_svc_time = statHistDeltaMedian(l->client_http.hitSvcTime,
957 f->client_http.hitSvcTime) / 1000.0;
958
959 stats.server_all_requests = XAVG(server.all.requests);
960 stats.server_all_errors = XAVG(server.all.errors);
961 stats.server_all_kbytes_in = XAVG(server.all.kbytes_in.kb);
962 stats.server_all_kbytes_out = XAVG(server.all.kbytes_out.kb);
963
964 stats.server_http_requests = XAVG(server.http.requests);
965 stats.server_http_errors = XAVG(server.http.errors);
966 stats.server_http_kbytes_in = XAVG(server.http.kbytes_in.kb);
967 stats.server_http_kbytes_out = XAVG(server.http.kbytes_out.kb);
968
969 stats.server_ftp_requests = XAVG(server.ftp.requests);
970 stats.server_ftp_errors = XAVG(server.ftp.errors);
971 stats.server_ftp_kbytes_in = XAVG(server.ftp.kbytes_in.kb);
972 stats.server_ftp_kbytes_out = XAVG(server.ftp.kbytes_out.kb);
973
974 stats.server_other_requests = XAVG(server.other.requests);
975 stats.server_other_errors = XAVG(server.other.errors);
976 stats.server_other_kbytes_in = XAVG(server.other.kbytes_in.kb);
977 stats.server_other_kbytes_out = XAVG(server.other.kbytes_out.kb);
978
979 stats.icp_pkts_sent = XAVG(icp.pkts_sent);
980 stats.icp_pkts_recv = XAVG(icp.pkts_recv);
981 stats.icp_queries_sent = XAVG(icp.queries_sent);
982 stats.icp_replies_sent = XAVG(icp.replies_sent);
983 stats.icp_queries_recv = XAVG(icp.queries_recv);
984 stats.icp_replies_recv = XAVG(icp.replies_recv);
985 stats.icp_replies_queued = XAVG(icp.replies_queued);
986 stats.icp_query_timeouts = XAVG(icp.query_timeouts);
987 stats.icp_kbytes_sent = XAVG(icp.kbytes_sent.kb);
988 stats.icp_kbytes_recv = XAVG(icp.kbytes_recv.kb);
989 stats.icp_q_kbytes_sent = XAVG(icp.q_kbytes_sent.kb);
990 stats.icp_r_kbytes_sent = XAVG(icp.r_kbytes_sent.kb);
991 stats.icp_q_kbytes_recv = XAVG(icp.q_kbytes_recv.kb);
992 stats.icp_r_kbytes_recv = XAVG(icp.r_kbytes_recv.kb);
993
994 stats.icp_query_median_svc_time = statHistDeltaMedian(l->icp.querySvcTime,
995 f->icp.querySvcTime) / 1000000.0;
996 stats.icp_reply_median_svc_time = statHistDeltaMedian(l->icp.replySvcTime,
997 f->icp.replySvcTime) / 1000000.0;
998 stats.dns_median_svc_time = statHistDeltaMedian(l->dns.svcTime,
999 f->dns.svcTime) / 1000.0;
1000
1001 stats.unlink_requests = XAVG(unlink.requests);
1002 stats.page_faults = XAVG(page_faults);
1003 stats.select_loops = XAVG(select_loops);
1004 stats.select_fds = XAVG(select_fds);
1005 stats.average_select_fd_period = f->select_fds > l->select_fds ?
1006 (f->select_time - l->select_time) / (f->select_fds - l->select_fds) : 0.0;
1007
1008 stats.median_select_fds = statHistDeltaMedian(l->select_fds_hist, f->select_fds_hist);
1009 stats.swap_outs = XAVG(swap.outs);
1010 stats.swap_ins = XAVG(swap.ins);
1011 stats.swap_files_cleaned = XAVG(swap.files_cleaned);
1012 stats.aborted_requests = XAVG(aborted_requests);
1013
1014 stats.syscalls_disk_opens = XAVG(syscalls.disk.opens);
1015 stats.syscalls_disk_closes = XAVG(syscalls.disk.closes);
1016 stats.syscalls_disk_reads = XAVG(syscalls.disk.reads);
1017 stats.syscalls_disk_writes = XAVG(syscalls.disk.writes);
1018 stats.syscalls_disk_seeks = XAVG(syscalls.disk.seeks);
1019 stats.syscalls_disk_unlinks = XAVG(syscalls.disk.unlinks);
1020 stats.syscalls_sock_accepts = XAVG(syscalls.sock.accepts);
1021 stats.syscalls_sock_sockets = XAVG(syscalls.sock.sockets);
1022 stats.syscalls_sock_connects = XAVG(syscalls.sock.connects);
1023 stats.syscalls_sock_binds = XAVG(syscalls.sock.binds);
1024 stats.syscalls_sock_closes = XAVG(syscalls.sock.closes);
1025 stats.syscalls_sock_reads = XAVG(syscalls.sock.reads);
1026 stats.syscalls_sock_writes = XAVG(syscalls.sock.writes);
1027 stats.syscalls_sock_recvfroms = XAVG(syscalls.sock.recvfroms);
1028 stats.syscalls_sock_sendtos = XAVG(syscalls.sock.sendtos);
1029 stats.syscalls_selects = XAVG(syscalls.selects);
1030
1031 stats.cpu_time = ct;
1032 stats.wall_time = dt;
1033 }
1034
1035 void
1036 DumpAvgStat(Mgr::IntervalActionData& stats, StoreEntry* sentry)
1037 {
1038 storeAppendPrintf(sentry, "sample_start_time = %d.%d (%s)\n",
1039 (int)stats.sample_start_time.tv_sec,
1040 (int)stats.sample_start_time.tv_usec,
1041 mkrfc1123(stats.sample_start_time.tv_sec));
1042 storeAppendPrintf(sentry, "sample_end_time = %d.%d (%s)\n",
1043 (int)stats.sample_end_time.tv_sec,
1044 (int)stats.sample_end_time.tv_usec,
1045 mkrfc1123(stats.sample_end_time.tv_sec));
1046
1047 storeAppendPrintf(sentry, "client_http.requests = %f/sec\n",
1048 stats.client_http_requests);
1049 storeAppendPrintf(sentry, "client_http.hits = %f/sec\n",
1050 stats.client_http_hits);
1051 storeAppendPrintf(sentry, "client_http.errors = %f/sec\n",
1052 stats.client_http_errors);
1053 storeAppendPrintf(sentry, "client_http.kbytes_in = %f/sec\n",
1054 stats.client_http_kbytes_in);
1055 storeAppendPrintf(sentry, "client_http.kbytes_out = %f/sec\n",
1056 stats.client_http_kbytes_out);
1057
1058 double fct = stats.count > 1 ? stats.count : 1.0;
1059 storeAppendPrintf(sentry, "client_http.all_median_svc_time = %f seconds\n",
1060 stats.client_http_all_median_svc_time / fct);
1061 storeAppendPrintf(sentry, "client_http.miss_median_svc_time = %f seconds\n",
1062 stats.client_http_miss_median_svc_time / fct);
1063 storeAppendPrintf(sentry, "client_http.nm_median_svc_time = %f seconds\n",
1064 stats.client_http_nm_median_svc_time / fct);
1065 storeAppendPrintf(sentry, "client_http.nh_median_svc_time = %f seconds\n",
1066 stats.client_http_nh_median_svc_time / fct);
1067 storeAppendPrintf(sentry, "client_http.hit_median_svc_time = %f seconds\n",
1068 stats.client_http_hit_median_svc_time / fct);
1069
1070 storeAppendPrintf(sentry, "server.all.requests = %f/sec\n",
1071 stats.server_all_requests);
1072 storeAppendPrintf(sentry, "server.all.errors = %f/sec\n",
1073 stats.server_all_errors);
1074 storeAppendPrintf(sentry, "server.all.kbytes_in = %f/sec\n",
1075 stats.server_all_kbytes_in);
1076 storeAppendPrintf(sentry, "server.all.kbytes_out = %f/sec\n",
1077 stats.server_all_kbytes_out);
1078
1079 storeAppendPrintf(sentry, "server.http.requests = %f/sec\n",
1080 stats.server_http_requests);
1081 storeAppendPrintf(sentry, "server.http.errors = %f/sec\n",
1082 stats.server_http_errors);
1083 storeAppendPrintf(sentry, "server.http.kbytes_in = %f/sec\n",
1084 stats.server_http_kbytes_in);
1085 storeAppendPrintf(sentry, "server.http.kbytes_out = %f/sec\n",
1086 stats.server_http_kbytes_out);
1087
1088 storeAppendPrintf(sentry, "server.ftp.requests = %f/sec\n",
1089 stats.server_ftp_requests);
1090 storeAppendPrintf(sentry, "server.ftp.errors = %f/sec\n",
1091 stats.server_ftp_errors);
1092 storeAppendPrintf(sentry, "server.ftp.kbytes_in = %f/sec\n",
1093 stats.server_ftp_kbytes_in);
1094 storeAppendPrintf(sentry, "server.ftp.kbytes_out = %f/sec\n",
1095 stats.server_ftp_kbytes_out);
1096
1097 storeAppendPrintf(sentry, "server.other.requests = %f/sec\n",
1098 stats.server_other_requests);
1099 storeAppendPrintf(sentry, "server.other.errors = %f/sec\n",
1100 stats.server_other_errors);
1101 storeAppendPrintf(sentry, "server.other.kbytes_in = %f/sec\n",
1102 stats.server_other_kbytes_in);
1103 storeAppendPrintf(sentry, "server.other.kbytes_out = %f/sec\n",
1104 stats.server_other_kbytes_out);
1105
1106 storeAppendPrintf(sentry, "icp.pkts_sent = %f/sec\n",
1107 stats.icp_pkts_sent);
1108 storeAppendPrintf(sentry, "icp.pkts_recv = %f/sec\n",
1109 stats.icp_pkts_recv);
1110 storeAppendPrintf(sentry, "icp.queries_sent = %f/sec\n",
1111 stats.icp_queries_sent);
1112 storeAppendPrintf(sentry, "icp.replies_sent = %f/sec\n",
1113 stats.icp_replies_sent);
1114 storeAppendPrintf(sentry, "icp.queries_recv = %f/sec\n",
1115 stats.icp_queries_recv);
1116 storeAppendPrintf(sentry, "icp.replies_recv = %f/sec\n",
1117 stats.icp_replies_recv);
1118 storeAppendPrintf(sentry, "icp.replies_queued = %f/sec\n",
1119 stats.icp_replies_queued);
1120 storeAppendPrintf(sentry, "icp.query_timeouts = %f/sec\n",
1121 stats.icp_query_timeouts);
1122 storeAppendPrintf(sentry, "icp.kbytes_sent = %f/sec\n",
1123 stats.icp_kbytes_sent);
1124 storeAppendPrintf(sentry, "icp.kbytes_recv = %f/sec\n",
1125 stats.icp_kbytes_recv);
1126 storeAppendPrintf(sentry, "icp.q_kbytes_sent = %f/sec\n",
1127 stats.icp_q_kbytes_sent);
1128 storeAppendPrintf(sentry, "icp.r_kbytes_sent = %f/sec\n",
1129 stats.icp_r_kbytes_sent);
1130 storeAppendPrintf(sentry, "icp.q_kbytes_recv = %f/sec\n",
1131 stats.icp_q_kbytes_recv);
1132 storeAppendPrintf(sentry, "icp.r_kbytes_recv = %f/sec\n",
1133 stats.icp_r_kbytes_recv);
1134 storeAppendPrintf(sentry, "icp.query_median_svc_time = %f seconds\n",
1135 stats.icp_query_median_svc_time / fct);
1136 storeAppendPrintf(sentry, "icp.reply_median_svc_time = %f seconds\n",
1137 stats.icp_reply_median_svc_time / fct);
1138 storeAppendPrintf(sentry, "dns.median_svc_time = %f seconds\n",
1139 stats.dns_median_svc_time / fct);
1140 storeAppendPrintf(sentry, "unlink.requests = %f/sec\n",
1141 stats.unlink_requests);
1142 storeAppendPrintf(sentry, "page_faults = %f/sec\n",
1143 stats.page_faults);
1144 storeAppendPrintf(sentry, "select_loops = %f/sec\n",
1145 stats.select_loops);
1146 storeAppendPrintf(sentry, "select_fds = %f/sec\n",
1147 stats.select_fds);
1148 storeAppendPrintf(sentry, "average_select_fd_period = %f/fd\n",
1149 stats.average_select_fd_period / fct);
1150 storeAppendPrintf(sentry, "median_select_fds = %f\n",
1151 stats.median_select_fds / fct);
1152 storeAppendPrintf(sentry, "swap.outs = %f/sec\n",
1153 stats.swap_outs);
1154 storeAppendPrintf(sentry, "swap.ins = %f/sec\n",
1155 stats.swap_ins);
1156 storeAppendPrintf(sentry, "swap.files_cleaned = %f/sec\n",
1157 stats.swap_files_cleaned);
1158 storeAppendPrintf(sentry, "aborted_requests = %f/sec\n",
1159 stats.aborted_requests);
1160
1161 #if USE_POLL
1162 storeAppendPrintf(sentry, "syscalls.polls = %f/sec\n", stats.syscalls_selects);
1163 #elif defined(USE_SELECT) || defined(USE_SELECT_WIN32)
1164 storeAppendPrintf(sentry, "syscalls.selects = %f/sec\n", stats.syscalls_selects);
1165 #endif
1166
1167 storeAppendPrintf(sentry, "syscalls.disk.opens = %f/sec\n", stats.syscalls_disk_opens);
1168 storeAppendPrintf(sentry, "syscalls.disk.closes = %f/sec\n", stats.syscalls_disk_closes);
1169 storeAppendPrintf(sentry, "syscalls.disk.reads = %f/sec\n", stats.syscalls_disk_reads);
1170 storeAppendPrintf(sentry, "syscalls.disk.writes = %f/sec\n", stats.syscalls_disk_writes);
1171 storeAppendPrintf(sentry, "syscalls.disk.seeks = %f/sec\n", stats.syscalls_disk_seeks);
1172 storeAppendPrintf(sentry, "syscalls.disk.unlinks = %f/sec\n", stats.syscalls_disk_unlinks);
1173 storeAppendPrintf(sentry, "syscalls.sock.accepts = %f/sec\n", stats.syscalls_sock_accepts);
1174 storeAppendPrintf(sentry, "syscalls.sock.sockets = %f/sec\n", stats.syscalls_sock_sockets);
1175 storeAppendPrintf(sentry, "syscalls.sock.connects = %f/sec\n", stats.syscalls_sock_connects);
1176 storeAppendPrintf(sentry, "syscalls.sock.binds = %f/sec\n", stats.syscalls_sock_binds);
1177 storeAppendPrintf(sentry, "syscalls.sock.closes = %f/sec\n", stats.syscalls_sock_closes);
1178 storeAppendPrintf(sentry, "syscalls.sock.reads = %f/sec\n", stats.syscalls_sock_reads);
1179 storeAppendPrintf(sentry, "syscalls.sock.writes = %f/sec\n", stats.syscalls_sock_writes);
1180 storeAppendPrintf(sentry, "syscalls.sock.recvfroms = %f/sec\n", stats.syscalls_sock_recvfroms);
1181 storeAppendPrintf(sentry, "syscalls.sock.sendtos = %f/sec\n", stats.syscalls_sock_sendtos);
1182
1183 storeAppendPrintf(sentry, "cpu_time = %f seconds\n", stats.cpu_time);
1184 storeAppendPrintf(sentry, "wall_time = %f seconds\n", stats.wall_time);
1185 storeAppendPrintf(sentry, "cpu_usage = %f%%\n", Math::doublePercent(stats.cpu_time, stats.wall_time));
1186 }
1187
1188 static void
1189 statRegisterWithCacheManager(void)
1190 {
1191 Mgr::RegisterAction("info", "General Runtime Information",
1192 &Mgr::InfoAction::Create, 0, 1);
1193 Mgr::RegisterAction("service_times", "Service Times (Percentiles)",
1194 &Mgr::ServiceTimesAction::Create, 0, 1);
1195 Mgr::RegisterAction("filedescriptors", "Process Filedescriptor Allocation",
1196 fde::DumpStats, 0, 1);
1197 Mgr::RegisterAction("objects", "All Cache Objects", stat_objects_get, 0, 0);
1198 Mgr::RegisterAction("vm_objects", "In-Memory and In-Transit Objects",
1199 stat_vmobjects_get, 0, 0);
1200 Mgr::RegisterAction("io", "Server-side network read() size histograms",
1201 &Mgr::IoAction::Create, 0, 1);
1202 Mgr::RegisterAction("counters", "Traffic and Resource Counters",
1203 &Mgr::CountersAction::Create, 0, 1);
1204 Mgr::RegisterAction("peer_select", "Peer Selection Algorithms",
1205 statPeerSelect, 0, 1);
1206 Mgr::RegisterAction("digest_stats", "Cache Digest and ICP blob",
1207 statDigestBlob, 0, 1);
1208 Mgr::RegisterAction("5min", "5 Minute Average of Counters",
1209 &Mgr::IntervalAction::Create5min, 0, 1);
1210 Mgr::RegisterAction("60min", "60 Minute Average of Counters",
1211 &Mgr::IntervalAction::Create60min, 0, 1);
1212 Mgr::RegisterAction("utilization", "Cache Utilization",
1213 statUtilization, 0, 1);
1214 Mgr::RegisterAction("histograms", "Full Histogram Counts",
1215 statCountersHistograms, 0, 1);
1216 Mgr::RegisterAction("active_requests",
1217 "Client-side Active Requests",
1218 statClientRequests, 0, 1);
1219 #if USE_AUTH
1220 Mgr::RegisterAction("username_cache",
1221 "Active Cached Usernames",
1222 Auth::User::UsernameCacheStats, 0, 1);
1223 #endif
1224 #if DEBUG_OPENFD
1225 Mgr::RegisterAction("openfd_objects", "Objects with Swapout files open",
1226 statOpenfdObj, 0, 0);
1227 #endif
1228 #if STAT_GRAPHS
1229 Mgr::RegisterAction("graph_variables", "Display cache metrics graphically",
1230 statGraphDump, 0, 1);
1231 #endif
1232 }
1233
1234 void
1235 statInit(void)
1236 {
1237 int i;
1238 debugs(18, 5, "statInit: Initializing...");
1239
1240 for (i = 0; i < N_COUNT_HIST; ++i)
1241 statCountersInit(&CountHist[i]);
1242
1243 for (i = 0; i < N_COUNT_HOUR_HIST; ++i)
1244 statCountersInit(&CountHourHist[i]);
1245
1246 statCountersInit(&statCounter);
1247
1248 eventAdd("statAvgTick", statAvgTick, NULL, (double) COUNT_INTERVAL, 1);
1249
1250 ClientActiveRequests.head = NULL;
1251
1252 ClientActiveRequests.tail = NULL;
1253
1254 statRegisterWithCacheManager();
1255 }
1256
1257 static void
1258 statAvgTick(void *notused)
1259 {
1260 StatCounters *t = &CountHist[0];
1261 StatCounters *p = &CountHist[1];
1262 StatCounters *c = &statCounter;
1263
1264 struct rusage rusage;
1265 eventAdd("statAvgTick", statAvgTick, NULL, (double) COUNT_INTERVAL, 1);
1266 squid_getrusage(&rusage);
1267 c->page_faults = rusage_pagefaults(&rusage);
1268 c->cputime = rusage_cputime(&rusage);
1269 c->timestamp = current_time;
1270 /* even if NCountHist is small, we already Init()ed the tail */
1271 statCountersClean(CountHist + N_COUNT_HIST - 1);
1272 memmove(p, t, (N_COUNT_HIST - 1) * sizeof(StatCounters));
1273 statCountersCopy(t, c);
1274 ++NCountHist;
1275
1276 if ((NCountHist % COUNT_INTERVAL) == 0) {
1277 /* we have an hours worth of readings. store previous hour */
1278 StatCounters *t2 = &CountHourHist[0];
1279 StatCounters *p2 = &CountHourHist[1];
1280 StatCounters *c2 = &CountHist[N_COUNT_HIST - 1];
1281 statCountersClean(CountHourHist + N_COUNT_HOUR_HIST - 1);
1282 memmove(p2, t2, (N_COUNT_HOUR_HIST - 1) * sizeof(StatCounters));
1283 statCountersCopy(t2, c2);
1284 ++NCountHourHist;
1285 }
1286
1287 if (Config.warnings.high_rptm > 0) {
1288 int i = (int) statPctileSvc(0.5, 20, PCTILE_HTTP);
1289
1290 if (Config.warnings.high_rptm < i)
1291 debugs(18, DBG_CRITICAL, "WARNING: Median response time is " << i << " milliseconds");
1292 }
1293
1294 if (Config.warnings.high_pf) {
1295 int i = (CountHist[0].page_faults - CountHist[1].page_faults);
1296 double dt = tvSubDsec(CountHist[0].timestamp, CountHist[1].timestamp);
1297
1298 if (i > 0 && dt > 0.0) {
1299 i /= (int) dt;
1300
1301 if (Config.warnings.high_pf < i)
1302 debugs(18, DBG_CRITICAL, "WARNING: Page faults occuring at " << i << "/sec");
1303 }
1304 }
1305
1306 if (Config.warnings.high_memory) {
1307 size_t i = 0;
1308 #if HAVE_MSTATS && HAVE_GNUMALLOC_H
1309 struct mstats ms = mstats();
1310 i = ms.bytes_total;
1311 #endif
1312 if (Config.warnings.high_memory < i)
1313 debugs(18, DBG_CRITICAL, "WARNING: Memory usage at " << ((unsigned long int)(i >> 20)) << " MB");
1314 }
1315 }
1316
1317 static void
1318 statCountersInit(StatCounters * C)
1319 {
1320 assert(C);
1321 memset(C, 0, sizeof(*C));
1322 C->timestamp = current_time;
1323 statCountersInitSpecial(C);
1324 }
1325
1326 /* add special cases here as they arrive */
1327 static void
1328 statCountersInitSpecial(StatCounters * C)
1329 {
1330 /*
1331 * HTTP svc_time hist is kept in milli-seconds; max of 3 hours.
1332 */
1333 C->client_http.allSvcTime.logInit(300, 0.0, 3600000.0 * 3.0);
1334 C->client_http.missSvcTime.logInit(300, 0.0, 3600000.0 * 3.0);
1335 C->client_http.nearMissSvcTime.logInit(300, 0.0, 3600000.0 * 3.0);
1336 C->client_http.nearHitSvcTime.logInit(300, 0.0, 3600000.0 * 3.0);
1337 C->client_http.hitSvcTime.logInit(300, 0.0, 3600000.0 * 3.0);
1338 /*
1339 * ICP svc_time hist is kept in micro-seconds; max of 1 minute.
1340 */
1341 C->icp.querySvcTime.logInit(300, 0.0, 1000000.0 * 60.0);
1342 C->icp.replySvcTime.logInit(300, 0.0, 1000000.0 * 60.0);
1343 /*
1344 * DNS svc_time hist is kept in milli-seconds; max of 10 minutes.
1345 */
1346 C->dns.svcTime.logInit(300, 0.0, 60000.0 * 10.0);
1347 /*
1348 * Cache Digest Stuff
1349 */
1350 C->cd.on_xition_count.enumInit(CacheDigestHashFuncCount);
1351 C->comm_udp_incoming.enumInit(INCOMING_UDP_MAX);
1352 C->comm_dns_incoming.enumInit(INCOMING_DNS_MAX);
1353 C->comm_tcp_incoming.enumInit(INCOMING_TCP_MAX);
1354 C->select_fds_hist.enumInit(256); /* was SQUID_MAXFD, but it is way too much. It is OK to crop this statistics */
1355 }
1356
1357 /* add special cases here as they arrive */
1358 static void
1359 statCountersClean(StatCounters * C)
1360 {
1361 assert(C);
1362 C->client_http.allSvcTime.clear();
1363 C->client_http.missSvcTime.clear();
1364 C->client_http.nearMissSvcTime.clear();
1365 C->client_http.nearHitSvcTime.clear();
1366 C->client_http.hitSvcTime.clear();
1367 C->icp.querySvcTime.clear();
1368 C->icp.replySvcTime.clear();
1369 C->dns.svcTime.clear();
1370 C->cd.on_xition_count.clear();
1371 C->comm_udp_incoming.clear();
1372 C->comm_dns_incoming.clear();
1373 C->comm_tcp_incoming.clear();
1374 C->select_fds_hist.clear();
1375 }
1376
1377 /* add special cases here as they arrive */
1378 static void
1379 statCountersCopy(StatCounters * dest, const StatCounters * orig)
1380 {
1381 assert(dest && orig);
1382 /* this should take care of all the fields, but "special" ones */
1383 memcpy(dest, orig, sizeof(*dest));
1384 /* prepare space where to copy special entries */
1385 statCountersInitSpecial(dest);
1386 /* now handle special cases */
1387 /* note: we assert that histogram capacities do not change */
1388 dest->client_http.allSvcTime=orig->client_http.allSvcTime;
1389 dest->client_http.missSvcTime=orig->client_http.missSvcTime;
1390 dest->client_http.nearMissSvcTime=orig->client_http.nearMissSvcTime;
1391 dest->client_http.nearHitSvcTime=orig->client_http.nearHitSvcTime;
1392
1393 dest->client_http.hitSvcTime=orig->client_http.hitSvcTime;
1394 dest->icp.querySvcTime=orig->icp.querySvcTime;
1395 dest->icp.replySvcTime=orig->icp.replySvcTime;
1396 dest->dns.svcTime=orig->dns.svcTime;
1397 dest->cd.on_xition_count=orig->cd.on_xition_count;
1398 dest->comm_udp_incoming=orig->comm_udp_incoming;
1399 dest->comm_dns_incoming=orig->comm_dns_incoming;
1400 dest->comm_tcp_incoming=orig->comm_tcp_incoming;
1401 dest->select_fds_hist=orig->select_fds_hist;
1402 }
1403
1404 static void
1405 statCountersHistograms(StoreEntry * sentry)
1406 {
1407 storeAppendPrintf(sentry, "client_http.allSvcTime histogram:\n");
1408 statCounter.client_http.allSvcTime.dump(sentry, NULL);
1409 storeAppendPrintf(sentry, "client_http.missSvcTime histogram:\n");
1410 statCounter.client_http.missSvcTime.dump(sentry, NULL);
1411 storeAppendPrintf(sentry, "client_http.nearMissSvcTime histogram:\n");
1412 statCounter.client_http.nearMissSvcTime.dump(sentry, NULL);
1413 storeAppendPrintf(sentry, "client_http.nearHitSvcTime histogram:\n");
1414 statCounter.client_http.nearHitSvcTime.dump(sentry, NULL);
1415 storeAppendPrintf(sentry, "client_http.hitSvcTime histogram:\n");
1416 statCounter.client_http.hitSvcTime.dump(sentry, NULL);
1417 storeAppendPrintf(sentry, "icp.querySvcTime histogram:\n");
1418 statCounter.icp.querySvcTime.dump(sentry, NULL);
1419 storeAppendPrintf(sentry, "icp.replySvcTime histogram:\n");
1420 statCounter.icp.replySvcTime.dump(sentry, NULL);
1421 storeAppendPrintf(sentry, "dns.svc_time histogram:\n");
1422 statCounter.dns.svcTime.dump(sentry, NULL);
1423 storeAppendPrintf(sentry, "select_fds_hist histogram:\n");
1424 statCounter.select_fds_hist.dump(sentry, NULL);
1425 }
1426
1427 static void
1428 statCountersDump(StoreEntry * sentry)
1429 {
1430 Mgr::CountersActionData stats;
1431 GetCountersStats(stats);
1432 DumpCountersStats(stats, sentry);
1433 }
1434
1435 void
1436 GetCountersStats(Mgr::CountersActionData& stats)
1437 {
1438 StatCounters *f = &statCounter;
1439
1440 struct rusage rusage;
1441 squid_getrusage(&rusage);
1442 f->page_faults = rusage_pagefaults(&rusage);
1443 f->cputime = rusage_cputime(&rusage);
1444
1445 stats.sample_time = f->timestamp;
1446 stats.client_http_requests = f->client_http.requests;
1447 stats.client_http_hits = f->client_http.hits;
1448 stats.client_http_errors = f->client_http.errors;
1449 stats.client_http_kbytes_in = f->client_http.kbytes_in.kb;
1450 stats.client_http_kbytes_out = f->client_http.kbytes_out.kb;
1451 stats.client_http_hit_kbytes_out = f->client_http.hit_kbytes_out.kb;
1452
1453 stats.server_all_requests = f->server.all.requests;
1454 stats.server_all_errors = f->server.all.errors;
1455 stats.server_all_kbytes_in = f->server.all.kbytes_in.kb;
1456 stats.server_all_kbytes_out = f->server.all.kbytes_out.kb;
1457
1458 stats.server_http_requests = f->server.http.requests;
1459 stats.server_http_errors = f->server.http.errors;
1460 stats.server_http_kbytes_in = f->server.http.kbytes_in.kb;
1461 stats.server_http_kbytes_out = f->server.http.kbytes_out.kb;
1462
1463 stats.server_ftp_requests = f->server.ftp.requests;
1464 stats.server_ftp_errors = f->server.ftp.errors;
1465 stats.server_ftp_kbytes_in = f->server.ftp.kbytes_in.kb;
1466 stats.server_ftp_kbytes_out = f->server.ftp.kbytes_out.kb;
1467
1468 stats.server_other_requests = f->server.other.requests;
1469 stats.server_other_errors = f->server.other.errors;
1470 stats.server_other_kbytes_in = f->server.other.kbytes_in.kb;
1471 stats.server_other_kbytes_out = f->server.other.kbytes_out.kb;
1472
1473 stats.icp_pkts_sent = f->icp.pkts_sent;
1474 stats.icp_pkts_recv = f->icp.pkts_recv;
1475 stats.icp_queries_sent = f->icp.queries_sent;
1476 stats.icp_replies_sent = f->icp.replies_sent;
1477 stats.icp_queries_recv = f->icp.queries_recv;
1478 stats.icp_replies_recv = f->icp.replies_recv;
1479 stats.icp_query_timeouts = f->icp.query_timeouts;
1480 stats.icp_replies_queued = f->icp.replies_queued;
1481 stats.icp_kbytes_sent = f->icp.kbytes_sent.kb;
1482 stats.icp_kbytes_recv = f->icp.kbytes_recv.kb;
1483 stats.icp_q_kbytes_sent = f->icp.q_kbytes_sent.kb;
1484 stats.icp_r_kbytes_sent = f->icp.r_kbytes_sent.kb;
1485 stats.icp_q_kbytes_recv = f->icp.q_kbytes_recv.kb;
1486 stats.icp_r_kbytes_recv = f->icp.r_kbytes_recv.kb;
1487
1488 #if USE_CACHE_DIGESTS
1489
1490 stats.icp_times_used = f->icp.times_used;
1491 stats.cd_times_used = f->cd.times_used;
1492 stats.cd_msgs_sent = f->cd.msgs_sent;
1493 stats.cd_msgs_recv = f->cd.msgs_recv;
1494 stats.cd_memory = f->cd.memory.kb;
1495 stats.cd_local_memory = store_digest ? store_digest->mask_size / 1024 : 0;
1496 stats.cd_kbytes_sent = f->cd.kbytes_sent.kb;
1497 stats.cd_kbytes_recv = f->cd.kbytes_recv.kb;
1498 #endif
1499
1500 stats.unlink_requests = f->unlink.requests;
1501 stats.page_faults = f->page_faults;
1502 stats.select_loops = f->select_loops;
1503 stats.cpu_time = f->cputime;
1504 stats.wall_time = tvSubDsec(f->timestamp, current_time);
1505 stats.swap_outs = f->swap.outs;
1506 stats.swap_ins = f->swap.ins;
1507 stats.swap_files_cleaned = f->swap.files_cleaned;
1508 stats.aborted_requests = f->aborted_requests;
1509 }
1510
1511 void
1512 DumpCountersStats(Mgr::CountersActionData& stats, StoreEntry* sentry)
1513 {
1514 storeAppendPrintf(sentry, "sample_time = %d.%d (%s)\n",
1515 (int) stats.sample_time.tv_sec,
1516 (int) stats.sample_time.tv_usec,
1517 mkrfc1123(stats.sample_time.tv_sec));
1518 storeAppendPrintf(sentry, "client_http.requests = %.0f\n",
1519 stats.client_http_requests);
1520 storeAppendPrintf(sentry, "client_http.hits = %.0f\n",
1521 stats.client_http_hits);
1522 storeAppendPrintf(sentry, "client_http.errors = %.0f\n",
1523 stats.client_http_errors);
1524 storeAppendPrintf(sentry, "client_http.kbytes_in = %.0f\n",
1525 stats.client_http_kbytes_in);
1526 storeAppendPrintf(sentry, "client_http.kbytes_out = %.0f\n",
1527 stats.client_http_kbytes_out);
1528 storeAppendPrintf(sentry, "client_http.hit_kbytes_out = %.0f\n",
1529 stats.client_http_hit_kbytes_out);
1530
1531 storeAppendPrintf(sentry, "server.all.requests = %.0f\n",
1532 stats.server_all_requests);
1533 storeAppendPrintf(sentry, "server.all.errors = %.0f\n",
1534 stats.server_all_errors);
1535 storeAppendPrintf(sentry, "server.all.kbytes_in = %.0f\n",
1536 stats.server_all_kbytes_in);
1537 storeAppendPrintf(sentry, "server.all.kbytes_out = %.0f\n",
1538 stats.server_all_kbytes_out);
1539
1540 storeAppendPrintf(sentry, "server.http.requests = %.0f\n",
1541 stats.server_http_requests);
1542 storeAppendPrintf(sentry, "server.http.errors = %.0f\n",
1543 stats.server_http_errors);
1544 storeAppendPrintf(sentry, "server.http.kbytes_in = %.0f\n",
1545 stats.server_http_kbytes_in);
1546 storeAppendPrintf(sentry, "server.http.kbytes_out = %.0f\n",
1547 stats.server_http_kbytes_out);
1548
1549 storeAppendPrintf(sentry, "server.ftp.requests = %.0f\n",
1550 stats.server_ftp_requests);
1551 storeAppendPrintf(sentry, "server.ftp.errors = %.0f\n",
1552 stats.server_ftp_errors);
1553 storeAppendPrintf(sentry, "server.ftp.kbytes_in = %.0f\n",
1554 stats.server_ftp_kbytes_in);
1555 storeAppendPrintf(sentry, "server.ftp.kbytes_out = %.0f\n",
1556 stats.server_ftp_kbytes_out);
1557
1558 storeAppendPrintf(sentry, "server.other.requests = %.0f\n",
1559 stats.server_other_requests);
1560 storeAppendPrintf(sentry, "server.other.errors = %.0f\n",
1561 stats.server_other_errors);
1562 storeAppendPrintf(sentry, "server.other.kbytes_in = %.0f\n",
1563 stats.server_other_kbytes_in);
1564 storeAppendPrintf(sentry, "server.other.kbytes_out = %.0f\n",
1565 stats.server_other_kbytes_out);
1566
1567 storeAppendPrintf(sentry, "icp.pkts_sent = %.0f\n",
1568 stats.icp_pkts_sent);
1569 storeAppendPrintf(sentry, "icp.pkts_recv = %.0f\n",
1570 stats.icp_pkts_recv);
1571 storeAppendPrintf(sentry, "icp.queries_sent = %.0f\n",
1572 stats.icp_queries_sent);
1573 storeAppendPrintf(sentry, "icp.replies_sent = %.0f\n",
1574 stats.icp_replies_sent);
1575 storeAppendPrintf(sentry, "icp.queries_recv = %.0f\n",
1576 stats.icp_queries_recv);
1577 storeAppendPrintf(sentry, "icp.replies_recv = %.0f\n",
1578 stats.icp_replies_recv);
1579 storeAppendPrintf(sentry, "icp.query_timeouts = %.0f\n",
1580 stats.icp_query_timeouts);
1581 storeAppendPrintf(sentry, "icp.replies_queued = %.0f\n",
1582 stats.icp_replies_queued);
1583 storeAppendPrintf(sentry, "icp.kbytes_sent = %.0f\n",
1584 stats.icp_kbytes_sent);
1585 storeAppendPrintf(sentry, "icp.kbytes_recv = %.0f\n",
1586 stats.icp_kbytes_recv);
1587 storeAppendPrintf(sentry, "icp.q_kbytes_sent = %.0f\n",
1588 stats.icp_q_kbytes_sent);
1589 storeAppendPrintf(sentry, "icp.r_kbytes_sent = %.0f\n",
1590 stats.icp_r_kbytes_sent);
1591 storeAppendPrintf(sentry, "icp.q_kbytes_recv = %.0f\n",
1592 stats.icp_q_kbytes_recv);
1593 storeAppendPrintf(sentry, "icp.r_kbytes_recv = %.0f\n",
1594 stats.icp_r_kbytes_recv);
1595
1596 #if USE_CACHE_DIGESTS
1597
1598 storeAppendPrintf(sentry, "icp.times_used = %.0f\n",
1599 stats.icp_times_used);
1600 storeAppendPrintf(sentry, "cd.times_used = %.0f\n",
1601 stats.cd_times_used);
1602 storeAppendPrintf(sentry, "cd.msgs_sent = %.0f\n",
1603 stats.cd_msgs_sent);
1604 storeAppendPrintf(sentry, "cd.msgs_recv = %.0f\n",
1605 stats.cd_msgs_recv);
1606 storeAppendPrintf(sentry, "cd.memory = %.0f\n",
1607 stats.cd_memory);
1608 storeAppendPrintf(sentry, "cd.local_memory = %.0f\n",
1609 stats.cd_local_memory);
1610 storeAppendPrintf(sentry, "cd.kbytes_sent = %.0f\n",
1611 stats.cd_kbytes_sent);
1612 storeAppendPrintf(sentry, "cd.kbytes_recv = %.0f\n",
1613 stats.cd_kbytes_recv);
1614 #endif
1615
1616 storeAppendPrintf(sentry, "unlink.requests = %.0f\n",
1617 stats.unlink_requests);
1618 storeAppendPrintf(sentry, "page_faults = %.0f\n",
1619 stats.page_faults);
1620 storeAppendPrintf(sentry, "select_loops = %.0f\n",
1621 stats.select_loops);
1622 storeAppendPrintf(sentry, "cpu_time = %f\n",
1623 stats.cpu_time);
1624 storeAppendPrintf(sentry, "wall_time = %f\n",
1625 stats.wall_time);
1626 storeAppendPrintf(sentry, "swap.outs = %.0f\n",
1627 stats.swap_outs);
1628 storeAppendPrintf(sentry, "swap.ins = %.0f\n",
1629 stats.swap_ins);
1630 storeAppendPrintf(sentry, "swap.files_cleaned = %.0f\n",
1631 stats.swap_files_cleaned);
1632 storeAppendPrintf(sentry, "aborted_requests = %.0f\n",
1633 stats.aborted_requests);
1634 }
1635
1636 void
1637 statFreeMemory(void)
1638 {
1639 int i;
1640
1641 for (i = 0; i < N_COUNT_HIST; ++i)
1642 statCountersClean(&CountHist[i]);
1643
1644 for (i = 0; i < N_COUNT_HOUR_HIST; ++i)
1645 statCountersClean(&CountHourHist[i]);
1646 }
1647
1648 static void
1649 statPeerSelect(StoreEntry * sentry)
1650 {
1651 #if USE_CACHE_DIGESTS
1652 StatCounters *f = &statCounter;
1653 CachePeer *peer;
1654 const int tot_used = f->cd.times_used + f->icp.times_used;
1655
1656 /* totals */
1657 cacheDigestGuessStatsReport(&f->cd.guess, sentry, "all peers");
1658 /* per-peer */
1659 storeAppendPrintf(sentry, "\nPer-peer statistics:\n");
1660
1661 for (peer = getFirstPeer(); peer; peer = getNextPeer(peer)) {
1662 if (peer->digest)
1663 peerDigestStatsReport(peer->digest, sentry);
1664 else
1665 storeAppendPrintf(sentry, "\nNo peer digest from %s\n", peer->host);
1666
1667 storeAppendPrintf(sentry, "\n");
1668 }
1669
1670 storeAppendPrintf(sentry, "\nAlgorithm usage:\n");
1671 storeAppendPrintf(sentry, "Cache Digest: %7d (%3d%%)\n",
1672 f->cd.times_used, xpercentInt(f->cd.times_used, tot_used));
1673 storeAppendPrintf(sentry, "Icp: %7d (%3d%%)\n",
1674 f->icp.times_used, xpercentInt(f->icp.times_used, tot_used));
1675 storeAppendPrintf(sentry, "Total: %7d (%3d%%)\n",
1676 tot_used, xpercentInt(tot_used, tot_used));
1677 #else
1678
1679 storeAppendPrintf(sentry, "peer digests are disabled; no stats is available.\n");
1680 #endif
1681 }
1682
1683 static void
1684 statDigestBlob(StoreEntry * sentry)
1685 {
1686 storeAppendPrintf(sentry, "\nCounters:\n");
1687 statCountersDump(sentry);
1688 storeAppendPrintf(sentry, "\n5 Min Averages:\n");
1689 statAvgDump(sentry, 5, 0);
1690 storeAppendPrintf(sentry, "\nHistograms:\n");
1691 statCountersHistograms(sentry);
1692 storeAppendPrintf(sentry, "\nPeer Digests:\n");
1693 statPeerSelect(sentry);
1694 storeAppendPrintf(sentry, "\nLocal Digest:\n");
1695 storeDigestReport(sentry);
1696 }
1697
1698 static double
1699 statPctileSvc(double pctile, int interval, int which)
1700 {
1701 StatCounters *f;
1702 StatCounters *l;
1703 double x;
1704 assert(interval > 0);
1705
1706 if (interval > N_COUNT_HIST - 1)
1707 interval = N_COUNT_HIST - 1;
1708
1709 f = &CountHist[0];
1710
1711 l = &CountHist[interval];
1712
1713 assert(f);
1714
1715 assert(l);
1716
1717 switch (which) {
1718
1719 case PCTILE_HTTP:
1720 x = statHistDeltaPctile(l->client_http.allSvcTime,f->client_http.allSvcTime, pctile);
1721 break;
1722
1723 case PCTILE_HIT:
1724 x = statHistDeltaPctile(l->client_http.hitSvcTime,f->client_http.hitSvcTime, pctile);
1725 break;
1726
1727 case PCTILE_MISS:
1728 x = statHistDeltaPctile(l->client_http.missSvcTime,f->client_http.missSvcTime, pctile);
1729 break;
1730
1731 case PCTILE_NM:
1732 x = statHistDeltaPctile(l->client_http.nearMissSvcTime,f->client_http.nearMissSvcTime, pctile);
1733 break;
1734
1735 case PCTILE_NH:
1736 x = statHistDeltaPctile(l->client_http.nearHitSvcTime,f->client_http.nearHitSvcTime, pctile);
1737 break;
1738
1739 case PCTILE_ICP_QUERY:
1740 x = statHistDeltaPctile(l->icp.querySvcTime,f->icp.querySvcTime, pctile);
1741 break;
1742
1743 case PCTILE_DNS:
1744 x = statHistDeltaPctile(l->dns.svcTime,f->dns.svcTime, pctile);
1745 break;
1746
1747 default:
1748 debugs(49, 5, "statPctileSvc: unknown type.");
1749 x = 0;
1750 }
1751
1752 return x;
1753 }
1754
1755 StatCounters *
1756 snmpStatGet(int minutes)
1757 {
1758 return &CountHist[minutes];
1759 }
1760
1761 int
1762 stat5minClientRequests(void)
1763 {
1764 assert(N_COUNT_HIST > 5);
1765 return statCounter.client_http.requests - CountHist[5].client_http.requests;
1766 }
1767
1768 static double
1769 statCPUUsage(int minutes)
1770 {
1771 assert(minutes < N_COUNT_HIST);
1772 return Math::doublePercent(CountHist[0].cputime - CountHist[minutes].cputime,
1773 tvSubDsec(CountHist[minutes].timestamp, CountHist[0].timestamp));
1774 }
1775
1776 double
1777 statRequestHitRatio(int minutes)
1778 {
1779 assert(minutes < N_COUNT_HIST);
1780 return Math::doublePercent(CountHist[0].client_http.hits -
1781 CountHist[minutes].client_http.hits,
1782 CountHist[0].client_http.requests -
1783 CountHist[minutes].client_http.requests);
1784 }
1785
1786 double
1787 statRequestHitMemoryRatio(int minutes)
1788 {
1789 assert(minutes < N_COUNT_HIST);
1790 return Math::doublePercent(CountHist[0].client_http.mem_hits -
1791 CountHist[minutes].client_http.mem_hits,
1792 CountHist[0].client_http.hits -
1793 CountHist[minutes].client_http.hits);
1794 }
1795
1796 double
1797 statRequestHitDiskRatio(int minutes)
1798 {
1799 assert(minutes < N_COUNT_HIST);
1800 return Math::doublePercent(CountHist[0].client_http.disk_hits -
1801 CountHist[minutes].client_http.disk_hits,
1802 CountHist[0].client_http.hits -
1803 CountHist[minutes].client_http.hits);
1804 }
1805
1806 double
1807 statByteHitRatio(int minutes)
1808 {
1809 size_t s;
1810 size_t c;
1811 #if USE_CACHE_DIGESTS
1812
1813 size_t cd;
1814 #endif
1815 /* size_t might be unsigned */
1816 assert(minutes < N_COUNT_HIST);
1817 c = CountHist[0].client_http.kbytes_out.kb - CountHist[minutes].client_http.kbytes_out.kb;
1818 s = CountHist[0].server.all.kbytes_in.kb - CountHist[minutes].server.all.kbytes_in.kb;
1819 #if USE_CACHE_DIGESTS
1820 /*
1821 * This ugly hack is here to prevent the user from seeing a
1822 * negative byte hit ratio. When we fetch a cache digest from
1823 * a neighbor, it gets treated like a cache miss because the
1824 * object is consumed internally. Thus, we subtract cache
1825 * digest bytes out before calculating the byte hit ratio.
1826 */
1827 cd = CountHist[0].cd.kbytes_recv.kb - CountHist[minutes].cd.kbytes_recv.kb;
1828
1829 if (s < cd)
1830 debugs(18, DBG_IMPORTANT, "STRANGE: srv_kbytes=" << s << ", cd_kbytes=" << cd);
1831
1832 s -= cd;
1833
1834 #endif
1835
1836 if (c > s)
1837 return Math::doublePercent(c - s, c);
1838 else
1839 return (-1.0 * Math::doublePercent(s - c, c));
1840 }
1841
1842 static void
1843 statClientRequests(StoreEntry * s)
1844 {
1845 dlink_node *i;
1846 ClientHttpRequest *http;
1847 StoreEntry *e;
1848 char buf[MAX_IPSTRLEN];
1849
1850 for (i = ClientActiveRequests.head; i; i = i->next) {
1851 const char *p = NULL;
1852 http = static_cast<ClientHttpRequest *>(i->data);
1853 assert(http);
1854 ConnStateData * conn = http->getConn();
1855 storeAppendPrintf(s, "Connection: %p\n", conn);
1856
1857 if (conn != NULL) {
1858 const int fd = conn->clientConnection->fd;
1859 storeAppendPrintf(s, "\tFD %d, read %" PRId64 ", wrote %" PRId64 "\n", fd,
1860 fd_table[fd].bytes_read, fd_table[fd].bytes_written);
1861 storeAppendPrintf(s, "\tFD desc: %s\n", fd_table[fd].desc);
1862 storeAppendPrintf(s, "\tin: buf %p, used %ld, free %ld\n",
1863 conn->in.buf.c_str(), (long int) conn->in.buf.length(), (long int) conn->in.buf.spaceSize());
1864 storeAppendPrintf(s, "\tremote: %s\n",
1865 conn->clientConnection->remote.toUrl(buf,MAX_IPSTRLEN));
1866 storeAppendPrintf(s, "\tlocal: %s\n",
1867 conn->clientConnection->local.toUrl(buf,MAX_IPSTRLEN));
1868 storeAppendPrintf(s, "\tnrequests: %d\n",
1869 conn->nrequests);
1870 }
1871
1872 storeAppendPrintf(s, "uri %s\n", http->uri);
1873 storeAppendPrintf(s, "logType %s\n", LogTags_str[http->logType]);
1874 storeAppendPrintf(s, "out.offset %ld, out.size %lu\n",
1875 (long int) http->out.offset, (unsigned long int) http->out.size);
1876 storeAppendPrintf(s, "req_sz %ld\n", (long int) http->req_sz);
1877 e = http->storeEntry();
1878 storeAppendPrintf(s, "entry %p/%s\n", e, e ? e->getMD5Text() : "N/A");
1879 storeAppendPrintf(s, "start %ld.%06d (%f seconds ago)\n",
1880 (long int) http->al->cache.start_time.tv_sec,
1881 (int) http->al->cache.start_time.tv_usec,
1882 tvSubDsec(http->al->cache.start_time, current_time));
1883 #if USE_AUTH
1884 if (http->request->auth_user_request != NULL)
1885 p = http->request->auth_user_request->username();
1886 else
1887 #endif
1888 if (http->request->extacl_user.size() > 0) {
1889 p = http->request->extacl_user.termedBuf();
1890 }
1891
1892 if (!p && conn != NULL && conn->clientConnection->rfc931[0])
1893 p = conn->clientConnection->rfc931;
1894
1895 #if USE_OPENSSL
1896
1897 if (!p && conn != NULL && Comm::IsConnOpen(conn->clientConnection))
1898 p = sslGetUserEmail(fd_table[conn->clientConnection->fd].ssl);
1899
1900 #endif
1901
1902 if (!p)
1903 p = dash_str;
1904
1905 storeAppendPrintf(s, "username %s\n", p);
1906
1907 #if USE_DELAY_POOLS
1908 storeAppendPrintf(s, "delay_pool %d\n", DelayId::DelayClient(http).pool());
1909 #endif
1910
1911 storeAppendPrintf(s, "\n");
1912 }
1913 }
1914
1915 #if STAT_GRAPHS
1916 /*
1917 * urgh, i don't like these, but they do cut the amount of code down immensely
1918 */
1919
1920 #define GRAPH_PER_MIN(Y) \
1921 for (i=0;i<(N_COUNT_HIST-2);++i) { \
1922 dt = tvSubDsec(CountHist[i+1].timestamp, CountHist[i].timestamp); \
1923 if (dt <= 0.0) \
1924 break; \
1925 storeAppendPrintf(e, "%lu,%0.2f:", \
1926 CountHist[i].timestamp.tv_sec, \
1927 ((CountHist[i].Y - CountHist[i+1].Y) / dt)); \
1928 }
1929
1930 #define GRAPH_PER_HOUR(Y) \
1931 for (i=0;i<(N_COUNT_HOUR_HIST-2);++i) { \
1932 dt = tvSubDsec(CountHourHist[i+1].timestamp, CountHourHist[i].timestamp); \
1933 if (dt <= 0.0) \
1934 break; \
1935 storeAppendPrintf(e, "%lu,%0.2f:", \
1936 CountHourHist[i].timestamp.tv_sec, \
1937 ((CountHourHist[i].Y - CountHourHist[i+1].Y) / dt)); \
1938 }
1939
1940 #define GRAPH_TITLE(X,Y) storeAppendPrintf(e,"%s\t%s\t",X,Y);
1941 #define GRAPH_END storeAppendPrintf(e,"\n");
1942
1943 #define GENGRAPH(X,Y,Z) \
1944 GRAPH_TITLE(Y,Z) \
1945 GRAPH_PER_MIN(X) \
1946 GRAPH_PER_HOUR(X) \
1947 GRAPH_END
1948
1949 static void
1950 statGraphDump(StoreEntry * e)
1951 {
1952 int i;
1953 double dt;
1954
1955 GENGRAPH(client_http.requests, "client_http.requests", "Client HTTP requests/sec");
1956 GENGRAPH(client_http.hits, "client_http.hits", "Client HTTP hits/sec");
1957 GENGRAPH(client_http.errors, "client_http.errors", "Client HTTP errors/sec");
1958 GENGRAPH(client_http.kbytes_in.kb, "client_http.kbytes_in", "Client HTTP kbytes_in/sec");
1959 GENGRAPH(client_http.kbytes_out.kb, "client_http.kbytes_out", "Client HTTP kbytes_out/sec");
1960
1961 /* XXX todo: http median service times */
1962
1963 GENGRAPH(server.all.requests, "server.all.requests", "Server requests/sec");
1964 GENGRAPH(server.all.errors, "server.all.errors", "Server errors/sec");
1965 GENGRAPH(server.all.kbytes_in.kb, "server.all.kbytes_in", "Server total kbytes_in/sec");
1966 GENGRAPH(server.all.kbytes_out.kb, "server.all.kbytes_out", "Server total kbytes_out/sec");
1967
1968 GENGRAPH(server.http.requests, "server.http.requests", "Server HTTP requests/sec");
1969 GENGRAPH(server.http.errors, "server.http.errors", "Server HTTP errors/sec");
1970 GENGRAPH(server.http.kbytes_in.kb, "server.http.kbytes_in", "Server HTTP kbytes_in/sec");
1971 GENGRAPH(server.http.kbytes_out.kb, "server.http.kbytes_out", "Server HTTP kbytes_out/sec");
1972
1973 GENGRAPH(server.ftp.requests, "server.ftp.requests", "Server FTP requests/sec");
1974 GENGRAPH(server.ftp.errors, "server.ftp.errors", "Server FTP errors/sec");
1975 GENGRAPH(server.ftp.kbytes_in.kb, "server.ftp.kbytes_in", "Server FTP kbytes_in/sec");
1976 GENGRAPH(server.ftp.kbytes_out.kb, "server.ftp.kbytes_out", "Server FTP kbytes_out/sec");
1977
1978 GENGRAPH(server.other.requests, "server.other.requests", "Server other requests/sec");
1979 GENGRAPH(server.other.errors, "server.other.errors", "Server other errors/sec");
1980 GENGRAPH(server.other.kbytes_in.kb, "server.other.kbytes_in", "Server other kbytes_in/sec");
1981 GENGRAPH(server.other.kbytes_out.kb, "server.other.kbytes_out", "Server other kbytes_out/sec");
1982
1983 GENGRAPH(icp.pkts_sent, "icp.pkts_sent", "ICP packets sent/sec");
1984 GENGRAPH(icp.pkts_recv, "icp.pkts_recv", "ICP packets received/sec");
1985 GENGRAPH(icp.kbytes_sent.kb, "icp.kbytes_sent", "ICP kbytes_sent/sec");
1986 GENGRAPH(icp.kbytes_recv.kb, "icp.kbytes_recv", "ICP kbytes_received/sec");
1987
1988 /* XXX todo: icp median service times */
1989 /* XXX todo: dns median service times */
1990
1991 GENGRAPH(unlink.requests, "unlink.requests", "Cache File unlink requests/sec");
1992 GENGRAPH(page_faults, "page_faults", "System Page Faults/sec");
1993 GENGRAPH(select_loops, "select_loops", "System Select Loop calls/sec");
1994 GENGRAPH(cputime, "cputime", "CPU utilisation");
1995 }
1996
1997 #endif /* STAT_GRAPHS */
1998
1999 int
2000 statMemoryAccounted(void)
2001 {
2002 return memPoolsTotalAllocated();
2003 }
2004