blob: fa90c1806f505ba8cf316e23b2e7ae647f1ee616 [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#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 Geoffray0c3c2662015-10-15 13:53:04 +010025#include "gc/allocator/dlmalloc.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080026#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
33namespace art {
34
Mathieu Chartiere401d142015-04-22 13:56:20 -070035class ArtMethod;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080036class CompiledMethod;
37class CompilerCallbacks;
38
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080039namespace jit {
40
41class JitInstrumentationCache;
42
43class JitCodeCache {
44 public:
45 static constexpr size_t kMaxCapacity = 1 * GB;
46 static constexpr size_t kDefaultCapacity = 2 * MB;
47
Mathieu Chartierbce416f2015-03-23 12:37:35 -070048 // Create the code cache with a code + data capacity equal to "capacity", error message is passed
49 // in the out arg error_msg.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080050 static JitCodeCache* Create(size_t capacity, std::string* error_msg);
51
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080052 size_t NumMethods() const {
53 return num_methods_;
54 }
55
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010056 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 Chartierbce416f2015-03-23 12:37:35 -070072 // Return true if the code cache contains the code pointer which si the entrypoint of the method.
Mathieu Chartiere401d142015-04-22 13:56:20 -070073 bool ContainsMethod(ArtMethod* method) const
Mathieu Chartier90443472015-07-16 20:32:27 -070074 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierbce416f2015-03-23 12:37:35 -070075
76 // Return true if the code cache contains a code ptr.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080077 bool ContainsCodePtr(const void* ptr) const;
78
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010079 // 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 Chartier2cebb242015-04-21 16:50:40 -070082 // Add a data array of size (end - begin) with the associated contents, returns null if there
Mathieu Chartierbce416f2015-03-23 12:37:35 -070083 // is no more room.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080084 uint8_t* AddDataArray(Thread* self, const uint8_t* begin, const uint8_t* end)
Mathieu Chartier90443472015-07-16 20:32:27 -070085 REQUIRES(!lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080086
87 // Get code for a method, returns null if it is not in the jit cache.
Mathieu Chartiere401d142015-04-22 13:56:20 -070088 const void* GetCodeFor(ArtMethod* method)
Mathieu Chartier90443472015-07-16 20:32:27 -070089 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080090
Mathieu Chartierbce416f2015-03-23 12:37:35 -070091 // 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 Chartiere401d142015-04-22 13:56:20 -070093 void SaveCompiledCode(ArtMethod* method, const void* old_code_ptr)
Mathieu Chartier90443472015-07-16 20:32:27 -070094 SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080095
96 private:
97 // Takes ownership of code_mem_map.
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +010098 JitCodeCache(MemMap* code_map, MemMap* data_map);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080099
Mathieu Chartierbce416f2015-03-23 12:37:35 -0700100 // Lock which guards.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800101 Mutex lock_;
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +0100102 // 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 Chartiere5f13e52015-02-24 09:37:21 -0800111 size_t num_methods_;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800112 // 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 Chartiere401d142015-04-22 13:56:20 -0700114 SafeMap<ArtMethod*, const void*> method_code_map_ GUARDED_BY(lock_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800115
Mathieu Chartier3130cdf2015-05-03 15:20:23 -0700116 DISALLOW_IMPLICIT_CONSTRUCTORS(JitCodeCache);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800117};
118
119
120} // namespace jit
121} // namespace art
122
123#endif // ART_RUNTIME_JIT_JIT_CODE_CACHE_H_