summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/performance.js
blob: 0107cc0c34555900dffdba8f35146979dcf8f239 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const { Actor, ActorClassWithSpec } = require("devtools/shared/protocol");
const { actorBridgeWithSpec } = require("devtools/server/actors/common");
const { performanceSpec } = require("devtools/shared/specs/performance");

loader.lazyRequireGetter(
  this,
  "PerformanceRecorder",
  "devtools/server/performance/recorder",
  true
);
loader.lazyRequireGetter(
  this,
  "normalizePerformanceFeatures",
  "devtools/shared/performance/recording-utils",
  true
);

const PIPE_TO_FRONT_EVENTS = new Set([
  "recording-started",
  "recording-stopping",
  "recording-stopped",
  "profiler-status",
  "timeline-data",
  "console-profile-start",
]);

const RECORDING_STATE_CHANGE_EVENTS = new Set([
  "recording-started",
  "recording-stopping",
  "recording-stopped",
]);

/**
 * This actor wraps the Performance module at devtools/shared/shared/performance.js
 * and provides RDP definitions.
 *
 * @see devtools/shared/shared/performance.js for documentation.
 */
var PerformanceActor = ActorClassWithSpec(performanceSpec, {
  traits: {
    features: {
      withMarkers: true,
      withTicks: true,
      withMemory: true,
      withFrames: true,
      withGCEvents: true,
      withDocLoadingEvents: true,
      withAllocations: true,
    },
  },

  initialize: function(conn, targetActor) {
    Actor.prototype.initialize.call(this, conn);

    this._onRecordingStarted = this._onRecordingStarted.bind(this);
    this._onRecordingStopping = this._onRecordingStopping.bind(this);
    this._onRecordingStopped = this._onRecordingStopped.bind(this);
    this._onProfilerStatus = this._onProfilerStatus.bind(this);
    this._onTimelineData = this._onTimelineData.bind(this);
    this._onConsoleProfileStart = this._onConsoleProfileStart.bind(this);

    this.bridge = new PerformanceRecorder(conn, targetActor);

    this.bridge.on("recording-started", this._onRecordingStarted);
    this.bridge.on("recording-stopping", this._onRecordingStopping);
    this.bridge.on("recording-stopped", this._onRecordingStopped);
    this.bridge.on("profiler-status", this._onProfilerStatus);
    this.bridge.on("timeline-data", this._onTimelineData);
    this.bridge.on("console-profile-start", this._onConsoleProfileStart);
  },

  destroy: function() {
    this.bridge.off("recording-started", this._onRecordingStarted);
    this.bridge.off("recording-stopping", this._onRecordingStopping);
    this.bridge.off("recording-stopped", this._onRecordingStopped);
    this.bridge.off("profiler-status", this._onProfilerStatus);
    this.bridge.off("timeline-data", this._onTimelineData);
    this.bridge.off("console-profile-start", this._onConsoleProfileStart);

    this.bridge.destroy();
    Actor.prototype.destroy.call(this);
  },

  connect: function(config) {
    this.bridge.connect({ systemClient: config.systemClient });
    return { traits: this.traits };
  },

  canCurrentlyRecord: function() {
    return this.bridge.canCurrentlyRecord();
  },

  async startRecording(options = {}) {
    if (!this.bridge.canCurrentlyRecord().success) {
      return null;
    }

    const normalizedOptions = normalizePerformanceFeatures(
      options,
      this.traits.features
    );
    const recording = await this.bridge.startRecording(normalizedOptions);
    this.manage(recording);

    return recording;
  },

  stopRecording: actorBridgeWithSpec("stopRecording"),
  isRecording: actorBridgeWithSpec("isRecording"),
  getRecordings: actorBridgeWithSpec("getRecordings"),
  getConfiguration: actorBridgeWithSpec("getConfiguration"),
  setProfilerStatusInterval: actorBridgeWithSpec("setProfilerStatusInterval"),

  /**
   * Filter which events get piped to the front.
   */
  _onRecordingStarted: function(...data) {
    this._onRecorderEvent("recording-started", data);
  },

  _onRecordingStopping: function(...data) {
    this._onRecorderEvent("recording-stopping", data);
  },

  _onRecordingStopped: function(...data) {
    this._onRecorderEvent("recording-stopped", data);
  },

  _onProfilerStatus: function(...data) {
    this._onRecorderEvent("profiler-status", data);
  },

  _onTimelineData: function(...data) {
    this._onRecorderEvent("timeline-data", data);
  },

  _onConsoleProfileStart: function(...data) {
    this._onRecorderEvent("console-profile-start", data);
  },

  _onRecorderEvent: function(eventName, data) {
    // If this is a recording state change, call
    // a method on the related PerformanceRecordingActor so it can
    // update its internal state.
    if (RECORDING_STATE_CHANGE_EVENTS.has(eventName)) {
      const recording = data[0];
      const extraData = data[1];
      recording._setState(eventName, extraData);
    }

    if (PIPE_TO_FRONT_EVENTS.has(eventName)) {
      this.emit(eventName, ...data);
    }
  },
});

exports.PerformanceActor = PerformanceActor;