Fri, 02 Jul 2010 15:01:47 -0700
6965671: fatal error: acquiring lock JNIGlobalHandle_lock/16 out of order with lock CodeCache_lock/1
Reviewed-by: kvn, dcubed
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 "incls/_precompiled.incl"
26 # include "incls/_jvmtiCodeBlobEvents.cpp.incl"
28 // Support class to collect a list of the non-nmethod CodeBlobs in
29 // the CodeCache.
30 //
31 // This class actually creates a list of JvmtiCodeBlobDesc - each JvmtiCodeBlobDesc
32 // describes a single CodeBlob in the CodeCache. Note that collection is
33 // done to a static list - this is because CodeCache::blobs_do is defined
34 // as void CodeCache::blobs_do(void f(CodeBlob* nm)) and hence requires
35 // a C or static method.
36 //
37 // Usage :-
38 //
39 // CodeBlobCollector collector;
40 //
41 // collector.collect();
42 // JvmtiCodeBlobDesc* blob = collector.first();
43 // while (blob != NULL) {
44 // :
45 // blob = collector.next();
46 // }
47 //
49 class CodeBlobCollector : StackObj {
50 private:
51 GrowableArray<JvmtiCodeBlobDesc*>* _code_blobs; // collected blobs
52 int _pos; // iterator position
54 // used during a collection
55 static GrowableArray<JvmtiCodeBlobDesc*>* _global_code_blobs;
56 static void do_blob(CodeBlob* cb);
57 public:
58 CodeBlobCollector() {
59 _code_blobs = NULL;
60 _pos = -1;
61 }
62 ~CodeBlobCollector() {
63 if (_code_blobs != NULL) {
64 for (int i=0; i<_code_blobs->length(); i++) {
65 FreeHeap(_code_blobs->at(i));
66 }
67 delete _code_blobs;
68 }
69 }
71 // collect list of code blobs in the cache
72 void collect();
74 // iteration support - return first code blob
75 JvmtiCodeBlobDesc* first() {
76 assert(_code_blobs != NULL, "not collected");
77 if (_code_blobs->length() == 0) {
78 return NULL;
79 }
80 _pos = 0;
81 return _code_blobs->at(0);
82 }
84 // iteration support - return next code blob
85 JvmtiCodeBlobDesc* next() {
86 assert(_pos >= 0, "iteration not started");
87 if (_pos+1 >= _code_blobs->length()) {
88 return NULL;
89 }
90 return _code_blobs->at(++_pos);
91 }
93 };
95 // used during collection
96 GrowableArray<JvmtiCodeBlobDesc*>* CodeBlobCollector::_global_code_blobs;
99 // called for each CodeBlob in the CodeCache
100 //
101 // This function filters out nmethods as it is only interested in
102 // other CodeBlobs. This function also filters out CodeBlobs that have
103 // a duplicate starting address as previous blobs. This is needed to
104 // handle the case where multiple stubs are generated into a single
105 // BufferBlob.
107 void CodeBlobCollector::do_blob(CodeBlob* cb) {
109 // ignore nmethods
110 if (cb->is_nmethod()) {
111 return;
112 }
114 // check if this starting address has been seen already - the
115 // assumption is that stubs are inserted into the list before the
116 // enclosing BufferBlobs.
117 address addr = cb->instructions_begin();
118 for (int i=0; i<_global_code_blobs->length(); i++) {
119 JvmtiCodeBlobDesc* scb = _global_code_blobs->at(i);
120 if (addr == scb->code_begin()) {
121 ShouldNotReachHere();
122 return;
123 }
124 }
126 // record the CodeBlob details as a JvmtiCodeBlobDesc
127 JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(cb->name(), cb->instructions_begin(),
128 cb->instructions_end());
129 _global_code_blobs->append(scb);
130 }
133 // collects a list of CodeBlobs in the CodeCache.
134 //
135 // The created list is growable array of JvmtiCodeBlobDesc - each one describes
136 // a CodeBlob. Note that the list is static - this is because CodeBlob::blobs_do
137 // requires a a C or static function so we can't use an instance function. This
138 // isn't a problem as the iteration is serial anyway as we need the CodeCache_lock
139 // to iterate over the code cache.
140 //
141 // Note that the CodeBlobs in the CodeCache will include BufferBlobs that may
142 // contain multiple stubs. As a profiler is interested in the stubs rather than
143 // the enclosing container we first iterate over the stub code descriptors so
144 // that the stubs go into the list first. do_blob will then filter out the
145 // enclosing blobs if the starting address of the enclosing blobs matches the
146 // starting address of first stub generated in the enclosing blob.
148 void CodeBlobCollector::collect() {
149 assert_locked_or_safepoint(CodeCache_lock);
150 assert(_global_code_blobs == NULL, "checking");
152 // create the global list
153 _global_code_blobs = new (ResourceObj::C_HEAP) GrowableArray<JvmtiCodeBlobDesc*>(50,true);
155 // iterate over the stub code descriptors and put them in the list first.
156 int index = 0;
157 StubCodeDesc* desc;
158 while ((desc = StubCodeDesc::desc_for_index(++index)) != NULL) {
159 _global_code_blobs->append(new JvmtiCodeBlobDesc(desc->name(), desc->begin(), desc->end()));
160 }
162 // next iterate over all the non-nmethod code blobs and add them to
163 // the list - as noted above this will filter out duplicates and
164 // enclosing blobs.
165 CodeCache::blobs_do(do_blob);
167 // make the global list the instance list so that it can be used
168 // for other iterations.
169 _code_blobs = _global_code_blobs;
170 _global_code_blobs = NULL;
171 }
174 // Generate a DYNAMIC_CODE_GENERATED event for each non-nmethod code blob.
176 jvmtiError JvmtiCodeBlobEvents::generate_dynamic_code_events(JvmtiEnv* env) {
177 CodeBlobCollector collector;
179 // First collect all the code blobs. This has to be done in a
180 // single pass over the code cache with CodeCache_lock held because
181 // there isn't any safe way to iterate over regular CodeBlobs since
182 // they can be freed at any point.
183 {
184 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
185 collector.collect();
186 }
188 // iterate over the collected list and post an event for each blob
189 JvmtiCodeBlobDesc* blob = collector.first();
190 while (blob != NULL) {
191 JvmtiExport::post_dynamic_code_generated(env, blob->name(), blob->code_begin(), blob->code_end());
192 blob = collector.next();
193 }
194 return JVMTI_ERROR_NONE;
195 }
198 // Generate a COMPILED_METHOD_LOAD event for each nnmethod
199 jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) {
200 HandleMark hm;
202 // Walk the CodeCache notifying for live nmethods. The code cache
203 // may be changing while this is happening which is ok since newly
204 // created nmethod will notify normally and nmethods which are freed
205 // can be safely skipped.
206 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
207 nmethod* current = CodeCache::first_nmethod();
208 while (current != NULL) {
209 // Lock the nmethod so it can't be freed
210 nmethodLocker nml(current);
212 // Only notify for live nmethods
213 if (current->is_alive()) {
214 // Don't hold the lock over the notify or jmethodID creation
215 MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
216 current->get_and_cache_jmethod_id();
217 JvmtiExport::post_compiled_method_load(current);
218 }
219 current = CodeCache::next_nmethod(current);
220 }
221 return JVMTI_ERROR_NONE;
222 }
225 // create a C-heap allocated address location map for an nmethod
226 void JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nmethod *nm,
227 jvmtiAddrLocationMap** map_ptr,
228 jint *map_length_ptr)
229 {
230 ResourceMark rm;
231 jvmtiAddrLocationMap* map = NULL;
232 jint map_length = 0;
235 // Generate line numbers using PcDesc and ScopeDesc info
236 methodHandle mh(nm->method());
238 if (!mh->is_native()) {
239 PcDesc *pcd;
240 int pcds_in_method;
242 pcds_in_method = (nm->scopes_pcs_end() - nm->scopes_pcs_begin());
243 map = NEW_C_HEAP_ARRAY(jvmtiAddrLocationMap, pcds_in_method);
245 address scopes_data = nm->scopes_data_begin();
246 for( pcd = nm->scopes_pcs_begin(); pcd < nm->scopes_pcs_end(); ++pcd ) {
247 ScopeDesc sc0(nm, pcd->scope_decode_offset(), pcd->should_reexecute(), pcd->return_oop());
248 ScopeDesc *sd = &sc0;
249 while( !sd->is_top() ) { sd = sd->sender(); }
250 int bci = sd->bci();
251 if (bci != InvocationEntryBci) {
252 assert(map_length < pcds_in_method, "checking");
253 map[map_length].start_address = (const void*)pcd->real_pc(nm);
254 map[map_length].location = bci;
255 ++map_length;
256 }
257 }
258 }
260 *map_ptr = map;
261 *map_length_ptr = map_length;
262 }