Wed, 14 Mar 2012 20:06:48 -0700
7123170: JCK vm/jvmti/ResourceExhausted/resexh001/resexh00101/ tests fails since 7u4 b02
Summary: The JVMTI ResourceExhausted events must be generated in all places where OOME is thrown
Reviewed-by: acorn, coleenp, dcubed, dholmes, dsamersoff, jwilhelm, tonyp
Contributed-by: serguei.spitsyn@oracle.com
1 /*
2 * Copyright (c) 2003, 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 #include "precompiled.hpp"
26 #include "code/codeBlob.hpp"
27 #include "code/codeCache.hpp"
28 #include "code/scopeDesc.hpp"
29 #include "memory/resourceArea.hpp"
30 #include "oops/oop.inline.hpp"
31 #include "prims/jvmtiCodeBlobEvents.hpp"
32 #include "prims/jvmtiExport.hpp"
33 #include "runtime/handles.hpp"
34 #include "runtime/handles.inline.hpp"
35 #include "runtime/vmThread.hpp"
37 // Support class to collect a list of the non-nmethod CodeBlobs in
38 // the CodeCache.
39 //
40 // This class actually creates a list of JvmtiCodeBlobDesc - each JvmtiCodeBlobDesc
41 // describes a single CodeBlob in the CodeCache. Note that collection is
42 // done to a static list - this is because CodeCache::blobs_do is defined
43 // as void CodeCache::blobs_do(void f(CodeBlob* nm)) and hence requires
44 // a C or static method.
45 //
46 // Usage :-
47 //
48 // CodeBlobCollector collector;
49 //
50 // collector.collect();
51 // JvmtiCodeBlobDesc* blob = collector.first();
52 // while (blob != NULL) {
53 // :
54 // blob = collector.next();
55 // }
56 //
58 class CodeBlobCollector : StackObj {
59 private:
60 GrowableArray<JvmtiCodeBlobDesc*>* _code_blobs; // collected blobs
61 int _pos; // iterator position
63 // used during a collection
64 static GrowableArray<JvmtiCodeBlobDesc*>* _global_code_blobs;
65 static void do_blob(CodeBlob* cb);
66 public:
67 CodeBlobCollector() {
68 _code_blobs = NULL;
69 _pos = -1;
70 }
71 ~CodeBlobCollector() {
72 if (_code_blobs != NULL) {
73 for (int i=0; i<_code_blobs->length(); i++) {
74 FreeHeap(_code_blobs->at(i));
75 }
76 delete _code_blobs;
77 }
78 }
80 // collect list of code blobs in the cache
81 void collect();
83 // iteration support - return first code blob
84 JvmtiCodeBlobDesc* first() {
85 assert(_code_blobs != NULL, "not collected");
86 if (_code_blobs->length() == 0) {
87 return NULL;
88 }
89 _pos = 0;
90 return _code_blobs->at(0);
91 }
93 // iteration support - return next code blob
94 JvmtiCodeBlobDesc* next() {
95 assert(_pos >= 0, "iteration not started");
96 if (_pos+1 >= _code_blobs->length()) {
97 return NULL;
98 }
99 return _code_blobs->at(++_pos);
100 }
102 };
104 // used during collection
105 GrowableArray<JvmtiCodeBlobDesc*>* CodeBlobCollector::_global_code_blobs;
108 // called for each CodeBlob in the CodeCache
109 //
110 // This function filters out nmethods as it is only interested in
111 // other CodeBlobs. This function also filters out CodeBlobs that have
112 // a duplicate starting address as previous blobs. This is needed to
113 // handle the case where multiple stubs are generated into a single
114 // BufferBlob.
116 void CodeBlobCollector::do_blob(CodeBlob* cb) {
118 // ignore nmethods
119 if (cb->is_nmethod()) {
120 return;
121 }
123 // check if this starting address has been seen already - the
124 // assumption is that stubs are inserted into the list before the
125 // enclosing BufferBlobs.
126 address addr = cb->code_begin();
127 for (int i=0; i<_global_code_blobs->length(); i++) {
128 JvmtiCodeBlobDesc* scb = _global_code_blobs->at(i);
129 if (addr == scb->code_begin()) {
130 return;
131 }
132 }
134 // record the CodeBlob details as a JvmtiCodeBlobDesc
135 JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(cb->name(), cb->code_begin(), cb->code_end());
136 _global_code_blobs->append(scb);
137 }
140 // collects a list of CodeBlobs in the CodeCache.
141 //
142 // The created list is growable array of JvmtiCodeBlobDesc - each one describes
143 // a CodeBlob. Note that the list is static - this is because CodeBlob::blobs_do
144 // requires a a C or static function so we can't use an instance function. This
145 // isn't a problem as the iteration is serial anyway as we need the CodeCache_lock
146 // to iterate over the code cache.
147 //
148 // Note that the CodeBlobs in the CodeCache will include BufferBlobs that may
149 // contain multiple stubs. As a profiler is interested in the stubs rather than
150 // the enclosing container we first iterate over the stub code descriptors so
151 // that the stubs go into the list first. do_blob will then filter out the
152 // enclosing blobs if the starting address of the enclosing blobs matches the
153 // starting address of first stub generated in the enclosing blob.
155 void CodeBlobCollector::collect() {
156 assert_locked_or_safepoint(CodeCache_lock);
157 assert(_global_code_blobs == NULL, "checking");
159 // create the global list
160 _global_code_blobs = new (ResourceObj::C_HEAP) GrowableArray<JvmtiCodeBlobDesc*>(50,true);
162 // iterate over the stub code descriptors and put them in the list first.
163 int index = 0;
164 StubCodeDesc* desc;
165 while ((desc = StubCodeDesc::desc_for_index(++index)) != NULL) {
166 _global_code_blobs->append(new JvmtiCodeBlobDesc(desc->name(), desc->begin(), desc->end()));
167 }
169 // next iterate over all the non-nmethod code blobs and add them to
170 // the list - as noted above this will filter out duplicates and
171 // enclosing blobs.
172 CodeCache::blobs_do(do_blob);
174 // make the global list the instance list so that it can be used
175 // for other iterations.
176 _code_blobs = _global_code_blobs;
177 _global_code_blobs = NULL;
178 }
181 // Generate a DYNAMIC_CODE_GENERATED event for each non-nmethod code blob.
183 jvmtiError JvmtiCodeBlobEvents::generate_dynamic_code_events(JvmtiEnv* env) {
184 CodeBlobCollector collector;
186 // First collect all the code blobs. This has to be done in a
187 // single pass over the code cache with CodeCache_lock held because
188 // there isn't any safe way to iterate over regular CodeBlobs since
189 // they can be freed at any point.
190 {
191 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
192 collector.collect();
193 }
195 // iterate over the collected list and post an event for each blob
196 JvmtiCodeBlobDesc* blob = collector.first();
197 while (blob != NULL) {
198 JvmtiExport::post_dynamic_code_generated(env, blob->name(), blob->code_begin(), blob->code_end());
199 blob = collector.next();
200 }
201 return JVMTI_ERROR_NONE;
202 }
205 // Generate a COMPILED_METHOD_LOAD event for each nnmethod
206 jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) {
207 HandleMark hm;
209 // Walk the CodeCache notifying for live nmethods. The code cache
210 // may be changing while this is happening which is ok since newly
211 // created nmethod will notify normally and nmethods which are freed
212 // can be safely skipped.
213 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
214 nmethod* current = CodeCache::first_nmethod();
215 while (current != NULL) {
216 // Only notify for live nmethods
217 if (current->is_alive()) {
218 // Lock the nmethod so it can't be freed
219 nmethodLocker nml(current);
221 // Don't hold the lock over the notify or jmethodID creation
222 MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
223 current->get_and_cache_jmethod_id();
224 JvmtiExport::post_compiled_method_load(current);
225 }
226 current = CodeCache::next_nmethod(current);
227 }
228 return JVMTI_ERROR_NONE;
229 }
232 // create a C-heap allocated address location map for an nmethod
233 void JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nmethod *nm,
234 jvmtiAddrLocationMap** map_ptr,
235 jint *map_length_ptr)
236 {
237 ResourceMark rm;
238 jvmtiAddrLocationMap* map = NULL;
239 jint map_length = 0;
242 // Generate line numbers using PcDesc and ScopeDesc info
243 methodHandle mh(nm->method());
245 if (!mh->is_native()) {
246 PcDesc *pcd;
247 int pcds_in_method;
249 pcds_in_method = (nm->scopes_pcs_end() - nm->scopes_pcs_begin());
250 map = NEW_C_HEAP_ARRAY(jvmtiAddrLocationMap, pcds_in_method);
252 address scopes_data = nm->scopes_data_begin();
253 for( pcd = nm->scopes_pcs_begin(); pcd < nm->scopes_pcs_end(); ++pcd ) {
254 ScopeDesc sc0(nm, pcd->scope_decode_offset(), pcd->should_reexecute(), pcd->return_oop());
255 ScopeDesc *sd = &sc0;
256 while( !sd->is_top() ) { sd = sd->sender(); }
257 int bci = sd->bci();
258 if (bci != InvocationEntryBci) {
259 assert(map_length < pcds_in_method, "checking");
260 map[map_length].start_address = (const void*)pcd->real_pc(nm);
261 map[map_length].location = bci;
262 ++map_length;
263 }
264 }
265 }
267 *map_ptr = map;
268 *map_length_ptr = map_length;
269 }