Bug 1910362 - Create new Nimbus helper r=aaronmt,ohorvath
[gecko.git] / xpcom / base / nsIMemoryReporter.idl
blob05b57015199885fd7aac37d62b55682b238e860b
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsISupports.idl"
8 %{C++
9 #include <stdio.h>
12 interface mozIDOMWindowProxy;
13 interface nsIRunnable;
14 interface nsISimpleEnumerator;
15 [ptr] native FILE(FILE);
18 * Memory reporters measure Firefox's memory usage. They are primarily used to
19 * generate the about:memory page. You should read
20 * https://developer.mozilla.org/en-US/docs/Mozilla/Performance/Memory_reporting
21 * before writing a memory reporter.
24 [scriptable, function, uuid(62ef0e1c-dbd6-11e3-aa75-3c970e9f4238)]
25 interface nsIHandleReportCallback : nsISupports
28 * The arguments to the callback are as follows.
31 * |process| The name of the process containing this reporter. Each
32 * reporter initially has "" in this field, indicating that it applies to the
33 * current process. (This is true even for reporters in a child process.)
34 * When a reporter from a child process is copied into the main process, the
35 * copy has its 'process' field set appropriately.
38 * |path| The path that this memory usage should be reported under. Paths
39 * are '/'-delimited, eg. "a/b/c".
41 * Each reporter can be viewed as representing a leaf node in a tree.
42 * Internal nodes of the tree don't have reporters. So, for example, the
43 * reporters "explicit/a/b", "explicit/a/c", "explicit/d/e", and
44 * "explicit/d/f" define this tree:
46 * explicit
47 * |--a
48 * | |--b [*]
49 * | \--c [*]
50 * \--d
51 * |--e [*]
52 * \--f [*]
54 * Nodes marked with a [*] have a reporter. Notice that the internal
55 * nodes are implicitly defined by the paths.
57 * Nodes within a tree should not overlap measurements, otherwise the
58 * parent node measurements will be double-counted. So in the example
59 * above, |b| should not count any allocations counted by |c|, and vice
60 * versa.
62 * All nodes within each tree must have the same units.
64 * If you want to include a '/' not as a path separator, e.g. because the
65 * path contains a URL, you need to convert each '/' in the URL to a '\'.
66 * Consumers of the path will undo this change. Any other '\' character
67 * in a path will also be changed. This is clumsy but hasn't caused any
68 * problems so far.
70 * The paths of all reporters form a set of trees. Trees can be
71 * "degenerate", i.e. contain a single entry with no '/'.
74 * |kind| There are three kinds of memory reporters.
76 * - HEAP: reporters measuring memory allocated by the heap allocator,
77 * e.g. by calling malloc, calloc, realloc, memalign, operator new, or
78 * operator new[]. Reporters in this category must have units
79 * UNITS_BYTES.
81 * - NONHEAP: reporters measuring memory which the program explicitly
82 * allocated, but does not live on the heap. Such memory is commonly
83 * allocated by calling one of the OS's memory-mapping functions (e.g.
84 * mmap, VirtualAlloc, or vm_allocate). Reporters in this category
85 * must have units UNITS_BYTES.
87 * - OTHER: reporters which don't fit into either of these categories.
88 * They can have any units.
90 * The kind only matters for reporters in the "explicit" tree;
91 * aboutMemory.js uses it to calculate "heap-unclassified".
94 * |units| The units on the reporter's amount. One of the following.
96 * - BYTES: The amount contains a number of bytes.
98 * - COUNT: The amount is an instantaneous count of things currently in
99 * existence. For instance, the number of tabs currently open would have
100 * units COUNT.
102 * - COUNT_CUMULATIVE: The amount contains the number of times some event
103 * has occurred since the application started up. For instance, the
104 * number of times the user has opened a new tab would have units
105 * COUNT_CUMULATIVE.
107 * The amount returned by a reporter with units COUNT_CUMULATIVE must
108 * never decrease over the lifetime of the application.
110 * - PERCENTAGE: The amount contains a fraction that should be expressed as
111 * a percentage. NOTE! The |amount| field should be given a value 100x
112 * the actual percentage; this number will be divided by 100 when shown.
113 * This allows a fractional percentage to be shown even though |amount| is
114 * an integer. E.g. if the actual percentage is 12.34%, |amount| should
115 * be 1234.
117 * Values greater than 100% are allowed.
120 * |amount| The numeric value reported by this memory reporter. Accesses
121 * can fail if something goes wrong when getting the amount.
124 * |description| A human-readable description of this memory usage report.
126 void callback(in ACString process, in AUTF8String path, in int32_t kind,
127 in int32_t units, in int64_t amount,
128 in AUTF8String description, in nsISupports data);
132 * An nsIMemoryReporter reports one or more memory measurements via a
133 * callback function which is called once for each measurement.
135 * An nsIMemoryReporter that reports a single measurement is sometimes called a
136 * "uni-reporter". One that reports multiple measurements is sometimes called
137 * a "multi-reporter".
139 * aboutMemory.js is the most important consumer of memory reports. It
140 * places the following constraints on reports.
142 * - All reports within a single sub-tree must have the same units.
144 * - There may be an "explicit" tree. If present, it represents
145 * non-overlapping regions of memory that have been explicitly allocated with
146 * an OS-level allocation (e.g. mmap/VirtualAlloc/vm_allocate) or a
147 * heap-level allocation (e.g. malloc/calloc/operator new). Reporters in
148 * this tree must have kind HEAP or NONHEAP, units BYTES.
150 * It is preferred, but not required, that report descriptions use complete
151 * sentences (i.e. start with a capital letter and end with a period, or
152 * similar).
154 [scriptable, uuid(92a36db1-46bd-4fe6-988e-47db47236d8b)]
155 interface nsIMemoryReporter : nsISupports
158 * Run the reporter.
160 * If |anonymize| is true, the memory reporter should anonymize any
161 * privacy-sensitive details in memory report paths, by replacing them with a
162 * string such as "<anonymized>". Anonymized memory reports may be sent
163 * automatically via crash reports or telemetry.
165 * The following things are considered privacy-sensitive.
167 * - Content domains and URLs, and information derived from them.
168 * - Content data, such as strings.
169 * - Details about content code, such as filenames, function names or stack
170 * traces.
171 * - Details about or data from the user's system, such as filenames.
172 * - Running apps.
174 * In short, anything that could identify parts of the user's browsing
175 * history is considered privacy-sensitive.
177 * The following thing are not considered privacy-sensitive.
179 * - Chrome domains and URLs.
180 * - Information about installed extensions.
182 void collectReports(in nsIHandleReportCallback callback,
183 in nsISupports data,
184 in boolean anonymize);
187 * Kinds. See the |kind| comment in nsIHandleReportCallback.
189 const int32_t KIND_NONHEAP = 0;
190 const int32_t KIND_HEAP = 1;
191 const int32_t KIND_OTHER = 2;
194 * Units. See the |units| comment in nsIHandleReportCallback.
196 const int32_t UNITS_BYTES = 0;
197 const int32_t UNITS_COUNT = 1;
198 const int32_t UNITS_COUNT_CUMULATIVE = 2;
199 const int32_t UNITS_PERCENTAGE = 3;
202 [scriptable, function, uuid(548b3909-c04d-4ca6-8466-b8bee3837457)]
203 interface nsIFinishReportingCallback : nsISupports
205 void callback(in nsISupports data);
208 [scriptable, function, uuid(1a80cd0f-0d9e-4397-be69-68ad28fe5175)]
209 interface nsIHeapAllocatedCallback : nsISupports
211 void callback(in int64_t bytesAllocated);
214 [scriptable, builtinclass, uuid(2998574d-8993-407a-b1a5-8ad7417653e1)]
215 interface nsIMemoryReporterManager : nsISupports
218 * Initialize.
220 [must_use] void init();
223 * Register the given nsIMemoryReporter. The Manager service will hold a
224 * strong reference to the given reporter, and will be responsible for freeing
225 * the reporter at shutdown. You may manually unregister the reporter with
226 * unregisterStrongReporter() at any point.
228 void registerStrongReporter(in nsIMemoryReporter reporter);
229 void registerStrongAsyncReporter(in nsIMemoryReporter reporter);
232 * Like registerReporter, but the Manager service will hold a weak reference
233 * via a raw pointer to the given reporter. The reporter should be
234 * unregistered before shutdown.
235 * You cannot register JavaScript components with this function! Always
236 * register your JavaScript components with registerStrongReporter().
238 void registerWeakReporter(in nsIMemoryReporter reporter);
239 void registerWeakAsyncReporter(in nsIMemoryReporter reporter);
242 * Unregister the given memory reporter, which must have been registered with
243 * registerStrongReporter(). You normally don't need to unregister your
244 * strong reporters, as nsIMemoryReporterManager will take care of that at
245 * shutdown.
247 void unregisterStrongReporter(in nsIMemoryReporter reporter);
250 * Unregister the given memory reporter, which must have been registered with
251 * registerWeakReporter().
253 void unregisterWeakReporter(in nsIMemoryReporter reporter);
256 * These functions should only be used for testing purposes.
258 void blockRegistrationAndHideExistingReporters();
259 void unblockRegistrationAndRestoreOriginalReporters();
260 void registerStrongReporterEvenIfBlocked(in nsIMemoryReporter aReporter);
263 * Get memory reports for the current process and all child processes.
264 * |handleReport| is called for each report, and |finishReporting| is called
265 * once all reports have been handled.
267 * |finishReporting| is called even if, for example, some child processes
268 * fail to report back. However, calls to this method will silently and
269 * immediately abort -- and |finishReporting| will not be called -- if a
270 * previous getReports() call is still in flight, i.e. if it has not yet
271 * finished invoking |finishReporting|. The silent abort is because the
272 * in-flight request will finish soon, and the caller would very likely just
273 * catch and ignore any error anyway.
275 * If |anonymize| is true, it indicates that the memory reporters should
276 * anonymize any privacy-sensitive data (see above).
278 void getReports(in nsIHandleReportCallback handleReport,
279 in nsISupports handleReportData,
280 in nsIFinishReportingCallback finishReporting,
281 in nsISupports finishReportingData,
282 in boolean anonymize);
285 * As above, but: If |minimizeMemoryUsage| is true, then each process will
286 * minimize its memory usage (see the |minimizeMemoryUsage| method) before
287 * gathering its report. If DMD is enabled and |DMDDumpIdent| is non-empty
288 * then write a DMD report to a file in the usual temporary directory (see
289 * |dumpMemoryInfoToTempDir| in |nsIMemoryInfoDumper|.)
291 [noscript] void
292 getReportsExtended(in nsIHandleReportCallback handleReport,
293 in nsISupports handleReportData,
294 in nsIFinishReportingCallback finishReporting,
295 in nsISupports finishReportingData,
296 in boolean anonymize,
297 in boolean minimizeMemoryUsage,
298 in AString DMDDumpIdent);
301 * As above, but if DMD is enabled and |DMDFile| is non-null then
302 * write a DMD report to that file and close it.
304 [noscript] void
305 getReportsForThisProcessExtended(in nsIHandleReportCallback handleReport,
306 in nsISupports handleReportData,
307 in boolean anonymize,
308 in FILE DMDFile,
309 in nsIFinishReportingCallback finishReporting,
310 in nsISupports finishReportingData);
313 * Called by an asynchronous memory reporter upon completion.
315 [noscript] void endReport();
318 * The memory reporter manager, for the most part, treats reporters
319 * registered with it as a black box. However, there are some
320 * "distinguished" amounts (as could be reported by a memory reporter) that
321 * the manager provides as attributes, because they are sufficiently
322 * interesting that we want external code (e.g. telemetry) to be able to rely
323 * on them.
325 * Note that these are not reporters and so getReports() does not look at
326 * them. However, distinguished amounts can be embedded in a reporter.
328 * Access to these attributes can fail. In particular, some of them are not
329 * available on all platforms.
331 * If you add a new distinguished amount, please update
332 * toolkit/components/aboutmemory/tests/test_memoryReporters.xul.
334 * |vsize| (UNITS_BYTES) The virtual size, i.e. the amount of address space
335 * taken up.
337 * |vsizeMaxContiguous| (UNITS_BYTES) The size of the largest contiguous
338 * block of virtual memory.
340 * |resident| (UNITS_BYTES) The resident size (a.k.a. RSS or physical memory
341 * used).
343 * |residentFast| (UNITS_BYTES) This is like |resident|, but on Mac OS
344 * |resident| can purge pages, which is slow. It also affects the result of
345 * |residentFast|, and so |resident| and |residentFast| should not be used
346 * together.
348 * |residentPeak| (UNITS_BYTES) The peak resident size.
350 * |residentUnique| (UNITS_BYTES) The unique set size (a.k.a. USS).
352 * |heapAllocated| (UNITS_BYTES) Memory mapped by the heap allocator.
354 * |heapOverheadFraction| (UNITS_PERCENTAGE) In the heap allocator, this is
355 * the fraction of committed heap bytes that are overhead. Like all
356 * UNITS_PERCENTAGE measurements, its amount is multiplied by 100x so it can
357 * be represented by an int64_t.
359 * |JSMainRuntimeGCHeap| (UNITS_BYTES) Size of the main JS runtime's GC
360 * heap.
362 * |JSMainRuntimeTemporaryPeak| (UNITS_BYTES) Peak size of the transient
363 * storage in the main JSRuntime.
365 * |JSMainRuntimeCompartments{System,User}| (UNITS_COUNT) The number of
366 * {system,user} compartments in the main JS runtime.
368 * |JSMainRuntimeRealms{System,User}| (UNITS_COUNT) The number of
369 * {system,user} realms in the main JS runtime.
371 * |imagesContentUsedUncompressed| (UNITS_BYTES) Memory used for decoded
372 * raster images in content.
374 * |storageSQLite| (UNITS_BYTES) Memory used by SQLite.
376 * |lowMemoryEventsPhysical| (UNITS_COUNT_CUMULATIVE)
377 * The number of low-physical-memory events that have occurred since the
378 * process started.
380 * |ghostWindows| (UNITS_COUNT) A cached value of the number of ghost
381 * windows. This should have been updated within the past 60s.
383 * |pageFaultsHard| (UNITS_COUNT_CUMULATIVE) The number of hard (a.k.a.
384 * major) page faults that have occurred since the process started.
386 [must_use] readonly attribute int64_t vsize;
387 [must_use] readonly attribute int64_t vsizeMaxContiguous;
388 [must_use] readonly attribute int64_t resident;
389 [must_use] readonly attribute int64_t residentFast;
390 [must_use] readonly attribute int64_t residentPeak;
391 [must_use] readonly attribute int64_t residentUnique;
393 [must_use] readonly attribute int64_t heapAllocated;
394 [must_use] readonly attribute int64_t heapOverheadFraction;
396 [must_use] readonly attribute int64_t JSMainRuntimeGCHeap;
397 [must_use] readonly attribute int64_t JSMainRuntimeTemporaryPeak;
398 [must_use] readonly attribute int64_t JSMainRuntimeCompartmentsSystem;
399 [must_use] readonly attribute int64_t JSMainRuntimeCompartmentsUser;
400 [must_use] readonly attribute int64_t JSMainRuntimeRealmsSystem;
401 [must_use] readonly attribute int64_t JSMainRuntimeRealmsUser;
403 [must_use] readonly attribute int64_t imagesContentUsedUncompressed;
405 [must_use] readonly attribute int64_t storageSQLite;
407 [must_use] readonly attribute int64_t lowMemoryEventsPhysical;
409 [must_use] readonly attribute int64_t ghostWindows;
411 [must_use] readonly attribute int64_t pageFaultsHard;
414 * This attribute indicates if moz_malloc_usable_size() works.
416 [infallible] readonly attribute boolean hasMozMallocUsableSize;
419 * These attributes indicate DMD's status. "Enabled" means enabled at
420 * build-time.
422 [infallible] readonly attribute boolean isDMDEnabled;
423 [infallible] readonly attribute boolean isDMDRunning;
426 * Run a series of GC/CC's in an attempt to minimize the application's memory
427 * usage. When we're finished doing this for the current process, we invoke
428 * the given runnable if it's not null. We do not wait for any child processes
429 * that might be doing their own minimization via child-mmu-request to finish.
431 [must_use] void minimizeMemoryUsage(in nsIRunnable callback);
434 * Measure the memory that is known to be owned by this tab, split up into
435 * several broad categories. Note that this will be an underestimate of the
436 * true number, due to imperfect memory reporter coverage (corresponding to
437 * about:memory's "heap-unclassified"), and due to some memory shared between
438 * tabs not being counted.
440 * The time taken for the measurement (split into JS and non-JS parts) is
441 * also returned.
443 [must_use]
444 void sizeOfTab(in mozIDOMWindowProxy window,
445 out int64_t jsObjectsSize, out int64_t jsStringsSize,
446 out int64_t jsOtherSize, out int64_t domSize,
447 out int64_t styleSize, out int64_t otherSize,
448 out int64_t totalSize,
449 out double jsMilliseconds, out double nonJSMilliseconds);
452 %{C++
454 #include "js/TypeDecls.h"
455 #include "nsString.h"
456 #include "nsTArray.h"
458 class nsPIDOMWindowOuter;
460 namespace mozilla {
462 // All the following registration/unregistration functions don't use
463 // [[nodiscard]] because ignoring failures is common and reasonable.
465 // Register a memory reporter. The manager service will hold a strong
466 // reference to this reporter.
467 XPCOM_API(nsresult) RegisterStrongMemoryReporter(nsIMemoryReporter* aReporter);
468 XPCOM_API(nsresult) RegisterStrongAsyncMemoryReporter(nsIMemoryReporter* aReporter);
470 // Register a memory reporter. The manager service will hold a weak reference
471 // to this reporter.
472 XPCOM_API(nsresult) RegisterWeakMemoryReporter(nsIMemoryReporter* aReporter);
473 XPCOM_API(nsresult) RegisterWeakAsyncMemoryReporter(nsIMemoryReporter* aReporter);
475 // Unregister a strong memory reporter.
476 XPCOM_API(nsresult) UnregisterStrongMemoryReporter(nsIMemoryReporter* aReporter);
478 // Unregister a weak memory reporter.
479 XPCOM_API(nsresult) UnregisterWeakMemoryReporter(nsIMemoryReporter* aReporter);
481 // The memory reporter manager provides access to several distinguished
482 // amounts via attributes. Some of these amounts are provided by Gecko
483 // components that cannot be accessed directly from XPCOM code. So we provide
484 // the following functions for those components to be registered with the
485 // manager.
487 typedef int64_t (*InfallibleAmountFn)();
489 #define DECL_REGISTER_DISTINGUISHED_AMOUNT(kind, name) \
490 nsresult Register##name##DistinguishedAmount(kind##AmountFn aAmountFn);
491 #define DECL_UNREGISTER_DISTINGUISHED_AMOUNT(name) \
492 nsresult Unregister##name##DistinguishedAmount();
494 DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, JSMainRuntimeGCHeap)
495 DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, JSMainRuntimeTemporaryPeak)
496 DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, JSMainRuntimeCompartmentsSystem)
497 DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, JSMainRuntimeCompartmentsUser)
498 DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, JSMainRuntimeRealmsSystem)
499 DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, JSMainRuntimeRealmsUser)
501 DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, ImagesContentUsedUncompressed)
502 DECL_UNREGISTER_DISTINGUISHED_AMOUNT(ImagesContentUsedUncompressed)
504 DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, StorageSQLite)
505 DECL_UNREGISTER_DISTINGUISHED_AMOUNT(StorageSQLite)
507 DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, LowMemoryEventsPhysical)
509 DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, GhostWindows)
511 #undef DECL_REGISTER_DISTINGUISHED_AMOUNT
512 #undef DECL_UNREGISTER_DISTINGUISHED_AMOUNT
514 // Likewise for per-tab measurement.
516 typedef nsresult (*JSSizeOfTabFn)(JSObject* aObj,
517 size_t* aJsObjectsSize,
518 size_t* aJsStringSize,
519 size_t* aJsPrivateSize,
520 size_t* aJsOtherSize);
521 typedef nsresult (*NonJSSizeOfTabFn)(nsPIDOMWindowOuter* aWindow,
522 size_t* aDomSize,
523 size_t* aStyleSize,
524 size_t* aOtherSize);
526 nsresult RegisterJSSizeOfTab(JSSizeOfTabFn aSizeOfTabFn);
527 nsresult RegisterNonJSSizeOfTab(NonJSSizeOfTabFn aSizeOfTabFn);
531 #if defined(MOZ_DMD)
532 #if !defined(MOZ_MEMORY)
533 #error "MOZ_DMD requires MOZ_MEMORY"
534 #endif
536 #include "DMD.h"
538 #define MOZ_REPORT(ptr) mozilla::dmd::Report(ptr)
539 #define MOZ_REPORT_ON_ALLOC(ptr) mozilla::dmd::ReportOnAlloc(ptr)
541 #else
543 #define MOZ_REPORT(ptr)
544 #define MOZ_REPORT_ON_ALLOC(ptr)
546 #endif // defined(MOZ_DMD)
548 // Functions generated via this macro should be used by all traversal-based
549 // memory reporters. Such functions return |moz_malloc_size_of(ptr)|; this
550 // will always be zero on some obscure platforms.
552 // You might be wondering why we have a macro that creates multiple functions
553 // that differ only in their name, instead of a single MallocSizeOf function.
554 // It's mostly to help with DMD integration, though it sometimes also helps
555 // with debugging and temporary ad hoc profiling. The function name chosen
556 // doesn't matter greatly, but it's best to make it similar to the path used by
557 // the relevant memory reporter(s).
558 #define MOZ_DEFINE_MALLOC_SIZE_OF(fn) \
559 static size_t fn(const void* aPtr) \
561 MOZ_REPORT(aPtr); \
562 return moz_malloc_size_of(aPtr); \
565 // This is an alternative to MOZ_DEFINE_MALLOC_SIZE_OF that defines a
566 // MallocSizeOf function that can handle interior pointers.
567 #ifdef MOZ_MEMORY
569 #include "mozmemory.h"
571 #define MOZ_DEFINE_MALLOC_ENCLOSING_SIZE_OF(fn) \
572 static size_t fn(const void* aPtr) \
574 jemalloc_ptr_info_t info; \
575 jemalloc_ptr_info(aPtr, &info); \
576 MOZ_REPORT(info.addr); \
577 return jemalloc_ptr_is_live(&info) ? info.size : 0; \
580 #else
582 #define MOZ_DEFINE_MALLOC_ENCLOSING_SIZE_OF(fn) \
583 static size_t fn(const void* aPtr) \
585 return 0; \
588 #endif
590 // Functions generated by the next two macros should be used by wrapping
591 // allocators that report heap blocks as soon as they are allocated and
592 // unreport them as soon as they are freed. Such allocators are used in cases
593 // where we have third-party code that we cannot modify. The two functions
594 // must always be used in tandem.
595 #define MOZ_DEFINE_MALLOC_SIZE_OF_ON_ALLOC(fn) \
596 static size_t fn(const void* aPtr) \
598 MOZ_REPORT_ON_ALLOC(aPtr); \
599 return moz_malloc_size_of(aPtr); \
601 #define MOZ_DEFINE_MALLOC_SIZE_OF_ON_FREE(fn) \
602 static size_t fn(const void* aPtr) \
604 return moz_malloc_size_of(aPtr); \
607 // This macro assumes the presence of appropriate |aHandleReport| and |aData|
608 // variables. The (void) is there because we should always ignore the return
609 // value of the callback, because callback failures aren't fatal.
610 #define MOZ_COLLECT_REPORT(path, kind, units, amount, description) \
611 (void)aHandleReport->Callback(""_ns, nsLiteralCString(path), \
612 kind, units, amount, \
613 nsLiteralCString(description), aData)