1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
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/
9 : *
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.
14 : *
15 : * The Initial Developer of the Original Code is
16 : * Mozilla Foundation
17 : * Portions created by the Initial Developer are Copyright (C) 2011
18 : * the Initial Developer. All Rights Reserved.
19 : *
20 : * Contributor(s):
21 : * Benoit Girard <bgirard@mozilla.com>
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 2 or later (the "GPL"), or
25 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #include <string>
38 : #include <sstream>
39 : #ifdef MOZ_INSTRUMENT_EVENT_LOOP
40 : #include "EventTracer.h"
41 : #endif
42 : #include "sampler.h"
43 : #include "nsProfiler.h"
44 : #include "nsMemory.h"
45 : #include "shared-libraries.h"
46 : #include "nsString.h"
47 : #include "jsapi.h"
48 :
49 : using std::string;
50 :
51 0 : NS_IMPL_ISUPPORTS1(nsProfiler, nsIProfiler)
52 :
53 :
54 0 : nsProfiler::nsProfiler()
55 : {
56 0 : }
57 :
58 :
59 : NS_IMETHODIMP
60 0 : nsProfiler::StartProfiler(PRUint32 aInterval, PRUint32 aEntries,
61 : const char** aFeatures, PRUint32 aFeatureCount)
62 : {
63 0 : SAMPLER_START(aInterval, aEntries, aFeatures, aFeatureCount);
64 : #ifdef MOZ_INSTRUMENT_EVENT_LOOP
65 0 : mozilla::InitEventTracing();
66 : #endif
67 0 : return NS_OK;
68 : }
69 :
70 : NS_IMETHODIMP
71 0 : nsProfiler::StopProfiler()
72 : {
73 0 : SAMPLER_STOP();
74 0 : return NS_OK;
75 : }
76 :
77 : NS_IMETHODIMP
78 0 : nsProfiler::GetProfile(char **aProfile)
79 : {
80 0 : char *profile = SAMPLER_GET_PROFILE();
81 0 : if (profile) {
82 0 : PRUint32 len = strlen(profile);
83 : char *profileStr = static_cast<char *>
84 0 : (nsMemory::Clone(profile, (len + 1) * sizeof(char)));
85 0 : profileStr[len] = '\0';
86 0 : *aProfile = profileStr;
87 0 : free(profile);
88 : }
89 0 : return NS_OK;
90 : }
91 :
92 : static void
93 0 : AddSharedLibraryInfoToStream(std::ostream& aStream, SharedLibrary& aLib)
94 : {
95 0 : aStream << "{";
96 0 : aStream << "\"start\":" << aLib.GetStart();
97 0 : aStream << ",\"end\":" << aLib.GetEnd();
98 0 : aStream << ",\"name\":\"" << aLib.GetName() << "\"";
99 0 : aStream << "}";
100 0 : }
101 :
102 : static std::string
103 0 : GetSharedLibraryInfoString()
104 : {
105 0 : SharedLibraryInfo info = SharedLibraryInfo::GetInfoForSelf();
106 0 : if (info.GetSize() == 0)
107 0 : return "[]";
108 :
109 0 : std::ostringstream os;
110 0 : os << "[";
111 0 : AddSharedLibraryInfoToStream(os, info.GetEntry(0));
112 :
113 0 : for (size_t i = 1; i < info.GetSize(); i++) {
114 0 : os << ",";
115 0 : AddSharedLibraryInfoToStream(os, info.GetEntry(i));
116 : }
117 :
118 0 : os << "]";
119 0 : return os.str();
120 : }
121 :
122 : NS_IMETHODIMP
123 0 : nsProfiler::GetSharedLibraryInformation(nsAString& aOutString)
124 : {
125 0 : aOutString.Assign(NS_ConvertUTF8toUTF16(GetSharedLibraryInfoString().c_str()));
126 0 : return NS_OK;
127 : }
128 :
129 :
130 :
131 0 : NS_IMETHODIMP nsProfiler::GetProfileData(JSContext* aCx, jsval* aResult)
132 : {
133 0 : JSObject *obj = SAMPLER_GET_PROFILE_DATA(aCx);
134 0 : if (!obj)
135 0 : return NS_ERROR_FAILURE;
136 :
137 0 : *aResult = OBJECT_TO_JSVAL(obj);
138 0 : return NS_OK;
139 : }
140 :
141 : NS_IMETHODIMP
142 0 : nsProfiler::IsActive(bool *aIsActive)
143 : {
144 0 : *aIsActive = SAMPLER_IS_ACTIVE();
145 0 : return NS_OK;
146 : }
147 :
148 : NS_IMETHODIMP
149 0 : nsProfiler::GetResponsivenessTimes(PRUint32 *aCount, double **aResult)
150 : {
151 0 : unsigned int len = 100;
152 0 : const double* times = SAMPLER_GET_RESPONSIVENESS();
153 0 : if (!times) {
154 0 : *aCount = 0;
155 0 : *aResult = nsnull;
156 0 : return NS_OK;
157 : }
158 :
159 : double *fs = static_cast<double *>
160 0 : (nsMemory::Clone(times, len * sizeof(double)));
161 :
162 0 : *aCount = len;
163 0 : *aResult = fs;
164 :
165 0 : return NS_OK;
166 : }
167 :
168 : NS_IMETHODIMP
169 0 : nsProfiler::GetFeatures(PRUint32 *aCount, char ***aFeatures)
170 : {
171 0 : PRUint32 len = 0;
172 :
173 0 : const char **features = SAMPLER_GET_FEATURES();
174 0 : if (!features) {
175 0 : *aCount = 0;
176 0 : *aFeatures = nsnull;
177 0 : return NS_OK;
178 : }
179 :
180 0 : while (features[len]) {
181 0 : len++;
182 : }
183 :
184 : char **featureList = static_cast<char **>
185 0 : (nsMemory::Alloc(len * sizeof(char*)));
186 :
187 0 : for (size_t i = 0; i < len; i++) {
188 0 : PRUint32 strLen = strlen(features[i]);
189 0 : featureList[i] = static_cast<char *>
190 0 : (nsMemory::Clone(features[i], (strLen + 1) * sizeof(char)));
191 : }
192 :
193 0 : *aFeatures = featureList;
194 0 : *aCount = len;
195 0 : return NS_OK;
196 : }
|