1 /*
2  * Copyright (C) 2019 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 #pragma once
18 
19 #include <pthread.h>
20 #include <stdint.h>
21 
22 #include <memory>
23 #include <mutex>
24 #include <optional>
25 #include <unordered_map>
26 
27 #include <unwindstack/Memory.h>
28 
29 namespace unwindstack {
30 
31 class MemoryCacheBase : public Memory {
32  public:
MemoryCacheBase(Memory * memory)33   MemoryCacheBase(Memory* memory) : impl_(memory) {}
34   virtual ~MemoryCacheBase() = default;
35 
AsMemoryCacheBase()36   MemoryCacheBase* AsMemoryCacheBase() override { return this; }
37 
UnderlyingMemory()38   const std::shared_ptr<Memory>& UnderlyingMemory() { return impl_; }
39 
Read(uint64_t addr,void * dst,size_t size)40   size_t Read(uint64_t addr, void* dst, size_t size) override {
41     // Only look at the cache for small reads.
42     if (size > 64) {
43       return impl_->Read(addr, dst, size);
44     }
45     return CachedRead(addr, dst, size);
46   }
47 
ReadTag(uint64_t addr)48   long ReadTag(uint64_t addr) override { return impl_->ReadTag(addr); }
49 
50  protected:
51   constexpr static size_t kCacheBits = 12;
52   constexpr static size_t kCacheMask = (1 << kCacheBits) - 1;
53   constexpr static size_t kCacheSize = 1 << kCacheBits;
54 
55   using CacheDataType = std::unordered_map<uint64_t, uint8_t[kCacheSize]>;
56 
57   virtual size_t CachedRead(uint64_t addr, void* dst, size_t size) = 0;
58 
59   size_t InternalCachedRead(uint64_t addr, void* dst, size_t size, CacheDataType* cache);
60 
61   std::shared_ptr<Memory> impl_;
62 };
63 
64 class MemoryCache : public MemoryCacheBase {
65  public:
MemoryCache(Memory * memory)66   MemoryCache(Memory* memory) : MemoryCacheBase(memory) {}
67   virtual ~MemoryCache() = default;
68 
69   size_t CachedRead(uint64_t addr, void* dst, size_t size) override;
70 
71   void Clear() override;
72 
73  protected:
74   CacheDataType cache_;
75 
76   std::mutex cache_lock_;
77 };
78 
79 class MemoryThreadCache : public MemoryCacheBase {
80  public:
81   MemoryThreadCache(Memory* memory);
82   virtual ~MemoryThreadCache();
83 
84   size_t CachedRead(uint64_t addr, void* dst, size_t size) override;
85 
86   void Clear() override;
87 
88  protected:
89   std::optional<pthread_key_t> thread_cache_;
90 };
91 
92 }  // namespace unwindstack
93