classLoaderStats.cpp 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
/*
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
 * 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.
 *
 * 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.
 *
 */

#include "precompiled.hpp"
#include "classfile/classLoaderStats.hpp"
#include "utilities/globalDefinitions.hpp"


class ClassStatsClosure : public KlassClosure {
public:
  int _num_classes;

  ClassStatsClosure() :
    _num_classes(0) {
  }

  virtual void do_klass(Klass* k) {
    _num_classes++;
  }
};


void ClassLoaderStatsClosure::do_cld(ClassLoaderData* cld) {
  oop cl = cld->class_loader();
  ClassLoaderStats* cls;

  // The hashtable key is the ClassLoader oop since we want to account
  // for "real" classes and anonymous classes together
  ClassLoaderStats** cls_ptr = _stats->get(cl);
  if (cls_ptr == NULL) {
    cls = new ClassLoaderStats();
    _stats->put(cl, cls);
    _total_loaders++;
  } else {
    cls = *cls_ptr;
  }

  if (!cld->is_anonymous()) {
    cls->_cld = cld;
  }

  cls->_class_loader = cl;
  if (cl != NULL) {
    cls->_parent = java_lang_ClassLoader::parent(cl);
    addEmptyParents(cls->_parent);
  }

  ClassStatsClosure csc;
  cld->classes_do(&csc);
  if(cld->is_anonymous()) {
    cls->_anon_classes_count += csc._num_classes;
  } else {
    cls->_classes_count = csc._num_classes;
  }
  _total_classes += csc._num_classes;

  Metaspace* ms = cld->metaspace_or_null();
  if (ms != NULL) {
    if(cld->is_anonymous()) {
      cls->_anon_chunk_sz += ms->allocated_chunks_bytes();
      cls->_anon_block_sz += ms->allocated_blocks_bytes();
    } else {
      cls->_chunk_sz = ms->allocated_chunks_bytes();
      cls->_block_sz = ms->allocated_blocks_bytes();
    }
    _total_chunk_sz += ms->allocated_chunks_bytes();
    _total_block_sz += ms->allocated_blocks_bytes();
  }
}


// Handles the difference in pointer width on 32 and 64 bit platforms
#ifdef _LP64
  #define SPACE "%8s"
#else
  #define SPACE "%s"
#endif


bool ClassLoaderStatsClosure::do_entry(oop const& key, ClassLoaderStats* const& cls) {
  Klass* class_loader_klass = (cls->_class_loader == NULL ? NULL : cls->_class_loader->klass());
  Klass* parent_klass = (cls->_parent == NULL ? NULL : cls->_parent->klass());

  _out->print(INTPTR_FORMAT "  " INTPTR_FORMAT "  " INTPTR_FORMAT "  " UINTX_FORMAT_W(6) "  " SIZE_FORMAT_W(8) "  " SIZE_FORMAT_W(8) "  ",
      p2i(class_loader_klass), p2i(parent_klass), p2i(cls->_cld),
      cls->_classes_count,
      cls->_chunk_sz, cls->_block_sz);
  if (class_loader_klass != NULL) {
    _out->print("%s", class_loader_klass->external_name());
  } else {
    _out->print("<boot class loader>");
  }
  _out->cr();
  if (cls->_anon_classes_count > 0) {
    _out->print_cr(SPACE SPACE SPACE "                                    " UINTX_FORMAT_W(6) "  " SIZE_FORMAT_W(8) "  " SIZE_FORMAT_W(8) "   + unsafe anonymous classes",
        "", "", "",
        cls->_anon_classes_count,
        cls->_anon_chunk_sz, cls->_anon_block_sz);
  }
  return true;
}


void ClassLoaderStatsClosure::print() {
  _out->print_cr("ClassLoader" SPACE " Parent" SPACE "      CLD*" SPACE "       Classes   ChunkSz   BlockSz  Type", "", "", "");
  _stats->iterate(this);
  _out->print("Total = " UINTX_FORMAT_W(-6), _total_loaders);
  _out->print(SPACE SPACE SPACE "                      ", "", "", "");
  _out->print_cr(UINTX_FORMAT_W(6) "  " SIZE_FORMAT_W(8) "  " SIZE_FORMAT_W(8) "  ",
      _total_classes,
      _total_chunk_sz,
      _total_block_sz);
  _out->print_cr("ChunkSz: Total size of all allocated metaspace chunks");
  _out->print_cr("BlockSz: Total size of all allocated metaspace blocks (each chunk has several blocks)");
}


void ClassLoaderStatsClosure::addEmptyParents(oop cl) {
  while (cl != NULL && java_lang_ClassLoader::loader_data(cl) == NULL) {
    // This classloader has not loaded any classes
    ClassLoaderStats** cls_ptr = _stats->get(cl);
    if (cls_ptr == NULL) {
      // It does not exist in our table - add it
      ClassLoaderStats* cls = new ClassLoaderStats();
      cls->_class_loader = cl;
      cls->_parent = java_lang_ClassLoader::parent(cl);
      _stats->put(cl, cls);
      _total_loaders++;
    }

    cl = java_lang_ClassLoader::parent(cl);
  }
}


void ClassLoaderStatsVMOperation::doit() {
  ClassLoaderStatsClosure clsc (_out);
  ClassLoaderDataGraph::cld_do(&clsc);
  clsc.print();
}


void ClassLoaderStatsDCmd::execute(DCmdSource source, TRAPS) {
  ClassLoaderStatsVMOperation op(output());
  VMThread::execute(&op);
}