blob: 8b76647161113f015028af761d753e6343a0724a [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
34class CompiledMethod;
35class CompilerCallbacks;
36
37namespace mirror {
38class ArtMethod;
39} // namespcae mirror
40
41namespace jit {
42
43class JitInstrumentationCache;
44
45class JitCodeCache {
46 public:
47 static constexpr size_t kMaxCapacity = 1 * GB;
48 static constexpr size_t kDefaultCapacity = 2 * MB;
49
Mathieu Chartierbce416f2015-03-23 12:37:35 -070050 // Create the code cache with a code + data capacity equal to "capacity", error message is passed
51 // in the out arg error_msg.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080052 static JitCodeCache* Create(size_t capacity, std::string* error_msg);
53
54 const uint8_t* CodeCachePtr() const {
55 return code_cache_ptr_;
56 }
Mathieu Chartierbce416f2015-03-23 12:37:35 -070057
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080058 size_t CodeCacheSize() const {
59 return code_cache_ptr_ - code_cache_begin_;
60 }
Mathieu Chartierbce416f2015-03-23 12:37:35 -070061
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080062 size_t CodeCacheRemain() const {
63 return code_cache_end_ - code_cache_ptr_;
64 }
Mathieu Chartierbce416f2015-03-23 12:37:35 -070065
66 const uint8_t* DataCachePtr() const {
67 return data_cache_ptr_;
68 }
69
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080070 size_t DataCacheSize() const {
71 return data_cache_ptr_ - data_cache_begin_;
72 }
Mathieu Chartierbce416f2015-03-23 12:37:35 -070073
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080074 size_t DataCacheRemain() const {
75 return data_cache_end_ - data_cache_ptr_;
76 }
Mathieu Chartierbce416f2015-03-23 12:37:35 -070077
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080078 size_t NumMethods() const {
79 return num_methods_;
80 }
81
Mathieu Chartierbce416f2015-03-23 12:37:35 -070082 // Return true if the code cache contains the code pointer which si the entrypoint of the method.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080083 bool ContainsMethod(mirror::ArtMethod* method) const
84 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartierbce416f2015-03-23 12:37:35 -070085
86 // Return true if the code cache contains a code ptr.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080087 bool ContainsCodePtr(const void* ptr) const;
88
Mathieu Chartier2cebb242015-04-21 16:50:40 -070089 // Reserve a region of code of size at least "size". Returns null if there is no more room.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080090 uint8_t* ReserveCode(Thread* self, size_t size) LOCKS_EXCLUDED(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)
95 LOCKS_EXCLUDED(lock_);
96
97 // Get code for a method, returns null if it is not in the jit cache.
98 const void* GetCodeFor(mirror::ArtMethod* method)
99 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
100
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 Chartiere5f13e52015-02-24 09:37:21 -0800103 void SaveCompiledCode(mirror::ArtMethod* method, const void* old_code_ptr)
104 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
105
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_;
128 // TODO: This relies on methods not moving.
129 // This map holds code for methods if they were deoptimized by the instrumentation stubs. This is
130 // required since we have to implement ClassLinker::GetQuickOatCodeFor for walking stacks.
Mathieu Chartierbce416f2015-03-23 12:37:35 -0700131 SafeMap<mirror::ArtMethod*, const void*> method_code_map_ GUARDED_BY(lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800132
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700133 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCodeCache);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800134};
135
136
137} // namespace jit
138} // namespace art
139
140#endif // ART_RUNTIME_JIT_JIT_CODE_CACHE_H_