Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2022, 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 ANDROID_MULTIFILE_BLOB_CACHE_H |
| 18 | #define ANDROID_MULTIFILE_BLOB_CACHE_H |
| 19 | |
| 20 | #include <EGL/egl.h> |
| 21 | #include <EGL/eglext.h> |
| 22 | |
Cody Northrop | be16373 | 2023-03-22 10:14:26 -0600 | [diff] [blame] | 23 | #include <android-base/thread_annotations.h> |
Cody Northrop | 027f242 | 2023-11-12 22:51:01 -0700 | [diff] [blame] | 24 | #include <cutils/properties.h> |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 25 | #include <future> |
| 26 | #include <map> |
| 27 | #include <queue> |
| 28 | #include <string> |
| 29 | #include <thread> |
| 30 | #include <unordered_map> |
| 31 | #include <unordered_set> |
| 32 | |
Cody Northrop | 999db23 | 2023-02-27 17:02:50 -0700 | [diff] [blame] | 33 | #include "FileBlobCache.h" |
| 34 | |
Cody Northrop | 4ee6386 | 2024-11-19 22:41:23 -0700 | [diff] [blame] | 35 | #include <com_android_graphics_egl_flags.h> |
| 36 | |
| 37 | using namespace com::android::graphics::egl; |
| 38 | |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 39 | namespace android { |
| 40 | |
Jisun Lee | 88a1b76 | 2024-10-17 20:12:29 +0900 | [diff] [blame] | 41 | constexpr uint32_t kMultifileBlobCacheVersion = 2; |
Cody Northrop | 027f242 | 2023-11-12 22:51:01 -0700 | [diff] [blame] | 42 | constexpr char kMultifileBlobCacheStatusFile[] = "cache.status"; |
| 43 | |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 44 | struct MultifileHeader { |
Cody Northrop | 999db23 | 2023-02-27 17:02:50 -0700 | [diff] [blame] | 45 | uint32_t magic; |
| 46 | uint32_t crc; |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 47 | EGLsizeiANDROID keySize; |
| 48 | EGLsizeiANDROID valueSize; |
| 49 | }; |
| 50 | |
| 51 | struct MultifileEntryStats { |
Cody Northrop | 99e8f2c | 2024-11-21 15:32:32 -0700 | [diff] [blame] | 52 | uint32_t entryHash; |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 53 | EGLsizeiANDROID valueSize; |
| 54 | size_t fileSize; |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Cody Northrop | 027f242 | 2023-11-12 22:51:01 -0700 | [diff] [blame] | 57 | struct MultifileStatus { |
| 58 | uint32_t magic; |
| 59 | uint32_t crc; |
| 60 | uint32_t cacheVersion; |
| 61 | char buildId[PROP_VALUE_MAX]; |
| 62 | }; |
| 63 | |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 64 | struct MultifileHotCache { |
| 65 | int entryFd; |
| 66 | uint8_t* entryBuffer; |
| 67 | size_t entrySize; |
| 68 | }; |
| 69 | |
| 70 | enum class TaskCommand { |
| 71 | Invalid = 0, |
| 72 | WriteToDisk, |
| 73 | Exit, |
| 74 | }; |
| 75 | |
| 76 | class DeferredTask { |
| 77 | public: |
| 78 | DeferredTask(TaskCommand command) |
| 79 | : mCommand(command), mEntryHash(0), mBuffer(nullptr), mBufferSize(0) {} |
| 80 | |
| 81 | TaskCommand getTaskCommand() { return mCommand; } |
| 82 | |
| 83 | void initWriteToDisk(uint32_t entryHash, std::string fullPath, uint8_t* buffer, |
| 84 | size_t bufferSize) { |
| 85 | mCommand = TaskCommand::WriteToDisk; |
| 86 | mEntryHash = entryHash; |
| 87 | mFullPath = std::move(fullPath); |
| 88 | mBuffer = buffer; |
| 89 | mBufferSize = bufferSize; |
| 90 | } |
| 91 | |
| 92 | uint32_t getEntryHash() { return mEntryHash; } |
| 93 | std::string& getFullPath() { return mFullPath; } |
| 94 | uint8_t* getBuffer() { return mBuffer; } |
| 95 | size_t getBufferSize() { return mBufferSize; }; |
| 96 | |
| 97 | private: |
| 98 | TaskCommand mCommand; |
| 99 | |
| 100 | // Parameters for WriteToDisk |
| 101 | uint32_t mEntryHash; |
| 102 | std::string mFullPath; |
| 103 | uint8_t* mBuffer; |
| 104 | size_t mBufferSize; |
| 105 | }; |
| 106 | |
Cody Northrop | 99e8f2c | 2024-11-21 15:32:32 -0700 | [diff] [blame] | 107 | #if COM_ANDROID_GRAPHICS_EGL_FLAGS(MULTIFILE_BLOBCACHE_ADVANCED_USAGE) |
| 108 | struct MultifileTimeLess { |
| 109 | bool operator()(const struct timespec& t1, const struct timespec& t2) const { |
| 110 | if (t1.tv_sec == t2.tv_sec) { |
| 111 | // If seconds are equal, check nanoseconds |
| 112 | return t1.tv_nsec < t2.tv_nsec; |
| 113 | } else { |
| 114 | // Otherwise, compare seconds |
| 115 | return t1.tv_sec < t2.tv_sec; |
| 116 | } |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | // The third parameter here causes all entries to be sorted by access time, |
| 121 | // so oldest will be accessed first in applyLRU |
| 122 | using MultifileEntryStatsMap = |
| 123 | std::multimap<struct timespec, MultifileEntryStats, MultifileTimeLess>; |
| 124 | using MultifileEntryStatsMapIter = MultifileEntryStatsMap::iterator; |
| 125 | #endif // COM_ANDROID_GRAPHICS_EGL_FLAGS(MULTIFILE_BLOBCACHE_ADVANCED_USAGE) |
| 126 | |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 127 | class MultifileBlobCache { |
| 128 | public: |
Cody Northrop | 5dbcfa7 | 2023-03-24 15:34:09 -0600 | [diff] [blame] | 129 | MultifileBlobCache(size_t maxKeySize, size_t maxValueSize, size_t maxTotalSize, |
Cody Northrop | b526703 | 2023-10-24 10:11:21 -0600 | [diff] [blame] | 130 | size_t maxTotalEntries, const std::string& baseDir); |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 131 | ~MultifileBlobCache(); |
| 132 | |
| 133 | void set(const void* key, EGLsizeiANDROID keySize, const void* value, |
| 134 | EGLsizeiANDROID valueSize); |
| 135 | EGLsizeiANDROID get(const void* key, EGLsizeiANDROID keySize, void* value, |
| 136 | EGLsizeiANDROID valueSize); |
| 137 | |
| 138 | void finish(); |
| 139 | |
| 140 | size_t getTotalSize() const { return mTotalCacheSize; } |
Cody Northrop | b526703 | 2023-10-24 10:11:21 -0600 | [diff] [blame] | 141 | size_t getTotalEntries() const { return mTotalCacheEntries; } |
Cody Northrop | 99e8f2c | 2024-11-21 15:32:32 -0700 | [diff] [blame] | 142 | size_t getTotalCacheSizeDivisor() const { return mTotalCacheSizeDivisor; } |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 143 | |
Cody Northrop | 027f242 | 2023-11-12 22:51:01 -0700 | [diff] [blame] | 144 | const std::string& getCurrentBuildId() const { return mBuildId; } |
| 145 | void setCurrentBuildId(const std::string& buildId) { mBuildId = buildId; } |
| 146 | |
| 147 | uint32_t getCurrentCacheVersion() const { return mCacheVersion; } |
| 148 | void setCurrentCacheVersion(uint32_t cacheVersion) { mCacheVersion = cacheVersion; } |
| 149 | |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 150 | private: |
| 151 | void trackEntry(uint32_t entryHash, EGLsizeiANDROID valueSize, size_t fileSize, |
Cody Northrop | 99e8f2c | 2024-11-21 15:32:32 -0700 | [diff] [blame] | 152 | const timespec& accessTime); |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 153 | bool contains(uint32_t entryHash) const; |
| 154 | bool removeEntry(uint32_t entryHash); |
| 155 | MultifileEntryStats getEntryStats(uint32_t entryHash); |
Cody Northrop | 99e8f2c | 2024-11-21 15:32:32 -0700 | [diff] [blame] | 156 | void updateEntryTime(uint32_t entryHash, const timespec& newTime); |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 157 | |
Cody Northrop | 027f242 | 2023-11-12 22:51:01 -0700 | [diff] [blame] | 158 | bool createStatus(const std::string& baseDir); |
| 159 | bool checkStatus(const std::string& baseDir); |
| 160 | |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 161 | size_t getFileSize(uint32_t entryHash); |
| 162 | size_t getValueSize(uint32_t entryHash); |
| 163 | |
| 164 | void increaseTotalCacheSize(size_t fileSize); |
| 165 | void decreaseTotalCacheSize(size_t fileSize); |
| 166 | |
| 167 | bool addToHotCache(uint32_t entryHash, int fd, uint8_t* entryBufer, size_t entrySize); |
| 168 | bool removeFromHotCache(uint32_t entryHash); |
| 169 | |
Cody Northrop | 027f242 | 2023-11-12 22:51:01 -0700 | [diff] [blame] | 170 | bool clearCache(); |
Cody Northrop | f2588a8 | 2023-03-27 23:03:49 -0600 | [diff] [blame] | 171 | void trimCache(); |
Cody Northrop | b526703 | 2023-10-24 10:11:21 -0600 | [diff] [blame] | 172 | bool applyLRU(size_t cacheSizeLimit, size_t cacheEntryLimit); |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 173 | |
| 174 | bool mInitialized; |
| 175 | std::string mMultifileDirName; |
| 176 | |
Cody Northrop | 027f242 | 2023-11-12 22:51:01 -0700 | [diff] [blame] | 177 | std::string mBuildId; |
| 178 | uint32_t mCacheVersion; |
| 179 | |
Cody Northrop | 99e8f2c | 2024-11-21 15:32:32 -0700 | [diff] [blame] | 180 | #if COM_ANDROID_GRAPHICS_EGL_FLAGS(MULTIFILE_BLOBCACHE_ADVANCED_USAGE) |
| 181 | std::unordered_map<uint32_t, MultifileEntryStatsMapIter> mEntries; |
| 182 | MultifileEntryStatsMap mEntryStats; |
| 183 | #else |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 184 | std::unordered_set<uint32_t> mEntries; |
| 185 | std::unordered_map<uint32_t, MultifileEntryStats> mEntryStats; |
Cody Northrop | 99e8f2c | 2024-11-21 15:32:32 -0700 | [diff] [blame] | 186 | #endif // COM_ANDROID_GRAPHICS_EGL_FLAGS(MULTIFILE_BLOBCACHE_ADVANCED_USAGE) |
| 187 | |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 188 | std::unordered_map<uint32_t, MultifileHotCache> mHotCache; |
| 189 | |
| 190 | size_t mMaxKeySize; |
| 191 | size_t mMaxValueSize; |
| 192 | size_t mMaxTotalSize; |
Cody Northrop | b526703 | 2023-10-24 10:11:21 -0600 | [diff] [blame] | 193 | size_t mMaxTotalEntries; |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 194 | size_t mTotalCacheSize; |
Cody Northrop | b526703 | 2023-10-24 10:11:21 -0600 | [diff] [blame] | 195 | size_t mTotalCacheEntries; |
Cody Northrop | 99e8f2c | 2024-11-21 15:32:32 -0700 | [diff] [blame] | 196 | size_t mTotalCacheSizeDivisor; |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 197 | size_t mHotCacheLimit; |
| 198 | size_t mHotCacheEntryLimit; |
| 199 | size_t mHotCacheSize; |
| 200 | |
| 201 | // Below are the components used for deferred writes |
| 202 | |
| 203 | // Track whether we have pending writes for an entry |
Cody Northrop | be16373 | 2023-03-22 10:14:26 -0600 | [diff] [blame] | 204 | std::mutex mDeferredWriteStatusMutex; |
| 205 | std::multimap<uint32_t, uint8_t*> mDeferredWrites GUARDED_BY(mDeferredWriteStatusMutex); |
Cody Northrop | 6cca6c2 | 2023-02-08 20:23:13 -0700 | [diff] [blame] | 206 | |
| 207 | // Functions to work through tasks in the queue |
| 208 | void processTasks(); |
| 209 | void processTasksImpl(bool* exitThread); |
| 210 | void processTask(DeferredTask& task); |
| 211 | |
| 212 | // Used by main thread to create work for the worker thread |
| 213 | void queueTask(DeferredTask&& task); |
| 214 | |
| 215 | // Used by main thread to wait for worker thread to complete all outstanding work. |
| 216 | void waitForWorkComplete(); |
| 217 | |
| 218 | std::thread mTaskThread; |
| 219 | std::queue<DeferredTask> mTasks; |
| 220 | std::mutex mWorkerMutex; |
| 221 | |
| 222 | // This condition will block the worker thread until a task is queued |
| 223 | std::condition_variable mWorkAvailableCondition; |
| 224 | |
| 225 | // This condition will block the main thread while the worker thread still has tasks |
| 226 | std::condition_variable mWorkerIdleCondition; |
| 227 | |
| 228 | // This bool will track whether all tasks have been completed |
| 229 | bool mWorkerThreadIdle; |
| 230 | }; |
| 231 | |
| 232 | }; // namespace android |
| 233 | |
| 234 | #endif // ANDROID_MULTIFILE_BLOB_CACHE_H |