Measure selectors.

This patch adds measurement of Selectors within StyleRule. This requires
exposing the pointer within ThinArc.

The patch also adds measurement of the several CssRule variants, in order to
measure nested CssRules (and PropertyDeclarationBlocks) within them:
DocumentRule, MediaRule, PageRule, SupportsRule.
This commit is contained in:
Nicholas Nethercote 2017-09-07 13:24:40 +10:00
parent 812cac78f0
commit 61877b7316
8 changed files with 95 additions and 15 deletions

View file

@ -423,7 +423,6 @@ impl<Impl: SelectorImpl> Selector<Impl> {
)) ))
} }
/// Returns an iterator over this selector in matching order (right-to-left). /// Returns an iterator over this selector in matching order (right-to-left).
/// When a combinator is reached, the iterator will return None, and /// When a combinator is reached, the iterator will return None, and
/// next_sequence() may be called to continue to the next sequence. /// next_sequence() may be called to continue to the next sequence.
@ -494,6 +493,11 @@ impl<Impl: SelectorImpl> Selector<Impl> {
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.0.slice.len() self.0.slice.len()
} }
/// Returns the address on the heap of the ThinArc for memory reporting.
pub fn thin_arc_heap_ptr(&self) -> *const ::std::os::raw::c_void {
self.0.heap_ptr()
}
} }
#[derive(Clone)] #[derive(Clone)]

View file

@ -676,6 +676,12 @@ impl<H: 'static, T: 'static> ThinArc<H, T> {
// Forward the result. // Forward the result.
result result
} }
/// Returns the address on the heap of the ThinArc itself -- not the T
/// within it -- for memory reporting.
pub fn heap_ptr(&self) -> *const c_void {
self.ptr as *const ArcInner<T> as *const c_void
}
} }
impl<H, T> Deref for ThinArc<H, T> { impl<H, T> Deref for ThinArc<H, T> {

View file

@ -13,7 +13,7 @@ use servo_arc::Arc;
use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt; use std::fmt;
use style_traits::{ToCss, ParseError, StyleParseError}; use style_traits::{ToCss, ParseError, StyleParseError};
use stylesheets::CssRules; use stylesheets::{CssRules, MallocSizeOfFn, MallocSizeOfWithGuard};
use values::specified::url::SpecifiedUrl; use values::specified::url::SpecifiedUrl;
#[derive(Debug)] #[derive(Debug)]
@ -27,6 +27,15 @@ pub struct DocumentRule {
pub source_location: SourceLocation, pub source_location: SourceLocation,
} }
impl DocumentRule {
/// Measure heap usage.
pub fn malloc_size_of_children(&self, guard: &SharedRwLockReadGuard,
malloc_size_of: MallocSizeOfFn) -> usize {
// Measurement of other fields may be added later.
self.rules.read_with(guard).malloc_size_of_children(guard, malloc_size_of)
}
}
impl ToCssWithGuard for DocumentRule { impl ToCssWithGuard for DocumentRule {
fn to_css<W>(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result fn to_css<W>(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result
where W: fmt::Write { where W: fmt::Write {

View file

@ -12,7 +12,7 @@ use servo_arc::Arc;
use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt; use std::fmt;
use style_traits::ToCss; use style_traits::ToCss;
use stylesheets::CssRules; use stylesheets::{CssRules, MallocSizeOfFn, MallocSizeOfWithGuard};
/// An [`@media`][media] urle. /// An [`@media`][media] urle.
/// ///
@ -27,6 +27,15 @@ pub struct MediaRule {
pub source_location: SourceLocation, pub source_location: SourceLocation,
} }
impl MediaRule {
/// Measure heap usage.
pub fn malloc_size_of_children(&self, guard: &SharedRwLockReadGuard,
malloc_size_of: MallocSizeOfFn) -> usize {
// Measurement of other fields may be added later.
self.rules.read_with(guard).malloc_size_of_children(guard, malloc_size_of)
}
}
impl ToCssWithGuard for MediaRule { impl ToCssWithGuard for MediaRule {
// Serialization of MediaRule is not specced. // Serialization of MediaRule is not specced.
// https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSMediaRule // https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSMediaRule

View file

@ -116,21 +116,40 @@ impl MallocSizeOfWithGuard for CssRule {
malloc_size_of: MallocSizeOfFn malloc_size_of: MallocSizeOfFn
) -> usize { ) -> usize {
match *self { match *self {
// Not all fields are currently fully measured. Extra measurement
// may be added later.
CssRule::Namespace(_) => 0,
// We don't need to measure ImportRule::stylesheet because we measure
// it on the C++ side in the child list of the ServoStyleSheet.
CssRule::Import(_) => 0,
CssRule::Style(ref lock) => { CssRule::Style(ref lock) => {
lock.read_with(guard).malloc_size_of_children(guard, malloc_size_of) lock.read_with(guard).malloc_size_of_children(guard, malloc_size_of)
}, },
// Measurement of these fields may be added later.
CssRule::Import(_) => 0, CssRule::Media(ref lock) => {
CssRule::Media(_) => 0, lock.read_with(guard).malloc_size_of_children(guard, malloc_size_of)
},
CssRule::FontFace(_) => 0, CssRule::FontFace(_) => 0,
CssRule::FontFeatureValues(_) => 0, CssRule::FontFeatureValues(_) => 0,
CssRule::CounterStyle(_) => 0, CssRule::CounterStyle(_) => 0,
CssRule::Keyframes(_) => 0,
CssRule::Namespace(_) => 0,
CssRule::Viewport(_) => 0, CssRule::Viewport(_) => 0,
CssRule::Supports(_) => 0, CssRule::Keyframes(_) => 0,
CssRule::Page(_) => 0,
CssRule::Document(_) => 0, CssRule::Supports(ref lock) => {
lock.read_with(guard).malloc_size_of_children(guard, malloc_size_of)
},
CssRule::Page(ref lock) => {
lock.read_with(guard).malloc_size_of_children(guard, malloc_size_of)
},
CssRule::Document(ref lock) => {
lock.read_with(guard).malloc_size_of_children(guard, malloc_size_of)
},
} }
} }
} }

View file

@ -12,6 +12,7 @@ use servo_arc::Arc;
use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt; use std::fmt;
use style_traits::ToCss; use style_traits::ToCss;
use stylesheets::{MallocSizeOf, MallocSizeOfFn};
/// A [`@page`][page] rule. /// A [`@page`][page] rule.
/// ///
@ -30,6 +31,15 @@ pub struct PageRule {
pub source_location: SourceLocation, pub source_location: SourceLocation,
} }
impl PageRule {
/// Measure heap usage.
pub fn malloc_size_of_children(&self, guard: &SharedRwLockReadGuard,
malloc_size_of: MallocSizeOfFn) -> usize {
// Measurement of other fields may be added later.
self.block.read_with(guard).malloc_size_of_children(malloc_size_of)
}
}
impl ToCssWithGuard for PageRule { impl ToCssWithGuard for PageRule {
/// Serialization of PageRule is not specced, adapted from steps for /// Serialization of PageRule is not specced, adapted from steps for
/// StyleRule. /// StyleRule.

View file

@ -12,7 +12,7 @@ use servo_arc::Arc;
use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt; use std::fmt;
use style_traits::ToCss; use style_traits::ToCss;
use stylesheets::{MallocSizeOf, MallocSizeOfFn, MallocSizeOfWithGuard}; use stylesheets::{MallocSizeOf, MallocSizeOfFn, MallocSizeOfVec, MallocSizeOfWithGuard};
/// A style rule, with selectors and declarations. /// A style rule, with selectors and declarations.
#[derive(Debug)] #[derive(Debug)]
@ -47,8 +47,22 @@ impl MallocSizeOfWithGuard for StyleRule {
guard: &SharedRwLockReadGuard, guard: &SharedRwLockReadGuard,
malloc_size_of: MallocSizeOfFn malloc_size_of: MallocSizeOfFn
) -> usize { ) -> usize {
// Measurement of other fields may be added later. let mut n = 0;
self.block.read_with(guard).malloc_size_of_children(malloc_size_of)
// We may add measurement of things hanging off the embedded Components
// later.
n += self.selectors.0.malloc_shallow_size_of_vec(malloc_size_of);
for selector in self.selectors.0.iter() {
// It's safe to measure this ThinArc directly because it's the
// "primary" reference. (The secondary references are on the
// Stylist.)
let ptr = selector.thin_arc_heap_ptr();
n += unsafe { (malloc_size_of.0)(ptr) };
}
n += self.block.read_with(guard).malloc_size_of_children(malloc_size_of);
n
} }
} }

View file

@ -13,7 +13,7 @@ use servo_arc::Arc;
use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt; use std::fmt;
use style_traits::{ToCss, ParseError, StyleParseError}; use style_traits::{ToCss, ParseError, StyleParseError};
use stylesheets::{CssRuleType, CssRules}; use stylesheets::{CssRuleType, CssRules, MallocSizeOfFn, MallocSizeOfWithGuard};
/// An [`@supports`][supports] rule. /// An [`@supports`][supports] rule.
/// ///
@ -30,6 +30,15 @@ pub struct SupportsRule {
pub source_location: SourceLocation, pub source_location: SourceLocation,
} }
impl SupportsRule {
/// Measure heap usage.
pub fn malloc_size_of_children(&self, guard: &SharedRwLockReadGuard,
malloc_size_of: MallocSizeOfFn) -> usize {
// Measurement of other fields may be added later.
self.rules.read_with(guard).malloc_size_of_children(guard, malloc_size_of)
}
}
impl ToCssWithGuard for SupportsRule { impl ToCssWithGuard for SupportsRule {
fn to_css<W>(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result fn to_css<W>(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result
where W: fmt::Write { where W: fmt::Write {