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