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