memory_monitor.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (c) 2009 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <stddef.h>
  5. #include <stdio.h>
  6. #include <map>
  7. #include "base/logging.h"
  8. #include "base/strings/string_util.h"
  9. #include "base/strings/stringprintf.h"
  10. bool inH = true;
  11. struct H {
  12. H() { inH = false; tick_ = 0; bw_ = 0; d_bw_ = d_tick_ = 0; m_bw_ = 0; mem_ = high_ = 0;}
  13. ~H() {
  14. inH = true;
  15. int i = 0;
  16. for (M::iterator p = m_.begin(); p != m_.end(); ++p, ++i) {
  17. size_t s = p->first;
  18. LOG(INFO) << base::StringPrintf("%3d %8u: %8u %8u %8u %8u", i, s,
  19. m_[s], c_[s], h_[s], h_[s] * s);
  20. }
  21. LOG(INFO) << "Peak " << fmt(high_);
  22. }
  23. std::string fmt(size_t s) {
  24. if (s > 1000000000) return base::StringPrintf("%.3gG", s/(1000000000.0));
  25. if (s > 1000000) return base::StringPrintf("%.3gM", s/(1000000.));
  26. if (s > 9999) return base::StringPrintf("%.3gk", s/(1000.));
  27. return base::NumberToString(s);
  28. }
  29. void tick(size_t w, char sign) {
  30. d_tick_ += 1;
  31. d_bw_ += w;
  32. const size_t T = 4*4*1024;
  33. const size_t M = 4*1024*1024;
  34. bool print = false;
  35. if (d_tick_ >= T) {
  36. tick_ += (d_tick_/T)*T;
  37. d_tick_ %= T;
  38. print = true;
  39. }
  40. if (d_bw_ >= M) {
  41. bw_ += (d_bw_/M) * M;
  42. d_bw_ %= M;
  43. print = true;
  44. }
  45. if (!print) return;
  46. std::string o;
  47. base::StringAppendF(&o, "%u:", tick_ + d_tick_);
  48. base::StringAppendF(&o, " (%c%s)", sign, fmt(w).c_str());
  49. size_t sum = 0;
  50. for (M::iterator p = c_.begin(); p != c_.end(); ++p) {
  51. size_t s = p->first;
  52. size_t n = p->second;
  53. if (n) {
  54. if (s*n >= 64*1024)
  55. if (n == 1)
  56. base::StringAppendF(&o, " %s", fmt(s).c_str());
  57. else
  58. base::StringAppendF(&o, " %s*%u", fmt(s).c_str(), n);
  59. sum += s*n;
  60. }
  61. }
  62. base::StringAppendF(&o, " = %s", fmt(sum).c_str());
  63. LOG(INFO) << o;
  64. //printf("%s\n", o.c_str());
  65. if (sum > 200*1024*1024) {
  66. // __asm int 3;
  67. m_bw_ = sum;
  68. }
  69. }
  70. void add(size_t s, void *p) {
  71. if (!inH) {
  72. inH = true;
  73. mem_ += s; if (mem_ > high_) high_ = mem_;
  74. c_[s] += 1;
  75. m_[s] += 1;
  76. if (c_[s] > h_[s]) h_[s] = c_[s];
  77. allocs_[p] = s;
  78. inH = false;
  79. tick(s, '+');
  80. }
  81. }
  82. void sub(void *p) {
  83. if (!inH) {
  84. inH = true;
  85. size_t s = allocs_[p];
  86. if (s) {
  87. mem_ -= s;
  88. c_[s] -= 1;
  89. allocs_[p] = 0;
  90. tick(s, '-');
  91. }
  92. inH = false;
  93. }
  94. }
  95. typedef std::map<size_t, size_t> M;
  96. M m_;
  97. M c_;
  98. M h_;
  99. size_t bw_;
  100. size_t d_bw_;
  101. size_t tick_;
  102. size_t d_tick_;
  103. size_t m_bw_;
  104. size_t mem_;
  105. size_t high_;
  106. std::map<void*, size_t> allocs_;
  107. } _H;
  108. void* operator new(size_t s) {
  109. //printf("%u\n", s);
  110. void *p = malloc(s);
  111. _H.add(s, p);
  112. return p;
  113. }
  114. void operator delete(void *p) {
  115. _H.sub(p);
  116. free(p);
  117. }