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 | #include "jit_code_cache.h" |
| 18 | |
| 19 | #include <sstream> |
| 20 | |
| 21 | #include "mem_map.h" |
| 22 | #include "mirror/art_method-inl.h" |
| 23 | #include "oat_file-inl.h" |
| 24 | |
| 25 | namespace art { |
| 26 | namespace jit { |
| 27 | |
| 28 | JitCodeCache* JitCodeCache::Create(size_t capacity, std::string* error_msg) { |
| 29 | CHECK_GT(capacity, 0U); |
| 30 | CHECK_LT(capacity, kMaxCapacity); |
| 31 | std::string error_str; |
| 32 | // Map name specific for android_os_Debug.cpp accounting. |
| 33 | MemMap* map = MemMap::MapAnonymous("jit-code-cache", nullptr, capacity, |
| 34 | PROT_READ | PROT_WRITE | PROT_EXEC, false, &error_str); |
| 35 | if (map == nullptr) { |
| 36 | std::ostringstream oss; |
| 37 | oss << "Failed to create read write execute cache: " << error_str << " size=" << capacity; |
| 38 | *error_msg = oss.str(); |
| 39 | return nullptr; |
| 40 | } |
| 41 | return new JitCodeCache(map); |
| 42 | } |
| 43 | |
| 44 | JitCodeCache::JitCodeCache(MemMap* mem_map) |
| 45 | : lock_("Jit code cache", kJitCodeCacheLock), num_methods_(0) { |
| 46 | VLOG(jit) << "Created jit code cache size=" << PrettySize(mem_map->Size()); |
| 47 | mem_map_.reset(mem_map); |
| 48 | uint8_t* divider = mem_map->Begin() + RoundUp(mem_map->Size() / 4, kPageSize); |
| 49 | // Data cache is 1 / 4 of the map. TODO: Make this variable? |
| 50 | // Put data at the start. |
| 51 | data_cache_ptr_ = mem_map->Begin(); |
| 52 | data_cache_end_ = divider; |
| 53 | data_cache_begin_ = data_cache_ptr_; |
| 54 | mprotect(data_cache_ptr_, data_cache_end_ - data_cache_begin_, PROT_READ | PROT_WRITE); |
| 55 | // Code cache after. |
| 56 | code_cache_begin_ = divider; |
| 57 | code_cache_ptr_ = divider; |
| 58 | code_cache_end_ = mem_map->End(); |
| 59 | } |
| 60 | |
| 61 | bool JitCodeCache::ContainsMethod(mirror::ArtMethod* method) const { |
| 62 | return ContainsCodePtr(method->GetEntryPointFromQuickCompiledCode()); |
| 63 | } |
| 64 | |
| 65 | bool JitCodeCache::ContainsCodePtr(const void* ptr) const { |
| 66 | return ptr >= code_cache_begin_ && ptr < code_cache_end_; |
| 67 | } |
| 68 | |
| 69 | void JitCodeCache::FlushInstructionCache() { |
| 70 | UNIMPLEMENTED(FATAL); |
| 71 | // TODO: Investigate if we need to do this. |
| 72 | // __clear_cache(reinterpret_cast<char*>(code_cache_begin_), static_cast<int>(CodeCacheSize())); |
| 73 | } |
| 74 | |
| 75 | uint8_t* JitCodeCache::ReserveCode(Thread* self, size_t size) { |
| 76 | MutexLock mu(self, lock_); |
| 77 | if (size > CodeCacheRemain()) { |
| 78 | return nullptr; |
| 79 | } |
| 80 | code_cache_ptr_ += size; |
| 81 | return code_cache_ptr_ - size; |
| 82 | } |
| 83 | |
| 84 | uint8_t* JitCodeCache::AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end) { |
| 85 | MutexLock mu(self, lock_); |
| 86 | const size_t size = end - begin; |
| 87 | if (size > DataCacheRemain()) { |
| 88 | return nullptr; // Out of space in the data cache. |
| 89 | } |
| 90 | std::copy(begin, end, data_cache_ptr_); |
| 91 | data_cache_ptr_ += size; |
| 92 | return data_cache_ptr_ - size; |
| 93 | } |
| 94 | |
| 95 | const void* JitCodeCache::GetCodeFor(mirror::ArtMethod* method) { |
| 96 | const void* code = method->GetEntryPointFromQuickCompiledCode(); |
| 97 | if (ContainsCodePtr(code)) { |
| 98 | return code; |
| 99 | } |
| 100 | MutexLock mu(Thread::Current(), lock_); |
| 101 | auto it = method_code_map_.find(method); |
| 102 | if (it != method_code_map_.end()) { |
| 103 | return it->second; |
| 104 | } |
| 105 | return nullptr; |
| 106 | } |
| 107 | |
| 108 | void JitCodeCache::SaveCompiledCode(mirror::ArtMethod* method, const void* old_code_ptr) { |
| 109 | DCHECK_EQ(method->GetEntryPointFromQuickCompiledCode(), old_code_ptr); |
| 110 | DCHECK(ContainsCodePtr(old_code_ptr)) << PrettyMethod(method) << " old_code_ptr=" |
| 111 | << old_code_ptr; |
| 112 | MutexLock mu(Thread::Current(), lock_); |
| 113 | auto it = method_code_map_.find(method); |
| 114 | if (it != method_code_map_.end()) { |
| 115 | return; |
| 116 | } |
| 117 | method_code_map_.Put(method, old_code_ptr); |
| 118 | } |
| 119 | |
| 120 | } // namespace jit |
| 121 | } // namespace art |