blob: b4966fd61570dde0e5e6c9a35475b1cb24438580 [file] [log] [blame]
David Srbecky912f36c2018-09-08 12:22:58 +01001/*
2 * Copyright (C) 2018 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_INTERPRETER_INTERPRETER_CACHE_H_
18#define ART_RUNTIME_INTERPRETER_INTERPRETER_CACHE_H_
19
20#include <array>
21#include <atomic>
22
23#include "base/bit_utils.h"
24#include "base/macros.h"
25
26namespace art {
27
28class Instruction;
29class Thread;
30
31// Small fast thread-local cache for the interpreter.
32// The key for the cache is the dex instruction pointer.
33// The interpretation of the value depends on the opcode.
34// Presence of entry might imply some performance pre-conditions.
35// All operations must be done from the owning thread,
36// or at a point when the owning thread is suspended.
37//
David Srbeckyef79aa32018-09-08 18:02:36 +010038// The values stored for opcodes in the cache currently are:
39// iget/iput: The field offset. The field must be non-volatile.
40// sget/sput: The ArtField* pointer. The field must be non-volitile.
41//
David Srbecky912f36c2018-09-08 12:22:58 +010042// Aligned to 16-bytes to make it easier to get the address of the cache
43// from assembly (it ensures that the offset is valid immediate value).
44class ALIGNED(16) InterpreterCache {
45 // Aligned since we load the whole entry in single assembly instruction.
46 typedef std::pair<const Instruction*, size_t> Entry ALIGNED(2 * sizeof(size_t));
47
48 public:
49 // 2x size increase/decrease corresponds to ~0.5% interpreter performance change.
50 // Value of 256 has around 75% cache hit rate.
51 static constexpr size_t kSize = 256;
52
53 InterpreterCache() {
54 // We can not use the Clear() method since the constructor will not
55 // be called from the owning thread.
56 data_.fill(Entry{});
57 }
58
59 // Clear the whole cache. It requires the owning thread for DCHECKs.
60 void Clear(Thread* owning_thread);
61
62 ALWAYS_INLINE bool Get(const Instruction* key, /* out */ size_t* value) {
63 DCHECK(IsCalledFromOwningThread());
64 Entry& entry = data_[IndexOf(key)];
65 if (LIKELY(entry.first == key)) {
66 *value = entry.second;
67 return true;
68 }
69 return false;
70 }
71
72 ALWAYS_INLINE void Set(const Instruction* key, size_t value) {
73 DCHECK(IsCalledFromOwningThread());
74 data_[IndexOf(key)] = Entry{key, value};
75 }
76
77 private:
78 bool IsCalledFromOwningThread();
79
80 static ALWAYS_INLINE size_t IndexOf(const Instruction* key) {
81 static_assert(IsPowerOfTwo(kSize), "Size must be power of two");
82 size_t index = (reinterpret_cast<uintptr_t>(key) >> 2) & (kSize - 1);
83 DCHECK_LT(index, kSize);
84 return index;
85 }
86
87 std::array<Entry, kSize> data_;
88};
89
90} // namespace art
91
92#endif // ART_RUNTIME_INTERPRETER_INTERPRETER_CACHE_H_