hotspot/src/share/vm/code/oopRecorder.hpp
author ant
Fri, 04 Dec 2009 15:07:15 +0300
changeset 4369 18b883ed2b58
parent 1 489c9b5090e2
child 5686 5435e77aa3df
child 5547 f4b087cbb361
permissions -rw-r--r--
6903354: deadlock involving Component.show & SunToolkit.getImageFromHash Reviewed-by: art, bae
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2005 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
// Recording and retrieval of oop relocations in compiled code.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
class CodeBlob;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
class OopRecorder : public ResourceObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
  // A two-way mapping from positive indexes to oop handles.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  // The zero index is reserved for a constant (sharable) null.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  // Indexes may not be negative.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  // Use the given arena to manage storage, if not NULL.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  // By default, uses the current ResourceArea.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  OopRecorder(Arena* arena = NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  // Generate a new index on which CodeBlob::oop_addr_at will work.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  // allocate_index and find_index never return the same index,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  // and allocate_index never returns the same index twice.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  // In fact, two successive calls to allocate_index return successive ints.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  int allocate_index(jobject h) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
    return add_handle(h, false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  // For a given jobject, this will return the same index repeatedly.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  // The index can later be given to oop_at to retrieve the oop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  // However, the oop must not be changed via CodeBlob::oop_addr_at.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  int find_index(jobject h) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    int index = maybe_find_index(h);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
    if (index < 0) {  // previously unallocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
      index = add_handle(h, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
    return index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  // variant of find_index which does not allocate if not found (yields -1)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  int maybe_find_index(jobject h);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  // returns the size of the generated oop table, for sizing the CodeBlob.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  // must be called after all oops are allocated!
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  int oop_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  // Retrieve the oop handle at a given index.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  jobject handle_at(int index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  int element_count() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    // there is always a NULL virtually present as first object
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    return _handles->length() + first_index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  // copy the generated oop table to CodeBlob
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  void copy_to(CodeBlob* code);  // => code->copy_oops(_handles)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  bool is_unused() { return _handles == NULL && !_complete; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  bool is_complete() { return _complete; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  // leaky hash table of handle => index, to help detect duplicate insertion
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  class IndexCache: public ResourceObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
    // This class is only used by the OopRecorder class.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    friend class OopRecorder;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
      _log_cache_size = 9,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
      _cache_size = (1<<_log_cache_size),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
      // Index entries are ints.  The LSBit is a collision indicator.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
      _collision_bit_shift = 0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
      _collision_bit = 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
      _index_shift = _collision_bit_shift+1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
    };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
    int _cache[_cache_size];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    static juint cache_index(jobject handle) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
      juint ci = (int) (intptr_t) handle;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
      ci ^= ci >> (BitsPerByte*2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
      ci += ci >> (BitsPerByte*1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
      return ci & (_cache_size-1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    int* cache_location(jobject handle) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
      return &_cache[ cache_index(handle) ];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    static bool cache_location_collision(int* cloc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
      return ((*cloc) & _collision_bit) != 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    static int cache_location_index(int* cloc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
      return (*cloc) >> _index_shift;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
    static void set_cache_location_index(int* cloc, int index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
      int cval0 = (*cloc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
      int cval1 = (index << _index_shift);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
      if (cval0 != 0 && cval1 != cval0)  cval1 += _collision_bit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
      (*cloc) = cval1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
    IndexCache();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  // Helper function; returns false for NULL or Universe::non_oop_word().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  inline bool is_real_jobject(jobject h);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  void maybe_initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  int add_handle(jobject h, bool make_findable);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  enum { null_index = 0, first_index = 1, index_cache_threshold = 20 };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  GrowableArray<jobject>*   _handles;  // ordered list (first is always NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  GrowableArray<int>*       _no_finds; // all unfindable indexes; usually empty
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  IndexCache*               _indexes;  // map: jobject -> its probable index
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  Arena*                    _arena;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  bool                      _complete;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  static int _find_index_calls, _hit_indexes, _missed_indexes;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
};