Commit f153f6fb authored by shivanigithub's avatar shivanigithub Committed by Commit Bot

Add Memory cache metrics for partitioning by top-frame site

This CL keeps track of the top-frame sites for each resource cached in
the blink cache and updates metrics to see the cache hits if the cache
was partitioned by top-frame site.
These metrics will then be compared with the per-document cache metrics
added in
https://chromium-review.googlesource.com/c/chromium/src/+/2543684
to help decide between the two design choices.

Bug: 1129860
Change-Id: I41a4dc7d3283a2e232df21c87afe9877c0482a3f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2558763Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarJesse Doherty <jwd@chromium.org>
Commit-Queue: Shivani Sharma <shivanisha@chromium.org>
Cr-Commit-Position: refs/heads/master@{#831027}
parent a1eabca2
include_rules = [ include_rules = [
"+net/base/ip_endpoint.h", "+net/base/ip_endpoint.h",
"+net/base/schemeful_site.h",
"+net/dns/public", "+net/dns/public",
"+services/network/public/cpp/fetch_api_utils.h", "+services/network/public/cpp/fetch_api_utils.h",
"+services/network/public/cpp/optional_trust_token_params.h", "+services/network/public/cpp/optional_trust_token_params.h",
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/time/default_clock.h" #include "base/time/default_clock.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "net/base/schemeful_site.h"
#include "services/network/public/mojom/fetch_api.mojom-blink.h" #include "services/network/public/mojom/fetch_api.mojom-blink.h"
#include "third_party/blink/public/platform/platform.h" #include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/web_security_origin.h" #include "third_party/blink/public/platform/web_security_origin.h"
...@@ -128,8 +129,14 @@ static inline bool ShouldUpdateHeaderAfterRevalidation( ...@@ -128,8 +129,14 @@ static inline bool ShouldUpdateHeaderAfterRevalidation(
namespace { namespace {
const base::Clock* g_clock_for_testing = nullptr; const base::Clock* g_clock_for_testing = nullptr;
String OriginToSchemefulSite(const SecurityOrigin& origin) {
net::SchemefulSite site(origin.ToUrlOrigin());
return String(site.Serialize().c_str());
} }
} // namespace
static inline base::Time Now() { static inline base::Time Now() {
const base::Clock* clock = g_clock_for_testing const base::Clock* clock = g_clock_for_testing
? g_clock_for_testing ? g_clock_for_testing
...@@ -155,6 +162,13 @@ Resource::Resource(const ResourceRequestHead& request, ...@@ -155,6 +162,13 @@ Resource::Resource(const ResourceRequestHead& request,
response_timestamp_(Now()), response_timestamp_(Now()),
resource_request_(request), resource_request_(request),
overhead_size_(CalculateOverheadSize()) { overhead_size_(CalculateOverheadSize()) {
scoped_refptr<const SecurityOrigin> top_frame_origin =
resource_request_.TopFrameOrigin();
if (top_frame_origin) {
existing_top_frame_sites_in_cache_.insert(
OriginToSchemefulSite(*top_frame_origin));
}
InstanceCounters::IncrementCounter(InstanceCounters::kResourceCounter); InstanceCounters::IncrementCounter(InstanceCounters::kResourceCounter);
if (IsMainThread()) if (IsMainThread())
...@@ -1186,4 +1200,10 @@ void Resource::SetClockForTesting(const base::Clock* clock) { ...@@ -1186,4 +1200,10 @@ void Resource::SetClockForTesting(const base::Clock* clock) {
g_clock_for_testing = clock; g_clock_for_testing = clock;
} }
bool Resource::AppendTopFrameSiteForMetrics(const SecurityOrigin& origin) {
auto result =
existing_top_frame_sites_in_cache_.insert(OriginToSchemefulSite(origin));
return !result.is_new_entry;
}
} // namespace blink } // namespace blink
...@@ -403,6 +403,11 @@ class PLATFORM_EXPORT Resource : public GarbageCollected<Resource>, ...@@ -403,6 +403,11 @@ class PLATFORM_EXPORT Resource : public GarbageCollected<Resource>,
return CalculateOverheadSize(); return CalculateOverheadSize();
} }
// Appends the top-frame site derived from |origin| to
// |existing_top_frame_sites_in_cache_| and returns true if the same site
// already exists.
bool AppendTopFrameSiteForMetrics(const SecurityOrigin& origin);
protected: protected:
Resource(const ResourceRequestHead&, Resource(const ResourceRequestHead&,
ResourceType, ResourceType,
...@@ -541,6 +546,11 @@ class PLATFORM_EXPORT Resource : public GarbageCollected<Resource>, ...@@ -541,6 +546,11 @@ class PLATFORM_EXPORT Resource : public GarbageCollected<Resource>,
WebScopedVirtualTimePauser virtual_time_pauser_; WebScopedVirtualTimePauser virtual_time_pauser_;
// To compute metrics for measuring the efficacy of the
// memory cache if it was partitioned by top-frame site (in addition to the
// current origin which it is already partitioned by).
HashSet<String> existing_top_frame_sites_in_cache_;
DISALLOW_COPY_AND_ASSIGN(Resource); DISALLOW_COPY_AND_ASSIGN(Resource);
}; };
......
...@@ -806,7 +806,8 @@ void ResourceFetcher::UpdateMemoryCacheStats( ...@@ -806,7 +806,8 @@ void ResourceFetcher::UpdateMemoryCacheStats(
const FetchParameters& params, const FetchParameters& params,
const ResourceFactory& factory, const ResourceFactory& factory,
bool is_static_data, bool is_static_data,
bool in_cached_resources_map) const { bool in_cached_resources_map,
bool same_top_frame_site_resource_cached) const {
if (is_static_data) if (is_static_data)
return; return;
...@@ -829,6 +830,11 @@ void ResourceFetcher::UpdateMemoryCacheStats( ...@@ -829,6 +830,11 @@ void ResourceFetcher::UpdateMemoryCacheStats(
if (in_cached_resources_map) if (in_cached_resources_map)
DEFINE_RESOURCE_HISTOGRAM("PerDocument."); DEFINE_RESOURCE_HISTOGRAM("PerDocument.");
// Log metrics to evaluate effectiveness of the memory cache if it was
// partitioned by the top-frame site.
if (same_top_frame_site_resource_cached)
DEFINE_RESOURCE_HISTOGRAM("PerTopFrameSite.");
// Async (and defer) scripts may have more cache misses, track them // Async (and defer) scripts may have more cache misses, track them
// separately. See https://crbug.com/1043679 for context. // separately. See https://crbug.com/1043679 for context.
if (params.Defer() != FetchParameters::DeferOption::kNoDefer && if (params.Defer() != FetchParameters::DeferOption::kNoDefer &&
...@@ -1074,6 +1080,7 @@ Resource* ResourceFetcher::RequestResource(FetchParameters& params, ...@@ -1074,6 +1080,7 @@ Resource* ResourceFetcher::RequestResource(FetchParameters& params,
} }
} }
bool same_top_frame_site_resource_cached = false;
if (!is_stale_revalidation && !resource) { if (!is_stale_revalidation && !resource) {
resource = MatchPreload(params, resource_type); resource = MatchPreload(params, resource_type);
if (resource) { if (resource) {
...@@ -1087,6 +1094,12 @@ Resource* ResourceFetcher::RequestResource(FetchParameters& params, ...@@ -1087,6 +1094,12 @@ Resource* ResourceFetcher::RequestResource(FetchParameters& params,
if (resource) { if (resource) {
policy = DetermineRevalidationPolicy(resource_type, params, *resource, policy = DetermineRevalidationPolicy(resource_type, params, *resource,
is_static_data); is_static_data);
scoped_refptr<const SecurityOrigin> top_frame_origin =
resource_request.TopFrameOrigin();
if (top_frame_origin) {
same_top_frame_site_resource_cached =
resource->AppendTopFrameSiteForMetrics(*top_frame_origin);
}
} }
} }
} }
...@@ -1095,7 +1108,8 @@ Resource* ResourceFetcher::RequestResource(FetchParameters& params, ...@@ -1095,7 +1108,8 @@ Resource* ResourceFetcher::RequestResource(FetchParameters& params,
MemoryCache::RemoveFragmentIdentifierIfNeeded(params.Url())); MemoryCache::RemoveFragmentIdentifierIfNeeded(params.Url()));
UpdateMemoryCacheStats(resource, policy, params, factory, is_static_data, UpdateMemoryCacheStats(resource, policy, params, factory, is_static_data,
in_cached_resources_map); in_cached_resources_map,
same_top_frame_site_resource_cached);
switch (policy) { switch (policy) {
case RevalidationPolicy::kReload: case RevalidationPolicy::kReload:
......
...@@ -405,7 +405,8 @@ class PLATFORM_EXPORT ResourceFetcher ...@@ -405,7 +405,8 @@ class PLATFORM_EXPORT ResourceFetcher
const FetchParameters&, const FetchParameters&,
const ResourceFactory&, const ResourceFactory&,
bool is_static_data, bool is_static_data,
bool in_cached_resources_map) const; bool in_cached_resources_map,
bool same_top_frame_site_resource_cached) const;
void ScheduleStaleRevalidate(Resource* stale_resource); void ScheduleStaleRevalidate(Resource* stale_resource);
void RevalidateStaleResource(Resource* stale_resource); void RevalidateStaleResource(Resource* stale_resource);
......
...@@ -277,6 +277,88 @@ TEST_F(ResourceFetcherTest, UseExistingResource) { ...@@ -277,6 +277,88 @@ TEST_F(ResourceFetcherTest, UseExistingResource) {
"Blink.MemoryCache.RevalidationPolicy.PerDocument.Mock", 1); "Blink.MemoryCache.RevalidationPolicy.PerDocument.Mock", 1);
histogram_tester.ExpectTotalCount("Blink.MemoryCache.RevalidationPolicy.Mock", histogram_tester.ExpectTotalCount("Blink.MemoryCache.RevalidationPolicy.Mock",
3); 3);
histogram_tester.ExpectBucketCount(
"Blink.MemoryCache.RevalidationPolicy.Mock",
3 /* RevalidationPolicy::kLoad */, 1);
histogram_tester.ExpectBucketCount(
"Blink.MemoryCache.RevalidationPolicy.Mock",
0 /* RevalidationPolicy::kUse */, 2);
}
TEST_F(ResourceFetcherTest, MetricsPerTopFrameSite) {
blink::HistogramTester histogram_tester;
KURL url("http://127.0.0.1:8000/foo.html");
ResourceResponse response(url);
response.SetHttpStatusCode(200);
response.SetHttpHeaderField(http_names::kCacheControl, "max-age=3600");
platform_->GetURLLoaderMockFactory()->RegisterURL(
url, WrappedResourceResponse(response),
test::PlatformTestDataPath(kTestResourceFilename));
ResourceRequestHead request_head(url);
scoped_refptr<const SecurityOrigin> origin_a =
SecurityOrigin::Create(KURL("https://a.test"));
request_head.SetTopFrameOrigin(origin_a);
request_head.SetRequestorOrigin(origin_a);
FetchParameters fetch_params =
FetchParameters::CreateForTest(ResourceRequest(request_head));
auto* fetcher_1 = CreateFetcher();
Resource* resource_1 = MockResource::Fetch(fetch_params, fetcher_1, nullptr);
ASSERT_TRUE(resource_1);
platform_->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
EXPECT_TRUE(resource_1->IsLoaded());
EXPECT_TRUE(GetMemoryCache()->Contains(resource_1));
auto* fetcher_2 = CreateFetcher();
ResourceRequestHead request_head_2(url);
scoped_refptr<const SecurityOrigin> origin_b =
SecurityOrigin::Create(KURL("https://b.test"));
request_head_2.SetTopFrameOrigin(origin_b);
request_head_2.SetRequestorOrigin(origin_a);
FetchParameters fetch_params_2 =
FetchParameters::CreateForTest(ResourceRequest(request_head_2));
Resource* resource_2 =
MockResource::Fetch(fetch_params_2, fetcher_2, nullptr);
EXPECT_EQ(resource_1, resource_2);
// Test histograms.
histogram_tester.ExpectTotalCount(
"Blink.MemoryCache.RevalidationPolicy.PerTopFrameSite.Mock", 0);
histogram_tester.ExpectTotalCount("Blink.MemoryCache.RevalidationPolicy.Mock",
2);
histogram_tester.ExpectBucketCount(
"Blink.MemoryCache.RevalidationPolicy.Mock",
3 /* RevalidationPolicy::kLoad */, 1);
histogram_tester.ExpectBucketCount(
"Blink.MemoryCache.RevalidationPolicy.Mock",
0 /* RevalidationPolicy::kUse */, 1);
// Now load the same resource with origin_b as top-frame site. The
// PerTopFrameSite histogram should be incremented.
auto* fetcher_3 = CreateFetcher();
ResourceRequestHead request_head_3(url);
scoped_refptr<const SecurityOrigin> foo_origin_b =
SecurityOrigin::Create(KURL("https://foo.b.test"));
request_head_3.SetTopFrameOrigin(foo_origin_b);
request_head_3.SetRequestorOrigin(origin_a);
FetchParameters fetch_params_3 =
FetchParameters::CreateForTest(ResourceRequest(request_head_3));
Resource* resource_3 =
MockResource::Fetch(fetch_params_2, fetcher_3, nullptr);
EXPECT_EQ(resource_1, resource_3);
histogram_tester.ExpectTotalCount(
"Blink.MemoryCache.RevalidationPolicy.PerTopFrameSite.Mock", 1);
histogram_tester.ExpectBucketCount(
"Blink.MemoryCache.RevalidationPolicy.PerTopFrameSite.Mock",
0 /* RevalidationPolicy::kUse */, 1);
histogram_tester.ExpectTotalCount("Blink.MemoryCache.RevalidationPolicy.Mock",
3);
histogram_tester.ExpectBucketCount(
"Blink.MemoryCache.RevalidationPolicy.Mock",
0 /* RevalidationPolicy::kUse */, 2);
} }
// Verify that the ad bit is copied to WillSendRequest's request when the // Verify that the ad bit is copied to WillSendRequest's request when the
......
...@@ -1479,6 +1479,17 @@ reviews. Googlers can read more about this at go/gwsq-gerrit. ...@@ -1479,6 +1479,17 @@ reviews. Googlers can read more about this at go/gwsq-gerrit.
</summary> </summary>
</histogram> </histogram>
<histogram name="Blink.MemoryCache.RevalidationPolicy.PerTopFrameSite"
enum="RevalidationPolicy" expires_after="2021-04-21">
<owner>shivanisha@chromium.org</owner>
<owner>privacy-sandbox-dev@chromium.org</owner>
<summary>
RevalidationPolicy used for requests for each resource type. Logged only if
the resource is found in the memory cache and if the same top-frame site had
loaded this resource earlier.
</summary>
</histogram>
<histogram name="Blink.MemoryCache.RevalidationPolicy.Preload" <histogram name="Blink.MemoryCache.RevalidationPolicy.Preload"
enum="RevalidationPolicy" expires_after="2021-04-21"> enum="RevalidationPolicy" expires_after="2021-04-21">
<owner>hiroshige@chromium.org</owner> <owner>hiroshige@chromium.org</owner>
......
...@@ -15861,6 +15861,8 @@ reviews. Googlers can read more about this at go/gwsq-gerrit. ...@@ -15861,6 +15861,8 @@ reviews. Googlers can read more about this at go/gwsq-gerrit.
<affected-histogram name="Blink.MemoryCache.RevalidationPolicy"/> <affected-histogram name="Blink.MemoryCache.RevalidationPolicy"/>
<affected-histogram name="Blink.MemoryCache.RevalidationPolicy.Dead"/> <affected-histogram name="Blink.MemoryCache.RevalidationPolicy.Dead"/>
<affected-histogram name="Blink.MemoryCache.RevalidationPolicy.PerDocument"/> <affected-histogram name="Blink.MemoryCache.RevalidationPolicy.PerDocument"/>
<affected-histogram
name="Blink.MemoryCache.RevalidationPolicy.PerTopFrameSite"/>
<affected-histogram name="Blink.MemoryCache.RevalidationPolicy.Preload"/> <affected-histogram name="Blink.MemoryCache.RevalidationPolicy.Preload"/>
<affected-histogram name="PreloadScanner.Counts"/> <affected-histogram name="PreloadScanner.Counts"/>
<affected-histogram name="PreloadScanner.Counts.Miss"/> <affected-histogram name="PreloadScanner.Counts.Miss"/>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment