src/share/vm/interpreter/oopMapCache.cpp

Fri, 29 Apr 2016 00:06:10 +0800

author
aoqi
date
Fri, 29 Apr 2016 00:06:10 +0800
changeset 1
2d8a650513c2
parent 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Added MIPS 64-bit port.

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "interpreter/oopMapCache.hpp"
aoqi@0 27 #include "memory/allocation.inline.hpp"
aoqi@0 28 #include "memory/resourceArea.hpp"
aoqi@0 29 #include "oops/oop.inline.hpp"
aoqi@0 30 #include "prims/jvmtiRedefineClassesTrace.hpp"
aoqi@0 31 #include "runtime/handles.inline.hpp"
aoqi@0 32 #include "runtime/signature.hpp"
aoqi@0 33
aoqi@0 34 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 35
aoqi@0 36 class OopMapCacheEntry: private InterpreterOopMap {
aoqi@0 37 friend class InterpreterOopMap;
aoqi@0 38 friend class OopMapForCacheEntry;
aoqi@0 39 friend class OopMapCache;
aoqi@0 40 friend class VerifyClosure;
aoqi@0 41
aoqi@0 42 protected:
aoqi@0 43 // Initialization
aoqi@0 44 void fill(methodHandle method, int bci);
aoqi@0 45 // fills the bit mask for native calls
aoqi@0 46 void fill_for_native(methodHandle method);
aoqi@0 47 void set_mask(CellTypeState* vars, CellTypeState* stack, int stack_top);
aoqi@0 48
aoqi@0 49 // Deallocate bit masks and initialize fields
aoqi@0 50 void flush();
aoqi@0 51
aoqi@0 52 private:
aoqi@0 53 void allocate_bit_mask(); // allocates the bit mask on C heap f necessary
aoqi@0 54 void deallocate_bit_mask(); // allocates the bit mask on C heap f necessary
aoqi@0 55 bool verify_mask(CellTypeState *vars, CellTypeState *stack, int max_locals, int stack_top);
aoqi@0 56
aoqi@0 57 public:
aoqi@0 58 OopMapCacheEntry() : InterpreterOopMap() {
aoqi@0 59 #ifdef ASSERT
aoqi@0 60 _resource_allocate_bit_mask = false;
aoqi@0 61 #endif
aoqi@0 62 }
aoqi@0 63 };
aoqi@0 64
aoqi@0 65
aoqi@0 66 // Implementation of OopMapForCacheEntry
aoqi@0 67 // (subclass of GenerateOopMap, initializes an OopMapCacheEntry for a given method and bci)
aoqi@0 68
aoqi@0 69 class OopMapForCacheEntry: public GenerateOopMap {
aoqi@0 70 OopMapCacheEntry *_entry;
aoqi@0 71 int _bci;
aoqi@0 72 int _stack_top;
aoqi@0 73
aoqi@0 74 virtual bool report_results() const { return false; }
aoqi@0 75 virtual bool possible_gc_point (BytecodeStream *bcs);
aoqi@0 76 virtual void fill_stackmap_prolog (int nof_gc_points);
aoqi@0 77 virtual void fill_stackmap_epilog ();
aoqi@0 78 virtual void fill_stackmap_for_opcodes (BytecodeStream *bcs,
aoqi@0 79 CellTypeState* vars,
aoqi@0 80 CellTypeState* stack,
aoqi@0 81 int stack_top);
aoqi@0 82 virtual void fill_init_vars (GrowableArray<intptr_t> *init_vars);
aoqi@0 83
aoqi@0 84 public:
aoqi@0 85 OopMapForCacheEntry(methodHandle method, int bci, OopMapCacheEntry *entry);
aoqi@0 86
aoqi@0 87 // Computes stack map for (method,bci) and initialize entry
aoqi@0 88 void compute_map(TRAPS);
aoqi@0 89 int size();
aoqi@0 90 };
aoqi@0 91
aoqi@0 92
aoqi@0 93 OopMapForCacheEntry::OopMapForCacheEntry(methodHandle method, int bci, OopMapCacheEntry* entry) : GenerateOopMap(method) {
aoqi@0 94 _bci = bci;
aoqi@0 95 _entry = entry;
aoqi@0 96 _stack_top = -1;
aoqi@0 97 }
aoqi@0 98
aoqi@0 99
aoqi@0 100 void OopMapForCacheEntry::compute_map(TRAPS) {
aoqi@0 101 assert(!method()->is_native(), "cannot compute oop map for native methods");
aoqi@0 102 // First check if it is a method where the stackmap is always empty
aoqi@0 103 if (method()->code_size() == 0 || method()->max_locals() + method()->max_stack() == 0) {
aoqi@0 104 _entry->set_mask_size(0);
aoqi@0 105 } else {
aoqi@0 106 ResourceMark rm;
aoqi@0 107 GenerateOopMap::compute_map(CATCH);
aoqi@0 108 result_for_basicblock(_bci);
aoqi@0 109 }
aoqi@0 110 }
aoqi@0 111
aoqi@0 112
aoqi@0 113 bool OopMapForCacheEntry::possible_gc_point(BytecodeStream *bcs) {
aoqi@0 114 return false; // We are not reporting any result. We call result_for_basicblock directly
aoqi@0 115 }
aoqi@0 116
aoqi@0 117
aoqi@0 118 void OopMapForCacheEntry::fill_stackmap_prolog(int nof_gc_points) {
aoqi@0 119 // Do nothing
aoqi@0 120 }
aoqi@0 121
aoqi@0 122
aoqi@0 123 void OopMapForCacheEntry::fill_stackmap_epilog() {
aoqi@0 124 // Do nothing
aoqi@0 125 }
aoqi@0 126
aoqi@0 127
aoqi@0 128 void OopMapForCacheEntry::fill_init_vars(GrowableArray<intptr_t> *init_vars) {
aoqi@0 129 // Do nothing
aoqi@0 130 }
aoqi@0 131
aoqi@0 132
aoqi@0 133 void OopMapForCacheEntry::fill_stackmap_for_opcodes(BytecodeStream *bcs,
aoqi@0 134 CellTypeState* vars,
aoqi@0 135 CellTypeState* stack,
aoqi@0 136 int stack_top) {
aoqi@0 137 // Only interested in one specific bci
aoqi@0 138 if (bcs->bci() == _bci) {
aoqi@0 139 _entry->set_mask(vars, stack, stack_top);
aoqi@0 140 _stack_top = stack_top;
aoqi@0 141 }
aoqi@0 142 }
aoqi@0 143
aoqi@0 144
aoqi@0 145 int OopMapForCacheEntry::size() {
aoqi@0 146 assert(_stack_top != -1, "compute_map must be called first");
aoqi@0 147 return ((method()->is_static()) ? 0 : 1) + method()->max_locals() + _stack_top;
aoqi@0 148 }
aoqi@0 149
aoqi@0 150
aoqi@0 151 // Implementation of InterpreterOopMap and OopMapCacheEntry
aoqi@0 152
aoqi@0 153 class VerifyClosure : public OffsetClosure {
aoqi@0 154 private:
aoqi@0 155 OopMapCacheEntry* _entry;
aoqi@0 156 bool _failed;
aoqi@0 157
aoqi@0 158 public:
aoqi@0 159 VerifyClosure(OopMapCacheEntry* entry) { _entry = entry; _failed = false; }
aoqi@0 160 void offset_do(int offset) { if (!_entry->is_oop(offset)) _failed = true; }
aoqi@0 161 bool failed() const { return _failed; }
aoqi@0 162 };
aoqi@0 163
aoqi@0 164 InterpreterOopMap::InterpreterOopMap() {
aoqi@0 165 initialize();
aoqi@0 166 #ifdef ASSERT
aoqi@0 167 _resource_allocate_bit_mask = true;
aoqi@0 168 #endif
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 InterpreterOopMap::~InterpreterOopMap() {
aoqi@0 172 // The expection is that the bit mask was allocated
aoqi@0 173 // last in this resource area. That would make the free of the
aoqi@0 174 // bit_mask effective (see how FREE_RESOURCE_ARRAY does a free).
aoqi@0 175 // If it was not allocated last, there is not a correctness problem
aoqi@0 176 // but the space for the bit_mask is not freed.
aoqi@0 177 assert(_resource_allocate_bit_mask, "Trying to free C heap space");
aoqi@0 178 if (mask_size() > small_mask_limit) {
aoqi@0 179 FREE_RESOURCE_ARRAY(uintptr_t, _bit_mask[0], mask_word_size());
aoqi@0 180 }
aoqi@0 181 }
aoqi@0 182
aoqi@0 183 bool InterpreterOopMap::is_empty() {
aoqi@0 184 bool result = _method == NULL;
aoqi@0 185 assert(_method != NULL || (_bci == 0 &&
aoqi@0 186 (_mask_size == 0 || _mask_size == USHRT_MAX) &&
aoqi@0 187 _bit_mask[0] == 0), "Should be completely empty");
aoqi@0 188 return result;
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 void InterpreterOopMap::initialize() {
aoqi@0 192 _method = NULL;
aoqi@0 193 _mask_size = USHRT_MAX; // This value should cause a failure quickly
aoqi@0 194 _bci = 0;
aoqi@0 195 _expression_stack_size = 0;
aoqi@0 196 for (int i = 0; i < N; i++) _bit_mask[i] = 0;
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 void InterpreterOopMap::iterate_oop(OffsetClosure* oop_closure) {
aoqi@0 200 int n = number_of_entries();
aoqi@0 201 int word_index = 0;
aoqi@0 202 uintptr_t value = 0;
aoqi@0 203 uintptr_t mask = 0;
aoqi@0 204 // iterate over entries
aoqi@0 205 for (int i = 0; i < n; i++, mask <<= bits_per_entry) {
aoqi@0 206 // get current word
aoqi@0 207 if (mask == 0) {
aoqi@0 208 value = bit_mask()[word_index++];
aoqi@0 209 mask = 1;
aoqi@0 210 }
aoqi@0 211 // test for oop
aoqi@0 212 if ((value & (mask << oop_bit_number)) != 0) oop_closure->offset_do(i);
aoqi@0 213 }
aoqi@0 214 }
aoqi@0 215
aoqi@0 216
aoqi@0 217 #ifdef ENABLE_ZAP_DEAD_LOCALS
aoqi@0 218
aoqi@0 219 void InterpreterOopMap::iterate_all(OffsetClosure* oop_closure, OffsetClosure* value_closure, OffsetClosure* dead_closure) {
aoqi@0 220 int n = number_of_entries();
aoqi@0 221 int word_index = 0;
aoqi@0 222 uintptr_t value = 0;
aoqi@0 223 uintptr_t mask = 0;
aoqi@0 224 // iterate over entries
aoqi@0 225 for (int i = 0; i < n; i++, mask <<= bits_per_entry) {
aoqi@0 226 // get current word
aoqi@0 227 if (mask == 0) {
aoqi@0 228 value = bit_mask()[word_index++];
aoqi@0 229 mask = 1;
aoqi@0 230 }
aoqi@0 231 // test for dead values & oops, and for live values
aoqi@0 232 if ((value & (mask << dead_bit_number)) != 0) dead_closure->offset_do(i); // call this for all dead values or oops
aoqi@0 233 else if ((value & (mask << oop_bit_number)) != 0) oop_closure->offset_do(i); // call this for all live oops
aoqi@0 234 else value_closure->offset_do(i); // call this for all live values
aoqi@0 235 }
aoqi@0 236 }
aoqi@0 237
aoqi@0 238 #endif
aoqi@0 239
aoqi@0 240
aoqi@0 241 void InterpreterOopMap::print() {
aoqi@0 242 int n = number_of_entries();
aoqi@0 243 tty->print("oop map for ");
aoqi@0 244 method()->print_value();
aoqi@0 245 tty->print(" @ %d = [%d] { ", bci(), n);
aoqi@0 246 for (int i = 0; i < n; i++) {
aoqi@0 247 #ifdef ENABLE_ZAP_DEAD_LOCALS
aoqi@0 248 if (is_dead(i)) tty->print("%d+ ", i);
aoqi@0 249 else
aoqi@0 250 #endif
aoqi@0 251 if (is_oop(i)) tty->print("%d ", i);
aoqi@0 252 }
aoqi@0 253 tty->print_cr("}");
aoqi@0 254 }
aoqi@0 255
aoqi@0 256 class MaskFillerForNative: public NativeSignatureIterator {
aoqi@0 257 private:
aoqi@0 258 uintptr_t * _mask; // the bit mask to be filled
aoqi@0 259 int _size; // the mask size in bits
aoqi@0 260
aoqi@0 261 void set_one(int i) {
aoqi@0 262 i *= InterpreterOopMap::bits_per_entry;
aoqi@0 263 assert(0 <= i && i < _size, "offset out of bounds");
aoqi@0 264 _mask[i / BitsPerWord] |= (((uintptr_t) 1 << InterpreterOopMap::oop_bit_number) << (i % BitsPerWord));
aoqi@0 265 }
aoqi@0 266
aoqi@0 267 public:
aoqi@0 268 void pass_int() { /* ignore */ }
aoqi@0 269 void pass_long() { /* ignore */ }
aoqi@0 270 void pass_float() { /* ignore */ }
aoqi@0 271 void pass_double() { /* ignore */ }
aoqi@0 272 void pass_object() { set_one(offset()); }
aoqi@0 273
aoqi@0 274 MaskFillerForNative(methodHandle method, uintptr_t* mask, int size) : NativeSignatureIterator(method) {
aoqi@0 275 _mask = mask;
aoqi@0 276 _size = size;
aoqi@0 277 // initialize with 0
aoqi@0 278 int i = (size + BitsPerWord - 1) / BitsPerWord;
aoqi@0 279 while (i-- > 0) _mask[i] = 0;
aoqi@0 280 }
aoqi@0 281
aoqi@0 282 void generate() {
aoqi@0 283 NativeSignatureIterator::iterate();
aoqi@0 284 }
aoqi@0 285 };
aoqi@0 286
aoqi@0 287 bool OopMapCacheEntry::verify_mask(CellTypeState* vars, CellTypeState* stack, int max_locals, int stack_top) {
aoqi@0 288 // Check mask includes map
aoqi@0 289 VerifyClosure blk(this);
aoqi@0 290 iterate_oop(&blk);
aoqi@0 291 if (blk.failed()) return false;
aoqi@0 292
aoqi@0 293 // Check if map is generated correctly
aoqi@0 294 // (Use ?: operator to make sure all 'true' & 'false' are represented exactly the same so we can use == afterwards)
aoqi@0 295 if (TraceOopMapGeneration && Verbose) tty->print("Locals (%d): ", max_locals);
aoqi@0 296
aoqi@0 297 for(int i = 0; i < max_locals; i++) {
aoqi@0 298 bool v1 = is_oop(i) ? true : false;
aoqi@0 299 bool v2 = vars[i].is_reference() ? true : false;
aoqi@0 300 assert(v1 == v2, "locals oop mask generation error");
aoqi@0 301 if (TraceOopMapGeneration && Verbose) tty->print("%d", v1 ? 1 : 0);
aoqi@0 302 #ifdef ENABLE_ZAP_DEAD_LOCALS
aoqi@0 303 bool v3 = is_dead(i) ? true : false;
aoqi@0 304 bool v4 = !vars[i].is_live() ? true : false;
aoqi@0 305 assert(v3 == v4, "locals live mask generation error");
aoqi@0 306 assert(!(v1 && v3), "dead value marked as oop");
aoqi@0 307 #endif
aoqi@0 308 }
aoqi@0 309
aoqi@0 310 if (TraceOopMapGeneration && Verbose) { tty->cr(); tty->print("Stack (%d): ", stack_top); }
aoqi@0 311 for(int j = 0; j < stack_top; j++) {
aoqi@0 312 bool v1 = is_oop(max_locals + j) ? true : false;
aoqi@0 313 bool v2 = stack[j].is_reference() ? true : false;
aoqi@0 314 assert(v1 == v2, "stack oop mask generation error");
aoqi@0 315 if (TraceOopMapGeneration && Verbose) tty->print("%d", v1 ? 1 : 0);
aoqi@0 316 #ifdef ENABLE_ZAP_DEAD_LOCALS
aoqi@0 317 bool v3 = is_dead(max_locals + j) ? true : false;
aoqi@0 318 bool v4 = !stack[j].is_live() ? true : false;
aoqi@0 319 assert(v3 == v4, "stack live mask generation error");
aoqi@0 320 assert(!(v1 && v3), "dead value marked as oop");
aoqi@0 321 #endif
aoqi@0 322 }
aoqi@0 323 if (TraceOopMapGeneration && Verbose) tty->cr();
aoqi@0 324 return true;
aoqi@0 325 }
aoqi@0 326
aoqi@0 327 void OopMapCacheEntry::allocate_bit_mask() {
aoqi@0 328 if (mask_size() > small_mask_limit) {
aoqi@0 329 assert(_bit_mask[0] == 0, "bit mask should be new or just flushed");
aoqi@0 330 _bit_mask[0] = (intptr_t)
aoqi@0 331 NEW_C_HEAP_ARRAY(uintptr_t, mask_word_size(), mtClass);
aoqi@0 332 }
aoqi@0 333 }
aoqi@0 334
aoqi@0 335 void OopMapCacheEntry::deallocate_bit_mask() {
aoqi@0 336 if (mask_size() > small_mask_limit && _bit_mask[0] != 0) {
aoqi@0 337 assert(!Thread::current()->resource_area()->contains((void*)_bit_mask[0]),
aoqi@0 338 "This bit mask should not be in the resource area");
aoqi@0 339 FREE_C_HEAP_ARRAY(uintptr_t, _bit_mask[0], mtClass);
aoqi@0 340 debug_only(_bit_mask[0] = 0;)
aoqi@0 341 }
aoqi@0 342 }
aoqi@0 343
aoqi@0 344
aoqi@0 345 void OopMapCacheEntry::fill_for_native(methodHandle mh) {
aoqi@0 346 assert(mh->is_native(), "method must be native method");
aoqi@0 347 set_mask_size(mh->size_of_parameters() * bits_per_entry);
aoqi@0 348 allocate_bit_mask();
aoqi@0 349 // fill mask for parameters
aoqi@0 350 MaskFillerForNative mf(mh, bit_mask(), mask_size());
aoqi@0 351 mf.generate();
aoqi@0 352 }
aoqi@0 353
aoqi@0 354
aoqi@0 355 void OopMapCacheEntry::fill(methodHandle method, int bci) {
aoqi@0 356 HandleMark hm;
aoqi@0 357 // Flush entry to deallocate an existing entry
aoqi@0 358 flush();
aoqi@0 359 set_method(method());
aoqi@0 360 set_bci(bci);
aoqi@0 361 if (method->is_native()) {
aoqi@0 362 // Native method activations have oops only among the parameters and one
aoqi@0 363 // extra oop following the parameters (the mirror for static native methods).
aoqi@0 364 fill_for_native(method);
aoqi@0 365 } else {
aoqi@0 366 EXCEPTION_MARK;
aoqi@0 367 OopMapForCacheEntry gen(method, bci, this);
aoqi@0 368 gen.compute_map(CATCH);
aoqi@0 369 }
aoqi@0 370 }
aoqi@0 371
aoqi@0 372
aoqi@0 373 void OopMapCacheEntry::set_mask(CellTypeState *vars, CellTypeState *stack, int stack_top) {
aoqi@0 374 // compute bit mask size
aoqi@0 375 int max_locals = method()->max_locals();
aoqi@0 376 int n_entries = max_locals + stack_top;
aoqi@0 377 set_mask_size(n_entries * bits_per_entry);
aoqi@0 378 allocate_bit_mask();
aoqi@0 379 set_expression_stack_size(stack_top);
aoqi@0 380
aoqi@0 381 // compute bits
aoqi@0 382 int word_index = 0;
aoqi@0 383 uintptr_t value = 0;
aoqi@0 384 uintptr_t mask = 1;
aoqi@0 385
aoqi@0 386 CellTypeState* cell = vars;
aoqi@0 387 for (int entry_index = 0; entry_index < n_entries; entry_index++, mask <<= bits_per_entry, cell++) {
aoqi@0 388 // store last word
aoqi@0 389 if (mask == 0) {
aoqi@0 390 bit_mask()[word_index++] = value;
aoqi@0 391 value = 0;
aoqi@0 392 mask = 1;
aoqi@0 393 }
aoqi@0 394
aoqi@0 395 // switch to stack when done with locals
aoqi@0 396 if (entry_index == max_locals) {
aoqi@0 397 cell = stack;
aoqi@0 398 }
aoqi@0 399
aoqi@0 400 // set oop bit
aoqi@0 401 if ( cell->is_reference()) {
aoqi@0 402 value |= (mask << oop_bit_number );
aoqi@0 403 }
aoqi@0 404
aoqi@0 405 #ifdef ENABLE_ZAP_DEAD_LOCALS
aoqi@0 406 // set dead bit
aoqi@0 407 if (!cell->is_live()) {
aoqi@0 408 value |= (mask << dead_bit_number);
aoqi@0 409 assert(!cell->is_reference(), "dead value marked as oop");
aoqi@0 410 }
aoqi@0 411 #endif
aoqi@0 412 }
aoqi@0 413
aoqi@0 414 // make sure last word is stored
aoqi@0 415 bit_mask()[word_index] = value;
aoqi@0 416
aoqi@0 417 // verify bit mask
aoqi@0 418 assert(verify_mask(vars, stack, max_locals, stack_top), "mask could not be verified");
aoqi@0 419
aoqi@0 420
aoqi@0 421 }
aoqi@0 422
aoqi@0 423 void OopMapCacheEntry::flush() {
aoqi@0 424 deallocate_bit_mask();
aoqi@0 425 initialize();
aoqi@0 426 }
aoqi@0 427
aoqi@0 428
aoqi@0 429 // Implementation of OopMapCache
aoqi@0 430
aoqi@0 431 #ifndef PRODUCT
aoqi@0 432
aoqi@0 433 static long _total_memory_usage = 0;
aoqi@0 434
aoqi@0 435 long OopMapCache::memory_usage() {
aoqi@0 436 return _total_memory_usage;
aoqi@0 437 }
aoqi@0 438
aoqi@0 439 #endif
aoqi@0 440
aoqi@0 441 void InterpreterOopMap::resource_copy(OopMapCacheEntry* from) {
aoqi@0 442 assert(_resource_allocate_bit_mask,
aoqi@0 443 "Should not resource allocate the _bit_mask");
aoqi@0 444
aoqi@0 445 set_method(from->method());
aoqi@0 446 set_bci(from->bci());
aoqi@0 447 set_mask_size(from->mask_size());
aoqi@0 448 set_expression_stack_size(from->expression_stack_size());
aoqi@0 449
aoqi@0 450 // Is the bit mask contained in the entry?
aoqi@0 451 if (from->mask_size() <= small_mask_limit) {
aoqi@0 452 memcpy((void *)_bit_mask, (void *)from->_bit_mask,
aoqi@0 453 mask_word_size() * BytesPerWord);
aoqi@0 454 } else {
aoqi@0 455 // The expectation is that this InterpreterOopMap is a recently created
aoqi@0 456 // and empty. It is used to get a copy of a cached entry.
aoqi@0 457 // If the bit mask has a value, it should be in the
aoqi@0 458 // resource area.
aoqi@0 459 assert(_bit_mask[0] == 0 ||
aoqi@0 460 Thread::current()->resource_area()->contains((void*)_bit_mask[0]),
aoqi@0 461 "The bit mask should have been allocated from a resource area");
aoqi@0 462 // Allocate the bit_mask from a Resource area for performance. Allocating
aoqi@0 463 // from the C heap as is done for OopMapCache has a significant
aoqi@0 464 // performance impact.
aoqi@0 465 _bit_mask[0] = (uintptr_t) NEW_RESOURCE_ARRAY(uintptr_t, mask_word_size());
aoqi@0 466 assert(_bit_mask[0] != 0, "bit mask was not allocated");
aoqi@0 467 memcpy((void*) _bit_mask[0], (void*) from->_bit_mask[0],
aoqi@0 468 mask_word_size() * BytesPerWord);
aoqi@0 469 }
aoqi@0 470 }
aoqi@0 471
aoqi@0 472 inline unsigned int OopMapCache::hash_value_for(methodHandle method, int bci) {
aoqi@0 473 // We use method->code_size() rather than method->identity_hash() below since
aoqi@0 474 // the mark may not be present if a pointer to the method is already reversed.
aoqi@0 475 return ((unsigned int) bci)
aoqi@0 476 ^ ((unsigned int) method->max_locals() << 2)
aoqi@0 477 ^ ((unsigned int) method->code_size() << 4)
aoqi@0 478 ^ ((unsigned int) method->size_of_parameters() << 6);
aoqi@0 479 }
aoqi@0 480
aoqi@0 481
aoqi@0 482 OopMapCache::OopMapCache() :
aoqi@0 483 _mut(Mutex::leaf, "An OopMapCache lock", true)
aoqi@0 484 {
aoqi@0 485 _array = NEW_C_HEAP_ARRAY(OopMapCacheEntry, _size, mtClass);
aoqi@0 486 // Cannot call flush for initialization, since flush
aoqi@0 487 // will check if memory should be deallocated
aoqi@0 488 for(int i = 0; i < _size; i++) _array[i].initialize();
aoqi@0 489 NOT_PRODUCT(_total_memory_usage += sizeof(OopMapCache) + (sizeof(OopMapCacheEntry) * _size);)
aoqi@0 490 }
aoqi@0 491
aoqi@0 492
aoqi@0 493 OopMapCache::~OopMapCache() {
aoqi@0 494 assert(_array != NULL, "sanity check");
aoqi@0 495 // Deallocate oop maps that are allocated out-of-line
aoqi@0 496 flush();
aoqi@0 497 // Deallocate array
aoqi@0 498 NOT_PRODUCT(_total_memory_usage -= sizeof(OopMapCache) + (sizeof(OopMapCacheEntry) * _size);)
aoqi@0 499 FREE_C_HEAP_ARRAY(OopMapCacheEntry, _array, mtClass);
aoqi@0 500 }
aoqi@0 501
aoqi@0 502 OopMapCacheEntry* OopMapCache::entry_at(int i) const {
aoqi@0 503 return &_array[i % _size];
aoqi@0 504 }
aoqi@0 505
aoqi@0 506 void OopMapCache::flush() {
aoqi@0 507 for (int i = 0; i < _size; i++) _array[i].flush();
aoqi@0 508 }
aoqi@0 509
aoqi@0 510 void OopMapCache::flush_obsolete_entries() {
aoqi@0 511 for (int i = 0; i < _size; i++)
aoqi@0 512 if (!_array[i].is_empty() && _array[i].method()->is_old()) {
aoqi@0 513 // Cache entry is occupied by an old redefined method and we don't want
aoqi@0 514 // to pin it down so flush the entry.
aoqi@0 515 RC_TRACE(0x08000000, ("flush: %s(%s): cached entry @%d",
aoqi@0 516 _array[i].method()->name()->as_C_string(),
aoqi@0 517 _array[i].method()->signature()->as_C_string(), i));
aoqi@0 518
aoqi@0 519 _array[i].flush();
aoqi@0 520 }
aoqi@0 521 }
aoqi@0 522
aoqi@0 523 void OopMapCache::lookup(methodHandle method,
aoqi@0 524 int bci,
aoqi@0 525 InterpreterOopMap* entry_for) {
aoqi@0 526 MutexLocker x(&_mut);
aoqi@0 527
aoqi@0 528 OopMapCacheEntry* entry = NULL;
aoqi@0 529 int probe = hash_value_for(method, bci);
aoqi@0 530
aoqi@0 531 // Search hashtable for match
aoqi@0 532 int i;
aoqi@0 533 for(i = 0; i < _probe_depth; i++) {
aoqi@0 534 entry = entry_at(probe + i);
aoqi@0 535 if (entry->match(method, bci)) {
aoqi@0 536 entry_for->resource_copy(entry);
aoqi@0 537 assert(!entry_for->is_empty(), "A non-empty oop map should be returned");
aoqi@0 538 return;
aoqi@0 539 }
aoqi@0 540 }
aoqi@0 541
aoqi@0 542 if (TraceOopMapGeneration) {
aoqi@0 543 static int count = 0;
aoqi@0 544 ResourceMark rm;
aoqi@0 545 tty->print("%d - Computing oopmap at bci %d for ", ++count, bci);
aoqi@0 546 method->print_value(); tty->cr();
aoqi@0 547 }
aoqi@0 548
aoqi@0 549 // Entry is not in hashtable.
aoqi@0 550 // Compute entry and return it
aoqi@0 551
aoqi@0 552 if (method->should_not_be_cached()) {
aoqi@0 553 // It is either not safe or not a good idea to cache this Method*
aoqi@0 554 // at this time. We give the caller of lookup() a copy of the
aoqi@0 555 // interesting info via parameter entry_for, but we don't add it to
aoqi@0 556 // the cache. See the gory details in Method*.cpp.
aoqi@0 557 compute_one_oop_map(method, bci, entry_for);
aoqi@0 558 return;
aoqi@0 559 }
aoqi@0 560
aoqi@0 561 // First search for an empty slot
aoqi@0 562 for(i = 0; i < _probe_depth; i++) {
aoqi@0 563 entry = entry_at(probe + i);
aoqi@0 564 if (entry->is_empty()) {
aoqi@0 565 entry->fill(method, bci);
aoqi@0 566 entry_for->resource_copy(entry);
aoqi@0 567 assert(!entry_for->is_empty(), "A non-empty oop map should be returned");
aoqi@0 568 return;
aoqi@0 569 }
aoqi@0 570 }
aoqi@0 571
aoqi@0 572 if (TraceOopMapGeneration) {
aoqi@0 573 ResourceMark rm;
aoqi@0 574 tty->print_cr("*** collision in oopmap cache - flushing item ***");
aoqi@0 575 }
aoqi@0 576
aoqi@0 577 // No empty slot (uncommon case). Use (some approximation of a) LRU algorithm
aoqi@0 578 //entry_at(probe + _probe_depth - 1)->flush();
aoqi@0 579 //for(i = _probe_depth - 1; i > 0; i--) {
aoqi@0 580 // // Coping entry[i] = entry[i-1];
aoqi@0 581 // OopMapCacheEntry *to = entry_at(probe + i);
aoqi@0 582 // OopMapCacheEntry *from = entry_at(probe + i - 1);
aoqi@0 583 // to->copy(from);
aoqi@0 584 // }
aoqi@0 585
aoqi@0 586 assert(method->is_method(), "gaga");
aoqi@0 587
aoqi@0 588 entry = entry_at(probe + 0);
aoqi@0 589 entry->fill(method, bci);
aoqi@0 590
aoqi@0 591 // Copy the newly cached entry to input parameter
aoqi@0 592 entry_for->resource_copy(entry);
aoqi@0 593
aoqi@0 594 if (TraceOopMapGeneration) {
aoqi@0 595 ResourceMark rm;
aoqi@0 596 tty->print("Done with ");
aoqi@0 597 method->print_value(); tty->cr();
aoqi@0 598 }
aoqi@0 599 assert(!entry_for->is_empty(), "A non-empty oop map should be returned");
aoqi@0 600
aoqi@0 601 return;
aoqi@0 602 }
aoqi@0 603
aoqi@0 604 void OopMapCache::compute_one_oop_map(methodHandle method, int bci, InterpreterOopMap* entry) {
aoqi@0 605 // Due to the invariants above it's tricky to allocate a temporary OopMapCacheEntry on the stack
aoqi@0 606 OopMapCacheEntry* tmp = NEW_C_HEAP_ARRAY(OopMapCacheEntry, 1, mtClass);
aoqi@0 607 tmp->initialize();
aoqi@0 608 tmp->fill(method, bci);
aoqi@0 609 entry->resource_copy(tmp);
aoqi@0 610 FREE_C_HEAP_ARRAY(OopMapCacheEntry, tmp, mtInternal);
aoqi@0 611 }

mercurial