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