Provide a GMScore based AccountIdProvider by default.
[chromium-blink-merge.git] / courgette / memory_monitor.cc
blob2769cdc30aeabe1ea1824d333889a4bf8b3cb3af
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.
5 #include <stdio.h>
6 #include <map>
8 #include "base/logging.h"
9 #include "base/strings/string_util.h"
11 bool inH = true;
12 struct H {
13 H() { inH = false; tick_ = 0; bw_ = 0; d_bw_ = d_tick_ = 0; m_bw_ = 0; mem_ = high_ = 0;}
14 ~H() {
15 inH = true;
16 int i = 0;
17 for (M::iterator p = m_.begin(); p != m_.end(); ++p, ++i) {
18 size_t s = p->first;
19 LOG(INFO) << base::StringPrintf("%3d %8u: %8u %8u %8u %8u", i, s,
20 m_[s], c_[s], h_[s], h_[s] * s);
22 LOG(INFO) << "Peak " << fmt(high_);
25 std::string fmt(size_t s) {
26 if (s > 1000000000) return base::StringPrintf("%.3gG", s/(1000000000.0));
27 if (s > 1000000) return base::StringPrintf("%.3gM", s/(1000000.));
28 if (s > 9999) return base::StringPrintf("%.3gk", s/(1000.));
29 return base::IntToString((int)s);
32 void tick(size_t w, char sign) {
33 d_tick_ += 1;
34 d_bw_ += w;
35 const size_t T = 4*4*1024;
36 const size_t M = 4*1024*1024;
37 bool print = false;
38 if (d_tick_ >= T) {
39 tick_ += (d_tick_/T)*T;
40 d_tick_ %= T;
41 print = true;
43 if (d_bw_ >= M) {
44 bw_ += (d_bw_/M) * M;
45 d_bw_ %= M;
46 print = true;
48 if (!print) return;
49 std::string o;
50 base::StringAppendF(&o, "%u:", tick_ + d_tick_);
51 base::StringAppendF(&o, " (%c%s)", sign, fmt(w).c_str());
52 size_t sum = 0;
53 for (M::iterator p = c_.begin(); p != c_.end(); ++p) {
54 size_t s = p->first;
55 size_t n = p->second;
56 if (n) {
57 if (s*n >= 64*1024)
58 if (n == 1)
59 base::StringAppendF(&o, " %s", fmt(s).c_str());
60 else
61 base::StringAppendF(&o, " %s*%u", fmt(s).c_str(), n);
62 sum += s*n;
65 base::StringAppendF(&o, " = %s", fmt(sum).c_str());
66 LOG(INFO) << o;
67 //printf("%s\n", o.c_str());
68 if (sum > 200*1024*1024) {
69 // __asm int 3;
70 m_bw_ = sum;
73 void add(size_t s, void *p) {
74 if (!inH) {
75 inH = true;
76 mem_ += s; if (mem_ > high_) high_ = mem_;
77 c_[s] += 1;
78 m_[s] += 1;
79 if (c_[s] > h_[s]) h_[s] = c_[s];
80 allocs_[p] = s;
81 inH = false;
82 tick(s, '+');
86 void sub(void *p) {
87 if (!inH) {
88 inH = true;
89 size_t s = allocs_[p];
90 if (s) {
91 mem_ -= s;
92 c_[s] -= 1;
93 allocs_[p] = 0;
94 tick(s, '-');
96 inH = false;
100 typedef std::map<size_t, size_t> M;
101 M m_;
102 M c_;
103 M h_;
105 size_t bw_;
106 size_t d_bw_;
107 size_t tick_;
108 size_t d_tick_;
109 size_t m_bw_;
110 size_t mem_;
111 size_t high_;
113 std::map<void*, size_t> allocs_;
114 } _H;
116 void* operator new(size_t s) {
117 //printf("%u\n", s);
118 void *p = malloc(s);
119 _H.add(s, p);
120 return p;
123 void operator delete(void *p) {
124 _H.sub(p);
125 free(p);