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
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2008
20 * the Initial Developer. All Rights Reserved.
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 ***** */
40 #include "nsServiceManagerUtils.h"
41 #include "nsMemoryReporterManager.h"
42 #include "nsArrayEnumerator.h"
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).
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.
62 extern void jemalloc_stats(jemalloc_stats_t
* stats
)
63 NS_VISIBILITY_DEFAULT
__attribute__((weak
));
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
;
124 #ifdef HAVE_MALLOC_REPORTERS
125 NS_MEMORY_REPORTER_IMPLEMENT(MallocAllocated
,
127 "Malloc bytes allocated (in use by application)",
131 NS_MEMORY_REPORTER_IMPLEMENT(MallocMapped
,
133 "Malloc bytes mapped (not necessarily committed)",
137 #if defined(HAVE_JEMALLOC_STATS)
138 NS_MEMORY_REPORTER_IMPLEMENT(MallocCommitted
,
140 "Malloc bytes committed (readable/writable)",
144 NS_MEMORY_REPORTER_IMPLEMENT(MallocDirty
,
146 "Malloc bytes dirty (committed unused pages)",
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
,
156 NS_MEMORY_REPORTER_IMPLEMENT(MallocDefaultAllocated
,
157 "malloc/zone0/allocated",
158 "Malloc bytes allocated (in use) in default zone",
159 getMallocDefaultAllocated
,
165 #if defined(XP_WIN) && !defined(WINCE)
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
)))
179 return pmcex
.PrivateUsage
;
185 static PRInt64
GetWin32WorkingSetSize(void *) {
186 PROCESS_MEMORY_COUNTERS pmc
;
187 pmc
.cb
= sizeof(PROCESS_MEMORY_COUNTERS
);
189 if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc
, sizeof(pmc
)))
192 return pmc
.WorkingSetSize
;
195 NS_MEMORY_REPORTER_IMPLEMENT(Win32WorkingSetSize
,
197 "Win32 working set size",
198 GetWin32WorkingSetSize
,
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
,
209 ** nsMemoryReporterManager implementation
212 NS_IMPL_THREADSAFE_ISUPPORTS1(nsMemoryReporterManager
, nsIMemoryReporterManager
)
215 nsMemoryReporterManager::Init()
217 #if HAVE_JEMALLOC_STATS && defined(XP_LINUX)
219 return NS_ERROR_FAILURE
;
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
);
242 #if defined(XP_WIN) && !defined(WINCE)
243 #if MOZ_WINSDK_TARGETVER >= MOZ_NTDDI_LONGHORN
244 REGISTER(Win32PrivateBytes
);
246 REGISTER(Win32WorkingSetSize
);
252 nsMemoryReporterManager::nsMemoryReporterManager()
253 : mMutex("nsMemoryReporterManager::mMutex")
257 nsMemoryReporterManager::~nsMemoryReporterManager()
262 nsMemoryReporterManager::EnumerateReporters(nsISimpleEnumerator
**result
)
265 mozilla::MutexAutoLock
autoLock(mMutex
);
266 rv
= NS_NewArrayEnumerator(result
, mReporters
);
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
);
282 nsMemoryReporterManager::UnregisterReporter(nsIMemoryReporter
*reporter
)
284 mozilla::MutexAutoLock
autoLock(mMutex
);
285 if (!mReporters
.RemoveObject(reporter
))
286 return NS_ERROR_FAILURE
;
292 NS_RegisterMemoryReporter (nsIMemoryReporter
*reporter
)
294 nsCOMPtr
<nsIMemoryReporterManager
> mgr
= do_GetService("@mozilla.org/memory-reporter-manager;1");
296 return NS_ERROR_FAILURE
;
297 return mgr
->RegisterReporter(reporter
);
301 NS_UnregisterMemoryReporter (nsIMemoryReporter
*reporter
)
303 nsCOMPtr
<nsIMemoryReporterManager
> mgr
= do_GetService("@mozilla.org/memory-reporter-manager;1");
305 return NS_ERROR_FAILURE
;
306 return mgr
->UnregisterReporter(reporter
);