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