Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/code/stubs.cpp
32285 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "code/codeBlob.hpp"26#include "code/stubs.hpp"27#include "memory/allocation.inline.hpp"28#include "oops/oop.inline.hpp"29#include "runtime/mutexLocker.hpp"303132// Implementation of StubQueue33//34// Standard wrap-around queue implementation; the queue dimensions35// are specified by the _queue_begin & _queue_end indices. The queue36// can be in two states (transparent to the outside):37//38// a) contiguous state: all queue entries in one block (or empty)39//40// Queue: |...|XXXXXXX|...............|41// ^0 ^begin ^end ^size = limit42// |_______|43// one block44//45// b) non-contiguous state: queue entries in two blocks46//47// Queue: |XXX|.......|XXXXXXX|.......|48// ^0 ^end ^begin ^limit ^size49// |___| |_______|50// 1st block 2nd block51//52// In the non-contiguous state, the wrap-around point is53// indicated via the _buffer_limit index since the last54// queue entry may not fill up the queue completely in55// which case we need to know where the 2nd block's end56// is to do the proper wrap-around. When removing the57// last entry of the 2nd block, _buffer_limit is reset58// to _buffer_size.59//60// CAUTION: DO NOT MESS WITH THIS CODE IF YOU CANNOT PROVE61// ITS CORRECTNESS! THIS CODE IS MORE SUBTLE THAN IT LOOKS!626364StubQueue::StubQueue(StubInterface* stub_interface, int buffer_size,65Mutex* lock, const char* name) : _mutex(lock) {66intptr_t size = round_to(buffer_size, 2*BytesPerWord);67BufferBlob* blob = BufferBlob::create(name, size);68if( blob == NULL) {69vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, err_msg("CodeCache: no room for %s", name));70}71_stub_interface = stub_interface;72_buffer_size = blob->content_size();73_buffer_limit = blob->content_size();74_stub_buffer = blob->content_begin();75_queue_begin = 0;76_queue_end = 0;77_number_of_stubs = 0;78register_queue(this);79}808182StubQueue::~StubQueue() {83// Note: Currently StubQueues are never destroyed so nothing needs to be done here.84// If we want to implement the destructor, we need to release the BufferBlob85// allocated in the constructor (i.e., we need to keep it around or look it86// up via CodeCache::find_blob(...).87Unimplemented();88}899091Stub* StubQueue::stub_containing(address pc) const {92if (contains(pc)) {93for (Stub* s = first(); s != NULL; s = next(s)) {94if (stub_contains(s, pc)) return s;95}96}97return NULL;98}99100101Stub* StubQueue::request_committed(int code_size) {102Stub* s = request(code_size);103CodeStrings strings;104if (s != NULL) commit(code_size, strings);105return s;106}107108109Stub* StubQueue::request(int requested_code_size) {110assert(requested_code_size > 0, "requested_code_size must be > 0");111if (_mutex != NULL) _mutex->lock();112Stub* s = current_stub();113int requested_size = round_to(stub_code_size_to_size(requested_code_size), CodeEntryAlignment);114if (requested_size <= available_space()) {115if (is_contiguous()) {116// Queue: |...|XXXXXXX|.............|117// ^0 ^begin ^end ^size = limit118assert(_buffer_limit == _buffer_size, "buffer must be fully usable");119if (_queue_end + requested_size <= _buffer_size) {120// code fits in at the end => nothing to do121CodeStrings strings;122stub_initialize(s, requested_size, strings);123return s;124} else {125// stub doesn't fit in at the queue end126// => reduce buffer limit & wrap around127assert(!is_empty(), "just checkin'");128_buffer_limit = _queue_end;129_queue_end = 0;130}131}132}133if (requested_size <= available_space()) {134assert(!is_contiguous(), "just checkin'");135assert(_buffer_limit <= _buffer_size, "queue invariant broken");136// Queue: |XXX|.......|XXXXXXX|.......|137// ^0 ^end ^begin ^limit ^size138s = current_stub();139CodeStrings strings;140stub_initialize(s, requested_size, strings);141return s;142}143// Not enough space left144if (_mutex != NULL) _mutex->unlock();145return NULL;146}147148149void StubQueue::commit(int committed_code_size, CodeStrings& strings) {150assert(committed_code_size > 0, "committed_code_size must be > 0");151int committed_size = round_to(stub_code_size_to_size(committed_code_size), CodeEntryAlignment);152Stub* s = current_stub();153assert(committed_size <= stub_size(s), "committed size must not exceed requested size");154stub_initialize(s, committed_size, strings);155_queue_end += committed_size;156_number_of_stubs++;157if (_mutex != NULL) _mutex->unlock();158debug_only(stub_verify(s);)159}160161162void StubQueue::remove_first() {163if (number_of_stubs() == 0) return;164Stub* s = first();165debug_only(stub_verify(s);)166stub_finalize(s);167_queue_begin += stub_size(s);168assert(_queue_begin <= _buffer_limit, "sanity check");169if (_queue_begin == _queue_end) {170// buffer empty171// => reset queue indices172_queue_begin = 0;173_queue_end = 0;174_buffer_limit = _buffer_size;175} else if (_queue_begin == _buffer_limit) {176// buffer limit reached177// => reset buffer limit & wrap around178_buffer_limit = _buffer_size;179_queue_begin = 0;180}181_number_of_stubs--;182}183184185void StubQueue::remove_first(int n) {186int i = MIN2(n, number_of_stubs());187while (i-- > 0) remove_first();188}189190191void StubQueue::remove_all(){192debug_only(verify();)193remove_first(number_of_stubs());194assert(number_of_stubs() == 0, "sanity check");195}196197198enum { StubQueueLimit = 10 }; // there are only a few in the world199static StubQueue* registered_stub_queues[StubQueueLimit];200201void StubQueue::register_queue(StubQueue* sq) {202for (int i = 0; i < StubQueueLimit; i++) {203if (registered_stub_queues[i] == NULL) {204registered_stub_queues[i] = sq;205return;206}207}208ShouldNotReachHere();209}210211212void StubQueue::queues_do(void f(StubQueue* sq)) {213for (int i = 0; i < StubQueueLimit; i++) {214if (registered_stub_queues[i] != NULL) {215f(registered_stub_queues[i]);216}217}218}219220221void StubQueue::stubs_do(void f(Stub* s)) {222debug_only(verify();)223MutexLockerEx lock(_mutex);224for (Stub* s = first(); s != NULL; s = next(s)) f(s);225}226227228void StubQueue::verify() {229// verify only if initialized230if (_stub_buffer == NULL) return;231MutexLockerEx lock(_mutex);232// verify index boundaries233guarantee(0 <= _buffer_size, "buffer size must be positive");234guarantee(0 <= _buffer_limit && _buffer_limit <= _buffer_size , "_buffer_limit out of bounds");235guarantee(0 <= _queue_begin && _queue_begin < _buffer_limit, "_queue_begin out of bounds");236guarantee(0 <= _queue_end && _queue_end <= _buffer_limit, "_queue_end out of bounds");237// verify alignment238guarantee(_buffer_size % CodeEntryAlignment == 0, "_buffer_size not aligned");239guarantee(_buffer_limit % CodeEntryAlignment == 0, "_buffer_limit not aligned");240guarantee(_queue_begin % CodeEntryAlignment == 0, "_queue_begin not aligned");241guarantee(_queue_end % CodeEntryAlignment == 0, "_queue_end not aligned");242// verify buffer limit/size relationship243if (is_contiguous()) {244guarantee(_buffer_limit == _buffer_size, "_buffer_limit must equal _buffer_size");245}246// verify contents247int n = 0;248for (Stub* s = first(); s != NULL; s = next(s)) {249stub_verify(s);250n++;251}252guarantee(n == number_of_stubs(), "number of stubs inconsistent");253guarantee(_queue_begin != _queue_end || n == 0, "buffer indices must be the same");254}255256257void StubQueue::print() {258MutexLockerEx lock(_mutex);259for (Stub* s = first(); s != NULL; s = next(s)) {260stub_print(s);261}262}263264265