blob: f485e4aded9a7bb0141e8d1bb196f41e92a215a0 [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"
25#include "gc_root.h"
26#include "jni.h"
27#include "oat_file.h"
28#include "object_callbacks.h"
29#include "safe_map.h"
30#include "thread_pool.h"
31
32namespace art {
33
Mathieu Chartiere401d142015-04-22 13:56:20 -070034class ArtMethod;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080035class CompiledMethod;
36class CompilerCallbacks;
37
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080038namespace jit {
39
40class JitInstrumentationCache;
41
42class JitCodeCache {
43 public:
44 static constexpr size_t kMaxCapacity = 1 * GB;
45 static constexpr size_t kDefaultCapacity = 2 * MB;
46
Mathieu Chartierbce416f2015-03-23 12:37:35 -070047 // Create the code cache with a code + data capacity equal to "capacity", error message is passed
48 // in the out arg error_msg.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080049 static JitCodeCache* Create(size_t capacity, std::string* error_msg);
50
51 const uint8_t* CodeCachePtr() const {
52 return code_cache_ptr_;
53 }
Mathieu Chartierbce416f2015-03-23 12:37:35 -070054
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080055 size_t CodeCacheSize() const {
56 return code_cache_ptr_ - code_cache_begin_;
57 }
Mathieu Chartierbce416f2015-03-23 12:37:35 -070058
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080059 size_t CodeCacheRemain() const {
60 return code_cache_end_ - code_cache_ptr_;
61 }
Mathieu Chartierbce416f2015-03-23 12:37:35 -070062
63 const uint8_t* DataCachePtr() const {
64 return data_cache_ptr_;
65 }
66
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080067 size_t DataCacheSize() const {
68 return data_cache_ptr_ - data_cache_begin_;
69 }
Mathieu Chartierbce416f2015-03-23 12:37:35 -070070
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080071 size_t DataCacheRemain() const {
72 return data_cache_end_ - data_cache_ptr_;
73 }
Mathieu Chartierbce416f2015-03-23 12:37:35 -070074
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080075 size_t NumMethods() const {
76 return num_methods_;
77 }
78
Mathieu Chartierbce416f2015-03-23 12:37:35 -070079 // Return true if the code cache contains the code pointer which si the entrypoint of the method.
Mathieu Chartiere401d142015-04-22 13:56:20 -070080 bool ContainsMethod(ArtMethod* method) const
Mathieu Chartier90443472015-07-16 20:32:27 -070081 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierbce416f2015-03-23 12:37:35 -070082
83 // Return true if the code cache contains a code ptr.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080084 bool ContainsCodePtr(const void* ptr) const;
85
Mathieu Chartier2cebb242015-04-21 16:50:40 -070086 // Reserve a region of code of size at least "size". Returns null if there is no more room.
Mathieu Chartier90443472015-07-16 20:32:27 -070087 uint8_t* ReserveCode(Thread* self, size_t size) REQUIRES(!lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080088
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.
90 uint8_t* ReserveData(Thread* self, size_t size) REQUIRES(!lock_);
91
Mathieu Chartier2cebb242015-04-21 16:50:40 -070092 // Add a data array of size (end - begin) with the associated contents, returns null if there
Mathieu Chartierbce416f2015-03-23 12:37:35 -070093 // is no more room.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080094 uint8_t* AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end)
Mathieu Chartier90443472015-07-16 20:32:27 -070095 REQUIRES(!lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080096
97 // Get code for a method, returns null if it is not in the jit cache.
Mathieu Chartiere401d142015-04-22 13:56:20 -070098 const void* GetCodeFor(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -070099 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800100
Mathieu Chartierbce416f2015-03-23 12:37:35 -0700101 // Save the compiled code for a method so that GetCodeFor(method) will return old_code_ptr if the
102 // entrypoint isn't within the cache.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700103 void SaveCompiledCode(ArtMethod* method, const void* old_code_ptr)
Mathieu Chartier90443472015-07-16 20:32:27 -0700104 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800105
106 private:
107 // Takes ownership of code_mem_map.
108 explicit JitCodeCache(MemMap* code_mem_map);
Mathieu Chartierbce416f2015-03-23 12:37:35 -0700109
110 // Unimplemented, TODO: Determine if it is necessary.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800111 void FlushInstructionCache();
112
Mathieu Chartierbce416f2015-03-23 12:37:35 -0700113 // Lock which guards.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800114 Mutex lock_;
115 // Mem map which holds code and data. We do this since we need to have 32 bit offsets from method
116 // headers in code cache which point to things in the data cache. If the maps are more than 4GB
117 // apart, having multiple maps wouldn't work.
118 std::unique_ptr<MemMap> mem_map_;
119 // Code cache section.
120 uint8_t* code_cache_ptr_;
121 const uint8_t* code_cache_begin_;
122 const uint8_t* code_cache_end_;
123 // Data cache section.
124 uint8_t* data_cache_ptr_;
125 const uint8_t* data_cache_begin_;
126 const uint8_t* data_cache_end_;
127 size_t num_methods_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800128 // This map holds code for methods if they were deoptimized by the instrumentation stubs. This is
129 // required since we have to implement ClassLinker::GetQuickOatCodeFor for walking stacks.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700130 SafeMap<ArtMethod*, const void*> method_code_map_ GUARDED_BY(lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800131
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700132 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCodeCache);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800133};
134
135
136} // namespace jit
137} // namespace art
138
139#endif // ART_RUNTIME_JIT_JIT_CODE_CACHE_H_