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