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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/command_buffer/service/gr_shader_cache.h"
#include <thread>
#include "base/base64.h"
#include "base/memory/memory_pressure_listener_registry.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "gpu/config/gpu_finch_features.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gpu {
namespace raster {
namespace {
constexpr char kShaderKey[] = "key";
constexpr char kShader[] = "shader";
constexpr size_t kCacheLimit = 1024u;
} // namespace
class GrShaderCacheTest : public GrShaderCache::Client, public testing::Test {
public:
GrShaderCacheTest() : cache_(kCacheLimit, this) {}
void StoreShader(const std::string& key, const std::string& shader) override {
disk_cache_[key] = shader;
}
base::MemoryPressureListenerRegistry memory_pressure_listener_registry_;
base::test::TaskEnvironment task_environment_;
GrShaderCache cache_;
std::unordered_map<std::string, std::string> disk_cache_;
};
TEST_F(GrShaderCacheTest, DoesNotCacheForIncognito) {
int32_t incognito_client_id = 2;
auto key = SkData::MakeWithCString(kShaderKey);
auto shader = SkData::MakeWithCString(kShader);
{
GrShaderCache::ScopedCacheUse cache_use(&cache_, incognito_client_id);
EXPECT_EQ(cache_.load(*key), nullptr);
cache_.store(*key, *shader);
}
EXPECT_EQ(disk_cache_.size(), 0u);
int32_t regular_client_id = 3;
cache_.CacheClientIdOnDisk(regular_client_id);
{
GrShaderCache::ScopedCacheUse cache_use(&cache_, regular_client_id);
auto cached_shader = cache_.load(*key);
ASSERT_TRUE(cached_shader);
EXPECT_TRUE(cached_shader->equals(shader.get()));
}
EXPECT_EQ(disk_cache_.size(), 1u);
{
GrShaderCache::ScopedCacheUse cache_use(&cache_, regular_client_id);
auto second_key = SkData::MakeWithCString("key2");
EXPECT_EQ(cache_.load(*second_key), nullptr);
cache_.store(*second_key, *shader);
}
EXPECT_EQ(disk_cache_.size(), 2u);
}
TEST_F(GrShaderCacheTest, LoadedFromDisk) {
int32_t regular_client_id = 3;
cache_.CacheClientIdOnDisk(regular_client_id);
auto key = SkData::MakeWithCopy(kShaderKey, strlen(kShaderKey));
auto shader = SkData::MakeWithCString(kShader);
std::string key_str(static_cast<const char*>(key->data()), key->size());
std::string shader_str(static_cast<const char*>(shader->data()),
shader->size());
std::string encoded_key = base::Base64Encode(key_str);
cache_.PopulateCache(encoded_key, shader_str);
{
GrShaderCache::ScopedCacheUse cache_use(&cache_, regular_client_id);
auto cached_shader = cache_.load(*key);
ASSERT_TRUE(cached_shader);
EXPECT_TRUE(cached_shader->equals(shader.get()));
}
EXPECT_EQ(disk_cache_.size(), 0u);
}
TEST_F(GrShaderCacheTest, EnforcesLimits) {
int32_t regular_client_id = 3;
cache_.CacheClientIdOnDisk(regular_client_id);
auto key = SkData::MakeWithCopy(kShaderKey, strlen(kShaderKey));
auto shader = SkData::MakeUninitialized(kCacheLimit);
{
GrShaderCache::ScopedCacheUse cache_use(&cache_, regular_client_id);
EXPECT_EQ(cache_.load(*key), nullptr);
cache_.store(*key, *shader);
}
EXPECT_EQ(cache_.num_cache_entries(), 1u);
{
auto second_key = SkData::MakeWithCString("key2");
GrShaderCache::ScopedCacheUse cache_use(&cache_, regular_client_id);
EXPECT_EQ(cache_.load(*second_key), nullptr);
cache_.store(*second_key, *shader);
}
EXPECT_EQ(cache_.num_cache_entries(), 1u);
{
auto third_key = SkData::MakeWithCString("key3");
GrShaderCache::ScopedCacheUse cache_use(&cache_, regular_client_id);
EXPECT_EQ(cache_.load(*third_key), nullptr);
std::string key_str(static_cast<const char*>(third_key->data()),
third_key->size());
std::string shader_str(static_cast<const char*>(shader->data()),
shader->size());
cache_.PopulateCache(key_str, shader_str);
}
EXPECT_EQ(cache_.num_cache_entries(), 1u);
}
TEST_F(GrShaderCacheTest, StoringSameEntry) {
int32_t regular_client_id = 3;
cache_.CacheClientIdOnDisk(regular_client_id);
auto key = SkData::MakeWithCopy(kShaderKey, strlen(kShaderKey));
auto shader = SkData::MakeUninitialized(kCacheLimit);
{
GrShaderCache::ScopedCacheUse cache_use(&cache_, regular_client_id);
EXPECT_EQ(cache_.load(*key), nullptr);
cache_.store(*key, *shader);
}
EXPECT_EQ(cache_.num_cache_entries(), 1u);
EXPECT_EQ(cache_.curr_size_bytes_for_testing(), shader->size());
auto shader2 = SkData::MakeUninitialized(kCacheLimit / 2);
{
GrShaderCache::ScopedCacheUse cache_use(&cache_, regular_client_id);
EXPECT_NE(cache_.load(*key), nullptr);
cache_.store(*key, *shader2);
}
EXPECT_EQ(cache_.num_cache_entries(), 1u);
EXPECT_EQ(cache_.curr_size_bytes_for_testing(), shader2->size());
}
TEST_F(GrShaderCacheTest, PopulateFromDiskAfterStoring) {
int32_t regular_client_id = 3;
cache_.CacheClientIdOnDisk(regular_client_id);
auto key = SkData::MakeWithCopy(kShaderKey, strlen(kShaderKey));
auto shader = SkData::MakeWithCString(kShader);
{
GrShaderCache::ScopedCacheUse cache_use(&cache_, regular_client_id);
EXPECT_EQ(cache_.load(*key), nullptr);
cache_.store(*key, *shader);
}
EXPECT_EQ(cache_.num_cache_entries(), 1u);
EXPECT_EQ(cache_.curr_size_bytes_for_testing(), shader->size());
// Try storing a different shader with the same key.
std::string key_str(static_cast<const char*>(key->data()), key->size());
std::string shader_str(static_cast<const char*>(shader->data()),
shader->size() / 2);
cache_.PopulateCache(key_str, shader_str);
{
GrShaderCache::ScopedCacheUse cache_use(&cache_, regular_client_id);
auto cached_shader = cache_.load(*key);
ASSERT_TRUE(cached_shader);
EXPECT_TRUE(cached_shader->equals(shader.get()));
}
EXPECT_EQ(disk_cache_.size(), 1u);
}
// This test creates GrShaderCache::ScopedCacheUse object from 2 different
// thread which exists together. In a non thread safe GrShaderCache, this will
// hit DCHECKS in ScopedCacheUse::ScopedCacheUse() since the current_client_id
// already exists from 1st object. In a thread safe model, it will not hit
// DCHECKS.
TEST_F(GrShaderCacheTest, MultipleThreadsUsingSameCache) {
int32_t regular_client_id = 3;
int32_t new_client_id = 4;
cache_.CacheClientIdOnDisk(regular_client_id);
auto key = SkData::MakeWithCopy(kShaderKey, strlen(kShaderKey));
auto shader = SkData::MakeWithCString(kShader);
GrShaderCache::ScopedCacheUse cache_use1(&cache_, regular_client_id);
EXPECT_EQ(cache_.load(*key), nullptr);
cache_.store(*key, *shader);
EXPECT_EQ(cache_.num_cache_entries(), 1u);
EXPECT_EQ(cache_.curr_size_bytes_for_testing(), shader->size());
// Different client id to use cache on a different thread.
std::thread second_thread([&]() {
auto key2 = SkData::MakeWithCString("key2");
GrShaderCache::ScopedCacheUse cache_use2(&cache_, new_client_id);
EXPECT_EQ(cache_.load(*key2), nullptr);
// Store same shader on a different key.
cache_.store(*key2, *shader);
});
second_thread.join();
EXPECT_EQ(cache_.num_cache_entries(), 2u);
EXPECT_EQ(cache_.curr_size_bytes_for_testing(), 2 * shader->size());
}
TEST_F(GrShaderCacheTest, MemoryPressure) {
int32_t regular_client_id = 3;
cache_.CacheClientIdOnDisk(regular_client_id);
// Fill the cache to its limit.
const size_t entry_size = kCacheLimit / 4;
auto shader = SkData::MakeUninitialized(entry_size);
{
GrShaderCache::ScopedCacheUse cache_use(&cache_, regular_client_id);
for (int i = 0; i < 4; ++i) {
auto key =
SkData::MakeWithCString(base::StringPrintf("key%d", i).c_str());
cache_.store(*key, *shader);
}
}
EXPECT_EQ(cache_.num_cache_entries(), 4u);
EXPECT_EQ(cache_.curr_size_bytes_for_testing(), kCacheLimit);
// Trigger moderate memory pressure.
base::MemoryPressureListenerRegistry::SimulatePressureNotificationAsync(
base::MEMORY_PRESSURE_LEVEL_MODERATE, task_environment_.QuitClosure());
task_environment_.RunUntilQuit();
// Moderate memory pressure reduces limit by 4x.
// New limit: kCacheLimit / 4 = 256.
// Since each entry is 256, only 1 entry should remain.
EXPECT_EQ(cache_.num_cache_entries(), 1u);
EXPECT_EQ(cache_.curr_size_bytes_for_testing(), entry_size);
// Verify that the limit is still enforced for new stores.
{
GrShaderCache::ScopedCacheUse cache_use(&cache_, regular_client_id);
auto key = SkData::MakeWithCString("new_key");
cache_.store(*key, *shader);
}
EXPECT_EQ(cache_.num_cache_entries(), 1u);
// Trigger critical memory pressure.
base::MemoryPressureListenerRegistry::SimulatePressureNotificationAsync(
base::MEMORY_PRESSURE_LEVEL_CRITICAL, task_environment_.QuitClosure());
task_environment_.RunUntilQuit();
// Critical memory pressure sets limit to 0.
EXPECT_EQ(cache_.num_cache_entries(), 0u);
EXPECT_EQ(cache_.curr_size_bytes_for_testing(), 0u);
// Verify that the limit is still enforced for new stores.
{
GrShaderCache::ScopedCacheUse cache_use(&cache_, regular_client_id);
auto key = SkData::MakeWithCString("new_key_critical");
cache_.store(*key, *shader);
}
EXPECT_EQ(cache_.num_cache_entries(), 0u);
// Restore memory pressure to none.
base::MemoryPressureListenerRegistry::SimulatePressureNotificationAsync(
base::MEMORY_PRESSURE_LEVEL_NONE, task_environment_.QuitClosure());
task_environment_.RunUntilQuit();
// Limit should be restored to kCacheLimit.
// We can now store more entries.
{
GrShaderCache::ScopedCacheUse cache_use(&cache_, regular_client_id);
for (int i = 0; i < 4; ++i) {
auto key = SkData::MakeWithCString(
base::StringPrintf("restore_key%d", i).c_str());
cache_.store(*key, *shader);
}
}
EXPECT_EQ(cache_.num_cache_entries(), 4u);
EXPECT_EQ(cache_.curr_size_bytes_for_testing(), kCacheLimit);
}
} // namespace raster
} // namespace gpu
|