1 | /* |
2 | * Copyright (c) 1997, 2019, 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 | #ifndef SHARE_INTERPRETER_OOPMAPCACHE_HPP |
26 | #define SHARE_INTERPRETER_OOPMAPCACHE_HPP |
27 | |
28 | #include "oops/generateOopMap.hpp" |
29 | #include "runtime/mutex.hpp" |
30 | |
31 | // A Cache for storing (method, bci) -> oopMap. |
32 | // The memory management system uses the cache when locating object |
33 | // references in an interpreted frame. |
34 | // |
35 | // OopMapCache's are allocated lazily per InstanceKlass. |
36 | |
37 | // The oopMap (InterpreterOopMap) is stored as a bit mask. If the |
38 | // bit_mask can fit into two words it is stored in |
39 | // the _bit_mask array, otherwise it is allocated on the heap. |
40 | // For OopMapCacheEntry the bit_mask is allocated in the C heap |
41 | // because these entries persist between garbage collections. |
42 | // For InterpreterOopMap the bit_mask is allocated in |
43 | // a resource area for better performance. InterpreterOopMap |
44 | // should only be created and deleted during same garbage collection. |
45 | // |
46 | // If ENABBLE_ZAP_DEAD_LOCALS is defined, two bits are used |
47 | // per entry instead of one. In all cases, |
48 | // the first bit is set to indicate oops as opposed to other |
49 | // values. If the second bit is available, |
50 | // it is set for dead values. We get the following encoding: |
51 | // |
52 | // 00 live value |
53 | // 01 live oop |
54 | // 10 dead value |
55 | // 11 <unused> (we cannot distinguish between dead oops or values with the current oop map generator) |
56 | |
57 | |
58 | class OffsetClosure { |
59 | public: |
60 | virtual void offset_do(int offset) = 0; |
61 | }; |
62 | |
63 | class OopMapCacheEntry; |
64 | |
65 | class InterpreterOopMap: ResourceObj { |
66 | friend class OopMapCache; |
67 | |
68 | public: |
69 | enum { |
70 | N = 4, // the number of words reserved |
71 | // for inlined mask storage |
72 | small_mask_limit = N * BitsPerWord, // the maximum number of bits |
73 | // available for small masks, |
74 | // small_mask_limit can be set to 0 |
75 | // for testing bit_mask allocation |
76 | |
77 | bits_per_entry = 2, |
78 | dead_bit_number = 1, |
79 | oop_bit_number = 0 |
80 | }; |
81 | |
82 | private: |
83 | Method* _method; // the method for which the mask is valid |
84 | unsigned short _bci; // the bci for which the mask is valid |
85 | int _mask_size; // the mask size in bits |
86 | int _expression_stack_size; // the size of the expression stack in slots |
87 | |
88 | protected: |
89 | intptr_t _bit_mask[N]; // the bit mask if |
90 | // mask_size <= small_mask_limit, |
91 | // ptr to bit mask otherwise |
92 | // "protected" so that sub classes can |
93 | // access it without using trickery in |
94 | // methd bit_mask(). |
95 | #ifdef ASSERT |
96 | bool _resource_allocate_bit_mask; |
97 | #endif |
98 | |
99 | // access methods |
100 | Method* method() const { return _method; } |
101 | void set_method(Method* v) { _method = v; } |
102 | int bci() const { return _bci; } |
103 | void set_bci(int v) { _bci = v; } |
104 | int mask_size() const { return _mask_size; } |
105 | void set_mask_size(int v) { _mask_size = v; } |
106 | // Test bit mask size and return either the in-line bit mask or allocated |
107 | // bit mask. |
108 | uintptr_t* bit_mask() const { return (uintptr_t*)(mask_size() <= small_mask_limit ? (intptr_t)_bit_mask : _bit_mask[0]); } |
109 | |
110 | // return the word size of_bit_mask. mask_size() <= 4 * MAX_USHORT |
111 | size_t mask_word_size() const { |
112 | return (mask_size() + BitsPerWord - 1) / BitsPerWord; |
113 | } |
114 | |
115 | uintptr_t entry_at(int offset) const { int i = offset * bits_per_entry; return bit_mask()[i / BitsPerWord] >> (i % BitsPerWord); } |
116 | |
117 | void set_expression_stack_size(int sz) { _expression_stack_size = sz; } |
118 | |
119 | // Lookup |
120 | bool match(const methodHandle& method, int bci) const { return _method == method() && _bci == bci; } |
121 | bool is_empty() const; |
122 | |
123 | // Initialization |
124 | void initialize(); |
125 | |
126 | public: |
127 | InterpreterOopMap(); |
128 | ~InterpreterOopMap(); |
129 | |
130 | // Copy the OopMapCacheEntry in parameter "from" into this |
131 | // InterpreterOopMap. If the _bit_mask[0] in "from" points to |
132 | // allocated space (i.e., the bit mask was to large to hold |
133 | // in-line), allocate the space from a Resource area. |
134 | void resource_copy(OopMapCacheEntry* from); |
135 | |
136 | void iterate_oop(OffsetClosure* oop_closure) const; |
137 | void print() const; |
138 | |
139 | int number_of_entries() const { return mask_size() / bits_per_entry; } |
140 | bool is_dead(int offset) const { return (entry_at(offset) & (1 << dead_bit_number)) != 0; } |
141 | bool is_oop (int offset) const { return (entry_at(offset) & (1 << oop_bit_number )) != 0; } |
142 | |
143 | int expression_stack_size() const { return _expression_stack_size; } |
144 | |
145 | }; |
146 | |
147 | class OopMapCache : public CHeapObj<mtClass> { |
148 | static OopMapCacheEntry* volatile _old_entries; |
149 | private: |
150 | enum { _size = 32, // Use fixed size for now |
151 | _probe_depth = 3 // probe depth in case of collisions |
152 | }; |
153 | |
154 | OopMapCacheEntry* volatile * _array; |
155 | |
156 | unsigned int hash_value_for(const methodHandle& method, int bci) const; |
157 | OopMapCacheEntry* entry_at(int i) const; |
158 | bool put_at(int i, OopMapCacheEntry* entry, OopMapCacheEntry* old); |
159 | |
160 | static void enqueue_for_cleanup(OopMapCacheEntry* entry); |
161 | |
162 | void flush(); |
163 | |
164 | public: |
165 | OopMapCache(); |
166 | ~OopMapCache(); // free up memory |
167 | |
168 | // flush cache entry is occupied by an obsolete method |
169 | void flush_obsolete_entries(); |
170 | |
171 | // Returns the oopMap for (method, bci) in parameter "entry". |
172 | // Returns false if an oop map was not found. |
173 | void lookup(const methodHandle& method, int bci, InterpreterOopMap* entry); |
174 | |
175 | // Compute an oop map without updating the cache or grabbing any locks (for debugging) |
176 | static void compute_one_oop_map(const methodHandle& method, int bci, InterpreterOopMap* entry); |
177 | static void cleanup_old_entries(); |
178 | }; |
179 | |
180 | #endif // SHARE_INTERPRETER_OOPMAPCACHE_HPP |
181 | |