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

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: Refactored. 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 Heap Profile";
32 static const int kProfileVersion = 1;
33 static const char kGlobalStatsHeader[] = "GLOBAL_STATS:\n";
34 static const char kStacktraceHeader[] = "STACKTRACES:\n";
35 static const char kProcSelfMapsHeader[] = "\nMAPPED_LIBRARIES:\n";
36
37 DeepHeapProfile::DeepHeapProfile(HeapProfileTable* heap_profile,
38 const char* prefix)
39 : heap_profile_(heap_profile),
40 pagemap_fd_(-1),
41 most_recent_pid_(-1),
42 stats_(),
43 dump_count_(0),
44 filename_prefix_(NULL),
45 profiler_buffer_(NULL),
46 bucket_id_(0) {
47 deep_bucket_map_ = new(heap_profile_->alloc_(sizeof(DeepBucketMap)))
48 DeepBucketMap(heap_profile_->alloc_, heap_profile_->dealloc_);
49
50 // Copy filename prefix.
51 const int prefix_length = strlen(prefix);
52 filename_prefix_ =
53 reinterpret_cast<char*>(heap_profile_->alloc_(prefix_length + 1));
54 memcpy(filename_prefix_, prefix, prefix_length);
55 filename_prefix_[prefix_length] = '\0';
56
57 profiler_buffer_ =
58 reinterpret_cast<char*>(heap_profile_->alloc_(kProfilerBufferSize));
59 }
60
61 DeepHeapProfile::~DeepHeapProfile() {
62 heap_profile_->dealloc_(profiler_buffer_);
63 heap_profile_->dealloc_(filename_prefix_);
64 deep_bucket_map_->~DeepBucketMap();
65 heap_profile_->dealloc_(deep_bucket_map_);
66 }
67
68 int DeepHeapProfile::FillOrderedProfile(char buffer[], int buffer_size) {
69 #ifndef NDEBUG
70 int64 starting_cycles = CycleClock::Now();
71 #endif
72 ++dump_count_;
73
74 // Re-open files in /proc/pid/ if the process is newly forked one.
75 if (most_recent_pid_ != getpid()) {
76 most_recent_pid_ = getpid();
77 pagemap_fd_ = OpenProcPagemap();
78
79 // Write maps into a .maps file with using the global buffer.
80 WriteMapsToFile(profiler_buffer_, kProfilerBufferSize, filename_prefix_);
81 }
82
83 // Reset committed sizes of buckets.
84 ResetCommittedSize(heap_profile_->alloc_table_);
85 ResetCommittedSize(heap_profile_->mmap_table_);
86
87 GetGlobalStats(pagemap_fd_, &stats_);
88 size_t anonymous_committed = stats_.anonymous.committed_bytes;
89
90 // Note: Don't allocate any memory from here.
91
92 // Record committed sizes.
93 RecordAllAllocs();
94
95 // Check if committed bytes changed during RecordAllAllocs.
96 GetGlobalStats(pagemap_fd_, &stats_);
97 #ifndef NDEBUG
98 size_t committed_difference =
99 stats_.anonymous.committed_bytes - anonymous_committed;
100 if (committed_difference != 0) {
101 RAW_LOG(0, "Difference in committed size: %ld", committed_difference);
102 }
103 #endif
104
105 HeapProfileTable::Stats stats;
106 memset(&stats, 0, sizeof(stats));
107
108 // Start filling buffer with the ordered profile.
109 int printed = snprintf(buffer, buffer_size,
110 "%s:%d\n", kProfileHeader, kProfileVersion);
111 if (printed < 0 || printed >= buffer_size) {
112 return 0;
113 }
114 int used_in_buffer = printed;
115
116 // Fill buffer with the global stats.
117 printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
118 kGlobalStatsHeader);
119 if (printed < 0 || printed >= buffer_size - used_in_buffer) {
120 return used_in_buffer;
121 }
122 used_in_buffer += printed;
123
124 used_in_buffer = UnparseGlobalStats(buffer, used_in_buffer, buffer_size);
125
126 // Fill buffer with the header for buckets.
127 printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
128 kStacktraceHeader);
129 if (printed < 0 || printed >= buffer_size - used_in_buffer) {
130 return used_in_buffer;
131 }
132 used_in_buffer += printed;
133
134 printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
135 "%10s %10s\n", "virtual", "committed");
136 if (printed < 0 || printed >= buffer_size - used_in_buffer) {
137 return used_in_buffer;
138 }
139 used_in_buffer += printed;
140
141 // Fill buffer with stack trace buckets.
142 used_in_buffer = FillBucketTable(heap_profile_->alloc_table_, buffer,
143 buffer_size, used_in_buffer, &stats);
144 used_in_buffer = FillBucketTable(heap_profile_->mmap_table_, buffer,
145 buffer_size, used_in_buffer, &stats);
146
147 RAW_DCHECK(used_in_buffer < buffer_size, "");
148
149 // Note: Don't allocate any memory until here.
150
151 // Write the bucket listing into a .bucket file.
152 WriteBucketsToBucketFile();
153
154 #ifndef NDEBUG
155 int64 elapsed_cycles = CycleClock::Now() - starting_cycles;
156 double elapsed_seconds = elapsed_cycles / CyclesPerSecond();
157 RAW_LOG(0, "Time spent on DeepProfiler: %.3f sec\n", elapsed_seconds);
158 #endif
159
160 return used_in_buffer;
161 }
162
163 void DeepHeapProfile::RegionStats::Initialize() {
164 virtual_bytes = 0;
165 committed_bytes = 0;
166 }
167
168 void DeepHeapProfile::RegionStats::Record(
169 int pagemap_fd, uint64 first_address, uint64 last_address) {
170 virtual_bytes += static_cast<size_t>(last_address - first_address + 1);
171 committed_bytes += GetCommittedSize(pagemap_fd, first_address, last_address);
172 }
173
174 // static
175 int DeepHeapProfile::OpenProcPagemap() {
176 char filename[100];
177 sprintf(filename, "/proc/%d/pagemap", getpid());
178 int pagemap_fd = open(filename, O_RDONLY);
179 RAW_DCHECK(pagemap_fd != -1, "Failed to open /proc/self/pagemap");
180 return pagemap_fd;
181 }
182
183 // static
184 bool DeepHeapProfile::SeekProcPagemap(int pagemap_fd, uint64 address) {
185 int64 index = (address / PAGE_SIZE) * PAGEMAP_BYTES;
186 int64 offset = lseek64(pagemap_fd, index, SEEK_SET);
187 RAW_DCHECK(offset == index, "");
188 if (offset < 0) {
189 return false;
190 }
191 return true;
192 }
193
194 // static
195 bool DeepHeapProfile::ReadProcPagemap(int pagemap_fd, PageState* state) {
196 static const uint64 U64_1 = 1;
197 static const uint64 PFN_FILTER = (U64_1 << 55) - U64_1;
198 static const uint64 PAGE_PRESENT = U64_1 << 63;
199 static const uint64 PAGE_SWAP = U64_1 << 62;
200 static const uint64 PAGE_RESERVED = U64_1 << 61;
201 static const uint64 FLAG_NOPAGE = U64_1 << 20;
202 static const uint64 FLAG_KSM = U64_1 << 21;
203 static const uint64 FLAG_MMAP = U64_1 << 11;
204
205 uint64 pagemap_value;
206 int result = read(pagemap_fd, &pagemap_value, PAGEMAP_BYTES);
207 if (result != PAGEMAP_BYTES) {
208 return false;
209 }
210
211 // Check if the page is committed.
212 state->is_committed = (pagemap_value & (PAGE_PRESENT | PAGE_SWAP));
213
214 state->is_present = (pagemap_value & PAGE_PRESENT);
215 state->is_swapped = (pagemap_value & PAGE_SWAP);
216 state->is_shared = false;
217
218 return true;
219 }
220
221 // static
222 size_t DeepHeapProfile::GetCommittedSize(
223 int pagemap_fd, uint64 first_address, uint64 last_address) {
224 uint64 page_address = (first_address / PAGE_SIZE) * PAGE_SIZE;
225 size_t committed_size = 0;
226
227 SeekProcPagemap(pagemap_fd, first_address);
228
229 // Check every page on which the allocation resides.
230 while (page_address <= last_address) {
231 // Read corresponding physical page.
232 PageState state;
233 if (ReadProcPagemap(pagemap_fd, &state) == false) {
234 // We can't read the last region (e.g vsyscall).
235 #ifndef NDEBUG
236 RAW_LOG(0, "pagemap read failed @ %#llx %"PRId64" bytes",
237 first_address, last_address - first_address + 1);
238 #endif
239 return 0;
240 }
241
242 if (state.is_committed) {
243 // Calculate the size of the allocation part in this page.
244 size_t bytes = PAGE_SIZE;
245
246 // If looking at the last page in a given region.
247 if (last_address <= page_address - 1 + PAGE_SIZE) {
248 bytes = last_address - page_address + 1;
249 }
250
251 // If looking at the first page in a given region.
252 if (page_address < first_address) {
253 bytes -= first_address - page_address;
254 }
255
256 committed_size += bytes;
257 }
258 if (page_address > TOP_ADDRESS - PAGE_SIZE) {
259 break;
260 }
261 page_address += PAGE_SIZE;
262 }
263
264 return committed_size;
265 }
266
267 // static
268 void DeepHeapProfile::WriteMapsToFile(char buffer[], int buffer_size,
269 char* filename_prefix) {
270 char filename[100];
271 snprintf(filename, sizeof(filename),
272 "%s.%05d.maps", filename_prefix, getpid());
273
274 RawFD maps_fd = RawOpenForWriting(filename);
275 RAW_DCHECK(maps_fd != kIllegalRawFD, "");
276
277 int map_length;
278 bool wrote_all;
279 map_length = tcmalloc::FillProcSelfMaps(buffer, buffer_size, &wrote_all);
280 RAW_DCHECK(wrote_all, "");
281 RAW_DCHECK(map_length <= buffer_size, "");
282 RawWrite(maps_fd, buffer, map_length);
283 RawClose(maps_fd);
284 }
285
286 // static
287 void DeepHeapProfile::GetGlobalStats(int pagemap_fd, GlobalStats* stats) {
288 ProcMapsIterator::Buffer iterator_buffer;
289 ProcMapsIterator it(0, &iterator_buffer);
290 uint64 first_address, last_address, offset;
291 int64 inode;
292 char *flags, *filename;
293
294 stats->total.Initialize();
295 stats->file_mapped.Initialize();
296 stats->anonymous.Initialize();
297 stats->other.Initialize();
298
299 while (it.Next(&first_address, &last_address,
300 &flags, &offset, &inode, &filename)) {
301 // 'last_address' should be the last inclusive address of the region.
302 last_address -= 1;
303 if (strcmp("[vsyscall]", filename) == 0) {
304 continue; // Reading pagemap will fail in [vsyscall].
305 }
306
307 int64 committed_bytes = stats->total.committed_bytes;
308 stats->total.Record(pagemap_fd, first_address, last_address);
309 committed_bytes = stats->total.committed_bytes - committed_bytes;
310
311 if (filename[0] == '/') {
312 stats->file_mapped.Record(pagemap_fd, first_address, last_address);
313 } else if (filename[0] == '\0' || filename[0] == '\n') {
314 stats->anonymous.Record(pagemap_fd, first_address, last_address);
315 } else {
316 stats->other.Record(pagemap_fd, first_address, last_address);
317 }
318 }
319 }
320
321 DeepHeapProfile::DeepBucket*
322 DeepHeapProfile::GetDeepBucket(Bucket* bucket) {
323 DeepBucket* found = deep_bucket_map_->FindMutable(bucket);
324 if (found == NULL) {
325 DeepBucket created;
326 created.bucket = bucket;
327 created.committed_size = 0;
328 created.id = (bucket_id_++);
329 created.is_logged = false;
330 deep_bucket_map_->Insert(bucket, created);
331 return deep_bucket_map_->FindMutable(bucket);
332 } else {
333 return found;
334 }
335 }
336
337 void DeepHeapProfile::ResetCommittedSize(Bucket** bucket_table) {
338 for (int i = 0; i < kHashTableSize; i++) {
339 for (Bucket* b = bucket_table[i]; b != 0; b = b->next) {
340 DeepBucket* db = GetDeepBucket(b);
341 db->committed_size = 0;
342 }
343 }
344 }
345
346 int DeepHeapProfile::FillBucketTable(Bucket** bucket_table,
347 char buffer[],
348 int buffer_size,
349 int used_in_buffer,
350 HeapProfileTable::Stats* stats) {
351 for (int i = 0; i < kHashTableSize; i++) {
352 for (Bucket* b = bucket_table[i]; b != 0; b = b->next) {
353 if (b->alloc_size - b->free_size == 0) {
354 continue; // Skip empty buckets.
355 }
356 const DeepBucket& db = *GetDeepBucket(b);
357 used_in_buffer =
358 UnparseBucket(db, buffer, used_in_buffer, buffer_size, "", stats);
359 }
360 }
361 return used_in_buffer;
362 }
363
364 void DeepHeapProfile::RecordAlloc(const void* pointer,
365 AllocValue* alloc_value,
366 DeepHeapProfile* deep_profile) {
367 uint64 address = reinterpret_cast<uintptr_t>(pointer);
368 size_t committed = GetCommittedSize(deep_profile->pagemap_fd_,
369 address, address + alloc_value->bytes - 1);
370
371 DeepBucket* db = deep_profile->GetDeepBucket(alloc_value->bucket());
372 db->committed_size += committed;
373 deep_profile->stats_.record_malloc.virtual_bytes += alloc_value->bytes;
374 deep_profile->stats_.record_malloc.committed_bytes += committed;
375 }
376
377 void DeepHeapProfile::RecordMMap(const void* pointer,
378 AllocValue* alloc_value,
379 DeepHeapProfile* deep_profile) {
380 uint64 address = reinterpret_cast<uintptr_t>(pointer);
381 size_t committed = GetCommittedSize(deep_profile->pagemap_fd_,
382 address, address + alloc_value->bytes - 1);
383
384 DeepBucket* db = deep_profile->GetDeepBucket(alloc_value->bucket());
385 db->committed_size += committed;
386 deep_profile->stats_.record_mmap.virtual_bytes += alloc_value->bytes;
387 deep_profile->stats_.record_mmap.committed_bytes += committed;
388 }
389
390 void DeepHeapProfile::RecordAllAllocs() {
391 stats_.record_mmap.virtual_bytes = 0;
392 stats_.record_mmap.committed_bytes = 0;
393 stats_.record_malloc.virtual_bytes = 0;
394 stats_.record_malloc.committed_bytes = 0;
395
396 // malloc allocations.
397 heap_profile_->alloc_address_map_->Iterate(RecordAlloc, this);
398
399 // mmap allocations.
400 heap_profile_->mmap_address_map_->Iterate(RecordMMap, this);
401 }
402
403 int DeepHeapProfile::FillBucketForBucketFile(const DeepBucket* deep_bucket,
404 char buffer[],
405 int buffer_size) {
406 const Bucket* bucket = deep_bucket->bucket;
407 int printed = snprintf(buffer, buffer_size, "%05d", deep_bucket->id);
408 if (printed < 0 || printed >= buffer_size) {
409 return 0;
410 }
411 int used_in_buffer = printed;
412
413 for (int d = 0; d < bucket->depth; d++) {
414 printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
415 " 0x%08" PRIxPTR,
416 reinterpret_cast<uintptr_t>(bucket->stack[d]));
417 if (printed < 0 || printed >= buffer_size - used_in_buffer) {
418 return used_in_buffer;
419 }
420 used_in_buffer += printed;
421 }
422 printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
423 "\n");
424 if (printed < 0 || printed >= buffer_size - used_in_buffer) {
425 return used_in_buffer;
426 }
427 used_in_buffer += printed;
428
429 return used_in_buffer;
430 }
431
432 void DeepHeapProfile::WriteBucketsTableToBucketFile(Bucket** bucket_table,
433 RawFD bucket_fd) {
434 // We will use the global buffer here.
435 char* buffer = profiler_buffer_;
436 int buffer_size = kProfilerBufferSize;
437 int used_in_buffer = 0;
438
439 for (int i = 0; i < kHashTableSize; i++) {
440 for (Bucket* b = bucket_table[i]; b != 0; b = b->next) {
441 DeepBucket* db = GetDeepBucket(b);
442 if (db->is_logged) {
443 continue; // Skip the bucket if it is already logged.
444 }
445 if (b->alloc_size - b->free_size <= 64) {
446 continue; // Skip small buckets.
447 }
448
449 used_in_buffer += FillBucketForBucketFile(
450 db, buffer + used_in_buffer, buffer_size - used_in_buffer);
451 db->is_logged = true;
452
453 // Write to file if buffer 80% full.
454 if (used_in_buffer > buffer_size * 0.8) {
455 RawWrite(bucket_fd, buffer, used_in_buffer);
456 used_in_buffer = 0;
457 }
458 }
459 }
460
461 RawWrite(bucket_fd, buffer, used_in_buffer);
462 }
463
464 void DeepHeapProfile::WriteBucketsToBucketFile() {
465 char filename[100];
466 snprintf(filename, sizeof(filename),
467 "%s.%05d.%04d.buckets", filename_prefix_, getpid(), dump_count_);
468 RawFD bucket_fd = RawOpenForWriting(filename);
469 RAW_DCHECK(bucket_fd != kIllegalRawFD, "");
470
471 WriteBucketsTableToBucketFile(heap_profile_->alloc_table_, bucket_fd);
472 WriteBucketsTableToBucketFile(heap_profile_->mmap_table_, bucket_fd);
473
474 RawClose(bucket_fd);
475 }
476
477 int DeepHeapProfile::UnparseBucket(const DeepBucket& deep_bucket,
478 char* buffer,
479 int used_in_buffer,
480 int buffer_size,
481 const char* extra,
482 Stats* profile_stats) {
483 const Bucket& bucket = *deep_bucket.bucket;
484 if (profile_stats != NULL) {
485 profile_stats->allocs += bucket.allocs;
486 profile_stats->alloc_size += bucket.alloc_size;
487 profile_stats->frees += bucket.frees;
488 profile_stats->free_size += bucket.free_size;
489 }
490
491 int printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
492 "%10"PRId64" %10"PRId64" %6d %6d @%s %d\n",
493 bucket.alloc_size - bucket.free_size,
494 deep_bucket.committed_size,
495 bucket.allocs, bucket.frees, extra, deep_bucket.id);
496 // If it looks like the snprintf failed, ignore the fact we printed anything.
497 if (printed < 0 || printed >= buffer_size - used_in_buffer) {
498 return used_in_buffer;
499 }
500 used_in_buffer += printed;
501
502 return used_in_buffer;
503 }
504
505 int DeepHeapProfile::UnparseRegionStats(const RegionStats* stats,
506 const char* name,
507 char* buffer,
508 int used_in_buffer,
509 int buffer_size) {
510 int printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
511 "%15s %10ld %10ld\n",
512 name, stats->virtual_bytes, stats->committed_bytes);
513 if (printed < 0 || printed >= buffer_size - used_in_buffer) {
514 return used_in_buffer;
515 }
516 used_in_buffer += printed;
517
518 return used_in_buffer;
519 }
520
521 int DeepHeapProfile::UnparseGlobalStats(char* buffer,
522 int used_in_buffer,
523 int buffer_size) {
524 int printed = snprintf(buffer + used_in_buffer, buffer_size - used_in_buffer,
525 "%15s %10s %10s\n", "", "virtual", "committed");
526 if (printed < 0 || printed >= buffer_size - used_in_buffer) {
527 return used_in_buffer;
528 }
529 used_in_buffer += printed;
530
531 used_in_buffer = UnparseRegionStats(&(stats_.total), "total",
532 buffer, used_in_buffer, buffer_size);
533 used_in_buffer = UnparseRegionStats(&(stats_.file_mapped), "file mapped",
534 buffer, used_in_buffer, buffer_size);
535 used_in_buffer = UnparseRegionStats(&(stats_.anonymous), "anonymous",
536 buffer, used_in_buffer, buffer_size);
537 used_in_buffer = UnparseRegionStats(&(stats_.other), "other",
538 buffer, used_in_buffer, buffer_size);
539 used_in_buffer = UnparseRegionStats(&(stats_.record_mmap), "mmap",
540 buffer, used_in_buffer, buffer_size);
541 used_in_buffer = UnparseRegionStats(&(stats_.record_malloc), "tcmalloc",
542 buffer, used_in_buffer, buffer_size);
543 return used_in_buffer;
544 }
545 #else // DEEP_HEAP_PROFILE
546
547 DeepHeapProfile::DeepHeapProfile(HeapProfileTable* heap_profile,
548 const char* prefix)
549 : heap_profile_(heap_profile) {
550 }
551
552 DeepHeapProfile::~DeepHeapProfile() {
553 }
554
555 int DeepHeapProfile::FillOrderedProfile(char buffer[], int buffer_size) {
556 return heap_profile_->FillOrderedProfile(buffer, buffer_size);
557 }
558
559 #endif // DEEP_HEAP_PROFILE
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698