blob: 4d367e01eb3a6d7a59210e5e74f4b962f86bfb9d [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#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
25namespace art {
26namespace jit {
27
28JitCodeCache* 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,
Vladimir Marko5c42c292015-02-25 12:02:49 +000034 PROT_READ | PROT_WRITE | PROT_EXEC, false, false, &error_str);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080035 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
44JitCodeCache::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
61bool JitCodeCache::ContainsMethod(mirror::ArtMethod* method) const {
62 return ContainsCodePtr(method->GetEntryPointFromQuickCompiledCode());
63}
64
65bool JitCodeCache::ContainsCodePtr(const void* ptr) const {
66 return ptr >= code_cache_begin_ && ptr < code_cache_end_;
67}
68
69void 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
75uint8_t* JitCodeCache::ReserveCode(Thread* self, size_t size) {
76 MutexLock mu(self, lock_);
77 if (size > CodeCacheRemain()) {
78 return nullptr;
79 }
Mathieu Chartiera4885cb2015-03-09 15:38:54 -070080 ++num_methods_; // TODO: This is hacky but works since each method has exactly one code region.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080081 code_cache_ptr_ += size;
82 return code_cache_ptr_ - size;
83}
84
85uint8_t* JitCodeCache::AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end) {
86 MutexLock mu(self, lock_);
87 const size_t size = end - begin;
88 if (size > DataCacheRemain()) {
89 return nullptr; // Out of space in the data cache.
90 }
91 std::copy(begin, end, data_cache_ptr_);
92 data_cache_ptr_ += size;
93 return data_cache_ptr_ - size;
94}
95
96const void* JitCodeCache::GetCodeFor(mirror::ArtMethod* method) {
97 const void* code = method->GetEntryPointFromQuickCompiledCode();
98 if (ContainsCodePtr(code)) {
99 return code;
100 }
101 MutexLock mu(Thread::Current(), lock_);
102 auto it = method_code_map_.find(method);
103 if (it != method_code_map_.end()) {
104 return it->second;
105 }
106 return nullptr;
107}
108
109void JitCodeCache::SaveCompiledCode(mirror::ArtMethod* method, const void* old_code_ptr) {
110 DCHECK_EQ(method->GetEntryPointFromQuickCompiledCode(), old_code_ptr);
111 DCHECK(ContainsCodePtr(old_code_ptr)) << PrettyMethod(method) << " old_code_ptr="
112 << old_code_ptr;
113 MutexLock mu(Thread::Current(), lock_);
114 auto it = method_code_map_.find(method);
115 if (it != method_code_map_.end()) {
116 return;
117 }
118 method_code_map_.Put(method, old_code_ptr);
119}
120
121} // namespace jit
122} // namespace art