blob: c25222eff30e3b19c1020d7d4f162449a765b77e [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//
38// Aligned to 16-bytes to make it easier to get the address of the cache
39// from assembly (it ensures that the offset is valid immediate value).
40class ALIGNED(16) InterpreterCache {
41 // Aligned since we load the whole entry in single assembly instruction.
42 typedef std::pair<const Instruction*, size_t> Entry ALIGNED(2 * sizeof(size_t));
43
44 public:
45 // 2x size increase/decrease corresponds to ~0.5% interpreter performance change.
46 // Value of 256 has around 75% cache hit rate.
47 static constexpr size_t kSize = 256;
48
49 InterpreterCache() {
50 // We can not use the Clear() method since the constructor will not
51 // be called from the owning thread.
52 data_.fill(Entry{});
53 }
54
55 // Clear the whole cache. It requires the owning thread for DCHECKs.
56 void Clear(Thread* owning_thread);
57
58 ALWAYS_INLINE bool Get(const Instruction* key, /* out */ size_t* value) {
59 DCHECK(IsCalledFromOwningThread());
60 Entry& entry = data_[IndexOf(key)];
61 if (LIKELY(entry.first == key)) {
62 *value = entry.second;
63 return true;
64 }
65 return false;
66 }
67
68 ALWAYS_INLINE void Set(const Instruction* key, size_t value) {
69 DCHECK(IsCalledFromOwningThread());
70 data_[IndexOf(key)] = Entry{key, value};
71 }
72
73 private:
74 bool IsCalledFromOwningThread();
75
76 static ALWAYS_INLINE size_t IndexOf(const Instruction* key) {
77 static_assert(IsPowerOfTwo(kSize), "Size must be power of two");
78 size_t index = (reinterpret_cast<uintptr_t>(key) >> 2) & (kSize - 1);
79 DCHECK_LT(index, kSize);
80 return index;
81 }
82
83 std::array<Entry, kSize> data_;
84};
85
86} // namespace art
87
88#endif // ART_RUNTIME_INTERPRETER_INTERPRETER_CACHE_H_