1 /* 2 * Copyright (c) 1997, 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 */ 24 25 #include "precompiled.hpp" 26 #include "code/codeBlob.hpp" 27 #include "code/stubs.hpp" 28 #include "memory/allocation.inline.hpp" 29 #include "oops/oop.inline.hpp" 30 #include "runtime/mutexLocker.hpp" 31 32 33 // Implementation of StubQueue 34 // 35 // Standard wrap-around queue implementation; the queue dimensions 36 // are specified by the _queue_begin & _queue_end indices. The queue 37 // can be in two states (transparent to the outside): 38 // 39 // a) contiguous state: all queue entries in one block (or empty) 40 // 41 // Queue: |...|XXXXXXX|...............| 42 // ^0 ^begin ^end ^size = limit 43 // |_______| 44 // one block 45 // 46 // b) non-contiguous state: queue entries in two blocks 47 // 48 // Queue: |XXX|.......|XXXXXXX|.......| 49 // ^0 ^end ^begin ^limit ^size 50 // |___| |_______| 51 // 1st block 2nd block 52 // 53 // In the non-contiguous state, the wrap-around point is 54 // indicated via the _buffer_limit index since the last 55 // queue entry may not fill up the queue completely in 56 // which case we need to know where the 2nd block's end 57 // is to do the proper wrap-around. When removing the 58 // last entry of the 2nd block, _buffer_limit is reset 59 // to _buffer_size. 60 // 61 // CAUTION: DO NOT MESS WITH THIS CODE IF YOU CANNOT PROVE 62 // ITS CORRECTNESS! THIS CODE IS MORE SUBTLE THAN IT LOOKS! 63 64 65 StubQueue::StubQueue(StubInterface* stub_interface, int buffer_size, 66 Mutex* lock, const char* name) : _mutex(lock) { 67 intptr_t size = round_to(buffer_size, 2*BytesPerWord); 68 BufferBlob* blob = BufferBlob::create(name, size); 69 if( blob == NULL) { 70 vm_exit_out_of_memory(size, err_msg("CodeCache: no room for %s", name)); 71 } 72 _stub_interface = stub_interface; 73 _buffer_size = blob->content_size(); 74 _buffer_limit = blob->content_size(); 75 _stub_buffer = blob->content_begin(); 76 _queue_begin = 0; 77 _queue_end = 0; 78 _number_of_stubs = 0; 79 register_queue(this); 80 } 81 82 83 StubQueue::~StubQueue() { 84 // Note: Currently StubQueues are never destroyed so nothing needs to be done here. 85 // If we want to implement the destructor, we need to release the BufferBlob 86 // allocated in the constructor (i.e., we need to keep it around or look it 87 // up via CodeCache::find_blob(...). 88 Unimplemented(); 89 } 90 91 92 Stub* StubQueue::stub_containing(address pc) const { 93 if (contains(pc)) { 94 for (Stub* s = first(); s != NULL; s = next(s)) { 95 if (stub_contains(s, pc)) return s; 96 } 97 } 98 return NULL; 99 } 100 101 102 Stub* StubQueue::request_committed(int code_size) { 103 Stub* s = request(code_size); 104 if (s != NULL) commit(code_size); 105 return s; 106 } 107 108 109 Stub* StubQueue::request(int requested_code_size) { 110 assert(requested_code_size > 0, "requested_code_size must be > 0"); 111 if (_mutex != NULL) _mutex->lock(); 112 Stub* s = current_stub(); 113 int requested_size = round_to(stub_code_size_to_size(requested_code_size), CodeEntryAlignment); 114 if (requested_size <= available_space()) { 115 if (is_contiguous()) { 116 // Queue: |...|XXXXXXX|.............| 117 // ^0 ^begin ^end ^size = limit 118 assert(_buffer_limit == _buffer_size, "buffer must be fully usable"); 119 if (_queue_end + requested_size <= _buffer_size) { 120 // code fits in at the end => nothing to do 121 stub_initialize(s, requested_size); 122 return s; 123 } else { 124 // stub doesn't fit in at the queue end 125 // => reduce buffer limit & wrap around 126 assert(!is_empty(), "just checkin'"); 127 _buffer_limit = _queue_end; 128 _queue_end = 0; 129 } 130 } 131 } 132 if (requested_size <= available_space()) { 133 assert(!is_contiguous(), "just checkin'"); 134 assert(_buffer_limit <= _buffer_size, "queue invariant broken"); 135 // Queue: |XXX|.......|XXXXXXX|.......| 136 // ^0 ^end ^begin ^limit ^size 137 s = current_stub(); 138 stub_initialize(s, requested_size); 139 return s; 140 } 141 // Not enough space left 142 if (_mutex != NULL) _mutex->unlock(); 143 return NULL; 144 } 145 146 147 void StubQueue::commit(int committed_code_size) { 148 assert(committed_code_size > 0, "committed_code_size must be > 0"); 149 int committed_size = round_to(stub_code_size_to_size(committed_code_size), CodeEntryAlignment); 150 Stub* s = current_stub(); 151 assert(committed_size <= stub_size(s), "committed size must not exceed requested size"); 152 stub_initialize(s, committed_size); 153 _queue_end += committed_size; 154 _number_of_stubs++; 155 if (_mutex != NULL) _mutex->unlock(); 156 debug_only(stub_verify(s);) 157 } 158 159 160 void StubQueue::remove_first() { 161 if (number_of_stubs() == 0) return; 162 Stub* s = first(); 163 debug_only(stub_verify(s);) 164 stub_finalize(s); 165 _queue_begin += stub_size(s); 166 assert(_queue_begin <= _buffer_limit, "sanity check"); 167 if (_queue_begin == _queue_end) { 168 // buffer empty 169 // => reset queue indices 170 _queue_begin = 0; 171 _queue_end = 0; 172 _buffer_limit = _buffer_size; 173 } else if (_queue_begin == _buffer_limit) { 174 // buffer limit reached 175 // => reset buffer limit & wrap around 176 _buffer_limit = _buffer_size; 177 _queue_begin = 0; 178 } 179 _number_of_stubs--; 180 } 181 182 183 void StubQueue::remove_first(int n) { 184 int i = MIN2(n, number_of_stubs()); 185 while (i-- > 0) remove_first(); 186 } 187 188 189 void StubQueue::remove_all(){ 190 debug_only(verify();) 191 remove_first(number_of_stubs()); 192 assert(number_of_stubs() == 0, "sanity check"); 193 } 194 195 196 enum { StubQueueLimit = 10 }; // there are only a few in the world 197 static StubQueue* registered_stub_queues[StubQueueLimit]; 198 199 void StubQueue::register_queue(StubQueue* sq) { 200 for (int i = 0; i < StubQueueLimit; i++) { 201 if (registered_stub_queues[i] == NULL) { 202 registered_stub_queues[i] = sq; 203 return; 204 } 205 } 206 ShouldNotReachHere(); 207 } 208 209 210 void StubQueue::queues_do(void f(StubQueue* sq)) { 211 for (int i = 0; i < StubQueueLimit; i++) { 212 if (registered_stub_queues[i] != NULL) { 213 f(registered_stub_queues[i]); 214 } 215 } 216 } 217 218 219 void StubQueue::stubs_do(void f(Stub* s)) { 220 debug_only(verify();) 221 MutexLockerEx lock(_mutex); 222 for (Stub* s = first(); s != NULL; s = next(s)) f(s); 223 } 224 225 226 void StubQueue::verify() { 227 // verify only if initialized 228 if (_stub_buffer == NULL) return; 229 MutexLockerEx lock(_mutex); 230 // verify index boundaries 231 guarantee(0 <= _buffer_size, "buffer size must be positive"); 232 guarantee(0 <= _buffer_limit && _buffer_limit <= _buffer_size , "_buffer_limit out of bounds"); 233 guarantee(0 <= _queue_begin && _queue_begin < _buffer_limit, "_queue_begin out of bounds"); 234 guarantee(0 <= _queue_end && _queue_end <= _buffer_limit, "_queue_end out of bounds"); 235 // verify alignment 236 guarantee(_buffer_size % CodeEntryAlignment == 0, "_buffer_size not aligned"); 237 guarantee(_buffer_limit % CodeEntryAlignment == 0, "_buffer_limit not aligned"); 238 guarantee(_queue_begin % CodeEntryAlignment == 0, "_queue_begin not aligned"); 239 guarantee(_queue_end % CodeEntryAlignment == 0, "_queue_end not aligned"); 240 // verify buffer limit/size relationship 241 if (is_contiguous()) { 242 guarantee(_buffer_limit == _buffer_size, "_buffer_limit must equal _buffer_size"); 243 } 244 // verify contents 245 int n = 0; 246 for (Stub* s = first(); s != NULL; s = next(s)) { 247 stub_verify(s); 248 n++; 249 } 250 guarantee(n == number_of_stubs(), "number of stubs inconsistent"); 251 guarantee(_queue_begin != _queue_end || n == 0, "buffer indices must be the same"); 252 } 253 254 255 void StubQueue::print() { 256 MutexLockerEx lock(_mutex); 257 for (Stub* s = first(); s != NULL; s = next(s)) { 258 stub_print(s); 259 } 260 }