On x86 compilers without fastcall, simulate it when invoking traces and un-simulate...
[wine-gecko.git] / netwerk / protocol / about / src / nsAboutBloat.cpp
blob83a3b79e46283128525e6a1968e8fbd61741820a
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsTraceRefcntImpl.h"
40 // if NS_BUILD_REFCNT_LOGGING isn't defined, don't try to build
41 #ifdef NS_BUILD_REFCNT_LOGGING
43 #include "nsAboutBloat.h"
44 #include "nsIIOService.h"
45 #include "nsIServiceManager.h"
46 #include "nsStringStream.h"
47 #include "nsXPIDLString.h"
48 #include "nsIURI.h"
49 #include "prtime.h"
50 #include "nsCOMPtr.h"
51 #include "nsIFileStreams.h"
52 #include "nsNetUtil.h"
53 #include "nsDirectoryServiceDefs.h"
54 #include "nsILocalFile.h"
56 #ifdef XP_MAC
57 extern "C" void GC_gcollect(void);
58 #else
59 static void GC_gcollect() {}
60 #endif
62 NS_IMPL_ISUPPORTS1(nsAboutBloat, nsIAboutModule)
64 NS_IMETHODIMP
65 nsAboutBloat::NewChannel(nsIURI *aURI, nsIChannel **result)
67 NS_ENSURE_ARG_POINTER(aURI);
68 nsresult rv;
69 nsCAutoString path;
70 rv = aURI->GetPath(path);
71 if (NS_FAILED(rv)) return rv;
73 nsTraceRefcntImpl::StatisticsType statType = nsTraceRefcntImpl::ALL_STATS;
74 PRBool clear = PR_FALSE;
75 PRBool leaks = PR_FALSE;
77 PRInt32 pos = path.Find("?");
78 if (pos > 0) {
79 nsCAutoString param;
80 (void)path.Right(param, path.Length() - (pos+1));
81 if (param.EqualsLiteral("new"))
82 statType = nsTraceRefcntImpl::NEW_STATS;
83 else if (param.EqualsLiteral("clear"))
84 clear = PR_TRUE;
85 else if (param.EqualsLiteral("leaks"))
86 leaks = PR_TRUE;
89 nsCOMPtr<nsIInputStream> inStr;
90 if (clear) {
91 nsTraceRefcntImpl::ResetStatistics();
93 const char* msg = "Bloat statistics cleared.";
94 rv = NS_NewCStringInputStream(getter_AddRefs(inStr), nsDependentCString(msg));
95 if (NS_FAILED(rv)) return rv;
97 else if (leaks) {
98 // dump the current set of leaks.
99 GC_gcollect();
101 const char* msg = "Memory leaks dumped.";
102 rv = NS_NewCStringInputStream(getter_AddRefs(inStr), nsDependentCString(msg));
103 if (NS_FAILED(rv)) return rv;
105 else {
106 nsCOMPtr<nsIFile> file;
107 rv = NS_GetSpecialDirectory(NS_OS_CURRENT_PROCESS_DIR,
108 getter_AddRefs(file));
109 if (NS_FAILED(rv)) return rv;
111 rv = file->AppendNative(NS_LITERAL_CSTRING("bloatlogs"));
112 if (NS_FAILED(rv)) return rv;
114 PRBool exists;
115 rv = file->Exists(&exists);
116 if (NS_FAILED(rv)) return rv;
118 if (!exists) {
119 // On all the platforms that I know use permissions,
120 // directories need to have the executable flag set
121 // if you want to do anything inside the directory.
122 rv = file->Create(nsIFile::DIRECTORY_TYPE, 0755);
123 if (NS_FAILED(rv)) return rv;
126 nsCAutoString dumpFileName;
127 if (statType == nsTraceRefcntImpl::ALL_STATS)
128 dumpFileName.AssignLiteral("all-");
129 else
130 dumpFileName.AssignLiteral("new-");
131 PRExplodedTime expTime;
132 PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &expTime);
133 char time[128];
134 PR_FormatTimeUSEnglish(time, 128, "%Y-%m-%d-%H%M%S.txt", &expTime);
135 dumpFileName += time;
136 rv = file->AppendNative(dumpFileName);
137 if (NS_FAILED(rv)) return rv;
139 FILE* out;
140 nsCOMPtr<nsILocalFile> lfile = do_QueryInterface(file);
141 if (lfile == nsnull)
142 return NS_ERROR_FAILURE;
143 rv = lfile->OpenANSIFileDesc("w", &out);
144 if (NS_FAILED(rv)) return rv;
146 rv = nsTraceRefcntImpl::DumpStatistics(statType, out);
147 ::fclose(out);
148 if (NS_FAILED(rv)) return rv;
150 rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), file);
151 if (NS_FAILED(rv)) return rv;
154 nsIChannel* channel;
155 rv = NS_NewInputStreamChannel(&channel, aURI, inStr,
156 NS_LITERAL_CSTRING("text/plain"),
157 NS_LITERAL_CSTRING("utf-8"));
158 if (NS_FAILED(rv)) return rv;
160 *result = channel;
161 return rv;
164 NS_IMETHODIMP
165 nsAboutBloat::GetURIFlags(nsIURI *aURI, PRUint32 *result)
167 *result = 0;
168 return NS_OK;
171 NS_METHOD
172 nsAboutBloat::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
174 nsAboutBloat* about = new nsAboutBloat();
175 if (about == nsnull)
176 return NS_ERROR_OUT_OF_MEMORY;
177 NS_ADDREF(about);
178 nsresult rv = about->QueryInterface(aIID, aResult);
179 NS_RELEASE(about);
180 return rv;
183 ////////////////////////////////////////////////////////////////////////////////
184 #endif /* NS_BUILD_REFCNT_LOGGING */