OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2014 Google Inc. All rights reserved. | 2 * Copyright (C) 2014 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * | 10 * |
11 * 2. Redistributions in binary form must reproduce the above | 11 * 2. Redistributions in binary form must reproduce the above |
12 * copyright notice, this list of conditions and the following disclaimer | 12 * copyright notice, this list of conditions and the following disclaimer |
13 * in the documentation and/or other materials provided with the | 13 * in the documentation and/or other materials provided with the |
14 * distribution. | 14 * distribution. |
15 * | 15 * |
16 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS | 16 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS |
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. | 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. |
20 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 */ | 27 */ |
28 | |
29 /** | 28 /** |
30 * @constructor | |
31 * @extends {WebInspector.SDKModel} | |
32 * @param {!WebInspector.Target} target | |
33 * @implements {ProfilerAgent.Dispatcher} | 29 * @implements {ProfilerAgent.Dispatcher} |
| 30 * @unrestricted |
34 */ | 31 */ |
35 WebInspector.CPUProfilerModel = function(target) | 32 WebInspector.CPUProfilerModel = class extends WebInspector.SDKModel { |
36 { | 33 /** |
37 WebInspector.SDKModel.call(this, WebInspector.CPUProfilerModel, target); | 34 * @param {!WebInspector.Target} target |
| 35 */ |
| 36 constructor(target) { |
| 37 super(WebInspector.CPUProfilerModel, target); |
38 this._isRecording = false; | 38 this._isRecording = false; |
39 target.registerProfilerDispatcher(this); | 39 target.registerProfilerDispatcher(this); |
40 target.profilerAgent().enable(); | 40 target.profilerAgent().enable(); |
41 | 41 |
42 this._configureCpuProfilerSamplingInterval(); | 42 this._configureCpuProfilerSamplingInterval(); |
43 WebInspector.moduleSetting("highResolutionCpuProfiling").addChangeListener(t
his._configureCpuProfilerSamplingInterval, this); | 43 WebInspector.moduleSetting('highResolutionCpuProfiling') |
| 44 .addChangeListener(this._configureCpuProfilerSamplingInterval, this); |
| 45 } |
| 46 |
| 47 _configureCpuProfilerSamplingInterval() { |
| 48 var intervalUs = WebInspector.moduleSetting('highResolutionCpuProfiling').ge
t() ? 100 : 1000; |
| 49 this.target().profilerAgent().setSamplingInterval(intervalUs); |
| 50 } |
| 51 |
| 52 /** |
| 53 * @override |
| 54 * @param {string} id |
| 55 * @param {!DebuggerAgent.Location} scriptLocation |
| 56 * @param {string=} title |
| 57 */ |
| 58 consoleProfileStarted(id, scriptLocation, title) { |
| 59 this._dispatchProfileEvent(WebInspector.CPUProfilerModel.Events.ConsoleProfi
leStarted, id, scriptLocation, title); |
| 60 } |
| 61 |
| 62 /** |
| 63 * @override |
| 64 * @param {string} id |
| 65 * @param {!DebuggerAgent.Location} scriptLocation |
| 66 * @param {!ProfilerAgent.Profile} cpuProfile |
| 67 * @param {string=} title |
| 68 */ |
| 69 consoleProfileFinished(id, scriptLocation, cpuProfile, title) { |
| 70 this._dispatchProfileEvent( |
| 71 WebInspector.CPUProfilerModel.Events.ConsoleProfileFinished, id, scriptL
ocation, title, cpuProfile); |
| 72 } |
| 73 |
| 74 /** |
| 75 * @param {symbol} eventName |
| 76 * @param {string} id |
| 77 * @param {!DebuggerAgent.Location} scriptLocation |
| 78 * @param {string=} title |
| 79 * @param {!ProfilerAgent.Profile=} cpuProfile |
| 80 */ |
| 81 _dispatchProfileEvent(eventName, id, scriptLocation, title, cpuProfile) { |
| 82 // Make sure ProfilesPanel is initialized and CPUProfileType is created. |
| 83 self.runtime.loadModulePromise('profiler').then(_ => { |
| 84 var debuggerModel = |
| 85 /** @type {!WebInspector.DebuggerModel} */ (WebInspector.DebuggerModel
.fromTarget(this.target())); |
| 86 var debuggerLocation = WebInspector.DebuggerModel.Location.fromPayload(deb
uggerModel, scriptLocation); |
| 87 var globalId = this.target().id() + '.' + id; |
| 88 var data = /** @type {!WebInspector.CPUProfilerModel.EventData} */ ( |
| 89 {id: globalId, scriptLocation: debuggerLocation, cpuProfile: cpuProfil
e, title: title}); |
| 90 this.dispatchEventToListeners(eventName, data); |
| 91 }); |
| 92 } |
| 93 |
| 94 /** |
| 95 * @return {boolean} |
| 96 */ |
| 97 isRecordingProfile() { |
| 98 return this._isRecording; |
| 99 } |
| 100 |
| 101 startRecording() { |
| 102 this._isRecording = true; |
| 103 this.target().profilerAgent().start(); |
| 104 WebInspector.userMetrics.actionTaken(WebInspector.UserMetrics.Action.Profile
sCPUProfileTaken); |
| 105 } |
| 106 |
| 107 /** |
| 108 * @return {!Promise.<?ProfilerAgent.Profile>} |
| 109 */ |
| 110 stopRecording() { |
| 111 /** |
| 112 * @param {?Protocol.Error} error |
| 113 * @param {?ProfilerAgent.Profile} profile |
| 114 * @return {?ProfilerAgent.Profile} |
| 115 */ |
| 116 function extractProfile(error, profile) { |
| 117 return !error && profile ? profile : null; |
| 118 } |
| 119 this._isRecording = false; |
| 120 return this.target().profilerAgent().stop(extractProfile); |
| 121 } |
| 122 |
| 123 /** |
| 124 * @override |
| 125 */ |
| 126 dispose() { |
| 127 WebInspector.moduleSetting('highResolutionCpuProfiling') |
| 128 .removeChangeListener(this._configureCpuProfilerSamplingInterval, this); |
| 129 } |
44 }; | 130 }; |
45 | 131 |
46 /** @enum {symbol} */ | 132 /** @enum {symbol} */ |
47 WebInspector.CPUProfilerModel.Events = { | 133 WebInspector.CPUProfilerModel.Events = { |
48 ConsoleProfileStarted: Symbol("ConsoleProfileStarted"), | 134 ConsoleProfileStarted: Symbol('ConsoleProfileStarted'), |
49 ConsoleProfileFinished: Symbol("ConsoleProfileFinished") | 135 ConsoleProfileFinished: Symbol('ConsoleProfileFinished') |
50 }; | 136 }; |
51 | 137 |
52 /** @typedef {!{id: string, scriptLocation: !WebInspector.DebuggerModel.Location
, title: (string|undefined), cpuProfile: (!ProfilerAgent.Profile|undefined)}} */ | 138 /** @typedef {!{id: string, scriptLocation: !WebInspector.DebuggerModel.Location
, title: (string|undefined), cpuProfile: (!ProfilerAgent.Profile|undefined)}} */ |
53 WebInspector.CPUProfilerModel.EventData; | 139 WebInspector.CPUProfilerModel.EventData; |
54 | |
55 WebInspector.CPUProfilerModel.prototype = { | |
56 _configureCpuProfilerSamplingInterval: function() | |
57 { | |
58 var intervalUs = WebInspector.moduleSetting("highResolutionCpuProfiling"
).get() ? 100 : 1000; | |
59 this.target().profilerAgent().setSamplingInterval(intervalUs); | |
60 }, | |
61 | |
62 /** | |
63 * @override | |
64 * @param {string} id | |
65 * @param {!DebuggerAgent.Location} scriptLocation | |
66 * @param {string=} title | |
67 */ | |
68 consoleProfileStarted: function(id, scriptLocation, title) | |
69 { | |
70 this._dispatchProfileEvent(WebInspector.CPUProfilerModel.Events.ConsoleP
rofileStarted, id, scriptLocation, title); | |
71 }, | |
72 | |
73 /** | |
74 * @override | |
75 * @param {string} id | |
76 * @param {!DebuggerAgent.Location} scriptLocation | |
77 * @param {!ProfilerAgent.Profile} cpuProfile | |
78 * @param {string=} title | |
79 */ | |
80 consoleProfileFinished: function(id, scriptLocation, cpuProfile, title) | |
81 { | |
82 this._dispatchProfileEvent(WebInspector.CPUProfilerModel.Events.ConsoleP
rofileFinished, id, scriptLocation, title, cpuProfile); | |
83 }, | |
84 | |
85 /** | |
86 * @param {symbol} eventName | |
87 * @param {string} id | |
88 * @param {!DebuggerAgent.Location} scriptLocation | |
89 * @param {string=} title | |
90 * @param {!ProfilerAgent.Profile=} cpuProfile | |
91 */ | |
92 _dispatchProfileEvent: function(eventName, id, scriptLocation, title, cpuPro
file) | |
93 { | |
94 // Make sure ProfilesPanel is initialized and CPUProfileType is created. | |
95 self.runtime.loadModulePromise("profiler").then(_ => { | |
96 var debuggerModel = /** @type {!WebInspector.DebuggerModel} */ (WebI
nspector.DebuggerModel.fromTarget(this.target())); | |
97 var debuggerLocation = WebInspector.DebuggerModel.Location.fromPaylo
ad(debuggerModel, scriptLocation); | |
98 var globalId = this.target().id() + "." + id; | |
99 var data = /** @type {!WebInspector.CPUProfilerModel.EventData} */ (
{id: globalId, scriptLocation: debuggerLocation, cpuProfile: cpuProfile, title:
title}); | |
100 this.dispatchEventToListeners(eventName, data); | |
101 }); | |
102 }, | |
103 | |
104 /** | |
105 * @return {boolean} | |
106 */ | |
107 isRecordingProfile: function() | |
108 { | |
109 return this._isRecording; | |
110 }, | |
111 | |
112 startRecording: function() | |
113 { | |
114 this._isRecording = true; | |
115 this.target().profilerAgent().start(); | |
116 WebInspector.userMetrics.actionTaken(WebInspector.UserMetrics.Action.Pro
filesCPUProfileTaken); | |
117 }, | |
118 | |
119 /** | |
120 * @return {!Promise.<?ProfilerAgent.Profile>} | |
121 */ | |
122 stopRecording: function() | |
123 { | |
124 /** | |
125 * @param {?Protocol.Error} error | |
126 * @param {?ProfilerAgent.Profile} profile | |
127 * @return {?ProfilerAgent.Profile} | |
128 */ | |
129 function extractProfile(error, profile) | |
130 { | |
131 return !error && profile ? profile : null; | |
132 } | |
133 this._isRecording = false; | |
134 return this.target().profilerAgent().stop(extractProfile); | |
135 }, | |
136 | |
137 dispose: function() | |
138 { | |
139 WebInspector.moduleSetting("highResolutionCpuProfiling").removeChangeLis
tener(this._configureCpuProfilerSamplingInterval, this); | |
140 }, | |
141 | |
142 __proto__: WebInspector.SDKModel.prototype | |
143 }; | |
OLD | NEW |