heap_show.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * heap_show.c
  3. */
  4. #include <stdio.h>
  5. #include "cache.h"
  6. /*--------------------------------------------------------- */
  7. static void
  8. process_free_block(void *block,
  9. unsigned index,
  10. size_t size,
  11. void *arg)
  12. {
  13. /* dump the details for this block */
  14. printf(" %8p[%"SIZE_FMT"]",block,size);
  15. }
  16. /*--------------------------------------------------------- */
  17. static void
  18. process_segment(void *segment,
  19. unsigned index,
  20. size_t size,
  21. void *arg)
  22. {
  23. /* dump the details for this segment */
  24. printf(" %8p[%"SIZE_FMT"]",segment,size);
  25. }
  26. /*--------------------------------------------------------- */
  27. void
  28. lub_heap_show(lub_heap_t *this,
  29. bool_t verbose)
  30. {
  31. static char *state_names[] = {"DISABLED","ENABLED"};
  32. lub_heap_stats_t stats;
  33. if(BOOL_FALSE == lub_heap_check_memory(this))
  34. {
  35. printf("*** HEAP CORRUPTED!!! ***\n");
  36. }
  37. if(BOOL_TRUE == verbose)
  38. {
  39. printf("HEAP:\n"
  40. " %8p\n",(void*)this);
  41. printf("\nHEAP SEGMENTS:\n");
  42. lub_heap_foreach_segment(this,process_segment,NULL);
  43. printf("\n");
  44. if(this->cache)
  45. {
  46. printf("\nCACHE:\n");
  47. lub_heap_cache_show(this->cache);
  48. }
  49. /* dump each free block's details */
  50. printf("\nFREE BLOCKS:\n");
  51. lub_heap_foreach_free_block(this,process_free_block,NULL);
  52. printf("\n");
  53. printf("\nSUMMARY:\n");
  54. }
  55. {
  56. /*lint -esym(644,cache_stats) may not have been initialized */
  57. lub_heap_stats_t cache_stats;
  58. /* get the stats */
  59. lub_heap__get_stats(this,&stats);
  60. /* dump the statistics for this heap */
  61. printf(" status bytes blocks avg block max block overhead\n");
  62. printf(" ------ ---------- --------- ---------- ---------- ----------\n");
  63. printf("current\n");
  64. printf(" free %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10"SIZE_FMT" %10"SIZE_FMT"\n",
  65. stats.free_bytes,
  66. stats.free_blocks,
  67. stats.free_blocks ? stats.free_bytes /stats.free_blocks : 0,
  68. lub_heap__get_max_free(this),
  69. stats.free_overhead + stats.segs_overhead);
  70. printf(" alloc %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10s %10"SIZE_FMT"\n",
  71. stats.alloc_bytes,
  72. stats.alloc_blocks,
  73. stats.alloc_blocks ? stats.alloc_bytes / stats.alloc_blocks : 0,
  74. "-",
  75. stats.alloc_overhead);
  76. printf(" static %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10s %10"SIZE_FMT"\n",
  77. stats.static_bytes,
  78. stats.static_blocks,
  79. stats.static_blocks ? stats.static_bytes / stats.static_blocks : 0,
  80. "-",
  81. stats.static_overhead);
  82. if(this->cache)
  83. {
  84. printf(" (cache)\n");
  85. lub_heap_cache__get_stats(this->cache,&cache_stats);
  86. printf(" free %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10"SIZE_FMT" %10"SIZE_FMT"\n",
  87. cache_stats.free_bytes,
  88. cache_stats.free_blocks,
  89. cache_stats.free_blocks ? cache_stats.free_bytes /cache_stats.free_blocks : 0,
  90. lub_heap_cache__get_max_free(this->cache),
  91. cache_stats.free_overhead);
  92. printf(" alloc %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10s %10"SIZE_FMT"\n",
  93. cache_stats.alloc_bytes,
  94. cache_stats.alloc_blocks,
  95. cache_stats.alloc_blocks ? cache_stats.alloc_bytes / cache_stats.alloc_blocks : 0,
  96. "-",
  97. cache_stats.alloc_overhead);
  98. }
  99. printf("cumulative\n");
  100. printf(" alloc %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10s %10s\n",
  101. stats.alloc_total_bytes,
  102. stats.alloc_total_blocks,
  103. stats.alloc_total_blocks ? stats.alloc_total_bytes / stats.alloc_total_blocks : 0,
  104. "-",
  105. "-");
  106. if(this->cache)
  107. {
  108. printf(" (cache)\n");
  109. printf(" alloc %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10s %10s\n",
  110. cache_stats.alloc_total_bytes,
  111. cache_stats.alloc_total_blocks,
  112. cache_stats.alloc_total_blocks ? cache_stats.alloc_total_bytes / cache_stats.alloc_total_blocks : 0,
  113. "-",
  114. "-");
  115. }
  116. printf("high-tide\n");
  117. printf(" free %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10s %10"SIZE_FMT"\n",
  118. stats.free_hightide_bytes,
  119. stats.free_hightide_blocks,
  120. stats.free_hightide_blocks ? stats.free_hightide_bytes /stats.free_hightide_blocks : 0,
  121. "-",
  122. stats.free_hightide_overhead);
  123. printf(" alloc %10"SIZE_FMT" %9"SIZE_FMT" %10"SIZE_FMT" %10s %10"SIZE_FMT"\n",
  124. stats.alloc_hightide_bytes,
  125. stats.alloc_hightide_blocks,
  126. stats.alloc_hightide_blocks ? stats.alloc_hightide_bytes / stats.alloc_hightide_blocks : 0,
  127. "-",
  128. stats.alloc_hightide_overhead);
  129. }
  130. if(BOOL_TRUE == verbose)
  131. {
  132. printf("\nSYSTEM:\n"
  133. " tainting(%s), full checking(%s), leak detection(%s),\n"
  134. " native alignment of %d bytes\n",
  135. state_names[lub_heap_is_tainting()],
  136. state_names[lub_heap_is_checking()],
  137. state_names[lub_heap__get_framecount() ? 0 : 1],
  138. LUB_HEAP_ALIGN_NATIVE);
  139. }
  140. printf("\n");
  141. }