Bug 575870 - Enable the firefox button on xp themed, classic, and aero basic. r=dao...
[mozilla-central.git] / xpcom / base / nsMemoryReporterManager.cpp
blobb1597255f226248c88c927b4f764dacf3a91bdef
1 /* -*- Mode: C++; tab-width: 50; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * mozilla.org
19 * Portions created by the Initial Developer are Copyright (C) 2008
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Vladimir Vukicevic <vladimir@pobox.com> (original author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsCOMPtr.h"
40 #include "nsServiceManagerUtils.h"
41 #include "nsMemoryReporterManager.h"
43 #include "nsArrayEnumerator.h"
45 /**
46 ** memory reporter implementation for jemalloc and OSX malloc,
47 ** to obtain info on total memory in use (that we know about,
48 ** at least -- on OSX, there are sometimes other zones in use).
49 **/
51 #if defined(MOZ_MEMORY)
52 # if defined(XP_WIN) || defined(SOLARIS)
53 # define HAVE_JEMALLOC_STATS 1
54 # include "jemalloc.h"
55 # elif defined(XP_LINUX)
56 # define HAVE_JEMALLOC_STATS 1
57 # include "jemalloc_types.h"
58 // jemalloc is directly linked into firefox-bin; libxul doesn't link
59 // with it. So if we tried to use jemalloc_stats directly here, it
60 // wouldn't be defined. Instead, we don't include the jemalloc header
61 // and weakly link against jemalloc_stats.
63 // NB: we don't null-check this symbol at runtime because we expect it
64 // to have been resolved. If it hasn't, the crash jumping to NULL
65 // will indicate the bug.
66 extern "C" {
67 extern void jemalloc_stats(jemalloc_stats_t* stats)
68 NS_VISIBILITY_DEFAULT __attribute__((weak));
70 # endif // XP_LINUX
71 #endif // MOZ_MEMORY
73 #if HAVE_JEMALLOC_STATS
74 # define HAVE_MALLOC_REPORTERS 1
76 PRInt64 getMallocMapped(void *) {
77 jemalloc_stats_t stats;
78 jemalloc_stats(&stats);
79 return (PRInt64) stats.mapped;
82 PRInt64 getMallocAllocated(void *) {
83 jemalloc_stats_t stats;
84 jemalloc_stats(&stats);
85 return (PRInt64) stats.allocated;
88 PRInt64 getMallocCommitted(void *) {
89 jemalloc_stats_t stats;
90 jemalloc_stats(&stats);
91 return (PRInt64) stats.committed;
94 PRInt64 getMallocDirty(void *) {
95 jemalloc_stats_t stats;
96 jemalloc_stats(&stats);
97 return (PRInt64) stats.dirty;
100 #elif defined(XP_MACOSX) && !defined(MOZ_MEMORY)
101 #define HAVE_MALLOC_REPORTERS 1
102 #include <malloc/malloc.h>
104 static PRInt64 getMallocAllocated(void *) {
105 struct mstats stats = mstats();
106 return (PRInt64) stats.bytes_used;
109 static PRInt64 getMallocMapped(void *) {
110 struct mstats stats = mstats();
111 return (PRInt64) stats.bytes_total;
114 static PRInt64 getMallocDefaultCommitted(void *) {
115 malloc_statistics_t stats;
116 malloc_zone_statistics(malloc_default_zone(), &stats);
117 return stats.size_in_use;
120 static PRInt64 getMallocDefaultAllocated(void *) {
121 malloc_statistics_t stats;
122 malloc_zone_statistics(malloc_default_zone(), &stats);
123 return stats.size_allocated;
126 #endif
129 #ifdef HAVE_MALLOC_REPORTERS
130 NS_MEMORY_REPORTER_IMPLEMENT(MallocAllocated,
131 "malloc/allocated",
132 "Malloc bytes allocated (in use by application)",
133 getMallocAllocated,
134 NULL)
136 NS_MEMORY_REPORTER_IMPLEMENT(MallocMapped,
137 "malloc/mapped",
138 "Malloc bytes mapped (not necessarily committed)",
139 getMallocMapped,
140 NULL)
142 #if defined(HAVE_JEMALLOC_STATS)
143 NS_MEMORY_REPORTER_IMPLEMENT(MallocCommitted,
144 "malloc/committed",
145 "Malloc bytes committed (readable/writable)",
146 getMallocCommitted,
147 NULL)
149 NS_MEMORY_REPORTER_IMPLEMENT(MallocDirty,
150 "malloc/dirty",
151 "Malloc bytes dirty (committed unused pages)",
152 getMallocDirty,
153 NULL)
154 #elif defined(XP_MACOSX) && !defined(MOZ_MEMORY)
155 NS_MEMORY_REPORTER_IMPLEMENT(MallocDefaultCommitted,
156 "malloc/zone0/committed",
157 "Malloc bytes committed (r/w) in default zone",
158 getMallocDefaultCommitted,
159 NULL)
161 NS_MEMORY_REPORTER_IMPLEMENT(MallocDefaultAllocated,
162 "malloc/zone0/allocated",
163 "Malloc bytes allocated (in use) in default zone",
164 getMallocDefaultAllocated,
165 NULL)
166 #endif
168 #endif
170 #if defined(XP_WIN) && !defined(WINCE)
171 #include <windows.h>
172 #include <psapi.h>
174 static PRInt64 GetWin32PrivateBytes(void *) {
175 #if MOZ_WINSDK_TARGETVER >= MOZ_NTDDI_LONGHORN
176 PROCESS_MEMORY_COUNTERS_EX pmcex;
177 pmcex.cb = sizeof(PROCESS_MEMORY_COUNTERS_EX);
179 if (!GetProcessMemoryInfo(GetCurrentProcess(),
180 (PPROCESS_MEMORY_COUNTERS) &pmcex,
181 sizeof(PROCESS_MEMORY_COUNTERS_EX)))
182 return 0;
184 return pmcex.PrivateUsage;
185 #else
186 return 0;
187 #endif
190 static PRInt64 GetWin32WorkingSetSize(void *) {
191 PROCESS_MEMORY_COUNTERS pmc;
192 pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
194 if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
195 return 0;
197 return pmc.WorkingSetSize;
200 NS_MEMORY_REPORTER_IMPLEMENT(Win32WorkingSetSize,
201 "win32/workingset",
202 "Win32 working set size",
203 GetWin32WorkingSetSize,
204 nsnull);
206 NS_MEMORY_REPORTER_IMPLEMENT(Win32PrivateBytes,
207 "win32/privatebytes",
208 "Win32 private bytes (cannot be shared with other processes). (Available only on Windows XP SP2 or later.)",
209 GetWin32PrivateBytes,
210 nsnull);
211 #endif
214 ** nsMemoryReporterManager implementation
217 NS_IMPL_ISUPPORTS1(nsMemoryReporterManager, nsIMemoryReporterManager)
219 NS_IMETHODIMP
220 nsMemoryReporterManager::Init()
223 * Register our core reporters
225 #define REGISTER(_x) RegisterReporter(new NS_MEMORY_REPORTER_NAME(_x))
228 * Register our core jemalloc/malloc reporters
230 #ifdef HAVE_MALLOC_REPORTERS
231 REGISTER(MallocAllocated);
232 REGISTER(MallocMapped);
234 #if defined(HAVE_JEMALLOC_STATS)
235 REGISTER(MallocCommitted);
236 REGISTER(MallocDirty);
237 #elif defined(XP_MACOSX) && !defined(MOZ_MEMORY)
238 REGISTER(MallocDefaultCommitted);
239 REGISTER(MallocDefaultAllocated);
240 #endif
241 #endif
243 #if defined(XP_WIN) && !defined(WINCE)
244 #if MOZ_WINSDK_TARGETVER >= MOZ_NTDDI_LONGHORN
245 REGISTER(Win32PrivateBytes);
246 #endif
247 REGISTER(Win32WorkingSetSize);
248 #endif
250 return NS_OK;
253 NS_IMETHODIMP
254 nsMemoryReporterManager::EnumerateReporters(nsISimpleEnumerator **result)
256 return NS_NewArrayEnumerator(result, mReporters);
259 NS_IMETHODIMP
260 nsMemoryReporterManager::RegisterReporter(nsIMemoryReporter *reporter)
262 if (mReporters.IndexOf(reporter) != -1)
263 return NS_ERROR_FAILURE;
265 mReporters.AppendObject(reporter);
266 return NS_OK;
269 NS_IMETHODIMP
270 nsMemoryReporterManager::UnregisterReporter(nsIMemoryReporter *reporter)
272 if (!mReporters.RemoveObject(reporter))
273 return NS_ERROR_FAILURE;
275 return NS_OK;
278 NS_COM nsresult
279 NS_RegisterMemoryReporter (nsIMemoryReporter *reporter)
281 nsCOMPtr<nsIMemoryReporterManager> mgr = do_GetService("@mozilla.org/memory-reporter-manager;1");
282 if (mgr == nsnull)
283 return NS_ERROR_FAILURE;
284 return mgr->RegisterReporter(reporter);
287 NS_COM nsresult
288 NS_UnregisterMemoryReporter (nsIMemoryReporter *reporter)
290 nsCOMPtr<nsIMemoryReporterManager> mgr = do_GetService("@mozilla.org/memory-reporter-manager;1");
291 if (mgr == nsnull)
292 return NS_ERROR_FAILURE;
293 return mgr->UnregisterReporter(reporter);