Add dawn version hash to cache keys.

Motivation is to simplify cache evicting by reusing the LRU to evict stale entries from older Dawn versions since there isn't already a simple cache busting solution. The dawn version will just be pushed into the cache keys instead so old version entries will eventually be retired.

- Removes the fingerprint from the GetCachingInterface API on DawnNative.
- Adds the "fingerprint", which was just the hash, to the device;s cache key directly instead.

Bug: dawn:549
Change-Id: I573aa03a2bb96dfe044293b1176d3a7746725572
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/94140
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Loko Kung <lokokung@google.com>
This commit is contained in:
Loko Kung 2022-06-24 23:39:49 +00:00 committed by Dawn LUCI CQ
parent b4ff8c859a
commit 7289bca018
11 changed files with 46 additions and 18 deletions

View File

@ -93,10 +93,10 @@ class DAWN_PLATFORM_EXPORT Platform {
const uint64_t* argValues,
unsigned char flags);
// The |fingerprint| is provided by Dawn to inform the client to discard the Dawn caches
// when the fingerprint changes. The returned CachingInterface is expected to outlive the
// device which uses it to persistently cache objects.
virtual CachingInterface* GetCachingInterface(const void* fingerprint, size_t fingerprintSize);
// The returned CachingInterface is expected to outlive the device which uses it to persistently
// cache objects.
virtual CachingInterface* GetCachingInterface();
virtual std::unique_ptr<WorkerTaskPool> CreateWorkerTaskPool();
private:

View File

@ -14,7 +14,10 @@
#include "dawn/native/BlobCache.h"
#include <algorithm>
#include "dawn/common/Assert.h"
#include "dawn/common/Version_autogen.h"
#include "dawn/native/CacheKey.h"
#include "dawn/native/Instance.h"
#include "dawn/platform/DawnPlatform.h"
@ -39,6 +42,7 @@ void BlobCache::Store(const CacheKey& key, const Blob& value) {
}
Blob BlobCache::LoadInternal(const CacheKey& key) {
ASSERT(ValidateCacheKey(key));
if (mCache == nullptr) {
return Blob();
}
@ -55,6 +59,7 @@ Blob BlobCache::LoadInternal(const CacheKey& key) {
}
void BlobCache::StoreInternal(const CacheKey& key, size_t valueSize, const void* value) {
ASSERT(ValidateCacheKey(key));
ASSERT(value != nullptr);
ASSERT(valueSize > 0);
if (mCache == nullptr) {
@ -63,4 +68,9 @@ void BlobCache::StoreInternal(const CacheKey& key, size_t valueSize, const void*
mCache->StoreData(key.data(), key.size(), value, valueSize);
}
bool BlobCache::ValidateCacheKey(const CacheKey& key) {
return std::search(key.begin(), key.end(), kDawnVersion.begin(), kDawnVersion.end()) !=
key.end();
}
} // namespace dawn::native

View File

@ -48,6 +48,10 @@ class BlobCache {
Blob LoadInternal(const CacheKey& key);
void StoreInternal(const CacheKey& key, size_t valueSize, const void* value);
// Validates the cache key for this version of Dawn. At the moment, this is naively checking
// that the cache key contains the dawn version string in it.
bool ValidateCacheKey(const CacheKey& key);
// Protects thread safety of access to mCache.
std::mutex mMutex;
dawn::platform::CachingInterface* mCache;

View File

@ -15,6 +15,8 @@
#include "dawn/native/CacheKey.h"
#include <iomanip>
#include <string>
#include <string_view>
namespace dawn::native {
@ -29,7 +31,13 @@ std::ostream& operator<<(std::ostream& os, const CacheKey& key) {
template <>
void CacheKeySerializer<std::string>::Serialize(CacheKey* key, const std::string& t) {
key->Record(static_cast<size_t>(t.length()));
key->Record(t.length());
key->insert(key->end(), t.begin(), t.end());
}
template <>
void CacheKeySerializer<std::string_view>::Serialize(CacheKey* key, const std::string_view& t) {
key->Record(t.length());
key->insert(key->end(), t.begin(), t.end());
}

View File

@ -19,7 +19,6 @@
#include <iostream>
#include <limits>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

View File

@ -20,6 +20,7 @@
#include <unordered_set>
#include "dawn/common/Log.h"
#include "dawn/common/Version_autogen.h"
#include "dawn/native/Adapter.h"
#include "dawn/native/AsyncTask.h"
#include "dawn/native/AttachmentState.h"
@ -208,7 +209,7 @@ DeviceBase::DeviceBase(AdapterBase* adapter, const DeviceDescriptor* descriptor)
// Record the cache key from the properties. Note that currently, if a new extension
// descriptor is added (and probably handled here), the cache key recording needs to be
// updated.
mDeviceCacheKey.Record(adapterProperties, mEnabledFeatures.featuresBitSet,
mDeviceCacheKey.Record(kDawnVersion, adapterProperties, mEnabledFeatures.featuresBitSet,
mEnabledToggles.toggleBitset, cacheDesc);
}

View File

@ -20,7 +20,6 @@
#include "dawn/common/GPUInfo.h"
#include "dawn/common/Log.h"
#include "dawn/common/SystemUtils.h"
#include "dawn/common/Version_autogen.h"
#include "dawn/native/ChainUtils_autogen.h"
#include "dawn/native/ErrorData.h"
#include "dawn/native/Surface.h"
@ -95,8 +94,8 @@ BackendsBitset GetEnabledBackends() {
}
dawn::platform::CachingInterface* GetCachingInterface(dawn::platform::Platform* platform) {
if (platform != nullptr && dawn::kDawnVersion.size() > 0) {
return platform->GetCachingInterface(dawn::kDawnVersion.data(), dawn::kDawnVersion.size());
if (platform != nullptr) {
return platform->GetCachingInterface();
}
return nullptr;
}

View File

@ -53,8 +53,7 @@ uint64_t Platform::AddTraceEvent(char phase,
return 0;
}
dawn::platform::CachingInterface* Platform::GetCachingInterface(const void* fingerprint,
size_t fingerprintSize) {
dawn::platform::CachingInterface* Platform::GetCachingInterface() {
return nullptr;
}

View File

@ -79,8 +79,6 @@ void CachingInterfaceMock::StoreDataDefault(const void* key,
DawnCachingMockPlatform::DawnCachingMockPlatform(dawn::platform::CachingInterface* cachingInterface)
: mCachingInterface(cachingInterface) {}
dawn::platform::CachingInterface* DawnCachingMockPlatform::GetCachingInterface(
const void* fingerprint,
size_t fingerprintSize) {
dawn::platform::CachingInterface* DawnCachingMockPlatform::GetCachingInterface() {
return mCachingInterface;
}

View File

@ -63,8 +63,8 @@ class CachingInterfaceMock : public dawn::platform::CachingInterface {
class DawnCachingMockPlatform : public dawn::platform::Platform {
public:
explicit DawnCachingMockPlatform(dawn::platform::CachingInterface* cachingInterface);
dawn::platform::CachingInterface* GetCachingInterface(const void* fingerprint,
size_t fingerprintSize) override;
dawn::platform::CachingInterface* GetCachingInterface() override;
private:
dawn::platform::CachingInterface* mCachingInterface = nullptr;

View File

@ -163,7 +163,17 @@ TEST(CacheKeySerializerTests, StdStrings) {
std::string str = "string";
CacheKey expected;
expected.Record((size_t)6);
expected.Record(size_t(6));
expected.insert(expected.end(), str.begin(), str.end());
EXPECT_THAT(CacheKey().Record(str), CacheKeyEq(expected));
}
TEST(CacheKeySerializerTests, StdStringViews) {
static constexpr std::string_view str("string");
CacheKey expected;
expected.Record(size_t(6));
expected.insert(expected.end(), str.begin(), str.end());
EXPECT_THAT(CacheKey().Record(str), CacheKeyEq(expected));