aprofiler.cpp 4.3 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32
#include "precompiled.hpp"
#include "classfile/systemDictionary.hpp"
#include "gc_interface/collectedHeap.inline.hpp"
#include "memory/resourceArea.hpp"
#include "memory/space.hpp"
#include "oops/oop.inline.hpp"
#include "oops/oop.inline2.hpp"
#include "runtime/aprofiler.hpp"
D
duke 已提交
33 34 35


bool AllocationProfiler::_active = false;
36
GrowableArray<Klass*>* AllocationProfiler::_print_array = NULL;
D
duke 已提交
37 38 39 40 41


class AllocProfClosure : public ObjectClosure {
 public:
  void do_object(oop obj) {
42
    Klass* k = obj->klass();
D
duke 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    k->set_alloc_count(k->alloc_count() + 1);
    k->set_alloc_size(k->alloc_size() + obj->size());
  }
};


void AllocationProfiler::iterate_since_last_gc() {
  if (is_active()) {
    AllocProfClosure blk;
    GenCollectedHeap* heap = GenCollectedHeap::heap();
    heap->object_iterate_since_last_GC(&blk);
  }
}


void AllocationProfiler::engage() {
  _active = true;
}


void AllocationProfiler::disengage() {
  _active = false;
}


68
void AllocationProfiler::add_class_to_array(Klass* k) {
D
duke 已提交
69 70 71 72
  _print_array->append(k);
}


73
void AllocationProfiler::add_classes_to_array(Klass* k) {
D
duke 已提交
74
  // Iterate over klass and all array klasses for klass
75
  k->with_array_klasses_do(&AllocationProfiler::add_class_to_array);
D
duke 已提交
76 77 78
}


79
int AllocationProfiler::compare_classes(Klass** k1, Klass** k2) {
D
duke 已提交
80
  // Sort by total allocation size
81
  return (*k2)->alloc_size() - (*k1)->alloc_size();
D
duke 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
}


int AllocationProfiler::average(size_t alloc_size, int alloc_count) {
  return (int) ((double) (alloc_size * BytesPerWord) / MAX2(alloc_count, 1) + 0.5);
}


void AllocationProfiler::sort_and_print_array(size_t cutoff) {
  _print_array->sort(&AllocationProfiler::compare_classes);
  tty->print_cr("________________Size"
                "__Instances"
                "__Average"
                "__Class________________");
  size_t total_alloc_size = 0;
  int total_alloc_count = 0;
  for (int index = 0; index < _print_array->length(); index++) {
99 100
    Klass* k = _print_array->at(index);
    size_t alloc_size = k->alloc_size();
D
duke 已提交
101
    if (alloc_size > cutoff) {
102
      int alloc_count = k->alloc_count();
D
duke 已提交
103
#ifdef PRODUCT
104
      const char* name = k->external_name();
D
duke 已提交
105
#else
106
      const char* name = k->internal_name();
D
duke 已提交
107 108 109 110 111 112 113 114 115
#endif
      tty->print_cr("%20u %10u %8u  %s",
        alloc_size * BytesPerWord,
        alloc_count,
        average(alloc_size, alloc_count),
        name);
      total_alloc_size += alloc_size;
      total_alloc_count += alloc_count;
    }
116 117
    k->set_alloc_count(0);
    k->set_alloc_size(0);
D
duke 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131
  }
  tty->print_cr("%20u %10u %8u  --total--",
    total_alloc_size * BytesPerWord,
    total_alloc_count,
    average(total_alloc_size, total_alloc_count));
  tty->cr();
}


void AllocationProfiler::print(size_t cutoff) {
  ResourceMark rm;
  assert(!is_active(), "AllocationProfiler cannot be active while printing profile");

  tty->cr();
132
  tty->print_cr("Allocation profile (sizes in bytes, cutoff = " SIZE_FORMAT " bytes):", cutoff * BytesPerWord);
D
duke 已提交
133 134 135
  tty->cr();

  // Print regular instance klasses and basic type array klasses
136
  _print_array = new GrowableArray<Klass*>(SystemDictionary::number_of_classes()*2);
D
duke 已提交
137 138 139 140
  SystemDictionary::classes_do(&add_classes_to_array);
  Universe::basic_type_classes_do(&add_classes_to_array);
  sort_and_print_array(cutoff);

141 142
  // This used to print metadata in the permgen but since there isn't a permgen
  // anymore, it is not yet implemented.
D
duke 已提交
143
}