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