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
1 /*
2 * Copyright (c) 1999, 2010, 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 */
25 #ifndef SHARE_VM_PRIMS_JVMTIIMPL_HPP
26 #define SHARE_VM_PRIMS_JVMTIIMPL_HPP
28 #ifndef JVMTI_KERNEL
30 #include "classfile/systemDictionary.hpp"
31 #include "jvmtifiles/jvmti.h"
32 #include "oops/objArrayOop.hpp"
33 #include "prims/jvmtiEnvThreadState.hpp"
34 #include "prims/jvmtiEventController.hpp"
35 #include "prims/jvmtiTrace.hpp"
36 #include "prims/jvmtiUtil.hpp"
37 #include "runtime/stackValueCollection.hpp"
38 #include "runtime/vm_operations.hpp"
40 //
41 // Forward Declarations
42 //
44 class JvmtiBreakpoint;
45 class JvmtiBreakpoints;
48 ///////////////////////////////////////////////////////////////
49 //
50 // class GrowableCache, GrowableElement
51 // Used by : JvmtiBreakpointCache
52 // Used by JVMTI methods: none directly.
53 //
54 // GrowableCache is a permanent CHeap growable array of <GrowableElement *>
55 //
56 // In addition, the GrowableCache maintains a NULL terminated cache array of type address
57 // that's created from the element array using the function:
58 // address GrowableElement::getCacheValue().
59 //
60 // Whenever the GrowableArray changes size, the cache array gets recomputed into a new C_HEAP allocated
61 // block of memory. Additionally, every time the cache changes its position in memory, the
62 // void (*_listener_fun)(void *this_obj, address* cache)
63 // gets called with the cache's new address. This gives the user of the GrowableCache a callback
64 // to update its pointer to the address cache.
65 //
67 class GrowableElement : public CHeapObj {
68 public:
69 virtual address getCacheValue() =0;
70 virtual bool equals(GrowableElement* e) =0;
71 virtual bool lessThan(GrowableElement *e)=0;
72 virtual GrowableElement *clone() =0;
73 virtual void oops_do(OopClosure* f) =0;
74 };
76 class GrowableCache VALUE_OBJ_CLASS_SPEC {
78 private:
79 // Object pointer passed into cache & listener functions.
80 void *_this_obj;
82 // Array of elements in the collection
83 GrowableArray<GrowableElement *> *_elements;
85 // Parallel array of cached values
86 address *_cache;
88 // Listener for changes to the _cache field.
89 // Called whenever the _cache field has it's value changed
90 // (but NOT when cached elements are recomputed).
91 void (*_listener_fun)(void *, address*);
93 static bool equals(void *, GrowableElement *);
95 // recache all elements after size change, notify listener
96 void recache();
98 public:
99 GrowableCache();
100 ~GrowableCache();
102 void initialize(void *this_obj, void listener_fun(void *, address*) );
104 // number of elements in the collection
105 int length();
106 // get the value of the index element in the collection
107 GrowableElement* at(int index);
108 // find the index of the element, -1 if it doesn't exist
109 int find(GrowableElement* e);
110 // append a copy of the element to the end of the collection, notify listener
111 void append(GrowableElement* e);
112 // insert a copy of the element using lessthan(), notify listener
113 void insert(GrowableElement* e);
114 // remove the element at index, notify listener
115 void remove (int index);
116 // clear out all elements and release all heap space, notify listener
117 void clear();
118 // apply f to every element and update the cache
119 void oops_do(OopClosure* f);
120 void gc_epilogue();
121 };
124 ///////////////////////////////////////////////////////////////
125 //
126 // class JvmtiBreakpointCache
127 // Used by : JvmtiBreakpoints
128 // Used by JVMTI methods: none directly.
129 // Note : typesafe wrapper for GrowableCache of JvmtiBreakpoint
130 //
132 class JvmtiBreakpointCache : public CHeapObj {
134 private:
135 GrowableCache _cache;
137 public:
138 JvmtiBreakpointCache() {}
139 ~JvmtiBreakpointCache() {}
141 void initialize(void *this_obj, void listener_fun(void *, address*) ) {
142 _cache.initialize(this_obj,listener_fun);
143 }
145 int length() { return _cache.length(); }
146 JvmtiBreakpoint& at(int index) { return (JvmtiBreakpoint&) *(_cache.at(index)); }
147 int find(JvmtiBreakpoint& e) { return _cache.find((GrowableElement *) &e); }
148 void append(JvmtiBreakpoint& e) { _cache.append((GrowableElement *) &e); }
149 void remove (int index) { _cache.remove(index); }
150 void clear() { _cache.clear(); }
151 void oops_do(OopClosure* f) { _cache.oops_do(f); }
152 void gc_epilogue() { _cache.gc_epilogue(); }
153 };
156 ///////////////////////////////////////////////////////////////
157 //
158 // class JvmtiBreakpoint
159 // Used by : JvmtiBreakpoints
160 // Used by JVMTI methods: SetBreakpoint, ClearBreakpoint, ClearAllBreakpoints
161 // Note: Extends GrowableElement for use in a GrowableCache
162 //
163 // A JvmtiBreakpoint describes a location (class, method, bci) to break at.
164 //
166 typedef void (methodOopDesc::*method_action)(int _bci);
168 class JvmtiBreakpoint : public GrowableElement {
169 private:
170 methodOop _method;
171 int _bci;
172 Bytecodes::Code _orig_bytecode;
174 public:
175 JvmtiBreakpoint();
176 JvmtiBreakpoint(methodOop m_method, jlocation location);
177 bool equals(JvmtiBreakpoint& bp);
178 bool lessThan(JvmtiBreakpoint &bp);
179 void copy(JvmtiBreakpoint& bp);
180 bool is_valid();
181 address getBcp();
182 void each_method_version_do(method_action meth_act);
183 void set();
184 void clear();
185 void print();
187 methodOop method() { return _method; }
189 // GrowableElement implementation
190 address getCacheValue() { return getBcp(); }
191 bool lessThan(GrowableElement* e) { Unimplemented(); return false; }
192 bool equals(GrowableElement* e) { return equals((JvmtiBreakpoint&) *e); }
193 void oops_do(OopClosure* f) { f->do_oop((oop *) &_method); }
194 GrowableElement *clone() {
195 JvmtiBreakpoint *bp = new JvmtiBreakpoint();
196 bp->copy(*this);
197 return bp;
198 }
199 };
202 ///////////////////////////////////////////////////////////////
203 //
204 // class VM_ChangeBreakpoints
205 // Used by : JvmtiBreakpoints
206 // Used by JVMTI methods: none directly.
207 // Note: A Helper class.
208 //
209 // VM_ChangeBreakpoints implements a VM_Operation for ALL modifications to the JvmtiBreakpoints class.
210 //
212 class VM_ChangeBreakpoints : public VM_Operation {
213 private:
214 JvmtiBreakpoints* _breakpoints;
215 int _operation;
216 JvmtiBreakpoint* _bp;
218 public:
219 enum { SET_BREAKPOINT=0, CLEAR_BREAKPOINT=1, CLEAR_ALL_BREAKPOINT=2 };
221 VM_ChangeBreakpoints(JvmtiBreakpoints* breakpoints, int operation) {
222 _breakpoints = breakpoints;
223 _bp = NULL;
224 _operation = operation;
225 assert(breakpoints != NULL, "breakpoints != NULL");
226 assert(operation == CLEAR_ALL_BREAKPOINT, "unknown breakpoint operation");
227 }
228 VM_ChangeBreakpoints(JvmtiBreakpoints* breakpoints, int operation, JvmtiBreakpoint *bp) {
229 _breakpoints = breakpoints;
230 _bp = bp;
231 _operation = operation;
232 assert(breakpoints != NULL, "breakpoints != NULL");
233 assert(bp != NULL, "bp != NULL");
234 assert(operation == SET_BREAKPOINT || operation == CLEAR_BREAKPOINT , "unknown breakpoint operation");
235 }
237 VMOp_Type type() const { return VMOp_ChangeBreakpoints; }
238 void doit();
239 void oops_do(OopClosure* f);
240 };
243 ///////////////////////////////////////////////////////////////
244 //
245 // class JvmtiBreakpoints
246 // Used by : JvmtiCurrentBreakpoints
247 // Used by JVMTI methods: none directly
248 // Note: A Helper class
249 //
250 // JvmtiBreakpoints is a GrowableCache of JvmtiBreakpoint.
251 // All changes to the GrowableCache occur at a safepoint using VM_ChangeBreakpoints.
252 //
253 // Because _bps is only modified at safepoints, its possible to always use the
254 // cached byte code pointers from _bps without doing any synchronization (see JvmtiCurrentBreakpoints).
255 //
256 // It would be possible to make JvmtiBreakpoints a static class, but I've made it
257 // CHeap allocated to emphasize its similarity to JvmtiFramePops.
258 //
260 class JvmtiBreakpoints : public CHeapObj {
261 private:
263 JvmtiBreakpointCache _bps;
265 // These should only be used by VM_ChangeBreakpoints
266 // to insure they only occur at safepoints.
267 // Todo: add checks for safepoint
268 friend class VM_ChangeBreakpoints;
269 void set_at_safepoint(JvmtiBreakpoint& bp);
270 void clear_at_safepoint(JvmtiBreakpoint& bp);
271 void clearall_at_safepoint();
273 static void do_element(GrowableElement *e);
275 public:
276 JvmtiBreakpoints(void listener_fun(void *, address *));
277 ~JvmtiBreakpoints();
279 int length();
280 void oops_do(OopClosure* f);
281 void gc_epilogue();
282 void print();
284 int set(JvmtiBreakpoint& bp);
285 int clear(JvmtiBreakpoint& bp);
286 void clearall_in_class_at_safepoint(klassOop klass);
287 void clearall();
288 };
291 ///////////////////////////////////////////////////////////////
292 //
293 // class JvmtiCurrentBreakpoints
294 //
295 // A static wrapper class for the JvmtiBreakpoints that provides:
296 // 1. a fast inlined function to check if a byte code pointer is a breakpoint (is_breakpoint).
297 // 2. a function for lazily creating the JvmtiBreakpoints class (this is not strictly necessary,
298 // but I'm copying the code from JvmtiThreadState which needs to lazily initialize
299 // JvmtiFramePops).
300 // 3. An oops_do entry point for GC'ing the breakpoint array.
301 //
303 class JvmtiCurrentBreakpoints : public AllStatic {
305 private:
307 // Current breakpoints, lazily initialized by get_jvmti_breakpoints();
308 static JvmtiBreakpoints *_jvmti_breakpoints;
310 // NULL terminated cache of byte-code pointers corresponding to current breakpoints.
311 // Updated only at safepoints (with listener_fun) when the cache is moved.
312 // It exists only to make is_breakpoint fast.
313 static address *_breakpoint_list;
314 static inline void set_breakpoint_list(address *breakpoint_list) { _breakpoint_list = breakpoint_list; }
315 static inline address *get_breakpoint_list() { return _breakpoint_list; }
317 // Listener for the GrowableCache in _jvmti_breakpoints, updates _breakpoint_list.
318 static void listener_fun(void *this_obj, address *cache);
320 public:
321 static void initialize();
322 static void destroy();
324 // lazily create _jvmti_breakpoints and _breakpoint_list
325 static JvmtiBreakpoints& get_jvmti_breakpoints();
327 // quickly test whether the bcp matches a cached breakpoint in the list
328 static inline bool is_breakpoint(address bcp);
330 static void oops_do(OopClosure* f);
331 static void gc_epilogue();
332 };
334 // quickly test whether the bcp matches a cached breakpoint in the list
335 bool JvmtiCurrentBreakpoints::is_breakpoint(address bcp) {
336 address *bps = get_breakpoint_list();
337 if (bps == NULL) return false;
338 for ( ; (*bps) != NULL; bps++) {
339 if ((*bps) == bcp) return true;
340 }
341 return false;
342 }
344 ///////////////////////////////////////////////////////////////
345 // The get/set local operations must only be done by the VM thread
346 // because the interpreter version needs to access oop maps, which can
347 // only safely be done by the VM thread
348 //
349 // I'm told that in 1.5 oop maps are now protected by a lock and
350 // we could get rid of the VM op
351 // However if the VM op is removed then the target thread must
352 // be suspended AND a lock will be needed to prevent concurrent
353 // setting of locals to the same java thread. This lock is needed
354 // to prevent compiledVFrames from trying to add deferred updates
355 // to the thread simultaneously.
356 //
357 class VM_GetOrSetLocal : public VM_Operation {
358 protected:
359 JavaThread* _thread;
360 JavaThread* _calling_thread;
361 jint _depth;
362 jint _index;
363 BasicType _type;
364 jvalue _value;
365 javaVFrame* _jvf;
366 bool _set;
368 // It is possible to get the receiver out of a non-static native wrapper
369 // frame. Use VM_GetReceiver to do this.
370 virtual bool getting_receiver() const { return false; }
372 jvmtiError _result;
374 vframe* get_vframe();
375 javaVFrame* get_java_vframe();
376 bool check_slot_type(javaVFrame* vf);
378 public:
379 // Constructor for non-object getter
380 VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type);
382 // Constructor for object or non-object setter
383 VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type, jvalue value);
385 // Constructor for object getter
386 VM_GetOrSetLocal(JavaThread* thread, JavaThread* calling_thread, jint depth,
387 int index);
389 VMOp_Type type() const { return VMOp_GetOrSetLocal; }
390 jvalue value() { return _value; }
391 jvmtiError result() { return _result; }
393 bool doit_prologue();
394 void doit();
395 bool allow_nested_vm_operations() const;
396 const char* name() const { return "get/set locals"; }
398 // Check that the klass is assignable to a type with the given signature.
399 static bool is_assignable(const char* ty_sign, Klass* klass, Thread* thread);
400 };
402 class VM_GetReceiver : public VM_GetOrSetLocal {
403 protected:
404 virtual bool getting_receiver() const { return true; }
406 public:
407 VM_GetReceiver(JavaThread* thread, JavaThread* calling_thread, jint depth);
408 const char* name() const { return "get receiver"; }
409 };
412 ///////////////////////////////////////////////////////////////
413 //
414 // class JvmtiSuspendControl
415 //
416 // Convenience routines for suspending and resuming threads.
417 //
418 // All attempts by JVMTI to suspend and resume threads must go through the
419 // JvmtiSuspendControl interface.
420 //
421 // methods return true if successful
422 //
423 class JvmtiSuspendControl : public AllStatic {
424 public:
425 // suspend the thread, taking it to a safepoint
426 static bool suspend(JavaThread *java_thread);
427 // resume the thread
428 static bool resume(JavaThread *java_thread);
430 static void print();
431 };
433 #endif // !JVMTI_KERNEL
435 // Utility macro that checks for NULL pointers:
436 #define NULL_CHECK(X, Y) if ((X) == NULL) { return (Y); }
438 #endif // SHARE_VM_PRIMS_JVMTIIMPL_HPP