Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 34 | class ArtMethod; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 35 | class CompiledMethod; |
| 36 | class CompilerCallbacks; |
| 37 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 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 | |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 47 | // Create the code cache with a code + data capacity equal to "capacity", error message is passed |
| 48 | // in the out arg error_msg. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 49 | static JitCodeCache* Create(size_t capacity, std::string* error_msg); |
| 50 | |
| 51 | const uint8_t* CodeCachePtr() const { |
| 52 | return code_cache_ptr_; |
| 53 | } |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 54 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 55 | size_t CodeCacheSize() const { |
| 56 | return code_cache_ptr_ - code_cache_begin_; |
| 57 | } |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 58 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 59 | size_t CodeCacheRemain() const { |
| 60 | return code_cache_end_ - code_cache_ptr_; |
| 61 | } |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 62 | |
| 63 | const uint8_t* DataCachePtr() const { |
| 64 | return data_cache_ptr_; |
| 65 | } |
| 66 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 67 | size_t DataCacheSize() const { |
| 68 | return data_cache_ptr_ - data_cache_begin_; |
| 69 | } |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 70 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 71 | size_t DataCacheRemain() const { |
| 72 | return data_cache_end_ - data_cache_ptr_; |
| 73 | } |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 74 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 75 | size_t NumMethods() const { |
| 76 | return num_methods_; |
| 77 | } |
| 78 | |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 79 | // Return true if the code cache contains the code pointer which si the entrypoint of the method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 80 | bool ContainsMethod(ArtMethod* method) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 81 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 82 | |
| 83 | // Return true if the code cache contains a code ptr. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 84 | bool ContainsCodePtr(const void* ptr) const; |
| 85 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 86 | // Reserve a region of code of size at least "size". Returns null if there is no more room. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 87 | uint8_t* ReserveCode(Thread* self, size_t size) REQUIRES(!lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 88 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 89 | // 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 Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 92 | // Add a data array of size (end - begin) with the associated contents, returns null if there |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 93 | // is no more room. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 94 | uint8_t* AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 95 | REQUIRES(!lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 96 | |
| 97 | // Get code for a method, returns null if it is not in the jit cache. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 98 | const void* GetCodeFor(ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 99 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 100 | |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 101 | // 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 Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 103 | void SaveCompiledCode(ArtMethod* method, const void* old_code_ptr) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 104 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 105 | |
| 106 | private: |
| 107 | // Takes ownership of code_mem_map. |
| 108 | explicit JitCodeCache(MemMap* code_mem_map); |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 109 | |
| 110 | // Unimplemented, TODO: Determine if it is necessary. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 111 | void FlushInstructionCache(); |
| 112 | |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 113 | // Lock which guards. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 114 | 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 Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 128 | // 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 Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 130 | SafeMap<ArtMethod*, const void*> method_code_map_ GUARDED_BY(lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 131 | |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 132 | DISALLOW_IMPLICIT_CONSTRUCTORS(JitCodeCache); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | |
| 136 | } // namespace jit |
| 137 | } // namespace art |
| 138 | |
| 139 | #endif // ART_RUNTIME_JIT_JIT_CODE_CACHE_H_ |