File: interpreter_cache.h

package info (click to toggle)
android-platform-art 11.0.0%2Br48-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 78,932 kB
  • sloc: cpp: 459,858; java: 163,268; asm: 22,644; python: 9,815; sh: 6,330; ansic: 4,117; xml: 2,855; perl: 77; makefile: 73
file content (99 lines) | stat: -rw-r--r-- 3,222 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2018 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.
 */

#ifndef ART_RUNTIME_INTERPRETER_INTERPRETER_CACHE_H_
#define ART_RUNTIME_INTERPRETER_INTERPRETER_CACHE_H_

#include <array>
#include <atomic>

#include "base/bit_utils.h"
#include "base/macros.h"

namespace art {

class Thread;

// Small fast thread-local cache for the interpreter.
// It can hold arbitrary pointer-sized key-value pair.
// The interpretation of the value depends on the key.
// Presence of entry might imply some pre-conditions.
// All operations must be done from the owning thread,
// or at a point when the owning thread is suspended.
//
// The key-value pairs stored in the cache currently are:
//   iget/iput: The field offset. The field must be non-volatile.
//   sget/sput: The ArtField* pointer. The field must be non-volitile.
//   invoke: The ArtMethod* pointer (before vtable indirection, etc).
//
// We ensure consistency of the cache by clearing it
// whenever any dex file is unloaded.
//
// Aligned to 16-bytes to make it easier to get the address of the cache
// from assembly (it ensures that the offset is valid immediate value).
class ALIGNED(16) InterpreterCache {
 public:
  // Aligned since we load the whole entry in single assembly instruction.
  typedef std::pair<const void*, size_t> Entry ALIGNED(2 * sizeof(size_t));

  // 2x size increase/decrease corresponds to ~0.5% interpreter performance change.
  // Value of 256 has around 75% cache hit rate.
  static constexpr size_t kSize = 256;

  InterpreterCache() {
    // We can not use the Clear() method since the constructor will not
    // be called from the owning thread.
    data_.fill(Entry{});
  }

  // Clear the whole cache. It requires the owning thread for DCHECKs.
  void Clear(Thread* owning_thread);

  ALWAYS_INLINE bool Get(const void* key, /* out */ size_t* value) {
    DCHECK(IsCalledFromOwningThread());
    Entry& entry = data_[IndexOf(key)];
    if (LIKELY(entry.first == key)) {
      *value = entry.second;
      return true;
    }
    return false;
  }

  ALWAYS_INLINE void Set(const void* key, size_t value) {
    DCHECK(IsCalledFromOwningThread());
    data_[IndexOf(key)] = Entry{key, value};
  }

  std::array<Entry, kSize>& GetArray() {
    return data_;
  }

 private:
  bool IsCalledFromOwningThread();

  static ALWAYS_INLINE size_t IndexOf(const void* key) {
    static_assert(IsPowerOfTwo(kSize), "Size must be power of two");
    size_t index = (reinterpret_cast<uintptr_t>(key) >> 2) & (kSize - 1);
    DCHECK_LT(index, kSize);
    return index;
  }

  std::array<Entry, kSize> data_;
};

}  // namespace art

#endif  // ART_RUNTIME_INTERPRETER_INTERPRETER_CACHE_H_