aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/classLoader.hpp" aoqi@0: #include "code/vtableStubs.hpp" aoqi@0: #include "gc_interface/collectedHeap.inline.hpp" aoqi@0: #include "interpreter/interpreter.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "memory/universe.inline.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "oops/oop.inline2.hpp" aoqi@0: #include "oops/symbol.hpp" aoqi@0: #include "runtime/deoptimization.hpp" aoqi@0: #include "runtime/fprofiler.hpp" aoqi@0: #include "runtime/mutexLocker.hpp" aoqi@0: #include "runtime/stubCodeGenerator.hpp" aoqi@0: #include "runtime/stubRoutines.hpp" aoqi@0: #include "runtime/task.hpp" goetz@6911: #include "runtime/thread.inline.hpp" aoqi@0: #include "runtime/vframe.hpp" aoqi@0: #include "utilities/macros.hpp" aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: // Static fields of FlatProfiler aoqi@0: int FlatProfiler::received_gc_ticks = 0; aoqi@0: int FlatProfiler::vm_operation_ticks = 0; aoqi@0: int FlatProfiler::threads_lock_ticks = 0; aoqi@0: int FlatProfiler::class_loader_ticks = 0; aoqi@0: int FlatProfiler::extra_ticks = 0; aoqi@0: int FlatProfiler::blocked_ticks = 0; aoqi@0: int FlatProfiler::deopt_ticks = 0; aoqi@0: int FlatProfiler::unknown_ticks = 0; aoqi@0: int FlatProfiler::interpreter_ticks = 0; aoqi@0: int FlatProfiler::compiler_ticks = 0; aoqi@0: int FlatProfiler::received_ticks = 0; aoqi@0: int FlatProfiler::delivered_ticks = 0; aoqi@0: int* FlatProfiler::bytecode_ticks = NULL; aoqi@0: int* FlatProfiler::bytecode_ticks_stub = NULL; aoqi@0: int FlatProfiler::all_int_ticks = 0; aoqi@0: int FlatProfiler::all_comp_ticks = 0; aoqi@0: int FlatProfiler::all_ticks = 0; aoqi@0: bool FlatProfiler::full_profile_flag = false; aoqi@0: ThreadProfiler* FlatProfiler::thread_profiler = NULL; aoqi@0: ThreadProfiler* FlatProfiler::vm_thread_profiler = NULL; aoqi@0: FlatProfilerTask* FlatProfiler::task = NULL; aoqi@0: elapsedTimer FlatProfiler::timer; aoqi@0: int FlatProfiler::interval_ticks_previous = 0; aoqi@0: IntervalData* FlatProfiler::interval_data = NULL; aoqi@0: aoqi@0: ThreadProfiler::ThreadProfiler() { aoqi@0: // Space for the ProfilerNodes aoqi@0: const int area_size = 1 * ProfilerNodeSize * 1024; aoqi@0: area_bottom = AllocateHeap(area_size, mtInternal); aoqi@0: area_top = area_bottom; aoqi@0: area_limit = area_bottom + area_size; aoqi@0: aoqi@0: // ProfilerNode pointer table aoqi@0: table = NEW_C_HEAP_ARRAY(ProfilerNode*, table_size, mtInternal); aoqi@0: initialize(); aoqi@0: engaged = false; aoqi@0: } aoqi@0: aoqi@0: ThreadProfiler::~ThreadProfiler() { aoqi@0: FreeHeap(area_bottom); aoqi@0: area_bottom = NULL; aoqi@0: area_top = NULL; aoqi@0: area_limit = NULL; aoqi@0: FreeHeap(table); aoqi@0: table = NULL; aoqi@0: } aoqi@0: aoqi@0: // Statics for ThreadProfiler aoqi@0: int ThreadProfiler::table_size = 1024; aoqi@0: aoqi@0: int ThreadProfiler::entry(int value) { aoqi@0: value = (value > 0) ? value : -value; aoqi@0: return value % table_size; aoqi@0: } aoqi@0: aoqi@0: ThreadProfilerMark::ThreadProfilerMark(ThreadProfilerMark::Region r) { aoqi@0: _r = r; aoqi@0: _pp = NULL; aoqi@0: assert(((r > ThreadProfilerMark::noRegion) && (r < ThreadProfilerMark::maxRegion)), "ThreadProfilerMark::Region out of bounds"); aoqi@0: Thread* tp = Thread::current(); aoqi@0: if (tp != NULL && tp->is_Java_thread()) { aoqi@0: JavaThread* jtp = (JavaThread*) tp; aoqi@0: ThreadProfiler* pp = jtp->get_thread_profiler(); aoqi@0: _pp = pp; aoqi@0: if (pp != NULL) { aoqi@0: pp->region_flag[r] = true; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: ThreadProfilerMark::~ThreadProfilerMark() { aoqi@0: if (_pp != NULL) { aoqi@0: _pp->region_flag[_r] = false; aoqi@0: } aoqi@0: _pp = NULL; aoqi@0: } aoqi@0: aoqi@0: // Random other statics aoqi@0: static const int col1 = 2; // position of output column 1 aoqi@0: static const int col2 = 11; // position of output column 2 aoqi@0: static const int col3 = 25; // position of output column 3 aoqi@0: static const int col4 = 55; // position of output column 4 aoqi@0: aoqi@0: aoqi@0: // Used for detailed profiling of nmethods. aoqi@0: class PCRecorder : AllStatic { aoqi@0: private: aoqi@0: static int* counters; aoqi@0: static address base; aoqi@0: enum { aoqi@0: bucket_size = 16 aoqi@0: }; aoqi@0: static int index_for(address pc) { return (pc - base)/bucket_size; } aoqi@0: static address pc_for(int index) { return base + (index * bucket_size); } aoqi@0: static int size() { aoqi@0: return ((int)CodeCache::max_capacity())/bucket_size * BytesPerWord; aoqi@0: } aoqi@0: public: aoqi@0: static address bucket_start_for(address pc) { aoqi@0: if (counters == NULL) return NULL; aoqi@0: return pc_for(index_for(pc)); aoqi@0: } aoqi@0: static int bucket_count_for(address pc) { return counters[index_for(pc)]; } aoqi@0: static void init(); aoqi@0: static void record(address pc); aoqi@0: static void print(); aoqi@0: static void print_blobs(CodeBlob* cb); aoqi@0: }; aoqi@0: aoqi@0: int* PCRecorder::counters = NULL; aoqi@0: address PCRecorder::base = NULL; aoqi@0: aoqi@0: void PCRecorder::init() { aoqi@0: MutexLockerEx lm(CodeCache_lock, Mutex::_no_safepoint_check_flag); aoqi@0: int s = size(); aoqi@0: counters = NEW_C_HEAP_ARRAY(int, s, mtInternal); aoqi@0: for (int index = 0; index < s; index++) { aoqi@0: counters[index] = 0; aoqi@0: } aoqi@0: base = CodeCache::first_address(); aoqi@0: } aoqi@0: aoqi@0: void PCRecorder::record(address pc) { aoqi@0: if (counters == NULL) return; aoqi@0: assert(CodeCache::contains(pc), "must be in CodeCache"); aoqi@0: counters[index_for(pc)]++; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: address FlatProfiler::bucket_start_for(address pc) { aoqi@0: return PCRecorder::bucket_start_for(pc); aoqi@0: } aoqi@0: aoqi@0: int FlatProfiler::bucket_count_for(address pc) { aoqi@0: return PCRecorder::bucket_count_for(pc); aoqi@0: } aoqi@0: aoqi@0: void PCRecorder::print() { aoqi@0: if (counters == NULL) return; aoqi@0: aoqi@0: tty->cr(); aoqi@0: tty->print_cr("Printing compiled methods with PC buckets having more than %d ticks", ProfilerPCTickThreshold); aoqi@0: tty->print_cr("==================================================================="); aoqi@0: tty->cr(); aoqi@0: aoqi@0: GrowableArray* candidates = new GrowableArray(20); aoqi@0: aoqi@0: aoqi@0: int s; aoqi@0: { aoqi@0: MutexLockerEx lm(CodeCache_lock, Mutex::_no_safepoint_check_flag); aoqi@0: s = size(); aoqi@0: } aoqi@0: aoqi@0: for (int index = 0; index < s; index++) { aoqi@0: int count = counters[index]; aoqi@0: if (count > ProfilerPCTickThreshold) { aoqi@0: address pc = pc_for(index); aoqi@0: CodeBlob* cb = CodeCache::find_blob_unsafe(pc); aoqi@0: if (cb != NULL && candidates->find(cb) < 0) { aoqi@0: candidates->push(cb); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: for (int i = 0; i < candidates->length(); i++) { aoqi@0: print_blobs(candidates->at(i)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void PCRecorder::print_blobs(CodeBlob* cb) { aoqi@0: if (cb != NULL) { aoqi@0: cb->print(); aoqi@0: if (cb->is_nmethod()) { aoqi@0: ((nmethod*)cb)->print_code(); aoqi@0: } aoqi@0: tty->cr(); aoqi@0: } else { aoqi@0: tty->print_cr("stub code"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: class tick_counter { // holds tick info for one node aoqi@0: public: aoqi@0: int ticks_in_code; aoqi@0: int ticks_in_native; aoqi@0: aoqi@0: tick_counter() { ticks_in_code = ticks_in_native = 0; } aoqi@0: tick_counter(int code, int native) { ticks_in_code = code; ticks_in_native = native; } aoqi@0: aoqi@0: int total() const { aoqi@0: return (ticks_in_code + ticks_in_native); aoqi@0: } aoqi@0: aoqi@0: void add(tick_counter* a) { aoqi@0: ticks_in_code += a->ticks_in_code; aoqi@0: ticks_in_native += a->ticks_in_native; aoqi@0: } aoqi@0: aoqi@0: void update(TickPosition where) { aoqi@0: switch(where) { aoqi@0: case tp_code: ticks_in_code++; break; aoqi@0: case tp_native: ticks_in_native++; break; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void print_code(outputStream* st, int total_ticks) { aoqi@0: st->print("%5.1f%% %5d ", total() * 100.0 / total_ticks, ticks_in_code); aoqi@0: } aoqi@0: aoqi@0: void print_native(outputStream* st) { aoqi@0: st->print(" + %5d ", ticks_in_native); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: class ProfilerNode { aoqi@0: private: aoqi@0: ProfilerNode* _next; aoqi@0: public: aoqi@0: tick_counter ticks; aoqi@0: aoqi@0: public: aoqi@0: aoqi@0: void* operator new(size_t size, ThreadProfiler* tp) throw(); aoqi@0: void operator delete(void* p); aoqi@0: aoqi@0: ProfilerNode() { aoqi@0: _next = NULL; aoqi@0: } aoqi@0: aoqi@0: virtual ~ProfilerNode() { aoqi@0: if (_next) aoqi@0: delete _next; aoqi@0: } aoqi@0: aoqi@0: void set_next(ProfilerNode* n) { _next = n; } aoqi@0: ProfilerNode* next() { return _next; } aoqi@0: aoqi@0: void update(TickPosition where) { ticks.update(where);} aoqi@0: int total_ticks() { return ticks.total(); } aoqi@0: aoqi@0: virtual bool is_interpreted() const { return false; } aoqi@0: virtual bool is_compiled() const { return false; } aoqi@0: virtual bool is_stub() const { return false; } aoqi@0: virtual bool is_runtime_stub() const{ return false; } aoqi@0: virtual void oops_do(OopClosure* f) = 0; aoqi@0: aoqi@0: virtual bool interpreted_match(Method* m) const { return false; } aoqi@0: virtual bool compiled_match(Method* m ) const { return false; } aoqi@0: virtual bool stub_match(Method* m, const char* name) const { return false; } aoqi@0: virtual bool adapter_match() const { return false; } aoqi@0: virtual bool runtimeStub_match(const CodeBlob* stub, const char* name) const { return false; } aoqi@0: virtual bool unknown_compiled_match(const CodeBlob* cb) const { return false; } aoqi@0: aoqi@0: static void print_title(outputStream* st) { aoqi@0: st->print(" + native"); aoqi@0: st->fill_to(col3); aoqi@0: st->print("Method"); aoqi@0: st->fill_to(col4); aoqi@0: st->cr(); aoqi@0: } aoqi@0: aoqi@0: static void print_total(outputStream* st, tick_counter* t, int total, const char* msg) { aoqi@0: t->print_code(st, total); aoqi@0: st->fill_to(col2); aoqi@0: t->print_native(st); aoqi@0: st->fill_to(col3); aoqi@0: st->print("%s", msg); aoqi@0: st->cr(); aoqi@0: } aoqi@0: aoqi@0: virtual Method* method() = 0; aoqi@0: aoqi@0: virtual void print_method_on(outputStream* st) { aoqi@0: int limit; aoqi@0: int i; aoqi@0: Method* m = method(); aoqi@0: Symbol* k = m->klass_name(); aoqi@0: // Print the class name with dots instead of slashes aoqi@0: limit = k->utf8_length(); aoqi@0: for (i = 0 ; i < limit ; i += 1) { aoqi@0: char c = (char) k->byte_at(i); aoqi@0: if (c == '/') { aoqi@0: c = '.'; aoqi@0: } aoqi@0: st->print("%c", c); aoqi@0: } aoqi@0: if (limit > 0) { aoqi@0: st->print("."); aoqi@0: } aoqi@0: Symbol* n = m->name(); aoqi@0: limit = n->utf8_length(); aoqi@0: for (i = 0 ; i < limit ; i += 1) { aoqi@0: char c = (char) n->byte_at(i); aoqi@0: st->print("%c", c); aoqi@0: } aoqi@0: if (Verbose || WizardMode) { aoqi@0: // Disambiguate overloaded methods aoqi@0: Symbol* sig = m->signature(); aoqi@0: sig->print_symbol_on(st); aoqi@0: } else if (MethodHandles::is_signature_polymorphic(m->intrinsic_id())) aoqi@0: // compare with Method::print_short_name aoqi@0: MethodHandles::print_as_basic_type_signature_on(st, m->signature(), true); aoqi@0: } aoqi@0: aoqi@0: virtual void print(outputStream* st, int total_ticks) { aoqi@0: ticks.print_code(st, total_ticks); aoqi@0: st->fill_to(col2); aoqi@0: ticks.print_native(st); aoqi@0: st->fill_to(col3); aoqi@0: print_method_on(st); aoqi@0: st->cr(); aoqi@0: } aoqi@0: aoqi@0: // for hashing into the table aoqi@0: static int hash(Method* method) { aoqi@0: // The point here is to try to make something fairly unique aoqi@0: // out of the fields we can read without grabbing any locks aoqi@0: // since the method may be locked when we need the hash. aoqi@0: return ( aoqi@0: method->code_size() ^ aoqi@0: method->max_stack() ^ aoqi@0: method->max_locals() ^ aoqi@0: method->size_of_parameters()); aoqi@0: } aoqi@0: aoqi@0: // for sorting aoqi@0: static int compare(ProfilerNode** a, ProfilerNode** b) { aoqi@0: return (*b)->total_ticks() - (*a)->total_ticks(); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: void* ProfilerNode::operator new(size_t size, ThreadProfiler* tp) throw() { aoqi@0: void* result = (void*) tp->area_top; aoqi@0: tp->area_top += size; aoqi@0: aoqi@0: if (tp->area_top > tp->area_limit) { aoqi@0: fatal("flat profiler buffer overflow"); aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: void ProfilerNode::operator delete(void* p){ aoqi@0: } aoqi@0: aoqi@0: class interpretedNode : public ProfilerNode { aoqi@0: private: aoqi@0: Method* _method; aoqi@0: oop _class_loader; // needed to keep metadata for the method alive aoqi@0: public: aoqi@0: interpretedNode(Method* method, TickPosition where) : ProfilerNode() { aoqi@0: _method = method; aoqi@0: _class_loader = method->method_holder()->class_loader(); aoqi@0: update(where); aoqi@0: } aoqi@0: aoqi@0: bool is_interpreted() const { return true; } aoqi@0: aoqi@0: bool interpreted_match(Method* m) const { aoqi@0: return _method == m; aoqi@0: } aoqi@0: aoqi@0: void oops_do(OopClosure* f) { aoqi@0: f->do_oop(&_class_loader); aoqi@0: } aoqi@0: aoqi@0: Method* method() { return _method; } aoqi@0: aoqi@0: static void print_title(outputStream* st) { aoqi@0: st->fill_to(col1); aoqi@0: st->print("%11s", "Interpreted"); aoqi@0: ProfilerNode::print_title(st); aoqi@0: } aoqi@0: aoqi@0: void print(outputStream* st, int total_ticks) { aoqi@0: ProfilerNode::print(st, total_ticks); aoqi@0: } aoqi@0: aoqi@0: void print_method_on(outputStream* st) { aoqi@0: ProfilerNode::print_method_on(st); aoqi@0: MethodCounters* mcs = method()->method_counters(); aoqi@0: if (Verbose && mcs != NULL) mcs->invocation_counter()->print_short(); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: class compiledNode : public ProfilerNode { aoqi@0: private: aoqi@0: Method* _method; aoqi@0: oop _class_loader; // needed to keep metadata for the method alive aoqi@0: public: aoqi@0: compiledNode(Method* method, TickPosition where) : ProfilerNode() { aoqi@0: _method = method; aoqi@0: _class_loader = method->method_holder()->class_loader(); aoqi@0: update(where); aoqi@0: } aoqi@0: bool is_compiled() const { return true; } aoqi@0: aoqi@0: bool compiled_match(Method* m) const { aoqi@0: return _method == m; aoqi@0: } aoqi@0: aoqi@0: Method* method() { return _method; } aoqi@0: aoqi@0: void oops_do(OopClosure* f) { aoqi@0: f->do_oop(&_class_loader); aoqi@0: } aoqi@0: aoqi@0: static void print_title(outputStream* st) { aoqi@0: st->fill_to(col1); aoqi@0: st->print("%11s", "Compiled"); aoqi@0: ProfilerNode::print_title(st); aoqi@0: } aoqi@0: aoqi@0: void print(outputStream* st, int total_ticks) { aoqi@0: ProfilerNode::print(st, total_ticks); aoqi@0: } aoqi@0: aoqi@0: void print_method_on(outputStream* st) { aoqi@0: ProfilerNode::print_method_on(st); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: class stubNode : public ProfilerNode { aoqi@0: private: aoqi@0: Method* _method; aoqi@0: oop _class_loader; // needed to keep metadata for the method alive aoqi@0: const char* _symbol; // The name of the nearest VM symbol (for +ProfileVM). Points to a unique string aoqi@0: public: aoqi@0: stubNode(Method* method, const char* name, TickPosition where) : ProfilerNode() { aoqi@0: _method = method; aoqi@0: _class_loader = method->method_holder()->class_loader(); aoqi@0: _symbol = name; aoqi@0: update(where); aoqi@0: } aoqi@0: aoqi@0: bool is_stub() const { return true; } aoqi@0: aoqi@0: void oops_do(OopClosure* f) { aoqi@0: f->do_oop(&_class_loader); aoqi@0: } aoqi@0: aoqi@0: bool stub_match(Method* m, const char* name) const { aoqi@0: return (_method == m) && (_symbol == name); aoqi@0: } aoqi@0: aoqi@0: Method* method() { return _method; } aoqi@0: aoqi@0: static void print_title(outputStream* st) { aoqi@0: st->fill_to(col1); aoqi@0: st->print("%11s", "Stub"); aoqi@0: ProfilerNode::print_title(st); aoqi@0: } aoqi@0: aoqi@0: void print(outputStream* st, int total_ticks) { aoqi@0: ProfilerNode::print(st, total_ticks); aoqi@0: } aoqi@0: aoqi@0: void print_method_on(outputStream* st) { aoqi@0: ProfilerNode::print_method_on(st); aoqi@0: print_symbol_on(st); aoqi@0: } aoqi@0: aoqi@0: void print_symbol_on(outputStream* st) { aoqi@0: if(_symbol) { aoqi@0: st->print(" (%s)", _symbol); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: class adapterNode : public ProfilerNode { aoqi@0: public: aoqi@0: adapterNode(TickPosition where) : ProfilerNode() { aoqi@0: update(where); aoqi@0: } aoqi@0: bool is_compiled() const { return true; } aoqi@0: aoqi@0: bool adapter_match() const { return true; } aoqi@0: aoqi@0: Method* method() { return NULL; } aoqi@0: aoqi@0: void oops_do(OopClosure* f) { aoqi@0: ; aoqi@0: } aoqi@0: aoqi@0: void print(outputStream* st, int total_ticks) { aoqi@0: ProfilerNode::print(st, total_ticks); aoqi@0: } aoqi@0: aoqi@0: void print_method_on(outputStream* st) { aoqi@0: st->print("%s", "adapters"); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: class runtimeStubNode : public ProfilerNode { aoqi@0: private: aoqi@0: const CodeBlob* _stub; aoqi@0: const char* _symbol; // The name of the nearest VM symbol when ProfileVM is on. Points to a unique string. aoqi@0: public: aoqi@0: runtimeStubNode(const CodeBlob* stub, const char* name, TickPosition where) : ProfilerNode(), _stub(stub), _symbol(name) { aoqi@0: assert(stub->is_runtime_stub(), "wrong code blob"); aoqi@0: update(where); aoqi@0: } aoqi@0: aoqi@0: bool is_runtime_stub() const { return true; } aoqi@0: aoqi@0: bool runtimeStub_match(const CodeBlob* stub, const char* name) const { aoqi@0: assert(stub->is_runtime_stub(), "wrong code blob"); aoqi@0: return ((RuntimeStub*)_stub)->entry_point() == ((RuntimeStub*)stub)->entry_point() && aoqi@0: (_symbol == name); aoqi@0: } aoqi@0: aoqi@0: Method* method() { return NULL; } aoqi@0: aoqi@0: static void print_title(outputStream* st) { aoqi@0: st->fill_to(col1); aoqi@0: st->print("%11s", "Runtime stub"); aoqi@0: ProfilerNode::print_title(st); aoqi@0: } aoqi@0: aoqi@0: void oops_do(OopClosure* f) { aoqi@0: ; aoqi@0: } aoqi@0: aoqi@0: void print(outputStream* st, int total_ticks) { aoqi@0: ProfilerNode::print(st, total_ticks); aoqi@0: } aoqi@0: aoqi@0: void print_method_on(outputStream* st) { aoqi@0: st->print("%s", ((RuntimeStub*)_stub)->name()); aoqi@0: print_symbol_on(st); aoqi@0: } aoqi@0: aoqi@0: void print_symbol_on(outputStream* st) { aoqi@0: if(_symbol) { aoqi@0: st->print(" (%s)", _symbol); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: class unknown_compiledNode : public ProfilerNode { aoqi@0: const char *_name; aoqi@0: public: aoqi@0: unknown_compiledNode(const CodeBlob* cb, TickPosition where) : ProfilerNode() { aoqi@0: if ( cb->is_buffer_blob() ) aoqi@0: _name = ((BufferBlob*)cb)->name(); aoqi@0: else aoqi@0: _name = ((SingletonBlob*)cb)->name(); aoqi@0: update(where); aoqi@0: } aoqi@0: bool is_compiled() const { return true; } aoqi@0: aoqi@0: bool unknown_compiled_match(const CodeBlob* cb) const { aoqi@0: if ( cb->is_buffer_blob() ) aoqi@0: return !strcmp(((BufferBlob*)cb)->name(), _name); aoqi@0: else aoqi@0: return !strcmp(((SingletonBlob*)cb)->name(), _name); aoqi@0: } aoqi@0: aoqi@0: Method* method() { return NULL; } aoqi@0: aoqi@0: void oops_do(OopClosure* f) { aoqi@0: ; aoqi@0: } aoqi@0: aoqi@0: void print(outputStream* st, int total_ticks) { aoqi@0: ProfilerNode::print(st, total_ticks); aoqi@0: } aoqi@0: aoqi@0: void print_method_on(outputStream* st) { aoqi@0: st->print("%s", _name); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: class vmNode : public ProfilerNode { aoqi@0: private: aoqi@0: const char* _name; // "optional" name obtained by os means such as dll lookup aoqi@0: public: aoqi@0: vmNode(const TickPosition where) : ProfilerNode() { aoqi@0: _name = NULL; aoqi@0: update(where); aoqi@0: } aoqi@0: aoqi@0: vmNode(const char* name, const TickPosition where) : ProfilerNode() { aoqi@0: _name = name; aoqi@0: update(where); aoqi@0: } aoqi@0: aoqi@0: const char *name() const { return _name; } aoqi@0: bool is_compiled() const { return true; } aoqi@0: aoqi@0: bool vm_match(const char* name) const { return strcmp(name, _name) == 0; } aoqi@0: aoqi@0: Method* method() { return NULL; } aoqi@0: aoqi@0: static int hash(const char* name){ aoqi@0: // Compute a simple hash aoqi@0: const char* cp = name; aoqi@0: int h = 0; aoqi@0: aoqi@0: if(name != NULL){ aoqi@0: while(*cp != '\0'){ aoqi@0: h = (h << 1) ^ *cp; aoqi@0: cp++; aoqi@0: } aoqi@0: } aoqi@0: return h; aoqi@0: } aoqi@0: aoqi@0: void oops_do(OopClosure* f) { aoqi@0: ; aoqi@0: } aoqi@0: aoqi@0: void print(outputStream* st, int total_ticks) { aoqi@0: ProfilerNode::print(st, total_ticks); aoqi@0: } aoqi@0: aoqi@0: void print_method_on(outputStream* st) { aoqi@0: if(_name==NULL){ aoqi@0: st->print("%s", "unknown code"); aoqi@0: } aoqi@0: else { aoqi@0: st->print("%s", _name); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: void ThreadProfiler::interpreted_update(Method* method, TickPosition where) { aoqi@0: int index = entry(ProfilerNode::hash(method)); aoqi@0: if (!table[index]) { aoqi@0: table[index] = new (this) interpretedNode(method, where); aoqi@0: } else { aoqi@0: ProfilerNode* prev = table[index]; aoqi@0: for(ProfilerNode* node = prev; node; node = node->next()) { aoqi@0: if (node->interpreted_match(method)) { aoqi@0: node->update(where); aoqi@0: return; aoqi@0: } aoqi@0: prev = node; aoqi@0: } aoqi@0: prev->set_next(new (this) interpretedNode(method, where)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void ThreadProfiler::compiled_update(Method* method, TickPosition where) { aoqi@0: int index = entry(ProfilerNode::hash(method)); aoqi@0: if (!table[index]) { aoqi@0: table[index] = new (this) compiledNode(method, where); aoqi@0: } else { aoqi@0: ProfilerNode* prev = table[index]; aoqi@0: for(ProfilerNode* node = prev; node; node = node->next()) { aoqi@0: if (node->compiled_match(method)) { aoqi@0: node->update(where); aoqi@0: return; aoqi@0: } aoqi@0: prev = node; aoqi@0: } aoqi@0: prev->set_next(new (this) compiledNode(method, where)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void ThreadProfiler::stub_update(Method* method, const char* name, TickPosition where) { aoqi@0: int index = entry(ProfilerNode::hash(method)); aoqi@0: if (!table[index]) { aoqi@0: table[index] = new (this) stubNode(method, name, where); aoqi@0: } else { aoqi@0: ProfilerNode* prev = table[index]; aoqi@0: for(ProfilerNode* node = prev; node; node = node->next()) { aoqi@0: if (node->stub_match(method, name)) { aoqi@0: node->update(where); aoqi@0: return; aoqi@0: } aoqi@0: prev = node; aoqi@0: } aoqi@0: prev->set_next(new (this) stubNode(method, name, where)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void ThreadProfiler::adapter_update(TickPosition where) { aoqi@0: int index = 0; aoqi@0: if (!table[index]) { aoqi@0: table[index] = new (this) adapterNode(where); aoqi@0: } else { aoqi@0: ProfilerNode* prev = table[index]; aoqi@0: for(ProfilerNode* node = prev; node; node = node->next()) { aoqi@0: if (node->adapter_match()) { aoqi@0: node->update(where); aoqi@0: return; aoqi@0: } aoqi@0: prev = node; aoqi@0: } aoqi@0: prev->set_next(new (this) adapterNode(where)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void ThreadProfiler::runtime_stub_update(const CodeBlob* stub, const char* name, TickPosition where) { aoqi@0: int index = 0; aoqi@0: if (!table[index]) { aoqi@0: table[index] = new (this) runtimeStubNode(stub, name, where); aoqi@0: } else { aoqi@0: ProfilerNode* prev = table[index]; aoqi@0: for(ProfilerNode* node = prev; node; node = node->next()) { aoqi@0: if (node->runtimeStub_match(stub, name)) { aoqi@0: node->update(where); aoqi@0: return; aoqi@0: } aoqi@0: prev = node; aoqi@0: } aoqi@0: prev->set_next(new (this) runtimeStubNode(stub, name, where)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void ThreadProfiler::unknown_compiled_update(const CodeBlob* cb, TickPosition where) { aoqi@0: int index = 0; aoqi@0: if (!table[index]) { aoqi@0: table[index] = new (this) unknown_compiledNode(cb, where); aoqi@0: } else { aoqi@0: ProfilerNode* prev = table[index]; aoqi@0: for(ProfilerNode* node = prev; node; node = node->next()) { aoqi@0: if (node->unknown_compiled_match(cb)) { aoqi@0: node->update(where); aoqi@0: return; aoqi@0: } aoqi@0: prev = node; aoqi@0: } aoqi@0: prev->set_next(new (this) unknown_compiledNode(cb, where)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void ThreadProfiler::vm_update(TickPosition where) { aoqi@0: vm_update(NULL, where); aoqi@0: } aoqi@0: aoqi@0: void ThreadProfiler::vm_update(const char* name, TickPosition where) { aoqi@0: int index = entry(vmNode::hash(name)); aoqi@0: assert(index >= 0, "Must be positive"); aoqi@0: // Note that we call strdup below since the symbol may be resource allocated aoqi@0: if (!table[index]) { aoqi@0: table[index] = new (this) vmNode(os::strdup(name), where); aoqi@0: } else { aoqi@0: ProfilerNode* prev = table[index]; aoqi@0: for(ProfilerNode* node = prev; node; node = node->next()) { aoqi@0: if (((vmNode *)node)->vm_match(name)) { aoqi@0: node->update(where); aoqi@0: return; aoqi@0: } aoqi@0: prev = node; aoqi@0: } aoqi@0: prev->set_next(new (this) vmNode(os::strdup(name), where)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: class FlatProfilerTask : public PeriodicTask { aoqi@0: public: aoqi@0: FlatProfilerTask(int interval_time) : PeriodicTask(interval_time) {} aoqi@0: void task(); aoqi@0: }; aoqi@0: aoqi@0: void FlatProfiler::record_vm_operation() { aoqi@0: if (Universe::heap()->is_gc_active()) { aoqi@0: FlatProfiler::received_gc_ticks += 1; aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: if (DeoptimizationMarker::is_active()) { aoqi@0: FlatProfiler::deopt_ticks += 1; aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: FlatProfiler::vm_operation_ticks += 1; aoqi@0: } aoqi@0: aoqi@0: void FlatProfiler::record_vm_tick() { aoqi@0: // Profile the VM Thread itself if needed aoqi@0: // This is done without getting the Threads_lock and we can go deep aoqi@0: // inside Safepoint, etc. aoqi@0: if( ProfileVM ) { aoqi@0: ResourceMark rm; aoqi@0: ExtendedPC epc; aoqi@0: const char *name = NULL; aoqi@0: char buf[256]; aoqi@0: buf[0] = '\0'; aoqi@0: aoqi@0: vm_thread_profiler->inc_thread_ticks(); aoqi@0: aoqi@0: // Get a snapshot of a current VMThread pc (and leave it running!) aoqi@0: // The call may fail if, for instance the VM thread is interrupted while aoqi@0: // holding the Interrupt_lock or for other reasons. aoqi@0: epc = os::get_thread_pc(VMThread::vm_thread()); aoqi@0: if(epc.pc() != NULL) { aoqi@0: if (os::dll_address_to_function_name(epc.pc(), buf, sizeof(buf), NULL)) { aoqi@0: name = buf; aoqi@0: } aoqi@0: } aoqi@0: if (name != NULL) { aoqi@0: vm_thread_profiler->vm_update(name, tp_native); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void FlatProfiler::record_thread_ticks() { aoqi@0: aoqi@0: int maxthreads, suspendedthreadcount; aoqi@0: JavaThread** threadsList; aoqi@0: bool interval_expired = false; aoqi@0: aoqi@0: if (ProfileIntervals && aoqi@0: (FlatProfiler::received_ticks >= interval_ticks_previous + ProfileIntervalsTicks)) { aoqi@0: interval_expired = true; aoqi@0: interval_ticks_previous = FlatProfiler::received_ticks; aoqi@0: } aoqi@0: aoqi@0: // Try not to wait for the Threads_lock aoqi@0: if (Threads_lock->try_lock()) { aoqi@0: { // Threads_lock scope aoqi@0: maxthreads = Threads::number_of_threads(); aoqi@0: threadsList = NEW_C_HEAP_ARRAY(JavaThread *, maxthreads, mtInternal); aoqi@0: suspendedthreadcount = 0; aoqi@0: for (JavaThread* tp = Threads::first(); tp != NULL; tp = tp->next()) { aoqi@0: if (tp->is_Compiler_thread()) { aoqi@0: // Only record ticks for active compiler threads aoqi@0: CompilerThread* cthread = (CompilerThread*)tp; aoqi@0: if (cthread->task() != NULL) { aoqi@0: // The compiler is active. If we need to access any of the fields aoqi@0: // of the compiler task we should suspend the CompilerThread first. aoqi@0: FlatProfiler::compiler_ticks += 1; aoqi@0: continue; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // First externally suspend all threads by marking each for aoqi@0: // external suspension - so it will stop at its next transition aoqi@0: // Then do a safepoint aoqi@0: ThreadProfiler* pp = tp->get_thread_profiler(); aoqi@0: if (pp != NULL && pp->engaged) { aoqi@0: MutexLockerEx ml(tp->SR_lock(), Mutex::_no_safepoint_check_flag); aoqi@0: if (!tp->is_external_suspend() && !tp->is_exiting()) { aoqi@0: tp->set_external_suspend(); aoqi@0: threadsList[suspendedthreadcount++] = tp; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: Threads_lock->unlock(); aoqi@0: } aoqi@0: // Suspend each thread. This call should just return aoqi@0: // for any threads that have already self-suspended aoqi@0: // Net result should be one safepoint aoqi@0: for (int j = 0; j < suspendedthreadcount; j++) { aoqi@0: JavaThread *tp = threadsList[j]; aoqi@0: if (tp) { aoqi@0: tp->java_suspend(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // We are responsible for resuming any thread on this list aoqi@0: for (int i = 0; i < suspendedthreadcount; i++) { aoqi@0: JavaThread *tp = threadsList[i]; aoqi@0: if (tp) { aoqi@0: ThreadProfiler* pp = tp->get_thread_profiler(); aoqi@0: if (pp != NULL && pp->engaged) { aoqi@0: HandleMark hm; aoqi@0: FlatProfiler::delivered_ticks += 1; aoqi@0: if (interval_expired) { aoqi@0: FlatProfiler::interval_record_thread(pp); aoqi@0: } aoqi@0: // This is the place where we check to see if a user thread is aoqi@0: // blocked waiting for compilation. aoqi@0: if (tp->blocked_on_compilation()) { aoqi@0: pp->compiler_ticks += 1; aoqi@0: pp->interval_data_ref()->inc_compiling(); aoqi@0: } else { aoqi@0: pp->record_tick(tp); aoqi@0: } aoqi@0: } aoqi@0: MutexLocker ml(Threads_lock); aoqi@0: tp->java_resume(); aoqi@0: } aoqi@0: } aoqi@0: if (interval_expired) { aoqi@0: FlatProfiler::interval_print(); aoqi@0: FlatProfiler::interval_reset(); aoqi@0: } aoqi@0: aoqi@0: FREE_C_HEAP_ARRAY(JavaThread *, threadsList, mtInternal); aoqi@0: } else { aoqi@0: // Couldn't get the threads lock, just record that rather than blocking aoqi@0: FlatProfiler::threads_lock_ticks += 1; aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: void FlatProfilerTask::task() { aoqi@0: FlatProfiler::received_ticks += 1; aoqi@0: aoqi@0: if (ProfileVM) { aoqi@0: FlatProfiler::record_vm_tick(); aoqi@0: } aoqi@0: aoqi@0: VM_Operation* op = VMThread::vm_operation(); aoqi@0: if (op != NULL) { aoqi@0: FlatProfiler::record_vm_operation(); aoqi@0: if (SafepointSynchronize::is_at_safepoint()) { aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: FlatProfiler::record_thread_ticks(); aoqi@0: } aoqi@0: aoqi@0: void ThreadProfiler::record_interpreted_tick(JavaThread* thread, frame fr, TickPosition where, int* ticks) { aoqi@0: FlatProfiler::all_int_ticks++; aoqi@0: if (!FlatProfiler::full_profile()) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: if (!fr.is_interpreted_frame_valid(thread)) { aoqi@0: // tick came at a bad time aoqi@0: interpreter_ticks += 1; aoqi@0: FlatProfiler::interpreter_ticks += 1; aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // The frame has been fully validated so we can trust the method and bci aoqi@0: aoqi@0: Method* method = *fr.interpreter_frame_method_addr(); aoqi@0: aoqi@0: interpreted_update(method, where); aoqi@0: aoqi@0: // update byte code table aoqi@0: InterpreterCodelet* desc = Interpreter::codelet_containing(fr.pc()); aoqi@0: if (desc != NULL && desc->bytecode() >= 0) { aoqi@0: ticks[desc->bytecode()]++; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void ThreadProfiler::record_compiled_tick(JavaThread* thread, frame fr, TickPosition where) { aoqi@0: const char *name = NULL; aoqi@0: TickPosition localwhere = where; aoqi@0: aoqi@0: FlatProfiler::all_comp_ticks++; aoqi@0: if (!FlatProfiler::full_profile()) return; aoqi@0: aoqi@0: CodeBlob* cb = fr.cb(); aoqi@0: aoqi@0: // For runtime stubs, record as native rather than as compiled aoqi@0: if (cb->is_runtime_stub()) { aoqi@0: RegisterMap map(thread, false); aoqi@0: fr = fr.sender(&map); aoqi@0: cb = fr.cb(); aoqi@0: localwhere = tp_native; aoqi@0: } aoqi@0: Method* method = (cb->is_nmethod()) ? ((nmethod *)cb)->method() : aoqi@0: (Method*)NULL; aoqi@0: aoqi@0: if (method == NULL) { aoqi@0: if (cb->is_runtime_stub()) aoqi@0: runtime_stub_update(cb, name, localwhere); aoqi@0: else aoqi@0: unknown_compiled_update(cb, localwhere); aoqi@0: } aoqi@0: else { aoqi@0: if (method->is_native()) { aoqi@0: stub_update(method, name, localwhere); aoqi@0: } else { aoqi@0: compiled_update(method, localwhere); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: extern "C" void find(int x); aoqi@0: aoqi@0: aoqi@0: void ThreadProfiler::record_tick_for_running_frame(JavaThread* thread, frame fr) { aoqi@0: // The tick happened in real code -> non VM code aoqi@0: if (fr.is_interpreted_frame()) { aoqi@0: interval_data_ref()->inc_interpreted(); aoqi@0: record_interpreted_tick(thread, fr, tp_code, FlatProfiler::bytecode_ticks); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: if (CodeCache::contains(fr.pc())) { aoqi@0: interval_data_ref()->inc_compiled(); aoqi@0: PCRecorder::record(fr.pc()); aoqi@0: record_compiled_tick(thread, fr, tp_code); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: if (VtableStubs::stub_containing(fr.pc()) != NULL) { aoqi@0: unknown_ticks_array[ut_vtable_stubs] += 1; aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: frame caller = fr.profile_find_Java_sender_frame(thread); aoqi@0: aoqi@0: if (caller.sp() != NULL && caller.pc() != NULL) { aoqi@0: record_tick_for_calling_frame(thread, caller); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: unknown_ticks_array[ut_running_frame] += 1; aoqi@0: FlatProfiler::unknown_ticks += 1; aoqi@0: } aoqi@0: aoqi@0: void ThreadProfiler::record_tick_for_calling_frame(JavaThread* thread, frame fr) { aoqi@0: // The tick happened in VM code aoqi@0: interval_data_ref()->inc_native(); aoqi@0: if (fr.is_interpreted_frame()) { aoqi@0: record_interpreted_tick(thread, fr, tp_native, FlatProfiler::bytecode_ticks_stub); aoqi@0: return; aoqi@0: } aoqi@0: if (CodeCache::contains(fr.pc())) { aoqi@0: record_compiled_tick(thread, fr, tp_native); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: frame caller = fr.profile_find_Java_sender_frame(thread); aoqi@0: aoqi@0: if (caller.sp() != NULL && caller.pc() != NULL) { aoqi@0: record_tick_for_calling_frame(thread, caller); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: unknown_ticks_array[ut_calling_frame] += 1; aoqi@0: FlatProfiler::unknown_ticks += 1; aoqi@0: } aoqi@0: aoqi@0: void ThreadProfiler::record_tick(JavaThread* thread) { aoqi@0: FlatProfiler::all_ticks++; aoqi@0: thread_ticks += 1; aoqi@0: aoqi@0: // Here's another way to track global state changes. aoqi@0: // When the class loader starts it marks the ThreadProfiler to tell it it is in the class loader aoqi@0: // and we check that here. aoqi@0: // This is more direct, and more than one thread can be in the class loader at a time, aoqi@0: // but it does mean the class loader has to know about the profiler. aoqi@0: if (region_flag[ThreadProfilerMark::classLoaderRegion]) { aoqi@0: class_loader_ticks += 1; aoqi@0: FlatProfiler::class_loader_ticks += 1; aoqi@0: return; aoqi@0: } else if (region_flag[ThreadProfilerMark::extraRegion]) { aoqi@0: extra_ticks += 1; aoqi@0: FlatProfiler::extra_ticks += 1; aoqi@0: return; aoqi@0: } aoqi@0: // Note that the WatcherThread can now stop for safepoints aoqi@0: uint32_t debug_bits = 0; aoqi@0: if (!thread->wait_for_ext_suspend_completion(SuspendRetryCount, aoqi@0: SuspendRetryDelay, &debug_bits)) { aoqi@0: unknown_ticks_array[ut_unknown_thread_state] += 1; aoqi@0: FlatProfiler::unknown_ticks += 1; aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: frame fr; aoqi@0: aoqi@0: switch (thread->thread_state()) { aoqi@0: case _thread_in_native: aoqi@0: case _thread_in_native_trans: aoqi@0: case _thread_in_vm: aoqi@0: case _thread_in_vm_trans: aoqi@0: if (thread->profile_last_Java_frame(&fr)) { aoqi@0: if (fr.is_runtime_frame()) { aoqi@0: RegisterMap map(thread, false); aoqi@0: fr = fr.sender(&map); aoqi@0: } aoqi@0: record_tick_for_calling_frame(thread, fr); aoqi@0: } else { aoqi@0: unknown_ticks_array[ut_no_last_Java_frame] += 1; aoqi@0: FlatProfiler::unknown_ticks += 1; aoqi@0: } aoqi@0: break; aoqi@0: // handle_special_runtime_exit_condition self-suspends threads in Java aoqi@0: case _thread_in_Java: aoqi@0: case _thread_in_Java_trans: aoqi@0: if (thread->profile_last_Java_frame(&fr)) { aoqi@0: if (fr.is_safepoint_blob_frame()) { aoqi@0: RegisterMap map(thread, false); aoqi@0: fr = fr.sender(&map); aoqi@0: } aoqi@0: record_tick_for_running_frame(thread, fr); aoqi@0: } else { aoqi@0: unknown_ticks_array[ut_no_last_Java_frame] += 1; aoqi@0: FlatProfiler::unknown_ticks += 1; aoqi@0: } aoqi@0: break; aoqi@0: case _thread_blocked: aoqi@0: case _thread_blocked_trans: aoqi@0: if (thread->osthread() && thread->osthread()->get_state() == RUNNABLE) { aoqi@0: if (thread->profile_last_Java_frame(&fr)) { aoqi@0: if (fr.is_safepoint_blob_frame()) { aoqi@0: RegisterMap map(thread, false); aoqi@0: fr = fr.sender(&map); aoqi@0: record_tick_for_running_frame(thread, fr); aoqi@0: } else { aoqi@0: record_tick_for_calling_frame(thread, fr); aoqi@0: } aoqi@0: } else { aoqi@0: unknown_ticks_array[ut_no_last_Java_frame] += 1; aoqi@0: FlatProfiler::unknown_ticks += 1; aoqi@0: } aoqi@0: } else { aoqi@0: blocked_ticks += 1; aoqi@0: FlatProfiler::blocked_ticks += 1; aoqi@0: } aoqi@0: break; aoqi@0: case _thread_uninitialized: aoqi@0: case _thread_new: aoqi@0: // not used, included for completeness aoqi@0: case _thread_new_trans: aoqi@0: unknown_ticks_array[ut_no_last_Java_frame] += 1; aoqi@0: FlatProfiler::unknown_ticks += 1; aoqi@0: break; aoqi@0: default: aoqi@0: unknown_ticks_array[ut_unknown_thread_state] += 1; aoqi@0: FlatProfiler::unknown_ticks += 1; aoqi@0: break; aoqi@0: } aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: void ThreadProfiler::engage() { aoqi@0: engaged = true; aoqi@0: timer.start(); aoqi@0: } aoqi@0: aoqi@0: void ThreadProfiler::disengage() { aoqi@0: engaged = false; aoqi@0: timer.stop(); aoqi@0: } aoqi@0: aoqi@0: void ThreadProfiler::initialize() { aoqi@0: for (int index = 0; index < table_size; index++) { aoqi@0: table[index] = NULL; aoqi@0: } aoqi@0: thread_ticks = 0; aoqi@0: blocked_ticks = 0; aoqi@0: compiler_ticks = 0; aoqi@0: interpreter_ticks = 0; aoqi@0: for (int ut = 0; ut < ut_end; ut += 1) { aoqi@0: unknown_ticks_array[ut] = 0; aoqi@0: } aoqi@0: region_flag[ThreadProfilerMark::classLoaderRegion] = false; aoqi@0: class_loader_ticks = 0; aoqi@0: region_flag[ThreadProfilerMark::extraRegion] = false; aoqi@0: extra_ticks = 0; aoqi@0: timer.start(); aoqi@0: interval_data_ref()->reset(); aoqi@0: } aoqi@0: aoqi@0: void ThreadProfiler::reset() { aoqi@0: timer.stop(); aoqi@0: if (table != NULL) { aoqi@0: for (int index = 0; index < table_size; index++) { aoqi@0: ProfilerNode* n = table[index]; aoqi@0: if (n != NULL) { aoqi@0: delete n; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: initialize(); aoqi@0: } aoqi@0: aoqi@0: void FlatProfiler::allocate_table() { aoqi@0: { // Bytecode table aoqi@0: bytecode_ticks = NEW_C_HEAP_ARRAY(int, Bytecodes::number_of_codes, mtInternal); aoqi@0: bytecode_ticks_stub = NEW_C_HEAP_ARRAY(int, Bytecodes::number_of_codes, mtInternal); aoqi@0: for(int index = 0; index < Bytecodes::number_of_codes; index++) { aoqi@0: bytecode_ticks[index] = 0; aoqi@0: bytecode_ticks_stub[index] = 0; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (ProfilerRecordPC) PCRecorder::init(); aoqi@0: aoqi@0: interval_data = NEW_C_HEAP_ARRAY(IntervalData, interval_print_size, mtInternal); aoqi@0: FlatProfiler::interval_reset(); aoqi@0: } aoqi@0: aoqi@0: void FlatProfiler::engage(JavaThread* mainThread, bool fullProfile) { aoqi@0: full_profile_flag = fullProfile; aoqi@0: if (bytecode_ticks == NULL) { aoqi@0: allocate_table(); aoqi@0: } aoqi@0: if(ProfileVM && (vm_thread_profiler == NULL)){ aoqi@0: vm_thread_profiler = new ThreadProfiler(); aoqi@0: } aoqi@0: if (task == NULL) { aoqi@0: task = new FlatProfilerTask(WatcherThread::delay_interval); aoqi@0: task->enroll(); aoqi@0: } aoqi@0: timer.start(); aoqi@0: if (mainThread != NULL) { aoqi@0: // When mainThread was created, it might not have a ThreadProfiler aoqi@0: ThreadProfiler* pp = mainThread->get_thread_profiler(); aoqi@0: if (pp == NULL) { aoqi@0: mainThread->set_thread_profiler(new ThreadProfiler()); aoqi@0: } else { aoqi@0: pp->reset(); aoqi@0: } aoqi@0: mainThread->get_thread_profiler()->engage(); aoqi@0: } aoqi@0: // This is where we would assign thread_profiler aoqi@0: // if we wanted only one thread_profiler for all threads. aoqi@0: thread_profiler = NULL; aoqi@0: } aoqi@0: aoqi@0: void FlatProfiler::disengage() { aoqi@0: if (!task) { aoqi@0: return; aoqi@0: } aoqi@0: timer.stop(); aoqi@0: task->disenroll(); aoqi@0: delete task; aoqi@0: task = NULL; aoqi@0: if (thread_profiler != NULL) { aoqi@0: thread_profiler->disengage(); aoqi@0: } else { aoqi@0: MutexLocker tl(Threads_lock); aoqi@0: for (JavaThread* tp = Threads::first(); tp != NULL; tp = tp->next()) { aoqi@0: ThreadProfiler* pp = tp->get_thread_profiler(); aoqi@0: if (pp != NULL) { aoqi@0: pp->disengage(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void FlatProfiler::reset() { aoqi@0: if (task) { aoqi@0: disengage(); aoqi@0: } aoqi@0: aoqi@0: class_loader_ticks = 0; aoqi@0: extra_ticks = 0; aoqi@0: received_gc_ticks = 0; aoqi@0: vm_operation_ticks = 0; aoqi@0: compiler_ticks = 0; aoqi@0: deopt_ticks = 0; aoqi@0: interpreter_ticks = 0; aoqi@0: blocked_ticks = 0; aoqi@0: unknown_ticks = 0; aoqi@0: received_ticks = 0; aoqi@0: delivered_ticks = 0; aoqi@0: timer.stop(); aoqi@0: } aoqi@0: aoqi@0: bool FlatProfiler::is_active() { aoqi@0: return task != NULL; aoqi@0: } aoqi@0: aoqi@0: void FlatProfiler::print_byte_code_statistics() { aoqi@0: GrowableArray * array = new GrowableArray(200); aoqi@0: aoqi@0: tty->print_cr(" Bytecode ticks:"); aoqi@0: for (int index = 0; index < Bytecodes::number_of_codes; index++) { aoqi@0: if (FlatProfiler::bytecode_ticks[index] > 0 || FlatProfiler::bytecode_ticks_stub[index] > 0) { aoqi@0: tty->print_cr(" %4d %4d = %s", aoqi@0: FlatProfiler::bytecode_ticks[index], aoqi@0: FlatProfiler::bytecode_ticks_stub[index], aoqi@0: Bytecodes::name( (Bytecodes::Code) index)); aoqi@0: } aoqi@0: } aoqi@0: tty->cr(); aoqi@0: } aoqi@0: aoqi@0: void print_ticks(const char* title, int ticks, int total) { aoqi@0: if (ticks > 0) { aoqi@0: tty->print("%5.1f%% %5d", ticks * 100.0 / total, ticks); aoqi@0: tty->fill_to(col3); aoqi@0: tty->print("%s", title); aoqi@0: tty->cr(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void ThreadProfiler::print(const char* thread_name) { aoqi@0: ResourceMark rm; aoqi@0: MutexLocker ppl(ProfilePrint_lock); aoqi@0: int index = 0; // Declared outside for loops for portability aoqi@0: aoqi@0: if (table == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: if (thread_ticks <= 0) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: const char* title = "too soon to tell"; aoqi@0: double secs = timer.seconds(); aoqi@0: aoqi@0: GrowableArray * array = new GrowableArray(200); aoqi@0: for(index = 0; index < table_size; index++) { aoqi@0: for(ProfilerNode* node = table[index]; node; node = node->next()) aoqi@0: array->append(node); aoqi@0: } aoqi@0: aoqi@0: array->sort(&ProfilerNode::compare); aoqi@0: aoqi@0: // compute total (sanity check) aoqi@0: int active = aoqi@0: class_loader_ticks + aoqi@0: compiler_ticks + aoqi@0: interpreter_ticks + aoqi@0: unknown_ticks(); aoqi@0: for (index = 0; index < array->length(); index++) { aoqi@0: active += array->at(index)->ticks.total(); aoqi@0: } aoqi@0: int total = active + blocked_ticks; aoqi@0: aoqi@0: tty->cr(); aoqi@0: tty->print_cr("Flat profile of %3.2f secs (%d total ticks): %s", secs, total, thread_name); aoqi@0: if (total != thread_ticks) { aoqi@0: print_ticks("Lost ticks", thread_ticks-total, thread_ticks); aoqi@0: } aoqi@0: tty->cr(); aoqi@0: aoqi@0: // print interpreted methods aoqi@0: tick_counter interpreted_ticks; aoqi@0: bool has_interpreted_ticks = false; aoqi@0: int print_count = 0; aoqi@0: for (index = 0; index < array->length(); index++) { aoqi@0: ProfilerNode* n = array->at(index); aoqi@0: if (n->is_interpreted()) { aoqi@0: interpreted_ticks.add(&n->ticks); aoqi@0: if (!has_interpreted_ticks) { aoqi@0: interpretedNode::print_title(tty); aoqi@0: has_interpreted_ticks = true; aoqi@0: } aoqi@0: if (print_count++ < ProfilerNumberOfInterpretedMethods) { aoqi@0: n->print(tty, active); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (has_interpreted_ticks) { aoqi@0: if (print_count <= ProfilerNumberOfInterpretedMethods) { aoqi@0: title = "Total interpreted"; aoqi@0: } else { aoqi@0: title = "Total interpreted (including elided)"; aoqi@0: } aoqi@0: interpretedNode::print_total(tty, &interpreted_ticks, active, title); aoqi@0: tty->cr(); aoqi@0: } aoqi@0: aoqi@0: // print compiled methods aoqi@0: tick_counter compiled_ticks; aoqi@0: bool has_compiled_ticks = false; aoqi@0: print_count = 0; aoqi@0: for (index = 0; index < array->length(); index++) { aoqi@0: ProfilerNode* n = array->at(index); aoqi@0: if (n->is_compiled()) { aoqi@0: compiled_ticks.add(&n->ticks); aoqi@0: if (!has_compiled_ticks) { aoqi@0: compiledNode::print_title(tty); aoqi@0: has_compiled_ticks = true; aoqi@0: } aoqi@0: if (print_count++ < ProfilerNumberOfCompiledMethods) { aoqi@0: n->print(tty, active); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (has_compiled_ticks) { aoqi@0: if (print_count <= ProfilerNumberOfCompiledMethods) { aoqi@0: title = "Total compiled"; aoqi@0: } else { aoqi@0: title = "Total compiled (including elided)"; aoqi@0: } aoqi@0: compiledNode::print_total(tty, &compiled_ticks, active, title); aoqi@0: tty->cr(); aoqi@0: } aoqi@0: aoqi@0: // print stub methods aoqi@0: tick_counter stub_ticks; aoqi@0: bool has_stub_ticks = false; aoqi@0: print_count = 0; aoqi@0: for (index = 0; index < array->length(); index++) { aoqi@0: ProfilerNode* n = array->at(index); aoqi@0: if (n->is_stub()) { aoqi@0: stub_ticks.add(&n->ticks); aoqi@0: if (!has_stub_ticks) { aoqi@0: stubNode::print_title(tty); aoqi@0: has_stub_ticks = true; aoqi@0: } aoqi@0: if (print_count++ < ProfilerNumberOfStubMethods) { aoqi@0: n->print(tty, active); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (has_stub_ticks) { aoqi@0: if (print_count <= ProfilerNumberOfStubMethods) { aoqi@0: title = "Total stub"; aoqi@0: } else { aoqi@0: title = "Total stub (including elided)"; aoqi@0: } aoqi@0: stubNode::print_total(tty, &stub_ticks, active, title); aoqi@0: tty->cr(); aoqi@0: } aoqi@0: aoqi@0: // print runtime stubs aoqi@0: tick_counter runtime_stub_ticks; aoqi@0: bool has_runtime_stub_ticks = false; aoqi@0: print_count = 0; aoqi@0: for (index = 0; index < array->length(); index++) { aoqi@0: ProfilerNode* n = array->at(index); aoqi@0: if (n->is_runtime_stub()) { aoqi@0: runtime_stub_ticks.add(&n->ticks); aoqi@0: if (!has_runtime_stub_ticks) { aoqi@0: runtimeStubNode::print_title(tty); aoqi@0: has_runtime_stub_ticks = true; aoqi@0: } aoqi@0: if (print_count++ < ProfilerNumberOfRuntimeStubNodes) { aoqi@0: n->print(tty, active); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (has_runtime_stub_ticks) { aoqi@0: if (print_count <= ProfilerNumberOfRuntimeStubNodes) { aoqi@0: title = "Total runtime stubs"; aoqi@0: } else { aoqi@0: title = "Total runtime stubs (including elided)"; aoqi@0: } aoqi@0: runtimeStubNode::print_total(tty, &runtime_stub_ticks, active, title); aoqi@0: tty->cr(); aoqi@0: } aoqi@0: aoqi@0: if (blocked_ticks + class_loader_ticks + interpreter_ticks + compiler_ticks + unknown_ticks() != 0) { aoqi@0: tty->fill_to(col1); aoqi@0: tty->print_cr("Thread-local ticks:"); aoqi@0: print_ticks("Blocked (of total)", blocked_ticks, total); aoqi@0: print_ticks("Class loader", class_loader_ticks, active); aoqi@0: print_ticks("Extra", extra_ticks, active); aoqi@0: print_ticks("Interpreter", interpreter_ticks, active); aoqi@0: print_ticks("Compilation", compiler_ticks, active); aoqi@0: print_ticks("Unknown: vtable stubs", unknown_ticks_array[ut_vtable_stubs], active); aoqi@0: print_ticks("Unknown: null method", unknown_ticks_array[ut_null_method], active); aoqi@0: print_ticks("Unknown: running frame", unknown_ticks_array[ut_running_frame], active); aoqi@0: print_ticks("Unknown: calling frame", unknown_ticks_array[ut_calling_frame], active); aoqi@0: print_ticks("Unknown: no pc", unknown_ticks_array[ut_no_pc], active); aoqi@0: print_ticks("Unknown: no last frame", unknown_ticks_array[ut_no_last_Java_frame], active); aoqi@0: print_ticks("Unknown: thread_state", unknown_ticks_array[ut_unknown_thread_state], active); aoqi@0: tty->cr(); aoqi@0: } aoqi@0: aoqi@0: if (WizardMode) { aoqi@0: tty->print_cr("Node area used: %dKb", (area_top - area_bottom) / 1024); aoqi@0: } aoqi@0: reset(); aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: ThreadProfiler::print_unknown(){ aoqi@0: if (table == NULL) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: if (thread_ticks <= 0) { aoqi@0: return; aoqi@0: } aoqi@0: } */ aoqi@0: aoqi@0: void FlatProfiler::print(int unused) { aoqi@0: ResourceMark rm; aoqi@0: if (thread_profiler != NULL) { aoqi@0: thread_profiler->print("All threads"); aoqi@0: } else { aoqi@0: MutexLocker tl(Threads_lock); aoqi@0: for (JavaThread* tp = Threads::first(); tp != NULL; tp = tp->next()) { aoqi@0: ThreadProfiler* pp = tp->get_thread_profiler(); aoqi@0: if (pp != NULL) { aoqi@0: pp->print(tp->get_thread_name()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (ProfilerPrintByteCodeStatistics) { aoqi@0: print_byte_code_statistics(); aoqi@0: } aoqi@0: aoqi@0: if (non_method_ticks() > 0) { aoqi@0: tty->cr(); aoqi@0: tty->print_cr("Global summary of %3.2f seconds:", timer.seconds()); aoqi@0: print_ticks("Received ticks", received_ticks, received_ticks); aoqi@0: print_ticks("Received GC ticks", received_gc_ticks, received_ticks); aoqi@0: print_ticks("Compilation", compiler_ticks, received_ticks); aoqi@0: print_ticks("Deoptimization", deopt_ticks, received_ticks); aoqi@0: print_ticks("Other VM operations", vm_operation_ticks, received_ticks); aoqi@0: #ifndef PRODUCT aoqi@0: print_ticks("Blocked ticks", blocked_ticks, received_ticks); aoqi@0: print_ticks("Threads_lock blocks", threads_lock_ticks, received_ticks); aoqi@0: print_ticks("Delivered ticks", delivered_ticks, received_ticks); aoqi@0: print_ticks("All ticks", all_ticks, received_ticks); aoqi@0: #endif aoqi@0: print_ticks("Class loader", class_loader_ticks, received_ticks); aoqi@0: print_ticks("Extra ", extra_ticks, received_ticks); aoqi@0: print_ticks("Interpreter", interpreter_ticks, received_ticks); aoqi@0: print_ticks("Unknown code", unknown_ticks, received_ticks); aoqi@0: } aoqi@0: aoqi@0: PCRecorder::print(); aoqi@0: aoqi@0: if(ProfileVM){ aoqi@0: tty->cr(); aoqi@0: vm_thread_profiler->print("VM Thread"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void IntervalData::print_header(outputStream* st) { aoqi@0: st->print("i/c/n/g"); aoqi@0: } aoqi@0: aoqi@0: void IntervalData::print_data(outputStream* st) { aoqi@0: st->print("%d/%d/%d/%d", interpreted(), compiled(), native(), compiling()); aoqi@0: } aoqi@0: aoqi@0: void FlatProfiler::interval_record_thread(ThreadProfiler* tp) { aoqi@0: IntervalData id = tp->interval_data(); aoqi@0: int total = id.total(); aoqi@0: tp->interval_data_ref()->reset(); aoqi@0: aoqi@0: // Insertion sort the data, if it's relevant. aoqi@0: for (int i = 0; i < interval_print_size; i += 1) { aoqi@0: if (total > interval_data[i].total()) { aoqi@0: for (int j = interval_print_size - 1; j > i; j -= 1) { aoqi@0: interval_data[j] = interval_data[j-1]; aoqi@0: } aoqi@0: interval_data[i] = id; aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void FlatProfiler::interval_print() { aoqi@0: if ((interval_data[0].total() > 0)) { aoqi@0: tty->stamp(); aoqi@0: tty->print("\t"); aoqi@0: IntervalData::print_header(tty); aoqi@0: for (int i = 0; i < interval_print_size; i += 1) { aoqi@0: if (interval_data[i].total() > 0) { aoqi@0: tty->print("\t"); aoqi@0: interval_data[i].print_data(tty); aoqi@0: } aoqi@0: } aoqi@0: tty->cr(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void FlatProfiler::interval_reset() { aoqi@0: for (int i = 0; i < interval_print_size; i += 1) { aoqi@0: interval_data[i].reset(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void ThreadProfiler::oops_do(OopClosure* f) { aoqi@0: if (table == NULL) return; aoqi@0: aoqi@0: for(int index = 0; index < table_size; index++) { aoqi@0: for(ProfilerNode* node = table[index]; node; node = node->next()) aoqi@0: node->oops_do(f); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void FlatProfiler::oops_do(OopClosure* f) { aoqi@0: if (thread_profiler != NULL) { aoqi@0: thread_profiler->oops_do(f); aoqi@0: } else { aoqi@0: for (JavaThread* tp = Threads::first(); tp != NULL; tp = tp->next()) { aoqi@0: ThreadProfiler* pp = tp->get_thread_profiler(); aoqi@0: if (pp != NULL) { aoqi@0: pp->oops_do(f); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: }