src/share/vm/runtime/fprofiler.cpp

Fri, 31 May 2013 06:41:50 +0200

author
anoll
date
Fri, 31 May 2013 06:41:50 +0200
changeset 5224
c07dd9be16e8
parent 4936
aeaca88565e6
child 5614
9758d9f36299
child 5616
522d69638aa8
permissions
-rw-r--r--

8013496: Code cache management command line options work only in special order. Another order of arguments does not deliver the second parameter to the jvm.
Summary: Moved check that ReservedCodeCacheSize >= InitialCodeCacheSize to Arguments::check_vm_args_consistency(). As a result, the ordering in which the two parameters are given to the VM is not relevant. Added a regression test.
Reviewed-by: kvn, twisti

     1 /*
     2  * Copyright (c) 1997, 2013, 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  */
    25 #include "precompiled.hpp"
    26 #include "classfile/classLoader.hpp"
    27 #include "code/vtableStubs.hpp"
    28 #include "gc_interface/collectedHeap.inline.hpp"
    29 #include "interpreter/interpreter.hpp"
    30 #include "memory/allocation.inline.hpp"
    31 #include "memory/universe.inline.hpp"
    32 #include "oops/oop.inline.hpp"
    33 #include "oops/oop.inline2.hpp"
    34 #include "oops/symbol.hpp"
    35 #include "runtime/deoptimization.hpp"
    36 #include "runtime/fprofiler.hpp"
    37 #include "runtime/mutexLocker.hpp"
    38 #include "runtime/stubCodeGenerator.hpp"
    39 #include "runtime/stubRoutines.hpp"
    40 #include "runtime/task.hpp"
    41 #include "runtime/vframe.hpp"
    42 #include "utilities/macros.hpp"
    44 // Static fields of FlatProfiler
    45 int               FlatProfiler::received_gc_ticks   = 0;
    46 int               FlatProfiler::vm_operation_ticks  = 0;
    47 int               FlatProfiler::threads_lock_ticks  = 0;
    48 int               FlatProfiler::class_loader_ticks  = 0;
    49 int               FlatProfiler::extra_ticks         = 0;
    50 int               FlatProfiler::blocked_ticks       = 0;
    51 int               FlatProfiler::deopt_ticks         = 0;
    52 int               FlatProfiler::unknown_ticks       = 0;
    53 int               FlatProfiler::interpreter_ticks   = 0;
    54 int               FlatProfiler::compiler_ticks      = 0;
    55 int               FlatProfiler::received_ticks      = 0;
    56 int               FlatProfiler::delivered_ticks     = 0;
    57 int*              FlatProfiler::bytecode_ticks      = NULL;
    58 int*              FlatProfiler::bytecode_ticks_stub = NULL;
    59 int               FlatProfiler::all_int_ticks       = 0;
    60 int               FlatProfiler::all_comp_ticks      = 0;
    61 int               FlatProfiler::all_ticks           = 0;
    62 bool              FlatProfiler::full_profile_flag   = false;
    63 ThreadProfiler*   FlatProfiler::thread_profiler     = NULL;
    64 ThreadProfiler*   FlatProfiler::vm_thread_profiler  = NULL;
    65 FlatProfilerTask* FlatProfiler::task                = NULL;
    66 elapsedTimer      FlatProfiler::timer;
    67 int               FlatProfiler::interval_ticks_previous = 0;
    68 IntervalData*     FlatProfiler::interval_data       = NULL;
    70 ThreadProfiler::ThreadProfiler() {
    71   // Space for the ProfilerNodes
    72   const int area_size = 1 * ProfilerNodeSize * 1024;
    73   area_bottom = AllocateHeap(area_size, mtInternal);
    74   area_top    = area_bottom;
    75   area_limit  = area_bottom + area_size;
    77   // ProfilerNode pointer table
    78   table = NEW_C_HEAP_ARRAY(ProfilerNode*, table_size, mtInternal);
    79   initialize();
    80   engaged = false;
    81 }
    83 ThreadProfiler::~ThreadProfiler() {
    84   FreeHeap(area_bottom);
    85   area_bottom = NULL;
    86   area_top = NULL;
    87   area_limit = NULL;
    88   FreeHeap(table);
    89   table = NULL;
    90 }
    92 // Statics for ThreadProfiler
    93 int ThreadProfiler::table_size = 1024;
    95 int ThreadProfiler::entry(int  value) {
    96   value = (value > 0) ? value : -value;
    97   return value % table_size;
    98 }
   100 ThreadProfilerMark::ThreadProfilerMark(ThreadProfilerMark::Region r) {
   101   _r = r;
   102   _pp = NULL;
   103   assert(((r > ThreadProfilerMark::noRegion) && (r < ThreadProfilerMark::maxRegion)), "ThreadProfilerMark::Region out of bounds");
   104   Thread* tp = Thread::current();
   105   if (tp != NULL && tp->is_Java_thread()) {
   106     JavaThread* jtp = (JavaThread*) tp;
   107     ThreadProfiler* pp = jtp->get_thread_profiler();
   108     _pp = pp;
   109     if (pp != NULL) {
   110       pp->region_flag[r] = true;
   111     }
   112   }
   113 }
   115 ThreadProfilerMark::~ThreadProfilerMark() {
   116   if (_pp != NULL) {
   117     _pp->region_flag[_r] = false;
   118   }
   119   _pp = NULL;
   120 }
   122 // Random other statics
   123 static const int col1 = 2;      // position of output column 1
   124 static const int col2 = 11;     // position of output column 2
   125 static const int col3 = 25;     // position of output column 3
   126 static const int col4 = 55;     // position of output column 4
   129 // Used for detailed profiling of nmethods.
   130 class PCRecorder : AllStatic {
   131  private:
   132   static int*    counters;
   133   static address base;
   134   enum {
   135    bucket_size = 16
   136   };
   137   static int     index_for(address pc) { return (pc - base)/bucket_size;   }
   138   static address pc_for(int index)     { return base + (index * bucket_size); }
   139   static int     size() {
   140     return ((int)CodeCache::max_capacity())/bucket_size * BytesPerWord;
   141   }
   142  public:
   143   static address bucket_start_for(address pc) {
   144     if (counters == NULL) return NULL;
   145     return pc_for(index_for(pc));
   146   }
   147   static int bucket_count_for(address pc)  { return counters[index_for(pc)]; }
   148   static void init();
   149   static void record(address pc);
   150   static void print();
   151   static void print_blobs(CodeBlob* cb);
   152 };
   154 int*    PCRecorder::counters = NULL;
   155 address PCRecorder::base     = NULL;
   157 void PCRecorder::init() {
   158   MutexLockerEx lm(CodeCache_lock, Mutex::_no_safepoint_check_flag);
   159   int s = size();
   160   counters = NEW_C_HEAP_ARRAY(int, s, mtInternal);
   161   for (int index = 0; index < s; index++) {
   162     counters[index] = 0;
   163   }
   164   base = CodeCache::first_address();
   165 }
   167 void PCRecorder::record(address pc) {
   168   if (counters == NULL) return;
   169   assert(CodeCache::contains(pc), "must be in CodeCache");
   170   counters[index_for(pc)]++;
   171 }
   174 address FlatProfiler::bucket_start_for(address pc) {
   175   return PCRecorder::bucket_start_for(pc);
   176 }
   178 int FlatProfiler::bucket_count_for(address pc) {
   179   return PCRecorder::bucket_count_for(pc);
   180 }
   182 void PCRecorder::print() {
   183   if (counters == NULL) return;
   185   tty->cr();
   186   tty->print_cr("Printing compiled methods with PC buckets having more than %d ticks", ProfilerPCTickThreshold);
   187   tty->print_cr("===================================================================");
   188   tty->cr();
   190   GrowableArray<CodeBlob*>* candidates = new GrowableArray<CodeBlob*>(20);
   193   int s;
   194   {
   195     MutexLockerEx lm(CodeCache_lock, Mutex::_no_safepoint_check_flag);
   196     s = size();
   197   }
   199   for (int index = 0; index < s; index++) {
   200     int count = counters[index];
   201     if (count > ProfilerPCTickThreshold) {
   202       address pc = pc_for(index);
   203       CodeBlob* cb = CodeCache::find_blob_unsafe(pc);
   204       if (cb != NULL && candidates->find(cb) < 0) {
   205         candidates->push(cb);
   206       }
   207     }
   208   }
   209   for (int i = 0; i < candidates->length(); i++) {
   210     print_blobs(candidates->at(i));
   211   }
   212 }
   214 void PCRecorder::print_blobs(CodeBlob* cb) {
   215   if (cb != NULL) {
   216     cb->print();
   217     if (cb->is_nmethod()) {
   218       ((nmethod*)cb)->print_code();
   219     }
   220     tty->cr();
   221   } else {
   222     tty->print_cr("stub code");
   223   }
   224 }
   226 class tick_counter {            // holds tick info for one node
   227  public:
   228   int ticks_in_code;
   229   int ticks_in_native;
   231   tick_counter()                     {  ticks_in_code = ticks_in_native = 0; }
   232   tick_counter(int code, int native) {  ticks_in_code = code; ticks_in_native = native; }
   234   int total() const {
   235     return (ticks_in_code + ticks_in_native);
   236   }
   238   void add(tick_counter* a) {
   239     ticks_in_code += a->ticks_in_code;
   240     ticks_in_native += a->ticks_in_native;
   241   }
   243   void update(TickPosition where) {
   244     switch(where) {
   245       case tp_code:     ticks_in_code++;       break;
   246       case tp_native:   ticks_in_native++;      break;
   247     }
   248   }
   250   void print_code(outputStream* st, int total_ticks) {
   251     st->print("%5.1f%% %5d ", total() * 100.0 / total_ticks, ticks_in_code);
   252   }
   254   void print_native(outputStream* st) {
   255     st->print(" + %5d ", ticks_in_native);
   256   }
   257 };
   259 class ProfilerNode {
   260  private:
   261   ProfilerNode* _next;
   262  public:
   263   tick_counter ticks;
   265  public:
   267   void* operator new(size_t size, ThreadProfiler* tp);
   268   void  operator delete(void* p);
   270   ProfilerNode() {
   271     _next = NULL;
   272   }
   274   virtual ~ProfilerNode() {
   275     if (_next)
   276       delete _next;
   277   }
   279   void set_next(ProfilerNode* n) { _next = n; }
   280   ProfilerNode* next()           { return _next; }
   282   void update(TickPosition where) { ticks.update(where);}
   283   int total_ticks() { return ticks.total(); }
   285   virtual bool is_interpreted() const { return false; }
   286   virtual bool is_compiled()    const { return false; }
   287   virtual bool is_stub()        const { return false; }
   288   virtual bool is_runtime_stub() const{ return false; }
   289   virtual void oops_do(OopClosure* f) = 0;
   291   virtual bool interpreted_match(Method* m) const { return false; }
   292   virtual bool compiled_match(Method* m ) const { return false; }
   293   virtual bool stub_match(Method* m, const char* name) const { return false; }
   294   virtual bool adapter_match() const { return false; }
   295   virtual bool runtimeStub_match(const CodeBlob* stub, const char* name) const { return false; }
   296   virtual bool unknown_compiled_match(const CodeBlob* cb) const { return false; }
   298   static void print_title(outputStream* st) {
   299     st->print(" + native");
   300     st->fill_to(col3);
   301     st->print("Method");
   302     st->fill_to(col4);
   303     st->cr();
   304   }
   306   static void print_total(outputStream* st, tick_counter* t, int total, const char* msg) {
   307     t->print_code(st, total);
   308     st->fill_to(col2);
   309     t->print_native(st);
   310     st->fill_to(col3);
   311     st->print(msg);
   312     st->cr();
   313   }
   315   virtual Method* method()         = 0;
   317   virtual void print_method_on(outputStream* st) {
   318     int limit;
   319     int i;
   320     Method* m = method();
   321     Symbol* k = m->klass_name();
   322     // Print the class name with dots instead of slashes
   323     limit = k->utf8_length();
   324     for (i = 0 ; i < limit ; i += 1) {
   325       char c = (char) k->byte_at(i);
   326       if (c == '/') {
   327         c = '.';
   328       }
   329       st->print("%c", c);
   330     }
   331     if (limit > 0) {
   332       st->print(".");
   333     }
   334     Symbol* n = m->name();
   335     limit = n->utf8_length();
   336     for (i = 0 ; i < limit ; i += 1) {
   337       char c = (char) n->byte_at(i);
   338       st->print("%c", c);
   339     }
   340     if (Verbose || WizardMode) {
   341       // Disambiguate overloaded methods
   342       Symbol* sig = m->signature();
   343       sig->print_symbol_on(st);
   344     } else if (MethodHandles::is_signature_polymorphic(m->intrinsic_id()))
   345       // compare with Method::print_short_name
   346       MethodHandles::print_as_basic_type_signature_on(st, m->signature(), true);
   347   }
   349   virtual void print(outputStream* st, int total_ticks) {
   350     ticks.print_code(st, total_ticks);
   351     st->fill_to(col2);
   352     ticks.print_native(st);
   353     st->fill_to(col3);
   354     print_method_on(st);
   355     st->cr();
   356   }
   358   // for hashing into the table
   359   static int hash(Method* method) {
   360       // The point here is to try to make something fairly unique
   361       // out of the fields we can read without grabbing any locks
   362       // since the method may be locked when we need the hash.
   363       return (
   364           method->code_size() ^
   365           method->max_stack() ^
   366           method->max_locals() ^
   367           method->size_of_parameters());
   368   }
   370   // for sorting
   371   static int compare(ProfilerNode** a, ProfilerNode** b) {
   372     return (*b)->total_ticks() - (*a)->total_ticks();
   373   }
   374 };
   376 void* ProfilerNode::operator new(size_t size, ThreadProfiler* tp){
   377   void* result = (void*) tp->area_top;
   378   tp->area_top += size;
   380   if (tp->area_top > tp->area_limit) {
   381     fatal("flat profiler buffer overflow");
   382   }
   383   return result;
   384 }
   386 void ProfilerNode::operator delete(void* p){
   387 }
   389 class interpretedNode : public ProfilerNode {
   390  private:
   391    Method* _method;
   392    oop       _class_loader;  // needed to keep metadata for the method alive
   393  public:
   394    interpretedNode(Method* method, TickPosition where) : ProfilerNode() {
   395      _method = method;
   396      _class_loader = method->method_holder()->class_loader();
   397      update(where);
   398    }
   400    bool is_interpreted() const { return true; }
   402    bool interpreted_match(Method* m) const {
   403       return _method == m;
   404    }
   406    void oops_do(OopClosure* f) {
   407      f->do_oop(&_class_loader);
   408    }
   410    Method* method() { return _method; }
   412    static void print_title(outputStream* st) {
   413      st->fill_to(col1);
   414      st->print("%11s", "Interpreted");
   415      ProfilerNode::print_title(st);
   416    }
   418    void print(outputStream* st, int total_ticks) {
   419      ProfilerNode::print(st, total_ticks);
   420    }
   422    void print_method_on(outputStream* st) {
   423      ProfilerNode::print_method_on(st);
   424      MethodCounters* mcs = method()->method_counters();
   425      if (Verbose && mcs != NULL) mcs->invocation_counter()->print_short();
   426    }
   427 };
   429 class compiledNode : public ProfilerNode {
   430  private:
   431    Method* _method;
   432    oop       _class_loader;  // needed to keep metadata for the method alive
   433  public:
   434    compiledNode(Method* method, TickPosition where) : ProfilerNode() {
   435      _method = method;
   436      _class_loader = method->method_holder()->class_loader();
   437      update(where);
   438   }
   439   bool is_compiled()    const { return true; }
   441   bool compiled_match(Method* m) const {
   442     return _method == m;
   443   }
   445   Method* method()         { return _method; }
   447   void oops_do(OopClosure* f) {
   448     f->do_oop(&_class_loader);
   449   }
   451   static void print_title(outputStream* st) {
   452     st->fill_to(col1);
   453     st->print("%11s", "Compiled");
   454     ProfilerNode::print_title(st);
   455   }
   457   void print(outputStream* st, int total_ticks) {
   458     ProfilerNode::print(st, total_ticks);
   459   }
   461   void print_method_on(outputStream* st) {
   462     ProfilerNode::print_method_on(st);
   463   }
   464 };
   466 class stubNode : public ProfilerNode {
   467  private:
   468   Method* _method;
   469   oop       _class_loader;  // needed to keep metadata for the method alive
   470   const char* _symbol;   // The name of the nearest VM symbol (for +ProfileVM). Points to a unique string
   471  public:
   472    stubNode(Method* method, const char* name, TickPosition where) : ProfilerNode() {
   473      _method = method;
   474      _class_loader = method->method_holder()->class_loader();
   475      _symbol = name;
   476      update(where);
   477    }
   479    bool is_stub() const { return true; }
   481    void oops_do(OopClosure* f) {
   482      f->do_oop(&_class_loader);
   483    }
   485    bool stub_match(Method* m, const char* name) const {
   486      return (_method == m) && (_symbol == name);
   487    }
   489    Method* method() { return _method; }
   491    static void print_title(outputStream* st) {
   492      st->fill_to(col1);
   493      st->print("%11s", "Stub");
   494      ProfilerNode::print_title(st);
   495    }
   497    void print(outputStream* st, int total_ticks) {
   498      ProfilerNode::print(st, total_ticks);
   499    }
   501    void print_method_on(outputStream* st) {
   502      ProfilerNode::print_method_on(st);
   503      print_symbol_on(st);
   504    }
   506   void print_symbol_on(outputStream* st) {
   507     if(_symbol) {
   508       st->print("  (%s)", _symbol);
   509     }
   510   }
   511 };
   513 class adapterNode : public ProfilerNode {
   514  public:
   515    adapterNode(TickPosition where) : ProfilerNode() {
   516      update(where);
   517   }
   518   bool is_compiled()    const { return true; }
   520   bool adapter_match() const { return true; }
   522   Method* method()         { return NULL; }
   524   void oops_do(OopClosure* f) {
   525     ;
   526   }
   528   void print(outputStream* st, int total_ticks) {
   529     ProfilerNode::print(st, total_ticks);
   530   }
   532   void print_method_on(outputStream* st) {
   533     st->print("%s", "adapters");
   534   }
   535 };
   537 class runtimeStubNode : public ProfilerNode {
   538  private:
   539    const CodeBlob* _stub;
   540   const char* _symbol;     // The name of the nearest VM symbol when ProfileVM is on. Points to a unique string.
   541  public:
   542    runtimeStubNode(const CodeBlob* stub, const char* name, TickPosition where) : ProfilerNode(), _stub(stub),  _symbol(name) {
   543      assert(stub->is_runtime_stub(), "wrong code blob");
   544      update(where);
   545    }
   547   bool is_runtime_stub() const { return true; }
   549   bool runtimeStub_match(const CodeBlob* stub, const char* name) const {
   550     assert(stub->is_runtime_stub(), "wrong code blob");
   551     return ((RuntimeStub*)_stub)->entry_point() == ((RuntimeStub*)stub)->entry_point() &&
   552             (_symbol == name);
   553   }
   555   Method* method() { return NULL; }
   557   static void print_title(outputStream* st) {
   558     st->fill_to(col1);
   559     st->print("%11s", "Runtime stub");
   560     ProfilerNode::print_title(st);
   561   }
   563   void oops_do(OopClosure* f) {
   564     ;
   565   }
   567   void print(outputStream* st, int total_ticks) {
   568     ProfilerNode::print(st, total_ticks);
   569   }
   571   void print_method_on(outputStream* st) {
   572     st->print("%s", ((RuntimeStub*)_stub)->name());
   573     print_symbol_on(st);
   574   }
   576   void print_symbol_on(outputStream* st) {
   577     if(_symbol) {
   578       st->print("  (%s)", _symbol);
   579     }
   580   }
   581 };
   584 class unknown_compiledNode : public ProfilerNode {
   585  const char *_name;
   586  public:
   587    unknown_compiledNode(const CodeBlob* cb, TickPosition where) : ProfilerNode() {
   588      if ( cb->is_buffer_blob() )
   589        _name = ((BufferBlob*)cb)->name();
   590      else
   591        _name = ((SingletonBlob*)cb)->name();
   592      update(where);
   593   }
   594   bool is_compiled()    const { return true; }
   596   bool unknown_compiled_match(const CodeBlob* cb) const {
   597      if ( cb->is_buffer_blob() )
   598        return !strcmp(((BufferBlob*)cb)->name(), _name);
   599      else
   600        return !strcmp(((SingletonBlob*)cb)->name(), _name);
   601   }
   603   Method* method()         { return NULL; }
   605   void oops_do(OopClosure* f) {
   606     ;
   607   }
   609   void print(outputStream* st, int total_ticks) {
   610     ProfilerNode::print(st, total_ticks);
   611   }
   613   void print_method_on(outputStream* st) {
   614     st->print("%s", _name);
   615   }
   616 };
   618 class vmNode : public ProfilerNode {
   619  private:
   620   const char* _name; // "optional" name obtained by os means such as dll lookup
   621  public:
   622   vmNode(const TickPosition where) : ProfilerNode() {
   623     _name = NULL;
   624     update(where);
   625   }
   627   vmNode(const char* name, const TickPosition where) : ProfilerNode() {
   628     _name = name;
   629     update(where);
   630   }
   632   const char *name()    const { return _name; }
   633   bool is_compiled()    const { return true; }
   635   bool vm_match(const char* name) const { return strcmp(name, _name) == 0; }
   637   Method* method()          { return NULL; }
   639   static int hash(const char* name){
   640     // Compute a simple hash
   641     const char* cp = name;
   642     int h = 0;
   644     if(name != NULL){
   645       while(*cp != '\0'){
   646         h = (h << 1) ^ *cp;
   647         cp++;
   648       }
   649     }
   650     return h;
   651   }
   653   void oops_do(OopClosure* f) {
   654     ;
   655   }
   657   void print(outputStream* st, int total_ticks) {
   658     ProfilerNode::print(st, total_ticks);
   659   }
   661   void print_method_on(outputStream* st) {
   662     if(_name==NULL){
   663       st->print("%s", "unknown code");
   664     }
   665     else {
   666       st->print("%s", _name);
   667     }
   668   }
   669 };
   671 void ThreadProfiler::interpreted_update(Method* method, TickPosition where) {
   672   int index = entry(ProfilerNode::hash(method));
   673   if (!table[index]) {
   674     table[index] = new (this) interpretedNode(method, where);
   675   } else {
   676     ProfilerNode* prev = table[index];
   677     for(ProfilerNode* node = prev; node; node = node->next()) {
   678       if (node->interpreted_match(method)) {
   679         node->update(where);
   680         return;
   681       }
   682       prev = node;
   683     }
   684     prev->set_next(new (this) interpretedNode(method, where));
   685   }
   686 }
   688 void ThreadProfiler::compiled_update(Method* method, TickPosition where) {
   689   int index = entry(ProfilerNode::hash(method));
   690   if (!table[index]) {
   691     table[index] = new (this) compiledNode(method, where);
   692   } else {
   693     ProfilerNode* prev = table[index];
   694     for(ProfilerNode* node = prev; node; node = node->next()) {
   695       if (node->compiled_match(method)) {
   696         node->update(where);
   697         return;
   698       }
   699       prev = node;
   700     }
   701     prev->set_next(new (this) compiledNode(method, where));
   702   }
   703 }
   705 void ThreadProfiler::stub_update(Method* method, const char* name, TickPosition where) {
   706   int index = entry(ProfilerNode::hash(method));
   707   if (!table[index]) {
   708     table[index] = new (this) stubNode(method, name, where);
   709   } else {
   710     ProfilerNode* prev = table[index];
   711     for(ProfilerNode* node = prev; node; node = node->next()) {
   712       if (node->stub_match(method, name)) {
   713         node->update(where);
   714         return;
   715       }
   716       prev = node;
   717     }
   718     prev->set_next(new (this) stubNode(method, name, where));
   719   }
   720 }
   722 void ThreadProfiler::adapter_update(TickPosition where) {
   723   int index = 0;
   724   if (!table[index]) {
   725     table[index] = new (this) adapterNode(where);
   726   } else {
   727     ProfilerNode* prev = table[index];
   728     for(ProfilerNode* node = prev; node; node = node->next()) {
   729       if (node->adapter_match()) {
   730         node->update(where);
   731         return;
   732       }
   733       prev = node;
   734     }
   735     prev->set_next(new (this) adapterNode(where));
   736   }
   737 }
   739 void ThreadProfiler::runtime_stub_update(const CodeBlob* stub, const char* name, TickPosition where) {
   740   int index = 0;
   741   if (!table[index]) {
   742     table[index] = new (this) runtimeStubNode(stub, name, where);
   743   } else {
   744     ProfilerNode* prev = table[index];
   745     for(ProfilerNode* node = prev; node; node = node->next()) {
   746       if (node->runtimeStub_match(stub, name)) {
   747         node->update(where);
   748         return;
   749       }
   750       prev = node;
   751     }
   752     prev->set_next(new (this) runtimeStubNode(stub, name, where));
   753   }
   754 }
   757 void ThreadProfiler::unknown_compiled_update(const CodeBlob* cb, TickPosition where) {
   758   int index = 0;
   759   if (!table[index]) {
   760     table[index] = new (this) unknown_compiledNode(cb, where);
   761   } else {
   762     ProfilerNode* prev = table[index];
   763     for(ProfilerNode* node = prev; node; node = node->next()) {
   764       if (node->unknown_compiled_match(cb)) {
   765         node->update(where);
   766         return;
   767       }
   768       prev = node;
   769     }
   770     prev->set_next(new (this) unknown_compiledNode(cb, where));
   771   }
   772 }
   774 void ThreadProfiler::vm_update(TickPosition where) {
   775   vm_update(NULL, where);
   776 }
   778 void ThreadProfiler::vm_update(const char* name, TickPosition where) {
   779   int index = entry(vmNode::hash(name));
   780   assert(index >= 0, "Must be positive");
   781   // Note that we call strdup below since the symbol may be resource allocated
   782   if (!table[index]) {
   783     table[index] = new (this) vmNode(os::strdup(name), where);
   784   } else {
   785     ProfilerNode* prev = table[index];
   786     for(ProfilerNode* node = prev; node; node = node->next()) {
   787       if (((vmNode *)node)->vm_match(name)) {
   788         node->update(where);
   789         return;
   790       }
   791       prev = node;
   792     }
   793     prev->set_next(new (this) vmNode(os::strdup(name), where));
   794   }
   795 }
   798 class FlatProfilerTask : public PeriodicTask {
   799 public:
   800   FlatProfilerTask(int interval_time) : PeriodicTask(interval_time) {}
   801   void task();
   802 };
   804 void FlatProfiler::record_vm_operation() {
   805   if (Universe::heap()->is_gc_active()) {
   806     FlatProfiler::received_gc_ticks += 1;
   807     return;
   808   }
   810   if (DeoptimizationMarker::is_active()) {
   811     FlatProfiler::deopt_ticks += 1;
   812     return;
   813   }
   815   FlatProfiler::vm_operation_ticks += 1;
   816 }
   818 void FlatProfiler::record_vm_tick() {
   819   // Profile the VM Thread itself if needed
   820   // This is done without getting the Threads_lock and we can go deep
   821   // inside Safepoint, etc.
   822   if( ProfileVM  ) {
   823     ResourceMark rm;
   824     ExtendedPC epc;
   825     const char *name = NULL;
   826     char buf[256];
   827     buf[0] = '\0';
   829     vm_thread_profiler->inc_thread_ticks();
   831     // Get a snapshot of a current VMThread pc (and leave it running!)
   832     // The call may fail if, for instance the VM thread is interrupted while
   833     // holding the Interrupt_lock or for other reasons.
   834     epc = os::get_thread_pc(VMThread::vm_thread());
   835     if(epc.pc() != NULL) {
   836       if (os::dll_address_to_function_name(epc.pc(), buf, sizeof(buf), NULL)) {
   837          name = buf;
   838       }
   839     }
   840     if (name != NULL) {
   841       vm_thread_profiler->vm_update(name, tp_native);
   842     }
   843   }
   844 }
   846 void FlatProfiler::record_thread_ticks() {
   848   int maxthreads, suspendedthreadcount;
   849   JavaThread** threadsList;
   850   bool interval_expired = false;
   852   if (ProfileIntervals &&
   853       (FlatProfiler::received_ticks >= interval_ticks_previous + ProfileIntervalsTicks)) {
   854     interval_expired = true;
   855     interval_ticks_previous = FlatProfiler::received_ticks;
   856   }
   858   // Try not to wait for the Threads_lock
   859   if (Threads_lock->try_lock()) {
   860     {  // Threads_lock scope
   861       maxthreads = Threads::number_of_threads();
   862       threadsList = NEW_C_HEAP_ARRAY(JavaThread *, maxthreads, mtInternal);
   863       suspendedthreadcount = 0;
   864       for (JavaThread* tp = Threads::first(); tp != NULL; tp = tp->next()) {
   865         if (tp->is_Compiler_thread()) {
   866           // Only record ticks for active compiler threads
   867           CompilerThread* cthread = (CompilerThread*)tp;
   868           if (cthread->task() != NULL) {
   869             // The compiler is active.  If we need to access any of the fields
   870             // of the compiler task we should suspend the CompilerThread first.
   871             FlatProfiler::compiler_ticks += 1;
   872             continue;
   873           }
   874         }
   876         // First externally suspend all threads by marking each for
   877         // external suspension - so it will stop at its next transition
   878         // Then do a safepoint
   879         ThreadProfiler* pp = tp->get_thread_profiler();
   880         if (pp != NULL && pp->engaged) {
   881           MutexLockerEx ml(tp->SR_lock(), Mutex::_no_safepoint_check_flag);
   882           if (!tp->is_external_suspend() && !tp->is_exiting()) {
   883             tp->set_external_suspend();
   884             threadsList[suspendedthreadcount++] = tp;
   885           }
   886         }
   887       }
   888       Threads_lock->unlock();
   889     }
   890     // Suspend each thread. This call should just return
   891     // for any threads that have already self-suspended
   892     // Net result should be one safepoint
   893     for (int j = 0; j < suspendedthreadcount; j++) {
   894       JavaThread *tp = threadsList[j];
   895       if (tp) {
   896         tp->java_suspend();
   897       }
   898     }
   900     // We are responsible for resuming any thread on this list
   901     for (int i = 0; i < suspendedthreadcount; i++) {
   902       JavaThread *tp = threadsList[i];
   903       if (tp) {
   904         ThreadProfiler* pp = tp->get_thread_profiler();
   905         if (pp != NULL && pp->engaged) {
   906           HandleMark hm;
   907           FlatProfiler::delivered_ticks += 1;
   908           if (interval_expired) {
   909           FlatProfiler::interval_record_thread(pp);
   910           }
   911           // This is the place where we check to see if a user thread is
   912           // blocked waiting for compilation.
   913           if (tp->blocked_on_compilation()) {
   914             pp->compiler_ticks += 1;
   915             pp->interval_data_ref()->inc_compiling();
   916           } else {
   917             pp->record_tick(tp);
   918           }
   919         }
   920         MutexLocker ml(Threads_lock);
   921         tp->java_resume();
   922       }
   923     }
   924     if (interval_expired) {
   925       FlatProfiler::interval_print();
   926       FlatProfiler::interval_reset();
   927     }
   928   } else {
   929     // Couldn't get the threads lock, just record that rather than blocking
   930     FlatProfiler::threads_lock_ticks += 1;
   931   }
   933 }
   935 void FlatProfilerTask::task() {
   936   FlatProfiler::received_ticks += 1;
   938   if (ProfileVM) {
   939     FlatProfiler::record_vm_tick();
   940   }
   942   VM_Operation* op = VMThread::vm_operation();
   943   if (op != NULL) {
   944     FlatProfiler::record_vm_operation();
   945     if (SafepointSynchronize::is_at_safepoint()) {
   946       return;
   947     }
   948   }
   949   FlatProfiler::record_thread_ticks();
   950 }
   952 void ThreadProfiler::record_interpreted_tick(JavaThread* thread, frame fr, TickPosition where, int* ticks) {
   953   FlatProfiler::all_int_ticks++;
   954   if (!FlatProfiler::full_profile()) {
   955     return;
   956   }
   958   if (!fr.is_interpreted_frame_valid(thread)) {
   959     // tick came at a bad time
   960     interpreter_ticks += 1;
   961     FlatProfiler::interpreter_ticks += 1;
   962     return;
   963   }
   965   // The frame has been fully validated so we can trust the method and bci
   967   Method* method = *fr.interpreter_frame_method_addr();
   969   interpreted_update(method, where);
   971   // update byte code table
   972   InterpreterCodelet* desc = Interpreter::codelet_containing(fr.pc());
   973   if (desc != NULL && desc->bytecode() >= 0) {
   974     ticks[desc->bytecode()]++;
   975   }
   976 }
   978 void ThreadProfiler::record_compiled_tick(JavaThread* thread, frame fr, TickPosition where) {
   979   const char *name = NULL;
   980   TickPosition localwhere = where;
   982   FlatProfiler::all_comp_ticks++;
   983   if (!FlatProfiler::full_profile()) return;
   985   CodeBlob* cb = fr.cb();
   987 // For runtime stubs, record as native rather than as compiled
   988    if (cb->is_runtime_stub()) {
   989         RegisterMap map(thread, false);
   990         fr = fr.sender(&map);
   991         cb = fr.cb();
   992         localwhere = tp_native;
   993   }
   994   Method* method = (cb->is_nmethod()) ? ((nmethod *)cb)->method() :
   995                                           (Method*)NULL;
   997   if (method == NULL) {
   998     if (cb->is_runtime_stub())
   999       runtime_stub_update(cb, name, localwhere);
  1000     else
  1001       unknown_compiled_update(cb, localwhere);
  1003   else {
  1004     if (method->is_native()) {
  1005       stub_update(method, name, localwhere);
  1006     } else {
  1007       compiled_update(method, localwhere);
  1012 extern "C" void find(int x);
  1015 void ThreadProfiler::record_tick_for_running_frame(JavaThread* thread, frame fr) {
  1016   // The tick happened in real code -> non VM code
  1017   if (fr.is_interpreted_frame()) {
  1018     interval_data_ref()->inc_interpreted();
  1019     record_interpreted_tick(thread, fr, tp_code, FlatProfiler::bytecode_ticks);
  1020     return;
  1023   if (CodeCache::contains(fr.pc())) {
  1024     interval_data_ref()->inc_compiled();
  1025     PCRecorder::record(fr.pc());
  1026     record_compiled_tick(thread, fr, tp_code);
  1027     return;
  1030   if (VtableStubs::stub_containing(fr.pc()) != NULL) {
  1031     unknown_ticks_array[ut_vtable_stubs] += 1;
  1032     return;
  1035   frame caller = fr.profile_find_Java_sender_frame(thread);
  1037   if (caller.sp() != NULL && caller.pc() != NULL) {
  1038     record_tick_for_calling_frame(thread, caller);
  1039     return;
  1042   unknown_ticks_array[ut_running_frame] += 1;
  1043   FlatProfiler::unknown_ticks += 1;
  1046 void ThreadProfiler::record_tick_for_calling_frame(JavaThread* thread, frame fr) {
  1047   // The tick happened in VM code
  1048   interval_data_ref()->inc_native();
  1049   if (fr.is_interpreted_frame()) {
  1050     record_interpreted_tick(thread, fr, tp_native, FlatProfiler::bytecode_ticks_stub);
  1051     return;
  1053   if (CodeCache::contains(fr.pc())) {
  1054     record_compiled_tick(thread, fr, tp_native);
  1055     return;
  1058   frame caller = fr.profile_find_Java_sender_frame(thread);
  1060   if (caller.sp() != NULL && caller.pc() != NULL) {
  1061     record_tick_for_calling_frame(thread, caller);
  1062     return;
  1065   unknown_ticks_array[ut_calling_frame] += 1;
  1066   FlatProfiler::unknown_ticks += 1;
  1069 void ThreadProfiler::record_tick(JavaThread* thread) {
  1070   FlatProfiler::all_ticks++;
  1071   thread_ticks += 1;
  1073   // Here's another way to track global state changes.
  1074   // When the class loader starts it marks the ThreadProfiler to tell it it is in the class loader
  1075   // and we check that here.
  1076   // This is more direct, and more than one thread can be in the class loader at a time,
  1077   // but it does mean the class loader has to know about the profiler.
  1078   if (region_flag[ThreadProfilerMark::classLoaderRegion]) {
  1079     class_loader_ticks += 1;
  1080     FlatProfiler::class_loader_ticks += 1;
  1081     return;
  1082   } else if (region_flag[ThreadProfilerMark::extraRegion]) {
  1083     extra_ticks += 1;
  1084     FlatProfiler::extra_ticks += 1;
  1085     return;
  1087   // Note that the WatcherThread can now stop for safepoints
  1088   uint32_t debug_bits = 0;
  1089   if (!thread->wait_for_ext_suspend_completion(SuspendRetryCount,
  1090       SuspendRetryDelay, &debug_bits)) {
  1091     unknown_ticks_array[ut_unknown_thread_state] += 1;
  1092     FlatProfiler::unknown_ticks += 1;
  1093     return;
  1096   frame fr;
  1098   switch (thread->thread_state()) {
  1099   case _thread_in_native:
  1100   case _thread_in_native_trans:
  1101   case _thread_in_vm:
  1102   case _thread_in_vm_trans:
  1103     if (thread->profile_last_Java_frame(&fr)) {
  1104       if (fr.is_runtime_frame()) {
  1105         RegisterMap map(thread, false);
  1106         fr = fr.sender(&map);
  1108       record_tick_for_calling_frame(thread, fr);
  1109     } else {
  1110       unknown_ticks_array[ut_no_last_Java_frame] += 1;
  1111       FlatProfiler::unknown_ticks += 1;
  1113     break;
  1114   // handle_special_runtime_exit_condition self-suspends threads in Java
  1115   case _thread_in_Java:
  1116   case _thread_in_Java_trans:
  1117     if (thread->profile_last_Java_frame(&fr)) {
  1118       if (fr.is_safepoint_blob_frame()) {
  1119         RegisterMap map(thread, false);
  1120         fr = fr.sender(&map);
  1122       record_tick_for_running_frame(thread, fr);
  1123     } else {
  1124       unknown_ticks_array[ut_no_last_Java_frame] += 1;
  1125       FlatProfiler::unknown_ticks += 1;
  1127     break;
  1128   case _thread_blocked:
  1129   case _thread_blocked_trans:
  1130     if (thread->osthread() && thread->osthread()->get_state() == RUNNABLE) {
  1131         if (thread->profile_last_Java_frame(&fr)) {
  1132           if (fr.is_safepoint_blob_frame()) {
  1133             RegisterMap map(thread, false);
  1134             fr = fr.sender(&map);
  1135             record_tick_for_running_frame(thread, fr);
  1136           } else {
  1137             record_tick_for_calling_frame(thread, fr);
  1139         } else {
  1140           unknown_ticks_array[ut_no_last_Java_frame] += 1;
  1141           FlatProfiler::unknown_ticks += 1;
  1143     } else {
  1144           blocked_ticks += 1;
  1145           FlatProfiler::blocked_ticks += 1;
  1147     break;
  1148   case _thread_uninitialized:
  1149   case _thread_new:
  1150   // not used, included for completeness
  1151   case _thread_new_trans:
  1152      unknown_ticks_array[ut_no_last_Java_frame] += 1;
  1153      FlatProfiler::unknown_ticks += 1;
  1154      break;
  1155   default:
  1156     unknown_ticks_array[ut_unknown_thread_state] += 1;
  1157     FlatProfiler::unknown_ticks += 1;
  1158     break;
  1160   return;
  1163 void ThreadProfiler::engage() {
  1164   engaged = true;
  1165   timer.start();
  1168 void ThreadProfiler::disengage() {
  1169   engaged = false;
  1170   timer.stop();
  1173 void ThreadProfiler::initialize() {
  1174   for (int index = 0; index < table_size; index++) {
  1175     table[index] = NULL;
  1177   thread_ticks = 0;
  1178   blocked_ticks = 0;
  1179   compiler_ticks = 0;
  1180   interpreter_ticks = 0;
  1181   for (int ut = 0; ut < ut_end; ut += 1) {
  1182     unknown_ticks_array[ut] = 0;
  1184   region_flag[ThreadProfilerMark::classLoaderRegion] = false;
  1185   class_loader_ticks = 0;
  1186   region_flag[ThreadProfilerMark::extraRegion] = false;
  1187   extra_ticks = 0;
  1188   timer.start();
  1189   interval_data_ref()->reset();
  1192 void ThreadProfiler::reset() {
  1193   timer.stop();
  1194   if (table != NULL) {
  1195     for (int index = 0; index < table_size; index++) {
  1196       ProfilerNode* n = table[index];
  1197       if (n != NULL) {
  1198         delete n;
  1202   initialize();
  1205 void FlatProfiler::allocate_table() {
  1206   { // Bytecode table
  1207     bytecode_ticks      = NEW_C_HEAP_ARRAY(int, Bytecodes::number_of_codes, mtInternal);
  1208     bytecode_ticks_stub = NEW_C_HEAP_ARRAY(int, Bytecodes::number_of_codes, mtInternal);
  1209     for(int index = 0; index < Bytecodes::number_of_codes; index++) {
  1210       bytecode_ticks[index]      = 0;
  1211       bytecode_ticks_stub[index] = 0;
  1215   if (ProfilerRecordPC) PCRecorder::init();
  1217   interval_data         = NEW_C_HEAP_ARRAY(IntervalData, interval_print_size, mtInternal);
  1218   FlatProfiler::interval_reset();
  1221 void FlatProfiler::engage(JavaThread* mainThread, bool fullProfile) {
  1222   full_profile_flag = fullProfile;
  1223   if (bytecode_ticks == NULL) {
  1224     allocate_table();
  1226   if(ProfileVM && (vm_thread_profiler == NULL)){
  1227     vm_thread_profiler = new ThreadProfiler();
  1229   if (task == NULL) {
  1230     task = new FlatProfilerTask(WatcherThread::delay_interval);
  1231     task->enroll();
  1233   timer.start();
  1234   if (mainThread != NULL) {
  1235     // When mainThread was created, it might not have a ThreadProfiler
  1236     ThreadProfiler* pp = mainThread->get_thread_profiler();
  1237     if (pp == NULL) {
  1238       mainThread->set_thread_profiler(new ThreadProfiler());
  1239     } else {
  1240       pp->reset();
  1242     mainThread->get_thread_profiler()->engage();
  1244   // This is where we would assign thread_profiler
  1245   // if we wanted only one thread_profiler for all threads.
  1246   thread_profiler = NULL;
  1249 void FlatProfiler::disengage() {
  1250   if (!task) {
  1251     return;
  1253   timer.stop();
  1254   task->disenroll();
  1255   delete task;
  1256   task = NULL;
  1257   if (thread_profiler != NULL) {
  1258     thread_profiler->disengage();
  1259   } else {
  1260     MutexLocker tl(Threads_lock);
  1261     for (JavaThread* tp = Threads::first(); tp != NULL; tp = tp->next()) {
  1262       ThreadProfiler* pp = tp->get_thread_profiler();
  1263       if (pp != NULL) {
  1264         pp->disengage();
  1270 void FlatProfiler::reset() {
  1271   if (task) {
  1272     disengage();
  1275   class_loader_ticks = 0;
  1276   extra_ticks        = 0;
  1277   received_gc_ticks  = 0;
  1278   vm_operation_ticks = 0;
  1279   compiler_ticks     = 0;
  1280   deopt_ticks        = 0;
  1281   interpreter_ticks  = 0;
  1282   blocked_ticks      = 0;
  1283   unknown_ticks      = 0;
  1284   received_ticks     = 0;
  1285   delivered_ticks    = 0;
  1286   timer.stop();
  1289 bool FlatProfiler::is_active() {
  1290   return task != NULL;
  1293 void FlatProfiler::print_byte_code_statistics() {
  1294   GrowableArray <ProfilerNode*>* array = new GrowableArray<ProfilerNode*>(200);
  1296   tty->print_cr(" Bytecode ticks:");
  1297   for (int index = 0; index < Bytecodes::number_of_codes; index++) {
  1298     if (FlatProfiler::bytecode_ticks[index] > 0 || FlatProfiler::bytecode_ticks_stub[index] > 0) {
  1299       tty->print_cr("  %4d %4d = %s",
  1300         FlatProfiler::bytecode_ticks[index],
  1301         FlatProfiler::bytecode_ticks_stub[index],
  1302         Bytecodes::name( (Bytecodes::Code) index));
  1305   tty->cr();
  1308 void print_ticks(const char* title, int ticks, int total) {
  1309   if (ticks > 0) {
  1310     tty->print("%5.1f%% %5d", ticks * 100.0 / total, ticks);
  1311     tty->fill_to(col3);
  1312     tty->print("%s", title);
  1313     tty->cr();
  1317 void ThreadProfiler::print(const char* thread_name) {
  1318   ResourceMark rm;
  1319   MutexLocker ppl(ProfilePrint_lock);
  1320   int index = 0; // Declared outside for loops for portability
  1322   if (table == NULL) {
  1323     return;
  1326   if (thread_ticks <= 0) {
  1327     return;
  1330   const char* title = "too soon to tell";
  1331   double secs = timer.seconds();
  1333   GrowableArray <ProfilerNode*>* array = new GrowableArray<ProfilerNode*>(200);
  1334   for(index = 0; index < table_size; index++) {
  1335     for(ProfilerNode* node = table[index]; node; node = node->next())
  1336       array->append(node);
  1339   array->sort(&ProfilerNode::compare);
  1341   // compute total (sanity check)
  1342   int active =
  1343     class_loader_ticks +
  1344     compiler_ticks +
  1345     interpreter_ticks +
  1346     unknown_ticks();
  1347   for (index = 0; index < array->length(); index++) {
  1348     active += array->at(index)->ticks.total();
  1350   int total = active + blocked_ticks;
  1352   tty->cr();
  1353   tty->print_cr("Flat profile of %3.2f secs (%d total ticks): %s", secs, total, thread_name);
  1354   if (total != thread_ticks) {
  1355     print_ticks("Lost ticks", thread_ticks-total, thread_ticks);
  1357   tty->cr();
  1359   // print interpreted methods
  1360   tick_counter interpreted_ticks;
  1361   bool has_interpreted_ticks = false;
  1362   int print_count = 0;
  1363   for (index = 0; index < array->length(); index++) {
  1364     ProfilerNode* n = array->at(index);
  1365     if (n->is_interpreted()) {
  1366       interpreted_ticks.add(&n->ticks);
  1367       if (!has_interpreted_ticks) {
  1368         interpretedNode::print_title(tty);
  1369         has_interpreted_ticks = true;
  1371       if (print_count++ < ProfilerNumberOfInterpretedMethods) {
  1372         n->print(tty, active);
  1376   if (has_interpreted_ticks) {
  1377     if (print_count <= ProfilerNumberOfInterpretedMethods) {
  1378       title = "Total interpreted";
  1379     } else {
  1380       title = "Total interpreted (including elided)";
  1382     interpretedNode::print_total(tty, &interpreted_ticks, active, title);
  1383     tty->cr();
  1386   // print compiled methods
  1387   tick_counter compiled_ticks;
  1388   bool has_compiled_ticks = false;
  1389   print_count = 0;
  1390   for (index = 0; index < array->length(); index++) {
  1391     ProfilerNode* n = array->at(index);
  1392     if (n->is_compiled()) {
  1393       compiled_ticks.add(&n->ticks);
  1394       if (!has_compiled_ticks) {
  1395         compiledNode::print_title(tty);
  1396         has_compiled_ticks = true;
  1398       if (print_count++ < ProfilerNumberOfCompiledMethods) {
  1399         n->print(tty, active);
  1403   if (has_compiled_ticks) {
  1404     if (print_count <= ProfilerNumberOfCompiledMethods) {
  1405       title = "Total compiled";
  1406     } else {
  1407       title = "Total compiled (including elided)";
  1409     compiledNode::print_total(tty, &compiled_ticks, active, title);
  1410     tty->cr();
  1413   // print stub methods
  1414   tick_counter stub_ticks;
  1415   bool has_stub_ticks = false;
  1416   print_count = 0;
  1417   for (index = 0; index < array->length(); index++) {
  1418     ProfilerNode* n = array->at(index);
  1419     if (n->is_stub()) {
  1420       stub_ticks.add(&n->ticks);
  1421       if (!has_stub_ticks) {
  1422         stubNode::print_title(tty);
  1423         has_stub_ticks = true;
  1425       if (print_count++ < ProfilerNumberOfStubMethods) {
  1426         n->print(tty, active);
  1430   if (has_stub_ticks) {
  1431     if (print_count <= ProfilerNumberOfStubMethods) {
  1432       title = "Total stub";
  1433     } else {
  1434       title = "Total stub (including elided)";
  1436     stubNode::print_total(tty, &stub_ticks, active, title);
  1437     tty->cr();
  1440   // print runtime stubs
  1441   tick_counter runtime_stub_ticks;
  1442   bool has_runtime_stub_ticks = false;
  1443   print_count = 0;
  1444   for (index = 0; index < array->length(); index++) {
  1445     ProfilerNode* n = array->at(index);
  1446     if (n->is_runtime_stub()) {
  1447       runtime_stub_ticks.add(&n->ticks);
  1448       if (!has_runtime_stub_ticks) {
  1449         runtimeStubNode::print_title(tty);
  1450         has_runtime_stub_ticks = true;
  1452       if (print_count++ < ProfilerNumberOfRuntimeStubNodes) {
  1453         n->print(tty, active);
  1457   if (has_runtime_stub_ticks) {
  1458     if (print_count <= ProfilerNumberOfRuntimeStubNodes) {
  1459       title = "Total runtime stubs";
  1460     } else {
  1461       title = "Total runtime stubs (including elided)";
  1463     runtimeStubNode::print_total(tty, &runtime_stub_ticks, active, title);
  1464     tty->cr();
  1467   if (blocked_ticks + class_loader_ticks + interpreter_ticks + compiler_ticks + unknown_ticks() != 0) {
  1468     tty->fill_to(col1);
  1469     tty->print_cr("Thread-local ticks:");
  1470     print_ticks("Blocked (of total)",  blocked_ticks,      total);
  1471     print_ticks("Class loader",        class_loader_ticks, active);
  1472     print_ticks("Extra",               extra_ticks,        active);
  1473     print_ticks("Interpreter",         interpreter_ticks,  active);
  1474     print_ticks("Compilation",         compiler_ticks,     active);
  1475     print_ticks("Unknown: vtable stubs",  unknown_ticks_array[ut_vtable_stubs],         active);
  1476     print_ticks("Unknown: null method",   unknown_ticks_array[ut_null_method],          active);
  1477     print_ticks("Unknown: running frame", unknown_ticks_array[ut_running_frame],        active);
  1478     print_ticks("Unknown: calling frame", unknown_ticks_array[ut_calling_frame],        active);
  1479     print_ticks("Unknown: no pc",         unknown_ticks_array[ut_no_pc],                active);
  1480     print_ticks("Unknown: no last frame", unknown_ticks_array[ut_no_last_Java_frame],   active);
  1481     print_ticks("Unknown: thread_state",  unknown_ticks_array[ut_unknown_thread_state], active);
  1482     tty->cr();
  1485   if (WizardMode) {
  1486     tty->print_cr("Node area used: %dKb", (area_top - area_bottom) / 1024);
  1488   reset();
  1491 /*
  1492 ThreadProfiler::print_unknown(){
  1493   if (table == NULL) {
  1494     return;
  1497   if (thread_ticks <= 0) {
  1498     return;
  1500 } */
  1502 void FlatProfiler::print(int unused) {
  1503   ResourceMark rm;
  1504   if (thread_profiler != NULL) {
  1505     thread_profiler->print("All threads");
  1506   } else {
  1507     MutexLocker tl(Threads_lock);
  1508     for (JavaThread* tp = Threads::first(); tp != NULL; tp = tp->next()) {
  1509       ThreadProfiler* pp = tp->get_thread_profiler();
  1510       if (pp != NULL) {
  1511         pp->print(tp->get_thread_name());
  1516   if (ProfilerPrintByteCodeStatistics) {
  1517     print_byte_code_statistics();
  1520   if (non_method_ticks() > 0) {
  1521     tty->cr();
  1522     tty->print_cr("Global summary of %3.2f seconds:", timer.seconds());
  1523     print_ticks("Received ticks",      received_ticks,     received_ticks);
  1524     print_ticks("Received GC ticks",   received_gc_ticks,  received_ticks);
  1525     print_ticks("Compilation",         compiler_ticks,     received_ticks);
  1526     print_ticks("Deoptimization",      deopt_ticks,        received_ticks);
  1527     print_ticks("Other VM operations", vm_operation_ticks, received_ticks);
  1528 #ifndef PRODUCT
  1529     print_ticks("Blocked ticks",       blocked_ticks,      received_ticks);
  1530     print_ticks("Threads_lock blocks", threads_lock_ticks, received_ticks);
  1531     print_ticks("Delivered ticks",     delivered_ticks,    received_ticks);
  1532     print_ticks("All ticks",           all_ticks,          received_ticks);
  1533 #endif
  1534     print_ticks("Class loader",        class_loader_ticks, received_ticks);
  1535     print_ticks("Extra       ",        extra_ticks,        received_ticks);
  1536     print_ticks("Interpreter",         interpreter_ticks,  received_ticks);
  1537     print_ticks("Unknown code",        unknown_ticks,      received_ticks);
  1540   PCRecorder::print();
  1542   if(ProfileVM){
  1543     tty->cr();
  1544     vm_thread_profiler->print("VM Thread");
  1548 void IntervalData::print_header(outputStream* st) {
  1549   st->print("i/c/n/g");
  1552 void IntervalData::print_data(outputStream* st) {
  1553   st->print("%d/%d/%d/%d", interpreted(), compiled(), native(), compiling());
  1556 void FlatProfiler::interval_record_thread(ThreadProfiler* tp) {
  1557   IntervalData id = tp->interval_data();
  1558   int total = id.total();
  1559   tp->interval_data_ref()->reset();
  1561   // Insertion sort the data, if it's relevant.
  1562   for (int i = 0; i < interval_print_size; i += 1) {
  1563     if (total > interval_data[i].total()) {
  1564       for (int j = interval_print_size - 1; j > i; j -= 1) {
  1565         interval_data[j] = interval_data[j-1];
  1567       interval_data[i] = id;
  1568       break;
  1573 void FlatProfiler::interval_print() {
  1574   if ((interval_data[0].total() > 0)) {
  1575     tty->stamp();
  1576     tty->print("\t");
  1577     IntervalData::print_header(tty);
  1578     for (int i = 0; i < interval_print_size; i += 1) {
  1579       if (interval_data[i].total() > 0) {
  1580         tty->print("\t");
  1581         interval_data[i].print_data(tty);
  1584     tty->cr();
  1588 void FlatProfiler::interval_reset() {
  1589   for (int i = 0; i < interval_print_size; i += 1) {
  1590     interval_data[i].reset();
  1594 void ThreadProfiler::oops_do(OopClosure* f) {
  1595   if (table == NULL) return;
  1597   for(int index = 0; index < table_size; index++) {
  1598     for(ProfilerNode* node = table[index]; node; node = node->next())
  1599       node->oops_do(f);
  1603 void FlatProfiler::oops_do(OopClosure* f) {
  1604   if (thread_profiler != NULL) {
  1605     thread_profiler->oops_do(f);
  1606   } else {
  1607     for (JavaThread* tp = Threads::first(); tp != NULL; tp = tp->next()) {
  1608       ThreadProfiler* pp = tp->get_thread_profiler();
  1609       if (pp != NULL) {
  1610         pp->oops_do(f);

mercurial