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