OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/renderer_host/duplicate_resource_handler.h" | |
6 | |
7 #include <cmath> | |
8 #include <cstring> | |
9 #include <set> | |
10 | |
11 #include "base/logging.h" | |
12 #include "base/metrics/histogram.h" | |
13 #include "content/browser/renderer_host/resource_request_info_impl.h" | |
14 #include "net/base/io_buffer.h" | |
15 #include "net/url_request/url_request.h" | |
16 #include "third_party/smhasher/src/MurmurHash3.h" | |
17 | |
18 | |
19 namespace content { | |
20 | |
21 namespace{ | |
22 | |
23 // This set keeps track of a hash of resources | |
24 // that we have seen | |
25 std::set<uint32>* GetSetOfHashes() { | |
26 static std::set<uint32> seen_resources; | |
gavinp
2012/07/14 19:05:06
This won't build in clang, due to our static destr
| |
27 return &seen_resources; | |
28 } | |
29 | |
30 // This set keeps track of hash of resources based on origin | |
31 // that we have seen previously | |
32 std::set<uint32>* GetSetOfHashesWithURL(){ | |
33 static std::set<uint32> seen_resources_with_url; | |
34 return &seen_resources_with_url; | |
35 } | |
36 | |
37 } // namespace | |
38 | |
39 DuplicateResourceHandler::DuplicateResourceHandler( | |
40 scoped_ptr<ResourceHandler> next_handler, | |
41 ResourceType::Type resource_type, | |
42 net::URLRequest* request) | |
43 : LayeredResourceHandler(next_handler.Pass()), | |
44 resource_type_(resource_type), | |
45 start_seed_(0), | |
46 bytes_read_(0), | |
47 read_buffer_(new net::IOBuffer(kReadBufSize)), | |
48 request_(request) { | |
49 } | |
50 | |
51 DuplicateResourceHandler::~DuplicateResourceHandler() { | |
52 } | |
53 | |
54 bool DuplicateResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, | |
55 int* buf_size, int min_size) { | |
56 DCHECK_EQ(-1, min_size); | |
57 | |
58 *buf = read_buffer_.get(); | |
59 *buf_size = kReadBufSize; | |
60 return next_handler_->OnWillRead(request_id, buf, buf_size, min_size); | |
gavinp
2012/07/14 19:05:06
This doesn't work; you're setting *buf and *buf_si
frankwang
2012/07/16 01:44:05
Done.
| |
61 | |
62 } | |
63 | |
64 bool DuplicateResourceHandler::OnReadCompleted(int request_id, int bytes_read, | |
65 bool* defer) { | |
gavinp
2012/07/14 19:05:06
Once you've fixed the above, you'll want to read t
frankwang
2012/07/16 01:44:05
Done.
| |
66 if (!next_handler_->OnReadCompleted(request_id, bytes_read, defer)) | |
67 return false; | |
68 | |
69 // Find hash of buffer, using previous hash as the seed (first seed is 0) | |
gavinp
2012/07/14 19:05:06
This does not work. MurmurHash3 is not incremental
| |
70 MurmurHash3_x86_32(read_buffer_->data(), bytes_read, start_seed_, &start_seed_ ); | |
71 | |
72 bytes_read_ += bytes_read; | |
73 return true; | |
74 } | |
75 | |
76 bool DuplicateResourceHandler::OnResponseCompleted( | |
77 int request_id, | |
78 const net::URLRequestStatus& status, | |
79 const std::string& security_info) { | |
80 | |
81 // Hash url into the resource to see whether it is from the same or different origin | |
82 uint32 hashed_with_url; | |
83 const char* url = request_->url().spec().c_str(); | |
84 MurmurHash3_x86_32(url, strlen(url), start_seed_, &hashed_with_url); | |
85 | |
86 // This boolean answers whether we found resource regardless of origin | |
87 const bool did_we_find_resource = GetSetOfHashes()->find(start_seed_) != GetSe tOfHashes()->end(); | |
88 // This boolean checks whether we found a resource from the same origin as one previously seen | |
89 const bool did_we_find_resource_same_origin = | |
gavinp
2012/07/14 19:05:06
Origin isn't the right term to use here, for http:
frankwang
2012/07/16 01:44:05
Done.
| |
90 GetSetOfHashesWithURL()->find(hashed_with_url) != GetSetOfHashesWithURL()- >end(); | |
91 | |
92 // If we found the resource, classify whether it is from the same origin or di fferent | |
93 if (did_we_find_resource) { | |
94 // If it is the same origin, it is a hit on both caches | |
95 if (did_we_find_resource_same_origin) { | |
96 UMA_HISTOGRAM_BOOLEAN("Duplicate.ProposedCache.HitRate", true); | |
gavinp
2012/07/14 19:05:06
Each of these macro invocations has a global varia
frankwang
2012/07/16 01:44:05
Done.
| |
97 UMA_HISTOGRAM_BOOLEAN("Duplicate.CurrentCache.HitRate", true); | |
gavinp
2012/07/14 19:05:06
You're not really measuring the current cache here
frankwang
2012/07/16 01:44:05
Done.
| |
98 } else { | |
99 // If it is a different origin (interesting case), it hits on the | |
100 // proposed cache not the current cache | |
101 UMA_HISTOGRAM_BOOLEAN("Duplicate.ProposedCache.HitRate", true); | |
102 UMA_HISTOGRAM_BOOLEAN("Duplicate.CurrentCache.HitRate", false); | |
103 // Record kilobytes (log base 10) missed because we are caching based on o rigin instead of resource | |
104 UMA_HISTOGRAM_COUNTS("Duplicate.SizeKB.Miss.CurrentCache", log10(bytes_rea d_)); | |
frankwang
2012/07/14 02:13:59
I changed this locally to "Duplicate.Size.Miss.Cur
gavinp
2012/07/14 19:05:06
UMA_HISTOGRAM_COUNTS is already exponentially binn
frankwang
2012/07/16 01:44:05
Done.
| |
105 // Record resource type for missed resource | |
106 UMA_HISTOGRAM_ENUMERATION("Duplicate.ResourceType", resource_type_, Resour ceType::LAST_TYPE); | |
107 GetSetOfHashesWithURL()->insert(hashed_with_url); | |
108 } | |
109 } else { | |
110 // We did not see the resource so it is a miss on both caches | |
111 UMA_HISTOGRAM_BOOLEAN("Duplicate.ProposedCache.HitRate", false); | |
112 UMA_HISTOGRAM_BOOLEAN("Duplicate.CurrentCache.HitRate", false); | |
113 GetSetOfHashes()->insert(start_seed_); | |
114 GetSetOfHashesWithURL()->insert(hashed_with_url); | |
115 } | |
116 | |
117 bytes_read_ = 0; | |
118 read_buffer_ = NULL; | |
119 return next_handler_->OnResponseCompleted(request_id, status, security_info); | |
120 } | |
121 | |
122 } //namespace content | |
OLD | NEW |