blob: 4c5316227c803b09adfe913760f12953e8b6856e [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080022#include "mem_map.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080023#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
Mathieu Chartiere401d142015-04-22 13:56:20 -070061bool JitCodeCache::ContainsMethod(ArtMethod* method) const {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080062 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
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010085uint8_t* JitCodeCache::ReserveData(Thread* self, size_t size) {
86 MutexLock mu(self, lock_);
87 size = RoundUp(size, sizeof(void*));
88 if (size > DataCacheRemain()) {
89 return nullptr;
90 }
91 data_cache_ptr_ += size;
92 return data_cache_ptr_ - size;
93}
94
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080095uint8_t* JitCodeCache::AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end) {
96 MutexLock mu(self, lock_);
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010097 const size_t size = RoundUp(end - begin, sizeof(void*));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080098 if (size > DataCacheRemain()) {
99 return nullptr; // Out of space in the data cache.
100 }
101 std::copy(begin, end, data_cache_ptr_);
102 data_cache_ptr_ += size;
103 return data_cache_ptr_ - size;
104}
105
Mathieu Chartiere401d142015-04-22 13:56:20 -0700106const void* JitCodeCache::GetCodeFor(ArtMethod* method) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800107 const void* code = method->GetEntryPointFromQuickCompiledCode();
108 if (ContainsCodePtr(code)) {
109 return code;
110 }
111 MutexLock mu(Thread::Current(), lock_);
112 auto it = method_code_map_.find(method);
113 if (it != method_code_map_.end()) {
114 return it->second;
115 }
116 return nullptr;
117}
118
Mathieu Chartiere401d142015-04-22 13:56:20 -0700119void JitCodeCache::SaveCompiledCode(ArtMethod* method, const void* old_code_ptr) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800120 DCHECK_EQ(method->GetEntryPointFromQuickCompiledCode(), old_code_ptr);
121 DCHECK(ContainsCodePtr(old_code_ptr)) << PrettyMethod(method) << " old_code_ptr="
122 << old_code_ptr;
123 MutexLock mu(Thread::Current(), lock_);
124 auto it = method_code_map_.find(method);
125 if (it != method_code_map_.end()) {
126 return;
127 }
128 method_code_map_.Put(method, old_code_ptr);
129}
130
131} // namespace jit
132} // namespace art