Platform/Memory Reporting: Difference between revisions

Replaced content with "The contents of this page have moved to [https://developer.mozilla.org/en-US/docs/Mozilla/Performance/Memory_reporting MDN]."
(Replaced content with "The contents of this page have moved to [https://developer.mozilla.org/en-US/docs/Mozilla/Performance/Memory_reporting MDN].")
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
'''''tl;dr:  You should read this document before writing a memory reporter.  And please ask nnethercote to co-review any memory reporter patch.'''''
The contents of this page have moved to [https://developer.mozilla.org/en-US/docs/Mozilla/Performance/Memory_reporting MDN].
 
Mozilla code has infrastructure that lets different parts of the code report on their memory usage.  This is most obviously used in about:memory and telemetry.  This document describes things that you should know when writing a memory reporter.
 
== Memory Reporters ==
 
A memory reporter makes one or more memory measurements (a.k.a. reports).
 
Each reporter implements a <tt>collectReports</tt> function which takes a <tt>nsIMemoryReporterCallback</tt> argument;  for each measurement the reporter must pass in several values, including:
 
* a path (which identifies the report);
* an amount (the most important thing);
* a unit (most commonly bytes, but sometimes a unitless count or percentage);
* a description of what is measured.
 
See the [https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIMemoryReporter nsIMemoryReporter documentation] and [http://mxr.mozilla.org/mozilla-central/source/xpcom/base/nsIMemoryReporter.idl nsIMemoryReporter.idl] for full details.
 
== Making Measurements ==
 
nsIMemoryReporter provides the high-level interface for a memory reporter, but the heart of a memory reporter is the measurement of the "amount".
 
=== Two Ways to Measure ===
 
Memory reporters can be divided into the two following kinds.
 
* ''Traversal-based'' reporters traverse one or more data structures and measure the size of all the allocated blocks in the data structure.
 
* ''Counter-based'' reporters maintain a counter that is incremented on each relevant allocation and decremented on each relevant deallocation.
 
Traversal-based reporters are preferable, for the following reasons.
 
* They are less error-prone (we've had multiple bugs in the past with counter-based reporters). 
 
* The cost of reporting isn't incurred unless the memory reporter is queried.
 
* They provide more information to DMD, which is a tool that helps keep about:memory's "heap-unclassified" number low (see below for more details).
 
Sometimes counter-based reporters are unavoidable, particularly when writing memory reporters for third-party code that shouldn't be modified.
 
=== A Simple Example ===
 
Imagine a simple string class with the following data fields:
 
  class MyString {
    private:
      char *mBuffer;    // heap-allocated
      size_t mLen;
 
    // ... methods ...
  }
 
Here are what the measurement functions (yes, functions) should look like for this class.
 
    size_t MyString::SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const {
        return aMallocSizeOf(mBuffer);
    }
    size_t MyString::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const {
        return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
    }
 
[http://mxr.mozilla.org/mozilla-central/source/mfbt/MemoryReporting.h mfbt/MemoryReporting.h] defines <tt>mozilla::MallocSizeOf</tt> as follows:
 
  typedef size_t (*MallocSizeOf)(const void* p);
 
Functions with this signature measure the size of <tt>p</tt> by asking the heap allocator how big it is (via <tt>moz_malloc_usable_size</tt>).
 
All this is probably not what you'd expect, but the above functions have the following crucial features.
 
* They let you measure the <tt>MyString</tt> object itself or not as necessary. This is important because sometimes an object might be embedded in another object that is measured separately.  And the names make it clear exactly what is being measured.
 
* On platforms that allow it, they count the actual memory in use, including "slop" bytes caused by the heap allocator rounding up request sizes (a.k.a. internal fragmentation).  If slop bytes aren't measured they'll end up in about:memory's <tt>heap-unclassified</tt> entry, which is bad.
 
* The size is not computed analytically.  For example, <tt>sizeof</tt> doesn't appear anywhere.  This is a good thing, because computing sizes analytically doesn't count slop, and it is much more error-prone than using <tt>moz_malloc_usable_size</tt>.
 
* They are flexible and integrate well with DMD.  The <tt>aMallocSizeOf</tt> parameter allows <tt>nsMallocSizeOfFun</tt> functions with DMD-specific hooks to be passed in when they are used by memory reporters, but functions without such hooks (such as <tt>moz_malloc_size_of</tt>) can also be passed in when they are used in other circumstances.
 
Some other things to note:
 
* Please use <tt>size_t</tt> for the sizes in functions like this. Although <tt>nsIMemoryReporter</tt> uses <tt>int64_t</tt>, this is because (a) you can't use <tt>size_t</tt> in IDL, and (b) not every "amount" is a bytes measurement.  So it's best to mostly use <tt>size_t</tt> and convert to <tt>int64_t</tt> as late as possible.
 
* You don't always need both <tt>SizeOfExcludingThis</tt> and <tt>SizeOfIncludingThis</tt>.  Implement one or both as needed.  If you have both, <tt>SizeOfExcludingThis</tt> is the interesting one;  <tt>SizeOfIncludingThis</tt> always has the same basic form.
 
And here's how you'd write a memory reporter if there was a single global MyString object.
 
  MyString *gMyString;
 
  class MyStringReporter MOZ_FINAL : public MemoryUniReporter
  {
    MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)
   
  public:
    NS_DECL_ISUPPORTS
   
    NS_IMETHOD
    CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData)
    {
      // BTW: If gMyString wasn't a pointer, you'd use
      // |gMyString.SizeOfExcludingThis(MallocSizeOf)| instead.
      return MOZ_COLLECT_REPORT(
        "explicit/mystring", KIND_HEAP, UNITS_BYTES,
        gMyString->SizeOfIncludingThis(MallocSizeOf),
        "Memory used for MyString.");
    }
  };
 
  NS_IMPL_ISUPPORTS1(MyStringReporter, nsIMemoryReporter)
 
Note that <tt>MOZ_DEFINE_MALLOC_SIZE_OF</tt> defines a function of type <tt>nsMallocSizeOfFun</tt> that is specific to this memory reporter (and will be identified as such in DMD's output).
 
=== An Example Involving Inheritance ===
 
Things are a little trickier when inheritance is involved.  An example:
 
  class B {
    virtual foo() { ... }
 
    virtual size_t
      SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const
    {
        return ... // measure things pointed to by B-specific fields
    }
 
    virtual size_t
      SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const
    {
        return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
    }
   
    // data members
  };
 
  class D : public B {
    virtual foo() { ... }
 
    virtual size_t
      SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const MOZ_OVERRIDE
    {
        size_t n = B::SizeOfExcludingThis(aMallocSizeOf);
        n += ...  // measure things pointed to by D-specific fields
        return n;
    }
   
    virtual size_t
      SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const MOZ_OVERRIDE
    {
        return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
    }
   
    // data members
  };
 
Things to note about <tt>SizeOfExcludingThis</tt>.
 
* If <tt>D</tt> has data members that point to other objects, then <tt>D::SizeOfExcludingThis</tt> must override <tt>B::SizeOfExcludingThis</tt>.  Otherwise, if we have a <tt>B*</tt> pointer that actually points to a <tt>D</tt> object, we'll end up calling <tt>B::SizeOfExcludingThis</tt> instead of <tt>D::SizeOfExcludingThis</tt>, and thus we'll fail to measure any objects that <tt>D</tt>'s fields point to.  <tt>D::SizeOfExcludingThis</tt> must also call <tt>B::SizeOfExcludingThis</tt>, to ensure that any objects pointed to by fields inherited from <tt>B</tt> are measured.
 
* However, if <tt>D</tt> does not have any of its own fields that point to other objects, then <tt>D::SizeOfExcludingThis</tt> is not necessary.
 
* Alternatively, if <tt>D</tt> does have fields that point to other objects but you don't want to measure them because they're insignificant, then <tt>D::SizeOfExcludingThis</tt> is again not necessary.  But it's a good idea to add a comment indicating this decision not to measure, and the names of the unmeasured fields.  The obvious place for this comment is within <tt>D</tt>, but if <tt>B</tt> has many sub-classes like that, it might be easier to put the comment above <tt>B::SizeOfIncludingThis</tt>.
 
Things to note about <tt>SizeOfIncludingThis</tt>.
 
* <tt>D::SizeOfIncludingThis</tt> is identical to <tt>B::SizeOfIncludingThis</tt>.  You might think that <tt>D</tt> could just inherit <tt>SizeOfIncludingThis</tt> from <tt>B</tt>.  In some cases this works, but there's a lot of C++ subtlety involving the <tt>aMallocSizeOf(this)</tt> and whether <tt>this</tt> actually points to the start of the allocated object or somewhere in it's middle.  This is doubly true if <tt>D</tt> features multiple inheritance.  And if <tt>aMallocSizeOf</tt> is passed a pointer that doesn't point to the start of an object it will give bogus results or even crash.  So, if in doubt, define <tt>D::SizeOfIncludingThis</tt> and you should be safe.
 
* The <tt>MOZ_OVERRIDE</tt> annotations say that <tt>D</tt>'s methods override the corresponding methods in <tt>B</tt>.  On supporting compilers it gets expanded to the <tt>override</tt> keyword.  It helps prevent accidentally having small differences in the function signatures (e.g. forgetting a <tt>const</tt>) that prevent the overriding from happening.
 
=== Other Considerations ===
 
A number of the existing basic data structures already have <tt>SizeOf{In,Ex}cludingThis</tt> functions, e.g. <tt>nsTArray</tt> and <tt>nsTHashtable</tt>.  <tt>nsTHashtable</tt>'s functions take an extra argument which is a pointer to a function that measures the memory usage of any structures pointed to by entries in the hash table.
 
Sometimes you may need variations on the above forms.  For example, if you have a function that just measures one member <tt>Foo</tt> of an object, it might be called <tt>SizeOfFoo</tt>.  Try to make the names descriptive enough that it's clear what's being measured.
 
Sometimes you might want to split the measurements of an object into two or more numbers, e.g. because you want to show them separately in about:memory.  In this case it's often clearer to *increment* the numbers rather than *assigning* to them, especially if you're measuring multiple entities and summing their measurements.  For example:
 
  void FooBar::AddSizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf,
                                      size_t *foo, size_t* bar) const
  {
      *foo += ...;
      *bar += ...;
  }
 
Alternatively, you could create a struct:
 
  struct FooBarSizes {
      size_t foo;
      size_t bar;
      FooBarSizes() { mozilla::PodZero(this); }
  }
  void FooBar::AddSizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf,
                                  FooBarSizes *sizes) const
  {
      sizes->foo += ...;
      sizes->bar += ...;
  }
 
Note the <tt>Add</tt> prefix that makes this incrementing behaviour clear.  Obviously, if you increment you have to zero the values at some point.  When using a struct, its constructor is the obvious place for this.
 
You could even put the <tt>nsMallocSizeOfFun</tt> in <tt>FooBarSizes</tt> to reduce the number of arguments.
 
Sometimes it's difficult and/or not worth measuring every member of an object of in a <tt>Foo::SizeOfExcludingThis</tt> method.  This is ok, but it's worth adding a comment explaining this to the method.
 
Occasionally you do want to compute sizes analytically instead of using <tt>moz_malloc_size_of</tt>.  In that case, use <tt>ComputedSizeOf</tt> as the prefix for the function name.
 
It's important that no memory is measured twice;  this can lead to very strange results in about:memory.  Avoiding double measurement is easy in tree-like structures.  In graph-like structures (where an object might be pointed to by more than one other object) it gets more difficult, and might even require some way to mark objects that have been counted (and then a way to unmark them once the measurement is complete).
 
It's easy to get confused as to whether you should use <tt>SizeOfIncludingThis</tt> or <tt>SizeOfExcludingThis</tt>.  A good rule of thumb:  if you're calling the method via a pointer, you usually want the former, otherwise you want the latter.  For example:
 
  foo->SizeOfIncludingThis()  // yes
  foo.SizeOfExcludingThis()  // yes
 
  foo.SizeOfIncludingThis()  // no
  foo->SizeOfExcludingThis()  // no
 
If you write a memory reporter, please get two people to review it: (a) someone who knows the data structures being measured, and (b) nnethercote, who can check for all the things covered by this document.  Thanks!
 
== DMD ==
 
DMD is a tool with two purposes.
 
* It identifies places in the code that allocate memory but do not have memory reporters for that memory.  This helps drive about:memory's <tt>heap-unclassified</tt> number down.
 
* It detects certain defects in existing heap memory reporters:  if non-heap memory is reported, or if a heap block is partially reported or double-reported.
 
DMD is absolutely crucial;  these things cannot be done without it.  That's why the integration of DMD and memory reporters is so important and thus mentioned multiple times above.
 
See [[Performance/MemShrink/DMD]] for instructions on how to run DMD.
Confirmed users
1,345

edits