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