Skip to content

Measure cache memory usage - #20391

Merged
bors-servo merged 1 commit into
servo:masterfrom
modal17:iss19251
Apr 2, 2018
Merged

bors-servo merged 1 commit into
servo:masterfrom
modal17:iss19251

Conversation

@modal17

@modal17 modal17 commented Mar 22, 2018 •

Copy link
Copy Markdown
  • make the memory cache data structure derive MallocSizeOf
  • 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)
  • use run_with_memory_reporting in the network code to register a memory reporter for that thread
  • when a message from the memory profiler arrives, create a report that includes that size of the public and private http caches

  • ./mach build -d does not report any errors
  • ./mach test-tidy does not report any errors
  • These changes fix Measure cache memory usage #19251 (github issue number if applicable).
  • There are tests for these changes OR
  • These changes do not require tests because _____

This change is Reviewable

@highfive

Copy link
Copy Markdown

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.

@highfive

Copy link
Copy Markdown

Heads up! This PR modifies the following files:

  • @paulrouget: components/servo/lib.rs
  • @fitzgen: components/profile_traits/mem.rs
  • @KiChjang: components/net/Cargo.toml, components/net/resource_thread.rs, components/net/lib.rs, components/net/tests/resource_thread.rs, components/net/http_cache.rs and 3 more

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Mar 22, 2018
@modal17 modal17 changed the title Iss19251 Measure cache memory usage Mar 22, 2018
@jdm jdm assigned jdm and unassigned pcwalton Mar 22, 2018

@jdm jdm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great work! Thank you for tackling this :)

}
}

impl<T: MallocSizeOf> MallocSizeOf for std::sync::Mutex<T> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread components/malloc_size_of/lib.rs Outdated

impl MallocSizeOf for hyper::header::Headers {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
let mut t = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.iter().fold(0, |acc, x| acc + x.size_of(ops))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Comment thread components/malloc_size_of/lib.rs Outdated
}
}

impl<T> MallocSizeOf for std::sync::mpsc::Sender<T> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread components/malloc_size_of/lib.rs Outdated
}
}

impl MallocSizeOf for hyper_serde::Serde<hyper::header::ContentType> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do:

impl<T: MallocSizeOf> MallocSizeOf for hyper_serde::Serde<T> {
    self.0.size_of(ops)
}

Comment thread components/malloc_size_of/lib.rs Outdated
* nanos: i32,
* }
*/
impl MallocSizeOf for time::Duration {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread components/malloc_size_of/lib.rs Outdated
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@modal17 modal17 Mar 26, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread components/net/resource_thread.rs Outdated
// 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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're already checking this two lines earlier.

Comment thread components/net/resource_thread.rs Outdated
}
}

/// False if report failed to be made

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no return value any more.

@jdm jdm added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. S-needs-rebase There are merge conflict errors. and removed S-awaiting-review There is new code that needs to be reviewed. labels Mar 23, 2018
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Mar 29, 2018
*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();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why were these lines rearranged, out of curiosity?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jdm jdm added S-needs-squash Some (or all) of the commits in the PR should be combined. and removed S-awaiting-review There is new code that needs to be reviewed. labels Mar 29, 2018

@jdm jdm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this needs now is a rebase against master and a squash.

@modal17

modal17 commented Mar 29, 2018

Copy link
Copy Markdown
Author

Pushing one last, very minor commit then will get onto rebase and squash!

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Mar 29, 2018
@jdm jdm removed the S-awaiting-review There is new code that needs to be reviewed. label Mar 30, 2018
@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Mar 30, 2018
@jdm jdm removed S-needs-rebase There are merge conflict errors. S-needs-squash Some (or all) of the commits in the PR should be combined. labels Mar 31, 2018
@jdm

jdm commented Mar 31, 2018

Copy link
Copy Markdown
Member

@bors-servo r+

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit a159d22 has been approved by jdm

@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. labels Mar 31, 2018
@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit a159d22 with merge e9a21fa...

@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-tests-failed The changes caused existing tests to fail. labels Apr 1, 2018
@jdm

jdm commented Apr 1, 2018

Copy link
Copy Markdown
Member

@bors-servo r+

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit 6c715a6 has been approved by jdm

@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. labels Apr 1, 2018
@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit 6c715a6 with merge 71ca1d9...

bors-servo pushed a commit that referenced this pull request Apr 1, 2018
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 -->
@bors-servo

Copy link
Copy Markdown
Contributor

💔 Test failed - linux-dev

@highfive highfive added S-tests-failed The changes caused existing tests to fail. and removed S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. labels Apr 1, 2018
@CYBAI

CYBAI commented Apr 1, 2018

Copy link
Copy Markdown
Member
Checking files for tidiness...

./components/net/resource_thread.rs:165: trailing whitespace

Running the dependency licensing lint...

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.
@highfive highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-tests-failed The changes caused existing tests to fail. labels Apr 1, 2018
@jdm

jdm commented Apr 2, 2018

Copy link
Copy Markdown
Member

@bors-servo r+

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit af445a3 has been approved by jdm

@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. labels Apr 2, 2018
@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit af445a3 with merge 8f226f8...

bors-servo pushed a commit that referenced this pull request Apr 2, 2018
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 -->
@bors-servo

Copy link
Copy Markdown
Contributor

☀️ 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
Approved by: jdm
Pushing 8f226f8 to master...

@bors-servo
bors-servo merged commit af445a3 into servo:master Apr 2, 2018
@highfive highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Apr 2, 2018
bors-servo pushed a commit that referenced this pull request Apr 2, 2018
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 -->
@modal17
modal17 deleted the iss19251 branch April 4, 2018 23:41
@modal17
modal17 restored the iss19251 branch April 4, 2018 23:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Measure cache memory usage

6 participants