]> git.ipfire.org Git - thirdparty/squid.git/blob - src/mem/old_api.cc
Return a reference from GetStrPool()
[thirdparty/squid.git] / src / mem / old_api.cc
1 /*
2 * Copyright (C) 1996-2016 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 13 High Level Memory Pool Management */
10
11 #include "squid.h"
12 #include "acl/AclDenyInfoList.h"
13 #include "acl/AclNameList.h"
14 #include "base/PackableStream.h"
15 #include "ClientInfo.h"
16 #include "dlink.h"
17 #include "event.h"
18 #include "fs_io.h"
19 #include "icmp/net_db.h"
20 #include "md5.h"
21 #include "mem/forward.h"
22 #include "mem/Meter.h"
23 #include "mem/Pool.h"
24 #include "MemBuf.h"
25 #include "mgr/Registration.h"
26 #include "SquidConfig.h"
27 #include "SquidList.h"
28 #include "SquidTime.h"
29 #include "Store.h"
30
31 #include <iomanip>
32 #include <vector>
33
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 *);
41
42 /* local prototypes */
43 static void memStringStats(std::ostream &);
44
45 /* module locals */
46 static double xm_time = 0;
47 static double xm_deltat = 0;
48
49 /* string pools */
50 #define mem_str_pool_count 6
51
52 struct PoolMeta {
53 const char *name;
54 size_t obj_size;
55 };
56
57 static Mem::Meter StrCountMeter;
58 static Mem::Meter StrVolumeMeter;
59
60 static Mem::Meter HugeBufCountMeter;
61 static Mem::Meter HugeBufVolumeMeter;
62
63 /* local routines */
64 static MemAllocator *&
65 GetPool(size_t type)
66 {
67 static MemAllocator *pools[MEM_MAX];
68 static bool initialized = false;
69
70 if (!initialized) {
71 memset(pools, '\0', sizeof(pools));
72 initialized = true;
73 // Mem::Init() makes use of GetPool(type) to initialize
74 // the actual pools. So must come after the flag is true
75 Mem::Init();
76 }
77
78 return pools[type];
79 }
80
81 static MemAllocator &
82 GetStrPool(size_t type)
83 {
84 static MemAllocator *strPools[mem_str_pool_count];
85 static bool initialized = false;
86
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)}
94 };
95
96 if (!initialized) {
97 memset(strPools, '\0', sizeof(strPools));
98
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);
103
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");
109 }
110
111 initialized = true;
112 }
113
114 return *strPools[type];
115 }
116
117 /* Find the best fit string pool type */
118 static mem_type
119 memFindStringSizeType(size_t net_size, bool fuzzy)
120 {
121 mem_type type = MEM_NONE;
122 for (unsigned int i = 0; i < mem_str_pool_count; ++i) {
123 auto &pool = GetStrPool(i);
124 if (fuzzy && net_size < pool.objectSize()) {
125 type = static_cast<mem_type>(i);
126 break;
127 } else if (net_size == pool.objectSize()) {
128 type = static_cast<mem_type>(i);
129 break;
130 }
131 }
132
133 return type;
134 }
135
136 static void
137 memStringStats(std::ostream &stream)
138 {
139 int i;
140 int pooled_count = 0;
141 size_t pooled_volume = 0;
142 /* heading */
143 stream << "String Pool\t Impact\t\t\n \t (%strings)\t (%volume)\n";
144 /* table body */
145
146 for (i = 0; i < mem_str_pool_count; ++i) {
147 const auto &pool = GetStrPool(i);
148 const auto plevel = pool.getMeter().inuse.currentLevel();
149 stream << std::setw(20) << std::left << pool.objectType();
150 stream << std::right << "\t " << xpercentInt(plevel, StrCountMeter.currentLevel());
151 stream << "\t " << xpercentInt(plevel * pool.objectSize(), StrVolumeMeter.currentLevel()) << "\n";
152 pooled_count += plevel;
153 pooled_volume += plevel * pool.objectSize();
154 }
155
156 /* malloc strings */
157 stream << std::setw(20) << std::left << "Other Strings";
158 stream << std::right << "\t ";
159 stream << xpercentInt(StrCountMeter.currentLevel() - pooled_count, StrCountMeter.currentLevel()) << "\t ";
160 stream << xpercentInt(StrVolumeMeter.currentLevel() - pooled_volume, StrVolumeMeter.currentLevel()) << "\n\n";
161 }
162
163 static void
164 memBufStats(std::ostream & stream)
165 {
166 stream << "Large buffers: " <<
167 HugeBufCountMeter.currentLevel() << " (" <<
168 HugeBufVolumeMeter.currentLevel() / 1024 << " KB)\n";
169 }
170
171 void
172 Mem::Stats(StoreEntry * sentry)
173 {
174 PackableStream stream(*sentry);
175 Report(stream);
176 memStringStats(stream);
177 memBufStats(stream);
178 #if WITH_VALGRIND
179 if (RUNNING_ON_VALGRIND) {
180 long int leaked = 0, dubious = 0, reachable = 0, suppressed = 0;
181 stream << "Valgrind Report:\n";
182 stream << "Type\tAmount\n";
183 debugs(13, DBG_IMPORTANT, "Asking valgrind for memleaks");
184 VALGRIND_DO_LEAK_CHECK;
185 debugs(13, DBG_IMPORTANT, "Getting valgrind statistics");
186 VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed);
187 stream << "Leaked\t" << leaked << "\n";
188 stream << "Dubious\t" << dubious << "\n";
189 stream << "Reachable\t" << reachable << "\n";
190 stream << "Suppressed\t" << suppressed << "\n";
191 }
192 #endif
193 stream.flush();
194 }
195
196 /*
197 * public routines
198 */
199
200 /*
201 * we have a limit on _total_ amount of idle memory so we ignore max_pages for now.
202 * Will ignore repeated calls for the same pool type.
203 *
204 * Relies on Mem::Init() having been called beforehand.
205 */
206 void
207 memDataInit(mem_type type, const char *name, size_t size, int, bool doZero)
208 {
209 assert(name && size);
210
211 if (GetPool(type) != NULL)
212 return;
213
214 GetPool(type) = memPoolCreate(name, size);
215 GetPool(type)->zeroBlocks(doZero);
216 }
217
218 /* find appropriate pool and use it (pools always init buffer with 0s) */
219 void *
220 memAllocate(mem_type type)
221 {
222 assert(GetPool(type));
223 return GetPool(type)->alloc();
224 }
225
226 /* give memory back to the pool */
227 void
228 memFree(void *p, int type)
229 {
230 assert(GetPool(type));
231 GetPool(type)->freeOne(p);
232 }
233
234 /* allocate a variable size buffer using best-fit string pool */
235 void *
236 memAllocString(size_t net_size, size_t * gross_size)
237 {
238 assert(gross_size);
239
240 auto type = memFindStringSizeType(net_size, true);
241 if (type != MEM_NONE) {
242 auto &pool = GetStrPool(type);
243 *gross_size = pool.objectSize();
244 assert(*gross_size >= net_size);
245 ++StrCountMeter;
246 StrVolumeMeter += *gross_size;
247 return pool.alloc();
248 }
249
250 *gross_size = net_size;
251 ++StrCountMeter;
252 StrVolumeMeter += *gross_size;
253 return xcalloc(1, net_size);
254 }
255
256 size_t
257 memStringCount()
258 {
259 size_t result = 0;
260
261 for (int counter = 0; counter < mem_str_pool_count; ++counter)
262 result += GetStrPool(counter).inUseCount();
263
264 return result;
265 }
266
267 /* free buffer allocated with memAllocString() */
268 void
269 memFreeString(size_t size, void *buf)
270 {
271 assert(buf);
272
273 auto type = memFindStringSizeType(size, false);
274 if (type != MEM_NONE)
275 GetStrPool(type).freeOne(buf);
276 else
277 xfree(buf);
278
279 --StrCountMeter;
280 StrVolumeMeter -= size;
281 }
282
283 /* Find the best fit MEM_X_BUF type */
284 static mem_type
285 memFindBufSizeType(size_t net_size, size_t * gross_size)
286 {
287 mem_type type;
288 size_t size;
289
290 if (net_size <= 2 * 1024) {
291 type = MEM_2K_BUF;
292 size = 2 * 1024;
293 } else if (net_size <= 4 * 1024) {
294 type = MEM_4K_BUF;
295 size = 4 * 1024;
296 } else if (net_size <= 8 * 1024) {
297 type = MEM_8K_BUF;
298 size = 8 * 1024;
299 } else if (net_size <= 16 * 1024) {
300 type = MEM_16K_BUF;
301 size = 16 * 1024;
302 } else if (net_size <= 32 * 1024) {
303 type = MEM_32K_BUF;
304 size = 32 * 1024;
305 } else if (net_size <= 64 * 1024) {
306 type = MEM_64K_BUF;
307 size = 64 * 1024;
308 } else {
309 type = MEM_NONE;
310 size = net_size;
311 }
312
313 if (gross_size)
314 *gross_size = size;
315
316 return type;
317 }
318
319 /* allocate a variable size buffer using best-fit pool */
320 void *
321 memAllocBuf(size_t net_size, size_t * gross_size)
322 {
323 mem_type type = memFindBufSizeType(net_size, gross_size);
324
325 if (type != MEM_NONE)
326 return memAllocate(type);
327 else {
328 ++HugeBufCountMeter;
329 HugeBufVolumeMeter += *gross_size;
330 return xcalloc(1, net_size);
331 }
332 }
333
334 /* resize a variable sized buffer using best-fit pool */
335 void *
336 memReallocBuf(void *oldbuf, size_t net_size, size_t * gross_size)
337 {
338 /* XXX This can be optimized on very large buffers to use realloc() */
339 /* TODO: if the existing gross size is >= new gross size, do nothing */
340 size_t new_gross_size;
341 void *newbuf = memAllocBuf(net_size, &new_gross_size);
342
343 if (oldbuf) {
344 size_t data_size = *gross_size;
345
346 if (data_size > net_size)
347 data_size = net_size;
348
349 memcpy(newbuf, oldbuf, data_size);
350
351 memFreeBuf(*gross_size, oldbuf);
352 }
353
354 *gross_size = new_gross_size;
355 return newbuf;
356 }
357
358 /* free buffer allocated with memAllocBuf() */
359 void
360 memFreeBuf(size_t size, void *buf)
361 {
362 mem_type type = memFindBufSizeType(size, NULL);
363
364 if (type != MEM_NONE)
365 memFree(buf, type);
366 else {
367 xfree(buf);
368 --HugeBufCountMeter;
369 HugeBufVolumeMeter -= size;
370 }
371 }
372
373 static double clean_interval = 15.0; /* time to live of idle chunk before release */
374
375 void
376 Mem::CleanIdlePools(void *)
377 {
378 MemPools::GetInstance().clean(static_cast<time_t>(clean_interval));
379 eventAdd("memPoolCleanIdlePools", CleanIdlePools, NULL, clean_interval, 1);
380 }
381
382 void
383 memConfigure(void)
384 {
385 int64_t new_pool_limit;
386
387 /** Set to configured value first */
388 if (!Config.onoff.mem_pools)
389 new_pool_limit = 0;
390 else if (Config.MemPools.limit > 0)
391 new_pool_limit = Config.MemPools.limit;
392 else {
393 if (Config.MemPools.limit == 0)
394 debugs(13, DBG_IMPORTANT, "memory_pools_limit 0 has been chagned to memory_pools_limit none. Please update your config");
395 new_pool_limit = -1;
396 }
397
398 #if 0
399 /** \par
400 * DPW 2007-04-12
401 * No debugging here please because this method is called before
402 * the debug log is configured and we'll get the message on
403 * stderr when doing things like 'squid -k reconfigure'
404 */
405 if (MemPools::GetInstance().idleLimit() > new_pool_limit)
406 debugs(13, DBG_IMPORTANT, "Shrinking idle mem pools to "<< std::setprecision(3) << toMB(new_pool_limit) << " MB");
407 #endif
408
409 MemPools::GetInstance().setIdleLimit(new_pool_limit);
410 }
411
412 /* XXX make these classes do their own memory management */
413 #include "HttpHdrContRange.h"
414
415 void
416 Mem::Init(void)
417 {
418 /* all pools are ready to be used */
419 static bool MemIsInitialized = false;
420 if (MemIsInitialized)
421 return;
422
423 /** \par
424 * NOTE: Mem::Init() is called before the config file is parsed
425 * and before the debugging module has been initialized. Any
426 * debug messages here at level 0 or 1 will always be printed
427 * on stderr.
428 */
429
430 /**
431 * Then initialize all pools.
432 * \par
433 * Starting with generic 2kB - 64kB buffr pools, then specific object types.
434 * \par
435 * It does not hurt much to have a lot of pools since sizeof(MemPool) is
436 * small; someday we will figure out what to do with all the entries here
437 * that are never used or used only once; perhaps we should simply use
438 * malloc() for those? @?@
439 */
440 memDataInit(MEM_2K_BUF, "2K Buffer", 2048, 10, false);
441 memDataInit(MEM_4K_BUF, "4K Buffer", 4096, 10, false);
442 memDataInit(MEM_8K_BUF, "8K Buffer", 8192, 10, false);
443 memDataInit(MEM_16K_BUF, "16K Buffer", 16384, 10, false);
444 memDataInit(MEM_32K_BUF, "32K Buffer", 32768, 10, false);
445 memDataInit(MEM_64K_BUF, "64K Buffer", 65536, 10, false);
446 memDataInit(MEM_ACL_DENY_INFO_LIST, "AclDenyInfoList",
447 sizeof(AclDenyInfoList), 0);
448 memDataInit(MEM_ACL_NAME_LIST, "acl_name_list", sizeof(AclNameList), 0);
449 memDataInit(MEM_LINK_LIST, "link_list", sizeof(link_list), 10);
450 memDataInit(MEM_DLINK_NODE, "dlink_node", sizeof(dlink_node), 10);
451 memDataInit(MEM_DREAD_CTRL, "dread_ctrl", sizeof(dread_ctrl), 0);
452 memDataInit(MEM_DWRITE_Q, "dwrite_q", sizeof(dwrite_q), 0);
453 memDataInit(MEM_HTTP_HDR_CONTENT_RANGE, "HttpHdrContRange", sizeof(HttpHdrContRange), 0);
454 memDataInit(MEM_NETDBENTRY, "netdbEntry", sizeof(netdbEntry), 0);
455 memDataInit(MEM_NET_DB_NAME, "net_db_name", sizeof(net_db_name), 0);
456 memDataInit(MEM_CLIENT_INFO, "ClientInfo", sizeof(ClientInfo), 0);
457 memDataInit(MEM_MD5_DIGEST, "MD5 digest", SQUID_MD5_DIGEST_LENGTH, 0);
458 GetPool(MEM_MD5_DIGEST)->setChunkSize(512 * 1024);
459
460 MemIsInitialized = true;
461
462 // finally register with the cache manager
463 Mgr::RegisterAction("mem", "Memory Utilization", Mem::Stats, 0, 1);
464 }
465
466 void
467 Mem::Report()
468 {
469 debugs(13, 3, "Memory pools are '" <<
470 (Config.onoff.mem_pools ? "on" : "off") << "'; limit: " <<
471 std::setprecision(3) << toMB(MemPools::GetInstance().idleLimit()) <<
472 " MB");
473 }
474
475 mem_type &operator++ (mem_type &aMem)
476 {
477 int tmp = (int)aMem;
478 aMem = (mem_type)(++tmp);
479 return aMem;
480 }
481
482 /*
483 * Test that all entries are initialized
484 */
485 void
486 memCheckInit(void)
487 {
488 mem_type t = MEM_NONE;
489
490 while (++t < MEM_DONTFREE) {
491 /*
492 * If you hit this assertion, then you forgot to add a
493 * memDataInit() line for type 't'.
494 * Or placed the pool type in the wrong section of the enum list.
495 */
496 assert(GetPool(t));
497 }
498 }
499
500 void
501 memClean(void)
502 {
503 MemPoolGlobalStats stats;
504 if (Config.MemPools.limit > 0) // do not reset if disabled or same
505 MemPools::GetInstance().setIdleLimit(0);
506 MemPools::GetInstance().clean(0);
507 memPoolGetGlobalStats(&stats);
508
509 if (stats.tot_items_inuse)
510 debugs(13, 2, "memCleanModule: " << stats.tot_items_inuse <<
511 " items in " << stats.tot_chunks_inuse << " chunks and " <<
512 stats.tot_pools_inuse << " pools are left dirty");
513 }
514
515 int
516 memInUse(mem_type type)
517 {
518 return GetPool(type)->inUseCount();
519 }
520
521 /* ick */
522
523 void
524 memFree2K(void *p)
525 {
526 memFree(p, MEM_2K_BUF);
527 }
528
529 void
530 memFree4K(void *p)
531 {
532 memFree(p, MEM_4K_BUF);
533 }
534
535 void
536 memFree8K(void *p)
537 {
538 memFree(p, MEM_8K_BUF);
539 }
540
541 void
542 memFree16K(void *p)
543 {
544 memFree(p, MEM_16K_BUF);
545 }
546
547 void
548 memFree32K(void *p)
549 {
550 memFree(p, MEM_32K_BUF);
551 }
552
553 void
554 memFree64K(void *p)
555 {
556 memFree(p, MEM_64K_BUF);
557 }
558
559 static void
560 cxx_xfree(void * ptr)
561 {
562 xfree(ptr);
563 }
564
565 FREE *
566 memFreeBufFunc(size_t size)
567 {
568 switch (size) {
569
570 case 2 * 1024:
571 return memFree2K;
572
573 case 4 * 1024:
574 return memFree4K;
575
576 case 8 * 1024:
577 return memFree8K;
578
579 case 16 * 1024:
580 return memFree16K;
581
582 case 32 * 1024:
583 return memFree32K;
584
585 case 64 * 1024:
586 return memFree64K;
587
588 default:
589 --HugeBufCountMeter;
590 HugeBufVolumeMeter -= size;
591 return cxx_xfree;
592 }
593 }
594
595 /* MemPoolMeter */
596
597 void
598 Mem::PoolReport(const MemPoolStats * mp_st, const MemPoolMeter * AllMeter, std::ostream &stream)
599 {
600 int excess = 0;
601 int needed = 0;
602 MemPoolMeter *pm = mp_st->meter;
603 const char *delim = "\t ";
604
605 stream.setf(std::ios_base::fixed);
606 stream << std::setw(20) << std::left << mp_st->label << delim;
607 stream << std::setw(4) << std::right << mp_st->obj_size << delim;
608
609 /* Chunks */
610 if (mp_st->chunk_capacity) {
611 stream << std::setw(4) << toKB(mp_st->obj_size * mp_st->chunk_capacity) << delim;
612 stream << std::setw(4) << mp_st->chunk_capacity << delim;
613
614 needed = mp_st->items_inuse / mp_st->chunk_capacity;
615
616 if (mp_st->items_inuse % mp_st->chunk_capacity)
617 ++needed;
618
619 excess = mp_st->chunks_inuse - needed;
620
621 stream << std::setw(4) << mp_st->chunks_alloc << delim;
622 stream << std::setw(4) << mp_st->chunks_inuse << delim;
623 stream << std::setw(4) << mp_st->chunks_free << delim;
624 stream << std::setw(4) << mp_st->chunks_partial << delim;
625 stream << std::setprecision(3) << xpercent(excess, needed) << delim;
626 } else {
627 stream << delim;
628 stream << delim;
629 stream << delim;
630 stream << delim;
631 stream << delim;
632 stream << delim;
633 stream << delim;
634 }
635 /*
636 * Fragmentation calculation:
637 * needed = inuse.currentLevel() / chunk_capacity
638 * excess = used - needed
639 * fragmentation = excess / needed * 100%
640 *
641 * Fragm = (alloced - (inuse / obj_ch) ) / alloced
642 */
643 /* allocated */
644 stream << mp_st->items_alloc << delim;
645 stream << toKB(mp_st->obj_size * pm->alloc.currentLevel()) << delim;
646 stream << toKB(mp_st->obj_size * pm->alloc.peak()) << delim;
647 stream << std::setprecision(2) << ((squid_curtime - pm->alloc.peakTime()) / 3600.) << delim;
648 stream << std::setprecision(3) << xpercent(mp_st->obj_size * pm->alloc.currentLevel(), AllMeter->alloc.currentLevel()) << delim;
649 /* in use */
650 stream << mp_st->items_inuse << delim;
651 stream << toKB(mp_st->obj_size * pm->inuse.currentLevel()) << delim;
652 stream << toKB(mp_st->obj_size * pm->inuse.peak()) << delim;
653 stream << std::setprecision(2) << ((squid_curtime - pm->inuse.peakTime()) / 3600.) << delim;
654 stream << std::setprecision(3) << xpercent(pm->inuse.currentLevel(), pm->alloc.currentLevel()) << delim;
655 /* idle */
656 stream << mp_st->items_idle << delim;
657 stream << toKB(mp_st->obj_size * pm->idle.currentLevel()) << delim;
658 stream << toKB(mp_st->obj_size * pm->idle.peak()) << delim;
659 /* saved */
660 stream << (int)pm->gb_saved.count << delim;
661 stream << std::setprecision(3) << xpercent(pm->gb_saved.count, AllMeter->gb_allocated.count) << delim;
662 stream << std::setprecision(3) << xpercent(pm->gb_saved.bytes, AllMeter->gb_allocated.bytes) << delim;
663 stream << std::setprecision(3) << xdiv(pm->gb_allocated.count - pm->gb_oallocated.count, xm_deltat) << "\n";
664 pm->gb_oallocated.count = pm->gb_allocated.count;
665 }
666
667 static int
668 MemPoolReportSorter(const void *a, const void *b)
669 {
670 const MemPoolStats *A = (MemPoolStats *) a;
671 const MemPoolStats *B = (MemPoolStats *) b;
672
673 // use this to sort on %Total Allocated
674 //
675 double pa = (double) A->obj_size * A->meter->alloc.currentLevel();
676 double pb = (double) B->obj_size * B->meter->alloc.currentLevel();
677
678 if (pa > pb)
679 return -1;
680
681 if (pb > pa)
682 return 1;
683
684 #if 0
685 // use this to sort on In Use high(hrs)
686 //
687 if (A->meter->inuse.peakTime() > B->meter->inuse.peakTime())
688 return -1;
689
690 if (B->meter->inuse.peakTime() > A->meter->inuse.peakTime())
691 return 1;
692
693 #endif
694
695 return 0;
696 }
697
698 void
699 Mem::Report(std::ostream &stream)
700 {
701 static char buf[64];
702 static MemPoolStats mp_stats;
703 static MemPoolGlobalStats mp_total;
704 int not_used = 0;
705 MemPoolIterator *iter;
706 MemAllocator *pool;
707
708 /* caption */
709 stream << "Current memory usage:\n";
710 /* heading */
711 stream << "Pool\t Obj Size\t"
712 "Chunks\t\t\t\t\t\t\t"
713 "Allocated\t\t\t\t\t"
714 "In Use\t\t\t\t\t"
715 "Idle\t\t\t"
716 "Allocations Saved\t\t\t"
717 "Rate\t"
718 "\n"
719 " \t (bytes)\t"
720 "KB/ch\t obj/ch\t"
721 "(#)\t used\t free\t part\t %Frag\t "
722 "(#)\t (KB)\t high (KB)\t high (hrs)\t %Tot\t"
723 "(#)\t (KB)\t high (KB)\t high (hrs)\t %alloc\t"
724 "(#)\t (KB)\t high (KB)\t"
725 "(#)\t %cnt\t %vol\t"
726 "(#)/sec\t"
727 "\n";
728 xm_deltat = current_dtime - xm_time;
729 xm_time = current_dtime;
730
731 /* Get stats for Totals report line */
732 memPoolGetGlobalStats(&mp_total);
733
734 MemPoolStats *sortme = (MemPoolStats *) xcalloc(mp_total.tot_pools_alloc ,sizeof(*sortme));
735 int npools = 0;
736
737 /* main table */
738 iter = memPoolIterate();
739
740 while ((pool = memPoolIterateNext(iter))) {
741 pool->getStats(&mp_stats);
742
743 if (!mp_stats.pool) /* pool destroyed */
744 continue;
745
746 if (mp_stats.pool->getMeter().gb_allocated.count > 0) {
747 /* this pool has been used */
748 sortme[npools] = mp_stats;
749 ++npools;
750 } else {
751 ++not_used;
752 }
753 }
754
755 memPoolIterateDone(&iter);
756
757 qsort(sortme, npools, sizeof(*sortme), MemPoolReportSorter);
758
759 for (int i = 0; i< npools; ++i) {
760 PoolReport(&sortme[i], mp_total.TheMeter, stream);
761 }
762
763 xfree(sortme);
764
765 mp_stats.pool = NULL;
766 mp_stats.label = "Total";
767 mp_stats.meter = mp_total.TheMeter;
768 mp_stats.obj_size = 1;
769 mp_stats.chunk_capacity = 0;
770 mp_stats.chunk_size = 0;
771 mp_stats.chunks_alloc = mp_total.tot_chunks_alloc;
772 mp_stats.chunks_inuse = mp_total.tot_chunks_inuse;
773 mp_stats.chunks_partial = mp_total.tot_chunks_partial;
774 mp_stats.chunks_free = mp_total.tot_chunks_free;
775 mp_stats.items_alloc = mp_total.tot_items_alloc;
776 mp_stats.items_inuse = mp_total.tot_items_inuse;
777 mp_stats.items_idle = mp_total.tot_items_idle;
778 mp_stats.overhead = mp_total.tot_overhead;
779
780 PoolReport(&mp_stats, mp_total.TheMeter, stream);
781
782 /* Cumulative */
783 stream << "Cumulative allocated volume: "<< double_to_str(buf, 64, mp_total.TheMeter->gb_allocated.bytes) << "\n";
784 /* overhead */
785 stream << "Current overhead: " << mp_total.tot_overhead << " bytes (" <<
786 std::setprecision(3) << xpercent(mp_total.tot_overhead, mp_total.TheMeter->inuse.currentLevel()) << "%)\n";
787 /* limits */
788 if (mp_total.mem_idle_limit >= 0)
789 stream << "Idle pool limit: " << std::setprecision(2) << toMB(mp_total.mem_idle_limit) << " MB\n";
790 /* limits */
791 stream << "Total Pools created: " << mp_total.tot_pools_alloc << "\n";
792 stream << "Pools ever used: " << mp_total.tot_pools_alloc - not_used << " (shown above)\n";
793 stream << "Currently in use: " << mp_total.tot_pools_inuse << "\n";
794 }
795