HeapBucketStats.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "CommonMemoryPch.h"
  6. #ifdef __clang__
  7. #include <cxxabi.h>
  8. #endif
  9. #if ENABLE_MEM_STATS
  10. MemStats::MemStats()
  11. : objectByteCount(0), totalByteCount(0)
  12. {}
  13. void MemStats::Reset()
  14. {
  15. objectByteCount = 0;
  16. totalByteCount = 0;
  17. }
  18. size_t MemStats::FreeBytes() const
  19. {
  20. return totalByteCount - objectByteCount;
  21. }
  22. double MemStats::UsedRatio() const
  23. {
  24. return (double)objectByteCount / totalByteCount;
  25. }
  26. void MemStats::Aggregate(const MemStats& other)
  27. {
  28. objectByteCount += other.objectByteCount;
  29. totalByteCount += other.totalByteCount;
  30. }
  31. #ifdef DUMP_FRAGMENTATION_STATS
  32. HeapBucketStats::HeapBucketStats()
  33. : totalBlockCount(0), objectCount(0), finalizeCount(0)
  34. {}
  35. void HeapBucketStats::Reset()
  36. {
  37. MemStats::Reset();
  38. totalBlockCount = 0;
  39. objectCount = 0;
  40. finalizeCount = 0;
  41. }
  42. void HeapBucketStats::Dump() const
  43. {
  44. Output::Print(_u("%5d %7d %7d %11lu %11lu %11lu %6.2f%%\n"),
  45. totalBlockCount, objectCount, finalizeCount,
  46. static_cast<ULONG>(objectByteCount),
  47. static_cast<ULONG>(FreeBytes()),
  48. static_cast<ULONG>(totalByteCount),
  49. UsedRatio() * 100);
  50. }
  51. #endif
  52. void HeapBucketStats::PreAggregate()
  53. {
  54. // When first enter Pre-Aggregate state, clear data and mark state.
  55. if (!(totalByteCount & 1))
  56. {
  57. Reset();
  58. totalByteCount |= 1;
  59. }
  60. }
  61. void HeapBucketStats::BeginAggregate()
  62. {
  63. // If was Pre-Aggregate state, keep data and clear state
  64. if (totalByteCount & 1)
  65. {
  66. totalByteCount &= ~1;
  67. }
  68. else
  69. {
  70. Reset();
  71. }
  72. }
  73. void HeapBucketStats::Aggregate(const HeapBucketStats& other)
  74. {
  75. MemStats::Aggregate(other);
  76. #ifdef DUMP_FRAGMENTATION_STATS
  77. totalBlockCount += other.totalBlockCount;
  78. objectCount += other.objectCount;
  79. finalizeCount += other.finalizeCount;
  80. #endif
  81. }
  82. #endif // ENABLE_MEM_STATS