summaryrefslogtreecommitdiff
path: root/runtime/jit/jit_code_cache.cc
blob: 4c5316227c803b09adfe913760f12953e8b6856e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "jit_code_cache.h"

#include <sstream>

#include "art_method-inl.h"
#include "mem_map.h"
#include "oat_file-inl.h"

namespace art {
namespace jit {

JitCodeCache* JitCodeCache::Create(size_t capacity, std::string* error_msg) {
  CHECK_GT(capacity, 0U);
  CHECK_LT(capacity, kMaxCapacity);
  std::string error_str;
  // Map name specific for android_os_Debug.cpp accounting.
  MemMap* map = MemMap::MapAnonymous("jit-code-cache", nullptr, capacity,
                                     PROT_READ | PROT_WRITE | PROT_EXEC, false, false, &error_str);
  if (map == nullptr) {
    std::ostringstream oss;
    oss << "Failed to create read write execute cache: " << error_str << " size=" << capacity;
    *error_msg = oss.str();
    return nullptr;
  }
  return new JitCodeCache(map);
}

JitCodeCache::JitCodeCache(MemMap* mem_map)
    : lock_("Jit code cache", kJitCodeCacheLock), num_methods_(0) {
  VLOG(jit) << "Created jit code cache size=" << PrettySize(mem_map->Size());
  mem_map_.reset(mem_map);
  uint8_t* divider = mem_map->Begin() + RoundUp(mem_map->Size() / 4, kPageSize);
  // Data cache is 1 / 4 of the map. TODO: Make this variable?
  // Put data at the start.
  data_cache_ptr_ = mem_map->Begin();
  data_cache_end_ = divider;
  data_cache_begin_ = data_cache_ptr_;
  mprotect(data_cache_ptr_, data_cache_end_ - data_cache_begin_, PROT_READ | PROT_WRITE);
  // Code cache after.
  code_cache_begin_ = divider;
  code_cache_ptr_ = divider;
  code_cache_end_ = mem_map->End();
}

bool JitCodeCache::ContainsMethod(ArtMethod* method) const {
  return ContainsCodePtr(method->GetEntryPointFromQuickCompiledCode());
}

bool JitCodeCache::ContainsCodePtr(const void* ptr) const {
  return ptr >= code_cache_begin_ && ptr < code_cache_end_;
}

void JitCodeCache::FlushInstructionCache() {
  UNIMPLEMENTED(FATAL);
  // TODO: Investigate if we need to do this.
  // __clear_cache(reinterpret_cast<char*>(code_cache_begin_), static_cast<int>(CodeCacheSize()));
}

uint8_t* JitCodeCache::ReserveCode(Thread* self, size_t size) {
  MutexLock mu(self, lock_);
  if (size > CodeCacheRemain()) {
    return nullptr;
  }
  ++num_methods_;  // TODO: This is hacky but works since each method has exactly one code region.
  code_cache_ptr_ += size;
  return code_cache_ptr_ - size;
}

uint8_t* JitCodeCache::ReserveData(Thread* self, size_t size) {
  MutexLock mu(self, lock_);
  size = RoundUp(size, sizeof(void*));
  if (size > DataCacheRemain()) {
    return nullptr;
  }
  data_cache_ptr_ += size;
  return data_cache_ptr_ - size;
}

uint8_t* JitCodeCache::AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end) {
  MutexLock mu(self, lock_);
  const size_t size = RoundUp(end - begin, sizeof(void*));
  if (size > DataCacheRemain()) {
    return nullptr;  // Out of space in the data cache.
  }
  std::copy(begin, end, data_cache_ptr_);
  data_cache_ptr_ += size;
  return data_cache_ptr_ - size;
}

const void* JitCodeCache::GetCodeFor(ArtMethod* method) {
  const void* code = method->GetEntryPointFromQuickCompiledCode();
  if (ContainsCodePtr(code)) {
    return code;
  }
  MutexLock mu(Thread::Current(), lock_);
  auto it = method_code_map_.find(method);
  if (it != method_code_map_.end()) {
    return it->second;
  }
  return nullptr;
}

void JitCodeCache::SaveCompiledCode(ArtMethod* method, const void* old_code_ptr) {
  DCHECK_EQ(method->GetEntryPointFromQuickCompiledCode(), old_code_ptr);
  DCHECK(ContainsCodePtr(old_code_ptr)) << PrettyMethod(method) << " old_code_ptr="
      << old_code_ptr;
  MutexLock mu(Thread::Current(), lock_);
  auto it = method_code_map_.find(method);
  if (it != method_code_map_.end()) {
    return;
  }
  method_code_map_.Put(method, old_code_ptr);
}

}  // namespace jit
}  // namespace art