jdk

Форк
0
/
stubs.cpp 
252 строки · 8.7 Кб
1
/*
2
 * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
 *
5
 * This code is free software; you can redistribute it and/or modify it
6
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.
8
 *
9
 * This code is distributed in the hope that it will be useful, but WITHOUT
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
 * version 2 for more details (a copy is included in the LICENSE file that
13
 * accompanied this code).
14
 *
15
 * You should have received a copy of the GNU General Public License version
16
 * 2 along with this work; if not, write to the Free Software Foundation,
17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
 * or visit www.oracle.com if you need additional information or have any
21
 * questions.
22
 *
23
 */
24

25
#include "precompiled.hpp"
26
#include "code/codeBlob.hpp"
27
#include "code/codeCache.hpp"
28
#include "code/stubs.hpp"
29
#include "memory/allocation.inline.hpp"
30
#include "oops/oop.inline.hpp"
31
#include "runtime/mutexLocker.hpp"
32
#include "utilities/align.hpp"
33
#include "utilities/checkedCast.hpp"
34

35

36
// Implementation of StubQueue
37
//
38
// Standard wrap-around queue implementation; the queue dimensions
39
// are specified by the _queue_begin & _queue_end indices. The queue
40
// can be in two states (transparent to the outside):
41
//
42
// a) contiguous state: all queue entries in one block (or empty)
43
//
44
// Queue: |...|XXXXXXX|...............|
45
//        ^0  ^begin  ^end            ^size = limit
46
//            |_______|
47
//            one block
48
//
49
// b) non-contiguous state: queue entries in two blocks
50
//
51
// Queue: |XXX|.......|XXXXXXX|.......|
52
//        ^0  ^end    ^begin  ^limit  ^size
53
//        |___|       |_______|
54
//         1st block  2nd block
55
//
56
// In the non-contiguous state, the wrap-around point is
57
// indicated via the _buffer_limit index since the last
58
// queue entry may not fill up the queue completely in
59
// which case we need to know where the 2nd block's end
60
// is to do the proper wrap-around. When removing the
61
// last entry of the 2nd block, _buffer_limit is reset
62
// to _buffer_size.
63
//
64
// CAUTION: DO NOT MESS WITH THIS CODE IF YOU CANNOT PROVE
65
// ITS CORRECTNESS! THIS CODE IS MORE SUBTLE THAN IT LOOKS!
66

67

68
StubQueue::StubQueue(StubInterface* stub_interface, int buffer_size,
69
                     Mutex* lock, const char* name) : _mutex(lock) {
70
  intptr_t size = align_up(buffer_size, 2*BytesPerWord);
71
  BufferBlob* blob = BufferBlob::create(name, checked_cast<int>(size));
72
  if( blob == nullptr) {
73
    vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "CodeCache: no room for %s", name);
74
  }
75
  _stub_interface  = stub_interface;
76

77
  // The code blob alignment can be smaller than the requested stub alignment.
78
  // Make sure we put the stubs at their requested alignment by aligning the buffer base and limits.
79
  address aligned_start = align_up(blob->content_begin(), stub_alignment());
80
  address aligned_end = align_down(blob->content_end(), stub_alignment());
81
  int aligned_size = aligned_end - aligned_start;
82
  _buffer_size     = aligned_size;
83
  _buffer_limit    = aligned_size;
84
  _stub_buffer     = aligned_start;
85
  _queue_begin     = 0;
86
  _queue_end       = 0;
87
  _number_of_stubs = 0;
88
}
89

90

91
StubQueue::~StubQueue() {
92
  // Note: Currently StubQueues are never destroyed so nothing needs to be done here.
93
  //       If we want to implement the destructor, we need to release the BufferBlob
94
  //       allocated in the constructor (i.e., we need to keep it around or look it
95
  //       up via CodeCache::find_blob(...).
96
  Unimplemented();
97
}
98

99
void StubQueue::deallocate_unused_tail() {
100
  CodeBlob* blob = CodeCache::find_blob((void*)_stub_buffer);
101
  CodeCache::free_unused_tail(blob, used_space());
102
  // Update the limits to the new, trimmed CodeBlob size
103
  address aligned_start = align_up(blob->content_begin(), stub_alignment());
104
  address aligned_end = align_down(blob->content_end(), stub_alignment());
105
  int aligned_size = aligned_end - aligned_start;
106
  _buffer_size = aligned_size;
107
  _buffer_limit = aligned_size;
108
}
109

110
Stub* StubQueue::stub_containing(address pc) const {
111
  if (contains(pc)) {
112
    for (Stub* s = first(); s != nullptr; s = next(s)) {
113
      if (stub_contains(s, pc)) return s;
114
    }
115
  }
116
  return nullptr;
117
}
118

119

120
Stub* StubQueue::request_committed(int code_size) {
121
  Stub* s = request(code_size);
122
  if (s != nullptr) commit(code_size);
123
  return s;
124
}
125

126
int StubQueue::compute_stub_size(Stub* stub, int code_size) {
127
  address stub_begin = (address) stub;
128
  address code_begin = stub_code_begin(stub);
129
  address code_end = align_up(code_begin + code_size, stub_alignment());
130
  return (int)(code_end - stub_begin);
131
}
132

133
Stub* StubQueue::request(int requested_code_size) {
134
  assert(requested_code_size > 0, "requested_code_size must be > 0");
135
  if (_mutex != nullptr) _mutex->lock_without_safepoint_check();
136
  Stub* s = current_stub();
137
  int requested_size = compute_stub_size(s, requested_code_size);
138
  if (requested_size <= available_space()) {
139
    if (is_contiguous()) {
140
      // Queue: |...|XXXXXXX|.............|
141
      //        ^0  ^begin  ^end          ^size = limit
142
      assert(_buffer_limit == _buffer_size, "buffer must be fully usable");
143
      if (_queue_end + requested_size <= _buffer_size) {
144
        // code fits in at the end => nothing to do
145
        stub_initialize(s, requested_size);
146
        return s;
147
      } else {
148
        // stub doesn't fit in at the queue end
149
        // => reduce buffer limit & wrap around
150
        assert(!is_empty(), "just checkin'");
151
        _buffer_limit = _queue_end;
152
        _queue_end = 0;
153
      }
154
    }
155
  }
156
  if (requested_size <= available_space()) {
157
    assert(!is_contiguous(), "just checkin'");
158
    assert(_buffer_limit <= _buffer_size, "queue invariant broken");
159
    // Queue: |XXX|.......|XXXXXXX|.......|
160
    //        ^0  ^end    ^begin  ^limit  ^size
161
    s = current_stub();
162
    stub_initialize(s, requested_size);
163
    return s;
164
  }
165
  // Not enough space left
166
  if (_mutex != nullptr) _mutex->unlock();
167
  return nullptr;
168
}
169

170

171
void StubQueue::commit(int committed_code_size) {
172
  assert(committed_code_size > 0, "committed_code_size must be > 0");
173
  Stub* s = current_stub();
174
  int committed_size = compute_stub_size(s, committed_code_size);
175
  assert(committed_size <= stub_size(s), "committed size must not exceed requested size");
176
  stub_initialize(s, committed_size);
177
  _queue_end += committed_size;
178
  _number_of_stubs++;
179
  if (_mutex != nullptr) _mutex->unlock();
180
  debug_only(stub_verify(s);)
181
}
182

183

184
void StubQueue::remove_first() {
185
  if (number_of_stubs() == 0) return;
186
  Stub* s = first();
187
  debug_only(stub_verify(s);)
188
  stub_finalize(s);
189
  _queue_begin += stub_size(s);
190
  assert(_queue_begin <= _buffer_limit, "sanity check");
191
  if (_queue_begin == _queue_end) {
192
    // buffer empty
193
    // => reset queue indices
194
    _queue_begin  = 0;
195
    _queue_end    = 0;
196
    _buffer_limit = _buffer_size;
197
  } else if (_queue_begin == _buffer_limit) {
198
    // buffer limit reached
199
    // => reset buffer limit & wrap around
200
    _buffer_limit = _buffer_size;
201
    _queue_begin = 0;
202
  }
203
  _number_of_stubs--;
204
}
205

206

207
void StubQueue::remove_first(int n) {
208
  int i = MIN2(n, number_of_stubs());
209
  while (i-- > 0) remove_first();
210
}
211

212

213
void StubQueue::remove_all(){
214
  debug_only(verify();)
215
  remove_first(number_of_stubs());
216
  assert(number_of_stubs() == 0, "sanity check");
217
}
218

219

220
void StubQueue::verify() {
221
  // verify only if initialized
222
  if (_stub_buffer == nullptr) return;
223
  MutexLocker lock(_mutex, Mutex::_no_safepoint_check_flag);
224
  // verify index boundaries
225
  guarantee(0 <= _buffer_size, "buffer size must be positive");
226
  guarantee(0 <= _buffer_limit && _buffer_limit <= _buffer_size , "_buffer_limit out of bounds");
227
  guarantee(0 <= _queue_begin  && _queue_begin  <  _buffer_limit, "_queue_begin out of bounds");
228
  guarantee(0 <= _queue_end    && _queue_end    <= _buffer_limit, "_queue_end   out of bounds");
229
  // verify alignment
230
  guarantee(_queue_begin  % stub_alignment() == 0, "_queue_begin  not aligned");
231
  guarantee(_queue_end    % stub_alignment() == 0, "_queue_end    not aligned");
232
  // verify buffer limit/size relationship
233
  if (is_contiguous()) {
234
    guarantee(_buffer_limit == _buffer_size, "_buffer_limit must equal _buffer_size");
235
  }
236
  // verify contents
237
  int n = 0;
238
  for (Stub* s = first(); s != nullptr; s = next(s)) {
239
    stub_verify(s);
240
    n++;
241
  }
242
  guarantee(n == number_of_stubs(), "number of stubs inconsistent");
243
  guarantee(_queue_begin != _queue_end || n == 0, "buffer indices must be the same");
244
}
245

246

247
void StubQueue::print() {
248
  ConditionalMutexLocker lock(_mutex, _mutex != nullptr, Mutex::_no_safepoint_check_flag);
249
  for (Stub* s = first(); s != nullptr; s = next(s)) {
250
    stub_print(s);
251
  }
252
}
253

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.