aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "code/codeBlob.hpp" aoqi@0: #include "code/stubs.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "runtime/mutexLocker.hpp" aoqi@0: aoqi@0: aoqi@0: // Implementation of StubQueue aoqi@0: // aoqi@0: // Standard wrap-around queue implementation; the queue dimensions aoqi@0: // are specified by the _queue_begin & _queue_end indices. The queue aoqi@0: // can be in two states (transparent to the outside): aoqi@0: // aoqi@0: // a) contiguous state: all queue entries in one block (or empty) aoqi@0: // aoqi@0: // Queue: |...|XXXXXXX|...............| aoqi@0: // ^0 ^begin ^end ^size = limit aoqi@0: // |_______| aoqi@0: // one block aoqi@0: // aoqi@0: // b) non-contiguous state: queue entries in two blocks aoqi@0: // aoqi@0: // Queue: |XXX|.......|XXXXXXX|.......| aoqi@0: // ^0 ^end ^begin ^limit ^size aoqi@0: // |___| |_______| aoqi@0: // 1st block 2nd block aoqi@0: // aoqi@0: // In the non-contiguous state, the wrap-around point is aoqi@0: // indicated via the _buffer_limit index since the last aoqi@0: // queue entry may not fill up the queue completely in aoqi@0: // which case we need to know where the 2nd block's end aoqi@0: // is to do the proper wrap-around. When removing the aoqi@0: // last entry of the 2nd block, _buffer_limit is reset aoqi@0: // to _buffer_size. aoqi@0: // aoqi@0: // CAUTION: DO NOT MESS WITH THIS CODE IF YOU CANNOT PROVE aoqi@0: // ITS CORRECTNESS! THIS CODE IS MORE SUBTLE THAN IT LOOKS! aoqi@0: aoqi@0: aoqi@0: StubQueue::StubQueue(StubInterface* stub_interface, int buffer_size, aoqi@0: Mutex* lock, const char* name) : _mutex(lock) { aoqi@0: intptr_t size = round_to(buffer_size, 2*BytesPerWord); aoqi@0: BufferBlob* blob = BufferBlob::create(name, size); aoqi@0: if( blob == NULL) { aoqi@0: vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, err_msg("CodeCache: no room for %s", name)); aoqi@0: } aoqi@0: _stub_interface = stub_interface; aoqi@0: _buffer_size = blob->content_size(); aoqi@0: _buffer_limit = blob->content_size(); aoqi@0: _stub_buffer = blob->content_begin(); aoqi@0: _queue_begin = 0; aoqi@0: _queue_end = 0; aoqi@0: _number_of_stubs = 0; aoqi@0: register_queue(this); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: StubQueue::~StubQueue() { aoqi@0: // Note: Currently StubQueues are never destroyed so nothing needs to be done here. aoqi@0: // If we want to implement the destructor, we need to release the BufferBlob aoqi@0: // allocated in the constructor (i.e., we need to keep it around or look it aoqi@0: // up via CodeCache::find_blob(...). aoqi@0: Unimplemented(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: Stub* StubQueue::stub_containing(address pc) const { aoqi@0: if (contains(pc)) { aoqi@0: for (Stub* s = first(); s != NULL; s = next(s)) { aoqi@0: if (stub_contains(s, pc)) return s; aoqi@0: } aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: Stub* StubQueue::request_committed(int code_size) { aoqi@0: Stub* s = request(code_size); aoqi@0: CodeStrings strings; aoqi@0: if (s != NULL) commit(code_size, strings); aoqi@0: return s; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: Stub* StubQueue::request(int requested_code_size) { aoqi@0: assert(requested_code_size > 0, "requested_code_size must be > 0"); aoqi@0: if (_mutex != NULL) _mutex->lock(); aoqi@0: Stub* s = current_stub(); aoqi@0: int requested_size = round_to(stub_code_size_to_size(requested_code_size), CodeEntryAlignment); aoqi@0: if (requested_size <= available_space()) { aoqi@0: if (is_contiguous()) { aoqi@0: // Queue: |...|XXXXXXX|.............| aoqi@0: // ^0 ^begin ^end ^size = limit aoqi@0: assert(_buffer_limit == _buffer_size, "buffer must be fully usable"); aoqi@0: if (_queue_end + requested_size <= _buffer_size) { aoqi@0: // code fits in at the end => nothing to do aoqi@0: CodeStrings strings; aoqi@0: stub_initialize(s, requested_size, strings); aoqi@0: return s; aoqi@0: } else { aoqi@0: // stub doesn't fit in at the queue end aoqi@0: // => reduce buffer limit & wrap around aoqi@0: assert(!is_empty(), "just checkin'"); aoqi@0: _buffer_limit = _queue_end; aoqi@0: _queue_end = 0; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (requested_size <= available_space()) { aoqi@0: assert(!is_contiguous(), "just checkin'"); aoqi@0: assert(_buffer_limit <= _buffer_size, "queue invariant broken"); aoqi@0: // Queue: |XXX|.......|XXXXXXX|.......| aoqi@0: // ^0 ^end ^begin ^limit ^size aoqi@0: s = current_stub(); aoqi@0: CodeStrings strings; aoqi@0: stub_initialize(s, requested_size, strings); aoqi@0: return s; aoqi@0: } aoqi@0: // Not enough space left aoqi@0: if (_mutex != NULL) _mutex->unlock(); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void StubQueue::commit(int committed_code_size, CodeStrings& strings) { aoqi@0: assert(committed_code_size > 0, "committed_code_size must be > 0"); aoqi@0: int committed_size = round_to(stub_code_size_to_size(committed_code_size), CodeEntryAlignment); aoqi@0: Stub* s = current_stub(); aoqi@0: assert(committed_size <= stub_size(s), "committed size must not exceed requested size"); aoqi@0: stub_initialize(s, committed_size, strings); aoqi@0: _queue_end += committed_size; aoqi@0: _number_of_stubs++; aoqi@0: if (_mutex != NULL) _mutex->unlock(); aoqi@0: debug_only(stub_verify(s);) aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void StubQueue::remove_first() { aoqi@0: if (number_of_stubs() == 0) return; aoqi@0: Stub* s = first(); aoqi@0: debug_only(stub_verify(s);) aoqi@0: stub_finalize(s); aoqi@0: _queue_begin += stub_size(s); aoqi@0: assert(_queue_begin <= _buffer_limit, "sanity check"); aoqi@0: if (_queue_begin == _queue_end) { aoqi@0: // buffer empty aoqi@0: // => reset queue indices aoqi@0: _queue_begin = 0; aoqi@0: _queue_end = 0; aoqi@0: _buffer_limit = _buffer_size; aoqi@0: } else if (_queue_begin == _buffer_limit) { aoqi@0: // buffer limit reached aoqi@0: // => reset buffer limit & wrap around aoqi@0: _buffer_limit = _buffer_size; aoqi@0: _queue_begin = 0; aoqi@0: } aoqi@0: _number_of_stubs--; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void StubQueue::remove_first(int n) { aoqi@0: int i = MIN2(n, number_of_stubs()); aoqi@0: while (i-- > 0) remove_first(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void StubQueue::remove_all(){ aoqi@0: debug_only(verify();) aoqi@0: remove_first(number_of_stubs()); aoqi@0: assert(number_of_stubs() == 0, "sanity check"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: enum { StubQueueLimit = 10 }; // there are only a few in the world aoqi@0: static StubQueue* registered_stub_queues[StubQueueLimit]; aoqi@0: aoqi@0: void StubQueue::register_queue(StubQueue* sq) { aoqi@0: for (int i = 0; i < StubQueueLimit; i++) { aoqi@0: if (registered_stub_queues[i] == NULL) { aoqi@0: registered_stub_queues[i] = sq; aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void StubQueue::queues_do(void f(StubQueue* sq)) { aoqi@0: for (int i = 0; i < StubQueueLimit; i++) { aoqi@0: if (registered_stub_queues[i] != NULL) { aoqi@0: f(registered_stub_queues[i]); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void StubQueue::stubs_do(void f(Stub* s)) { aoqi@0: debug_only(verify();) aoqi@0: MutexLockerEx lock(_mutex); aoqi@0: for (Stub* s = first(); s != NULL; s = next(s)) f(s); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void StubQueue::verify() { aoqi@0: // verify only if initialized aoqi@0: if (_stub_buffer == NULL) return; aoqi@0: MutexLockerEx lock(_mutex); aoqi@0: // verify index boundaries aoqi@0: guarantee(0 <= _buffer_size, "buffer size must be positive"); aoqi@0: guarantee(0 <= _buffer_limit && _buffer_limit <= _buffer_size , "_buffer_limit out of bounds"); aoqi@0: guarantee(0 <= _queue_begin && _queue_begin < _buffer_limit, "_queue_begin out of bounds"); aoqi@0: guarantee(0 <= _queue_end && _queue_end <= _buffer_limit, "_queue_end out of bounds"); aoqi@0: // verify alignment aoqi@0: guarantee(_buffer_size % CodeEntryAlignment == 0, "_buffer_size not aligned"); aoqi@0: guarantee(_buffer_limit % CodeEntryAlignment == 0, "_buffer_limit not aligned"); aoqi@0: guarantee(_queue_begin % CodeEntryAlignment == 0, "_queue_begin not aligned"); aoqi@0: guarantee(_queue_end % CodeEntryAlignment == 0, "_queue_end not aligned"); aoqi@0: // verify buffer limit/size relationship aoqi@0: if (is_contiguous()) { aoqi@0: guarantee(_buffer_limit == _buffer_size, "_buffer_limit must equal _buffer_size"); aoqi@0: } aoqi@0: // verify contents aoqi@0: int n = 0; aoqi@0: for (Stub* s = first(); s != NULL; s = next(s)) { aoqi@0: stub_verify(s); aoqi@0: n++; aoqi@0: } aoqi@0: guarantee(n == number_of_stubs(), "number of stubs inconsistent"); aoqi@0: guarantee(_queue_begin != _queue_end || n == 0, "buffer indices must be the same"); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void StubQueue::print() { aoqi@0: MutexLockerEx lock(_mutex); aoqi@0: for (Stub* s = first(); s != NULL; s = next(s)) { aoqi@0: stub_print(s); aoqi@0: } aoqi@0: }