bug 313956: expand installer .exe contents to make complete mar. r=ted.
[gecko.git] / xpcom / base / nsMemoryReporterManager.cpp
blobc380d2f91e9115c600adb9c3c05e7ae34f387710
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"
42 #include "nsArrayEnumerator.h"
44 /**
45 ** memory reporter implementation for jemalloc and OSX malloc,
46 ** to obtain info on total memory in use (that we know about,
47 ** at least -- on OSX, there are sometimes other zones in use).
48 **/
50 #if defined(MOZ_MEMORY)
51 # if defined(XP_WIN) || defined(SOLARIS) || defined(ANDROID)
52 # define HAVE_JEMALLOC_STATS 1
53 # include "jemalloc.h"
54 # elif defined(XP_LINUX)
55 # define HAVE_JEMALLOC_STATS 1
56 # include "jemalloc_types.h"
57 // jemalloc is directly linked into firefox-bin; libxul doesn't link
58 // with it. So if we tried to use jemalloc_stats directly here, it
59 // wouldn't be defined. Instead, we don't include the jemalloc header
60 // and weakly link against jemalloc_stats.
61 extern "C" {
62 extern void jemalloc_stats(jemalloc_stats_t* stats)
63 NS_VISIBILITY_DEFAULT __attribute__((weak));
65 # endif // XP_LINUX
66 #endif // MOZ_MEMORY
68 #if HAVE_JEMALLOC_STATS
69 # define HAVE_MALLOC_REPORTERS 1
71 PRInt64 getMallocMapped(void *) {
72 jemalloc_stats_t stats;
73 jemalloc_stats(&stats);
74 return (PRInt64) stats.mapped;
77 PRInt64 getMallocAllocated(void *) {
78 jemalloc_stats_t stats;
79 jemalloc_stats(&stats);
80 return (PRInt64) stats.allocated;
83 PRInt64 getMallocCommitted(void *) {
84 jemalloc_stats_t stats;
85 jemalloc_stats(&stats);
86 return (PRInt64) stats.committed;
89 PRInt64 getMallocDirty(void *) {
90 jemalloc_stats_t stats;
91 jemalloc_stats(&stats);
92 return (PRInt64) stats.dirty;
95 #elif defined(XP_MACOSX) && !defined(MOZ_MEMORY)
96 #define HAVE_MALLOC_REPORTERS 1
97 #include <malloc/malloc.h>
99 static PRInt64 getMallocAllocated(void *) {
100 struct mstats stats = mstats();
101 return (PRInt64) stats.bytes_used;
104 static PRInt64 getMallocMapped(void *) {
105 struct mstats stats = mstats();
106 return (PRInt64) stats.bytes_total;
109 static PRInt64 getMallocDefaultCommitted(void *) {
110 malloc_statistics_t stats;
111 malloc_zone_statistics(malloc_default_zone(), &stats);
112 return stats.size_in_use;
115 static PRInt64 getMallocDefaultAllocated(void *) {
116 malloc_statistics_t stats;
117 malloc_zone_statistics(malloc_default_zone(), &stats);
118 return stats.size_allocated;
121 #endif
124 #ifdef HAVE_MALLOC_REPORTERS
125 NS_MEMORY_REPORTER_IMPLEMENT(MallocAllocated,
126 "malloc/allocated",
127 "Malloc bytes allocated (in use by application)",
128 getMallocAllocated,
129 NULL)
131 NS_MEMORY_REPORTER_IMPLEMENT(MallocMapped,
132 "malloc/mapped",
133 "Malloc bytes mapped (not necessarily committed)",
134 getMallocMapped,
135 NULL)
137 #if defined(HAVE_JEMALLOC_STATS)
138 NS_MEMORY_REPORTER_IMPLEMENT(MallocCommitted,
139 "malloc/committed",
140 "Malloc bytes committed (readable/writable)",
141 getMallocCommitted,
142 NULL)
144 NS_MEMORY_REPORTER_IMPLEMENT(MallocDirty,
145 "malloc/dirty",
146 "Malloc bytes dirty (committed unused pages)",
147 getMallocDirty,
148 NULL)
149 #elif defined(XP_MACOSX) && !defined(MOZ_MEMORY)
150 NS_MEMORY_REPORTER_IMPLEMENT(MallocDefaultCommitted,
151 "malloc/zone0/committed",
152 "Malloc bytes committed (r/w) in default zone",
153 getMallocDefaultCommitted,
154 NULL)
156 NS_MEMORY_REPORTER_IMPLEMENT(MallocDefaultAllocated,
157 "malloc/zone0/allocated",
158 "Malloc bytes allocated (in use) in default zone",
159 getMallocDefaultAllocated,
160 NULL)
161 #endif
163 #endif
165 #if defined(XP_WIN) && !defined(WINCE)
166 #include <windows.h>
167 #include <psapi.h>
169 static PRInt64 GetWin32PrivateBytes(void *) {
170 #if MOZ_WINSDK_TARGETVER >= MOZ_NTDDI_LONGHORN
171 PROCESS_MEMORY_COUNTERS_EX pmcex;
172 pmcex.cb = sizeof(PROCESS_MEMORY_COUNTERS_EX);
174 if (!GetProcessMemoryInfo(GetCurrentProcess(),
175 (PPROCESS_MEMORY_COUNTERS) &pmcex,
176 sizeof(PROCESS_MEMORY_COUNTERS_EX)))
177 return 0;
179 return pmcex.PrivateUsage;
180 #else
181 return 0;
182 #endif
185 static PRInt64 GetWin32WorkingSetSize(void *) {
186 PROCESS_MEMORY_COUNTERS pmc;
187 pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
189 if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
190 return 0;
192 return pmc.WorkingSetSize;
195 NS_MEMORY_REPORTER_IMPLEMENT(Win32WorkingSetSize,
196 "win32/workingset",
197 "Win32 working set size",
198 GetWin32WorkingSetSize,
199 nsnull);
201 NS_MEMORY_REPORTER_IMPLEMENT(Win32PrivateBytes,
202 "win32/privatebytes",
203 "Win32 private bytes (cannot be shared with other processes). (Available only on Windows XP SP2 or later.)",
204 GetWin32PrivateBytes,
205 nsnull);
206 #endif
209 ** nsMemoryReporterManager implementation
212 NS_IMPL_THREADSAFE_ISUPPORTS1(nsMemoryReporterManager, nsIMemoryReporterManager)
214 NS_IMETHODIMP
215 nsMemoryReporterManager::Init()
217 #if HAVE_JEMALLOC_STATS && defined(XP_LINUX)
218 if (!jemalloc_stats)
219 return NS_ERROR_FAILURE;
220 #endif
222 * Register our core reporters
224 #define REGISTER(_x) RegisterReporter(new NS_MEMORY_REPORTER_NAME(_x))
227 * Register our core jemalloc/malloc reporters
229 #ifdef HAVE_MALLOC_REPORTERS
230 REGISTER(MallocAllocated);
231 REGISTER(MallocMapped);
233 #if defined(HAVE_JEMALLOC_STATS)
234 REGISTER(MallocCommitted);
235 REGISTER(MallocDirty);
236 #elif defined(XP_MACOSX) && !defined(MOZ_MEMORY)
237 REGISTER(MallocDefaultCommitted);
238 REGISTER(MallocDefaultAllocated);
239 #endif
240 #endif
242 #if defined(XP_WIN) && !defined(WINCE)
243 #if MOZ_WINSDK_TARGETVER >= MOZ_NTDDI_LONGHORN
244 REGISTER(Win32PrivateBytes);
245 #endif
246 REGISTER(Win32WorkingSetSize);
247 #endif
249 return NS_OK;
252 nsMemoryReporterManager::nsMemoryReporterManager()
253 : mMutex("nsMemoryReporterManager::mMutex")
257 nsMemoryReporterManager::~nsMemoryReporterManager()
261 NS_IMETHODIMP
262 nsMemoryReporterManager::EnumerateReporters(nsISimpleEnumerator **result)
264 nsresult rv;
265 mozilla::MutexAutoLock autoLock(mMutex);
266 rv = NS_NewArrayEnumerator(result, mReporters);
267 return rv;
270 NS_IMETHODIMP
271 nsMemoryReporterManager::RegisterReporter(nsIMemoryReporter *reporter)
273 mozilla::MutexAutoLock autoLock(mMutex);
274 if (mReporters.IndexOf(reporter) != -1)
275 return NS_ERROR_FAILURE;
277 mReporters.AppendObject(reporter);
278 return NS_OK;
281 NS_IMETHODIMP
282 nsMemoryReporterManager::UnregisterReporter(nsIMemoryReporter *reporter)
284 mozilla::MutexAutoLock autoLock(mMutex);
285 if (!mReporters.RemoveObject(reporter))
286 return NS_ERROR_FAILURE;
288 return NS_OK;
291 NS_IMPL_ISUPPORTS1(nsMemoryReporter, nsIMemoryReporter)
293 nsMemoryReporter::nsMemoryReporter(nsCString& prefix,
294 nsCString& path,
295 nsCString& desc,
296 PRInt64 memoryUsed)
297 : mDesc(desc)
298 , mMemoryUsed(memoryUsed)
300 if (!prefix.IsEmpty()) {
301 mPath.Append(prefix);
302 mPath.Append(NS_LITERAL_CSTRING(" - "));
304 mPath.Append(path);
307 nsMemoryReporter::~nsMemoryReporter()
311 NS_IMETHODIMP nsMemoryReporter::GetPath(char **aPath)
313 *aPath = strdup(mPath.get());
314 return NS_OK;
317 NS_IMETHODIMP nsMemoryReporter::GetDescription(char **aDescription)
319 *aDescription = strdup(mDesc.get());
320 return NS_OK;
323 NS_IMETHODIMP nsMemoryReporter::GetMemoryUsed(PRInt64 *aMemoryUsed)
325 *aMemoryUsed = mMemoryUsed;
326 return NS_OK;
330 NS_COM nsresult
331 NS_RegisterMemoryReporter (nsIMemoryReporter *reporter)
333 nsCOMPtr<nsIMemoryReporterManager> mgr = do_GetService("@mozilla.org/memory-reporter-manager;1");
334 if (mgr == nsnull)
335 return NS_ERROR_FAILURE;
336 return mgr->RegisterReporter(reporter);
339 NS_COM nsresult
340 NS_UnregisterMemoryReporter (nsIMemoryReporter *reporter)
342 nsCOMPtr<nsIMemoryReporterManager> mgr = do_GetService("@mozilla.org/memory-reporter-manager;1");
343 if (mgr == nsnull)
344 return NS_ERROR_FAILURE;
345 return mgr->UnregisterReporter(reporter);