blob: 0ada562438e93ead221ed0cc0ac472ac5f3378d1 [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
David Srbecky912f36c2018-09-08 12:22:58 +010028class Thread;
29
30// Small fast thread-local cache for the interpreter.
David Srbecky6c2b86e2018-10-15 12:57:20 +010031// It can hold arbitrary pointer-sized key-value pair.
32// The interpretation of the value depends on the key.
33// Presence of entry might imply some pre-conditions.
David Srbecky912f36c2018-09-08 12:22:58 +010034// All operations must be done from the owning thread,
35// or at a point when the owning thread is suspended.
36//
David Srbecky6c2b86e2018-10-15 12:57:20 +010037// The key-value pairs stored in the cache currently are:
David Srbeckyef79aa32018-09-08 18:02:36 +010038// iget/iput: The field offset. The field must be non-volatile.
39// sget/sput: The ArtField* pointer. The field must be non-volitile.
David Srbecky6c2b86e2018-10-15 12:57:20 +010040// invoke: The ArtMethod* pointer (before vtable indirection, etc).
David Srbecky6c2b86e2018-10-15 12:57:20 +010041//
42// We ensure consistency of the cache by clearing it
43// whenever any dex file is unloaded.
David Srbeckyef79aa32018-09-08 18:02:36 +010044//
David Srbecky912f36c2018-09-08 12:22:58 +010045// Aligned to 16-bytes to make it easier to get the address of the cache
46// from assembly (it ensures that the offset is valid immediate value).
47class ALIGNED(16) InterpreterCache {
Nicolas Geoffraya00b54b2019-12-03 14:36:42 +000048 public:
David Srbecky912f36c2018-09-08 12:22:58 +010049 // Aligned since we load the whole entry in single assembly instruction.
David Srbecky6c2b86e2018-10-15 12:57:20 +010050 typedef std::pair<const void*, size_t> Entry ALIGNED(2 * sizeof(size_t));
David Srbecky912f36c2018-09-08 12:22:58 +010051
David Srbecky912f36c2018-09-08 12:22:58 +010052 // 2x size increase/decrease corresponds to ~0.5% interpreter performance change.
53 // Value of 256 has around 75% cache hit rate.
54 static constexpr size_t kSize = 256;
55
56 InterpreterCache() {
57 // We can not use the Clear() method since the constructor will not
58 // be called from the owning thread.
59 data_.fill(Entry{});
60 }
61
62 // Clear the whole cache. It requires the owning thread for DCHECKs.
63 void Clear(Thread* owning_thread);
64
David Srbecky6c2b86e2018-10-15 12:57:20 +010065 ALWAYS_INLINE bool Get(const void* key, /* out */ size_t* value) {
David Srbecky912f36c2018-09-08 12:22:58 +010066 DCHECK(IsCalledFromOwningThread());
67 Entry& entry = data_[IndexOf(key)];
68 if (LIKELY(entry.first == key)) {
69 *value = entry.second;
70 return true;
71 }
72 return false;
73 }
74
David Srbecky6c2b86e2018-10-15 12:57:20 +010075 ALWAYS_INLINE void Set(const void* key, size_t value) {
David Srbecky912f36c2018-09-08 12:22:58 +010076 DCHECK(IsCalledFromOwningThread());
77 data_[IndexOf(key)] = Entry{key, value};
78 }
79
Nicolas Geoffraya00b54b2019-12-03 14:36:42 +000080 std::array<Entry, kSize>& GetArray() {
81 return data_;
82 }
83
David Srbecky912f36c2018-09-08 12:22:58 +010084 private:
85 bool IsCalledFromOwningThread();
86
David Srbecky6c2b86e2018-10-15 12:57:20 +010087 static ALWAYS_INLINE size_t IndexOf(const void* key) {
David Srbecky912f36c2018-09-08 12:22:58 +010088 static_assert(IsPowerOfTwo(kSize), "Size must be power of two");
89 size_t index = (reinterpret_cast<uintptr_t>(key) >> 2) & (kSize - 1);
90 DCHECK_LT(index, kSize);
91 return index;
92 }
93
94 std::array<Entry, kSize> data_;
95};
96
97} // namespace art
98
99#endif // ART_RUNTIME_INTERPRETER_INTERPRETER_CACHE_H_