1 /*
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 
32 namespace art {
33 
34 class ArtMethod;
35 class CompiledMethod;
36 class CompilerCallbacks;
37 
38 namespace jit {
39 
40 class JitInstrumentationCache;
41 
42 class JitCodeCache {
43  public:
44   static constexpr size_t kMaxCapacity = 1 * GB;
45   static constexpr size_t kDefaultCapacity = 2 * MB;
46 
47   // Create the code cache with a code + data capacity equal to "capacity", error message is passed
48   // in the out arg error_msg.
49   static JitCodeCache* Create(size_t capacity, std::string* error_msg);
50 
CodeCachePtr()51   const uint8_t* CodeCachePtr() const {
52     return code_cache_ptr_;
53   }
54 
CodeCacheSize()55   size_t CodeCacheSize() const {
56     return code_cache_ptr_ - code_cache_begin_;
57   }
58 
CodeCacheRemain()59   size_t CodeCacheRemain() const {
60     return code_cache_end_ - code_cache_ptr_;
61   }
62 
DataCachePtr()63   const uint8_t* DataCachePtr() const {
64     return data_cache_ptr_;
65   }
66 
DataCacheSize()67   size_t DataCacheSize() const {
68     return data_cache_ptr_ - data_cache_begin_;
69   }
70 
DataCacheRemain()71   size_t DataCacheRemain() const {
72     return data_cache_end_ - data_cache_ptr_;
73   }
74 
NumMethods()75   size_t NumMethods() const {
76     return num_methods_;
77   }
78 
79   // Return true if the code cache contains the code pointer which si the entrypoint of the method.
80   bool ContainsMethod(ArtMethod* method) const
81       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
82 
83   // Return true if the code cache contains a code ptr.
84   bool ContainsCodePtr(const void* ptr) const;
85 
86   // Reserve a region of code of size at least "size". Returns null if there is no more room.
87   uint8_t* ReserveCode(Thread* self, size_t size) LOCKS_EXCLUDED(lock_);
88 
89   // Add a data array of size (end - begin) with the associated contents, returns null if there
90   // is no more room.
91   uint8_t* AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end)
92       LOCKS_EXCLUDED(lock_);
93 
94   // Get code for a method, returns null if it is not in the jit cache.
95   const void* GetCodeFor(ArtMethod* method)
96       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
97 
98   // Save the compiled code for a method so that GetCodeFor(method) will return old_code_ptr if the
99   // entrypoint isn't within the cache.
100   void SaveCompiledCode(ArtMethod* method, const void* old_code_ptr)
101       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_);
102 
103  private:
104   // Takes ownership of code_mem_map.
105   explicit JitCodeCache(MemMap* code_mem_map);
106 
107   // Unimplemented, TODO: Determine if it is necessary.
108   void FlushInstructionCache();
109 
110   // Lock which guards.
111   Mutex lock_;
112   // Mem map which holds code and data. We do this since we need to have 32 bit offsets from method
113   // headers in code cache which point to things in the data cache. If the maps are more than 4GB
114   // apart, having multiple maps wouldn't work.
115   std::unique_ptr<MemMap> mem_map_;
116   // Code cache section.
117   uint8_t* code_cache_ptr_;
118   const uint8_t* code_cache_begin_;
119   const uint8_t* code_cache_end_;
120   // Data cache section.
121   uint8_t* data_cache_ptr_;
122   const uint8_t* data_cache_begin_;
123   const uint8_t* data_cache_end_;
124   size_t num_methods_;
125   // This map holds code for methods if they were deoptimized by the instrumentation stubs. This is
126   // required since we have to implement ClassLinker::GetQuickOatCodeFor for walking stacks.
127   SafeMap<ArtMethod*, const void*> method_code_map_ GUARDED_BY(lock_);
128 
129   DISALLOW_IMPLICIT_CONSTRUCTORS(JitCodeCache);
130 };
131 
132 
133 }  // namespace jit
134 }  // namespace art
135 
136 #endif  // ART_RUNTIME_JIT_JIT_CODE_CACHE_H_
137