Repository navigation
Measure cache memory usage - #20391
Conversation
|
Thanks for the pull request, and welcome! The Servo team is excited to review your changes, and you should hear from @pcwalton (or someone else) soon. |
|
Heads up! This PR modifies the following files:
|
jdm
left a comment
There was a problem hiding this comment.
This is great work! Thank you for tackling this :)
| } | ||
| } | ||
|
|
||
| impl<T: MallocSizeOf> MallocSizeOf for std::sync::Mutex<T> { |
There was a problem hiding this comment.
Let's add a comment about why this implementation makes sense:
If a mutex is stored directly as a member of a data type that is being measured, it is the unique owner of its contents and deserves to be measured. If a mutex is stored inside of an Arc value as a member of a data type that is being measure, the Arc will not be automatically measured so there is no risk of overcounting the mutex's contents.
|
|
||
| impl MallocSizeOf for hyper::header::Headers { | ||
| fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { | ||
| let mut t = 0; |
There was a problem hiding this comment.
self.iter().fold(0, |acc, x| acc + x.size_of(ops))
There was a problem hiding this comment.
For this, I followed the suggestion for when we could not implement MallocSizeOf for HeaderView! But, on quick glance, it does look like I could fit it with fold!
| } | ||
| } | ||
|
|
||
| impl<T> MallocSizeOf for std::sync::mpsc::Sender<T> { |
There was a problem hiding this comment.
This isn't correct, unfortunately. MallocSizeOf for T is supposed to calculate the heap size of the members of T, not the size of T itself. Since we don't know anything about the internals of mpsc::Sender, let's remove this implementation and use #[ignore_malloc_size_of = "Can't measure mpsc::Sender"] instead.
| } | ||
| } | ||
|
|
||
| impl MallocSizeOf for hyper_serde::Serde<hyper::header::ContentType> { |
There was a problem hiding this comment.
Let's do:
impl<T: MallocSizeOf> MallocSizeOf for hyper_serde::Serde<T> {
self.0.size_of(ops)
}| * nanos: i32, | ||
| * } | ||
| */ | ||
| impl MallocSizeOf for time::Duration { |
There was a problem hiding this comment.
Since MallocSizeOf measures heap usage of members, not the size of the value itself, let's use malloc_size_of_is_0!(time::Duration) and malloc_size_of_is_0!(time::Tm).
| impl<'a> MallocSizeOf for hyper::header::HeaderView<'a> { | ||
| fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { | ||
| // Close enough? | ||
| self.name().size_of(ops) + self.value_string().size_of(ops) |
There was a problem hiding this comment.
This won't do what we want, unfortunately. Instead, let's do self.0.size_of(ops) and implement MallocSizeOf for Unicase<T> where T: MallocSizeOf, and add self.1.raw_mut().size_of(ops).
There was a problem hiding this comment.
The compiler is complaining about private fields for HeaderView when attempting to directly use self.0 and self.1 - I remember trying to skirt this issue with the above.
Any advice would be helpful!!
There was a problem hiding this comment.
Ok, I understand better now. In that case, rather than implementing MallocSizeOf for HeaderView, we can change the implementation for Headers to this:
for x in self.iter() {
let name = x.name();
let raw = self.get_raw(name);
t += raw.size_of(ops);
}This will undercount, but it's better than nothing!
| awaiting_body: Arc<Mutex<Vec<Sender<Data>>>> | ||
| } | ||
|
|
||
| impl MallocSizeOf for CachedResource { |
There was a problem hiding this comment.
What if we create a struct Measurable<T: MallocSizeOf>(pub T) that derefs to the inner value in the malloc_size_of crate? Then we could do this:
#[derive(Clone, MallocSizeOf)]
struct MeasurableCachedResource {
metadata: CachedMetadata,
location_url: Option<Result<ServoUrl, String>>,
https_state: HttpsState,
status: Option<StatusCode>,
raw_status: Option<(u16, Vec<u8>)>,
url_list: Vec<ServoUrl>,
expires: Duration,
last_validated: Tm,
}
struct CachedResource {
request_headers: Arc<Mutex<Headers>>,
body: Arc<Mutex<ResponseBody>>,
aborted: Arc<AtomicBool>,
awaiting_body: Arc<Mutex<Vec<Sender<Data>>>>,
data: Measurable<MeasurableCachedResource>,
}This would make it easier to verify the manual implementation of size_of for CachedResource.
There was a problem hiding this comment.
So to access the measurable resource fields, the result of this would be something like, cachedresource.data.0.https_state or (*cachedresource.data).https_state if Deref is implemented for Measurable?
There was a problem hiding this comment.
Yeah, cachedresource.data.https_state should be enough. If anything needs to update the fields, you can implement DerefMut for Measurable as well.
| pub status: Option<(u16, Vec<u8>)> | ||
| } | ||
|
|
||
| impl MallocSizeOf for CachedMetadata { |
There was a problem hiding this comment.
I've filed #20401 about making it easier to deal with structures like this, but it would be nice to use the same Measurable pattern as CachedResource here.
| // If message is memory report, get the size_of of public and private http caches | ||
| if id == reporter_id { | ||
| if let Ok(msg) = data.to() { | ||
| if id == reporter_id { |
There was a problem hiding this comment.
We're already checking this two lines earlier.
| } | ||
| } | ||
|
|
||
| /// False if report failed to be made |
| *done_chan = None; | ||
| cached_resource.expires = get_response_expiry(&constructed_response); | ||
| cached_resource.data.expires = get_response_expiry(&constructed_response); | ||
| let mut stored_headers = cached_resource.data.metadata.headers.lock().unwrap(); |
There was a problem hiding this comment.
Why were these lines rearranged, out of curiosity?
There was a problem hiding this comment.
I got this error:
error[E0502]: cannot borrow `cached_resource.data` as mutable because it is also borrowed as immutable
--> components/net/http_cache.rs:644:17
|
627 | let mut stored_headers = cached_resource.data.metadata.headers.lock().unwrap();
| -------------------- immutable borrow occurs here
...
644 | cached_resource.data.expires = get_response_expiry(&constructed_response);
| ^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
645 | return Some(constructed_response);
646 | }
| - immutable borrow ends here
Maybe I could have called .clone() instead of moving it around?
There was a problem hiding this comment.
Ah, that makes sense. Cloning would not have been the right solution, since the original value would not have been updated. This solution seems fine to me.
|
Pushing one last, very minor commit then will get onto rebase and squash! |
|
@bors-servo r+ |
|
📌 Commit a159d22 has been approved by |
|
@bors-servo r+ |
|
📌 Commit 6c715a6 has been approved by |
Measure cache memory usage <!-- Please describe your changes on the following line: --> - [X] make the memory cache data structure derive MallocSizeOf - [X] add a new IpcReceiver argument to ResourceChannelManager::start that is used for listening to messages from the memory profiler (it must correspond with this sender) - [X] use run_with_memory_reporting in the network code to register a memory reporter for that thread - [X] when a message from the memory profiler arrives, create a report that includes that size of the public and private http caches --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [x] These changes fix #19251 (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/20391) <!-- Reviewable:end -->
|
💔 Test failed - linux-dev |
|
Made the memory cache data structure derive MallocSizeOf, along with manual size_of() implementations in malloc_size_of. Added a Measurable struct that acts as a container for fields size_of() can be called for. Added a new IpcReceiver used for listening to messages from the memory profiler, and used run_with_memory reporting to register a memory reporter in the thread. Now when a message from the memory profiler arrives, report includes sizes of public and private http caches. Updated test file.
|
@bors-servo r+ |
|
📌 Commit af445a3 has been approved by |
Measure cache memory usage <!-- Please describe your changes on the following line: --> - [X] make the memory cache data structure derive MallocSizeOf - [X] add a new IpcReceiver argument to ResourceChannelManager::start that is used for listening to messages from the memory profiler (it must correspond with this sender) - [X] use run_with_memory_reporting in the network code to register a memory reporter for that thread - [X] when a message from the memory profiler arrives, create a report that includes that size of the public and private http caches --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [x] These changes fix #19251 (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/20391) <!-- Reviewable:end -->
|
☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css1, mac-rel-css2, mac-rel-wpt1, mac-rel-wpt2, mac-rel-wpt3, mac-rel-wpt4, windows-msvc-dev |
Hide non-Servo heap measurement. This hides the changes in #20391 from Gecko builds that use the malloc_size_of crate. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/20504) <!-- Reviewable:end -->
./mach build -ddoes not report any errors./mach test-tidydoes not report any errorsThis change is