OLD | NEW |
(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 #include <config.h> |
| 6 |
| 7 #include "deep-heap-profile.h" |
| 8 |
| 9 #ifdef DEEP_HEAP_PROFILE |
| 10 #include <fcntl.h> |
| 11 #include <sys/stat.h> |
| 12 #include <sys/types.h> |
| 13 #ifdef HAVE_UNISTD_H |
| 14 #include <unistd.h> // for getpid() |
| 15 #endif |
| 16 |
| 17 #include "base/cycleclock.h" |
| 18 #include "base/sysinfo.h" |
| 19 |
| 20 static const int kProfilerBufferSize = 1 << 20; |
| 21 static const int kHashTableSize = 179999; // The same as heap-profile-table.cc |
| 22 |
| 23 static const int PAGE_SIZE = 4096; |
| 24 static const int PAGEMAP_BYTES = 8; |
| 25 |
| 26 // header of the dumped heap profile |
| 27 static const char kProfileHeader[] = "Deep Memory Profile\n"; |
| 28 static const char kGlobalStatsHeader[] = "GLOBAL_STATS:\n"; |
| 29 static const char kStacktraceHeader[] = "STACKTRACES:\n"; |
| 30 static const char kProcSelfMapsHeader[] = "\nMAPPED_LIBRARIES:\n"; |
| 31 |
| 32 DeepHeapProfile::DeepHeapProfile(HeapProfileTable* heap_profile, |
| 33 const char* prefix) |
| 34 : heap_profile_(heap_profile), |
| 35 pagemap_fd_(-1), |
| 36 most_recent_pid_(-1), |
| 37 stats_(), |
| 38 page_map_(NULL), |
| 39 dump_count_(0), |
| 40 filename_prefix_(NULL), |
| 41 profiler_buffer_(NULL), |
| 42 bucket_id_(0) { |
| 43 page_map_ = new(heap_profile_->alloc_(sizeof(PageStateMap))) |
| 44 PageStateMap(heap_profile_->alloc_, heap_profile_->dealloc_); |
| 45 |
| 46 deep_bucket_map_ = new(heap_profile_->alloc_(sizeof(DeepBucketMap))) |
| 47 DeepBucketMap(heap_profile_->alloc_, heap_profile_->dealloc_); |
| 48 |
| 49 // Copy filename prefix |
| 50 RAW_DCHECK(filename_prefix_ == NULL, ""); |
| 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 page_map_->~PageStateMap(); |
| 67 heap_profile_->dealloc_(page_map_); |
| 68 } |
| 69 |
| 70 int DeepHeapProfile::FillOrderedProfile(char buf[], int size) { |
| 71 int64 start_time = CycleClock::Now(); |
| 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 OpenProcPagemap(); |
| 78 |
| 79 // Write maps into a .maps file with using the global buffer. |
| 80 WriteMapsToFile(profiler_buffer_, kProfilerBufferSize); |
| 81 } |
| 82 |
| 83 // Reset committed sizes of buckets. |
| 84 ResetCommittedSize(heap_profile_->malloc_table_); |
| 85 ResetCommittedSize(heap_profile_->mmap_table_); |
| 86 |
| 87 GetGlobalStats(); |
| 88 uint64 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(); |
| 97 uint64 committed_difference = |
| 98 stats_.anonymous.committed_bytes - anonymous_committed; |
| 99 if (committed_difference != 0) |
| 100 RAW_LOG(0, "Difference in committed size: %"PRId64"", committed_difference); |
| 101 |
| 102 HeapProfileTable::Stats stats; |
| 103 memset(&stats, 0, sizeof(stats)); |
| 104 |
| 105 // Start filling buf with the ordered profile. |
| 106 int bucket_length = snprintf(buf, size, kProfileHeader); |
| 107 if (bucket_length < 0 || bucket_length >= size) return 0; |
| 108 |
| 109 // Fill buf with the global stats. |
| 110 bucket_length += |
| 111 snprintf(buf + bucket_length, size - bucket_length, kGlobalStatsHeader); |
| 112 bucket_length = UnparseGlobalStats(buf, bucket_length, size); |
| 113 |
| 114 // Fill buf with the header for buckets. |
| 115 bucket_length += |
| 116 snprintf(buf + bucket_length, size - bucket_length, kStacktraceHeader); |
| 117 bucket_length += snprintf(buf + bucket_length, size - bucket_length, |
| 118 "%10s %10s\n", "virtual", "committed"); |
| 119 |
| 120 // Fill buf with stack trace buckets. |
| 121 bucket_length = FillBucketTable(heap_profile_->malloc_table_, |
| 122 buf, size, bucket_length, &stats); |
| 123 bucket_length = FillBucketTable(heap_profile_->mmap_table_, |
| 124 buf, size, bucket_length, &stats); |
| 125 |
| 126 RAW_DCHECK(bucket_length < size, ""); |
| 127 |
| 128 // Note: Don't allocate any memory until here. |
| 129 |
| 130 // Write the bucket listing into a .bucket file. |
| 131 WriteBucketsToBucketFile(); |
| 132 |
| 133 int64 dt = CycleClock::Now() - start_time; |
| 134 double dtf = dt / CyclesPerSecond(); |
| 135 RAW_LOG(0, "Time spent on DeepProfiler: %.3f sec\n", dtf); |
| 136 |
| 137 return bucket_length; |
| 138 } |
| 139 |
| 140 DeepHeapProfile::DeepBucket* |
| 141 DeepHeapProfile::GetDeepBucket(Bucket* bucket) { |
| 142 DeepBucket* found = deep_bucket_map_->FindMutable(bucket); |
| 143 if (found == NULL) { |
| 144 DeepBucket created; |
| 145 created.bucket = bucket; |
| 146 created.committed_size = 0; |
| 147 created.id = (bucket_id_++); |
| 148 created.is_logged = false; |
| 149 deep_bucket_map_->Insert(bucket, created); |
| 150 return deep_bucket_map_->FindMutable(bucket); |
| 151 } else { |
| 152 return found; |
| 153 } |
| 154 } |
| 155 |
| 156 void DeepHeapProfile::ResetCommittedSize(Bucket** table) { |
| 157 for (int i = 0; i < kHashTableSize; i++) { |
| 158 for (Bucket* b = table[i]; b != 0; b = b->next) { |
| 159 DeepBucket* db = GetDeepBucket(b); |
| 160 db->committed_size = 0; |
| 161 } |
| 162 } |
| 163 } |
| 164 |
| 165 int DeepHeapProfile::FillBucketTable(Bucket** table, |
| 166 char buf[], int size, int bucket_length, |
| 167 HeapProfileTable::Stats* stats) { |
| 168 for (int i = 0; i < kHashTableSize; i++) { |
| 169 for (Bucket* b = table[i]; b != 0; b = b->next) { |
| 170 if (b->alloc_size - b->free_size == 0) |
| 171 continue; // Skip empty buckets |
| 172 const DeepBucket& db = *GetDeepBucket(b); |
| 173 bucket_length = UnparseBucket(db, buf, bucket_length, size, "", stats); |
| 174 } |
| 175 } |
| 176 return bucket_length; |
| 177 } |
| 178 |
| 179 // This function need to be called after each fork. |
| 180 void DeepHeapProfile::OpenProcPagemap() { |
| 181 char filename[100]; |
| 182 sprintf(filename, "/proc/%d/pagemap", getpid()); |
| 183 pagemap_fd_ = open(filename, O_RDONLY); |
| 184 RAW_DCHECK(pagemap_fd_ != -1, "Failed to open /proc/self/pagemap"); |
| 185 } |
| 186 |
| 187 bool DeepHeapProfile::SeekProcPagemap(uint64 address) { |
| 188 uint64 index = (address / PAGE_SIZE) * PAGEMAP_BYTES; |
| 189 uint64 o = lseek64(pagemap_fd_, index, SEEK_SET); |
| 190 RAW_DCHECK(o == index, ""); |
| 191 return true; |
| 192 } |
| 193 |
| 194 bool DeepHeapProfile::ReadProcPagemap(PageState* state) { |
| 195 static const uint64 U64_1 = 1; |
| 196 static const uint64 PFN_FILTER = (U64_1 << 55) - U64_1; |
| 197 static const uint64 PAGE_PRESENT = U64_1 << 63; |
| 198 static const uint64 PAGE_SWAP = U64_1 << 62; |
| 199 static const uint64 PAGE_RESERVED = U64_1 << 61; |
| 200 static const uint64 FLAG_NOPAGE = U64_1 << 20; |
| 201 static const uint64 FLAG_KSM = U64_1 << 21; |
| 202 static const uint64 FLAG_MMAP = U64_1 << 11; |
| 203 |
| 204 uint64 pagemap_value; |
| 205 int result = read(pagemap_fd_, &pagemap_value, PAGEMAP_BYTES); |
| 206 if (result != PAGEMAP_BYTES) |
| 207 return false; |
| 208 |
| 209 // Check if the page is committed. |
| 210 state->is_committed = (pagemap_value & (PAGE_PRESENT | PAGE_SWAP)); |
| 211 |
| 212 state->is_present = (pagemap_value & PAGE_PRESENT); |
| 213 state->is_swapped = (pagemap_value & PAGE_SWAP); |
| 214 state->is_shared = false; |
| 215 |
| 216 return true; |
| 217 } |
| 218 |
| 219 uint64 DeepHeapProfile::GetCommittedSize(uint64 address, uint64 size) { |
| 220 uint64 page_address = (address / PAGE_SIZE) * PAGE_SIZE; |
| 221 uint64 committed_size = 0; |
| 222 |
| 223 SeekProcPagemap(address); |
| 224 |
| 225 // Check every pages on which the allocation reside. |
| 226 while (page_address < address + size) { |
| 227 // Read corresponding physical page. |
| 228 PageState state; |
| 229 if (ReadProcPagemap(&state) == false) { |
| 230 // We can't read the last region (e.g vsyscall). |
| 231 RAW_LOG(0, "pagemap read failed @ %#llx %"PRId64" bytes", address, size); |
| 232 return 0; |
| 233 } |
| 234 |
| 235 if (state.is_committed) { |
| 236 // Calculate the size of the allocation part in this page. |
| 237 uint64 bytes = PAGE_SIZE; |
| 238 if (page_address < address) |
| 239 bytes -= address - page_address; |
| 240 if (address + size < page_address + PAGE_SIZE) |
| 241 bytes -= PAGE_SIZE - (address + size - page_address); |
| 242 |
| 243 committed_size += bytes; |
| 244 } |
| 245 page_address += PAGE_SIZE; |
| 246 } |
| 247 |
| 248 return committed_size; |
| 249 } |
| 250 |
| 251 void DeepHeapProfile::InitRegionStats(RegionStats* stats) { |
| 252 stats->virtual_bytes = 0; |
| 253 stats->committed_bytes = 0; |
| 254 } |
| 255 |
| 256 void DeepHeapProfile::RecordRegionStats(uint64 start, |
| 257 uint64 end, |
| 258 RegionStats* stats) { |
| 259 stats->virtual_bytes += end - start; |
| 260 stats->committed_bytes += GetCommittedSize(start, end - start); |
| 261 } |
| 262 |
| 263 void DeepHeapProfile::GetGlobalStats() { |
| 264 ProcMapsIterator::Buffer iterator_buffer; |
| 265 ProcMapsIterator it(0, &iterator_buffer); |
| 266 uint64 start, end, offset; |
| 267 int64 inode; |
| 268 char *flags, *filename; |
| 269 |
| 270 InitRegionStats(&(stats_.total)); |
| 271 InitRegionStats(&(stats_.file_mapped)); |
| 272 InitRegionStats(&(stats_.anonymous)); |
| 273 InitRegionStats(&(stats_.other)); |
| 274 |
| 275 while (it.Next(&start, &end, &flags, &offset, &inode, &filename)) { |
| 276 if (strcmp("[vsyscall]", filename) == 0) |
| 277 continue; // pagemap read fails in this region |
| 278 |
| 279 int64 committed_bytes = stats_.total.committed_bytes; |
| 280 RecordRegionStats(start, end, &(stats_.total)); |
| 281 committed_bytes = stats_.total.committed_bytes - committed_bytes; |
| 282 |
| 283 if (filename[0] == '/') { |
| 284 RecordRegionStats(start, end, &(stats_.file_mapped)); |
| 285 } else if (filename[0] == '\0' || |
| 286 filename[0] == '\n' || |
| 287 filename[0] == EOF) { |
| 288 RecordRegionStats(start, end, &(stats_.anonymous)); |
| 289 } else { |
| 290 RecordRegionStats(start, end, &(stats_.other)); |
| 291 } |
| 292 } |
| 293 } |
| 294 |
| 295 void DeepHeapProfile::RecordAlloc(const void* pointer, |
| 296 AllocValue* v, |
| 297 DeepHeapProfile* deep_profile) { |
| 298 uint64 alloc_address = (uint64) pointer; |
| 299 uint64 committed = deep_profile->GetCommittedSize(alloc_address, v->bytes); |
| 300 |
| 301 (deep_profile->GetDeepBucket(v->bucket()))->committed_size += committed; |
| 302 deep_profile->stats_.record_tcmalloc.virtual_bytes += v->bytes; |
| 303 deep_profile->stats_.record_tcmalloc.committed_bytes += committed; |
| 304 } |
| 305 |
| 306 void DeepHeapProfile::RecordMMap(const void* pointer, |
| 307 AllocValue* v, |
| 308 DeepHeapProfile* deep_profile) { |
| 309 uint64 alloc_address = (uint64) pointer; |
| 310 uint64 committed = deep_profile->GetCommittedSize(alloc_address, v->bytes); |
| 311 |
| 312 (deep_profile->GetDeepBucket(v->bucket()))->committed_size += committed; |
| 313 deep_profile->stats_.record_mmap.virtual_bytes += v->bytes; |
| 314 deep_profile->stats_.record_mmap.committed_bytes += committed; |
| 315 } |
| 316 |
| 317 void DeepHeapProfile::RecordAllAllocs() { |
| 318 stats_.record_mmap.virtual_bytes = 0; |
| 319 stats_.record_mmap.committed_bytes = 0; |
| 320 stats_.record_tcmalloc.virtual_bytes = 0; |
| 321 stats_.record_tcmalloc.committed_bytes = 0; |
| 322 |
| 323 // Tcmalloc allocs |
| 324 heap_profile_->allocation_->Iterate(RecordAlloc, this); |
| 325 |
| 326 // Mmap allocs |
| 327 heap_profile_->mmap_allocation_->Iterate(RecordMMap, this); |
| 328 } |
| 329 |
| 330 void DeepHeapProfile::WriteMapsToFile(char buf[], int size) { |
| 331 char file_name[100]; |
| 332 snprintf(file_name, sizeof(file_name), |
| 333 "%s.%05d.maps", filename_prefix_, getpid()); |
| 334 |
| 335 RawFD maps_fd = RawOpenForWriting(file_name); |
| 336 RAW_DCHECK(maps_fd != kIllegalRawFD, ""); |
| 337 |
| 338 int map_length; |
| 339 bool wrote_all; |
| 340 map_length = tcmalloc::FillProcSelfMaps( |
| 341 profiler_buffer_, kProfilerBufferSize, &wrote_all); |
| 342 RAW_DCHECK(wrote_all, ""); |
| 343 RAW_DCHECK(map_length <= kProfilerBufferSize, ""); |
| 344 RawWrite(maps_fd, profiler_buffer_, map_length); |
| 345 RawClose(maps_fd); |
| 346 } |
| 347 |
| 348 int DeepHeapProfile::FillBucketForBucketFile(const DeepBucket* deep_bucket, |
| 349 char buf[], int bufsize) { |
| 350 const Bucket* bucket = deep_bucket->bucket; |
| 351 int buflen = 0; |
| 352 buflen += snprintf(buf + buflen, bufsize - buflen, "%05d", |
| 353 deep_bucket->id); |
| 354 for (int d = 0; d < bucket->depth; d++) { |
| 355 buflen += snprintf(buf + buflen, bufsize - buflen, " 0x%08" PRIxPTR, |
| 356 reinterpret_cast<uintptr_t>(bucket->stack[d])); |
| 357 } |
| 358 buflen += snprintf(buf + buflen, bufsize - buflen, "\n"); |
| 359 return buflen; |
| 360 } |
| 361 |
| 362 void DeepHeapProfile::WriteBucketsTableToBucketFile(Bucket** table, |
| 363 RawFD bucket_fd) { |
| 364 // We will use the global buffer here. |
| 365 char* buf = profiler_buffer_; |
| 366 int size = kProfilerBufferSize; |
| 367 int buflen = 0; |
| 368 |
| 369 for (int i = 0; i < kHashTableSize; i++) { |
| 370 for (Bucket* b = table[i]; b != 0; b = b->next) { |
| 371 DeepBucket* db = GetDeepBucket(b); |
| 372 if (db->is_logged) continue; // Skip the bucket if it is already logged |
| 373 if (b->alloc_size - b->free_size <= 64) continue; // Skip small buckets |
| 374 |
| 375 buflen += FillBucketForBucketFile(db, buf + buflen, size - buflen); |
| 376 db->is_logged = true; |
| 377 |
| 378 // Write to file if buffer 80% full. |
| 379 if (buflen > size * 0.8) { |
| 380 RawWrite(bucket_fd, buf, buflen); |
| 381 buflen = 0; |
| 382 } |
| 383 } |
| 384 } |
| 385 |
| 386 RawWrite(bucket_fd, buf, buflen); |
| 387 } |
| 388 |
| 389 void DeepHeapProfile::WriteBucketsToBucketFile() { |
| 390 char file_name[100]; |
| 391 snprintf(file_name, sizeof(file_name), "%s.%05d.%04d.buckets", |
| 392 filename_prefix_, getpid(), dump_count_); |
| 393 RawFD bucket_fd = RawOpenForWriting(file_name); |
| 394 RAW_DCHECK(bucket_fd != kIllegalRawFD, ""); |
| 395 |
| 396 WriteBucketsTableToBucketFile(heap_profile_->malloc_table_, bucket_fd); |
| 397 WriteBucketsTableToBucketFile(heap_profile_->mmap_table_, bucket_fd); |
| 398 |
| 399 RawClose(bucket_fd); |
| 400 } |
| 401 |
| 402 int DeepHeapProfile::UnparseBucket(const DeepBucket& deep_bucket, |
| 403 char* buf, int buflen, int bufsize, |
| 404 const char* extra, |
| 405 Stats* profile_stats) { |
| 406 const Bucket& bucket = *deep_bucket.bucket; |
| 407 if (profile_stats != NULL) { |
| 408 profile_stats->allocs += bucket.allocs; |
| 409 profile_stats->alloc_size += bucket.alloc_size; |
| 410 profile_stats->frees += bucket.frees; |
| 411 profile_stats->free_size += bucket.free_size; |
| 412 } |
| 413 |
| 414 int printed = snprintf(buf + buflen, bufsize - buflen, |
| 415 "%10"PRId64" %10"PRId64" %6d %6d @%s %d\n", |
| 416 bucket.alloc_size - bucket.free_size, deep_bucket.committed_size, |
| 417 bucket.allocs, bucket.frees, extra, deep_bucket.id); |
| 418 // If it looks like the snprintf failed, ignore the fact we printed anything. |
| 419 if (printed < 0 || printed >= bufsize - buflen) return buflen; |
| 420 buflen += printed; |
| 421 |
| 422 return buflen; |
| 423 } |
| 424 |
| 425 int DeepHeapProfile::UnparseRegionStats(const RegionStats* stats, |
| 426 const char* name, |
| 427 char* buf, |
| 428 int buflen, |
| 429 int bufsize) { |
| 430 int printed = snprintf(buf + buflen, bufsize - buflen, |
| 431 "%15s %10"PRId64" %10"PRId64"\n", |
| 432 name, |
| 433 stats->virtual_bytes, |
| 434 stats->committed_bytes); |
| 435 |
| 436 return buflen + printed; |
| 437 } |
| 438 |
| 439 int DeepHeapProfile::UnparseGlobalStats(char* buf, int buflen, int bufsize) { |
| 440 buflen += snprintf(buf + buflen, bufsize - buflen, |
| 441 "%15s %10s %10s\n", |
| 442 "", "virtual", "committed"); |
| 443 |
| 444 buflen = UnparseRegionStats( |
| 445 &(stats_.total), "total", buf, buflen, bufsize); |
| 446 buflen = UnparseRegionStats( |
| 447 &(stats_.file_mapped), "file mapped", buf, buflen, bufsize); |
| 448 buflen = UnparseRegionStats( |
| 449 &(stats_.anonymous), "anonymous", buf, buflen, bufsize); |
| 450 buflen = UnparseRegionStats( |
| 451 &(stats_.other), "other", buf, buflen, bufsize); |
| 452 buflen = UnparseRegionStats( |
| 453 &(stats_.record_mmap), "mmap", buf, buflen, bufsize); |
| 454 buflen = UnparseRegionStats( |
| 455 &(stats_.record_tcmalloc), "tcmalloc", buf, buflen, bufsize); |
| 456 return buflen; |
| 457 } |
| 458 #else // DEEP_HEAP_PROFILE |
| 459 |
| 460 DeepHeapProfile::DeepHeapProfile(HeapProfileTable* heap_profile, |
| 461 const char* prefix) |
| 462 : heap_profile_(heap_profile) { |
| 463 } |
| 464 |
| 465 DeepHeapProfile::~DeepHeapProfile() { |
| 466 } |
| 467 |
| 468 int DeepHeapProfile::FillOrderedProfile(char buf[], int size) { |
| 469 return heap_profile_->FillOrderedProfile(buf, size); |
| 470 } |
| 471 |
| 472 #endif // DEEP_HEAP_PROFILE |
OLD | NEW |