2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 13 High Level Memory Pool Management */
12 #include "acl/AclDenyInfoList.h"
13 #include "acl/AclNameList.h"
14 #include "base/PackableStream.h"
15 #include "ClientInfo.h"
19 #include "icmp/net_db.h"
21 #include "mem/forward.h"
22 #include "mem/Meter.h"
25 #include "mgr/Registration.h"
26 #include "SquidConfig.h"
27 #include "SquidList.h"
28 #include "SquidTime.h"
34 /* forward declarations */
35 static void memFree2K(void *);
36 static void memFree4K(void *);
37 static void memFree8K(void *);
38 static void memFree16K(void *);
39 static void memFree32K(void *);
40 static void memFree64K(void *);
42 /* local prototypes */
43 static void memStringStats(std::ostream
&);
46 static double xm_time
= 0;
47 static double xm_deltat
= 0;
50 #define mem_str_pool_count 6
57 static Mem::Meter StrCountMeter
;
58 static Mem::Meter StrVolumeMeter
;
60 static Mem::Meter HugeBufCountMeter
;
61 static Mem::Meter HugeBufVolumeMeter
;
64 static MemAllocator
*&
67 static MemAllocator
*pools
[MEM_MAX
];
68 static bool initialized
= false;
71 memset(pools
, '\0', sizeof(pools
));
73 // Mem::Init() makes use of GetPool(type) to initialize
74 // the actual pools. So must come after the flag is true
81 static MemAllocator
*&
82 GetStrPool(size_t type
)
84 static MemAllocator
*strPools
[mem_str_pool_count
];
85 static bool initialized
= false;
87 static const PoolMeta PoolAttrs
[mem_str_pool_count
] = {
88 {"Short Strings", MemAllocator::RoundedSize(36)}, /* to fit rfc1123 and similar */
89 {"Medium Strings", MemAllocator::RoundedSize(128)}, /* to fit most urls */
90 {"Long Strings", MemAllocator::RoundedSize(512)},
91 {"1KB Strings", MemAllocator::RoundedSize(1024)},
92 {"4KB Strings", MemAllocator::RoundedSize(4*1024)},
93 {"16KB Strings", MemAllocator::RoundedSize(16*1024)}
97 memset(strPools
, '\0', sizeof(strPools
));
99 /** Lastly init the string pools. */
100 for (int i
= 0; i
< mem_str_pool_count
; ++i
) {
101 strPools
[i
] = memPoolCreate(PoolAttrs
[i
].name
, PoolAttrs
[i
].obj_size
);
102 strPools
[i
]->zeroBlocks(false);
104 if (strPools
[i
]->objectSize() != PoolAttrs
[i
].obj_size
)
105 debugs(13, DBG_IMPORTANT
, "NOTICE: " << PoolAttrs
[i
].name
<<
106 " is " << strPools
[i
]->objectSize() <<
107 " bytes instead of requested " <<
108 PoolAttrs
[i
].obj_size
<< " bytes");
114 return strPools
[type
];
117 /* Find the best fit string pool type */
119 memFindStringSizeType(size_t net_size
, bool fuzzy
)
121 mem_type type
= MEM_NONE
;
122 for (unsigned int i
= 0; i
< mem_str_pool_count
; ++i
) {
123 auto pool
= GetStrPool(i
);
126 if (fuzzy
&& net_size
< pool
->objectSize()) {
127 type
= static_cast<mem_type
>(i
);
129 } else if (net_size
== pool
->objectSize()) {
130 type
= static_cast<mem_type
>(i
);
139 memStringStats(std::ostream
&stream
)
142 int pooled_count
= 0;
143 size_t pooled_volume
= 0;
145 stream
<< "String Pool\t Impact\t\t\n \t (%strings)\t (%volume)\n";
148 for (i
= 0; i
< mem_str_pool_count
; ++i
) {
149 const MemAllocator
*pool
= GetStrPool(i
);
150 const auto plevel
= pool
->getMeter().inuse
.currentLevel();
151 stream
<< std::setw(20) << std::left
<< pool
->objectType();
152 stream
<< std::right
<< "\t " << xpercentInt(plevel
, StrCountMeter
.currentLevel());
153 stream
<< "\t " << xpercentInt(plevel
* pool
->objectSize(), StrVolumeMeter
.currentLevel()) << "\n";
154 pooled_count
+= plevel
;
155 pooled_volume
+= plevel
* pool
->objectSize();
159 stream
<< std::setw(20) << std::left
<< "Other Strings";
160 stream
<< std::right
<< "\t ";
161 stream
<< xpercentInt(StrCountMeter
.currentLevel() - pooled_count
, StrCountMeter
.currentLevel()) << "\t ";
162 stream
<< xpercentInt(StrVolumeMeter
.currentLevel() - pooled_volume
, StrVolumeMeter
.currentLevel()) << "\n\n";
166 memBufStats(std::ostream
& stream
)
168 stream
<< "Large buffers: " <<
169 HugeBufCountMeter
.currentLevel() << " (" <<
170 HugeBufVolumeMeter
.currentLevel() / 1024 << " KB)\n";
174 Mem::Stats(StoreEntry
* sentry
)
176 PackableStream
stream(*sentry
);
178 memStringStats(stream
);
181 if (RUNNING_ON_VALGRIND
) {
182 long int leaked
= 0, dubious
= 0, reachable
= 0, suppressed
= 0;
183 stream
<< "Valgrind Report:\n";
184 stream
<< "Type\tAmount\n";
185 debugs(13, DBG_IMPORTANT
, "Asking valgrind for memleaks");
186 VALGRIND_DO_LEAK_CHECK
;
187 debugs(13, DBG_IMPORTANT
, "Getting valgrind statistics");
188 VALGRIND_COUNT_LEAKS(leaked
, dubious
, reachable
, suppressed
);
189 stream
<< "Leaked\t" << leaked
<< "\n";
190 stream
<< "Dubious\t" << dubious
<< "\n";
191 stream
<< "Reachable\t" << reachable
<< "\n";
192 stream
<< "Suppressed\t" << suppressed
<< "\n";
203 * we have a limit on _total_ amount of idle memory so we ignore max_pages for now.
204 * Will ignore repeated calls for the same pool type.
206 * Relies on Mem::Init() having been called beforehand.
209 memDataInit(mem_type type
, const char *name
, size_t size
, int, bool doZero
)
211 assert(name
&& size
);
213 if (GetPool(type
) != NULL
)
216 GetPool(type
) = memPoolCreate(name
, size
);
217 GetPool(type
)->zeroBlocks(doZero
);
220 /* find appropriate pool and use it (pools always init buffer with 0s) */
222 memAllocate(mem_type type
)
224 assert(GetPool(type
));
225 return GetPool(type
)->alloc();
228 /* give memory back to the pool */
230 memFree(void *p
, int type
)
232 assert(GetPool(type
));
233 GetPool(type
)->freeOne(p
);
236 /* allocate a variable size buffer using best-fit string pool */
238 memAllocString(size_t net_size
, size_t * gross_size
)
240 MemAllocator
*pool
= NULL
;
243 auto type
= memFindStringSizeType(net_size
, true);
244 if (type
!= MEM_NONE
)
245 pool
= GetStrPool(type
);
247 *gross_size
= pool
? pool
->objectSize() : net_size
;
248 assert(*gross_size
>= net_size
);
250 StrVolumeMeter
+= *gross_size
;
251 return pool
? pool
->alloc() : xcalloc(1, net_size
);
259 for (int counter
= 0; counter
< mem_str_pool_count
; ++counter
)
260 result
+= GetStrPool(counter
)->inUseCount();
265 /* free buffer allocated with memAllocString() */
267 memFreeString(size_t size
, void *buf
)
269 MemAllocator
*pool
= NULL
;
272 auto type
= memFindStringSizeType(size
, false);
273 if (type
!= MEM_NONE
)
274 pool
= GetStrPool(type
);
277 StrVolumeMeter
-= size
;
278 pool
? pool
->freeOne(buf
) : xfree(buf
);
281 /* Find the best fit MEM_X_BUF type */
283 memFindBufSizeType(size_t net_size
, size_t * gross_size
)
288 if (net_size
<= 2 * 1024) {
291 } else if (net_size
<= 4 * 1024) {
294 } else if (net_size
<= 8 * 1024) {
297 } else if (net_size
<= 16 * 1024) {
300 } else if (net_size
<= 32 * 1024) {
303 } else if (net_size
<= 64 * 1024) {
317 /* allocate a variable size buffer using best-fit pool */
319 memAllocBuf(size_t net_size
, size_t * gross_size
)
321 mem_type type
= memFindBufSizeType(net_size
, gross_size
);
323 if (type
!= MEM_NONE
)
324 return memAllocate(type
);
327 HugeBufVolumeMeter
+= *gross_size
;
328 return xcalloc(1, net_size
);
332 /* resize a variable sized buffer using best-fit pool */
334 memReallocBuf(void *oldbuf
, size_t net_size
, size_t * gross_size
)
336 /* XXX This can be optimized on very large buffers to use realloc() */
337 /* TODO: if the existing gross size is >= new gross size, do nothing */
338 size_t new_gross_size
;
339 void *newbuf
= memAllocBuf(net_size
, &new_gross_size
);
342 size_t data_size
= *gross_size
;
344 if (data_size
> net_size
)
345 data_size
= net_size
;
347 memcpy(newbuf
, oldbuf
, data_size
);
349 memFreeBuf(*gross_size
, oldbuf
);
352 *gross_size
= new_gross_size
;
356 /* free buffer allocated with memAllocBuf() */
358 memFreeBuf(size_t size
, void *buf
)
360 mem_type type
= memFindBufSizeType(size
, NULL
);
362 if (type
!= MEM_NONE
)
367 HugeBufVolumeMeter
-= size
;
371 static double clean_interval
= 15.0; /* time to live of idle chunk before release */
374 Mem::CleanIdlePools(void *)
376 MemPools::GetInstance().clean(static_cast<time_t>(clean_interval
));
377 eventAdd("memPoolCleanIdlePools", CleanIdlePools
, NULL
, clean_interval
, 1);
383 int64_t new_pool_limit
;
385 /** Set to configured value first */
386 if (!Config
.onoff
.mem_pools
)
388 else if (Config
.MemPools
.limit
> 0)
389 new_pool_limit
= Config
.MemPools
.limit
;
391 if (Config
.MemPools
.limit
== 0)
392 debugs(13, DBG_IMPORTANT
, "memory_pools_limit 0 has been chagned to memory_pools_limit none. Please update your config");
399 * No debugging here please because this method is called before
400 * the debug log is configured and we'll get the message on
401 * stderr when doing things like 'squid -k reconfigure'
403 if (MemPools::GetInstance().idleLimit() > new_pool_limit
)
404 debugs(13, DBG_IMPORTANT
, "Shrinking idle mem pools to "<< std::setprecision(3) << toMB(new_pool_limit
) << " MB");
407 MemPools::GetInstance().setIdleLimit(new_pool_limit
);
410 /* XXX make these classes do their own memory management */
411 #include "HttpHdrContRange.h"
416 /* all pools are ready to be used */
417 static bool MemIsInitialized
= false;
418 if (MemIsInitialized
)
422 * NOTE: Mem::Init() is called before the config file is parsed
423 * and before the debugging module has been initialized. Any
424 * debug messages here at level 0 or 1 will always be printed
429 * Then initialize all pools.
431 * Starting with generic 2kB - 64kB buffr pools, then specific object types.
433 * It does not hurt much to have a lot of pools since sizeof(MemPool) is
434 * small; someday we will figure out what to do with all the entries here
435 * that are never used or used only once; perhaps we should simply use
436 * malloc() for those? @?@
438 memDataInit(MEM_2K_BUF
, "2K Buffer", 2048, 10, false);
439 memDataInit(MEM_4K_BUF
, "4K Buffer", 4096, 10, false);
440 memDataInit(MEM_8K_BUF
, "8K Buffer", 8192, 10, false);
441 memDataInit(MEM_16K_BUF
, "16K Buffer", 16384, 10, false);
442 memDataInit(MEM_32K_BUF
, "32K Buffer", 32768, 10, false);
443 memDataInit(MEM_64K_BUF
, "64K Buffer", 65536, 10, false);
444 memDataInit(MEM_ACL_DENY_INFO_LIST
, "AclDenyInfoList",
445 sizeof(AclDenyInfoList
), 0);
446 memDataInit(MEM_ACL_NAME_LIST
, "acl_name_list", sizeof(AclNameList
), 0);
447 memDataInit(MEM_LINK_LIST
, "link_list", sizeof(link_list
), 10);
448 memDataInit(MEM_DLINK_NODE
, "dlink_node", sizeof(dlink_node
), 10);
449 memDataInit(MEM_DREAD_CTRL
, "dread_ctrl", sizeof(dread_ctrl
), 0);
450 memDataInit(MEM_DWRITE_Q
, "dwrite_q", sizeof(dwrite_q
), 0);
451 memDataInit(MEM_HTTP_HDR_CONTENT_RANGE
, "HttpHdrContRange", sizeof(HttpHdrContRange
), 0);
452 memDataInit(MEM_NETDBENTRY
, "netdbEntry", sizeof(netdbEntry
), 0);
453 memDataInit(MEM_NET_DB_NAME
, "net_db_name", sizeof(net_db_name
), 0);
454 memDataInit(MEM_CLIENT_INFO
, "ClientInfo", sizeof(ClientInfo
), 0);
455 memDataInit(MEM_MD5_DIGEST
, "MD5 digest", SQUID_MD5_DIGEST_LENGTH
, 0);
456 GetPool(MEM_MD5_DIGEST
)->setChunkSize(512 * 1024);
458 MemIsInitialized
= true;
460 // finally register with the cache manager
461 Mgr::RegisterAction("mem", "Memory Utilization", Mem::Stats
, 0, 1);
467 debugs(13, 3, "Memory pools are '" <<
468 (Config
.onoff
.mem_pools
? "on" : "off") << "'; limit: " <<
469 std::setprecision(3) << toMB(MemPools::GetInstance().idleLimit()) <<
473 mem_type
&operator++ (mem_type
&aMem
)
476 aMem
= (mem_type
)(++tmp
);
481 * Test that all entries are initialized
486 mem_type t
= MEM_NONE
;
488 while (++t
< MEM_DONTFREE
) {
490 * If you hit this assertion, then you forgot to add a
491 * memDataInit() line for type 't'.
492 * Or placed the pool type in the wrong section of the enum list.
501 MemPoolGlobalStats stats
;
502 if (Config
.MemPools
.limit
> 0) // do not reset if disabled or same
503 MemPools::GetInstance().setIdleLimit(0);
504 MemPools::GetInstance().clean(0);
505 memPoolGetGlobalStats(&stats
);
507 if (stats
.tot_items_inuse
)
508 debugs(13, 2, "memCleanModule: " << stats
.tot_items_inuse
<<
509 " items in " << stats
.tot_chunks_inuse
<< " chunks and " <<
510 stats
.tot_pools_inuse
<< " pools are left dirty");
514 memInUse(mem_type type
)
516 return GetPool(type
)->inUseCount();
524 memFree(p
, MEM_2K_BUF
);
530 memFree(p
, MEM_4K_BUF
);
536 memFree(p
, MEM_8K_BUF
);
542 memFree(p
, MEM_16K_BUF
);
548 memFree(p
, MEM_32K_BUF
);
554 memFree(p
, MEM_64K_BUF
);
558 cxx_xfree(void * ptr
)
564 memFreeBufFunc(size_t size
)
588 HugeBufVolumeMeter
-= size
;
596 Mem::PoolReport(const MemPoolStats
* mp_st
, const MemPoolMeter
* AllMeter
, std::ostream
&stream
)
600 MemPoolMeter
*pm
= mp_st
->meter
;
601 const char *delim
= "\t ";
603 stream
.setf(std::ios_base::fixed
);
604 stream
<< std::setw(20) << std::left
<< mp_st
->label
<< delim
;
605 stream
<< std::setw(4) << std::right
<< mp_st
->obj_size
<< delim
;
608 if (mp_st
->chunk_capacity
) {
609 stream
<< std::setw(4) << toKB(mp_st
->obj_size
* mp_st
->chunk_capacity
) << delim
;
610 stream
<< std::setw(4) << mp_st
->chunk_capacity
<< delim
;
612 needed
= mp_st
->items_inuse
/ mp_st
->chunk_capacity
;
614 if (mp_st
->items_inuse
% mp_st
->chunk_capacity
)
617 excess
= mp_st
->chunks_inuse
- needed
;
619 stream
<< std::setw(4) << mp_st
->chunks_alloc
<< delim
;
620 stream
<< std::setw(4) << mp_st
->chunks_inuse
<< delim
;
621 stream
<< std::setw(4) << mp_st
->chunks_free
<< delim
;
622 stream
<< std::setw(4) << mp_st
->chunks_partial
<< delim
;
623 stream
<< std::setprecision(3) << xpercent(excess
, needed
) << delim
;
634 * Fragmentation calculation:
635 * needed = inuse.currentLevel() / chunk_capacity
636 * excess = used - needed
637 * fragmentation = excess / needed * 100%
639 * Fragm = (alloced - (inuse / obj_ch) ) / alloced
642 stream
<< mp_st
->items_alloc
<< delim
;
643 stream
<< toKB(mp_st
->obj_size
* pm
->alloc
.currentLevel()) << delim
;
644 stream
<< toKB(mp_st
->obj_size
* pm
->alloc
.peak()) << delim
;
645 stream
<< std::setprecision(2) << ((squid_curtime
- pm
->alloc
.peakTime()) / 3600.) << delim
;
646 stream
<< std::setprecision(3) << xpercent(mp_st
->obj_size
* pm
->alloc
.currentLevel(), AllMeter
->alloc
.currentLevel()) << delim
;
648 stream
<< mp_st
->items_inuse
<< delim
;
649 stream
<< toKB(mp_st
->obj_size
* pm
->inuse
.currentLevel()) << delim
;
650 stream
<< toKB(mp_st
->obj_size
* pm
->inuse
.peak()) << delim
;
651 stream
<< std::setprecision(2) << ((squid_curtime
- pm
->inuse
.peakTime()) / 3600.) << delim
;
652 stream
<< std::setprecision(3) << xpercent(pm
->inuse
.currentLevel(), pm
->alloc
.currentLevel()) << delim
;
654 stream
<< mp_st
->items_idle
<< delim
;
655 stream
<< toKB(mp_st
->obj_size
* pm
->idle
.currentLevel()) << delim
;
656 stream
<< toKB(mp_st
->obj_size
* pm
->idle
.peak()) << delim
;
658 stream
<< (int)pm
->gb_saved
.count
<< delim
;
659 stream
<< std::setprecision(3) << xpercent(pm
->gb_saved
.count
, AllMeter
->gb_allocated
.count
) << delim
;
660 stream
<< std::setprecision(3) << xpercent(pm
->gb_saved
.bytes
, AllMeter
->gb_allocated
.bytes
) << delim
;
661 stream
<< std::setprecision(3) << xdiv(pm
->gb_allocated
.count
- pm
->gb_oallocated
.count
, xm_deltat
) << "\n";
662 pm
->gb_oallocated
.count
= pm
->gb_allocated
.count
;
666 MemPoolReportSorter(const void *a
, const void *b
)
668 const MemPoolStats
*A
= (MemPoolStats
*) a
;
669 const MemPoolStats
*B
= (MemPoolStats
*) b
;
671 // use this to sort on %Total Allocated
673 double pa
= (double) A
->obj_size
* A
->meter
->alloc
.currentLevel();
674 double pb
= (double) B
->obj_size
* B
->meter
->alloc
.currentLevel();
683 // use this to sort on In Use high(hrs)
685 if (A
->meter
->inuse
.peakTime() > B
->meter
->inuse
.peakTime())
688 if (B
->meter
->inuse
.peakTime() > A
->meter
->inuse
.peakTime())
697 Mem::Report(std::ostream
&stream
)
700 static MemPoolStats mp_stats
;
701 static MemPoolGlobalStats mp_total
;
703 MemPoolIterator
*iter
;
707 stream
<< "Current memory usage:\n";
709 stream
<< "Pool\t Obj Size\t"
710 "Chunks\t\t\t\t\t\t\t"
711 "Allocated\t\t\t\t\t"
714 "Allocations Saved\t\t\t"
719 "(#)\t used\t free\t part\t %Frag\t "
720 "(#)\t (KB)\t high (KB)\t high (hrs)\t %Tot\t"
721 "(#)\t (KB)\t high (KB)\t high (hrs)\t %alloc\t"
722 "(#)\t (KB)\t high (KB)\t"
723 "(#)\t %cnt\t %vol\t"
726 xm_deltat
= current_dtime
- xm_time
;
727 xm_time
= current_dtime
;
729 /* Get stats for Totals report line */
730 memPoolGetGlobalStats(&mp_total
);
732 MemPoolStats
*sortme
= (MemPoolStats
*) xcalloc(mp_total
.tot_pools_alloc
,sizeof(*sortme
));
736 iter
= memPoolIterate();
738 while ((pool
= memPoolIterateNext(iter
))) {
739 pool
->getStats(&mp_stats
);
741 if (!mp_stats
.pool
) /* pool destroyed */
744 if (mp_stats
.pool
->getMeter().gb_allocated
.count
> 0) {
745 /* this pool has been used */
746 sortme
[npools
] = mp_stats
;
753 memPoolIterateDone(&iter
);
755 qsort(sortme
, npools
, sizeof(*sortme
), MemPoolReportSorter
);
757 for (int i
= 0; i
< npools
; ++i
) {
758 PoolReport(&sortme
[i
], mp_total
.TheMeter
, stream
);
763 mp_stats
.pool
= NULL
;
764 mp_stats
.label
= "Total";
765 mp_stats
.meter
= mp_total
.TheMeter
;
766 mp_stats
.obj_size
= 1;
767 mp_stats
.chunk_capacity
= 0;
768 mp_stats
.chunk_size
= 0;
769 mp_stats
.chunks_alloc
= mp_total
.tot_chunks_alloc
;
770 mp_stats
.chunks_inuse
= mp_total
.tot_chunks_inuse
;
771 mp_stats
.chunks_partial
= mp_total
.tot_chunks_partial
;
772 mp_stats
.chunks_free
= mp_total
.tot_chunks_free
;
773 mp_stats
.items_alloc
= mp_total
.tot_items_alloc
;
774 mp_stats
.items_inuse
= mp_total
.tot_items_inuse
;
775 mp_stats
.items_idle
= mp_total
.tot_items_idle
;
776 mp_stats
.overhead
= mp_total
.tot_overhead
;
778 PoolReport(&mp_stats
, mp_total
.TheMeter
, stream
);
781 stream
<< "Cumulative allocated volume: "<< double_to_str(buf
, 64, mp_total
.TheMeter
->gb_allocated
.bytes
) << "\n";
783 stream
<< "Current overhead: " << mp_total
.tot_overhead
<< " bytes (" <<
784 std::setprecision(3) << xpercent(mp_total
.tot_overhead
, mp_total
.TheMeter
->inuse
.currentLevel()) << "%)\n";
786 if (mp_total
.mem_idle_limit
>= 0)
787 stream
<< "Idle pool limit: " << std::setprecision(2) << toMB(mp_total
.mem_idle_limit
) << " MB\n";
789 stream
<< "Total Pools created: " << mp_total
.tot_pools_alloc
<< "\n";
790 stream
<< "Pools ever used: " << mp_total
.tot_pools_alloc
- not_used
<< " (shown above)\n";
791 stream
<< "Currently in use: " << mp_total
.tot_pools_inuse
<< "\n";