blob: 131446c48488e60107c0d6637b9cf9f46fc9babf [file] [log] [blame]
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_JIT_JIT_CODE_CACHE_H_
18#define ART_RUNTIME_JIT_JIT_CODE_CACHE_H_
19
20#include "instrumentation.h"
21
22#include "atomic.h"
23#include "base/macros.h"
24#include "base/mutex.h"
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010025#include "gc/accounting/bitmap.h"
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010026#include "gc/allocator/dlmalloc.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080027#include "gc_root.h"
28#include "jni.h"
29#include "oat_file.h"
30#include "object_callbacks.h"
31#include "safe_map.h"
32#include "thread_pool.h"
33
34namespace art {
35
Mathieu Chartiere401d142015-04-22 13:56:20 -070036class ArtMethod;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010037class LinearAlloc;
Nicolas Geoffray26705e22015-10-28 12:50:11 +000038class ProfilingInfo;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080039
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080040namespace jit {
41
42class JitInstrumentationCache;
43
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010044// Alignment that will suit all architectures.
45static constexpr int kJitCodeAlignment = 16;
46using CodeCacheBitmap = gc::accounting::MemoryRangeBitmap<kJitCodeAlignment>;
47
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080048class JitCodeCache {
49 public:
50 static constexpr size_t kMaxCapacity = 1 * GB;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010051 // Put the default to a very low amount for debug builds to stress the code cache
52 // collection.
53 static constexpr size_t kDefaultCapacity = kIsDebugBuild ? 20 * KB : 2 * MB;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080054
Mathieu Chartierbce416f2015-03-23 12:37:35 -070055 // Create the code cache with a code + data capacity equal to "capacity", error message is passed
56 // in the out arg error_msg.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080057 static JitCodeCache* Create(size_t capacity, std::string* error_msg);
58
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010059 // Number of bytes allocated in the code cache.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010060 size_t CodeCacheSize() REQUIRES(!lock_);
61
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010062 // Number of bytes allocated in the data cache.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010063 size_t DataCacheSize() REQUIRES(!lock_);
64
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010065 // Number of compiled code in the code cache. Note that this is not the number
66 // of methods that got JIT compiled, as we might have collected some.
67 size_t NumberOfCompiledCode() REQUIRES(!lock_);
68
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010069 // Allocate and write code and its metadata to the code cache.
70 uint8_t* CommitCode(Thread* self,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010071 ArtMethod* method,
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010072 const uint8_t* mapping_table,
73 const uint8_t* vmap_table,
74 const uint8_t* gc_map,
75 size_t frame_size_in_bytes,
76 size_t core_spill_mask,
77 size_t fp_spill_mask,
78 const uint8_t* code,
79 size_t code_size)
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010080 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010081 REQUIRES(!lock_);
82
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010083 // Return true if the code cache contains this pc.
84 bool ContainsPc(const void* pc) const;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080085
Nicolas Geoffraya5891e82015-11-06 14:18:27 +000086 // Return true if the code cache contains this method.
87 bool ContainsMethod(ArtMethod* method) REQUIRES(!lock_);
88
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010089 // Reserve a region of data of size at least "size". Returns null if there is no more room.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010090 uint8_t* ReserveData(Thread* self, size_t size)
91 SHARED_REQUIRES(Locks::mutator_lock_)
92 REQUIRES(!lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010093
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +000094 // Clear data from the data portion of the code cache.
95 void ClearData(Thread* self, void* data)
96 SHARED_REQUIRES(Locks::mutator_lock_)
97 REQUIRES(!lock_);
98
Mathieu Chartier2cebb242015-04-21 16:50:40 -070099 // Add a data array of size (end - begin) with the associated contents, returns null if there
Mathieu Chartierbce416f2015-03-23 12:37:35 -0700100 // is no more room.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800101 uint8_t* AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end)
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100102 SHARED_REQUIRES(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700103 REQUIRES(!lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800104
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100105 CodeCacheBitmap* GetLiveBitmap() const {
106 return live_bitmap_.get();
107 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800108
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100109 // Perform a collection on the code cache.
110 void GarbageCollectCache(Thread* self)
111 REQUIRES(!lock_)
112 SHARED_REQUIRES(Locks::mutator_lock_);
113
114 // Given the 'pc', try to find the JIT compiled code associated with it.
115 // Return null if 'pc' is not in the code cache. 'method' is passed for
116 // sanity check.
117 OatQuickMethodHeader* LookupMethodHeader(uintptr_t pc, ArtMethod* method)
118 REQUIRES(!lock_)
119 SHARED_REQUIRES(Locks::mutator_lock_);
120
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000121 // Remove all methods in our cache that were allocated by 'alloc'.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100122 void RemoveMethodsIn(Thread* self, const LinearAlloc& alloc)
123 REQUIRES(!lock_)
124 REQUIRES(Locks::classlinker_classes_lock_)
125 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800126
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000127 // Create a 'ProfileInfo' for 'method'. If 'retry_allocation' is true,
128 // will collect and retry if the first allocation is unsuccessful.
129 ProfilingInfo* AddProfilingInfo(Thread* self,
130 ArtMethod* method,
131 const std::vector<uint32_t>& entries,
132 bool retry_allocation)
133 REQUIRES(!lock_)
134 SHARED_REQUIRES(Locks::mutator_lock_);
135
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800136 private:
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100137 // Take ownership of code_mem_map.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100138 JitCodeCache(MemMap* code_map, MemMap* data_map);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800139
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100140 // Internal version of 'CommitCode' that will not retry if the
141 // allocation fails. Return null if the allocation fails.
142 uint8_t* CommitCodeInternal(Thread* self,
143 ArtMethod* method,
144 const uint8_t* mapping_table,
145 const uint8_t* vmap_table,
146 const uint8_t* gc_map,
147 size_t frame_size_in_bytes,
148 size_t core_spill_mask,
149 size_t fp_spill_mask,
150 const uint8_t* code,
151 size_t code_size)
152 REQUIRES(!lock_)
153 SHARED_REQUIRES(Locks::mutator_lock_);
154
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000155 ProfilingInfo* AddProfilingInfoInternal(Thread* self,
156 ArtMethod* method,
157 const std::vector<uint32_t>& entries)
158 REQUIRES(!lock_)
159 SHARED_REQUIRES(Locks::mutator_lock_);
160
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100161 // If a collection is in progress, wait for it to finish. Return
162 // whether the thread actually waited.
163 bool WaitForPotentialCollectionToComplete(Thread* self)
164 REQUIRES(lock_) REQUIRES(!Locks::mutator_lock_);
165
166 // Free in the mspace allocations taken by 'method'.
167 void FreeCode(const void* code_ptr, ArtMethod* method) REQUIRES(lock_);
168
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000169 // Number of bytes allocated in the code cache.
170 size_t CodeCacheSizeLocked() REQUIRES(lock_);
171
172 // Number of bytes allocated in the data cache.
173 size_t DataCacheSizeLocked() REQUIRES(lock_);
174
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100175 // Lock for guarding allocations, collections, and the method_code_map_.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800176 Mutex lock_;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100177 // Condition to wait on during collection.
178 ConditionVariable lock_cond_ GUARDED_BY(lock_);
179 // Whether there is a code cache collection in progress.
180 bool collection_in_progress_ GUARDED_BY(lock_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100181 // Mem map which holds code.
182 std::unique_ptr<MemMap> code_map_;
183 // Mem map which holds data (stack maps and profiling info).
184 std::unique_ptr<MemMap> data_map_;
185 // The opaque mspace for allocating code.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100186 void* code_mspace_ GUARDED_BY(lock_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100187 // The opaque mspace for allocating data.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100188 void* data_mspace_ GUARDED_BY(lock_);
189 // Bitmap for collecting code and data.
190 std::unique_ptr<CodeCacheBitmap> live_bitmap_;
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000191 // This map holds compiled code associated to the ArtMethod.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100192 SafeMap<const void*, ArtMethod*> method_code_map_ GUARDED_BY(lock_);
Nicolas Geoffray26705e22015-10-28 12:50:11 +0000193 // ProfilingInfo objects we have allocated.
194 std::vector<ProfilingInfo*> profiling_infos_ GUARDED_BY(lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800195
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700196 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCodeCache);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800197};
198
199
200} // namespace jit
201} // namespace art
202
203#endif // ART_RUNTIME_JIT_JIT_CODE_CACHE_H_