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

25 26 27 28
#ifndef SHARE_VM_MEMORY_UNIVERSE_HPP
#define SHARE_VM_MEMORY_UNIVERSE_HPP

#include "runtime/handles.hpp"
29
#include "utilities/array.hpp"
30 31
#include "utilities/growableArray.hpp"

D
duke 已提交
32 33 34 35 36 37 38 39 40 41 42 43
// Universe is a name space holding known system classes and objects in the VM.
//
// Loaded classes are accessible through the SystemDictionary.
//
// The object heap is allocated and accessed through Universe, and various allocation
// support is provided. Allocation by the interpreter and compiled code is done inline
// and bails out to Scavenge::invoke_and_allocate.

class CollectedHeap;
class DeferredObjAllocEvent;


44 45 46 47 48
// A helper class for caching a Method* when the user of the cache
// only cares about the latest version of the Method*.  This cache safely
// interacts with the RedefineClasses API.

class LatestMethodCache : public CHeapObj<mtClass> {
49 50
  // We save the Klass* and the idnum of Method* in order to get
  // the current cached Method*.
D
duke 已提交
51
 private:
52
  Klass*                _klass;
D
duke 已提交
53 54 55
  int                   _method_idnum;

 public:
56 57
  LatestMethodCache()   { _klass = NULL; _method_idnum = -1; }
  ~LatestMethodCache()  { _klass = NULL; _method_idnum = -1; }
D
duke 已提交
58

59 60 61 62 63
  void   init(Klass* k, Method* m);
  Klass* klass() const           { return _klass; }
  int    method_idnum() const    { return _method_idnum; }

  Method* get_method();
D
duke 已提交
64

65 66 67 68 69 70 71 72 73 74
  // Enhanced Class Redefinition support
  void classes_do(void f(Klass*)) {
    f(_klass);
  }

  // CDS support.  Replace the klass in this with the archive version
  // could use this for Enhanced Class Redefinition also.
  void serialize(SerializeClosure* f) {
    f->do_ptr((void**)&_klass);
  }
D
duke 已提交
75 76 77
};


78
// For UseCompressedOops.
79
struct NarrowPtrStruct {
80 81
  // Base address for oop-within-java-object materialization.
  // NULL if using wide oops or zero based narrow oops.
82
  address _base;
83 84 85
  // Number of shift bits for encoding/decoding narrow ptrs.
  // 0 if using wide ptrs or zero based unscaled narrow ptrs,
  // LogMinObjAlignmentInBytes/LogKlassAlignmentInBytes otherwise.
86
  int     _shift;
87
  // Generate code with implicit null checks for narrow ptrs.
88 89 90
  bool    _use_implicit_null_checks;
};

91 92 93 94 95 96 97 98
enum VerifyOption {
      VerifyOption_Default = 0,

      // G1
      VerifyOption_G1UsePrevMarking = VerifyOption_Default,
      VerifyOption_G1UseNextMarking = VerifyOption_G1UsePrevMarking + 1,
      VerifyOption_G1UseMarkWord    = VerifyOption_G1UseNextMarking + 1
};
D
duke 已提交
99 100

class Universe: AllStatic {
101
  // Ugh.  Universe is much too friendly.
D
duke 已提交
102 103 104 105 106 107 108
  friend class MarkSweep;
  friend class oopDesc;
  friend class ClassLoader;
  friend class Arguments;
  friend class SystemDictionary;
  friend class VMStructs;
  friend class VM_PopulateDumpSharedSpace;
109
  friend class Metaspace;
D
duke 已提交
110 111 112 113 114 115 116

  friend jint  universe_init();
  friend void  universe2_init();
  friend bool  universe_post_init();

 private:
  // Known classes in the VM
117 118 119 120 121 122 123 124 125 126 127
  static Klass* _boolArrayKlassObj;
  static Klass* _byteArrayKlassObj;
  static Klass* _charArrayKlassObj;
  static Klass* _intArrayKlassObj;
  static Klass* _shortArrayKlassObj;
  static Klass* _longArrayKlassObj;
  static Klass* _singleArrayKlassObj;
  static Klass* _doubleArrayKlassObj;
  static Klass* _typeArrayKlassObjs[T_VOID+1];

  static Klass* _objectArrayKlassObj;
D
duke 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

  // Known objects in the VM

  // Primitive objects
  static oop _int_mirror;
  static oop _float_mirror;
  static oop _double_mirror;
  static oop _byte_mirror;
  static oop _bool_mirror;
  static oop _char_mirror;
  static oop _long_mirror;
  static oop _short_mirror;
  static oop _void_mirror;

  static oop          _main_thread_group;             // Reference to the main thread group object
  static oop          _system_thread_group;           // Reference to the system thread group object

  static objArrayOop  _the_empty_class_klass_array;   // Canonicalized obj array of type java.lang.Class
146 147
  static oop          _the_null_string;               // A cache of "null" as a Java string
  static oop          _the_min_jint_string;          // A cache of "-2147483648" as a Java string
148 149 150 151
  static LatestMethodCache* _finalizer_register_cache; // static method for registering finalizable objects
  static LatestMethodCache* _loader_addClass_cache;    // method for registering loaded classes in class loader vector
  static LatestMethodCache* _pd_implies_cache;         // method for checking protection domain attributes

152 153
  static Method* _throw_illegal_access_error;

154 155 156 157 158 159
  // preallocated error objects (no backtrace)
  static oop          _out_of_memory_error_java_heap;
  static oop          _out_of_memory_error_metaspace;
  static oop          _out_of_memory_error_class_metaspace;
  static oop          _out_of_memory_error_array_size;
  static oop          _out_of_memory_error_gc_overhead_limit;
D
duke 已提交
160

161 162 163 164 165 166 167
  static Array<int>*       _the_empty_int_array;    // Canonicalized int array
  static Array<u2>*        _the_empty_short_array;  // Canonicalized short array
  static Array<Klass*>*  _the_empty_klass_array;  // Canonicalized klass obj array
  static Array<Method*>* _the_empty_method_array; // Canonicalized method obj array

  static Array<Klass*>*  _the_array_interfaces_array;

D
duke 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  // array of preallocated error objects with backtrace
  static objArrayOop   _preallocated_out_of_memory_error_array;

  // number of preallocated error objects available for use
  static volatile jint _preallocated_out_of_memory_error_avail_count;

  static oop          _null_ptr_exception_instance;   // preallocated exception object
  static oop          _arithmetic_exception_instance; // preallocated exception object
  static oop          _virtual_machine_error_instance; // preallocated exception object
  // The object used as an exception dummy when exceptions are thrown for
  // the vm thread.
  static oop          _vm_exception;

  // The particular choice of collected heap.
  static CollectedHeap* _collectedHeap;
183

184 185
  static intptr_t _non_oop_bits;

186
  // For UseCompressedOops.
187
  static struct NarrowPtrStruct _narrow_oop;
188
  // For UseCompressedClassPointers.
189 190
  static struct NarrowPtrStruct _narrow_klass;
  static address _narrow_ptrs_base;
D
duke 已提交
191 192 193

  // array of dummy objects used with +FullGCAlot
  debug_only(static objArrayOop _fullgc_alot_dummy_array;)
194
  // index of next entry to clear
D
duke 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
  debug_only(static int         _fullgc_alot_dummy_next;)

  // Compiler/dispatch support
  static int  _base_vtable_size;                      // Java vtbl size of klass Object (in words)

  // Initialization
  static bool _bootstrapping;                         // true during genesis
  static bool _fully_initialized;                     // true after universe_init and initialize_vtables called

  // the array of preallocated errors with backtraces
  static objArrayOop  preallocated_out_of_memory_errors()     { return _preallocated_out_of_memory_error_array; }

  // generate an out of memory error; if possible using an error with preallocated backtrace;
  // otherwise return the given default error.
  static oop        gen_out_of_memory_error(oop default_err);

  // Historic gc information
  static size_t _heap_capacity_at_last_gc;
  static size_t _heap_used_at_last_gc;

  static jint initialize_heap();
  static void initialize_basic_type_mirrors(TRAPS);
  static void fixup_mirrors(TRAPS);

  static void reinitialize_vtable_of(KlassHandle h_k, TRAPS);
  static void reinitialize_itables(TRAPS);
  static void compute_base_vtable_size();             // compute vtable size of class Object

  static void genesis(TRAPS);                         // Create the initial world

  // Mirrors for primitive classes (created eagerly)
  static oop check_mirror(oop m) {
    assert(m != NULL, "mirror not initialized");
    return m;
  }

231 232 233 234 235
  static void     set_narrow_oop_base(address base) {
    assert(UseCompressedOops, "no compressed oops?");
    _narrow_oop._base    = base;
  }
  static void     set_narrow_klass_base(address base) {
236
    assert(UseCompressedClassPointers, "no compressed klass ptrs?");
237 238 239 240 241 242
    _narrow_klass._base   = base;
  }
  static void     set_narrow_oop_use_implicit_null_checks(bool use) {
    assert(UseCompressedOops, "no compressed ptrs?");
    _narrow_oop._use_implicit_null_checks   = use;
  }
243

D
duke 已提交
244 245 246 247 248 249 250 251 252
  // Debugging
  static int _verify_count;                           // number of verifies done
  // True during call to verify().  Should only be set/cleared in verify().
  static bool _verify_in_progress;

  static void compute_verify_oop_data();

 public:
  // Known classes in the VM
253 254 255 256 257 258 259 260 261 262
  static Klass* boolArrayKlassObj()                 { return _boolArrayKlassObj;   }
  static Klass* byteArrayKlassObj()                 { return _byteArrayKlassObj;   }
  static Klass* charArrayKlassObj()                 { return _charArrayKlassObj;   }
  static Klass* intArrayKlassObj()                  { return _intArrayKlassObj;    }
  static Klass* shortArrayKlassObj()                { return _shortArrayKlassObj;  }
  static Klass* longArrayKlassObj()                 { return _longArrayKlassObj;   }
  static Klass* singleArrayKlassObj()               { return _singleArrayKlassObj; }
  static Klass* doubleArrayKlassObj()               { return _doubleArrayKlassObj; }

  static Klass* objectArrayKlassObj() {
D
duke 已提交
263 264 265
    return _objectArrayKlassObj;
  }

266
  static Klass* typeArrayKlassObj(BasicType t) {
267
    assert((uint)t < T_VOID+1, err_msg("range check for type: %s", type2name(t)));
D
duke 已提交
268 269 270 271
    assert(_typeArrayKlassObjs[t] != NULL, "domain check");
    return _typeArrayKlassObjs[t];
  }

272 273
  // Known objects in the VM
  static oop int_mirror()                   { return check_mirror(_int_mirror); }
D
duke 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  static oop float_mirror()                 { return check_mirror(_float_mirror); }
  static oop double_mirror()                { return check_mirror(_double_mirror); }
  static oop byte_mirror()                  { return check_mirror(_byte_mirror); }
  static oop bool_mirror()                  { return check_mirror(_bool_mirror); }
  static oop char_mirror()                  { return check_mirror(_char_mirror); }
  static oop long_mirror()                  { return check_mirror(_long_mirror); }
  static oop short_mirror()                 { return check_mirror(_short_mirror); }
  static oop void_mirror()                  { return check_mirror(_void_mirror); }

  // table of same
  static oop _mirrors[T_VOID+1];

  static oop java_mirror(BasicType t) {
    assert((uint)t < T_VOID+1, "range check");
    return check_mirror(_mirrors[t]);
  }
  static oop      main_thread_group()                 { return _main_thread_group; }
  static void set_main_thread_group(oop group)        { _main_thread_group = group;}

  static oop      system_thread_group()               { return _system_thread_group; }
  static void set_system_thread_group(oop group)      { _system_thread_group = group;}

  static objArrayOop  the_empty_class_klass_array ()  { return _the_empty_class_klass_array;   }
297
  static Array<Klass*>* the_array_interfaces_array() { return _the_array_interfaces_array;   }
298 299
  static oop          the_null_string()               { return _the_null_string;               }
  static oop          the_min_jint_string()          { return _the_min_jint_string;          }
M
mullan 已提交
300

301 302 303 304
  static Method*      finalizer_register_method()     { return _finalizer_register_cache->get_method(); }
  static Method*      loader_addClass_method()        { return _loader_addClass_cache->get_method(); }

  static Method*      protection_domain_implies_method() { return _pd_implies_cache->get_method(); }
M
mullan 已提交
305

D
duke 已提交
306 307 308 309
  static oop          null_ptr_exception_instance()   { return _null_ptr_exception_instance;   }
  static oop          arithmetic_exception_instance() { return _arithmetic_exception_instance; }
  static oop          virtual_machine_error_instance() { return _virtual_machine_error_instance; }
  static oop          vm_exception()                  { return _vm_exception; }
310
  static Method*      throw_illegal_access_error()    { return _throw_illegal_access_error; }
D
duke 已提交
311

312 313 314 315 316
  static Array<int>*       the_empty_int_array()    { return _the_empty_int_array; }
  static Array<u2>*        the_empty_short_array()  { return _the_empty_short_array; }
  static Array<Method*>* the_empty_method_array() { return _the_empty_method_array; }
  static Array<Klass*>*  the_empty_klass_array()  { return _the_empty_klass_array; }

D
duke 已提交
317 318 319 320
  // OutOfMemoryError support. Returns an error with the required message. The returned error
  // may or may not have a backtrace. If error has a backtrace then the stack trace is already
  // filled in.
  static oop out_of_memory_error_java_heap()          { return gen_out_of_memory_error(_out_of_memory_error_java_heap);  }
321 322
  static oop out_of_memory_error_metaspace()          { return gen_out_of_memory_error(_out_of_memory_error_metaspace);   }
  static oop out_of_memory_error_class_metaspace()    { return gen_out_of_memory_error(_out_of_memory_error_class_metaspace);   }
D
duke 已提交
323 324 325 326
  static oop out_of_memory_error_array_size()         { return gen_out_of_memory_error(_out_of_memory_error_array_size); }
  static oop out_of_memory_error_gc_overhead_limit()  { return gen_out_of_memory_error(_out_of_memory_error_gc_overhead_limit);  }

  // Accessors needed for fast allocation
327 328 329 330 331 332 333 334 335
  static Klass** boolArrayKlassObj_addr()           { return &_boolArrayKlassObj;   }
  static Klass** byteArrayKlassObj_addr()           { return &_byteArrayKlassObj;   }
  static Klass** charArrayKlassObj_addr()           { return &_charArrayKlassObj;   }
  static Klass** intArrayKlassObj_addr()            { return &_intArrayKlassObj;    }
  static Klass** shortArrayKlassObj_addr()          { return &_shortArrayKlassObj;  }
  static Klass** longArrayKlassObj_addr()           { return &_longArrayKlassObj;   }
  static Klass** singleArrayKlassObj_addr()         { return &_singleArrayKlassObj; }
  static Klass** doubleArrayKlassObj_addr()         { return &_doubleArrayKlassObj; }
  static Klass** objectArrayKlassObj_addr()         { return &_objectArrayKlassObj; }
D
duke 已提交
336 337 338 339

  // The particular choice of collected heap.
  static CollectedHeap* heap() { return _collectedHeap; }

340
  // For UseCompressedOops
S
sla 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353
  // Narrow Oop encoding mode:
  // 0 - Use 32-bits oops without encoding when
  //     NarrowOopHeapBaseMin + heap_size < 4Gb
  // 1 - Use zero based compressed oops with encoding when
  //     NarrowOopHeapBaseMin + heap_size < 32Gb
  // 2 - Use compressed oops with heap base + encoding.
  enum NARROW_OOP_MODE {
    UnscaledNarrowOop  = 0,
    ZeroBasedNarrowOop = 1,
    HeapBasedNarrowOop = 2
  };
  static NARROW_OOP_MODE narrow_oop_mode();
  static const char* narrow_oop_mode_to_string(NARROW_OOP_MODE mode);
354
  static char*    preferred_heap_base(size_t heap_size, size_t alignment, NARROW_OOP_MODE mode);
S
sla 已提交
355
  static char*    preferred_metaspace_base(size_t heap_size, NARROW_OOP_MODE mode);
356 357 358 359 360
  static address  narrow_oop_base()                       { return  _narrow_oop._base; }
  static bool  is_narrow_oop_base(void* addr)             { return (narrow_oop_base() == (address)addr); }
  static int      narrow_oop_shift()                      { return  _narrow_oop._shift; }
  static bool     narrow_oop_use_implicit_null_checks()   { return  _narrow_oop._use_implicit_null_checks; }

361
  // For UseCompressedClassPointers
362 363 364 365 366 367 368 369
  static address  narrow_klass_base()                     { return  _narrow_klass._base; }
  static bool  is_narrow_klass_base(void* addr)           { return (narrow_klass_base() == (address)addr); }
  static int      narrow_klass_shift()                    { return  _narrow_klass._shift; }
  static bool     narrow_klass_use_implicit_null_checks() { return  _narrow_klass._use_implicit_null_checks; }

  static address* narrow_ptrs_base_addr()                 { return &_narrow_ptrs_base; }
  static void     set_narrow_ptrs_base(address a)         { _narrow_ptrs_base = a; }
  static address  narrow_ptrs_base()                      { return _narrow_ptrs_base; }
370 371

  // this is set in vm_version on sparc (and then reset in universe afaict)
372 373 374 375 376 377 378 379
  static void     set_narrow_oop_shift(int shift)         {
    _narrow_oop._shift   = shift;
  }

  static void     set_narrow_klass_shift(int shift)       {
    assert(shift == 0 || shift == LogKlassAlignmentInBytes, "invalid shift for klass ptrs");
    _narrow_klass._shift   = shift;
  }
380 381 382

  // Reserve Java heap and determine CompressedOops mode
  static ReservedSpace reserve_heap(size_t heap_size, size_t alignment);
383

D
duke 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
  // Historic gc information
  static size_t get_heap_capacity_at_last_gc()         { return _heap_capacity_at_last_gc; }
  static size_t get_heap_free_at_last_gc()             { return _heap_capacity_at_last_gc - _heap_used_at_last_gc; }
  static size_t get_heap_used_at_last_gc()             { return _heap_used_at_last_gc; }
  static void update_heap_info_at_gc();

  // Testers
  static bool is_bootstrapping()                      { return _bootstrapping; }
  static bool is_fully_initialized()                  { return _fully_initialized; }

  static inline bool element_type_should_be_aligned(BasicType type);
  static inline bool field_type_should_be_aligned(BasicType type);
  static bool        on_page_boundary(void* addr);
  static bool        should_fill_in_stack_trace(Handle throwable);
  static void check_alignment(uintx size, uintx alignment, const char* name);

  // Finalizer support.
  static void run_finalizers_on_exit();

  // Iteration

  // Apply "f" to the addresses of all the direct heap pointers maintained
  // as static fields of "Universe".
  static void oops_do(OopClosure* f, bool do_all = false);

409 410 411
  // CDS support
  static void serialize(SerializeClosure* f, bool do_all = false);

D
duke 已提交
412 413
  // Apply "f" to all klasses for basic types (classes not present in
  // SystemDictionary).
414
  static void basic_type_classes_do(void f(Klass*));
D
duke 已提交
415 416 417 418 419 420

  // For sharing -- fill in a list of known vtable pointers.
  static void init_self_patching_vtbl_list(void** list, int count);

  // Debugging
  static bool verify_in_progress() { return _verify_in_progress; }
421 422 423
  static void verify(VerifyOption option, const char* prefix, bool silent = VerifySilently);
  static void verify(const char* prefix, bool silent = VerifySilently) {
    verify(VerifyOption_Default, prefix, silent);
424
  }
425 426
  static void verify(bool silent = VerifySilently) {
    verify("", silent);
427 428
  }

429 430
  static int  verify_count()       { return _verify_count; }
  // The default behavior is to call print_on() on gclog_or_tty.
D
duke 已提交
431
  static void print();
432 433 434 435
  // The extended parameter determines which method on the heap will
  // be called: print_on() (extended == false) or print_extended_on()
  // (extended == true).
  static void print_on(outputStream* st, bool extended = false);
D
duke 已提交
436 437 438
  static void print_heap_at_SIGBREAK();
  static void print_heap_before_gc() { print_heap_before_gc(gclog_or_tty); }
  static void print_heap_after_gc()  { print_heap_after_gc(gclog_or_tty); }
N
never 已提交
439 440
  static void print_heap_before_gc(outputStream* st, bool ignore_extended = false);
  static void print_heap_after_gc(outputStream* st, bool ignore_extended = false);
D
duke 已提交
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

  // Change the number of dummy objects kept reachable by the full gc dummy
  // array; this should trigger relocation in a sliding compaction collector.
  debug_only(static bool release_fullgc_alot_dummy();)
  // The non-oop pattern (see compiledIC.hpp, etc)
  static void*   non_oop_word();

  // Oop verification (see MacroAssembler::verify_oop)
  static uintptr_t verify_oop_mask()          PRODUCT_RETURN0;
  static uintptr_t verify_oop_bits()          PRODUCT_RETURN0;
  static uintptr_t verify_mark_bits()         PRODUCT_RETURN0;
  static uintptr_t verify_mark_mask()         PRODUCT_RETURN0;

  // Flushing and deoptimization
  static void flush_dependents_on(instanceKlassHandle dependee);
456
  static void flush_dependents_on(Handle call_site, Handle method_handle);
D
duke 已提交
457 458 459 460 461 462 463 464 465 466 467
#ifdef HOTSWAP
  // Flushing and deoptimization in case of evolution
  static void flush_evol_dependents_on(instanceKlassHandle dependee);
#endif // HOTSWAP
  // Support for fullspeed debugging
  static void flush_dependents_on_method(methodHandle dependee);

  // Compiler support
  static int base_vtable_size()               { return _base_vtable_size; }
};

Z
zgu 已提交
468
class DeferredObjAllocEvent : public CHeapObj<mtInternal> {
D
duke 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
  private:
    oop    _oop;
    size_t _bytesize;
    jint   _arena_id;

  public:
    DeferredObjAllocEvent(const oop o, const size_t s, const jint id) {
      _oop      = o;
      _bytesize = s;
      _arena_id = id;
    }

    ~DeferredObjAllocEvent() {
    }

    jint   arena_id() { return _arena_id; }
    size_t bytesize() { return _bytesize; }
    oop    get_oop()  { return _oop; }
};
488 489

#endif // SHARE_VM_MEMORY_UNIVERSE_HPP