]> git.ipfire.org Git - thirdparty/squid.git/blame - src/mem.cc
Prep for 3.1.4
[thirdparty/squid.git] / src / mem.cc
CommitLineData
acf5589a 1/*
262a0e14 2 * $Id$
acf5589a 3 *
7021844c 4 * DEBUG: section 13 High Level Memory Pool Management
acf5589a 5 * AUTHOR: Harvest Derived
6 *
2b6662ba 7 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 8 * ----------------------------------------------------------
acf5589a 9 *
2b6662ba 10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
acf5589a 18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
26ac0430 23 *
acf5589a 24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
26ac0430 28 *
acf5589a 29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
cbdec147 31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 32 *
acf5589a 33 */
34
35#include "squid.h"
a553a5a3 36#include "event.h"
62ee09ca 37#include "CacheManager.h"
1c898d4c 38#include "ClientInfo.h"
528b2c61 39#include "Mem.h"
d96ceb8e 40#include "memMeter.h"
e6ccf245 41#include "Store.h"
c21ad0f5 42#include "StoreEntryStream.h"
0eb49b6d 43#include "MemBuf.h"
985c86bc 44#include "SquidTime.h"
acf5589a 45
27e059d4
AJ
46#if HAVE_IOMANIP
47#include <iomanip>
48#endif
49#if HAVE_OSTREAM
50#include <ostream>
51#endif
52
7021844c 53/* module globals */
acf5589a 54
d96ceb8e 55/* local prototypes */
c21ad0f5 56static void memStringStats(std::ostream &);
d96ceb8e 57
58/* module locals */
04eb0689 59static MemAllocator *MemPools[MEM_MAX];
d96ceb8e 60static double xm_time = 0;
61static double xm_deltat = 0;
acf5589a 62
9fe7e747 63/* string pools */
64#define mem_str_pool_count 3
62e76326 65
26ac0430 66static const struct {
ec878047 67 const char *name;
9fe7e747 68 size_t obj_size;
62e76326 69}
70
71StrPoolsAttrs[mem_str_pool_count] = {
72
26ac0430
AJ
73 {
74 "Short Strings", MemAllocator::RoundedSize(36),
75 }, /* to fit rfc1123 and similar */
76 {
77 "Medium Strings", MemAllocator::RoundedSize(128),
78 }, /* to fit most urls */
79 {
80 "Long Strings", MemAllocator::RoundedSize(512)
81 } /* other */
82};
83
84static struct {
b001e822 85 MemAllocator *pool;
62e76326 86}
87
88StrPools[mem_str_pool_count];
9fe7e747 89static MemMeter StrCountMeter;
90static MemMeter StrVolumeMeter;
91
1eb41ae8 92static MemMeter HugeBufCountMeter;
93static MemMeter HugeBufVolumeMeter;
9fe7e747 94
95/* local routines */
96
9fe7e747 97static void
c21ad0f5 98memStringStats(std::ostream &stream)
9fe7e747 99{
9fe7e747 100 int i;
101 int pooled_count = 0;
102 size_t pooled_volume = 0;
103 /* heading */
35268c70 104 stream << "String Pool\t Impact\t\t\n \t (%strings)\t (%volume)\n";
9fe7e747 105 /* table body */
62e76326 106
9fe7e747 107 for (i = 0; i < mem_str_pool_count; i++) {
b001e822 108 const MemAllocator *pool = StrPools[i].pool;
109 const int plevel = pool->getMeter().inuse.level;
c21ad0f5 110 stream << std::setw(20) << std::left << pool->objectType();
111 stream << std::right << "\t " << xpercentInt(plevel, StrCountMeter.level);
112 stream << "\t " << xpercentInt(plevel * pool->objectSize(), StrVolumeMeter.level) << "\n";
62e76326 113 pooled_count += plevel;
b001e822 114 pooled_volume += plevel * pool->objectSize();
9fe7e747 115 }
62e76326 116
9fe7e747 117 /* malloc strings */
c21ad0f5 118 stream << std::setw(20) << std::left << "Other Strings";
119
120 stream << std::right << "\t ";
62e76326 121
c21ad0f5 122 stream << xpercentInt(StrCountMeter.level - pooled_count, StrCountMeter.level) << "\t ";
123
124 stream << xpercentInt(StrVolumeMeter.level - pooled_volume, StrVolumeMeter.level) << "\n\n";
1eb41ae8 125}
126
127static void
c21ad0f5 128memBufStats(std::ostream & stream)
1eb41ae8 129{
c21ad0f5 130 stream << "Large buffers: " <<
131 HugeBufCountMeter.level << " (" <<
132 HugeBufVolumeMeter.level / 1024 << " KB)\n";
9fe7e747 133}
134
528b2c61 135void
136Mem::Stats(StoreEntry * sentry)
acf5589a 137{
c21ad0f5 138 StoreEntryStream stream(sentry);
139 Report(stream);
140 memStringStats(stream);
141 memBufStats(stream);
b4bab919 142#if WITH_VALGRIND
143 if (RUNNING_ON_VALGRIND) {
26ac0430
AJ
144 long int leaked = 0, dubious = 0, reachable = 0, suppressed = 0;
145 stream << "Valgrind Report:\n";
146 stream << "Type\tAmount\n";
147 debugs(13, 1, "Asking valgrind for memleaks");
148 VALGRIND_DO_LEAK_CHECK;
149 debugs(13, 1, "Getting valgrind statistics");
150 VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed);
151 stream << "Leaked\t" << leaked << "\n";
152 stream << "Dubious\t" << dubious << "\n";
153 stream << "Reachable\t" << reachable << "\n";
154 stream << "Suppressed\t" << suppressed << "\n";
b4bab919 155 }
156#endif
c21ad0f5 157 stream.flush();
acf5589a 158}
159
160/*
9fe7e747 161 * public routines
acf5589a 162 */
163
58a39dc9 164/*
165 * we have a limit on _total_ amount of idle memory so we ignore
166 * max_pages for now
167 */
168void
60da11d3 169memDataInit(mem_type type, const char *name, size_t size, int max_pages_notused, bool zeroOnPush)
58a39dc9 170{
171 assert(name && size);
d96ceb8e 172 assert(MemPools[type] == NULL);
04eb0689 173 MemPools[type] = memPoolCreate(name, size);
60da11d3 174 MemPools[type]->zeroOnPush(zeroOnPush);
58a39dc9 175}
176
9fe7e747 177
7021844c 178/* find appropriate pool and use it (pools always init buffer with 0s) */
acf5589a 179void *
7021844c 180memAllocate(mem_type type)
acf5589a 181{
b001e822 182 return MemPools[type]->alloc();
acf5589a 183}
184
db1cd23c 185/* give memory back to the pool */
acf5589a 186void
db1cd23c 187memFree(void *p, int type)
acf5589a 188{
dc47f531 189 MemPools[type]->freeOne(p);
acf5589a 190}
191
9fe7e747 192/* allocate a variable size buffer using best-fit pool */
193void *
4be8fe59 194memAllocString(size_t net_size, size_t * gross_size)
9fe7e747 195{
196 int i;
b001e822 197 MemAllocator *pool = NULL;
9fe7e747 198 assert(gross_size);
62e76326 199
9fe7e747 200 for (i = 0; i < mem_str_pool_count; i++) {
62e76326 201 if (net_size <= StrPoolsAttrs[i].obj_size) {
202 pool = StrPools[i].pool;
203 break;
204 }
9fe7e747 205 }
62e76326 206
e231a8ce 207 *gross_size = pool ? StrPoolsAttrs[i].obj_size : net_size;
9fe7e747 208 assert(*gross_size >= net_size);
209 memMeterInc(StrCountMeter);
210 memMeterAdd(StrVolumeMeter, *gross_size);
b001e822 211 return pool ? pool->alloc() : xcalloc(1, net_size);
9fe7e747 212}
213
0353e724 214extern size_t memStringCount();
215size_t
216memStringCount()
217{
218 size_t result = 0;
219
220 for (int counter = 0; counter < mem_str_pool_count; ++counter)
221 result += memPoolInUseCount(StrPools[counter].pool);
222
223 return result;
224}
225
4be8fe59 226/* free buffer allocated with memAllocString() */
9fe7e747 227void
4be8fe59 228memFreeString(size_t size, void *buf)
9fe7e747 229{
230 int i;
b001e822 231 MemAllocator *pool = NULL;
9fe7e747 232 assert(size && buf);
62e76326 233
9fe7e747 234 for (i = 0; i < mem_str_pool_count; i++) {
62e76326 235 if (size <= StrPoolsAttrs[i].obj_size) {
236 assert(size == StrPoolsAttrs[i].obj_size);
237 pool = StrPools[i].pool;
238 break;
239 }
9fe7e747 240 }
62e76326 241
9fe7e747 242 memMeterDec(StrCountMeter);
243 memMeterDel(StrVolumeMeter, size);
dc47f531 244 pool ? pool->freeOne(buf) : xfree(buf);
9fe7e747 245}
246
1eb41ae8 247/* Find the best fit MEM_X_BUF type */
248static mem_type
249memFindBufSizeType(size_t net_size, size_t * gross_size)
250{
251 mem_type type;
252 size_t size;
62e76326 253
fa80a8ef 254 if (net_size <= 2 * 1024) {
62e76326 255 type = MEM_2K_BUF;
256 size = 2 * 1024;
fa80a8ef 257 } else if (net_size <= 4 * 1024) {
62e76326 258 type = MEM_4K_BUF;
259 size = 4 * 1024;
fa80a8ef 260 } else if (net_size <= 8 * 1024) {
62e76326 261 type = MEM_8K_BUF;
262 size = 8 * 1024;
fa80a8ef 263 } else if (net_size <= 16 * 1024) {
62e76326 264 type = MEM_16K_BUF;
265 size = 16 * 1024;
fa80a8ef 266 } else if (net_size <= 32 * 1024) {
62e76326 267 type = MEM_32K_BUF;
268 size = 32 * 1024;
fa80a8ef 269 } else if (net_size <= 64 * 1024) {
62e76326 270 type = MEM_64K_BUF;
271 size = 64 * 1024;
1eb41ae8 272 } else {
62e76326 273 type = MEM_NONE;
274 size = net_size;
1eb41ae8 275 }
62e76326 276
1eb41ae8 277 if (gross_size)
62e76326 278 *gross_size = size;
279
1eb41ae8 280 return type;
281}
282
283/* allocate a variable size buffer using best-fit pool */
284void *
285memAllocBuf(size_t net_size, size_t * gross_size)
286{
287 mem_type type = memFindBufSizeType(net_size, gross_size);
62e76326 288
1eb41ae8 289 if (type != MEM_NONE)
62e76326 290 return memAllocate(type);
1eb41ae8 291 else {
62e76326 292 memMeterInc(HugeBufCountMeter);
293 memMeterAdd(HugeBufVolumeMeter, *gross_size);
294 return xcalloc(1, net_size);
1eb41ae8 295 }
296}
297
298/* resize a variable sized buffer using best-fit pool */
299void *
300memReallocBuf(void *oldbuf, size_t net_size, size_t * gross_size)
301{
302 /* XXX This can be optimized on very large buffers to use realloc() */
edce4d98 303 /* TODO: if the existing gross size is >= new gross size, do nothing */
e6ccf245 304 size_t new_gross_size;
1eb41ae8 305 void *newbuf = memAllocBuf(net_size, &new_gross_size);
62e76326 306
1eb41ae8 307 if (oldbuf) {
62e76326 308 size_t data_size = *gross_size;
309
310 if (data_size > net_size)
311 data_size = net_size;
312
313 memcpy(newbuf, oldbuf, data_size);
314
315 memFreeBuf(*gross_size, oldbuf);
1eb41ae8 316 }
62e76326 317
1eb41ae8 318 *gross_size = new_gross_size;
319 return newbuf;
320}
321
322/* free buffer allocated with memAllocBuf() */
323void
324memFreeBuf(size_t size, void *buf)
325{
326 mem_type type = memFindBufSizeType(size, NULL);
62e76326 327
1eb41ae8 328 if (type != MEM_NONE)
62e76326 329 memFree(buf, type);
1eb41ae8 330 else {
62e76326 331 xfree(buf);
332 memMeterDec(HugeBufCountMeter);
333 memMeterDel(HugeBufVolumeMeter, size);
1eb41ae8 334 }
335}
336
d96ceb8e 337static double clean_interval = 15.0; /* time to live of idle chunk before release */
338
339void
528b2c61 340Mem::CleanIdlePools(void *unused)
d96ceb8e 341{
b001e822 342 MemPools::GetInstance().clean(static_cast<time_t>(clean_interval));
528b2c61 343 eventAdd("memPoolCleanIdlePools", CleanIdlePools, NULL, clean_interval, 1);
d96ceb8e 344}
345
d96ceb8e 346void
347memConfigure(void)
348{
b001e822 349 size_t new_pool_limit;
62e76326 350
09c5ae5a 351 /** Set to configured value first */
d96ceb8e 352 if (!Config.onoff.mem_pools)
62e76326 353 new_pool_limit = 0;
d96ceb8e 354 else if (Config.MemPools.limit > 0)
62e76326 355 new_pool_limit = Config.MemPools.limit;
89646bd7
HN
356 else {
357 if (Config.MemPools.limit == 0)
358 debugs(13, 1, "memory_pools_limit 0 has been chagned to memory_pools_limit none. Please update your config");
359 new_pool_limit = -1;
360 }
d96ceb8e 361
af00d03d 362#if 0
09c5ae5a 363 /** \par
af00d03d 364 * DPW 2007-04-12
365 * No debugging here please because this method is called before
366 * the debug log is configured and we'll get the message on
367 * stderr when doing things like 'squid -k reconfigure'
368 */
b001e822 369 if (MemPools::GetInstance().idleLimit() > new_pool_limit)
bf8fe701 370 debugs(13, 1, "Shrinking idle mem pools to "<< std::setprecision(3) << toMB(new_pool_limit) << " MB");
af00d03d 371#endif
62e76326 372
b001e822 373 MemPools::GetInstance().setIdleLimit(new_pool_limit);
d96ceb8e 374}
1eb41ae8 375
528b2c61 376/* XXX make these classes do their own memory management */
377#include "HttpHdrContRange.h"
378
acf5589a 379void
528b2c61 380Mem::Init(void)
acf5589a 381{
9fe7e747 382 int i;
d96ceb8e 383
09c5ae5a 384 /** \par
33135cfb 385 * NOTE: Mem::Init() is called before the config file is parsed
386 * and before the debugging module has been initialized. Any
387 * debug messages here at level 0 or 1 will always be printed
388 * on stderr.
389 */
d96ceb8e 390
09c5ae5a
AJ
391 /** \par
392 * Set all pointers to null. */
7021844c 393 memset(MemPools, '\0', sizeof(MemPools));
09c5ae5a
AJ
394 /**
395 * Then initialize all pools.
396 * \par
397 * Starting with generic 2kB - 64kB buffr pools, then specific object types.
398 * \par
399 * It does not hurt much to have a lot of pools since sizeof(MemPool) is
7021844c 400 * small; someday we will figure out what to do with all the entries here
401 * that are never used or used only once; perhaps we should simply use
402 * malloc() for those? @?@
403 */
60da11d3 404 memDataInit(MEM_2K_BUF, "2K Buffer", 2048, 10, false);
405 memDataInit(MEM_4K_BUF, "4K Buffer", 4096, 10, false);
406 memDataInit(MEM_8K_BUF, "8K Buffer", 8192, 10, false);
407 memDataInit(MEM_16K_BUF, "16K Buffer", 16384, 10, false);
408 memDataInit(MEM_32K_BUF, "32K Buffer", 32768, 10, false);
409 memDataInit(MEM_64K_BUF, "64K Buffer", 65536, 10, false);
acf5589a 410 memDataInit(MEM_ACL_DENY_INFO_LIST, "acl_deny_info_list",
62e76326 411 sizeof(acl_deny_info_list), 0);
acf5589a 412 memDataInit(MEM_ACL_NAME_LIST, "acl_name_list", sizeof(acl_name_list), 0);
c68e9c6b 413#if USE_CACHE_DIGESTS
62e76326 414
26c2ce6f 415 memDataInit(MEM_CACHE_DIGEST, "CacheDigest", sizeof(CacheDigest), 0);
c68e9c6b 416#endif
62e76326 417
58cd5bbd 418 memDataInit(MEM_LINK_LIST, "link_list", sizeof(link_list), 10);
acf5589a 419 memDataInit(MEM_DLINK_NODE, "dlink_node", sizeof(dlink_node), 10);
acf5589a 420 memDataInit(MEM_DREAD_CTRL, "dread_ctrl", sizeof(dread_ctrl), 0);
421 memDataInit(MEM_DWRITE_Q, "dwrite_q", sizeof(dwrite_q), 0);
7faf2bdb 422 memDataInit(MEM_HTTP_HDR_CC, "HttpHdrCc", sizeof(HttpHdrCc), 0);
d76fcfa7 423 memDataInit(MEM_HTTP_HDR_CONTENT_RANGE, "HttpHdrContRange", sizeof(HttpHdrContRange), 0);
acf5589a 424 memDataInit(MEM_NETDBENTRY, "netdbEntry", sizeof(netdbEntry), 0);
425 memDataInit(MEM_NET_DB_NAME, "net_db_name", sizeof(net_db_name), 0);
acf5589a 426 memDataInit(MEM_RELIST, "relist", sizeof(relist), 0);
59c4d35b 427 memDataInit(MEM_CLIENT_INFO, "ClientInfo", sizeof(ClientInfo), 0);
c3031d67 428 memDataInit(MEM_MD5_DIGEST, "MD5 digest", SQUID_MD5_DIGEST_LENGTH, 0);
b001e822 429 MemPools[MEM_MD5_DIGEST]->setChunkSize(512 * 1024);
58cd5bbd 430
09c5ae5a 431 /** Lastly init the string pools. */
58a39dc9 432 for (i = 0; i < mem_str_pool_count; i++) {
04eb0689 433 StrPools[i].pool = memPoolCreate(StrPoolsAttrs[i].name, StrPoolsAttrs[i].obj_size);
60da11d3 434 StrPools[i].pool->zeroOnPush(false);
e231a8ce 435
b001e822 436 if (StrPools[i].pool->objectSize() != StrPoolsAttrs[i].obj_size)
83ba0bf7 437 debugs(13, 1, "Notice: " << StrPoolsAttrs[i].name << " is " << StrPools[i].pool->objectSize() << " bytes instead of requested " << StrPoolsAttrs[i].obj_size << " bytes");
58a39dc9 438 }
ea391f18 439
09c5ae5a
AJ
440 /** \par
441 * finally register with the cache manager */
ea391f18 442 RegisterWithCacheManager();
62ee09ca 443}
62e76326 444
97244680 445void
446Mem::Report()
447{
448 debugs(13, 3, "Memory pools are '" <<
26ac0430
AJ
449 (Config.onoff.mem_pools ? "on" : "off") << "'; limit: " <<
450 std::setprecision(3) << toMB(MemPools::GetInstance().idleLimit()) <<
451 " MB");
97244680 452}
453
62ee09ca 454void
84f50787 455Mem::RegisterWithCacheManager(void)
62ee09ca 456{
84f50787 457 CacheManager::GetInstance()->registerAction("mem", "Memory Utilization",
26ac0430 458 Mem::Stats, 0, 1);
58a39dc9 459}
460
e6ccf245 461mem_type &operator++ (mem_type &aMem)
462{
1f1ae50a 463 int tmp = (int)aMem;
464 aMem = (mem_type)(++tmp);
e6ccf245 465 return aMem;
466}
467
58a39dc9 468/*
469 * Test that all entries are initialized
470 */
471void
472memCheckInit(void)
473{
474 mem_type t;
62e76326 475
e6ccf245 476 for (t = MEM_NONE, ++t; t < MEM_MAX; ++t) {
62e76326 477 if (MEM_DONTFREE == t)
478 continue;
479
480 /*
481 * If you hit this assertion, then you forgot to add a
482 * memDataInit() line for type 't'.
483 */
484 assert(MemPools[t]);
acf5589a 485 }
486}
487
488void
58a39dc9 489memClean(void)
acf5589a 490{
d96ceb8e 491 MemPoolGlobalStats stats;
b001e822 492 MemPools::GetInstance().setIdleLimit(0);
493 MemPools::GetInstance().clean(0);
d96ceb8e 494 memPoolGetGlobalStats(&stats);
62e76326 495
d96ceb8e 496 if (stats.tot_items_inuse)
bf8fe701 497 debugs(13, 2, "memCleanModule: " << stats.tot_items_inuse <<
498 " items in " << stats.tot_chunks_inuse << " chunks and " <<
499 stats.tot_pools_inuse << " pools are left dirty");
acf5589a 500}
501
acf5589a 502int
503memInUse(mem_type type)
504{
b4832aa9 505 return memPoolInUseCount(MemPools[type]);
acf5589a 506}
507
508/* ick */
509
e4ae841b 510void
137ee196 511memFree2K(void *p)
512{
db1cd23c 513 memFree(p, MEM_2K_BUF);
137ee196 514}
515
acf5589a 516void
517memFree4K(void *p)
518{
db1cd23c 519 memFree(p, MEM_4K_BUF);
acf5589a 520}
521
522void
523memFree8K(void *p)
524{
db1cd23c 525 memFree(p, MEM_8K_BUF);
acf5589a 526}
58cd5bbd 527
e4ae841b 528void
58cd5bbd 529memFree16K(void *p)
530{
531 memFree(p, MEM_16K_BUF);
532}
533
e4ae841b 534void
58cd5bbd 535memFree32K(void *p)
536{
537 memFree(p, MEM_32K_BUF);
538}
539
e4ae841b 540void
58cd5bbd 541memFree64K(void *p)
542{
543 memFree(p, MEM_64K_BUF);
544}
1eb41ae8 545
546FREE *
547memFreeBufFunc(size_t size)
548{
fa80a8ef 549 switch (size) {
62e76326 550
fa80a8ef 551 case 2 * 1024:
62e76326 552 return memFree2K;
553
fa80a8ef 554 case 4 * 1024:
62e76326 555 return memFree4K;
556
fa80a8ef 557 case 8 * 1024:
62e76326 558 return memFree8K;
559
fa80a8ef 560 case 16 * 1024:
62e76326 561 return memFree16K;
562
fa80a8ef 563 case 32 * 1024:
62e76326 564 return memFree32K;
565
fa80a8ef 566 case 64 * 1024:
62e76326 567 return memFree64K;
568
1eb41ae8 569 default:
62e76326 570 memMeterDec(HugeBufCountMeter);
571 memMeterDel(HugeBufVolumeMeter, size);
572 return xfree;
1eb41ae8 573 }
574}
d96ceb8e 575
576/* MemPoolMeter */
577
528b2c61 578void
c21ad0f5 579Mem::PoolReport(const MemPoolStats * mp_st, const MemPoolMeter * AllMeter, std::ostream &stream)
d96ceb8e 580{
60eed7c2 581 int excess = 0;
d96ceb8e 582 int needed = 0;
583 MemPoolMeter *pm = mp_st->meter;
eecdacf6 584 const char *delim = "\t ";
d96ceb8e 585
903a6eec
HN
586#if HAVE_IOMANIP
587 stream.setf(std::ios_base::fixed);
588#endif
eecdacf6 589 stream << std::setw(20) << std::left << mp_st->label << delim;
590 stream << std::setw(4) << std::right << mp_st->obj_size << delim;
d96ceb8e 591
592 /* Chunks */
d96ceb8e 593 if (mp_st->chunk_capacity) {
3b32112a
A
594 stream << std::setw(4) << toKB(mp_st->obj_size * mp_st->chunk_capacity) << delim;
595 stream << std::setw(4) << mp_st->chunk_capacity << delim;
333c86f5 596
62e76326 597 needed = mp_st->items_inuse / mp_st->chunk_capacity;
598
599 if (mp_st->items_inuse % mp_st->chunk_capacity)
600 needed++;
601
602 excess = mp_st->chunks_inuse - needed;
62e76326 603
3b32112a
A
604 stream << std::setw(4) << mp_st->chunks_alloc << delim;
605 stream << std::setw(4) << mp_st->chunks_inuse << delim;
606 stream << std::setw(4) << mp_st->chunks_free << delim;
607 stream << std::setw(4) << mp_st->chunks_partial << delim;
608 stream << std::setprecision(3) << xpercent(excess, needed) << delim;
333c86f5 609 } else {
3b32112a
A
610 stream << delim;
611 stream << delim;
612 stream << delim;
613 stream << delim;
614 stream << delim;
615 stream << delim;
616 stream << delim;
333c86f5 617 }
62e76326 618 /*
619 * Fragmentation calculation:
620 * needed = inuse.level / chunk_capacity
621 * excess = used - needed
622 * fragmentation = excess / needed * 100%
623 *
624 * Fragm = (alloced - (inuse / obj_ch) ) / alloced
625 */
c21ad0f5 626 /* allocated */
eecdacf6 627 stream << mp_st->items_alloc << delim;
628 stream << toKB(mp_st->obj_size * pm->alloc.level) << delim;
629 stream << toKB(mp_st->obj_size * pm->alloc.hwater_level) << delim;
630 stream << std::setprecision(2) << ((squid_curtime - pm->alloc.hwater_stamp) / 3600.) << delim;
631 stream << std::setprecision(3) << xpercent(mp_st->obj_size * pm->alloc.level, AllMeter->alloc.level) << delim;
c21ad0f5 632 /* in use */
eecdacf6 633 stream << mp_st->items_inuse << delim;
634 stream << toKB(mp_st->obj_size * pm->inuse.level) << delim;
635 stream << toKB(mp_st->obj_size * pm->inuse.hwater_level) << delim;
636 stream << std::setprecision(2) << ((squid_curtime - pm->inuse.hwater_stamp) / 3600.) << delim;
637 stream << std::setprecision(3) << xpercent(pm->inuse.level, pm->alloc.level) << delim;
c21ad0f5 638 /* idle */
eecdacf6 639 stream << mp_st->items_idle << delim;
640 stream << toKB(mp_st->obj_size * pm->idle.level) << delim;
641 stream << toKB(mp_st->obj_size * pm->idle.hwater_level) << delim;
c21ad0f5 642 /* saved */
eecdacf6 643 stream << (int)pm->gb_saved.count << delim;
903a6eec
HN
644 stream << std::setprecision(3) << xpercent(pm->gb_saved.count, AllMeter->gb_allocated.count) << delim;
645 stream << std::setprecision(3) << xpercent(pm->gb_saved.bytes, AllMeter->gb_allocated.bytes) << delim;
646 stream << std::setprecision(3) << xdiv(pm->gb_allocated.count - pm->gb_oallocated.count, xm_deltat) << "\n";
647 pm->gb_oallocated.count = pm->gb_allocated.count;
d96ceb8e 648}
649
729102f4 650static int
651MemPoolReportSorter(const void *a, const void *b)
652{
653 const MemPoolStats *A = (MemPoolStats *) a;
654 const MemPoolStats *B = (MemPoolStats *) b;
655
656 // use this to sort on %Total Allocated
657 //
658 double pa = (double) A->obj_size * A->meter->alloc.level;
659 double pb = (double) B->obj_size * B->meter->alloc.level;
660
661 if (pa > pb)
662 return -1;
663
664 if (pb > pa)
665 return 1;
666
667#if 0
668 // use this to sort on In Use high(hrs)
669 //
670 if (A->meter->inuse.hwater_stamp > B->meter->inuse.hwater_stamp)
671 return -1;
672
673 if (B->meter->inuse.hwater_stamp > A->meter->inuse.hwater_stamp)
674 return 1;
675
676#endif
677
678 return 0;
679}
680
d96ceb8e 681void
c21ad0f5 682Mem::Report(std::ostream &stream)
d96ceb8e 683{
684 static char buf[64];
685 static MemPoolStats mp_stats;
686 static MemPoolGlobalStats mp_total;
687 int not_used = 0;
688 MemPoolIterator *iter;
b001e822 689 MemAllocator *pool;
d96ceb8e 690
691 /* caption */
c21ad0f5 692 stream << "Current memory usage:\n";
d96ceb8e 693 /* heading */
c21ad0f5 694 stream << "Pool\t Obj Size\t"
695 "Chunks\t\t\t\t\t\t\t"
696 "Allocated\t\t\t\t\t"
697 "In Use\t\t\t\t\t"
698 "Idle\t\t\t"
699 "Allocations Saved\t\t\t"
903a6eec 700 "Rate\t"
c21ad0f5 701 "\n"
702 " \t (bytes)\t"
703 "KB/ch\t obj/ch\t"
35268c70 704 "(#)\t used\t free\t part\t %Frag\t "
705 "(#)\t (KB)\t high (KB)\t high (hrs)\t %Tot\t"
706 "(#)\t (KB)\t high (KB)\t high (hrs)\t %alloc\t"
c21ad0f5 707 "(#)\t (KB)\t high (KB)\t"
35268c70 708 "(#)\t %cnt\t %vol\t"
903a6eec 709 "(#)/sec\t"
c21ad0f5 710 "\n";
d96ceb8e 711 xm_deltat = current_dtime - xm_time;
712 xm_time = current_dtime;
713
714 /* Get stats for Totals report line */
715 memPoolGetGlobalStats(&mp_total);
716
729102f4 717 MemPoolStats *sortme = (MemPoolStats *) xcalloc(mp_total.tot_pools_alloc ,sizeof(*sortme));
718 int npools = 0;
719
d96ceb8e 720 /* main table */
721 iter = memPoolIterate();
62e76326 722
d96ceb8e 723 while ((pool = memPoolIterateNext(iter))) {
b001e822 724 pool->getStats(&mp_stats);
62e76326 725
726 if (!mp_stats.pool) /* pool destroyed */
727 continue;
728
903a6eec 729 if (mp_stats.pool->getMeter().gb_allocated.count > 0) /* this pool has been used */
729102f4 730 sortme[npools++] = mp_stats;
62e76326 731 else
732 not_used++;
d96ceb8e 733 }
62e76326 734
d96ceb8e 735 memPoolIterateDone(&iter);
736
729102f4 737 qsort(sortme, npools, sizeof(*sortme), MemPoolReportSorter);
738
739 for (int i = 0; i< npools; i++) {
c21ad0f5 740 PoolReport(&sortme[i], mp_total.TheMeter, stream);
729102f4 741 }
742
743 xfree(sortme);
744
d96ceb8e 745 mp_stats.pool = NULL;
746 mp_stats.label = "Total";
747 mp_stats.meter = mp_total.TheMeter;
748 mp_stats.obj_size = 1;
749 mp_stats.chunk_capacity = 0;
750 mp_stats.chunk_size = 0;
751 mp_stats.chunks_alloc = mp_total.tot_chunks_alloc;
752 mp_stats.chunks_inuse = mp_total.tot_chunks_inuse;
753 mp_stats.chunks_partial = mp_total.tot_chunks_partial;
754 mp_stats.chunks_free = mp_total.tot_chunks_free;
755 mp_stats.items_alloc = mp_total.tot_items_alloc;
756 mp_stats.items_inuse = mp_total.tot_items_inuse;
757 mp_stats.items_idle = mp_total.tot_items_idle;
758 mp_stats.overhead = mp_total.tot_overhead;
759
c21ad0f5 760 PoolReport(&mp_stats, mp_total.TheMeter, stream);
d96ceb8e 761
762 /* Cumulative */
903a6eec 763 stream << "Cumulative allocated volume: "<< double_to_str(buf, 64, mp_total.TheMeter->gb_allocated.bytes) << "\n";
d96ceb8e 764 /* overhead */
c21ad0f5 765 stream << "Current overhead: " << mp_total.tot_overhead << " bytes (" <<
35268c70 766 std::setprecision(3) << xpercent(mp_total.tot_overhead, mp_total.TheMeter->inuse.level) << "%)\n";
d96ceb8e 767 /* limits */
89646bd7
HN
768 if (mp_total.mem_idle_limit >= 0)
769 stream << "Idle pool limit: " << std::setprecision(2) << toMB(mp_total.mem_idle_limit) << " MB\n";
d96ceb8e 770 /* limits */
c21ad0f5 771 stream << "Total Pools created: " << mp_total.tot_pools_alloc << "\n";
772 stream << "Pools ever used: " << mp_total.tot_pools_alloc - not_used << " (shown above)\n";
773 stream << "Currently in use: " << mp_total.tot_pools_inuse << "\n";
d96ceb8e 774}