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" |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 25 | #include "gc/allocator/dlmalloc.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 26 | #include "gc_root.h" |
| 27 | #include "jni.h" |
| 28 | #include "oat_file.h" |
| 29 | #include "object_callbacks.h" |
| 30 | #include "safe_map.h" |
| 31 | #include "thread_pool.h" |
| 32 | |
| 33 | namespace art { |
| 34 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 35 | class ArtMethod; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 36 | class CompiledMethod; |
| 37 | class CompilerCallbacks; |
| 38 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 39 | namespace jit { |
| 40 | |
| 41 | class JitInstrumentationCache; |
| 42 | |
| 43 | class JitCodeCache { |
| 44 | public: |
| 45 | static constexpr size_t kMaxCapacity = 1 * GB; |
| 46 | static constexpr size_t kDefaultCapacity = 2 * MB; |
| 47 | |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 48 | // Create the code cache with a code + data capacity equal to "capacity", error message is passed |
| 49 | // in the out arg error_msg. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 50 | static JitCodeCache* Create(size_t capacity, std::string* error_msg); |
| 51 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 52 | size_t NumMethods() const { |
| 53 | return num_methods_; |
| 54 | } |
| 55 | |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 56 | size_t CodeCacheSize() REQUIRES(!lock_); |
| 57 | |
| 58 | size_t DataCacheSize() REQUIRES(!lock_); |
| 59 | |
| 60 | // Allocate and write code and its metadata to the code cache. |
| 61 | uint8_t* CommitCode(Thread* self, |
| 62 | const uint8_t* mapping_table, |
| 63 | const uint8_t* vmap_table, |
| 64 | const uint8_t* gc_map, |
| 65 | size_t frame_size_in_bytes, |
| 66 | size_t core_spill_mask, |
| 67 | size_t fp_spill_mask, |
| 68 | const uint8_t* code, |
| 69 | size_t code_size) |
| 70 | REQUIRES(!lock_); |
| 71 | |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 72 | // 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] | 73 | bool ContainsMethod(ArtMethod* method) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 74 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 75 | |
| 76 | // Return true if the code cache contains a code ptr. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 77 | bool ContainsCodePtr(const void* ptr) const; |
| 78 | |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 79 | // Reserve a region of data of size at least "size". Returns null if there is no more room. |
| 80 | uint8_t* ReserveData(Thread* self, size_t size) REQUIRES(!lock_); |
| 81 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 82 | // 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] | 83 | // is no more room. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 84 | 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] | 85 | REQUIRES(!lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 86 | |
| 87 | // 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] | 88 | const void* GetCodeFor(ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 89 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 90 | |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 91 | // Save the compiled code for a method so that GetCodeFor(method) will return old_code_ptr if the |
| 92 | // entrypoint isn't within the cache. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 93 | void SaveCompiledCode(ArtMethod* method, const void* old_code_ptr) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 94 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 95 | |
| 96 | private: |
| 97 | // Takes ownership of code_mem_map. |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 98 | JitCodeCache(MemMap* code_map, MemMap* data_map); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 99 | |
Mathieu Chartier | bce416f | 2015-03-23 12:37:35 -0700 | [diff] [blame] | 100 | // Lock which guards. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 101 | Mutex lock_; |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 102 | // Mem map which holds code. |
| 103 | std::unique_ptr<MemMap> code_map_; |
| 104 | // Mem map which holds data (stack maps and profiling info). |
| 105 | std::unique_ptr<MemMap> data_map_; |
| 106 | // The opaque mspace for allocating code. |
| 107 | void* code_mspace_; |
| 108 | // The opaque mspace for allocating data. |
| 109 | void* data_mspace_; |
| 110 | // Number of compiled methods. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 111 | size_t num_methods_; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 112 | // This map holds code for methods if they were deoptimized by the instrumentation stubs. This is |
| 113 | // required since we have to implement ClassLinker::GetQuickOatCodeFor for walking stacks. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 114 | SafeMap<ArtMethod*, const void*> method_code_map_ GUARDED_BY(lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 115 | |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 116 | DISALLOW_IMPLICIT_CONSTRUCTORS(JitCodeCache); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | |
| 120 | } // namespace jit |
| 121 | } // namespace art |
| 122 | |
| 123 | #endif // ART_RUNTIME_JIT_JIT_CODE_CACHE_H_ |