toolkit/devtools/server/actors/profiler.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 var startedProfilers = 0;
michael@0 8 var startTime = 0;
michael@0 9
michael@0 10 function getCurrentTime() {
michael@0 11 return (new Date()).getTime() - startTime;
michael@0 12 }
michael@0 13
michael@0 14 /**
michael@0 15 * Creates a ProfilerActor. ProfilerActor provides remote access to the
michael@0 16 * built-in profiler module.
michael@0 17 *
michael@0 18 * ProfilerActor.onGetProfile returns a JavaScript object with data
michael@0 19 * generated by our built-in profiler moduele. It has the following
michael@0 20 * format:
michael@0 21 *
michael@0 22 * {
michael@0 23 * libs: string,
michael@0 24 * meta: {
michael@0 25 * interval: number,
michael@0 26 * platform: string,
michael@0 27 * (...)
michael@0 28 * },
michael@0 29 * threads: [
michael@0 30 * {
michael@0 31 * samples: [
michael@0 32 * {
michael@0 33 * frames: [
michael@0 34 * {
michael@0 35 * line: number,
michael@0 36 * location: string
michael@0 37 * }
michael@0 38 * ],
michael@0 39 * name: string
michael@0 40 * responsiveness: number (in ms)
michael@0 41 * time: number (nspr time)
michael@0 42 * }
michael@0 43 * ]
michael@0 44 * }
michael@0 45 * ]
michael@0 46 * }
michael@0 47 *
michael@0 48 */
michael@0 49 function ProfilerActor(aConnection)
michael@0 50 {
michael@0 51 this._profiler = Cc["@mozilla.org/tools/profiler;1"].getService(Ci.nsIProfiler);
michael@0 52 this._started = false;
michael@0 53 this._observedEvents = [];
michael@0 54 }
michael@0 55
michael@0 56 ProfilerActor.prototype = {
michael@0 57 actorPrefix: "profiler",
michael@0 58
michael@0 59 disconnect: function() {
michael@0 60 for (var event of this._observedEvents) {
michael@0 61 Services.obs.removeObserver(this, event);
michael@0 62 }
michael@0 63
michael@0 64 this.stopProfiler();
michael@0 65 this._profiler = null;
michael@0 66 },
michael@0 67
michael@0 68 stopProfiler: function() {
michael@0 69 // We stop the profiler only after the last client has
michael@0 70 // stopped profiling. Otherwise there's a problem where
michael@0 71 // we stop the profiler as soon as you close the devtools
michael@0 72 // panel in one tab even though there might be other
michael@0 73 // profiler instances running in other tabs.
michael@0 74 if (!this._started) {
michael@0 75 return;
michael@0 76 }
michael@0 77 this._started = false;
michael@0 78 startedProfilers -= 1;
michael@0 79 if (startedProfilers <= 0) {
michael@0 80 this._profiler.StopProfiler();
michael@0 81 }
michael@0 82 },
michael@0 83
michael@0 84 onStartProfiler: function(aRequest) {
michael@0 85 this._profiler.StartProfiler(aRequest.entries, aRequest.interval,
michael@0 86 aRequest.features, aRequest.features.length);
michael@0 87 this._started = true;
michael@0 88 startedProfilers += 1;
michael@0 89 startTime = (new Date()).getTime();
michael@0 90 return { "msg": "profiler started" }
michael@0 91 },
michael@0 92 onStopProfiler: function(aRequest) {
michael@0 93 this.stopProfiler();
michael@0 94 return { "msg": "profiler stopped" }
michael@0 95 },
michael@0 96 onGetProfileStr: function(aRequest) {
michael@0 97 var profileStr = this._profiler.GetProfile();
michael@0 98 return { "profileStr": profileStr }
michael@0 99 },
michael@0 100 onGetProfile: function(aRequest) {
michael@0 101 var profile = this._profiler.getProfileData();
michael@0 102 return { "profile": profile, "currentTime": getCurrentTime() }
michael@0 103 },
michael@0 104 onIsActive: function(aRequest) {
michael@0 105 var isActive = this._profiler.IsActive();
michael@0 106 var currentTime = isActive ? getCurrentTime() : null;
michael@0 107 return { "isActive": isActive, "currentTime": currentTime }
michael@0 108 },
michael@0 109 onGetResponsivenessTimes: function(aRequest) {
michael@0 110 var times = this._profiler.GetResponsivenessTimes({});
michael@0 111 return { "responsivenessTimes": times }
michael@0 112 },
michael@0 113 onGetFeatures: function(aRequest) {
michael@0 114 var features = this._profiler.GetFeatures([]);
michael@0 115 return { "features": features }
michael@0 116 },
michael@0 117 onGetSharedLibraryInformation: function(aRequest) {
michael@0 118 var sharedLibraries = this._profiler.getSharedLibraryInformation();
michael@0 119 return { "sharedLibraryInformation": sharedLibraries }
michael@0 120 },
michael@0 121 onRegisterEventNotifications: function(aRequest) {
michael@0 122 let registered = [];
michael@0 123 for (var event of aRequest.events) {
michael@0 124 if (this._observedEvents.indexOf(event) != -1)
michael@0 125 continue;
michael@0 126 Services.obs.addObserver(this, event, false);
michael@0 127 this._observedEvents.push(event);
michael@0 128 registered.push(event);
michael@0 129 }
michael@0 130 return { registered: registered }
michael@0 131 },
michael@0 132 onUnregisterEventNotifications: function(aRequest) {
michael@0 133 let unregistered = [];
michael@0 134 for (var event of aRequest.events) {
michael@0 135 let idx = this._observedEvents.indexOf(event);
michael@0 136 if (idx == -1)
michael@0 137 continue;
michael@0 138 Services.obs.removeObserver(this, event);
michael@0 139 this._observedEvents.splice(idx, 1);
michael@0 140 unregistered.push(event);
michael@0 141 }
michael@0 142 return { unregistered: unregistered }
michael@0 143 },
michael@0 144 observe: DevToolsUtils.makeInfallible(function(aSubject, aTopic, aData) {
michael@0 145 /*
michael@0 146 * this.conn.send can only transmit acyclic values. However, it is
michael@0 147 * idiomatic for wrapped JS objects like aSubject (and possibly aData?)
michael@0 148 * to have a 'wrappedJSObject' property pointing to themselves.
michael@0 149 *
michael@0 150 * this.conn.send also assumes that it can retain the object it is
michael@0 151 * passed to be handled on later event ticks; and that it's okay to
michael@0 152 * freeze it. Since we don't really know what aSubject and aData are,
michael@0 153 * we need to pass this.conn.send a copy of them, not the originals.
michael@0 154 *
michael@0 155 * We break the cycle and make the copy by JSON.stringifying those
michael@0 156 * values with a replacer that omits properties known to introduce
michael@0 157 * cycles, and then JSON.parsing the result. This spends processor
michael@0 158 * time, but it's simple.
michael@0 159 */
michael@0 160 function cycleBreaker(key, value) {
michael@0 161 if (key === 'wrappedJSObject') {
michael@0 162 return undefined;
michael@0 163 }
michael@0 164 return value;
michael@0 165 }
michael@0 166
michael@0 167 /*
michael@0 168 * If these values are objects with a non-null 'wrappedJSObject' property
michael@0 169 * and aren't Xrays, use their .wrappedJSObject. Otherwise, use the value
michael@0 170 * unchanged.
michael@0 171 */
michael@0 172 aSubject = (aSubject && !Cu.isXrayWrapper(aSubject) && aSubject.wrappedJSObject) || aSubject;
michael@0 173 aData = (aData && !Cu.isXrayWrapper(aData) && aData.wrappedJSObject) || aData;
michael@0 174
michael@0 175 let subj = JSON.parse(JSON.stringify(aSubject, cycleBreaker));
michael@0 176 let data = JSON.parse(JSON.stringify(aData, cycleBreaker));
michael@0 177
michael@0 178 let send = (extra) => {
michael@0 179 data = data || {};
michael@0 180
michael@0 181 if (extra)
michael@0 182 data.extra = extra;
michael@0 183
michael@0 184 this.conn.send({
michael@0 185 from: this.actorID,
michael@0 186 type: "eventNotification",
michael@0 187 event: aTopic,
michael@0 188 subject: subj,
michael@0 189 data: data
michael@0 190 });
michael@0 191 }
michael@0 192
michael@0 193 if (aTopic !== "console-api-profiler")
michael@0 194 return void send();
michael@0 195
michael@0 196 // If the event was generated from console.profile or
michael@0 197 // console.profileEnd we need to start the profiler
michael@0 198 // right away and only then notify our client. Otherwise,
michael@0 199 // we'll lose precious samples.
michael@0 200
michael@0 201 let name = subj.arguments[0];
michael@0 202
michael@0 203 if (subj.action === "profile") {
michael@0 204 let resp = this.onIsActive();
michael@0 205
michael@0 206 if (resp.isActive) {
michael@0 207 return void send({
michael@0 208 name: name,
michael@0 209 currentTime: resp.currentTime,
michael@0 210 action: "profile"
michael@0 211 });
michael@0 212 }
michael@0 213
michael@0 214 this.onStartProfiler({
michael@0 215 entries: 1000000,
michael@0 216 interval: 1,
michael@0 217 features: ["js"]
michael@0 218 });
michael@0 219
michael@0 220 return void send({ currentTime: 0, action: "profile", name: name });
michael@0 221 }
michael@0 222
michael@0 223 if (subj.action === "profileEnd") {
michael@0 224 let resp = this.onGetProfile();
michael@0 225 resp.action = "profileEnd";
michael@0 226 resp.name = name;
michael@0 227 send(resp);
michael@0 228 }
michael@0 229
michael@0 230 return undefined; // Otherwise xpcshell tests fail.
michael@0 231 }, "ProfilerActor.prototype.observe"),
michael@0 232 };
michael@0 233
michael@0 234 /**
michael@0 235 * The request types this actor can handle.
michael@0 236 */
michael@0 237 ProfilerActor.prototype.requestTypes = {
michael@0 238 "startProfiler": ProfilerActor.prototype.onStartProfiler,
michael@0 239 "stopProfiler": ProfilerActor.prototype.onStopProfiler,
michael@0 240 "getProfileStr": ProfilerActor.prototype.onGetProfileStr,
michael@0 241 "getProfile": ProfilerActor.prototype.onGetProfile,
michael@0 242 "isActive": ProfilerActor.prototype.onIsActive,
michael@0 243 "getResponsivenessTimes": ProfilerActor.prototype.onGetResponsivenessTimes,
michael@0 244 "getFeatures": ProfilerActor.prototype.onGetFeatures,
michael@0 245 "getSharedLibraryInformation": ProfilerActor.prototype.onGetSharedLibraryInformation,
michael@0 246 "registerEventNotifications": ProfilerActor.prototype.onRegisterEventNotifications,
michael@0 247 "unregisterEventNotifications": ProfilerActor.prototype.onUnregisterEventNotifications
michael@0 248 };
michael@0 249
michael@0 250 DebuggerServer.addGlobalActor(ProfilerActor, "profilerActor");

mercurial