[go: nahoru, domu]

blob: ad66f526bb917f32dda7cd349cfc97e02652b053 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Tim van der Lippe01e1c462021-04-19 15:04:035import * as Common from '../../core/common/common.js';
6import * as i18n from '../../core/i18n/i18n.js';
7import * as Platform from '../../core/platform/platform.js';
8import * as Root from '../../core/root/root.js';
9import * as SDK from '../../core/sdk/sdk.js';
Tim van der Lippe01e1c462021-04-19 15:04:0310import * as TimelineModel from '../../models/timeline_model/timeline_model.js';
Jack Franklin0b4f9632023-03-03 15:40:4111import * as TraceEngine from '../../models/trace/trace.js';
Tim van der Lippe229a54f2021-05-14 16:59:0512import type * as Protocol from '../../generated/protocol.js';
Tim van der Lippecec9b762020-02-13 15:31:2213
Paul Lewis897ad8b2020-01-15 16:49:1714import {PerformanceModel} from './PerformanceModel.js';
Paul Lewis897ad8b2020-01-15 16:49:1715
Simon Zünd697fb0b2021-03-01 10:12:4216const UIStrings = {
Vidal Guillermo Diazleal Ortega87060e42021-02-11 21:32:3417 /**
Simon Zünd648d6cf2021-04-20 06:30:5718 * @description Text in Timeline Controller of the Performance panel.
19 * A "CPU profile" is a recorded performance measurement how a specific target behaves.
20 * "Target" in this context can mean a web page, service or normal worker.
21 * "Not available" is used as there are multiple things that can go wrong, but we do not
22 * know what exactly, just that the CPU profile was not correctly recorded.
23 */
Vidal Guillermo Diazleal Ortega87060e42021-02-11 21:32:3424 cpuProfileForATargetIsNot: 'CPU profile for a target is not available.',
Sigurd Schneiderab1f2b52021-04-06 08:31:0425 /**
Sigurd Schneiderfdc7e132021-05-07 06:33:4526 *@description Text in Timeline Controller of the Performance panel indicating that the Performance Panel cannot
27 * record a performance trace because the type of target (where possible types are page, service worker and shared
28 * worker) doesn't support it.
29 */
30 tracingNotSupported: 'Performance trace recording not supported for this type of target',
Vidal Guillermo Diazleal Ortega87060e42021-02-11 21:32:3431};
Tim van der Lippe01e1c462021-04-19 15:04:0332const str_ = i18n.i18n.registerUIStrings('panels/timeline/TimelineController.ts', UIStrings);
Vidal Guillermo Diazleal Ortega87060e42021-02-11 21:32:3433const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
Sigurd Schneiderb9f6c792021-05-31 10:57:2434export class TimelineController implements SDK.TargetManager.SDKModelObserver<SDK.CPUProfilerModel.CPUProfilerModel>,
Jan Scheffler0d88c012021-04-05 18:45:5335 SDK.TracingManager.TracingManagerClient {
Jan Schefflerf9867912021-08-12 13:25:0336 private readonly target: SDK.Target.Target;
37 private tracingManager: SDK.TracingManager.TracingManager|null;
38 private performanceModel: PerformanceModel;
39 private readonly client: Client;
40 private readonly tracingModel: SDK.TracingModel.TracingModel;
Jan Scheffler0d88c012021-04-05 18:45:5341 // TODO(crbug.com/1172300) Ignored during the jsdoc to ts migration
42 // eslint-disable-next-line @typescript-eslint/no-explicit-any
Jan Schefflerf9867912021-08-12 13:25:0343 private tracingCompleteCallback?: ((value: any) => void)|null;
44 private profiling?: boolean;
Jan Scheffler0d88c012021-04-05 18:45:5345 // TODO(crbug.com/1172300) Ignored during the jsdoc to ts migration
46 // eslint-disable-next-line @typescript-eslint/no-explicit-any
Jan Schefflerf9867912021-08-12 13:25:0347 private cpuProfiles?: Map<any, any>|null;
Jan Scheffler0d88c012021-04-05 18:45:5348
Sigurd Schneider79e812e2021-06-04 06:48:2349 constructor(target: SDK.Target.Target, client: Client) {
Jan Schefflerf9867912021-08-12 13:25:0350 this.target = target;
51 this.tracingManager = target.model(SDK.TracingManager.TracingManager);
52 this.performanceModel = new PerformanceModel();
53 this.performanceModel.setMainTarget(target);
54 this.client = client;
Jack Franklin45522ff2023-06-01 13:19:5155 this.tracingModel = new SDK.TracingModel.TracingModel();
Blink Reformat4c46d092018-04-07 15:32:3756
Sigurd Schneiderb9f6c792021-05-31 10:57:2457 SDK.TargetManager.TargetManager.instance().observeModels(SDK.CPUProfilerModel.CPUProfilerModel, this);
Blink Reformat4c46d092018-04-07 15:32:3758 }
59
Jan Scheffler0d88c012021-04-05 18:45:5360 dispose(): void {
Sigurd Schneiderb9f6c792021-05-31 10:57:2461 SDK.TargetManager.TargetManager.instance().unobserveModels(SDK.CPUProfilerModel.CPUProfilerModel, this);
Alexei Filippove712dbc2018-05-30 22:31:3362 }
63
Sigurd Schneider79e812e2021-06-04 06:48:2364 mainTarget(): SDK.Target.Target {
Jan Schefflerf9867912021-08-12 13:25:0365 return this.target;
Blink Reformat4c46d092018-04-07 15:32:3766 }
67
Jack Franklin0de5e822023-03-09 12:00:3768 async startRecording(options: RecordingOptions): Promise<Protocol.ProtocolResponseWithError> {
Jan Scheffler0d88c012021-04-05 18:45:5369 function disabledByDefault(category: string): string {
Blink Reformat4c46d092018-04-07 15:32:3770 return 'disabled-by-default-' + category;
71 }
Victor Porofc22da122022-03-02 17:17:5772
73 // The following categories are also used in other tools, but this panel
74 // offers the possibility of turning them off (see below).
75 // 'disabled-by-default-devtools.screenshot'
76 // └ default: on, option: captureFilmStrip
77 // 'disabled-by-default-devtools.timeline.invalidationTracking'
78 // └ default: off, experiment: timelineInvalidationTracking
79 // 'disabled-by-default-v8.cpu_profiler'
80 // └ default: on, option: enableJSSampling
Blink Reformat4c46d092018-04-07 15:32:3781 const categoriesArray = [
Paul Irish0567b732022-05-02 18:31:1282 Root.Runtime.experiments.isEnabled('timelineShowAllEvents') ? '*' : '-*',
Victor Porofc22da122022-03-02 17:17:5783 TimelineModel.TimelineModel.TimelineModelImpl.Category.Console,
84 TimelineModel.TimelineModel.TimelineModelImpl.Category.UserTiming,
Jaroslav Sevcikb3d1dd22022-02-09 06:57:5485 'devtools.timeline',
86 disabledByDefault('devtools.timeline'),
87 disabledByDefault('devtools.timeline.frame'),
Victor Porofc22da122022-03-02 17:17:5788 disabledByDefault('devtools.timeline.stack'),
Jaroslav Sevcikb3d1dd22022-02-09 06:57:5489 disabledByDefault('v8.compile'),
Victor Porofc22da122022-03-02 17:17:5790 disabledByDefault('v8.cpu_profiler.hires'),
Paul Lewis9edf7512020-04-01 09:37:2191 TimelineModel.TimelineModel.TimelineModelImpl.Category.Loading,
Victor Porofc22da122022-03-02 17:17:5792 disabledByDefault('lighthouse'),
93 'v8.execute',
94 'v8',
Blink Reformat4c46d092018-04-07 15:32:3795 ];
Blink Reformat4c46d092018-04-07 15:32:3796
Simon Zünd7b7b35e2021-11-25 06:31:2497 if (Root.Runtime.experiments.isEnabled('timelineV8RuntimeCallStats') && options.enableJSSampling) {
98 categoriesArray.push(disabledByDefault('v8.runtime_stats_sampling'));
99 }
Andrés Olivares7cc96e82023-05-04 15:54:56100 if (options.enableJSSampling) {
Blink Reformat4c46d092018-04-07 15:32:37101 categoriesArray.push(disabledByDefault('v8.cpu_profiler'));
Blink Reformat4c46d092018-04-07 15:32:37102 }
Tim van der Lippe99e59b82019-09-30 20:00:59103 if (Root.Runtime.experiments.isEnabled('timelineInvalidationTracking')) {
Blink Reformat4c46d092018-04-07 15:32:37104 categoriesArray.push(disabledByDefault('devtools.timeline.invalidationTracking'));
Tim van der Lippe1d6e57a2019-09-30 11:55:34105 }
Blink Reformat4c46d092018-04-07 15:32:37106 if (options.capturePictures) {
107 categoriesArray.push(
108 disabledByDefault('devtools.timeline.layers'), disabledByDefault('devtools.timeline.picture'),
109 disabledByDefault('blink.graphics_context_annotations'));
110 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34111 if (options.captureFilmStrip) {
Blink Reformat4c46d092018-04-07 15:32:37112 categoriesArray.push(disabledByDefault('devtools.screenshot'));
Tim van der Lippe1d6e57a2019-09-30 11:55:34113 }
Blink Reformat4c46d092018-04-07 15:32:37114
Jan Schefflerf9867912021-08-12 13:25:03115 this.performanceModel.setRecordStartTime(Date.now());
Andrés Olivares7cc96e82023-05-04 15:54:56116 const response = await this.startRecordingWithCategories(categoriesArray.join(','));
Sigurd Schneiderab1f2b52021-04-06 08:31:04117 if (response.getError()) {
Jan Schefflerf9867912021-08-12 13:25:03118 await this.waitForTracingToStop(false);
Sigurd Schneiderb9f6c792021-05-31 10:57:24119 await SDK.TargetManager.TargetManager.instance().resumeAllTargets();
Tim van der Lippe1d6e57a2019-09-30 11:55:34120 }
Sigurd Schneider6eecdaa2019-08-20 07:47:10121 return response;
Blink Reformat4c46d092018-04-07 15:32:37122 }
123
Jan Scheffler0d88c012021-04-05 18:45:53124 async stopRecording(): Promise<PerformanceModel> {
Jan Schefflerf9867912021-08-12 13:25:03125 if (this.tracingManager) {
126 this.tracingManager.stop();
Tim van der Lippe1d6e57a2019-09-30 11:55:34127 }
Blink Reformat4c46d092018-04-07 15:32:37128
Jan Schefflerf9867912021-08-12 13:25:03129 this.client.loadingStarted();
130 await this.waitForTracingToStop(true);
Andrés Olivares435b3eb2022-10-17 11:46:39131 await this.allSourcesFinished();
132 return this.performanceModel;
133 }
134
135 getPerformanceModel(): PerformanceModel {
Jan Schefflerf9867912021-08-12 13:25:03136 return this.performanceModel;
Sigurd Schneider6eecdaa2019-08-20 07:47:10137 }
138
Jan Schefflerf9867912021-08-12 13:25:03139 private async waitForTracingToStop(awaitTracingCompleteCallback: boolean): Promise<void> {
Sigurd Schneider6eecdaa2019-08-20 07:47:10140 const tracingStoppedPromises = [];
Jan Schefflerf9867912021-08-12 13:25:03141 if (this.tracingManager && awaitTracingCompleteCallback) {
Patrick Brossete65aaac2020-06-22 08:04:40142 tracingStoppedPromises.push(new Promise(resolve => {
Jan Schefflerf9867912021-08-12 13:25:03143 this.tracingCompleteCallback = resolve;
Patrick Brossete65aaac2020-06-22 08:04:40144 }));
Tim van der Lippe1d6e57a2019-09-30 11:55:34145 }
Jan Schefflerf9867912021-08-12 13:25:03146 tracingStoppedPromises.push(this.stopProfilingOnAllModels());
Blink Reformat4c46d092018-04-07 15:32:37147
Alex Rudenko1f8ea582020-11-19 09:16:07148 await Promise.all(tracingStoppedPromises);
Blink Reformat4c46d092018-04-07 15:32:37149 }
150
Jan Scheffler0d88c012021-04-05 18:45:53151 modelAdded(cpuProfilerModel: SDK.CPUProfilerModel.CPUProfilerModel): void {
Jan Schefflerf9867912021-08-12 13:25:03152 if (this.profiling) {
Tim van der Lippe2d9a95c2022-01-04 15:18:03153 void cpuProfilerModel.startRecording();
Tim van der Lippe1d6e57a2019-09-30 11:55:34154 }
Blink Reformat4c46d092018-04-07 15:32:37155 }
156
Jan Scheffler0d88c012021-04-05 18:45:53157 modelRemoved(_cpuProfilerModel: SDK.CPUProfilerModel.CPUProfilerModel): void {
Blink Reformat4c46d092018-04-07 15:32:37158 // FIXME: We'd like to stop profiling on the target and retrieve a profile
159 // but it's too late. Backend connection is closed.
160 }
161
Sigurd Schneidera64c1352021-08-18 10:49:29162 private addCpuProfile(targetId: Protocol.Target.TargetID|'main', cpuProfile: Protocol.Profiler.Profile|null): void {
Blink Reformat4c46d092018-04-07 15:32:37163 if (!cpuProfile) {
Vidal Guillermo Diazleal Ortega87060e42021-02-11 21:32:34164 Common.Console.Console.instance().warn(i18nString(UIStrings.cpuProfileForATargetIsNot));
Blink Reformat4c46d092018-04-07 15:32:37165 return;
166 }
Jan Schefflerf9867912021-08-12 13:25:03167 if (!this.cpuProfiles) {
168 this.cpuProfiles = new Map();
Tim van der Lippe1d6e57a2019-09-30 11:55:34169 }
Jan Schefflerf9867912021-08-12 13:25:03170 this.cpuProfiles.set(targetId, cpuProfile);
Blink Reformat4c46d092018-04-07 15:32:37171 }
172
Jan Schefflerf9867912021-08-12 13:25:03173 private async stopProfilingOnAllModels(): Promise<void> {
Paul Lewisdaac1062020-03-05 14:37:10174 const models =
Jan Schefflerf9867912021-08-12 13:25:03175 this.profiling ? SDK.TargetManager.TargetManager.instance().models(SDK.CPUProfilerModel.CPUProfilerModel) : [];
176 this.profiling = false;
Blink Reformat4c46d092018-04-07 15:32:37177 const promises = [];
178 for (const model of models) {
179 const targetId = model.target().id();
Jan Schefflerf9867912021-08-12 13:25:03180 const modelPromise = model.stopRecording().then(this.addCpuProfile.bind(this, targetId));
Blink Reformat4c46d092018-04-07 15:32:37181 promises.push(modelPromise);
182 }
Alex Rudenko1f8ea582020-11-19 09:16:07183 await Promise.all(promises);
Blink Reformat4c46d092018-04-07 15:32:37184 }
185
Andrés Olivares7cc96e82023-05-04 15:54:56186 private async startRecordingWithCategories(categories: string): Promise<Protocol.ProtocolResponseWithError> {
Jan Schefflerf9867912021-08-12 13:25:03187 if (!this.tracingManager) {
Sigurd Schneiderab1f2b52021-04-06 08:31:04188 throw new Error(UIStrings.tracingNotSupported);
189 }
Sigurd Schneider8c2bcfd2019-09-04 09:03:29190 // There might be a significant delay in the beginning of timeline recording
191 // caused by starting CPU profiler, that needs to traverse JS heap to collect
192 // all the functions data.
Sigurd Schneiderb9f6c792021-05-31 10:57:24193 await SDK.TargetManager.TargetManager.instance().suspendAllTargets('performance-timeline');
Jan Schefflerf9867912021-08-12 13:25:03194 return this.tracingManager.start(this, categories, '');
Blink Reformat4c46d092018-04-07 15:32:37195 }
196
Jan Scheffler0d88c012021-04-05 18:45:53197 traceEventsCollected(events: SDK.TracingManager.EventPayload[]): void {
Jan Schefflerf9867912021-08-12 13:25:03198 this.tracingModel.addEvents(events);
Blink Reformat4c46d092018-04-07 15:32:37199 }
200
Jan Scheffler0d88c012021-04-05 18:45:53201 tracingComplete(): void {
Jan Schefflerf9867912021-08-12 13:25:03202 if (!this.tracingCompleteCallback) {
Alex Rudenko1f8ea582020-11-19 09:16:07203 return;
204 }
Jan Schefflerf9867912021-08-12 13:25:03205 this.tracingCompleteCallback(undefined);
206 this.tracingCompleteCallback = null;
Blink Reformat4c46d092018-04-07 15:32:37207 }
208
Andrés Olivares435b3eb2022-10-17 11:46:39209 private async allSourcesFinished(): Promise<void> {
Jan Schefflerf9867912021-08-12 13:25:03210 this.client.processingStarted();
Andrés Olivares435b3eb2022-10-17 11:46:39211 await this.finalizeTrace();
Blink Reformat4c46d092018-04-07 15:32:37212 }
213
Jan Schefflerf9867912021-08-12 13:25:03214 private async finalizeTrace(): Promise<void> {
215 this.injectCpuProfileEvents();
Sigurd Schneiderb9f6c792021-05-31 10:57:24216 await SDK.TargetManager.TargetManager.instance().resumeAllTargets();
Jan Schefflerf9867912021-08-12 13:25:03217 this.tracingModel.tracingComplete();
Nancy Li351e0052023-02-25 00:53:32218 await this.client.loadingComplete(this.tracingModel, null);
Andrés Olivares435b3eb2022-10-17 11:46:39219 this.client.loadingCompleteForTest();
Blink Reformat4c46d092018-04-07 15:32:37220 }
221
Jan Schefflerf9867912021-08-12 13:25:03222 private injectCpuProfileEvent(pid: number, tid: number, cpuProfile: Protocol.Profiler.Profile|null): void {
Tim van der Lippe1d6e57a2019-09-30 11:55:34223 if (!cpuProfile) {
Blink Reformat4c46d092018-04-07 15:32:37224 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34225 }
Alex Rudenko1f8ea582020-11-19 09:16:07226 // TODO(crbug/1011811): This event type is not compatible with the SDK.TracingManager.EventPayload.
227 // EventPayload requires many properties to be defined but it's not clear if they will have
228 // any side effects.
Jan Scheffler0d88c012021-04-05 18:45:53229 const cpuProfileEvent = ({
Blink Reformat4c46d092018-04-07 15:32:37230 cat: SDK.TracingModel.DevToolsMetadataEventCategory,
Jack Franklin0b4f9632023-03-03 15:40:41231 ph: TraceEngine.Types.TraceEvents.Phase.INSTANT,
Jan Schefflerf9867912021-08-12 13:25:03232 ts: this.tracingModel.maximumRecordTime() * 1000,
Blink Reformat4c46d092018-04-07 15:32:37233 pid: pid,
234 tid: tid,
235 name: TimelineModel.TimelineModel.RecordType.CpuProfile,
Jan Scheffler0d88c012021-04-05 18:45:53236 args: {data: {cpuProfile: cpuProfile}},
237 // TODO(crbug.com/1172300) Ignored during the jsdoc to ts migration
238 // eslint-disable-next-line @typescript-eslint/no-explicit-any
239 } as any);
Jan Schefflerf9867912021-08-12 13:25:03240 this.tracingModel.addEvents([cpuProfileEvent]);
Blink Reformat4c46d092018-04-07 15:32:37241 }
242
Jan Schefflerf9867912021-08-12 13:25:03243 private buildTargetToProcessIdMap(): Map<string, number>|null {
Tim van der Lippecec9b762020-02-13 15:31:22244 const metadataEventTypes = TimelineModel.TimelineModel.TimelineModelImpl.DevToolsMetadataEvent;
Jan Schefflerf9867912021-08-12 13:25:03245 const metadataEvents = this.tracingModel.devToolsMetadataEvents();
Alexei Filippove712dbc2018-05-30 22:31:33246 const browserMetaEvent = metadataEvents.find(e => e.name === metadataEventTypes.TracingStartedInBrowser);
Tim van der Lippe1d6e57a2019-09-30 11:55:34247 if (!browserMetaEvent) {
Alexei Filippove712dbc2018-05-30 22:31:33248 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34249 }
Alexei Filippove712dbc2018-05-30 22:31:33250
Jan Scheffler0d88c012021-04-05 18:45:53251 const pseudoPidToFrames = new Platform.MapUtilities.Multimap<string, string>();
252 const targetIdToPid = new Map<string, number>();
253 // TODO(crbug.com/1172300) Ignored during the jsdoc to ts migration
254 // eslint-disable-next-line @typescript-eslint/no-explicit-any
255 const frames: any[] = browserMetaEvent.args.data.frames;
Tim van der Lippe1d6e57a2019-09-30 11:55:34256 for (const frameInfo of frames) {
Alexei Filippove712dbc2018-05-30 22:31:33257 targetIdToPid.set(frameInfo.frame, frameInfo.processId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34258 }
Alexei Filippove712dbc2018-05-30 22:31:33259 for (const event of metadataEvents) {
260 const data = event.args.data;
261 switch (event.name) {
262 case metadataEventTypes.FrameCommittedInBrowser:
Tim van der Lippe1d6e57a2019-09-30 11:55:34263 if (data.processId) {
Alexei Filippove712dbc2018-05-30 22:31:33264 targetIdToPid.set(data.frame, data.processId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34265 } else {
Alexei Filippove712dbc2018-05-30 22:31:33266 pseudoPidToFrames.set(data.processPseudoId, data.frame);
Tim van der Lippe1d6e57a2019-09-30 11:55:34267 }
Alexei Filippove712dbc2018-05-30 22:31:33268 break;
269 case metadataEventTypes.ProcessReadyInBrowser:
Tim van der Lippe1d6e57a2019-09-30 11:55:34270 for (const frame of pseudoPidToFrames.get(data.processPseudoId) || []) {
Alexei Filippove712dbc2018-05-30 22:31:33271 targetIdToPid.set(frame, data.processId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34272 }
Alexei Filippove712dbc2018-05-30 22:31:33273 break;
274 }
275 }
276 const mainFrame = frames.find(frame => !frame.parent);
277 const mainRendererProcessId = mainFrame.processId;
Jan Schefflerf9867912021-08-12 13:25:03278 const mainProcess = this.tracingModel.getProcessById(mainRendererProcessId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34279 if (mainProcess) {
Danil Somsikov11250822023-02-27 15:02:19280 const target = SDK.TargetManager.TargetManager.instance().primaryPageTarget();
Alex Rudenko1f8ea582020-11-19 09:16:07281 if (target) {
282 targetIdToPid.set(target.id(), mainProcess.id());
283 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34284 }
Alexei Filippove712dbc2018-05-30 22:31:33285 return targetIdToPid;
286 }
287
Jan Schefflerf9867912021-08-12 13:25:03288 private injectCpuProfileEvents(): void {
289 if (!this.cpuProfiles) {
Blink Reformat4c46d092018-04-07 15:32:37290 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34291 }
Blink Reformat4c46d092018-04-07 15:32:37292
Tim van der Lippecec9b762020-02-13 15:31:22293 const metadataEventTypes = TimelineModel.TimelineModel.TimelineModelImpl.DevToolsMetadataEvent;
Jan Schefflerf9867912021-08-12 13:25:03294 const metadataEvents = this.tracingModel.devToolsMetadataEvents();
Blink Reformat4c46d092018-04-07 15:32:37295
Jan Schefflerf9867912021-08-12 13:25:03296 const targetIdToPid = this.buildTargetToProcessIdMap();
Alexei Filippove712dbc2018-05-30 22:31:33297 if (targetIdToPid) {
Jan Schefflerf9867912021-08-12 13:25:03298 for (const [id, profile] of this.cpuProfiles) {
Alexei Filippove712dbc2018-05-30 22:31:33299 const pid = targetIdToPid.get(id);
Tim van der Lippe1d6e57a2019-09-30 11:55:34300 if (!pid) {
Alexei Filippove712dbc2018-05-30 22:31:33301 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34302 }
Jan Schefflerf9867912021-08-12 13:25:03303 const process = this.tracingModel.getProcessById(pid);
Tim van der Lippecec9b762020-02-13 15:31:22304 const thread =
305 process && process.threadByName(TimelineModel.TimelineModel.TimelineModelImpl.RendererMainThreadName);
Tim van der Lippe1d6e57a2019-09-30 11:55:34306 if (thread) {
Jan Schefflerf9867912021-08-12 13:25:03307 this.injectCpuProfileEvent(pid, thread.id(), profile);
Tim van der Lippe1d6e57a2019-09-30 11:55:34308 }
Alexei Filippove712dbc2018-05-30 22:31:33309 }
310 } else {
311 // Legacy backends support.
Tim van der Lippe779b46b2021-01-13 13:40:22312 const filteredEvents = metadataEvents.filter(event => event.name === metadataEventTypes.TracingStartedInPage);
313 const mainMetaEvent = filteredEvents[filteredEvents.length - 1];
Alexei Filippove712dbc2018-05-30 22:31:33314 if (mainMetaEvent) {
315 const pid = mainMetaEvent.thread.process().id();
Jan Schefflerf9867912021-08-12 13:25:03316 if (this.tracingManager) {
317 const mainCpuProfile = this.cpuProfiles.get(this.tracingManager.target().id());
318 this.injectCpuProfileEvent(pid, mainMetaEvent.thread.id(), mainCpuProfile);
Alex Rudenko1f8ea582020-11-19 09:16:07319 }
Pavel Feldmanc0395912018-08-03 02:05:00320 } else {
321 // Or there was no tracing manager in the main target at all, in this case build the model full
322 // of cpu profiles.
Alexei Filippov2af968a2018-08-17 02:24:36323 let tid = 0;
Jan Schefflerf9867912021-08-12 13:25:03324 for (const pair of this.cpuProfiles) {
Sigurd Schneiderb9f6c792021-05-31 10:57:24325 const target = SDK.TargetManager.TargetManager.instance().targetById(pair[0]);
Alexei Filippov2af968a2018-08-17 02:24:36326 const name = target && target.name();
Jan Schefflerf9867912021-08-12 13:25:03327 this.tracingModel.addEvents(
Paul Irish3e218b22023-03-02 03:21:59328 TimelineModel.TimelineJSProfile.TimelineJSProfileProcessor.createFakeTraceFromCpuProfile(
Tim van der Lippecec9b762020-02-13 15:31:22329 pair[1], ++tid, /* injectPageEvent */ tid === 1, name));
Pavel Feldmanc0395912018-08-03 02:05:00330 }
Alexei Filippove712dbc2018-05-30 22:31:33331 }
332 }
Blink Reformat4c46d092018-04-07 15:32:37333
334 const workerMetaEvents =
335 metadataEvents.filter(event => event.name === metadataEventTypes.TracingSessionIdForWorker);
336 for (const metaEvent of workerMetaEvents) {
337 const workerId = metaEvent.args['data']['workerId'];
Jan Schefflerf9867912021-08-12 13:25:03338 const cpuProfile = this.cpuProfiles.get(workerId);
339 this.injectCpuProfileEvent(metaEvent.thread.process().id(), metaEvent.args['data']['workerThreadId'], cpuProfile);
Blink Reformat4c46d092018-04-07 15:32:37340 }
Jan Schefflerf9867912021-08-12 13:25:03341 this.cpuProfiles = null;
Blink Reformat4c46d092018-04-07 15:32:37342 }
343
Jan Scheffler0d88c012021-04-05 18:45:53344 tracingBufferUsage(usage: number): void {
Jan Schefflerf9867912021-08-12 13:25:03345 this.client.recordingProgress(usage);
Blink Reformat4c46d092018-04-07 15:32:37346 }
347
Jan Scheffler0d88c012021-04-05 18:45:53348 eventsRetrievalProgress(progress: number): void {
Jan Schefflerf9867912021-08-12 13:25:03349 this.client.loadingProgress(progress);
Blink Reformat4c46d092018-04-07 15:32:37350 }
Tim van der Lippe0176f6c2020-01-08 11:07:01351}
Blink Reformat4c46d092018-04-07 15:32:37352
Jan Scheffler0d88c012021-04-05 18:45:53353export interface Client {
354 recordingProgress(usage: number): void;
355 loadingStarted(): void;
356 processingStarted(): void;
357 loadingProgress(progress?: number): void;
Nancy Li351e0052023-02-25 00:53:32358 loadingComplete(
359 tracingModel: SDK.TracingModel.TracingModel|null,
Jack Franklin45522ff2023-06-01 13:19:51360 exclusiveFilter: TimelineModel.TimelineModelFilter.TimelineModelFilter|null): Promise<void>;
Andrés Olivares435b3eb2022-10-17 11:46:39361 loadingCompleteForTest(): void;
Tim van der Lippe0176f6c2020-01-08 11:07:01362}
Jan Scheffler0d88c012021-04-05 18:45:53363export interface RecordingOptions {
364 enableJSSampling?: boolean;
365 capturePictures?: boolean;
366 captureFilmStrip?: boolean;
Jan Scheffler0d88c012021-04-05 18:45:53367}