1 /*
   2  * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 #include "precompiled.hpp"
  25 
  26 #include "memory/allocation.hpp"
  27 #include "services/mallocTracker.hpp"
  28 #include "services/memReporter.hpp"
  29 #include "services/virtualMemoryTracker.hpp"
  30 #include "utilities/globalDefinitions.hpp"
  31 
  32 size_t MemReporterBase::reserved_total(const MallocMemory* malloc, const VirtualMemory* vm) const {
  33   return malloc->malloc_size() + malloc->arena_size() + vm->reserved();
  34 }
  35 
  36 size_t MemReporterBase::committed_total(const MallocMemory* malloc, const VirtualMemory* vm) const {
  37   return malloc->malloc_size() + malloc->arena_size() + vm->committed();
  38 }
  39 
  40 void MemReporterBase::print_total(size_t reserved, size_t committed) const {
  41   const char* scale = current_scale();
  42   output()->print("reserved=" SIZE_FORMAT "%s, committed=" SIZE_FORMAT "%s",
  43     amount_in_current_scale(reserved), scale, amount_in_current_scale(committed), scale);
  44 }
  45 
  46 void MemReporterBase::print_malloc(size_t amount, size_t count, MEMFLAGS flag) const {
  47   const char* scale = current_scale();
  48   outputStream* out = output();
  49   if (flag != mtNone) {
  50     out->print("(malloc=" SIZE_FORMAT "%s type=%s",
  51       amount_in_current_scale(amount), scale, NMTUtil::flag_to_name(flag));
  52   } else {
  53     out->print("(malloc=" SIZE_FORMAT "%s",
  54       amount_in_current_scale(amount), scale);
  55   }
  56 
  57   if (count > 0) {
  58     out->print(" #" SIZE_FORMAT "", count);
  59   }
  60 
  61   out->print(")");
  62 }
  63 
  64 void MemReporterBase::print_virtual_memory(size_t reserved, size_t committed) const {
  65   const char* scale = current_scale();
  66   output()->print("(mmap: reserved=" SIZE_FORMAT "%s, committed=" SIZE_FORMAT "%s)",
  67     amount_in_current_scale(reserved), scale, amount_in_current_scale(committed), scale);
  68 }
  69 
  70 void MemReporterBase::print_malloc_line(size_t amount, size_t count) const {
  71   output()->print("%28s", " ");
  72   print_malloc(amount, count);
  73   output()->print_cr(" ");
  74 }
  75 
  76 void MemReporterBase::print_virtual_memory_line(size_t reserved, size_t committed) const {
  77   output()->print("%28s", " ");
  78   print_virtual_memory(reserved, committed);
  79   output()->print_cr(" ");
  80 }
  81 
  82 void MemReporterBase::print_arena_line(size_t amount, size_t count) const {
  83   const char* scale = current_scale();
  84   output()->print_cr("%27s (arena=" SIZE_FORMAT "%s #" SIZE_FORMAT ")", " ",
  85     amount_in_current_scale(amount), scale, count);
  86 }
  87 
  88 void MemReporterBase::print_virtual_memory_region(const char* type, address base, size_t size) const {
  89   const char* scale = current_scale();
  90   output()->print("[" PTR_FORMAT " - " PTR_FORMAT "] %s " SIZE_FORMAT "%s",
  91     p2i(base), p2i(base + size), type, amount_in_current_scale(size), scale);
  92 }
  93 
  94 
  95 void MemSummaryReporter::report() {
  96   const char* scale = current_scale();
  97   outputStream* out = output();
  98   size_t total_reserved_amount = _malloc_snapshot->total() +
  99     _vm_snapshot->total_reserved();
 100   size_t total_committed_amount = _malloc_snapshot->total() +
 101     _vm_snapshot->total_committed();
 102 
 103   // Overall total
 104   out->print_cr("\nNative Memory Tracking:\n");
 105   out->print("Total: ");
 106   print_total(total_reserved_amount, total_committed_amount);
 107   out->print("\n");
 108 
 109   // Summary by memory type
 110   for (int index = 0; index < mt_number_of_types; index ++) {
 111     MEMFLAGS flag = NMTUtil::index_to_flag(index);
 112     // thread stack is reported as part of thread category
 113     if (flag == mtThreadStack) continue;
 114     MallocMemory* malloc_memory = _malloc_snapshot->by_type(flag);
 115     VirtualMemory* virtual_memory = _vm_snapshot->by_type(flag);
 116 
 117     report_summary_of_type(flag, malloc_memory, virtual_memory);
 118   }
 119 }
 120 
 121 void MemSummaryReporter::report_summary_of_type(MEMFLAGS flag,
 122   MallocMemory*  malloc_memory, VirtualMemory* virtual_memory) {
 123 
 124   size_t reserved_amount  = reserved_total (malloc_memory, virtual_memory);
 125   size_t committed_amount = committed_total(malloc_memory, virtual_memory);
 126 
 127   // Count thread's native stack in "Thread" category
 128   if (flag == mtThread) {
 129     const VirtualMemory* thread_stack_usage =
 130       (const VirtualMemory*)_vm_snapshot->by_type(mtThreadStack);
 131     reserved_amount  += thread_stack_usage->reserved();
 132     committed_amount += thread_stack_usage->committed();
 133   } else if (flag == mtNMT) {
 134     // Count malloc headers in "NMT" category
 135     reserved_amount  += _malloc_snapshot->malloc_overhead()->size();
 136     committed_amount += _malloc_snapshot->malloc_overhead()->size();
 137   }
 138 
 139   if (amount_in_current_scale(reserved_amount) > 0) {
 140     outputStream* out   = output();
 141     const char*   scale = current_scale();
 142     out->print("-%26s (", NMTUtil::flag_to_name(flag));
 143     print_total(reserved_amount, committed_amount);
 144     out->print_cr(")");
 145 
 146     if (flag == mtClass) {
 147       // report class count
 148       out->print_cr("%27s (classes #" SIZE_FORMAT ")", " ", _class_count);
 149     } else if (flag == mtThread) {
 150       // report thread count
 151       out->print_cr("%27s (thread #" SIZE_FORMAT ")", " ", _malloc_snapshot->thread_count());
 152       const VirtualMemory* thread_stack_usage =
 153        _vm_snapshot->by_type(mtThreadStack);
 154       out->print("%27s (stack: ", " ");
 155       print_total(thread_stack_usage->reserved(), thread_stack_usage->committed());
 156       out->print_cr(")");
 157     }
 158 
 159      // report malloc'd memory
 160     if (amount_in_current_scale(malloc_memory->malloc_size()) > 0) {
 161       // We don't know how many arena chunks are in used, so don't report the count
 162       size_t count = (flag == mtChunk) ? 0 : malloc_memory->malloc_count();
 163       print_malloc_line(malloc_memory->malloc_size(), count);
 164     }
 165 
 166     if (amount_in_current_scale(virtual_memory->reserved()) > 0) {
 167       print_virtual_memory_line(virtual_memory->reserved(), virtual_memory->committed());
 168     }
 169 
 170     if (amount_in_current_scale(malloc_memory->arena_size()) > 0) {
 171       print_arena_line(malloc_memory->arena_size(), malloc_memory->arena_count());
 172     }
 173 
 174     if (flag == mtNMT &&
 175       amount_in_current_scale(_malloc_snapshot->malloc_overhead()->size()) > 0) {
 176       out->print_cr("%27s (tracking overhead=" SIZE_FORMAT "%s)", " ",
 177         amount_in_current_scale(_malloc_snapshot->malloc_overhead()->size()), scale);
 178     }
 179 
 180     out->print_cr(" ");
 181   }
 182 }
 183 
 184 void MemDetailReporter::report_detail() {
 185   // Start detail report
 186   outputStream* out = output();
 187   out->print_cr("Details:\n");
 188 
 189   report_malloc_sites();
 190   report_virtual_memory_allocation_sites();
 191 }
 192 
 193 void MemDetailReporter::report_malloc_sites() {
 194   MallocSiteIterator         malloc_itr = _baseline.malloc_sites(MemBaseline::by_size);
 195   if (malloc_itr.is_empty()) return;
 196 
 197   outputStream* out = output();
 198 
 199   const MallocSite* malloc_site;
 200   while ((malloc_site = malloc_itr.next()) != NULL) {
 201     // Don't report if size is too small
 202     if (amount_in_current_scale(malloc_site->size()) == 0)
 203       continue;
 204 
 205     const NativeCallStack* stack = malloc_site->call_stack();
 206     stack->print_on(out);
 207     out->print("%29s", " ");
 208     MEMFLAGS flag = malloc_site->flags();
 209     assert((flag >= 0 && flag < (int)mt_number_of_types) && flag != mtNone,
 210       "Must have a valid memory type");
 211     print_malloc(malloc_site->size(), malloc_site->count(),flag);
 212     out->print_cr("\n");
 213   }
 214 }
 215 
 216 void MemDetailReporter::report_virtual_memory_allocation_sites()  {
 217   VirtualMemorySiteIterator  virtual_memory_itr =
 218     _baseline.virtual_memory_sites(MemBaseline::by_size);
 219 
 220   if (virtual_memory_itr.is_empty()) return;
 221 
 222   outputStream* out = output();
 223   const VirtualMemoryAllocationSite*  virtual_memory_site;
 224 
 225   while ((virtual_memory_site = virtual_memory_itr.next()) != NULL) {
 226     // Don't report if size is too small
 227     if (amount_in_current_scale(virtual_memory_site->reserved()) == 0)
 228       continue;
 229 
 230     const NativeCallStack* stack = virtual_memory_site->call_stack();
 231     stack->print_on(out);
 232     out->print("%28s (", " ");
 233     print_total(virtual_memory_site->reserved(), virtual_memory_site->committed());
 234     out->print_cr(")\n");
 235   }
 236 }
 237 
 238 
 239 void MemDetailReporter::report_virtual_memory_map() {
 240   // Virtual memory map always in base address order
 241   VirtualMemoryAllocationIterator itr = _baseline.virtual_memory_allocations();
 242   const ReservedMemoryRegion* rgn;
 243 
 244   output()->print_cr("Virtual memory map:");
 245   while ((rgn = itr.next()) != NULL) {
 246     report_virtual_memory_region(rgn);
 247   }
 248 }
 249 
 250 void MemDetailReporter::report_virtual_memory_region(const ReservedMemoryRegion* reserved_rgn) {
 251   assert(reserved_rgn != NULL, "NULL pointer");
 252 
 253   // Don't report if size is too small
 254   if (amount_in_current_scale(reserved_rgn->size()) == 0) return;
 255 
 256   outputStream* out = output();
 257   const char* scale = current_scale();
 258   const NativeCallStack*  stack = reserved_rgn->call_stack();
 259   bool all_committed = reserved_rgn->all_committed();
 260   const char* region_type = (all_committed ? "reserved and committed" : "reserved");
 261   out->print_cr(" ");
 262   print_virtual_memory_region(region_type, reserved_rgn->base(), reserved_rgn->size());
 263   out->print(" for %s", NMTUtil::flag_to_name(reserved_rgn->flag()));
 264   if (stack->is_empty()) {
 265     out->print_cr(" ");
 266   } else {
 267     out->print_cr(" from");
 268     stack->print_on(out, 4);
 269   }
 270 
 271   if (all_committed) return;
 272 
 273   CommittedRegionIterator itr = reserved_rgn->iterate_committed_regions();
 274   const CommittedMemoryRegion* committed_rgn;
 275   while ((committed_rgn = itr.next()) != NULL) {
 276     // Don't report if size is too small
 277     if (amount_in_current_scale(committed_rgn->size()) == 0) continue;
 278     stack = committed_rgn->call_stack();
 279     out->print("\n\t");
 280     print_virtual_memory_region("committed", committed_rgn->base(), committed_rgn->size());
 281     if (stack->is_empty()) {
 282       out->print_cr(" ");
 283     } else {
 284       out->print_cr(" from");
 285       stack->print_on(out, 12);
 286     }
 287   }
 288 }
 289 
 290 void MemSummaryDiffReporter::report_diff() {
 291   const char* scale = current_scale();
 292   outputStream* out = output();
 293   out->print_cr("\nNative Memory Tracking:\n");
 294 
 295   // Overall diff
 296   out->print("Total: ");
 297   print_virtual_memory_diff(_current_baseline.total_reserved_memory(),
 298     _current_baseline.total_committed_memory(), _early_baseline.total_reserved_memory(),
 299     _early_baseline.total_committed_memory());
 300 
 301   out->print_cr("\n");
 302 
 303   // Summary diff by memory type
 304   for (int index = 0; index < mt_number_of_types; index ++) {
 305     MEMFLAGS flag = NMTUtil::index_to_flag(index);
 306     // thread stack is reported as part of thread category
 307     if (flag == mtThreadStack) continue;
 308     diff_summary_of_type(flag, _early_baseline.malloc_memory(flag),
 309       _early_baseline.virtual_memory(flag), _current_baseline.malloc_memory(flag),
 310       _current_baseline.virtual_memory(flag));
 311   }
 312 }
 313 
 314 void MemSummaryDiffReporter::print_malloc_diff(size_t current_amount, size_t current_count,
 315     size_t early_amount, size_t early_count, MEMFLAGS flags) const {
 316   const char* scale = current_scale();
 317   outputStream* out = output();
 318 
 319   out->print("malloc=" SIZE_FORMAT "%s", amount_in_current_scale(current_amount), scale);
 320   // Report type only if it is valid
 321   if (flags != mtNone) {
 322     out->print(" type=%s", NMTUtil::flag_to_name(flags));
 323   }
 324 
 325   long amount_diff = diff_in_current_scale(current_amount, early_amount);
 326   if (amount_diff != 0) {
 327     out->print(" %+ld%s", amount_diff, scale);
 328   }
 329   if (current_count > 0) {
 330     out->print(" #" SIZE_FORMAT "", current_count);
 331     if (current_count != early_count) {
 332       out->print(" %+d", (int)(current_count - early_count));
 333     }
 334   }
 335 }
 336 
 337 void MemSummaryDiffReporter::print_arena_diff(size_t current_amount, size_t current_count,
 338   size_t early_amount, size_t early_count) const {
 339   const char* scale = current_scale();
 340   outputStream* out = output();
 341   out->print("arena=" SIZE_FORMAT "%s", amount_in_current_scale(current_amount), scale);
 342   if (diff_in_current_scale(current_amount, early_amount) != 0) {
 343     out->print(" %+ld", diff_in_current_scale(current_amount, early_amount));
 344   }
 345 
 346   out->print(" #" SIZE_FORMAT "", current_count);
 347   if (current_count != early_count) {
 348     out->print(" %+d", (int)(current_count - early_count));
 349   }
 350 }
 351 
 352 void MemSummaryDiffReporter::print_virtual_memory_diff(size_t current_reserved, size_t current_committed,
 353     size_t early_reserved, size_t early_committed) const {
 354   const char* scale = current_scale();
 355   outputStream* out = output();
 356   out->print("reserved=" SIZE_FORMAT "%s", amount_in_current_scale(current_reserved), scale);
 357   long reserved_diff = diff_in_current_scale(current_reserved, early_reserved);
 358   if (reserved_diff != 0) {
 359     out->print(" %+ld%s", reserved_diff, scale);
 360   }
 361 
 362   out->print(", committed=" SIZE_FORMAT "%s", amount_in_current_scale(current_committed), scale);
 363   long committed_diff = diff_in_current_scale(current_committed, early_committed);
 364   if (committed_diff != 0) {
 365     out->print(" %+ld%s", committed_diff, scale);
 366   }
 367 }
 368 
 369 
 370 void MemSummaryDiffReporter::diff_summary_of_type(MEMFLAGS flag, const MallocMemory* early_malloc,
 371   const VirtualMemory* early_vm, const MallocMemory* current_malloc,
 372   const VirtualMemory* current_vm) const {
 373 
 374   outputStream* out = output();
 375   const char* scale = current_scale();
 376 
 377   // Total reserved and committed memory in current baseline
 378   size_t current_reserved_amount  = reserved_total (current_malloc, current_vm);
 379   size_t current_committed_amount = committed_total(current_malloc, current_vm);
 380 
 381   // Total reserved and committed memory in early baseline
 382   size_t early_reserved_amount  = reserved_total(early_malloc, early_vm);
 383   size_t early_committed_amount = committed_total(early_malloc, early_vm);
 384 
 385   // Adjust virtual memory total
 386   if (flag == mtThread) {
 387     const VirtualMemory* early_thread_stack_usage =
 388       _early_baseline.virtual_memory(mtThreadStack);
 389     const VirtualMemory* current_thread_stack_usage =
 390       _current_baseline.virtual_memory(mtThreadStack);
 391 
 392     early_reserved_amount  += early_thread_stack_usage->reserved();
 393     early_committed_amount += early_thread_stack_usage->committed();
 394 
 395     current_reserved_amount  += current_thread_stack_usage->reserved();
 396     current_committed_amount += current_thread_stack_usage->committed();
 397   } else if (flag == mtNMT) {
 398     early_reserved_amount  += _early_baseline.malloc_tracking_overhead();
 399     early_committed_amount += _early_baseline.malloc_tracking_overhead();
 400 
 401     current_reserved_amount  += _current_baseline.malloc_tracking_overhead();
 402     current_committed_amount += _current_baseline.malloc_tracking_overhead();
 403   }
 404 
 405   if (amount_in_current_scale(current_reserved_amount) > 0 ||
 406       diff_in_current_scale(current_reserved_amount, early_reserved_amount) != 0) {
 407 
 408     // print summary line
 409     out->print("-%26s (", NMTUtil::flag_to_name(flag));
 410     print_virtual_memory_diff(current_reserved_amount, current_committed_amount,
 411       early_reserved_amount, early_committed_amount);
 412     out->print_cr(")");
 413 
 414     // detail lines
 415     if (flag == mtClass) {
 416       // report class count
 417       out->print("%27s (classes #" SIZE_FORMAT "", " ", _current_baseline.class_count());
 418       int class_count_diff = (int)(_current_baseline.class_count() -
 419         _early_baseline.class_count());
 420       if (_current_baseline.class_count() != _early_baseline.class_count()) {
 421         out->print(" %+d", (int)(_current_baseline.class_count() - _early_baseline.class_count()));
 422       }
 423       out->print_cr(")");
 424     } else if (flag == mtThread) {
 425       // report thread count
 426       out->print("%27s (thread #" SIZE_FORMAT "", " ", _current_baseline.thread_count());
 427       int thread_count_diff = (int)(_current_baseline.thread_count() -
 428           _early_baseline.thread_count());
 429       if (thread_count_diff != 0) {
 430         out->print(" %+d", thread_count_diff);
 431       }
 432       out->print_cr(")");
 433 
 434       // report thread stack
 435       const VirtualMemory* current_thread_stack =
 436           _current_baseline.virtual_memory(mtThreadStack);
 437       const VirtualMemory* early_thread_stack =
 438         _early_baseline.virtual_memory(mtThreadStack);
 439 
 440       out->print("%27s (stack: ", " ");
 441       print_virtual_memory_diff(current_thread_stack->reserved(), current_thread_stack->committed(),
 442         early_thread_stack->reserved(), early_thread_stack->committed());
 443       out->print_cr(")");
 444     }
 445 
 446     // Report malloc'd memory
 447     size_t current_malloc_amount = current_malloc->malloc_size();
 448     size_t early_malloc_amount   = early_malloc->malloc_size();
 449     if (amount_in_current_scale(current_malloc_amount) > 0 ||
 450         diff_in_current_scale(current_malloc_amount, early_malloc_amount) != 0) {
 451       out->print("%28s(", " ");
 452       print_malloc_diff(current_malloc_amount, (flag == mtChunk) ? 0 : current_malloc->malloc_count(),
 453         early_malloc_amount, early_malloc->malloc_count(), mtNone);
 454       out->print_cr(")");
 455     }
 456 
 457     // Report virtual memory
 458     if (amount_in_current_scale(current_vm->reserved()) > 0 ||
 459         diff_in_current_scale(current_vm->reserved(), early_vm->reserved()) != 0) {
 460       out->print("%27s (mmap: ", " ");
 461       print_virtual_memory_diff(current_vm->reserved(), current_vm->committed(),
 462         early_vm->reserved(), early_vm->committed());
 463       out->print_cr(")");
 464     }
 465 
 466     // Report arena memory
 467     if (amount_in_current_scale(current_malloc->arena_size()) > 0 ||
 468         diff_in_current_scale(current_malloc->arena_size(), early_malloc->arena_size()) != 0) {
 469       out->print("%28s(", " ");
 470       print_arena_diff(current_malloc->arena_size(), current_malloc->arena_count(),
 471         early_malloc->arena_size(), early_malloc->arena_count());
 472       out->print_cr(")");
 473     }
 474 
 475     // Report native memory tracking overhead
 476     if (flag == mtNMT) {
 477       size_t current_tracking_overhead = amount_in_current_scale(_current_baseline.malloc_tracking_overhead());
 478       size_t early_tracking_overhead   = amount_in_current_scale(_early_baseline.malloc_tracking_overhead());
 479 
 480       out->print("%27s (tracking overhead=" SIZE_FORMAT "%s", " ",
 481         amount_in_current_scale(_current_baseline.malloc_tracking_overhead()), scale);
 482 
 483       long overhead_diff = diff_in_current_scale(_current_baseline.malloc_tracking_overhead(),
 484            _early_baseline.malloc_tracking_overhead());
 485       if (overhead_diff != 0) {
 486         out->print(" %+ld%s", overhead_diff, scale);
 487       }
 488       out->print_cr(")");
 489     }
 490     out->print_cr(" ");
 491   }
 492 }
 493 
 494 void MemDetailDiffReporter::report_diff() {
 495   MemSummaryDiffReporter::report_diff();
 496   diff_malloc_sites();
 497   diff_virtual_memory_sites();
 498 }
 499 
 500 void MemDetailDiffReporter::diff_malloc_sites() const {
 501   MallocSiteIterator early_itr = _early_baseline.malloc_sites(MemBaseline::by_site_and_type);
 502   MallocSiteIterator current_itr = _current_baseline.malloc_sites(MemBaseline::by_site_and_type);
 503 
 504   const MallocSite* early_site   = early_itr.next();
 505   const MallocSite* current_site = current_itr.next();
 506 
 507   while (early_site != NULL || current_site != NULL) {
 508     if (early_site == NULL) {
 509       new_malloc_site(current_site);
 510       current_site = current_itr.next();
 511     } else if (current_site == NULL) {
 512       old_malloc_site(early_site);
 513       early_site = early_itr.next();
 514     } else {
 515       int compVal = current_site->call_stack()->compare(*early_site->call_stack());
 516       if (compVal < 0) {
 517         new_malloc_site(current_site);
 518         current_site = current_itr.next();
 519       } else if (compVal > 0) {
 520         old_malloc_site(early_site);
 521         early_site = early_itr.next();
 522       } else {
 523         diff_malloc_site(early_site, current_site);
 524         early_site   = early_itr.next();
 525         current_site = current_itr.next();
 526       }
 527     }
 528   }
 529 }
 530 
 531 void MemDetailDiffReporter::diff_virtual_memory_sites() const {
 532   VirtualMemorySiteIterator early_itr = _early_baseline.virtual_memory_sites(MemBaseline::by_site);
 533   VirtualMemorySiteIterator current_itr = _current_baseline.virtual_memory_sites(MemBaseline::by_site);
 534 
 535   const VirtualMemoryAllocationSite* early_site   = early_itr.next();
 536   const VirtualMemoryAllocationSite* current_site = current_itr.next();
 537 
 538   while (early_site != NULL || current_site != NULL) {
 539     if (early_site == NULL) {
 540       new_virtual_memory_site(current_site);
 541       current_site = current_itr.next();
 542     } else if (current_site == NULL) {
 543       old_virtual_memory_site(early_site);
 544       early_site = early_itr.next();
 545     } else {
 546       int compVal = current_site->call_stack()->compare(*early_site->call_stack());
 547       if (compVal < 0) {
 548         new_virtual_memory_site(current_site);
 549         current_site = current_itr.next();
 550       } else if (compVal > 0) {
 551         old_virtual_memory_site(early_site);
 552         early_site = early_itr.next();
 553       } else {
 554         diff_virtual_memory_site(early_site, current_site);
 555         early_site   = early_itr.next();
 556         current_site = current_itr.next();
 557       }
 558     }
 559   }
 560 }
 561 
 562 
 563 void MemDetailDiffReporter::new_malloc_site(const MallocSite* malloc_site) const {
 564   diff_malloc_site(malloc_site->call_stack(), malloc_site->size(), malloc_site->count(),
 565     0, 0, malloc_site->flags());
 566 }
 567 
 568 void MemDetailDiffReporter::old_malloc_site(const MallocSite* malloc_site) const {
 569   diff_malloc_site(malloc_site->call_stack(), 0, 0, malloc_site->size(),
 570     malloc_site->count(), malloc_site->flags());
 571 }
 572 
 573 void MemDetailDiffReporter::diff_malloc_site(const MallocSite* early,
 574   const MallocSite* current)  const {
 575   if (early->flags() != current->flags()) {
 576     // If malloc site type changed, treat it as deallocation of old type and
 577     // allocation of new type.
 578     old_malloc_site(early);
 579     new_malloc_site(current);
 580   } else {
 581     diff_malloc_site(current->call_stack(), current->size(), current->count(),
 582       early->size(), early->count(), early->flags());
 583   }
 584 }
 585 
 586 void MemDetailDiffReporter::diff_malloc_site(const NativeCallStack* stack, size_t current_size,
 587   size_t current_count, size_t early_size, size_t early_count, MEMFLAGS flags) const {
 588   outputStream* out = output();
 589 
 590   assert(stack != NULL, "NULL stack");
 591 
 592   if (diff_in_current_scale(current_size, early_size) == 0) {
 593       return;
 594   }
 595 
 596   stack->print_on(out);
 597   out->print("%28s (", " ");
 598   print_malloc_diff(current_size, current_count,
 599     early_size, early_count, flags);
 600 
 601   out->print_cr(")\n");
 602 }
 603 
 604 
 605 void MemDetailDiffReporter::new_virtual_memory_site(const VirtualMemoryAllocationSite* site) const {
 606   diff_virtual_memory_site(site->call_stack(), site->reserved(), site->committed(), 0, 0);
 607 }
 608 
 609 void MemDetailDiffReporter::old_virtual_memory_site(const VirtualMemoryAllocationSite* site) const {
 610   diff_virtual_memory_site(site->call_stack(), 0, 0, site->reserved(), site->committed());
 611 }
 612 
 613 void MemDetailDiffReporter::diff_virtual_memory_site(const VirtualMemoryAllocationSite* early,
 614   const VirtualMemoryAllocationSite* current) const {
 615   diff_virtual_memory_site(current->call_stack(), current->reserved(), current->committed(),
 616     early->reserved(), early->committed());
 617 }
 618 
 619 void MemDetailDiffReporter::diff_virtual_memory_site(const NativeCallStack* stack, size_t current_reserved,
 620   size_t current_committed, size_t early_reserved, size_t early_committed) const  {
 621   outputStream* out = output();
 622 
 623   // no change
 624   if (diff_in_current_scale(current_reserved, early_reserved) == 0 &&
 625       diff_in_current_scale(current_committed, early_committed) == 0) {
 626     return;
 627   }
 628 
 629   stack->print_on(out);
 630   out->print("%28s (mmap: ", " ");
 631   print_virtual_memory_diff(current_reserved, current_committed,
 632     early_reserved, early_committed);
 633 
 634   out->print_cr(")\n");
 635  }
 636