heap_show.c 4.9 KB

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