Wed, 17 Sep 2008 19:59:35 +0400
Merge
duke@435 | 1 | /* |
xdono@631 | 2 | * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | // a simple flat profiler for Java |
duke@435 | 26 | |
duke@435 | 27 | |
duke@435 | 28 | // Forward declaration of classes defined in this header file |
duke@435 | 29 | class ThreadProfiler; |
duke@435 | 30 | class ThreadProfilerMark; |
duke@435 | 31 | class FlatProfiler; |
duke@435 | 32 | class IntervalData; |
duke@435 | 33 | |
duke@435 | 34 | // Declarations of classes defined only in the implementation. |
duke@435 | 35 | class ProfilerNode; |
duke@435 | 36 | class FlatProfilerTask; |
duke@435 | 37 | |
duke@435 | 38 | enum TickPosition { |
duke@435 | 39 | tp_code, |
duke@435 | 40 | tp_native |
duke@435 | 41 | }; |
duke@435 | 42 | |
duke@435 | 43 | // One of these guys is constructed as we enter interesting regions |
duke@435 | 44 | // and destructed as we exit the region. While we are in the region |
duke@435 | 45 | // ticks are allotted to the region. |
duke@435 | 46 | class ThreadProfilerMark: public StackObj { |
duke@435 | 47 | public: |
duke@435 | 48 | // For now, the only thread-specific region is the class loader. |
duke@435 | 49 | enum Region { noRegion, classLoaderRegion, extraRegion, maxRegion }; |
duke@435 | 50 | |
duke@435 | 51 | ThreadProfilerMark(Region) KERNEL_RETURN; |
duke@435 | 52 | ~ThreadProfilerMark() KERNEL_RETURN; |
duke@435 | 53 | |
duke@435 | 54 | private: |
duke@435 | 55 | ThreadProfiler* _pp; |
duke@435 | 56 | Region _r; |
duke@435 | 57 | }; |
duke@435 | 58 | |
duke@435 | 59 | #ifndef FPROF_KERNEL |
duke@435 | 60 | |
duke@435 | 61 | class IntervalData VALUE_OBJ_CLASS_SPEC { |
duke@435 | 62 | // Just to keep these things all together |
duke@435 | 63 | private: |
duke@435 | 64 | int _interpreted; |
duke@435 | 65 | int _compiled; |
duke@435 | 66 | int _native; |
duke@435 | 67 | int _compiling; |
duke@435 | 68 | public: |
duke@435 | 69 | int interpreted() { |
duke@435 | 70 | return _interpreted; |
duke@435 | 71 | } |
duke@435 | 72 | int compiled() { |
duke@435 | 73 | return _compiled; |
duke@435 | 74 | } |
duke@435 | 75 | int native() { |
duke@435 | 76 | return _native; |
duke@435 | 77 | } |
duke@435 | 78 | int compiling() { |
duke@435 | 79 | return _compiling; |
duke@435 | 80 | } |
duke@435 | 81 | int total() { |
duke@435 | 82 | return (interpreted() + compiled() + native() + compiling()); |
duke@435 | 83 | } |
duke@435 | 84 | void inc_interpreted() { |
duke@435 | 85 | _interpreted += 1; |
duke@435 | 86 | } |
duke@435 | 87 | void inc_compiled() { |
duke@435 | 88 | _compiled += 1; |
duke@435 | 89 | } |
duke@435 | 90 | void inc_native() { |
duke@435 | 91 | _native += 1; |
duke@435 | 92 | } |
duke@435 | 93 | void inc_compiling() { |
duke@435 | 94 | _compiling += 1; |
duke@435 | 95 | } |
duke@435 | 96 | void reset() { |
duke@435 | 97 | _interpreted = 0; |
duke@435 | 98 | _compiled = 0; |
duke@435 | 99 | _native = 0; |
duke@435 | 100 | _compiling = 0; |
duke@435 | 101 | } |
duke@435 | 102 | static void print_header(outputStream* st); |
duke@435 | 103 | void print_data(outputStream* st); |
duke@435 | 104 | }; |
duke@435 | 105 | #endif // FPROF_KERNEL |
duke@435 | 106 | |
duke@435 | 107 | class ThreadProfiler: public CHeapObj { |
duke@435 | 108 | public: |
duke@435 | 109 | ThreadProfiler() KERNEL_RETURN; |
duke@435 | 110 | ~ThreadProfiler() KERNEL_RETURN; |
duke@435 | 111 | |
duke@435 | 112 | // Resets the profiler |
duke@435 | 113 | void reset() KERNEL_RETURN; |
duke@435 | 114 | |
duke@435 | 115 | // Activates the profiler for a certain thread |
duke@435 | 116 | void engage() KERNEL_RETURN; |
duke@435 | 117 | |
duke@435 | 118 | // Deactivates the profiler |
duke@435 | 119 | void disengage() KERNEL_RETURN; |
duke@435 | 120 | |
duke@435 | 121 | // Prints the collected profiling information |
duke@435 | 122 | void print(const char* thread_name) KERNEL_RETURN; |
duke@435 | 123 | |
duke@435 | 124 | // Garbage Collection Support |
duke@435 | 125 | void oops_do(OopClosure* f) KERNEL_RETURN; |
duke@435 | 126 | |
duke@435 | 127 | #ifndef FPROF_KERNEL |
duke@435 | 128 | private: |
duke@435 | 129 | // for recording ticks. |
duke@435 | 130 | friend class ProfilerNode; |
duke@435 | 131 | char* area_bottom; // preallocated area for pnodes |
duke@435 | 132 | char* area_top; |
duke@435 | 133 | char* area_limit; |
duke@435 | 134 | static int table_size; |
duke@435 | 135 | ProfilerNode** table; |
duke@435 | 136 | |
duke@435 | 137 | private: |
sgoldman@542 | 138 | void record_interpreted_tick(JavaThread* thread, frame fr, TickPosition where, int* ticks); |
duke@435 | 139 | void record_compiled_tick (JavaThread* thread, frame fr, TickPosition where); |
duke@435 | 140 | void interpreted_update(methodOop method, TickPosition where); |
duke@435 | 141 | void compiled_update (methodOop method, TickPosition where); |
duke@435 | 142 | void stub_update (methodOop method, const char* name, TickPosition where); |
duke@435 | 143 | void adapter_update (TickPosition where); |
duke@435 | 144 | |
duke@435 | 145 | void runtime_stub_update(const CodeBlob* stub, const char* name, TickPosition where); |
duke@435 | 146 | void unknown_compiled_update (const CodeBlob* cb, TickPosition where); |
duke@435 | 147 | |
duke@435 | 148 | void vm_update (TickPosition where); |
duke@435 | 149 | void vm_update (const char* name, TickPosition where); |
duke@435 | 150 | |
duke@435 | 151 | void record_tick_for_running_frame(JavaThread* thread, frame fr); |
duke@435 | 152 | void record_tick_for_calling_frame(JavaThread* thread, frame fr); |
duke@435 | 153 | |
duke@435 | 154 | void initialize(); |
duke@435 | 155 | |
duke@435 | 156 | static int entry(int value); |
duke@435 | 157 | |
duke@435 | 158 | |
duke@435 | 159 | private: |
duke@435 | 160 | friend class FlatProfiler; |
duke@435 | 161 | void record_tick(JavaThread* thread); |
duke@435 | 162 | bool engaged; |
duke@435 | 163 | // so we can do percentages for this thread, and quick checks for activity |
duke@435 | 164 | int thread_ticks; |
duke@435 | 165 | int compiler_ticks; |
duke@435 | 166 | int interpreter_ticks; |
duke@435 | 167 | |
duke@435 | 168 | public: |
duke@435 | 169 | void inc_thread_ticks() { thread_ticks += 1; } |
duke@435 | 170 | |
duke@435 | 171 | private: |
duke@435 | 172 | friend class ThreadProfilerMark; |
duke@435 | 173 | // counters for thread-specific regions |
duke@435 | 174 | bool region_flag[ThreadProfilerMark::maxRegion]; |
duke@435 | 175 | int class_loader_ticks; |
duke@435 | 176 | int extra_ticks; |
duke@435 | 177 | |
duke@435 | 178 | private: |
duke@435 | 179 | // other thread-specific regions |
duke@435 | 180 | int blocked_ticks; |
duke@435 | 181 | enum UnknownTickSites { |
duke@435 | 182 | ut_null_method, |
duke@435 | 183 | ut_vtable_stubs, |
duke@435 | 184 | ut_running_frame, |
duke@435 | 185 | ut_calling_frame, |
duke@435 | 186 | ut_no_pc, |
duke@435 | 187 | ut_no_last_Java_frame, |
duke@435 | 188 | ut_unknown_thread_state, |
duke@435 | 189 | ut_end |
duke@435 | 190 | }; |
duke@435 | 191 | int unknown_ticks_array[ut_end]; |
duke@435 | 192 | int unknown_ticks() { |
duke@435 | 193 | int result = 0; |
duke@435 | 194 | for (int ut = 0; ut < ut_end; ut += 1) { |
duke@435 | 195 | result += unknown_ticks_array[ut]; |
duke@435 | 196 | } |
duke@435 | 197 | return result; |
duke@435 | 198 | } |
duke@435 | 199 | |
duke@435 | 200 | elapsedTimer timer; |
duke@435 | 201 | |
duke@435 | 202 | // For interval timing |
duke@435 | 203 | private: |
duke@435 | 204 | IntervalData _interval_data; |
duke@435 | 205 | IntervalData interval_data() { |
duke@435 | 206 | return _interval_data; |
duke@435 | 207 | } |
duke@435 | 208 | IntervalData* interval_data_ref() { |
duke@435 | 209 | return &_interval_data; |
duke@435 | 210 | } |
duke@435 | 211 | #endif // FPROF_KERNEL |
duke@435 | 212 | }; |
duke@435 | 213 | |
duke@435 | 214 | class FlatProfiler: AllStatic { |
duke@435 | 215 | public: |
duke@435 | 216 | static void reset() KERNEL_RETURN ; |
duke@435 | 217 | static void engage(JavaThread* mainThread, bool fullProfile) KERNEL_RETURN ; |
duke@435 | 218 | static void disengage() KERNEL_RETURN ; |
duke@435 | 219 | static void print(int unused) KERNEL_RETURN ; |
duke@435 | 220 | static bool is_active() KERNEL_RETURN_(return false;) ; |
duke@435 | 221 | |
duke@435 | 222 | // This is NULL if each thread has its own thread profiler, |
duke@435 | 223 | // else this is the single thread profiler used by all threads. |
duke@435 | 224 | // In particular it makes a difference during garbage collection, |
duke@435 | 225 | // where you only want to traverse each thread profiler once. |
duke@435 | 226 | static ThreadProfiler* get_thread_profiler() KERNEL_RETURN_(return NULL;); |
duke@435 | 227 | |
duke@435 | 228 | // Garbage Collection Support |
duke@435 | 229 | static void oops_do(OopClosure* f) KERNEL_RETURN ; |
duke@435 | 230 | |
duke@435 | 231 | // Support for disassembler to inspect the PCRecorder |
duke@435 | 232 | |
duke@435 | 233 | // Returns the start address for a given pc |
duke@435 | 234 | // NULL is returned if the PCRecorder is inactive |
duke@435 | 235 | static address bucket_start_for(address pc) KERNEL_RETURN_(return NULL;); |
duke@435 | 236 | |
duke@435 | 237 | enum { MillisecsPerTick = 10 }; // ms per profiling ticks |
duke@435 | 238 | |
duke@435 | 239 | // Returns the number of ticks recorded for the bucket |
duke@435 | 240 | // pc belongs to. |
duke@435 | 241 | static int bucket_count_for(address pc) KERNEL_RETURN_(return 0;); |
duke@435 | 242 | |
duke@435 | 243 | #ifndef FPROF_KERNEL |
duke@435 | 244 | |
duke@435 | 245 | private: |
duke@435 | 246 | static bool full_profile() { |
duke@435 | 247 | return full_profile_flag; |
duke@435 | 248 | } |
duke@435 | 249 | |
duke@435 | 250 | friend class ThreadProfiler; |
duke@435 | 251 | // the following group of ticks cover everything that's not attributed to individual Java methods |
duke@435 | 252 | static int received_gc_ticks; // ticks during which gc was active |
duke@435 | 253 | static int vm_operation_ticks; // total ticks in vm_operations other than GC |
duke@435 | 254 | static int threads_lock_ticks; // the number of times we couldn't get the Threads_lock without blocking |
duke@435 | 255 | static int blocked_ticks; // ticks when the thread was blocked. |
duke@435 | 256 | static int class_loader_ticks; // total ticks in class loader |
duke@435 | 257 | static int extra_ticks; // total ticks an extra temporary measuring |
duke@435 | 258 | static int compiler_ticks; // total ticks in compilation |
duke@435 | 259 | static int interpreter_ticks; // ticks in unknown interpreted method |
duke@435 | 260 | static int deopt_ticks; // ticks in deoptimization |
duke@435 | 261 | static int unknown_ticks; // ticks that cannot be categorized |
duke@435 | 262 | static int received_ticks; // ticks that were received by task |
duke@435 | 263 | static int delivered_ticks; // ticks that were delivered by task |
duke@435 | 264 | static int non_method_ticks() { |
duke@435 | 265 | return |
duke@435 | 266 | ( received_gc_ticks |
duke@435 | 267 | + vm_operation_ticks |
duke@435 | 268 | + deopt_ticks |
duke@435 | 269 | + threads_lock_ticks |
duke@435 | 270 | + blocked_ticks |
duke@435 | 271 | + compiler_ticks |
duke@435 | 272 | + interpreter_ticks |
duke@435 | 273 | + unknown_ticks ); |
duke@435 | 274 | } |
duke@435 | 275 | static elapsedTimer timer; |
duke@435 | 276 | |
duke@435 | 277 | // Counts of each of the byte codes |
duke@435 | 278 | static int* bytecode_ticks; |
duke@435 | 279 | static int* bytecode_ticks_stub; |
duke@435 | 280 | static void print_byte_code_statistics(); |
duke@435 | 281 | |
duke@435 | 282 | // the ticks below are for continuous profiling (to adjust recompilation, etc.) |
duke@435 | 283 | static int all_ticks; // total count of ticks received so far |
duke@435 | 284 | static int all_int_ticks; // ticks in interpreter |
duke@435 | 285 | static int all_comp_ticks; // ticks in compiled code (+ native) |
duke@435 | 286 | static bool full_profile_flag; // collecting full profile? |
duke@435 | 287 | |
duke@435 | 288 | // to accumulate thread-specific data |
duke@435 | 289 | // if we aren't profiling individual threads. |
duke@435 | 290 | static ThreadProfiler* thread_profiler; |
duke@435 | 291 | static ThreadProfiler* vm_thread_profiler; |
duke@435 | 292 | |
duke@435 | 293 | static void allocate_table(); |
duke@435 | 294 | |
duke@435 | 295 | // The task that periodically interrupts things. |
duke@435 | 296 | friend class FlatProfilerTask; |
duke@435 | 297 | static FlatProfilerTask* task; |
duke@435 | 298 | static void record_vm_operation(); |
duke@435 | 299 | static void record_vm_tick(); |
duke@435 | 300 | static void record_thread_ticks(); |
duke@435 | 301 | |
duke@435 | 302 | // For interval analysis |
duke@435 | 303 | private: |
duke@435 | 304 | static int interval_ticks_previous; // delivered_ticks from the last interval |
duke@435 | 305 | static void interval_record_thread(ThreadProfiler* tp); // extract ticks from ThreadProfiler. |
duke@435 | 306 | static void interval_print(); // print interval data. |
duke@435 | 307 | static void interval_reset(); // reset interval data. |
duke@435 | 308 | enum {interval_print_size = 10}; |
duke@435 | 309 | static IntervalData* interval_data; |
duke@435 | 310 | #endif // FPROF_KERNEL |
duke@435 | 311 | }; |