Fri, 07 Jan 2011 10:42:32 -0500
7003271: Hotspot should track cumulative Java heap bytes allocated on a per-thread basis
Summary: Track allocated bytes in Thread's, update on TLAB retirement and direct allocation in Eden and tenured, add JNI methods for ThreadMXBean.
Reviewed-by: coleenp, kvn, dholmes, ysr
duke@435 | 1 | /* |
stefank@2314 | 2 | * Copyright (c) 2003, 2010, Oracle and/or its affiliates. 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 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #include "precompiled.hpp" |
stefank@2314 | 26 | #include "jvmtifiles/jvmtiEnv.hpp" |
stefank@2314 | 27 | #include "memory/gcLocker.hpp" |
stefank@2314 | 28 | #include "memory/resourceArea.hpp" |
stefank@2314 | 29 | #include "prims/jvmtiEventController.inline.hpp" |
stefank@2314 | 30 | #include "prims/jvmtiImpl.hpp" |
stefank@2314 | 31 | #include "prims/jvmtiThreadState.inline.hpp" |
stefank@2314 | 32 | #include "runtime/vframe.hpp" |
duke@435 | 33 | |
duke@435 | 34 | // marker for when the stack depth has been reset and is now unknown. |
duke@435 | 35 | // any negative number would work but small ones might obscure an |
duke@435 | 36 | // underrun error. |
duke@435 | 37 | static const int UNKNOWN_STACK_DEPTH = -99; |
duke@435 | 38 | |
duke@435 | 39 | /////////////////////////////////////////////////////////////// |
duke@435 | 40 | // |
duke@435 | 41 | // class JvmtiThreadState |
duke@435 | 42 | // |
duke@435 | 43 | // Instances of JvmtiThreadState hang off of each thread. |
duke@435 | 44 | // Thread local storage for JVMTI. |
duke@435 | 45 | // |
duke@435 | 46 | |
duke@435 | 47 | JvmtiThreadState *JvmtiThreadState::_head = NULL; |
duke@435 | 48 | |
duke@435 | 49 | JvmtiThreadState::JvmtiThreadState(JavaThread* thread) |
duke@435 | 50 | : _thread_event_enable() { |
duke@435 | 51 | assert(JvmtiThreadState_lock->is_locked(), "sanity check"); |
duke@435 | 52 | _thread = thread; |
duke@435 | 53 | _exception_detected = false; |
duke@435 | 54 | _exception_caught = false; |
duke@435 | 55 | _debuggable = true; |
duke@435 | 56 | _hide_single_stepping = false; |
duke@435 | 57 | _hide_level = 0; |
duke@435 | 58 | _pending_step_for_popframe = false; |
duke@435 | 59 | _class_being_redefined = NULL; |
duke@435 | 60 | _class_load_kind = jvmti_class_load_kind_load; |
duke@435 | 61 | _head_env_thread_state = NULL; |
duke@435 | 62 | _dynamic_code_event_collector = NULL; |
duke@435 | 63 | _vm_object_alloc_event_collector = NULL; |
duke@435 | 64 | _the_class_for_redefinition_verification = NULL; |
duke@435 | 65 | _scratch_class_for_redefinition_verification = NULL; |
duke@435 | 66 | |
duke@435 | 67 | // JVMTI ForceEarlyReturn support |
duke@435 | 68 | _pending_step_for_earlyret = false; |
duke@435 | 69 | _earlyret_state = earlyret_inactive; |
duke@435 | 70 | _earlyret_tos = ilgl; |
duke@435 | 71 | _earlyret_value.j = 0L; |
duke@435 | 72 | _earlyret_oop = NULL; |
duke@435 | 73 | |
duke@435 | 74 | // add all the JvmtiEnvThreadState to the new JvmtiThreadState |
duke@435 | 75 | { |
duke@435 | 76 | JvmtiEnvIterator it; |
duke@435 | 77 | for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) { |
duke@435 | 78 | if (env->is_valid()) { |
duke@435 | 79 | add_env(env); |
duke@435 | 80 | } |
duke@435 | 81 | } |
duke@435 | 82 | } |
duke@435 | 83 | |
duke@435 | 84 | // link us into the list |
duke@435 | 85 | { |
duke@435 | 86 | // The thread state list manipulation code must not have safepoints. |
duke@435 | 87 | // See periodic_clean_up(). |
duke@435 | 88 | debug_only(No_Safepoint_Verifier nosafepoint;) |
duke@435 | 89 | |
duke@435 | 90 | _prev = NULL; |
duke@435 | 91 | _next = _head; |
duke@435 | 92 | if (_head != NULL) { |
duke@435 | 93 | _head->_prev = this; |
duke@435 | 94 | } |
duke@435 | 95 | _head = this; |
duke@435 | 96 | } |
duke@435 | 97 | |
duke@435 | 98 | // set this as the state for the thread |
duke@435 | 99 | thread->set_jvmti_thread_state(this); |
duke@435 | 100 | } |
duke@435 | 101 | |
duke@435 | 102 | |
duke@435 | 103 | JvmtiThreadState::~JvmtiThreadState() { |
duke@435 | 104 | assert(JvmtiThreadState_lock->is_locked(), "sanity check"); |
duke@435 | 105 | |
duke@435 | 106 | // clear this as the state for the thread |
duke@435 | 107 | get_thread()->set_jvmti_thread_state(NULL); |
duke@435 | 108 | |
duke@435 | 109 | // zap our env thread states |
duke@435 | 110 | { |
duke@435 | 111 | JvmtiEnvBase::entering_dying_thread_env_iteration(); |
duke@435 | 112 | JvmtiEnvThreadStateIterator it(this); |
duke@435 | 113 | for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ) { |
duke@435 | 114 | JvmtiEnvThreadState* zap = ets; |
duke@435 | 115 | ets = it.next(ets); |
duke@435 | 116 | delete zap; |
duke@435 | 117 | } |
duke@435 | 118 | JvmtiEnvBase::leaving_dying_thread_env_iteration(); |
duke@435 | 119 | } |
duke@435 | 120 | |
duke@435 | 121 | // remove us from the list |
duke@435 | 122 | { |
duke@435 | 123 | // The thread state list manipulation code must not have safepoints. |
duke@435 | 124 | // See periodic_clean_up(). |
duke@435 | 125 | debug_only(No_Safepoint_Verifier nosafepoint;) |
duke@435 | 126 | |
duke@435 | 127 | if (_prev == NULL) { |
duke@435 | 128 | assert(_head == this, "sanity check"); |
duke@435 | 129 | _head = _next; |
duke@435 | 130 | } else { |
duke@435 | 131 | assert(_head != this, "sanity check"); |
duke@435 | 132 | _prev->_next = _next; |
duke@435 | 133 | } |
duke@435 | 134 | if (_next != NULL) { |
duke@435 | 135 | _next->_prev = _prev; |
duke@435 | 136 | } |
duke@435 | 137 | _next = NULL; |
duke@435 | 138 | _prev = NULL; |
duke@435 | 139 | } |
duke@435 | 140 | } |
duke@435 | 141 | |
duke@435 | 142 | |
duke@435 | 143 | void |
duke@435 | 144 | JvmtiThreadState::periodic_clean_up() { |
duke@435 | 145 | assert(SafepointSynchronize::is_at_safepoint(), "at safepoint"); |
duke@435 | 146 | |
duke@435 | 147 | // This iteration is initialized with "_head" instead of "JvmtiThreadState::first()" |
duke@435 | 148 | // because the latter requires the JvmtiThreadState_lock. |
duke@435 | 149 | // This iteration is safe at a safepoint as well, see the No_Safepoint_Verifier |
duke@435 | 150 | // asserts at all list manipulation sites. |
duke@435 | 151 | for (JvmtiThreadState *state = _head; state != NULL; state = state->next()) { |
duke@435 | 152 | // For each environment thread state corresponding to an invalid environment |
duke@435 | 153 | // unlink it from the list and deallocate it. |
duke@435 | 154 | JvmtiEnvThreadStateIterator it(state); |
duke@435 | 155 | JvmtiEnvThreadState* previous_ets = NULL; |
duke@435 | 156 | JvmtiEnvThreadState* ets = it.first(); |
duke@435 | 157 | while (ets != NULL) { |
duke@435 | 158 | if (ets->get_env()->is_valid()) { |
duke@435 | 159 | previous_ets = ets; |
duke@435 | 160 | ets = it.next(ets); |
duke@435 | 161 | } else { |
duke@435 | 162 | // This one isn't valid, remove it from the list and deallocate it |
duke@435 | 163 | JvmtiEnvThreadState* defunct_ets = ets; |
duke@435 | 164 | ets = ets->next(); |
duke@435 | 165 | if (previous_ets == NULL) { |
duke@435 | 166 | assert(state->head_env_thread_state() == defunct_ets, "sanity check"); |
duke@435 | 167 | state->set_head_env_thread_state(ets); |
duke@435 | 168 | } else { |
duke@435 | 169 | previous_ets->set_next(ets); |
duke@435 | 170 | } |
duke@435 | 171 | delete defunct_ets; |
duke@435 | 172 | } |
duke@435 | 173 | } |
duke@435 | 174 | } |
duke@435 | 175 | } |
duke@435 | 176 | |
duke@435 | 177 | void JvmtiThreadState::add_env(JvmtiEnvBase *env) { |
duke@435 | 178 | assert(JvmtiThreadState_lock->is_locked(), "sanity check"); |
duke@435 | 179 | |
duke@435 | 180 | JvmtiEnvThreadState *new_ets = new JvmtiEnvThreadState(_thread, env); |
duke@435 | 181 | // add this environment thread state to the end of the list (order is important) |
duke@435 | 182 | { |
duke@435 | 183 | // list deallocation (which occurs at a safepoint) cannot occur simultaneously |
duke@435 | 184 | debug_only(No_Safepoint_Verifier nosafepoint;) |
duke@435 | 185 | |
duke@435 | 186 | JvmtiEnvThreadStateIterator it(this); |
duke@435 | 187 | JvmtiEnvThreadState* previous_ets = NULL; |
duke@435 | 188 | for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { |
duke@435 | 189 | previous_ets = ets; |
duke@435 | 190 | } |
duke@435 | 191 | if (previous_ets == NULL) { |
duke@435 | 192 | set_head_env_thread_state(new_ets); |
duke@435 | 193 | } else { |
duke@435 | 194 | previous_ets->set_next(new_ets); |
duke@435 | 195 | } |
duke@435 | 196 | } |
duke@435 | 197 | } |
duke@435 | 198 | |
duke@435 | 199 | |
duke@435 | 200 | |
duke@435 | 201 | |
duke@435 | 202 | void JvmtiThreadState::enter_interp_only_mode() { |
duke@435 | 203 | assert(_thread->get_interp_only_mode() == 0, "entering interp only when mode not zero"); |
duke@435 | 204 | _thread->increment_interp_only_mode(); |
duke@435 | 205 | } |
duke@435 | 206 | |
duke@435 | 207 | |
duke@435 | 208 | void JvmtiThreadState::leave_interp_only_mode() { |
duke@435 | 209 | assert(_thread->get_interp_only_mode() == 1, "leaving interp only when mode not one"); |
duke@435 | 210 | _thread->decrement_interp_only_mode(); |
duke@435 | 211 | } |
duke@435 | 212 | |
duke@435 | 213 | |
duke@435 | 214 | // Helper routine used in several places |
duke@435 | 215 | int JvmtiThreadState::count_frames() { |
duke@435 | 216 | #ifdef ASSERT |
duke@435 | 217 | uint32_t debug_bits = 0; |
duke@435 | 218 | #endif |
duke@435 | 219 | assert(SafepointSynchronize::is_at_safepoint() || |
duke@435 | 220 | JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits), |
duke@435 | 221 | "at safepoint or must be suspended"); |
duke@435 | 222 | |
duke@435 | 223 | if (!get_thread()->has_last_Java_frame()) return 0; // no Java frames |
duke@435 | 224 | |
duke@435 | 225 | ResourceMark rm; |
duke@435 | 226 | RegisterMap reg_map(get_thread()); |
duke@435 | 227 | javaVFrame *jvf = get_thread()->last_java_vframe(®_map); |
duke@435 | 228 | int n = 0; |
duke@435 | 229 | // tty->print_cr("CSD: counting frames on %s ...", |
duke@435 | 230 | // JvmtiTrace::safe_get_thread_name(get_thread())); |
duke@435 | 231 | while (jvf != NULL) { |
duke@435 | 232 | methodOop method = jvf->method(); |
duke@435 | 233 | // tty->print_cr("CSD: frame - method %s.%s - loc %d", |
duke@435 | 234 | // method->klass_name()->as_C_string(), |
duke@435 | 235 | // method->name()->as_C_string(), |
duke@435 | 236 | // jvf->bci() ); |
duke@435 | 237 | jvf = jvf->java_sender(); |
duke@435 | 238 | n++; |
duke@435 | 239 | } |
duke@435 | 240 | // tty->print_cr("CSD: frame count: %d", n); |
duke@435 | 241 | return n; |
duke@435 | 242 | } |
duke@435 | 243 | |
duke@435 | 244 | |
duke@435 | 245 | void JvmtiThreadState::invalidate_cur_stack_depth() { |
duke@435 | 246 | Thread *cur = Thread::current(); |
duke@435 | 247 | uint32_t debug_bits = 0; |
duke@435 | 248 | |
duke@435 | 249 | // The caller can be the VMThread at a safepoint, the current thread |
duke@435 | 250 | // or the target thread must be suspended. |
duke@435 | 251 | guarantee((cur->is_VM_thread() && SafepointSynchronize::is_at_safepoint()) || |
duke@435 | 252 | (JavaThread *)cur == get_thread() || |
duke@435 | 253 | JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits), |
duke@435 | 254 | "sanity check"); |
duke@435 | 255 | |
duke@435 | 256 | _cur_stack_depth = UNKNOWN_STACK_DEPTH; |
duke@435 | 257 | } |
duke@435 | 258 | |
duke@435 | 259 | void JvmtiThreadState::incr_cur_stack_depth() { |
duke@435 | 260 | guarantee(JavaThread::current() == get_thread(), "must be current thread"); |
duke@435 | 261 | |
duke@435 | 262 | if (!is_interp_only_mode()) { |
duke@435 | 263 | _cur_stack_depth = UNKNOWN_STACK_DEPTH; |
duke@435 | 264 | } |
duke@435 | 265 | if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) { |
duke@435 | 266 | ++_cur_stack_depth; |
duke@435 | 267 | } |
duke@435 | 268 | } |
duke@435 | 269 | |
duke@435 | 270 | void JvmtiThreadState::decr_cur_stack_depth() { |
duke@435 | 271 | guarantee(JavaThread::current() == get_thread(), "must be current thread"); |
duke@435 | 272 | |
duke@435 | 273 | if (!is_interp_only_mode()) { |
duke@435 | 274 | _cur_stack_depth = UNKNOWN_STACK_DEPTH; |
duke@435 | 275 | } |
duke@435 | 276 | if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) { |
duke@435 | 277 | --_cur_stack_depth; |
duke@435 | 278 | assert(_cur_stack_depth >= 0, "incr/decr_cur_stack_depth mismatch"); |
duke@435 | 279 | } |
duke@435 | 280 | } |
duke@435 | 281 | |
duke@435 | 282 | int JvmtiThreadState::cur_stack_depth() { |
duke@435 | 283 | uint32_t debug_bits = 0; |
duke@435 | 284 | guarantee(JavaThread::current() == get_thread() || |
duke@435 | 285 | JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits), |
duke@435 | 286 | "must be current thread or suspended"); |
duke@435 | 287 | |
duke@435 | 288 | if (!is_interp_only_mode() || _cur_stack_depth == UNKNOWN_STACK_DEPTH) { |
duke@435 | 289 | _cur_stack_depth = count_frames(); |
duke@435 | 290 | } else { |
duke@435 | 291 | // heavy weight assert |
duke@435 | 292 | assert(_cur_stack_depth == count_frames(), |
duke@435 | 293 | "cur_stack_depth out of sync"); |
duke@435 | 294 | } |
duke@435 | 295 | return _cur_stack_depth; |
duke@435 | 296 | } |
duke@435 | 297 | |
duke@435 | 298 | bool JvmtiThreadState::may_be_walked() { |
duke@435 | 299 | return (get_thread()->is_being_ext_suspended() || (JavaThread::current() == get_thread())); |
duke@435 | 300 | } |
duke@435 | 301 | |
duke@435 | 302 | |
duke@435 | 303 | void JvmtiThreadState::process_pending_step_for_popframe() { |
duke@435 | 304 | // We are single stepping as the last part of the PopFrame() dance |
duke@435 | 305 | // so we have some house keeping to do. |
duke@435 | 306 | |
duke@435 | 307 | JavaThread *thr = get_thread(); |
duke@435 | 308 | if (thr->popframe_condition() != JavaThread::popframe_inactive) { |
duke@435 | 309 | // If the popframe_condition field is not popframe_inactive, then |
duke@435 | 310 | // we missed all of the popframe_field cleanup points: |
duke@435 | 311 | // |
duke@435 | 312 | // - unpack_frames() was not called (nothing to deopt) |
duke@435 | 313 | // - remove_activation_preserving_args_entry() was not called |
duke@435 | 314 | // (did not get suspended in a call_vm() family call and did |
duke@435 | 315 | // not complete a call_vm() family call on the way here) |
duke@435 | 316 | thr->clear_popframe_condition(); |
duke@435 | 317 | } |
duke@435 | 318 | |
duke@435 | 319 | // clearing the flag indicates we are done with the PopFrame() dance |
duke@435 | 320 | clr_pending_step_for_popframe(); |
duke@435 | 321 | |
duke@435 | 322 | // If step is pending for popframe then it may not be |
duke@435 | 323 | // a repeat step. The new_bci and method_id is same as current_bci |
duke@435 | 324 | // and current method_id after pop and step for recursive calls. |
duke@435 | 325 | // Force the step by clearing the last location. |
duke@435 | 326 | JvmtiEnvThreadStateIterator it(this); |
duke@435 | 327 | for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { |
duke@435 | 328 | ets->clear_current_location(); |
duke@435 | 329 | } |
duke@435 | 330 | } |
duke@435 | 331 | |
duke@435 | 332 | |
duke@435 | 333 | // Class: JvmtiThreadState |
duke@435 | 334 | // Function: update_for_pop_top_frame |
duke@435 | 335 | // Description: |
duke@435 | 336 | // This function removes any frame pop notification request for |
duke@435 | 337 | // the top frame and invalidates both the current stack depth and |
duke@435 | 338 | // all cached frameIDs. |
duke@435 | 339 | // |
duke@435 | 340 | // Called by: PopFrame |
duke@435 | 341 | // |
duke@435 | 342 | void JvmtiThreadState::update_for_pop_top_frame() { |
duke@435 | 343 | if (is_interp_only_mode()) { |
duke@435 | 344 | // remove any frame pop notification request for the top frame |
duke@435 | 345 | // in any environment |
duke@435 | 346 | int popframe_number = cur_stack_depth(); |
duke@435 | 347 | { |
duke@435 | 348 | JvmtiEnvThreadStateIterator it(this); |
duke@435 | 349 | for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { |
duke@435 | 350 | if (ets->is_frame_pop(popframe_number)) { |
duke@435 | 351 | ets->clear_frame_pop(popframe_number); |
duke@435 | 352 | } |
duke@435 | 353 | } |
duke@435 | 354 | } |
duke@435 | 355 | // force stack depth to be recalculated |
duke@435 | 356 | invalidate_cur_stack_depth(); |
duke@435 | 357 | } else { |
duke@435 | 358 | assert(!is_enabled(JVMTI_EVENT_FRAME_POP), "Must have no framepops set"); |
duke@435 | 359 | } |
duke@435 | 360 | } |
duke@435 | 361 | |
duke@435 | 362 | |
duke@435 | 363 | void JvmtiThreadState::process_pending_step_for_earlyret() { |
duke@435 | 364 | // We are single stepping as the last part of the ForceEarlyReturn |
duke@435 | 365 | // dance so we have some house keeping to do. |
duke@435 | 366 | |
duke@435 | 367 | if (is_earlyret_pending()) { |
duke@435 | 368 | // If the earlyret_state field is not earlyret_inactive, then |
duke@435 | 369 | // we missed all of the earlyret_field cleanup points: |
duke@435 | 370 | // |
duke@435 | 371 | // - remove_activation() was not called |
duke@435 | 372 | // (did not get suspended in a call_vm() family call and did |
duke@435 | 373 | // not complete a call_vm() family call on the way here) |
duke@435 | 374 | // |
duke@435 | 375 | // One legitimate way for us to miss all the cleanup points is |
duke@435 | 376 | // if we got here right after handling a compiled return. If that |
duke@435 | 377 | // is the case, then we consider our return from compiled code to |
duke@435 | 378 | // complete the ForceEarlyReturn request and we clear the condition. |
duke@435 | 379 | clr_earlyret_pending(); |
duke@435 | 380 | set_earlyret_oop(NULL); |
duke@435 | 381 | clr_earlyret_value(); |
duke@435 | 382 | } |
duke@435 | 383 | |
duke@435 | 384 | // clearing the flag indicates we are done with |
duke@435 | 385 | // the ForceEarlyReturn() dance |
duke@435 | 386 | clr_pending_step_for_earlyret(); |
duke@435 | 387 | |
duke@435 | 388 | // If step is pending for earlyret then it may not be a repeat step. |
duke@435 | 389 | // The new_bci and method_id is same as current_bci and current |
duke@435 | 390 | // method_id after earlyret and step for recursive calls. |
duke@435 | 391 | // Force the step by clearing the last location. |
duke@435 | 392 | JvmtiEnvThreadStateIterator it(this); |
duke@435 | 393 | for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { |
duke@435 | 394 | ets->clear_current_location(); |
duke@435 | 395 | } |
duke@435 | 396 | } |
duke@435 | 397 | |
duke@435 | 398 | void JvmtiThreadState::oops_do(OopClosure* f) { |
duke@435 | 399 | f->do_oop((oop*) &_earlyret_oop); |
duke@435 | 400 | } |