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