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