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