Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(479)

Side by Side Diff: third_party/tcmalloc/chromium/src/deep-heap-profile.cc

Issue 8632007: A deeper heap profile dumper in third_party/tcmalloc/chromium. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Fixed the snprintf issues. Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 // ---
6 // Author: Sainbayar Sukhbaatar
7 // Dai Mikurube
8 //
9
10 #include "deep-heap-profile.h"
11
12 #ifdef DEEP_HEAP_PROFILE
13 #include <fcntl.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h> // for getpid()
18 #endif
19
20 #include "base/cycleclock.h"
21 #include "base/sysinfo.h"
22
23 static const int kProfilerBufferSize = 1 << 20;
24 static const int kHashTableSize = 179999; // The same as heap-profile-table.cc.
25
26 static const int PAGE_SIZE = 4096;
27 static const int PAGEMAP_BYTES = 8;
28 static const uint64 TOP_ADDRESS = kuint64max;
29
30 // Header strings of the dumped heap profile.
31 static const char kProfileHeader[] = "Deep Memory Profile\n";
32 static const char kGlobalStatsHeader[] = "GLOBAL_STATS:\n";
33 static const char kStacktraceHeader[] = "STACKTRACES:\n";
34 static const char kProcSelfMapsHeader[] = "\nMAPPED_LIBRARIES:\n";
35
36 DeepHeapProfile::DeepHeapProfile(HeapProfileTable* heap_profile,
37 const char* prefix)
38 : heap_profile_(heap_profile),
39 pagemap_fd_(-1),
40 most_recent_pid_(-1),
41 stats_(),
42 dump_count_(0),
43 filename_prefix_(NULL),
44 profiler_buffer_(NULL),
45 bucket_id_(0) {
46 deep_bucket_map_ = new(heap_profile_->alloc_(sizeof(DeepBucketMap)))
47 DeepBucketMap(heap_profile_->alloc_, heap_profile_->dealloc_);
48
49 // Copy filename prefix.
50 const int prefix_length = strlen(prefix);
51 filename_prefix_ =
52 reinterpret_cast<char*>(heap_profile_->alloc_(prefix_length + 1));
53 memcpy(filename_prefix_, prefix, prefix_length);
54 filename_prefix_[prefix_length] = '\0';
55
56 profiler_buffer_ =
57 reinterpret_cast<char*>(heap_profile_->alloc_(kProfilerBufferSize));
58 }
59
60 DeepHeapProfile::~DeepHeapProfile() {
61 heap_profile_->dealloc_(profiler_buffer_);
62 heap_profile_->dealloc_(filename_prefix_);
63 deep_bucket_map_->~DeepBucketMap();
64 heap_profile_->dealloc_(deep_bucket_map_);
65 }
66
67 int DeepHeapProfile::FillOrderedProfile(char buf[], int size) {
68 #ifndef NDEBUG
69 int64 starting_cycles = CycleClock::Now();
70 #endif
71 ++dump_count_;
72
73 // Re-open files in /proc/pid/ if the process is newly forked one.
74 if (most_recent_pid_ != getpid()) {
75 most_recent_pid_ = getpid();
76 OpenProcPagemap();
77
78 // Write maps into a .maps file with using the global buffer.
79 WriteMapsToFile(profiler_buffer_, kProfilerBufferSize);
80 }
81
82 // Reset committed sizes of buckets.
83 ResetCommittedSize(heap_profile_->malloc_table_);
84 ResetCommittedSize(heap_profile_->mmap_table_);
85
86 GetGlobalStats();
87 size_t anonymous_committed = stats_.anonymous.committed_bytes;
88
89 // Note: Don't allocate any memory from here.
90
91 // Record committed sizes.
92 RecordAllAllocs();
93
94 // Check if committed bytes changed during RecordAllAllocs.
95 GetGlobalStats();
96 #ifndef NDEBUG
97 size_t committed_difference =
98 stats_.anonymous.committed_bytes - anonymous_committed;
99 if (committed_difference != 0)
100 RAW_LOG(0, "Difference in committed size: %ld", committed_difference);
101 #endif
102
103 HeapProfileTable::Stats stats;
104 memset(&stats, 0, sizeof(stats));
105
106 // Start filling buf with the ordered profile.
107 int printed = snprintf(buf, size, kProfileHeader);
108 if (printed < 0 || printed >= size) return 0;
jar (doing other things) 2012/01/06 03:05:10 nit: prefer putting condition on separate line unl
Dai Mikurube (NOT FULLTIME) 2012/01/06 20:11:27 Done. I like the "separated lines" policy actuall
109 int bucket_length = printed;
jar (doing other things) 2012/01/06 03:05:10 nit: I really liked you variable name |printed|, b
Dai Mikurube (NOT FULLTIME) 2012/01/06 20:11:27 Agreed. Replaced to 'used_in_buffer'. On 2012/01
110
111 // Fill buf with the global stats.
112 printed = snprintf(buf + bucket_length, size - bucket_length,
113 kGlobalStatsHeader);
jar (doing other things) 2012/01/06 03:05:10 nit: wrap args at paren.
Dai Mikurube (NOT FULLTIME) 2012/01/06 20:11:27 Done.
114 if (printed < 0 || printed >= size - bucket_length) return bucket_length;
jar (doing other things) 2012/01/06 03:05:10 nit: prefer putting condition on separate line unl
Dai Mikurube (NOT FULLTIME) 2012/01/06 20:11:27 Done.
115 bucket_length += printed;
116
117 bucket_length = UnparseGlobalStats(buf, bucket_length, size);
118
119 // Fill buf with the header for buckets.
120 printed = snprintf(buf + bucket_length, size - bucket_length,
121 kStacktraceHeader);
122 if (printed < 0 || printed >= size - bucket_length) return bucket_length;
jar (doing other things) 2012/01/06 03:05:10 nit: prefer putting condition on separate line unl
Dai Mikurube (NOT FULLTIME) 2012/01/06 20:11:27 Done.
123 bucket_length += printed;
124
125 printed = snprintf(buf + bucket_length, size - bucket_length,
126 "%10s %10s\n", "virtual", "committed");
127 if (printed < 0 || printed >= size - bucket_length) return bucket_length;
jar (doing other things) 2012/01/06 03:05:10 nit: condition on separate line please.
Dai Mikurube (NOT FULLTIME) 2012/01/06 20:11:27 Done.
128 bucket_length += printed;
129
130 // Fill buf with stack trace buckets.
131 bucket_length = FillBucketTable(heap_profile_->malloc_table_,
132 buf, size, bucket_length, &stats);
133 bucket_length = FillBucketTable(heap_profile_->mmap_table_,
134 buf, size, bucket_length, &stats);
135
136 RAW_DCHECK(bucket_length < size, "");
137
138 // Note: Don't allocate any memory until here.
139
140 // Write the bucket listing into a .bucket file.
141 WriteBucketsToBucketFile();
142
143 #ifndef NDEBUG
144 int64 elapsed_cycles = CycleClock::Now() - starting_cycles;
145 double elapsed_seconds = elapsed_cycles / CyclesPerSecond();
146 RAW_LOG(0, "Time spent on DeepProfiler: %.3f sec\n", elapsed_seconds);
147 #endif
148
149 return bucket_length;
150 }
151
152 DeepHeapProfile::DeepBucket*
153 DeepHeapProfile::GetDeepBucket(Bucket* bucket) {
154 DeepBucket* found = deep_bucket_map_->FindMutable(bucket);
155 if (found == NULL) {
156 DeepBucket created;
157 created.bucket = bucket;
158 created.committed_size = 0;
159 created.id = (bucket_id_++);
160 created.is_logged = false;
161 deep_bucket_map_->Insert(bucket, created);
162 return deep_bucket_map_->FindMutable(bucket);
163 } else {
164 return found;
165 }
166 }
167
168 void DeepHeapProfile::ResetCommittedSize(Bucket** table) {
169 for (int i = 0; i < kHashTableSize; i++) {
170 for (Bucket* b = table[i]; b != 0; b = b->next) {
171 DeepBucket* db = GetDeepBucket(b);
172 db->committed_size = 0;
173 }
174 }
175 }
176
177 int DeepHeapProfile::FillBucketTable(Bucket** table,
178 char buf[], int size, int bucket_length,
179 HeapProfileTable::Stats* stats) {
180 for (int i = 0; i < kHashTableSize; i++) {
181 for (Bucket* b = table[i]; b != 0; b = b->next) {
182 if (b->alloc_size - b->free_size == 0)
183 continue; // Skip empty buckets.
184 const DeepBucket& db = *GetDeepBucket(b);
185 bucket_length = UnparseBucket(db, buf, bucket_length, size, "", stats);
186 }
187 }
188 return bucket_length;
189 }
190
191 // This function need to be called after each fork.
192 void DeepHeapProfile::OpenProcPagemap() {
193 char filename[100];
194 sprintf(filename, "/proc/%d/pagemap", getpid());
195 pagemap_fd_ = open(filename, O_RDONLY);
196 RAW_DCHECK(pagemap_fd_ != -1, "Failed to open /proc/self/pagemap");
197 }
198
199 bool DeepHeapProfile::SeekProcPagemap(uint64 address) {
200 uint64 index = (address / PAGE_SIZE) * PAGEMAP_BYTES;
201 uint64 o = lseek64(pagemap_fd_, index, SEEK_SET);
202 RAW_DCHECK(o == index, "");
203 return true;
204 }
205
206 bool DeepHeapProfile::ReadProcPagemap(PageState* state) {
207 static const uint64 U64_1 = 1;
208 static const uint64 PFN_FILTER = (U64_1 << 55) - U64_1;
209 static const uint64 PAGE_PRESENT = U64_1 << 63;
210 static const uint64 PAGE_SWAP = U64_1 << 62;
211 static const uint64 PAGE_RESERVED = U64_1 << 61;
212 static const uint64 FLAG_NOPAGE = U64_1 << 20;
213 static const uint64 FLAG_KSM = U64_1 << 21;
214 static const uint64 FLAG_MMAP = U64_1 << 11;
215
216 uint64 pagemap_value;
217 int result = read(pagemap_fd_, &pagemap_value, PAGEMAP_BYTES);
218 if (result != PAGEMAP_BYTES)
219 return false;
220
221 // Check if the page is committed.
222 state->is_committed = (pagemap_value & (PAGE_PRESENT | PAGE_SWAP));
223
224 state->is_present = (pagemap_value & PAGE_PRESENT);
225 state->is_swapped = (pagemap_value & PAGE_SWAP);
226 state->is_shared = false;
227
228 return true;
229 }
230
231 size_t DeepHeapProfile::GetCommittedSize(uint64 address, size_t size) {
232 uint64 page_address = (address / PAGE_SIZE) * PAGE_SIZE;
233 size_t committed_size = 0;
234
235 SeekProcPagemap(address);
236
237 // Check every page on which the allocation resides.
238 while (page_address < address + size) {
239 // Read corresponding physical page.
240 PageState state;
241 if (ReadProcPagemap(&state) == false) {
242 // We can't read the last region (e.g vsyscall).
243 #ifndef NDEBUG
244 RAW_LOG(0, "pagemap read failed @ %#llx %"PRId64" bytes", address, size);
245 #endif
246 return 0;
247 }
248
249 if (state.is_committed) {
250 // Calculate the size of the allocation part in this page.
251 size_t bytes = PAGE_SIZE;
252 if (address + size <= page_address - 1 + PAGE_SIZE)
253 bytes = address + size - page_address;
254 if (page_address < address)
255 bytes -= address - page_address;
256
257 committed_size += bytes;
258 }
259 if (page_address > TOP_ADDRESS - PAGE_SIZE) {
260 break;
261 }
262 page_address += PAGE_SIZE;
263 }
264
265 return committed_size;
266 }
267
268 void DeepHeapProfile::InitRegionStats(RegionStats* stats) {
269 stats->virtual_bytes = 0;
270 stats->committed_bytes = 0;
271 }
272
273 void DeepHeapProfile::RecordRegionStats(uint64 start,
274 uint64 end,
275 RegionStats* stats) {
276 size_t size = static_cast<size_t>(end - start);
277 stats->virtual_bytes += size;
278 stats->committed_bytes += GetCommittedSize(start, size);
279 }
280
281 void DeepHeapProfile::GetGlobalStats() {
282 ProcMapsIterator::Buffer iterator_buffer;
283 ProcMapsIterator it(0, &iterator_buffer);
284 uint64 start, end, offset;
285 int64 inode;
286 char *flags, *filename;
287
288 InitRegionStats(&(stats_.total));
289 InitRegionStats(&(stats_.file_mapped));
290 InitRegionStats(&(stats_.anonymous));
291 InitRegionStats(&(stats_.other));
292
293 while (it.Next(&start, &end, &flags, &offset, &inode, &filename)) {
294 if (strcmp("[vsyscall]", filename) == 0)
295 continue; // Reading pagemap will fail in [vsyscall].
296
297 int64 committed_bytes = stats_.total.committed_bytes;
298 RecordRegionStats(start, end, &(stats_.total));
299 committed_bytes = stats_.total.committed_bytes - committed_bytes;
300
301 if (filename[0] == '/') {
302 RecordRegionStats(start, end, &(stats_.file_mapped));
303 } else if (filename[0] == '\0' || filename[0] == '\n') {
304 RecordRegionStats(start, end, &(stats_.anonymous));
305 } else {
306 RecordRegionStats(start, end, &(stats_.other));
307 }
308 }
309 }
310
311 void DeepHeapProfile::RecordAlloc(const void* pointer,
312 AllocValue* v,
313 DeepHeapProfile* deep_profile) {
314 uint64 alloc_address = reinterpret_cast<uintptr_t>(pointer);
315 size_t committed = deep_profile->GetCommittedSize(alloc_address, v->bytes);
316
317 (deep_profile->GetDeepBucket(v->bucket()))->committed_size += committed;
318 deep_profile->stats_.record_tcmalloc.virtual_bytes += v->bytes;
319 deep_profile->stats_.record_tcmalloc.committed_bytes += committed;
320 }
321
322 void DeepHeapProfile::RecordMMap(const void* pointer,
323 AllocValue* v,
324 DeepHeapProfile* deep_profile) {
325 uint64 alloc_address = reinterpret_cast<uintptr_t>(pointer);
326 size_t committed = deep_profile->GetCommittedSize(alloc_address, v->bytes);
327
328 (deep_profile->GetDeepBucket(v->bucket()))->committed_size += committed;
329 deep_profile->stats_.record_mmap.virtual_bytes += v->bytes;
330 deep_profile->stats_.record_mmap.committed_bytes += committed;
331 }
332
333 void DeepHeapProfile::RecordAllAllocs() {
334 stats_.record_mmap.virtual_bytes = 0;
335 stats_.record_mmap.committed_bytes = 0;
336 stats_.record_tcmalloc.virtual_bytes = 0;
337 stats_.record_tcmalloc.committed_bytes = 0;
338
339 // tcmalloc allocations.
340 heap_profile_->allocation_->Iterate(RecordAlloc, this);
341
342 // mmap allocations.
343 heap_profile_->mmap_allocation_->Iterate(RecordMMap, this);
344 }
345
346 void DeepHeapProfile::WriteMapsToFile(char buf[], int size) {
347 char file_name[100];
348 snprintf(file_name, sizeof(file_name),
349 "%s.%05d.maps", filename_prefix_, getpid());
350
351 RawFD maps_fd = RawOpenForWriting(file_name);
352 RAW_DCHECK(maps_fd != kIllegalRawFD, "");
353
354 int map_length;
355 bool wrote_all;
356 map_length = tcmalloc::FillProcSelfMaps(
357 profiler_buffer_, kProfilerBufferSize, &wrote_all);
358 RAW_DCHECK(wrote_all, "");
359 RAW_DCHECK(map_length <= kProfilerBufferSize, "");
360 RawWrite(maps_fd, profiler_buffer_, map_length);
361 RawClose(maps_fd);
362 }
363
364 int DeepHeapProfile::FillBucketForBucketFile(const DeepBucket* deep_bucket,
365 char buf[], int bufsize) {
366 const Bucket* bucket = deep_bucket->bucket;
367 int printed = snprintf(buf, bufsize, "%05d", deep_bucket->id);
368 if (printed < 0 || printed >= bufsize) return 0;
jar (doing other things) 2012/01/06 03:05:10 nit: put return on next line.
Dai Mikurube (NOT FULLTIME) 2012/01/06 20:11:27 Done.
369 int buflen = printed;
370
371 for (int d = 0; d < bucket->depth; d++) {
372 printed = snprintf(buf + buflen, bufsize - buflen,
373 " 0x%08" PRIxPTR, reinterpret_cast<uintptr_t>(bucket->stack[d]));
374 if (printed < 0 || printed >= bufsize - buflen) return buflen;
jar (doing other things) 2012/01/06 03:05:10 nit: return on next line.
Dai Mikurube (NOT FULLTIME) 2012/01/06 20:11:27 Done.
375 buflen += printed;
jar (doing other things) 2012/01/06 03:05:10 Maybe |buflen| is a better name than bucket_size m
Dai Mikurube (NOT FULLTIME) 2012/01/06 20:11:27 Done.
376 }
377 printed = snprintf(buf + buflen, bufsize - buflen, "\n");
378 if (printed < 0 || printed >= bufsize - buflen) return buflen;
jar (doing other things) 2012/01/06 03:05:10 nit: return on next line.
Dai Mikurube (NOT FULLTIME) 2012/01/06 20:11:27 Done.
379 buflen += printed;
380
381 return buflen;
382 }
383
384 void DeepHeapProfile::WriteBucketsTableToBucketFile(Bucket** table,
385 RawFD bucket_fd) {
386 // We will use the global buffer here.
387 char* buf = profiler_buffer_;
388 int size = kProfilerBufferSize;
389 int buflen = 0;
390
391 for (int i = 0; i < kHashTableSize; i++) {
392 for (Bucket* b = table[i]; b != 0; b = b->next) {
393 DeepBucket* db = GetDeepBucket(b);
394 if (db->is_logged) continue; // Skip the bucket if it is already logged.
395 if (b->alloc_size - b->free_size <= 64) continue; // Skip small buckets.
396
397 buflen += FillBucketForBucketFile(db, buf + buflen, size - buflen);
398 db->is_logged = true;
399
400 // Write to file if buffer 80% full.
401 if (buflen > size * 0.8) {
402 RawWrite(bucket_fd, buf, buflen);
403 buflen = 0;
404 }
405 }
406 }
407
408 RawWrite(bucket_fd, buf, buflen);
409 }
410
411 void DeepHeapProfile::WriteBucketsToBucketFile() {
412 char file_name[100];
413 snprintf(file_name, sizeof(file_name), "%s.%05d.%04d.buckets",
414 filename_prefix_, getpid(), dump_count_);
415 RawFD bucket_fd = RawOpenForWriting(file_name);
416 RAW_DCHECK(bucket_fd != kIllegalRawFD, "");
417
418 WriteBucketsTableToBucketFile(heap_profile_->malloc_table_, bucket_fd);
419 WriteBucketsTableToBucketFile(heap_profile_->mmap_table_, bucket_fd);
420
421 RawClose(bucket_fd);
422 }
423
424 int DeepHeapProfile::UnparseBucket(const DeepBucket& deep_bucket,
425 char* buf, int buflen, int bufsize,
426 const char* extra,
427 Stats* profile_stats) {
428 const Bucket& bucket = *deep_bucket.bucket;
429 if (profile_stats != NULL) {
430 profile_stats->allocs += bucket.allocs;
431 profile_stats->alloc_size += bucket.alloc_size;
432 profile_stats->frees += bucket.frees;
433 profile_stats->free_size += bucket.free_size;
434 }
435
436 int printed = snprintf(buf + buflen, bufsize - buflen,
437 "%10"PRId64" %10"PRId64" %6d %6d @%s %d\n",
438 bucket.alloc_size - bucket.free_size, deep_bucket.committed_size,
439 bucket.allocs, bucket.frees, extra, deep_bucket.id);
440 // If it looks like the snprintf failed, ignore the fact we printed anything.
441 if (printed < 0 || printed >= bufsize - buflen) return buflen;
442 buflen += printed;
443
444 return buflen;
445 }
446
447 int DeepHeapProfile::UnparseRegionStats(const RegionStats* stats,
448 const char* name,
449 char* buf,
450 int buflen,
451 int bufsize) {
452 int printed = snprintf(buf + buflen, bufsize - buflen,
453 "%15s %10ld %10ld\n",
454 name,
455 stats->virtual_bytes,
456 stats->committed_bytes);
457 if (printed < 0 || printed >= bufsize - buflen) return buflen;
jar (doing other things) 2012/01/06 03:05:10 nit: two lines.
Dai Mikurube (NOT FULLTIME) 2012/01/06 20:11:27 Done.
458 buflen += printed;
459
460 return buflen;
461 }
462
463 int DeepHeapProfile::UnparseGlobalStats(char* buf, int buflen, int bufsize) {
464 int printed = snprintf(buf + buflen, bufsize - buflen,
465 "%15s %10s %10s\n", "", "virtual", "committed");
466 if (printed < 0 || printed >= bufsize - buflen) return buflen;
jar (doing other things) 2012/01/06 03:05:10 nit: two lines.
Dai Mikurube (NOT FULLTIME) 2012/01/06 20:11:27 Done.
467 buflen += printed;
468
469 buflen = UnparseRegionStats(
470 &(stats_.total), "total", buf, buflen, bufsize);
471 buflen = UnparseRegionStats(
472 &(stats_.file_mapped), "file mapped", buf, buflen, bufsize);
473 buflen = UnparseRegionStats(
474 &(stats_.anonymous), "anonymous", buf, buflen, bufsize);
475 buflen = UnparseRegionStats(
476 &(stats_.other), "other", buf, buflen, bufsize);
477 buflen = UnparseRegionStats(
478 &(stats_.record_mmap), "mmap", buf, buflen, bufsize);
479 buflen = UnparseRegionStats(
480 &(stats_.record_tcmalloc), "tcmalloc", buf, buflen, bufsize);
481 return buflen;
482 }
483 #else // DEEP_HEAP_PROFILE
484
485 DeepHeapProfile::DeepHeapProfile(HeapProfileTable* heap_profile,
486 const char* prefix)
487 : heap_profile_(heap_profile) {
488 }
489
490 DeepHeapProfile::~DeepHeapProfile() {
491 }
492
493 int DeepHeapProfile::FillOrderedProfile(char buf[], int size) {
494 return heap_profile_->FillOrderedProfile(buf, size);
495 }
496
497 #endif // DEEP_HEAP_PROFILE
OLDNEW
« no previous file with comments | « third_party/tcmalloc/chromium/src/deep-heap-profile.h ('k') | third_party/tcmalloc/chromium/src/heap-profile-table.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698