1 /*
   2  * Copyright (c) 1997, 2016, 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 in some circumstances
 840     epc = os::get_thread_pc(VMThread::vm_thread());
 841     if(epc.pc() != NULL) {
 842       if (os::dll_address_to_function_name(epc.pc(), buf, sizeof(buf), NULL)) {
 843          name = buf;
 844       }
 845     }
 846     if (name != NULL) {
 847       vm_thread_profiler->vm_update(name, tp_native);
 848     }
 849   }
 850 }
 851 
 852 void FlatProfiler::record_thread_ticks() {
 853 
 854   int maxthreads, suspendedthreadcount;
 855   JavaThread** threadsList;
 856   bool interval_expired = false;
 857 
 858   if (ProfileIntervals &&
 859       (FlatProfiler::received_ticks >= interval_ticks_previous + ProfileIntervalsTicks)) {
 860     interval_expired = true;
 861     interval_ticks_previous = FlatProfiler::received_ticks;
 862   }
 863 
 864   // Try not to wait for the Threads_lock
 865   if (Threads_lock->try_lock()) {
 866     {  // Threads_lock scope
 867       maxthreads = Threads::number_of_threads();
 868       threadsList = NEW_C_HEAP_ARRAY(JavaThread *, maxthreads, mtInternal);
 869       suspendedthreadcount = 0;
 870       for (JavaThread* tp = Threads::first(); tp != NULL; tp = tp->next()) {
 871         if (tp->is_Compiler_thread()) {
 872           // Only record ticks for active compiler threads
 873           CompilerThread* cthread = (CompilerThread*)tp;
 874           if (cthread->task() != NULL) {
 875             // The compiler is active.  If we need to access any of the fields
 876             // of the compiler task we should suspend the CompilerThread first.
 877             FlatProfiler::compiler_ticks += 1;
 878             continue;
 879           }
 880         }
 881 
 882         // First externally suspend all threads by marking each for
 883         // external suspension - so it will stop at its next transition
 884         // Then do a safepoint
 885         ThreadProfiler* pp = tp->get_thread_profiler();
 886         if (pp != NULL && pp->engaged) {
 887           MutexLockerEx ml(tp->SR_lock(), Mutex::_no_safepoint_check_flag);
 888           if (!tp->is_external_suspend() && !tp->is_exiting()) {
 889             tp->set_external_suspend();
 890             threadsList[suspendedthreadcount++] = tp;
 891           }
 892         }
 893       }
 894       Threads_lock->unlock();
 895     }
 896     // Suspend each thread. This call should just return
 897     // for any threads that have already self-suspended
 898     // Net result should be one safepoint
 899     for (int j = 0; j < suspendedthreadcount; j++) {
 900       JavaThread *tp = threadsList[j];
 901       if (tp) {
 902         tp->java_suspend();
 903       }
 904     }
 905 
 906     // We are responsible for resuming any thread on this list
 907     for (int i = 0; i < suspendedthreadcount; i++) {
 908       JavaThread *tp = threadsList[i];
 909       if (tp) {
 910         ThreadProfiler* pp = tp->get_thread_profiler();
 911         if (pp != NULL && pp->engaged) {
 912           HandleMark hm;
 913           FlatProfiler::delivered_ticks += 1;
 914           if (interval_expired) {
 915           FlatProfiler::interval_record_thread(pp);
 916           }
 917           // This is the place where we check to see if a user thread is
 918           // blocked waiting for compilation.
 919           if (tp->blocked_on_compilation()) {
 920             pp->compiler_ticks += 1;
 921             pp->interval_data_ref()->inc_compiling();
 922           } else {
 923             pp->record_tick(tp);
 924           }
 925         }
 926         MutexLocker ml(Threads_lock);
 927         tp->java_resume();
 928       }
 929     }
 930     if (interval_expired) {
 931       FlatProfiler::interval_print();
 932       FlatProfiler::interval_reset();
 933     }
 934 
 935     FREE_C_HEAP_ARRAY(JavaThread *, threadsList);
 936   } else {
 937     // Couldn't get the threads lock, just record that rather than blocking
 938     FlatProfiler::threads_lock_ticks += 1;
 939   }
 940 
 941 }
 942 
 943 void FlatProfilerTask::task() {
 944   FlatProfiler::received_ticks += 1;
 945 
 946   if (ProfileVM) {
 947     FlatProfiler::record_vm_tick();
 948   }
 949 
 950   VM_Operation* op = VMThread::vm_operation();
 951   if (op != NULL) {
 952     FlatProfiler::record_vm_operation();
 953     if (SafepointSynchronize::is_at_safepoint()) {
 954       return;
 955     }
 956   }
 957   FlatProfiler::record_thread_ticks();
 958 }
 959 
 960 void ThreadProfiler::record_interpreted_tick(JavaThread* thread, frame fr, TickPosition where, int* ticks) {
 961   FlatProfiler::all_int_ticks++;
 962   if (!FlatProfiler::full_profile()) {
 963     return;
 964   }
 965 
 966   if (!fr.is_interpreted_frame_valid(thread)) {
 967     // tick came at a bad time
 968     interpreter_ticks += 1;
 969     FlatProfiler::interpreter_ticks += 1;
 970     return;
 971   }
 972 
 973   // The frame has been fully validated so we can trust the method and bci
 974 
 975   Method* method = *fr.interpreter_frame_method_addr();
 976 
 977   interpreted_update(method, where);
 978 
 979   // update byte code table
 980   InterpreterCodelet* desc = Interpreter::codelet_containing(fr.pc());
 981   if (desc != NULL && desc->bytecode() >= 0) {
 982     ticks[desc->bytecode()]++;
 983   }
 984 }
 985 
 986 void ThreadProfiler::record_compiled_tick(JavaThread* thread, frame fr, TickPosition where) {
 987   const char *name = NULL;
 988   TickPosition localwhere = where;
 989 
 990   FlatProfiler::all_comp_ticks++;
 991   if (!FlatProfiler::full_profile()) return;
 992 
 993   CodeBlob* cb = fr.cb();
 994 
 995 // For runtime stubs, record as native rather than as compiled
 996    if (cb->is_runtime_stub()) {
 997         RegisterMap map(thread, false);
 998         fr = fr.sender(&map);
 999         cb = fr.cb();
1000         localwhere = tp_native;
1001   }
1002   Method* method = (cb->is_nmethod()) ? ((nmethod *)cb)->method() :
1003                                           (Method*)NULL;
1004 
1005   if (method == NULL) {
1006     if (cb->is_runtime_stub())
1007       runtime_stub_update(cb, name, localwhere);
1008     else
1009       unknown_compiled_update(cb, localwhere);
1010   }
1011   else {
1012     if (method->is_native()) {
1013       stub_update(method, name, localwhere);
1014     } else {
1015       compiled_update(method, localwhere);
1016     }
1017   }
1018 }
1019 
1020 extern "C" void find(int x);
1021 
1022 
1023 void ThreadProfiler::record_tick_for_running_frame(JavaThread* thread, frame fr) {
1024   // The tick happened in real code -> non VM code
1025   if (fr.is_interpreted_frame()) {
1026     interval_data_ref()->inc_interpreted();
1027     record_interpreted_tick(thread, fr, tp_code, FlatProfiler::bytecode_ticks);
1028     return;
1029   }
1030 
1031   if (CodeCache::contains(fr.pc())) {
1032     interval_data_ref()->inc_compiled();
1033     PCRecorder::record(fr.pc());
1034     record_compiled_tick(thread, fr, tp_code);
1035     return;
1036   }
1037 
1038   if (VtableStubs::stub_containing(fr.pc()) != NULL) {
1039     unknown_ticks_array[ut_vtable_stubs] += 1;
1040     return;
1041   }
1042 
1043   frame caller = fr.profile_find_Java_sender_frame(thread);
1044 
1045   if (caller.sp() != NULL && caller.pc() != NULL) {
1046     record_tick_for_calling_frame(thread, caller);
1047     return;
1048   }
1049 
1050   unknown_ticks_array[ut_running_frame] += 1;
1051   FlatProfiler::unknown_ticks += 1;
1052 }
1053 
1054 void ThreadProfiler::record_tick_for_calling_frame(JavaThread* thread, frame fr) {
1055   // The tick happened in VM code
1056   interval_data_ref()->inc_native();
1057   if (fr.is_interpreted_frame()) {
1058     record_interpreted_tick(thread, fr, tp_native, FlatProfiler::bytecode_ticks_stub);
1059     return;
1060   }
1061   if (CodeCache::contains(fr.pc())) {
1062     record_compiled_tick(thread, fr, tp_native);
1063     return;
1064   }
1065 
1066   frame caller = fr.profile_find_Java_sender_frame(thread);
1067 
1068   if (caller.sp() != NULL && caller.pc() != NULL) {
1069     record_tick_for_calling_frame(thread, caller);
1070     return;
1071   }
1072 
1073   unknown_ticks_array[ut_calling_frame] += 1;
1074   FlatProfiler::unknown_ticks += 1;
1075 }
1076 
1077 void ThreadProfiler::record_tick(JavaThread* thread) {
1078   FlatProfiler::all_ticks++;
1079   thread_ticks += 1;
1080 
1081   // Here's another way to track global state changes.
1082   // When the class loader starts it marks the ThreadProfiler to tell it it is in the class loader
1083   // and we check that here.
1084   // This is more direct, and more than one thread can be in the class loader at a time,
1085   // but it does mean the class loader has to know about the profiler.
1086   if (region_flag[ThreadProfilerMark::classLoaderRegion]) {
1087     class_loader_ticks += 1;
1088     FlatProfiler::class_loader_ticks += 1;
1089     return;
1090   } else if (region_flag[ThreadProfilerMark::extraRegion]) {
1091     extra_ticks += 1;
1092     FlatProfiler::extra_ticks += 1;
1093     return;
1094   }
1095   // Note that the WatcherThread can now stop for safepoints
1096   uint32_t debug_bits = 0;
1097   if (!thread->wait_for_ext_suspend_completion(SuspendRetryCount,
1098       SuspendRetryDelay, &debug_bits)) {
1099     unknown_ticks_array[ut_unknown_thread_state] += 1;
1100     FlatProfiler::unknown_ticks += 1;
1101     return;
1102   }
1103 
1104   frame fr;
1105 
1106   switch (thread->thread_state()) {
1107   case _thread_in_native:
1108   case _thread_in_native_trans:
1109   case _thread_in_vm:
1110   case _thread_in_vm_trans:
1111     if (thread->profile_last_Java_frame(&fr)) {
1112       if (fr.is_runtime_frame()) {
1113         RegisterMap map(thread, false);
1114         fr = fr.sender(&map);
1115       }
1116       record_tick_for_calling_frame(thread, fr);
1117     } else {
1118       unknown_ticks_array[ut_no_last_Java_frame] += 1;
1119       FlatProfiler::unknown_ticks += 1;
1120     }
1121     break;
1122   // handle_special_runtime_exit_condition self-suspends threads in Java
1123   case _thread_in_Java:
1124   case _thread_in_Java_trans:
1125     if (thread->profile_last_Java_frame(&fr)) {
1126       if (fr.is_safepoint_blob_frame()) {
1127         RegisterMap map(thread, false);
1128         fr = fr.sender(&map);
1129       }
1130       record_tick_for_running_frame(thread, fr);
1131     } else {
1132       unknown_ticks_array[ut_no_last_Java_frame] += 1;
1133       FlatProfiler::unknown_ticks += 1;
1134     }
1135     break;
1136   case _thread_blocked:
1137   case _thread_blocked_trans:
1138     if (thread->osthread() && thread->osthread()->get_state() == RUNNABLE) {
1139         if (thread->profile_last_Java_frame(&fr)) {
1140           if (fr.is_safepoint_blob_frame()) {
1141             RegisterMap map(thread, false);
1142             fr = fr.sender(&map);
1143             record_tick_for_running_frame(thread, fr);
1144           } else {
1145             record_tick_for_calling_frame(thread, fr);
1146           }
1147         } else {
1148           unknown_ticks_array[ut_no_last_Java_frame] += 1;
1149           FlatProfiler::unknown_ticks += 1;
1150         }
1151     } else {
1152           blocked_ticks += 1;
1153           FlatProfiler::blocked_ticks += 1;
1154     }
1155     break;
1156   case _thread_uninitialized:
1157   case _thread_new:
1158   // not used, included for completeness
1159   case _thread_new_trans:
1160      unknown_ticks_array[ut_no_last_Java_frame] += 1;
1161      FlatProfiler::unknown_ticks += 1;
1162      break;
1163   default:
1164     unknown_ticks_array[ut_unknown_thread_state] += 1;
1165     FlatProfiler::unknown_ticks += 1;
1166     break;
1167   }
1168   return;
1169 }
1170 
1171 void ThreadProfiler::engage() {
1172   engaged = true;
1173   timer.start();
1174 }
1175 
1176 void ThreadProfiler::disengage() {
1177   engaged = false;
1178   timer.stop();
1179 }
1180 
1181 void ThreadProfiler::initialize() {
1182   for (int index = 0; index < table_size; index++) {
1183     table[index] = NULL;
1184   }
1185   thread_ticks = 0;
1186   blocked_ticks = 0;
1187   compiler_ticks = 0;
1188   interpreter_ticks = 0;
1189   for (int ut = 0; ut < ut_end; ut += 1) {
1190     unknown_ticks_array[ut] = 0;
1191   }
1192   region_flag[ThreadProfilerMark::classLoaderRegion] = false;
1193   class_loader_ticks = 0;
1194   region_flag[ThreadProfilerMark::extraRegion] = false;
1195   extra_ticks = 0;
1196   timer.start();
1197   interval_data_ref()->reset();
1198 }
1199 
1200 void ThreadProfiler::reset() {
1201   timer.stop();
1202   if (table != NULL) {
1203     for (int index = 0; index < table_size; index++) {
1204       ProfilerNode* n = table[index];
1205       if (n != NULL) {
1206         delete n;
1207       }
1208     }
1209   }
1210   initialize();
1211 }
1212 
1213 void FlatProfiler::allocate_table() {
1214   { // Bytecode table
1215     bytecode_ticks      = NEW_C_HEAP_ARRAY(int, Bytecodes::number_of_codes, mtInternal);
1216     bytecode_ticks_stub = NEW_C_HEAP_ARRAY(int, Bytecodes::number_of_codes, mtInternal);
1217     for(int index = 0; index < Bytecodes::number_of_codes; index++) {
1218       bytecode_ticks[index]      = 0;
1219       bytecode_ticks_stub[index] = 0;
1220     }
1221   }
1222 
1223   if (ProfilerRecordPC) PCRecorder::init();
1224 
1225   interval_data         = NEW_C_HEAP_ARRAY(IntervalData, interval_print_size, mtInternal);
1226   FlatProfiler::interval_reset();
1227 }
1228 
1229 void FlatProfiler::engage(JavaThread* mainThread, bool fullProfile) {
1230   full_profile_flag = fullProfile;
1231   if (bytecode_ticks == NULL) {
1232     allocate_table();
1233   }
1234   if(ProfileVM && (vm_thread_profiler == NULL)){
1235     vm_thread_profiler = new ThreadProfiler();
1236   }
1237   if (task == NULL) {
1238     task = new FlatProfilerTask(WatcherThread::delay_interval);
1239     task->enroll();
1240   }
1241   timer.start();
1242   if (mainThread != NULL) {
1243     // When mainThread was created, it might not have a ThreadProfiler
1244     ThreadProfiler* pp = mainThread->get_thread_profiler();
1245     if (pp == NULL) {
1246       mainThread->set_thread_profiler(new ThreadProfiler());
1247     } else {
1248       pp->reset();
1249     }
1250     mainThread->get_thread_profiler()->engage();
1251   }
1252   // This is where we would assign thread_profiler
1253   // if we wanted only one thread_profiler for all threads.
1254   thread_profiler = NULL;
1255 }
1256 
1257 void FlatProfiler::disengage() {
1258   if (!task) {
1259     return;
1260   }
1261   timer.stop();
1262   task->disenroll();
1263   delete task;
1264   task = NULL;
1265   if (thread_profiler != NULL) {
1266     thread_profiler->disengage();
1267   } else {
1268     MutexLocker tl(Threads_lock);
1269     for (JavaThread* tp = Threads::first(); tp != NULL; tp = tp->next()) {
1270       ThreadProfiler* pp = tp->get_thread_profiler();
1271       if (pp != NULL) {
1272         pp->disengage();
1273       }
1274     }
1275   }
1276 }
1277 
1278 void FlatProfiler::reset() {
1279   if (task) {
1280     disengage();
1281   }
1282 
1283   class_loader_ticks = 0;
1284   extra_ticks        = 0;
1285   received_gc_ticks  = 0;
1286   vm_operation_ticks = 0;
1287   compiler_ticks     = 0;
1288   deopt_ticks        = 0;
1289   interpreter_ticks  = 0;
1290   blocked_ticks      = 0;
1291   unknown_ticks      = 0;
1292   received_ticks     = 0;
1293   delivered_ticks    = 0;
1294   timer.stop();
1295 }
1296 
1297 bool FlatProfiler::is_active() {
1298   return task != NULL;
1299 }
1300 
1301 void FlatProfiler::print_byte_code_statistics() {
1302   GrowableArray <ProfilerNode*>* array = new GrowableArray<ProfilerNode*>(200);
1303 
1304   tty->print_cr(" Bytecode ticks:");
1305   for (int index = 0; index < Bytecodes::number_of_codes; index++) {
1306     if (FlatProfiler::bytecode_ticks[index] > 0 || FlatProfiler::bytecode_ticks_stub[index] > 0) {
1307       tty->print_cr("  %4d %4d = %s",
1308         FlatProfiler::bytecode_ticks[index],
1309         FlatProfiler::bytecode_ticks_stub[index],
1310         Bytecodes::name( (Bytecodes::Code) index));
1311     }
1312   }
1313   tty->cr();
1314 }
1315 
1316 void print_ticks(const char* title, int ticks, int total) {
1317   if (ticks > 0) {
1318     tty->print("%5.1f%% %5d", ticks * 100.0 / total, ticks);
1319     tty->fill_to(col3);
1320     tty->print("%s", title);
1321     tty->cr();
1322   }
1323 }
1324 
1325 void ThreadProfiler::print(const char* thread_name) {
1326   ResourceMark rm;
1327   MutexLocker ppl(ProfilePrint_lock);
1328   int index = 0; // Declared outside for loops for portability
1329 
1330   if (table == NULL) {
1331     return;
1332   }
1333 
1334   if (thread_ticks <= 0) {
1335     return;
1336   }
1337 
1338   const char* title = "too soon to tell";
1339   double secs = timer.seconds();
1340 
1341   GrowableArray <ProfilerNode*>* array = new GrowableArray<ProfilerNode*>(200);
1342   for(index = 0; index < table_size; index++) {
1343     for(ProfilerNode* node = table[index]; node; node = node->next())
1344       array->append(node);
1345   }
1346 
1347   array->sort(&ProfilerNode::compare);
1348 
1349   // compute total (sanity check)
1350   int active =
1351     class_loader_ticks +
1352     compiler_ticks +
1353     interpreter_ticks +
1354     unknown_ticks();
1355   for (index = 0; index < array->length(); index++) {
1356     active += array->at(index)->ticks.total();
1357   }
1358   int total = active + blocked_ticks;
1359 
1360   tty->cr();
1361   tty->print_cr("Flat profile of %3.2f secs (%d total ticks): %s", secs, total, thread_name);
1362   if (total != thread_ticks) {
1363     print_ticks("Lost ticks", thread_ticks-total, thread_ticks);
1364   }
1365   tty->cr();
1366 
1367   // print interpreted methods
1368   tick_counter interpreted_ticks;
1369   bool has_interpreted_ticks = false;
1370   int print_count = 0;
1371   for (index = 0; index < array->length(); index++) {
1372     ProfilerNode* n = array->at(index);
1373     if (n->is_interpreted()) {
1374       interpreted_ticks.add(&n->ticks);
1375       if (!has_interpreted_ticks) {
1376         interpretedNode::print_title(tty);
1377         has_interpreted_ticks = true;
1378       }
1379       if (print_count++ < ProfilerNumberOfInterpretedMethods) {
1380         n->print(tty, active);
1381       }
1382     }
1383   }
1384   if (has_interpreted_ticks) {
1385     if (print_count <= ProfilerNumberOfInterpretedMethods) {
1386       title = "Total interpreted";
1387     } else {
1388       title = "Total interpreted (including elided)";
1389     }
1390     interpretedNode::print_total(tty, &interpreted_ticks, active, title);
1391     tty->cr();
1392   }
1393 
1394   // print compiled methods
1395   tick_counter compiled_ticks;
1396   bool has_compiled_ticks = false;
1397   print_count = 0;
1398   for (index = 0; index < array->length(); index++) {
1399     ProfilerNode* n = array->at(index);
1400     if (n->is_compiled()) {
1401       compiled_ticks.add(&n->ticks);
1402       if (!has_compiled_ticks) {
1403         compiledNode::print_title(tty);
1404         has_compiled_ticks = true;
1405       }
1406       if (print_count++ < ProfilerNumberOfCompiledMethods) {
1407         n->print(tty, active);
1408       }
1409     }
1410   }
1411   if (has_compiled_ticks) {
1412     if (print_count <= ProfilerNumberOfCompiledMethods) {
1413       title = "Total compiled";
1414     } else {
1415       title = "Total compiled (including elided)";
1416     }
1417     compiledNode::print_total(tty, &compiled_ticks, active, title);
1418     tty->cr();
1419   }
1420 
1421   // print stub methods
1422   tick_counter stub_ticks;
1423   bool has_stub_ticks = false;
1424   print_count = 0;
1425   for (index = 0; index < array->length(); index++) {
1426     ProfilerNode* n = array->at(index);
1427     if (n->is_stub()) {
1428       stub_ticks.add(&n->ticks);
1429       if (!has_stub_ticks) {
1430         stubNode::print_title(tty);
1431         has_stub_ticks = true;
1432       }
1433       if (print_count++ < ProfilerNumberOfStubMethods) {
1434         n->print(tty, active);
1435       }
1436     }
1437   }
1438   if (has_stub_ticks) {
1439     if (print_count <= ProfilerNumberOfStubMethods) {
1440       title = "Total stub";
1441     } else {
1442       title = "Total stub (including elided)";
1443     }
1444     stubNode::print_total(tty, &stub_ticks, active, title);
1445     tty->cr();
1446   }
1447 
1448   // print runtime stubs
1449   tick_counter runtime_stub_ticks;
1450   bool has_runtime_stub_ticks = false;
1451   print_count = 0;
1452   for (index = 0; index < array->length(); index++) {
1453     ProfilerNode* n = array->at(index);
1454     if (n->is_runtime_stub()) {
1455       runtime_stub_ticks.add(&n->ticks);
1456       if (!has_runtime_stub_ticks) {
1457         runtimeStubNode::print_title(tty);
1458         has_runtime_stub_ticks = true;
1459       }
1460       if (print_count++ < ProfilerNumberOfRuntimeStubNodes) {
1461         n->print(tty, active);
1462       }
1463     }
1464   }
1465   if (has_runtime_stub_ticks) {
1466     if (print_count <= ProfilerNumberOfRuntimeStubNodes) {
1467       title = "Total runtime stubs";
1468     } else {
1469       title = "Total runtime stubs (including elided)";
1470     }
1471     runtimeStubNode::print_total(tty, &runtime_stub_ticks, active, title);
1472     tty->cr();
1473   }
1474 
1475   if (blocked_ticks + class_loader_ticks + interpreter_ticks + compiler_ticks + unknown_ticks() != 0) {
1476     tty->fill_to(col1);
1477     tty->print_cr("Thread-local ticks:");
1478     print_ticks("Blocked (of total)",  blocked_ticks,      total);
1479     print_ticks("Class loader",        class_loader_ticks, active);
1480     print_ticks("Extra",               extra_ticks,        active);
1481     print_ticks("Interpreter",         interpreter_ticks,  active);
1482     print_ticks("Compilation",         compiler_ticks,     active);
1483     print_ticks("Unknown: vtable stubs",  unknown_ticks_array[ut_vtable_stubs],         active);
1484     print_ticks("Unknown: null method",   unknown_ticks_array[ut_null_method],          active);
1485     print_ticks("Unknown: running frame", unknown_ticks_array[ut_running_frame],        active);
1486     print_ticks("Unknown: calling frame", unknown_ticks_array[ut_calling_frame],        active);
1487     print_ticks("Unknown: no pc",         unknown_ticks_array[ut_no_pc],                active);
1488     print_ticks("Unknown: no last frame", unknown_ticks_array[ut_no_last_Java_frame],   active);
1489     print_ticks("Unknown: thread_state",  unknown_ticks_array[ut_unknown_thread_state], active);
1490     tty->cr();
1491   }
1492 
1493   if (WizardMode) {
1494     tty->print_cr("Node area used: " INTX_FORMAT " Kb", (area_top - area_bottom) / 1024);
1495   }
1496   reset();
1497 }
1498 
1499 /*
1500 ThreadProfiler::print_unknown(){
1501   if (table == NULL) {
1502     return;
1503   }
1504 
1505   if (thread_ticks <= 0) {
1506     return;
1507   }
1508 } */
1509 
1510 void FlatProfiler::print(int unused) {
1511   ResourceMark rm;
1512   if (thread_profiler != NULL) {
1513     thread_profiler->print("All threads");
1514   } else {
1515     MutexLocker tl(Threads_lock);
1516     for (JavaThread* tp = Threads::first(); tp != NULL; tp = tp->next()) {
1517       ThreadProfiler* pp = tp->get_thread_profiler();
1518       if (pp != NULL) {
1519         pp->print(tp->get_thread_name());
1520       }
1521     }
1522   }
1523 
1524   if (ProfilerPrintByteCodeStatistics) {
1525     print_byte_code_statistics();
1526   }
1527 
1528   if (non_method_ticks() > 0) {
1529     tty->cr();
1530     tty->print_cr("Global summary of %3.2f seconds:", timer.seconds());
1531     print_ticks("Received ticks",      received_ticks,     received_ticks);
1532     print_ticks("Received GC ticks",   received_gc_ticks,  received_ticks);
1533     print_ticks("Compilation",         compiler_ticks,     received_ticks);
1534     print_ticks("Deoptimization",      deopt_ticks,        received_ticks);
1535     print_ticks("Other VM operations", vm_operation_ticks, received_ticks);
1536 #ifndef PRODUCT
1537     print_ticks("Blocked ticks",       blocked_ticks,      received_ticks);
1538     print_ticks("Threads_lock blocks", threads_lock_ticks, received_ticks);
1539     print_ticks("Delivered ticks",     delivered_ticks,    received_ticks);
1540     print_ticks("All ticks",           all_ticks,          received_ticks);
1541 #endif
1542     print_ticks("Class loader",        class_loader_ticks, received_ticks);
1543     print_ticks("Extra       ",        extra_ticks,        received_ticks);
1544     print_ticks("Interpreter",         interpreter_ticks,  received_ticks);
1545     print_ticks("Unknown code",        unknown_ticks,      received_ticks);
1546   }
1547 
1548   PCRecorder::print();
1549 
1550   if(ProfileVM){
1551     tty->cr();
1552     vm_thread_profiler->print("VM Thread");
1553   }
1554 }
1555 
1556 void IntervalData::print_header(outputStream* st) {
1557   st->print("i/c/n/g");
1558 }
1559 
1560 void IntervalData::print_data(outputStream* st) {
1561   st->print("%d/%d/%d/%d", interpreted(), compiled(), native(), compiling());
1562 }
1563 
1564 void FlatProfiler::interval_record_thread(ThreadProfiler* tp) {
1565   IntervalData id = tp->interval_data();
1566   int total = id.total();
1567   tp->interval_data_ref()->reset();
1568 
1569   // Insertion sort the data, if it's relevant.
1570   for (int i = 0; i < interval_print_size; i += 1) {
1571     if (total > interval_data[i].total()) {
1572       for (int j = interval_print_size - 1; j > i; j -= 1) {
1573         interval_data[j] = interval_data[j-1];
1574       }
1575       interval_data[i] = id;
1576       break;
1577     }
1578   }
1579 }
1580 
1581 void FlatProfiler::interval_print() {
1582   if ((interval_data[0].total() > 0)) {
1583     tty->stamp();
1584     tty->print("\t");
1585     IntervalData::print_header(tty);
1586     for (int i = 0; i < interval_print_size; i += 1) {
1587       if (interval_data[i].total() > 0) {
1588         tty->print("\t");
1589         interval_data[i].print_data(tty);
1590       }
1591     }
1592     tty->cr();
1593   }
1594 }
1595 
1596 void FlatProfiler::interval_reset() {
1597   for (int i = 0; i < interval_print_size; i += 1) {
1598     interval_data[i].reset();
1599   }
1600 }
1601 
1602 void ThreadProfiler::oops_do(OopClosure* f) {
1603   if (table == NULL) return;
1604 
1605   for(int index = 0; index < table_size; index++) {
1606     for(ProfilerNode* node = table[index]; node; node = node->next())
1607       node->oops_do(f);
1608   }
1609 }
1610 
1611 void FlatProfiler::oops_do(OopClosure* f) {
1612   if (thread_profiler != NULL) {
1613     thread_profiler->oops_do(f);
1614   } else {
1615     for (JavaThread* tp = Threads::first(); tp != NULL; tp = tp->next()) {
1616       ThreadProfiler* pp = tp->get_thread_profiler();
1617       if (pp != NULL) {
1618         pp->oops_do(f);
1619       }
1620     }
1621   }
1622 }