blob: 4e415b84037037dce75d88405e55f0445ba324a6 [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;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080038
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080039namespace jit {
40
41class JitInstrumentationCache;
42
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010043// Alignment that will suit all architectures.
44static constexpr int kJitCodeAlignment = 16;
45using CodeCacheBitmap = gc::accounting::MemoryRangeBitmap<kJitCodeAlignment>;
46
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080047class JitCodeCache {
48 public:
49 static constexpr size_t kMaxCapacity = 1 * GB;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010050 // Put the default to a very low amount for debug builds to stress the code cache
51 // collection.
52 static constexpr size_t kDefaultCapacity = kIsDebugBuild ? 20 * KB : 2 * MB;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080053
Mathieu Chartierbce416f2015-03-23 12:37:35 -070054 // Create the code cache with a code + data capacity equal to "capacity", error message is passed
55 // in the out arg error_msg.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080056 static JitCodeCache* Create(size_t capacity, std::string* error_msg);
57
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010058 // Number of bytes allocated in the code cache.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010059 size_t CodeCacheSize() REQUIRES(!lock_);
60
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010061 // Number of bytes allocated in the data cache.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010062 size_t DataCacheSize() REQUIRES(!lock_);
63
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010064 // Number of compiled code in the code cache. Note that this is not the number
65 // of methods that got JIT compiled, as we might have collected some.
66 size_t NumberOfCompiledCode() REQUIRES(!lock_);
67
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010068 // Allocate and write code and its metadata to the code cache.
69 uint8_t* CommitCode(Thread* self,
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010070 ArtMethod* method,
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010071 const uint8_t* mapping_table,
72 const uint8_t* vmap_table,
73 const uint8_t* gc_map,
74 size_t frame_size_in_bytes,
75 size_t core_spill_mask,
76 size_t fp_spill_mask,
77 const uint8_t* code,
78 size_t code_size)
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010079 SHARED_REQUIRES(Locks::mutator_lock_)
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010080 REQUIRES(!lock_);
81
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010082 // Return true if the code cache contains this pc.
83 bool ContainsPc(const void* pc) const;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080084
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010085 // 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 +010086 uint8_t* ReserveData(Thread* self, size_t size)
87 SHARED_REQUIRES(Locks::mutator_lock_)
88 REQUIRES(!lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010089
Mathieu Chartier2cebb242015-04-21 16:50:40 -070090 // Add a data array of size (end - begin) with the associated contents, returns null if there
Mathieu Chartierbce416f2015-03-23 12:37:35 -070091 // is no more room.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080092 uint8_t* AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end)
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010093 SHARED_REQUIRES(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070094 REQUIRES(!lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080095
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +010096 CodeCacheBitmap* GetLiveBitmap() const {
97 return live_bitmap_.get();
98 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080099
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100100 // Perform a collection on the code cache.
101 void GarbageCollectCache(Thread* self)
102 REQUIRES(!lock_)
103 SHARED_REQUIRES(Locks::mutator_lock_);
104
105 // Given the 'pc', try to find the JIT compiled code associated with it.
106 // Return null if 'pc' is not in the code cache. 'method' is passed for
107 // sanity check.
108 OatQuickMethodHeader* LookupMethodHeader(uintptr_t pc, ArtMethod* method)
109 REQUIRES(!lock_)
110 SHARED_REQUIRES(Locks::mutator_lock_);
111
112 void RemoveMethodsIn(Thread* self, const LinearAlloc& alloc)
113 REQUIRES(!lock_)
114 REQUIRES(Locks::classlinker_classes_lock_)
115 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800116
117 private:
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100118 // Take ownership of code_mem_map.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100119 JitCodeCache(MemMap* code_map, MemMap* data_map);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800120
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100121 // Internal version of 'CommitCode' that will not retry if the
122 // allocation fails. Return null if the allocation fails.
123 uint8_t* CommitCodeInternal(Thread* self,
124 ArtMethod* method,
125 const uint8_t* mapping_table,
126 const uint8_t* vmap_table,
127 const uint8_t* gc_map,
128 size_t frame_size_in_bytes,
129 size_t core_spill_mask,
130 size_t fp_spill_mask,
131 const uint8_t* code,
132 size_t code_size)
133 REQUIRES(!lock_)
134 SHARED_REQUIRES(Locks::mutator_lock_);
135
136 // If a collection is in progress, wait for it to finish. Return
137 // whether the thread actually waited.
138 bool WaitForPotentialCollectionToComplete(Thread* self)
139 REQUIRES(lock_) REQUIRES(!Locks::mutator_lock_);
140
141 // Free in the mspace allocations taken by 'method'.
142 void FreeCode(const void* code_ptr, ArtMethod* method) REQUIRES(lock_);
143
144 // Lock for guarding allocations, collections, and the method_code_map_.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800145 Mutex lock_;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100146 // Condition to wait on during collection.
147 ConditionVariable lock_cond_ GUARDED_BY(lock_);
148 // Whether there is a code cache collection in progress.
149 bool collection_in_progress_ GUARDED_BY(lock_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100150 // Mem map which holds code.
151 std::unique_ptr<MemMap> code_map_;
152 // Mem map which holds data (stack maps and profiling info).
153 std::unique_ptr<MemMap> data_map_;
154 // The opaque mspace for allocating code.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100155 void* code_mspace_ GUARDED_BY(lock_);
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100156 // The opaque mspace for allocating data.
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100157 void* data_mspace_ GUARDED_BY(lock_);
158 // Bitmap for collecting code and data.
159 std::unique_ptr<CodeCacheBitmap> live_bitmap_;
160 // This map holds compiled code associated to the ArtMethod
161 SafeMap<const void*, ArtMethod*> method_code_map_ GUARDED_BY(lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800162
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700163 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCodeCache);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800164};
165
166
167} // namespace jit
168} // namespace art
169
170#endif // ART_RUNTIME_JIT_JIT_CODE_CACHE_H_