Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsISupports.idl" |
michael@0 | 8 | |
michael@0 | 9 | interface nsIDOMWindow; |
michael@0 | 10 | interface nsIRunnable; |
michael@0 | 11 | interface nsISimpleEnumerator; |
michael@0 | 12 | |
michael@0 | 13 | /* |
michael@0 | 14 | * Memory reporters measure Firefox's memory usage. They are primarily used to |
michael@0 | 15 | * generate the about:memory page. You should read |
michael@0 | 16 | * https://wiki.mozilla.org/Memory_Reporting before writing a memory |
michael@0 | 17 | * reporter. |
michael@0 | 18 | */ |
michael@0 | 19 | |
michael@0 | 20 | [scriptable, function, uuid(3a61be3b-b93b-461a-a4f8-388214f558b1)] |
michael@0 | 21 | interface nsIMemoryReporterCallback : nsISupports |
michael@0 | 22 | { |
michael@0 | 23 | /* |
michael@0 | 24 | * The arguments to the callback are as follows. |
michael@0 | 25 | * |
michael@0 | 26 | * |
michael@0 | 27 | * |process| The name of the process containing this reporter. Each |
michael@0 | 28 | * reporter initially has "" in this field, indicating that it applies to the |
michael@0 | 29 | * current process. (This is true even for reporters in a child process.) |
michael@0 | 30 | * When a reporter from a child process is copied into the main process, the |
michael@0 | 31 | * copy has its 'process' field set appropriately. |
michael@0 | 32 | * |
michael@0 | 33 | * |
michael@0 | 34 | * |path| The path that this memory usage should be reported under. Paths |
michael@0 | 35 | * are '/'-delimited, eg. "a/b/c". |
michael@0 | 36 | * |
michael@0 | 37 | * Each reporter can be viewed as representing a leaf node in a tree. |
michael@0 | 38 | * Internal nodes of the tree don't have reporters. So, for example, the |
michael@0 | 39 | * reporters "explicit/a/b", "explicit/a/c", "explicit/d/e", and |
michael@0 | 40 | * "explicit/d/f" define this tree: |
michael@0 | 41 | * |
michael@0 | 42 | * explicit |
michael@0 | 43 | * |--a |
michael@0 | 44 | * | |--b [*] |
michael@0 | 45 | * | \--c [*] |
michael@0 | 46 | * \--d |
michael@0 | 47 | * |--e [*] |
michael@0 | 48 | * \--f [*] |
michael@0 | 49 | * |
michael@0 | 50 | * Nodes marked with a [*] have a reporter. Notice that the internal |
michael@0 | 51 | * nodes are implicitly defined by the paths. |
michael@0 | 52 | * |
michael@0 | 53 | * Nodes within a tree should not overlap measurements, otherwise the |
michael@0 | 54 | * parent node measurements will be double-counted. So in the example |
michael@0 | 55 | * above, |b| should not count any allocations counted by |c|, and vice |
michael@0 | 56 | * versa. |
michael@0 | 57 | * |
michael@0 | 58 | * All nodes within each tree must have the same units. |
michael@0 | 59 | * |
michael@0 | 60 | * If you want to include a '/' not as a path separator, e.g. because the |
michael@0 | 61 | * path contains a URL, you need to convert each '/' in the URL to a '\'. |
michael@0 | 62 | * Consumers of the path will undo this change. Any other '\' character |
michael@0 | 63 | * in a path will also be changed. This is clumsy but hasn't caused any |
michael@0 | 64 | * problems so far. |
michael@0 | 65 | * |
michael@0 | 66 | * The paths of all reporters form a set of trees. Trees can be |
michael@0 | 67 | * "degenerate", i.e. contain a single entry with no '/'. |
michael@0 | 68 | * |
michael@0 | 69 | * |
michael@0 | 70 | * |kind| There are three kinds of memory reporters. |
michael@0 | 71 | * |
michael@0 | 72 | * - HEAP: reporters measuring memory allocated by the heap allocator, |
michael@0 | 73 | * e.g. by calling malloc, calloc, realloc, memalign, operator new, or |
michael@0 | 74 | * operator new[]. Reporters in this category must have units |
michael@0 | 75 | * UNITS_BYTES. |
michael@0 | 76 | * |
michael@0 | 77 | * - NONHEAP: reporters measuring memory which the program explicitly |
michael@0 | 78 | * allocated, but does not live on the heap. Such memory is commonly |
michael@0 | 79 | * allocated by calling one of the OS's memory-mapping functions (e.g. |
michael@0 | 80 | * mmap, VirtualAlloc, or vm_allocate). Reporters in this category |
michael@0 | 81 | * must have units UNITS_BYTES. |
michael@0 | 82 | * |
michael@0 | 83 | * - OTHER: reporters which don't fit into either of these categories. |
michael@0 | 84 | * They can have any units. |
michael@0 | 85 | * |
michael@0 | 86 | * The kind only matters for reporters in the "explicit" tree; |
michael@0 | 87 | * aboutMemory.js uses it to calculate "heap-unclassified". |
michael@0 | 88 | * |
michael@0 | 89 | * |
michael@0 | 90 | * |units| The units on the reporter's amount. One of the following. |
michael@0 | 91 | * |
michael@0 | 92 | * - BYTES: The amount contains a number of bytes. |
michael@0 | 93 | * |
michael@0 | 94 | * - COUNT: The amount is an instantaneous count of things currently in |
michael@0 | 95 | * existence. For instance, the number of tabs currently open would have |
michael@0 | 96 | * units COUNT. |
michael@0 | 97 | * |
michael@0 | 98 | * - COUNT_CUMULATIVE: The amount contains the number of times some event |
michael@0 | 99 | * has occurred since the application started up. For instance, the |
michael@0 | 100 | * number of times the user has opened a new tab would have units |
michael@0 | 101 | * COUNT_CUMULATIVE. |
michael@0 | 102 | * |
michael@0 | 103 | * The amount returned by a reporter with units COUNT_CUMULATIVE must |
michael@0 | 104 | * never decrease over the lifetime of the application. |
michael@0 | 105 | * |
michael@0 | 106 | * - PERCENTAGE: The amount contains a fraction that should be expressed as |
michael@0 | 107 | * a percentage. NOTE! The |amount| field should be given a value 100x |
michael@0 | 108 | * the actual percentage; this number will be divided by 100 when shown. |
michael@0 | 109 | * This allows a fractional percentage to be shown even though |amount| is |
michael@0 | 110 | * an integer. E.g. if the actual percentage is 12.34%, |amount| should |
michael@0 | 111 | * be 1234. |
michael@0 | 112 | * |
michael@0 | 113 | * Values greater than 100% are allowed. |
michael@0 | 114 | * |
michael@0 | 115 | * |
michael@0 | 116 | * |amount| The numeric value reported by this memory reporter. Accesses |
michael@0 | 117 | * can fail if something goes wrong when getting the amount. |
michael@0 | 118 | * |
michael@0 | 119 | * |
michael@0 | 120 | * |description| A human-readable description of this memory usage report. |
michael@0 | 121 | */ |
michael@0 | 122 | void callback(in ACString process, in AUTF8String path, in int32_t kind, |
michael@0 | 123 | in int32_t units, in int64_t amount, |
michael@0 | 124 | in AUTF8String description, in nsISupports data); |
michael@0 | 125 | }; |
michael@0 | 126 | |
michael@0 | 127 | /* |
michael@0 | 128 | * An nsIMemoryReporter reports one or more memory measurements via a |
michael@0 | 129 | * callback function which is called once for each measurement. |
michael@0 | 130 | * |
michael@0 | 131 | * An nsIMemoryReporter that reports a single measurement is sometimes called a |
michael@0 | 132 | * "uni-reporter". One that reports multiple measurements is sometimes called |
michael@0 | 133 | * a "multi-reporter". |
michael@0 | 134 | * |
michael@0 | 135 | * aboutMemory.js is the most important consumer of memory reports. It |
michael@0 | 136 | * places the following constraints on reports. |
michael@0 | 137 | * |
michael@0 | 138 | * - All reports within a single sub-tree must have the same units. |
michael@0 | 139 | * |
michael@0 | 140 | * - There may be an "explicit" tree. If present, it represents |
michael@0 | 141 | * non-overlapping regions of memory that have been explicitly allocated with |
michael@0 | 142 | * an OS-level allocation (e.g. mmap/VirtualAlloc/vm_allocate) or a |
michael@0 | 143 | * heap-level allocation (e.g. malloc/calloc/operator new). Reporters in |
michael@0 | 144 | * this tree must have kind HEAP or NONHEAP, units BYTES. |
michael@0 | 145 | * |
michael@0 | 146 | * It is preferred, but not required, that report descriptions use complete |
michael@0 | 147 | * sentences (i.e. start with a capital letter and end with a period, or |
michael@0 | 148 | * similar). |
michael@0 | 149 | */ |
michael@0 | 150 | [scriptable, uuid(0884cd0f-5829-4381-979b-0f53904030ed)] |
michael@0 | 151 | interface nsIMemoryReporter : nsISupports |
michael@0 | 152 | { |
michael@0 | 153 | /* |
michael@0 | 154 | * Run the reporter. |
michael@0 | 155 | */ |
michael@0 | 156 | void collectReports(in nsIMemoryReporterCallback callback, |
michael@0 | 157 | in nsISupports data); |
michael@0 | 158 | |
michael@0 | 159 | /* |
michael@0 | 160 | * Kinds. See the |kind| comment in nsIMemoryReporterCallback. |
michael@0 | 161 | */ |
michael@0 | 162 | const int32_t KIND_NONHEAP = 0; |
michael@0 | 163 | const int32_t KIND_HEAP = 1; |
michael@0 | 164 | const int32_t KIND_OTHER = 2; |
michael@0 | 165 | |
michael@0 | 166 | /* |
michael@0 | 167 | * Units. See the |units| comment in nsIMemoryReporterCallback. |
michael@0 | 168 | */ |
michael@0 | 169 | const int32_t UNITS_BYTES = 0; |
michael@0 | 170 | const int32_t UNITS_COUNT = 1; |
michael@0 | 171 | const int32_t UNITS_COUNT_CUMULATIVE = 2; |
michael@0 | 172 | const int32_t UNITS_PERCENTAGE = 3; |
michael@0 | 173 | }; |
michael@0 | 174 | |
michael@0 | 175 | [scriptable, function, uuid(548b3909-c04d-4ca6-8466-b8bee3837457)] |
michael@0 | 176 | interface nsIFinishReportingCallback : nsISupports |
michael@0 | 177 | { |
michael@0 | 178 | void callback(in nsISupports data); |
michael@0 | 179 | }; |
michael@0 | 180 | |
michael@0 | 181 | [scriptable, builtinclass, uuid(b6e5ec8a-71d9-48db-8ae9-68b4c5bbf2c3)] |
michael@0 | 182 | interface nsIMemoryReporterManager : nsISupports |
michael@0 | 183 | { |
michael@0 | 184 | /* |
michael@0 | 185 | * Initialize. |
michael@0 | 186 | */ |
michael@0 | 187 | void init(); |
michael@0 | 188 | |
michael@0 | 189 | /* |
michael@0 | 190 | * Register the given nsIMemoryReporter. The Manager service will hold a |
michael@0 | 191 | * strong reference to the given reporter, and will be responsible for freeing |
michael@0 | 192 | * the reporter at shutdown. You may manually unregister the reporter with |
michael@0 | 193 | * unregisterStrongReporter() at any point. |
michael@0 | 194 | */ |
michael@0 | 195 | void registerStrongReporter(in nsIMemoryReporter reporter); |
michael@0 | 196 | |
michael@0 | 197 | /* |
michael@0 | 198 | * Like registerReporter, but the Manager service will hold a weak reference |
michael@0 | 199 | * via a raw pointer to the given reporter. The reporter should be |
michael@0 | 200 | * unregistered before shutdown. |
michael@0 | 201 | * You cannot register JavaScript components with this function! Always |
michael@0 | 202 | * register your JavaScript components with registerStrongReporter(). |
michael@0 | 203 | */ |
michael@0 | 204 | void registerWeakReporter(in nsIMemoryReporter reporter); |
michael@0 | 205 | |
michael@0 | 206 | /* |
michael@0 | 207 | * Unregister the given memory reporter, which must have been registered with |
michael@0 | 208 | * registerStrongReporter(). You normally don't need to unregister your |
michael@0 | 209 | * strong reporters, as nsIMemoryReporterManager will take care of that at |
michael@0 | 210 | * shutdown. |
michael@0 | 211 | */ |
michael@0 | 212 | void unregisterStrongReporter(in nsIMemoryReporter reporter); |
michael@0 | 213 | |
michael@0 | 214 | /* |
michael@0 | 215 | * Unregister the given memory reporter, which must have been registered with |
michael@0 | 216 | * registerWeakReporter(). |
michael@0 | 217 | */ |
michael@0 | 218 | void unregisterWeakReporter(in nsIMemoryReporter reporter); |
michael@0 | 219 | |
michael@0 | 220 | /* |
michael@0 | 221 | * These functions should only be used for testing purposes. |
michael@0 | 222 | */ |
michael@0 | 223 | void blockRegistrationAndHideExistingReporters(); |
michael@0 | 224 | void unblockRegistrationAndRestoreOriginalReporters(); |
michael@0 | 225 | void registerStrongReporterEvenIfBlocked(in nsIMemoryReporter aReporter); |
michael@0 | 226 | |
michael@0 | 227 | /* |
michael@0 | 228 | * Get memory reports for the current process and all child processes. |
michael@0 | 229 | * |handleReport| is called for each report, and |finishReporting| is called |
michael@0 | 230 | * once all reports have been handled. |
michael@0 | 231 | * |
michael@0 | 232 | * |finishReporting| is called even if, for example, some child processes |
michael@0 | 233 | * fail to report back. However, calls to this method will silently and |
michael@0 | 234 | * immediately abort -- and |finishReporting| will not be called -- if a |
michael@0 | 235 | * previous getReports() call is still in flight, i.e. if it has not yet |
michael@0 | 236 | * finished invoking |finishReporting|. The silent abort is because the |
michael@0 | 237 | * in-flight request will finish soon, and the caller would very likely just |
michael@0 | 238 | * catch and ignore any error anyway. |
michael@0 | 239 | */ |
michael@0 | 240 | void getReports(in nsIMemoryReporterCallback handleReport, |
michael@0 | 241 | in nsISupports handleReportData, |
michael@0 | 242 | in nsIFinishReportingCallback finishReporting, |
michael@0 | 243 | in nsISupports finishReportingData); |
michael@0 | 244 | |
michael@0 | 245 | /* |
michael@0 | 246 | * As above, but: If |minimizeMemoryUsage| is true, then each process will |
michael@0 | 247 | * minimize its memory usage (see the |minimizeMemoryUsage| method) before |
michael@0 | 248 | * gathering its report. If DMD is enabled and |DMDDumpIdent| is non-empty |
michael@0 | 249 | * then write a DMD report to a file in the usual temporary directory (see |
michael@0 | 250 | * |dumpMemoryInfoToTempDir| in |nsIMemoryInfoDumper|.) |
michael@0 | 251 | */ |
michael@0 | 252 | [noscript] void |
michael@0 | 253 | getReportsExtended(in nsIMemoryReporterCallback handleReport, |
michael@0 | 254 | in nsISupports handleReportData, |
michael@0 | 255 | in nsIFinishReportingCallback finishReporting, |
michael@0 | 256 | in nsISupports finishReportingData, |
michael@0 | 257 | in boolean minimizeMemoryUsage, |
michael@0 | 258 | in AString DMDDumpIdent); |
michael@0 | 259 | |
michael@0 | 260 | /* |
michael@0 | 261 | * Get memory reports in the current process only. |handleReport| is called |
michael@0 | 262 | * for each report. |
michael@0 | 263 | */ |
michael@0 | 264 | void getReportsForThisProcess(in nsIMemoryReporterCallback handleReport, |
michael@0 | 265 | in nsISupports handleReportData); |
michael@0 | 266 | |
michael@0 | 267 | /* |
michael@0 | 268 | * As above, but if DMD is enabled and |DMDDumpIdent| is non-empty |
michael@0 | 269 | * then write a DMD report to a file in the usual temporary directory (see |
michael@0 | 270 | * |dumpMemoryInfoToTempDir| in |nsIMemoryInfoDumper|.) |
michael@0 | 271 | */ |
michael@0 | 272 | [noscript] void |
michael@0 | 273 | getReportsForThisProcessExtended(in nsIMemoryReporterCallback handleReport, |
michael@0 | 274 | in nsISupports handleReportData, |
michael@0 | 275 | in AString DMDDumpIdent); |
michael@0 | 276 | |
michael@0 | 277 | /* |
michael@0 | 278 | * The memory reporter manager, for the most part, treats reporters |
michael@0 | 279 | * registered with it as a black box. However, there are some |
michael@0 | 280 | * "distinguished" amounts (as could be reported by a memory reporter) that |
michael@0 | 281 | * the manager provides as attributes, because they are sufficiently |
michael@0 | 282 | * interesting that we want external code (e.g. telemetry) to be able to rely |
michael@0 | 283 | * on them. |
michael@0 | 284 | * |
michael@0 | 285 | * Note that these are not reporters and so getReports() and |
michael@0 | 286 | * getReportsForThisProcess() do not look at them. However, distinguished |
michael@0 | 287 | * amounts can be embedded in a reporter. |
michael@0 | 288 | * |
michael@0 | 289 | * Access to these attributes can fail. In particular, some of them are not |
michael@0 | 290 | * available on all platforms. |
michael@0 | 291 | * |
michael@0 | 292 | * If you add a new distinguished amount, please update |
michael@0 | 293 | * toolkit/components/aboutmemory/tests/test_memoryReporters.xul. |
michael@0 | 294 | * |
michael@0 | 295 | * |explicit| (UNITS_BYTES) The total size of explicit memory allocations, |
michael@0 | 296 | * both at the OS-level (eg. via mmap, VirtualAlloc) and at the heap level |
michael@0 | 297 | * (eg. via malloc, calloc, operator new). It covers all heap allocations, |
michael@0 | 298 | * but will miss any OS-level ones not covered by memory reporters. |
michael@0 | 299 | * |
michael@0 | 300 | * |vsize| (UNITS_BYTES) The virtual size, i.e. the amount of address space |
michael@0 | 301 | * taken up. |
michael@0 | 302 | * |
michael@0 | 303 | * |vsizeMaxContiguous| (UNITS_BYTES) The size of the largest contiguous |
michael@0 | 304 | * block of virtual memory. |
michael@0 | 305 | * |
michael@0 | 306 | * |resident| (UNITS_BYTES) The resident size (a.k.a. RSS or physical memory |
michael@0 | 307 | * used). |
michael@0 | 308 | * |
michael@0 | 309 | * |residentFast| (UNITS_BYTES) This is like |resident|, but on Mac OS |
michael@0 | 310 | * |resident| can purge pages, which is slow. It also affects the result of |
michael@0 | 311 | * |residentFast|, and so |resident| and |residentFast| should not be used |
michael@0 | 312 | * together. |
michael@0 | 313 | * |
michael@0 | 314 | * |heapAllocated| (UNITS_BYTES) Memory mapped by the heap allocator. |
michael@0 | 315 | * |
michael@0 | 316 | * |heapOverheadRatio| (UNITS_PERCENTAGE) In the heap allocator, this is the |
michael@0 | 317 | * ratio of committed, unused bytes to allocated bytes. Like all |
michael@0 | 318 | * UNITS_PERCENTAGE measurements, its amount is multiplied by 100x so it can |
michael@0 | 319 | * be represented by an int64_t. |
michael@0 | 320 | * |
michael@0 | 321 | * |JSMainRuntimeGCHeap| (UNITS_BYTES) Size of the main JS runtime's GC |
michael@0 | 322 | * heap. |
michael@0 | 323 | * |
michael@0 | 324 | * |JSMainRuntimeTemporaryPeak| (UNITS_BYTES) Peak size of the transient |
michael@0 | 325 | * storage in the main JSRuntime. |
michael@0 | 326 | * |
michael@0 | 327 | * |JSMainRuntimeCompartments{System,User}| (UNITS_COUNT) The number of |
michael@0 | 328 | * {system,user} compartments in the main JS runtime. |
michael@0 | 329 | * |
michael@0 | 330 | * |imagesContentUsedUncompressed| (UNITS_BYTES) Memory used for decoded |
michael@0 | 331 | * images in content. |
michael@0 | 332 | * |
michael@0 | 333 | * |storageSQLite| (UNITS_BYTES) Memory used by SQLite. |
michael@0 | 334 | * |
michael@0 | 335 | * |lowMemoryEvents{Virtual,Physical}| (UNITS_COUNT_CUMULATIVE) The number |
michael@0 | 336 | * of low-{virtual,physical}-memory events that have occurred since the |
michael@0 | 337 | * process started. |
michael@0 | 338 | * |
michael@0 | 339 | * |ghostWindows| (UNITS_COUNT) The number of ghost windows. |
michael@0 | 340 | * |
michael@0 | 341 | * |pageFaultsHard| (UNITS_COUNT_CUMULATIVE) The number of hard (a.k.a. |
michael@0 | 342 | * major) page faults that have occurred since the process started. |
michael@0 | 343 | */ |
michael@0 | 344 | readonly attribute int64_t explicit; |
michael@0 | 345 | readonly attribute int64_t vsize; |
michael@0 | 346 | readonly attribute int64_t vsizeMaxContiguous; |
michael@0 | 347 | readonly attribute int64_t resident; |
michael@0 | 348 | readonly attribute int64_t residentFast; |
michael@0 | 349 | |
michael@0 | 350 | readonly attribute int64_t heapAllocated; |
michael@0 | 351 | readonly attribute int64_t heapOverheadRatio; |
michael@0 | 352 | |
michael@0 | 353 | readonly attribute int64_t JSMainRuntimeGCHeap; |
michael@0 | 354 | readonly attribute int64_t JSMainRuntimeTemporaryPeak; |
michael@0 | 355 | readonly attribute int64_t JSMainRuntimeCompartmentsSystem; |
michael@0 | 356 | readonly attribute int64_t JSMainRuntimeCompartmentsUser; |
michael@0 | 357 | |
michael@0 | 358 | readonly attribute int64_t imagesContentUsedUncompressed; |
michael@0 | 359 | |
michael@0 | 360 | readonly attribute int64_t storageSQLite; |
michael@0 | 361 | |
michael@0 | 362 | readonly attribute int64_t lowMemoryEventsVirtual; |
michael@0 | 363 | readonly attribute int64_t lowMemoryEventsPhysical; |
michael@0 | 364 | |
michael@0 | 365 | readonly attribute int64_t ghostWindows; |
michael@0 | 366 | |
michael@0 | 367 | readonly attribute int64_t pageFaultsHard; |
michael@0 | 368 | |
michael@0 | 369 | /* |
michael@0 | 370 | * This attribute indicates if moz_malloc_usable_size() works. |
michael@0 | 371 | */ |
michael@0 | 372 | [infallible] readonly attribute boolean hasMozMallocUsableSize; |
michael@0 | 373 | |
michael@0 | 374 | /* |
michael@0 | 375 | * Run a series of GC/CC's in an attempt to minimize the application's memory |
michael@0 | 376 | * usage. When we're finished, we invoke the given runnable if it's not |
michael@0 | 377 | * null. |
michael@0 | 378 | */ |
michael@0 | 379 | void minimizeMemoryUsage(in nsIRunnable callback); |
michael@0 | 380 | |
michael@0 | 381 | /* |
michael@0 | 382 | * Measure the memory that is known to be owned by this tab, split up into |
michael@0 | 383 | * several broad categories. Note that this will be an underestimate of the |
michael@0 | 384 | * true number, due to imperfect memory reporter coverage (corresponding to |
michael@0 | 385 | * about:memory's "heap-unclassified"), and due to some memory shared between |
michael@0 | 386 | * tabs not being counted. |
michael@0 | 387 | * |
michael@0 | 388 | * The time taken for the measurement (split into JS and non-JS parts) is |
michael@0 | 389 | * also returned. |
michael@0 | 390 | */ |
michael@0 | 391 | void sizeOfTab(in nsIDOMWindow window, |
michael@0 | 392 | out int64_t jsObjectsSize, out int64_t jsStringsSize, |
michael@0 | 393 | out int64_t jsOtherSize, out int64_t domSize, |
michael@0 | 394 | out int64_t styleSize, out int64_t otherSize, |
michael@0 | 395 | out int64_t totalSize, |
michael@0 | 396 | out double jsMilliseconds, out double nonJSMilliseconds); |
michael@0 | 397 | }; |
michael@0 | 398 | |
michael@0 | 399 | %{C++ |
michael@0 | 400 | |
michael@0 | 401 | #include "js/TypeDecls.h" |
michael@0 | 402 | #include "nsStringGlue.h" |
michael@0 | 403 | #include "nsTArray.h" |
michael@0 | 404 | #include "mozilla/Atomics.h" |
michael@0 | 405 | |
michael@0 | 406 | class nsPIDOMWindow; |
michael@0 | 407 | |
michael@0 | 408 | // nsIHandleReportCallback is a better name, but keep nsIMemoryReporterCallback |
michael@0 | 409 | // around for backwards compatibility. |
michael@0 | 410 | typedef nsIMemoryReporterCallback nsIHandleReportCallback; |
michael@0 | 411 | |
michael@0 | 412 | namespace mozilla { |
michael@0 | 413 | |
michael@0 | 414 | // Register a memory reporter. The manager service will hold a strong |
michael@0 | 415 | // reference to this reporter. |
michael@0 | 416 | XPCOM_API(nsresult) RegisterStrongMemoryReporter(nsIMemoryReporter* aReporter); |
michael@0 | 417 | |
michael@0 | 418 | // Register a memory reporter. The manager service will hold a weak reference |
michael@0 | 419 | // to this reporter. |
michael@0 | 420 | XPCOM_API(nsresult) RegisterWeakMemoryReporter(nsIMemoryReporter* aReporter); |
michael@0 | 421 | |
michael@0 | 422 | // Unregister a weak memory reporter. |
michael@0 | 423 | XPCOM_API(nsresult) UnregisterWeakMemoryReporter(nsIMemoryReporter* aReporter); |
michael@0 | 424 | |
michael@0 | 425 | // The memory reporter manager provides access to several distinguished |
michael@0 | 426 | // amounts via attributes. Some of these amounts are provided by Gecko |
michael@0 | 427 | // components that cannot be accessed directly from XPCOM code. So we provide |
michael@0 | 428 | // the following functions for those components to be registered with the |
michael@0 | 429 | // manager. |
michael@0 | 430 | |
michael@0 | 431 | typedef int64_t (*InfallibleAmountFn)(); |
michael@0 | 432 | typedef nsresult (*FallibleAmountFn)(int64_t* aAmount); |
michael@0 | 433 | |
michael@0 | 434 | #define DECL_REGISTER_DISTINGUISHED_AMOUNT(kind, name) \ |
michael@0 | 435 | nsresult Register##name##DistinguishedAmount(kind##AmountFn aAmountFn); |
michael@0 | 436 | #define DECL_UNREGISTER_DISTINGUISHED_AMOUNT(name) \ |
michael@0 | 437 | nsresult Unregister##name##DistinguishedAmount(); |
michael@0 | 438 | |
michael@0 | 439 | DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, JSMainRuntimeGCHeap) |
michael@0 | 440 | DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, JSMainRuntimeTemporaryPeak) |
michael@0 | 441 | DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, JSMainRuntimeCompartmentsSystem) |
michael@0 | 442 | DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, JSMainRuntimeCompartmentsUser) |
michael@0 | 443 | |
michael@0 | 444 | DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, ImagesContentUsedUncompressed) |
michael@0 | 445 | |
michael@0 | 446 | DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, StorageSQLite) |
michael@0 | 447 | DECL_UNREGISTER_DISTINGUISHED_AMOUNT(StorageSQLite) |
michael@0 | 448 | |
michael@0 | 449 | DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, LowMemoryEventsVirtual) |
michael@0 | 450 | DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, LowMemoryEventsPhysical) |
michael@0 | 451 | |
michael@0 | 452 | DECL_REGISTER_DISTINGUISHED_AMOUNT(Infallible, GhostWindows) |
michael@0 | 453 | |
michael@0 | 454 | #undef DECL_REGISTER_DISTINGUISHED_AMOUNT |
michael@0 | 455 | #undef DECL_UNREGISTER_DISTINGUISHED_AMOUNT |
michael@0 | 456 | |
michael@0 | 457 | // Likewise for per-tab measurement. |
michael@0 | 458 | |
michael@0 | 459 | typedef nsresult (*JSSizeOfTabFn)(JSObject* aObj, |
michael@0 | 460 | size_t* aJsObjectsSize, |
michael@0 | 461 | size_t* aJsStringSize, |
michael@0 | 462 | size_t* aJsPrivateSize, |
michael@0 | 463 | size_t* aJsOtherSize); |
michael@0 | 464 | typedef nsresult (*NonJSSizeOfTabFn)(nsPIDOMWindow* aWindow, |
michael@0 | 465 | size_t* aDomSize, |
michael@0 | 466 | size_t* aStyleSize, |
michael@0 | 467 | size_t* aOtherSize); |
michael@0 | 468 | |
michael@0 | 469 | nsresult RegisterJSSizeOfTab(JSSizeOfTabFn aSizeOfTabFn); |
michael@0 | 470 | nsresult RegisterNonJSSizeOfTab(NonJSSizeOfTabFn aSizeOfTabFn); |
michael@0 | 471 | |
michael@0 | 472 | } |
michael@0 | 473 | |
michael@0 | 474 | #if defined(MOZ_DMD) |
michael@0 | 475 | namespace mozilla { |
michael@0 | 476 | namespace dmd { |
michael@0 | 477 | // This runs all the memory reporters in the current process but does nothing |
michael@0 | 478 | // with the results; i.e. it does the minimal amount of work possible for DMD |
michael@0 | 479 | // to do its thing. It does nothing with child processes. |
michael@0 | 480 | void RunReportersForThisProcess(); |
michael@0 | 481 | } |
michael@0 | 482 | } |
michael@0 | 483 | |
michael@0 | 484 | #if !defined(MOZ_MEMORY) |
michael@0 | 485 | #error "MOZ_DMD requires MOZ_MEMORY" |
michael@0 | 486 | #endif |
michael@0 | 487 | |
michael@0 | 488 | #include "DMD.h" |
michael@0 | 489 | |
michael@0 | 490 | #define MOZ_REPORT(ptr) mozilla::dmd::Report(ptr) |
michael@0 | 491 | #define MOZ_REPORT_ON_ALLOC(ptr) mozilla::dmd::ReportOnAlloc(ptr) |
michael@0 | 492 | |
michael@0 | 493 | #else |
michael@0 | 494 | |
michael@0 | 495 | #define MOZ_REPORT(ptr) |
michael@0 | 496 | #define MOZ_REPORT_ON_ALLOC(ptr) |
michael@0 | 497 | |
michael@0 | 498 | #endif // defined(MOZ_DMD) |
michael@0 | 499 | |
michael@0 | 500 | // Functions generated via this macro should be used by all traversal-based |
michael@0 | 501 | // memory reporters. Such functions return |moz_malloc_size_of(ptr)|; this |
michael@0 | 502 | // will always be zero on some obscure platforms. |
michael@0 | 503 | // |
michael@0 | 504 | // You might be wondering why we have a macro that creates multiple functions |
michael@0 | 505 | // that differ only in their name, instead of a single MallocSizeOf function. |
michael@0 | 506 | // It's mostly to help with DMD integration, though it sometimes also helps |
michael@0 | 507 | // with debugging and temporary ad hoc profiling. The function name chosen |
michael@0 | 508 | // doesn't matter greatly, but it's best to make it similar to the path used by |
michael@0 | 509 | // the relevant memory reporter(s). |
michael@0 | 510 | #define MOZ_DEFINE_MALLOC_SIZE_OF(fn) \ |
michael@0 | 511 | static size_t fn(const void* aPtr) \ |
michael@0 | 512 | { \ |
michael@0 | 513 | MOZ_REPORT(aPtr); \ |
michael@0 | 514 | return moz_malloc_size_of(aPtr); \ |
michael@0 | 515 | } |
michael@0 | 516 | |
michael@0 | 517 | // Functions generated by the next two macros should be used by wrapping |
michael@0 | 518 | // allocators that report heap blocks as soon as they are allocated and |
michael@0 | 519 | // unreport them as soon as they are freed. Such allocators are used in cases |
michael@0 | 520 | // where we have third-party code that we cannot modify. The two functions |
michael@0 | 521 | // must always be used in tandem. |
michael@0 | 522 | #define MOZ_DEFINE_MALLOC_SIZE_OF_ON_ALLOC(fn) \ |
michael@0 | 523 | static size_t fn(const void* aPtr) \ |
michael@0 | 524 | { \ |
michael@0 | 525 | MOZ_REPORT_ON_ALLOC(aPtr); \ |
michael@0 | 526 | return moz_malloc_size_of(aPtr); \ |
michael@0 | 527 | } |
michael@0 | 528 | #define MOZ_DEFINE_MALLOC_SIZE_OF_ON_FREE(fn) \ |
michael@0 | 529 | static size_t fn(const void* aPtr) \ |
michael@0 | 530 | { \ |
michael@0 | 531 | return moz_malloc_size_of(aPtr); \ |
michael@0 | 532 | } |
michael@0 | 533 | |
michael@0 | 534 | namespace mozilla { |
michael@0 | 535 | |
michael@0 | 536 | // This CRTP class handles several details of wrapping allocators and should |
michael@0 | 537 | // be preferred to manually counting with MOZ_DEFINE_MALLOC_SIZE_OF_ON_ALLOC |
michael@0 | 538 | // and MOZ_DEFINE_MALLOC_SIZE_OF_ON_FREE. The typical use is in a memory |
michael@0 | 539 | // reporter for a particular third party library: |
michael@0 | 540 | // |
michael@0 | 541 | // class MyMemoryReporter : public CountingAllocatorBase<MyMemoryReporter> |
michael@0 | 542 | // { |
michael@0 | 543 | // ... |
michael@0 | 544 | // NS_IMETHODIMP |
michael@0 | 545 | // CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData) |
michael@0 | 546 | // { |
michael@0 | 547 | // return MOZ_COLLECT_REPORTER( |
michael@0 | 548 | // "explicit/path/to/somewhere", KIND_HEAP, UNITS_BYTES, |
michael@0 | 549 | // MemoryAllocated(), |
michael@0 | 550 | // "A description of what we are reporting." |
michael@0 | 551 | // } |
michael@0 | 552 | // }; |
michael@0 | 553 | // |
michael@0 | 554 | // ...somewhere later in the code... |
michael@0 | 555 | // SetThirdPartyMemoryFunctions(MyMemoryReporter::CountingAlloc, |
michael@0 | 556 | // MyMemoryReporter::CountingFree); |
michael@0 | 557 | template<typename T> |
michael@0 | 558 | class CountingAllocatorBase |
michael@0 | 559 | { |
michael@0 | 560 | public: |
michael@0 | 561 | CountingAllocatorBase() |
michael@0 | 562 | { |
michael@0 | 563 | #ifdef DEBUG |
michael@0 | 564 | // There must be only one instance of this class, due to |sAmount| being |
michael@0 | 565 | // static. |
michael@0 | 566 | static bool hasRun = false; |
michael@0 | 567 | MOZ_ASSERT(!hasRun); |
michael@0 | 568 | hasRun = true; |
michael@0 | 569 | #endif |
michael@0 | 570 | } |
michael@0 | 571 | |
michael@0 | 572 | static size_t |
michael@0 | 573 | MemoryAllocated() |
michael@0 | 574 | { |
michael@0 | 575 | return sAmount; |
michael@0 | 576 | } |
michael@0 | 577 | |
michael@0 | 578 | static void* |
michael@0 | 579 | CountingMalloc(size_t size) |
michael@0 | 580 | { |
michael@0 | 581 | void* p = malloc(size); |
michael@0 | 582 | sAmount += MallocSizeOfOnAlloc(p); |
michael@0 | 583 | return p; |
michael@0 | 584 | } |
michael@0 | 585 | |
michael@0 | 586 | static void* |
michael@0 | 587 | CountingCalloc(size_t nmemb, size_t size) |
michael@0 | 588 | { |
michael@0 | 589 | void* p = calloc(nmemb, size); |
michael@0 | 590 | sAmount += MallocSizeOfOnAlloc(p); |
michael@0 | 591 | return p; |
michael@0 | 592 | } |
michael@0 | 593 | |
michael@0 | 594 | static void* |
michael@0 | 595 | CountingRealloc(void* p, size_t size) |
michael@0 | 596 | { |
michael@0 | 597 | size_t oldsize = MallocSizeOfOnFree(p); |
michael@0 | 598 | void *pnew = realloc(p, size); |
michael@0 | 599 | if (pnew) { |
michael@0 | 600 | size_t newsize = MallocSizeOfOnAlloc(pnew); |
michael@0 | 601 | sAmount += newsize - oldsize; |
michael@0 | 602 | } else if (size == 0) { |
michael@0 | 603 | // We asked for a 0-sized (re)allocation of some existing pointer |
michael@0 | 604 | // and received NULL in return. 0-sized allocations are permitted |
michael@0 | 605 | // to either return NULL or to allocate a unique object per call (!). |
michael@0 | 606 | // For a malloc implementation that chooses the second strategy, |
michael@0 | 607 | // that allocation may fail (unlikely, but possible). |
michael@0 | 608 | // |
michael@0 | 609 | // Given a NULL return value and an allocation size of 0, then, we |
michael@0 | 610 | // don't know if that means the original pointer was freed or if |
michael@0 | 611 | // the allocation of the unique object failed. If the original |
michael@0 | 612 | // pointer was freed, then we have nothing to do here. If the |
michael@0 | 613 | // allocation of the unique object failed, the original pointer is |
michael@0 | 614 | // still valid and we ought to undo the decrement from above. |
michael@0 | 615 | // However, we have no way of knowing how the underlying realloc |
michael@0 | 616 | // implementation is behaving. Assuming that the original pointer |
michael@0 | 617 | // was freed is the safest course of action. We do, however, need |
michael@0 | 618 | // to note that we freed memory. |
michael@0 | 619 | sAmount -= oldsize; |
michael@0 | 620 | } else { |
michael@0 | 621 | // realloc failed. The amount allocated hasn't changed. |
michael@0 | 622 | } |
michael@0 | 623 | return pnew; |
michael@0 | 624 | } |
michael@0 | 625 | |
michael@0 | 626 | static void |
michael@0 | 627 | CountingFree(void* p) |
michael@0 | 628 | { |
michael@0 | 629 | sAmount -= MallocSizeOfOnFree(p); |
michael@0 | 630 | free(p); |
michael@0 | 631 | } |
michael@0 | 632 | |
michael@0 | 633 | private: |
michael@0 | 634 | // |sAmount| can be (implicitly) accessed by multiple threads, so it |
michael@0 | 635 | // must be thread-safe. |
michael@0 | 636 | static Atomic<size_t> sAmount; |
michael@0 | 637 | |
michael@0 | 638 | MOZ_DEFINE_MALLOC_SIZE_OF_ON_ALLOC(MallocSizeOfOnAlloc) |
michael@0 | 639 | MOZ_DEFINE_MALLOC_SIZE_OF_ON_FREE(MallocSizeOfOnFree) |
michael@0 | 640 | }; |
michael@0 | 641 | |
michael@0 | 642 | } |
michael@0 | 643 | |
michael@0 | 644 | // This macro assumes the presence of appropriate |aHandleReport| and |aData| |
michael@0 | 645 | // variables. |
michael@0 | 646 | #define MOZ_COLLECT_REPORT(path, kind, units, amount, description) \ |
michael@0 | 647 | aHandleReport->Callback(EmptyCString(), NS_LITERAL_CSTRING(path), \ |
michael@0 | 648 | kind, units, amount, \ |
michael@0 | 649 | NS_LITERAL_CSTRING(description), aData) |
michael@0 | 650 | |
michael@0 | 651 | %} |