@atlaspack/profiler 2.14.1-canary.43 → 2.14.1-canary.431
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +353 -0
- package/dist/NativeProfiler.js +205 -0
- package/dist/SamplingProfiler.js +57 -0
- package/dist/Trace.js +109 -0
- package/dist/Tracer.js +132 -0
- package/dist/index.js +15 -0
- package/dist/types.js +2 -0
- package/lib/NativeProfiler.js +235 -0
- package/lib/SamplingProfiler.js +6 -0
- package/lib/Tracer.js +5 -2
- package/lib/index.js +7 -0
- package/lib/types/NativeProfiler.d.ts +7 -0
- package/lib/{SamplingProfiler.d.ts → types/SamplingProfiler.d.ts} +2 -2
- package/lib/{Trace.d.ts → types/Trace.d.ts} +3 -3
- package/lib/{Tracer.d.ts → types/Tracer.d.ts} +9 -5
- package/lib/types/index.d.ts +6 -0
- package/lib/types/types.d.ts +7 -0
- package/package.json +13 -10
- package/src/NativeProfiler.ts +245 -0
- package/src/{SamplingProfiler.js → SamplingProfiler.ts} +59 -40
- package/src/{Trace.js → Trace.ts} +0 -1
- package/src/{Tracer.js → Tracer.ts} +27 -15
- package/src/{index.js → index.ts} +2 -1
- package/src/types.ts +8 -0
- package/test/{Tracer.test.js → Tracer.test.ts} +2 -3
- package/tsconfig.json +24 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/lib/index.d.ts +0 -4
- package/lib/types.d.ts +0 -5
- package/src/types.js +0 -8
package/dist/Trace.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const chrome_trace_event_1 = require("chrome-trace-event");
|
|
4
|
+
class Trace {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.tracer = new chrome_trace_event_1.Tracer();
|
|
7
|
+
this.tid = 0;
|
|
8
|
+
this.eventId = 0;
|
|
9
|
+
}
|
|
10
|
+
getEventId() {
|
|
11
|
+
return this.eventId++;
|
|
12
|
+
}
|
|
13
|
+
init(ts) {
|
|
14
|
+
this.tracer.instantEvent({
|
|
15
|
+
name: 'TracingStartedInPage',
|
|
16
|
+
id: this.getEventId(),
|
|
17
|
+
ts,
|
|
18
|
+
cat: ['disabled-by-default-devtools.timeline'],
|
|
19
|
+
args: {
|
|
20
|
+
data: {
|
|
21
|
+
sessionId: '-1',
|
|
22
|
+
page: '0xfff',
|
|
23
|
+
frames: [
|
|
24
|
+
{
|
|
25
|
+
frame: '0xfff',
|
|
26
|
+
url: 'parcel',
|
|
27
|
+
name: '',
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
this.tracer.instantEvent({
|
|
34
|
+
name: 'TracingStartedInBrowser',
|
|
35
|
+
id: this.getEventId(),
|
|
36
|
+
ts,
|
|
37
|
+
cat: ['disabled-by-default-devtools.timeline'],
|
|
38
|
+
args: {
|
|
39
|
+
data: {
|
|
40
|
+
sessionId: '-1',
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
addCPUProfile(name, profile) {
|
|
46
|
+
if (this.eventId === 0) {
|
|
47
|
+
this.init(profile.startTime);
|
|
48
|
+
}
|
|
49
|
+
const trace = this.tracer;
|
|
50
|
+
const tid = this.tid;
|
|
51
|
+
this.tid++;
|
|
52
|
+
const cpuStartTime = profile.startTime;
|
|
53
|
+
const cpuEndTime = profile.endTime;
|
|
54
|
+
trace.instantEvent({
|
|
55
|
+
tid,
|
|
56
|
+
id: this.getEventId(),
|
|
57
|
+
cat: ['toplevel'],
|
|
58
|
+
name: 'TaskQueueManager::ProcessTaskFromWorkQueue',
|
|
59
|
+
args: {
|
|
60
|
+
src_file: '../../ipc/ipc_moji_bootstrap.cc',
|
|
61
|
+
src_func: 'Accept',
|
|
62
|
+
},
|
|
63
|
+
ts: cpuStartTime,
|
|
64
|
+
});
|
|
65
|
+
trace.completeEvent({
|
|
66
|
+
tid,
|
|
67
|
+
name: 'EvaluateScript',
|
|
68
|
+
id: this.getEventId(),
|
|
69
|
+
cat: ['devtools.timeline'],
|
|
70
|
+
ts: cpuStartTime,
|
|
71
|
+
dur: cpuEndTime - cpuStartTime,
|
|
72
|
+
args: {
|
|
73
|
+
data: {
|
|
74
|
+
url: 'parcel',
|
|
75
|
+
lineNumber: 1,
|
|
76
|
+
columnNumber: 1,
|
|
77
|
+
frame: '0xFFF',
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
trace.instantEvent({
|
|
82
|
+
tid,
|
|
83
|
+
ts: 0,
|
|
84
|
+
ph: 'M',
|
|
85
|
+
cat: ['__metadata'],
|
|
86
|
+
name: 'thread_name',
|
|
87
|
+
args: { name },
|
|
88
|
+
});
|
|
89
|
+
trace.instantEvent({
|
|
90
|
+
tid,
|
|
91
|
+
name: 'CpuProfile',
|
|
92
|
+
id: this.getEventId(),
|
|
93
|
+
cat: ['disabled-by-default-devtools.timeline'],
|
|
94
|
+
ts: cpuEndTime,
|
|
95
|
+
args: {
|
|
96
|
+
data: {
|
|
97
|
+
cpuProfile: profile,
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
pipe(writable) {
|
|
103
|
+
return this.tracer.pipe(writable);
|
|
104
|
+
}
|
|
105
|
+
flush() {
|
|
106
|
+
this.tracer.push(null);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.default = Trace;
|
package/dist/Tracer.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _TraceMeasurement_active, _TraceMeasurement_name, _TraceMeasurement_pid, _TraceMeasurement_tid, _TraceMeasurement_start, _TraceMeasurement_data, _Tracer_traceEmitter, _Tracer_enabled;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.PluginTracer = exports.tracer = void 0;
|
|
16
|
+
const events_1 = require("@atlaspack/events");
|
|
17
|
+
const perf_hooks_1 = require("perf_hooks");
|
|
18
|
+
// @ts-expect-error TS7034
|
|
19
|
+
let tid;
|
|
20
|
+
try {
|
|
21
|
+
tid = require('worker_threads').threadId;
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
tid = 0;
|
|
25
|
+
}
|
|
26
|
+
const pid = process.pid;
|
|
27
|
+
class TraceMeasurement {
|
|
28
|
+
constructor(tracer, name, pid, tid, data) {
|
|
29
|
+
_TraceMeasurement_active.set(this, true);
|
|
30
|
+
_TraceMeasurement_name.set(this, void 0);
|
|
31
|
+
_TraceMeasurement_pid.set(this, void 0);
|
|
32
|
+
_TraceMeasurement_tid.set(this, void 0);
|
|
33
|
+
_TraceMeasurement_start.set(this, void 0);
|
|
34
|
+
_TraceMeasurement_data.set(this, void 0);
|
|
35
|
+
__classPrivateFieldSet(this, _TraceMeasurement_name, name, "f");
|
|
36
|
+
__classPrivateFieldSet(this, _TraceMeasurement_pid, pid, "f");
|
|
37
|
+
__classPrivateFieldSet(this, _TraceMeasurement_tid, tid, "f");
|
|
38
|
+
__classPrivateFieldSet(this, _TraceMeasurement_start, perf_hooks_1.performance.now(), "f");
|
|
39
|
+
__classPrivateFieldSet(this, _TraceMeasurement_data, data, "f");
|
|
40
|
+
}
|
|
41
|
+
end() {
|
|
42
|
+
if (!__classPrivateFieldGet(this, _TraceMeasurement_active, "f"))
|
|
43
|
+
return;
|
|
44
|
+
const duration = perf_hooks_1.performance.now() - __classPrivateFieldGet(this, _TraceMeasurement_start, "f");
|
|
45
|
+
exports.tracer.trace({
|
|
46
|
+
type: 'trace',
|
|
47
|
+
name: __classPrivateFieldGet(this, _TraceMeasurement_name, "f"),
|
|
48
|
+
pid: __classPrivateFieldGet(this, _TraceMeasurement_pid, "f"),
|
|
49
|
+
tid: __classPrivateFieldGet(this, _TraceMeasurement_tid, "f"),
|
|
50
|
+
duration,
|
|
51
|
+
ts: __classPrivateFieldGet(this, _TraceMeasurement_start, "f"),
|
|
52
|
+
...__classPrivateFieldGet(this, _TraceMeasurement_data, "f"),
|
|
53
|
+
});
|
|
54
|
+
__classPrivateFieldSet(this, _TraceMeasurement_active, false, "f");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
_TraceMeasurement_active = new WeakMap(), _TraceMeasurement_name = new WeakMap(), _TraceMeasurement_pid = new WeakMap(), _TraceMeasurement_tid = new WeakMap(), _TraceMeasurement_start = new WeakMap(), _TraceMeasurement_data = new WeakMap();
|
|
58
|
+
class Tracer {
|
|
59
|
+
constructor() {
|
|
60
|
+
_Tracer_traceEmitter.set(this, new events_1.ValueEmitter());
|
|
61
|
+
_Tracer_enabled.set(this, false);
|
|
62
|
+
}
|
|
63
|
+
onTrace(cb) {
|
|
64
|
+
return __classPrivateFieldGet(this, _Tracer_traceEmitter, "f").addListener(cb);
|
|
65
|
+
}
|
|
66
|
+
async wrap(name, fn) {
|
|
67
|
+
let measurement = this.createMeasurement(name);
|
|
68
|
+
try {
|
|
69
|
+
await fn();
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
measurement && measurement.end();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
createMeasurement(name, category = 'Core', argumentName, otherArgs) {
|
|
76
|
+
if (!this.enabled)
|
|
77
|
+
return null;
|
|
78
|
+
// We create `args` in a fairly verbose way to avoid object
|
|
79
|
+
// allocation where not required.
|
|
80
|
+
let args;
|
|
81
|
+
if (typeof argumentName === 'string') {
|
|
82
|
+
args = { name: argumentName };
|
|
83
|
+
}
|
|
84
|
+
if (typeof otherArgs === 'object') {
|
|
85
|
+
// @ts-expect-error TS2454
|
|
86
|
+
if (typeof args == 'undefined') {
|
|
87
|
+
args = {};
|
|
88
|
+
}
|
|
89
|
+
for (const [k, v] of Object.entries(otherArgs)) {
|
|
90
|
+
args[k] = v;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const data = {
|
|
94
|
+
categories: [category],
|
|
95
|
+
// @ts-expect-error TS2454
|
|
96
|
+
args,
|
|
97
|
+
};
|
|
98
|
+
// @ts-expect-error TS7005
|
|
99
|
+
return new TraceMeasurement(this, name, pid, tid, data);
|
|
100
|
+
}
|
|
101
|
+
get enabled() {
|
|
102
|
+
return __classPrivateFieldGet(this, _Tracer_enabled, "f");
|
|
103
|
+
}
|
|
104
|
+
enable() {
|
|
105
|
+
__classPrivateFieldSet(this, _Tracer_enabled, true, "f");
|
|
106
|
+
}
|
|
107
|
+
disable() {
|
|
108
|
+
__classPrivateFieldSet(this, _Tracer_enabled, false, "f");
|
|
109
|
+
}
|
|
110
|
+
trace(event) {
|
|
111
|
+
if (!__classPrivateFieldGet(this, _Tracer_enabled, "f"))
|
|
112
|
+
return;
|
|
113
|
+
__classPrivateFieldGet(this, _Tracer_traceEmitter, "f").emit(event);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
_Tracer_traceEmitter = new WeakMap(), _Tracer_enabled = new WeakMap();
|
|
117
|
+
exports.default = Tracer;
|
|
118
|
+
exports.tracer = new Tracer();
|
|
119
|
+
class PluginTracer {
|
|
120
|
+
/** @private */
|
|
121
|
+
constructor(opts) {
|
|
122
|
+
this.origin = opts.origin;
|
|
123
|
+
this.category = opts.category;
|
|
124
|
+
}
|
|
125
|
+
get enabled() {
|
|
126
|
+
return exports.tracer.enabled;
|
|
127
|
+
}
|
|
128
|
+
createMeasurement(name, category, argumentName, otherArgs) {
|
|
129
|
+
return exports.tracer.createMeasurement(name, `${this.category}:${this.origin}${typeof category === 'string' ? `:${category}` : ''}`, argumentName, otherArgs);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
exports.PluginTracer = PluginTracer;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.NativeProfiler = exports.PluginTracer = exports.tracer = exports.Trace = exports.SamplingProfiler = void 0;
|
|
7
|
+
var SamplingProfiler_1 = require("./SamplingProfiler");
|
|
8
|
+
Object.defineProperty(exports, "SamplingProfiler", { enumerable: true, get: function () { return __importDefault(SamplingProfiler_1).default; } });
|
|
9
|
+
var Trace_1 = require("./Trace");
|
|
10
|
+
Object.defineProperty(exports, "Trace", { enumerable: true, get: function () { return __importDefault(Trace_1).default; } });
|
|
11
|
+
var Tracer_1 = require("./Tracer");
|
|
12
|
+
Object.defineProperty(exports, "tracer", { enumerable: true, get: function () { return Tracer_1.tracer; } });
|
|
13
|
+
Object.defineProperty(exports, "PluginTracer", { enumerable: true, get: function () { return Tracer_1.PluginTracer; } });
|
|
14
|
+
var NativeProfiler_1 = require("./NativeProfiler");
|
|
15
|
+
Object.defineProperty(exports, "NativeProfiler", { enumerable: true, get: function () { return __importDefault(NativeProfiler_1).default; } });
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
function _utils() {
|
|
8
|
+
const data = require("@atlaspack/utils");
|
|
9
|
+
_utils = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _logger() {
|
|
15
|
+
const data = _interopRequireDefault(require("@atlaspack/logger"));
|
|
16
|
+
_logger = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
function _readline() {
|
|
22
|
+
const data = _interopRequireDefault(require("readline"));
|
|
23
|
+
_readline = function () {
|
|
24
|
+
return data;
|
|
25
|
+
};
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
function _chalk() {
|
|
29
|
+
const data = _interopRequireDefault(require("chalk"));
|
|
30
|
+
_chalk = function () {
|
|
31
|
+
return data;
|
|
32
|
+
};
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
35
|
+
function _child_process() {
|
|
36
|
+
const data = require("child_process");
|
|
37
|
+
_child_process = function () {
|
|
38
|
+
return data;
|
|
39
|
+
};
|
|
40
|
+
return data;
|
|
41
|
+
}
|
|
42
|
+
function _util() {
|
|
43
|
+
const data = require("util");
|
|
44
|
+
_util = function () {
|
|
45
|
+
return data;
|
|
46
|
+
};
|
|
47
|
+
return data;
|
|
48
|
+
}
|
|
49
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
50
|
+
const execAsync = (0, _util().promisify)(_child_process().exec);
|
|
51
|
+
class NativeProfiler {
|
|
52
|
+
startProfiling(profilerType) {
|
|
53
|
+
const pid = process.pid;
|
|
54
|
+
const timeId = (0, _utils().getTimeId)();
|
|
55
|
+
let filename;
|
|
56
|
+
let command;
|
|
57
|
+
_logger().default.info({
|
|
58
|
+
origin: '@atlaspack/profiler',
|
|
59
|
+
message: 'Starting native profiling...'
|
|
60
|
+
});
|
|
61
|
+
if (profilerType === 'instruments') {
|
|
62
|
+
filename = `native-profile-${timeId}.trace`;
|
|
63
|
+
command = `xcrun xctrace record --template "CPU Profiler" --output ${filename} --attach ${pid}`;
|
|
64
|
+
} else {
|
|
65
|
+
filename = `native-profile-${timeId}.json`;
|
|
66
|
+
command = `samply record --save-only --output ${filename} --pid ${pid}`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Display banner with PID and command
|
|
70
|
+
// Strip ANSI codes for length calculation
|
|
71
|
+
// eslint-disable-next-line no-control-regex
|
|
72
|
+
const stripAnsi = str => str.replace(/\u001b\[[0-9;]*m/g, '');
|
|
73
|
+
const boxWidth = Math.max(60, stripAnsi(command).length + 6);
|
|
74
|
+
const title = 'Native Profiling';
|
|
75
|
+
const titlePadding = Math.floor((boxWidth - title.length - 2) / 2);
|
|
76
|
+
const isTTY = process.stdin.isTTY;
|
|
77
|
+
const maxWaitTime = 30; // seconds
|
|
78
|
+
|
|
79
|
+
const padLine = content => {
|
|
80
|
+
const contentLength = stripAnsi(content).length;
|
|
81
|
+
const padding = Math.max(0, boxWidth - contentLength - 2);
|
|
82
|
+
return _chalk().default.blue('│') + ' ' + content + ' '.repeat(padding) + ' ' + _chalk().default.blue('│');
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// Make the command visually distinct and easy to copy
|
|
86
|
+
// Note: Hyperlinks can cause issues with commands (words become separate links)
|
|
87
|
+
// So we just make it visually prominent with colors
|
|
88
|
+
const makeCommandDisplay = cmd => {
|
|
89
|
+
return _chalk().default.cyan.bold(cmd);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// Contextual message based on TTY
|
|
93
|
+
const continueMessage = isTTY ? 'Press Enter or start the profiler to continue' : `Build will continue when profiler has started, or after ${maxWaitTime}s`;
|
|
94
|
+
const banner = ['', _chalk().default.blue('┌' + '─'.repeat(boxWidth) + '┐'), _chalk().default.blue('│') + ' '.repeat(titlePadding) + _chalk().default.blue.bold(title) + ' '.repeat(boxWidth - title.length - titlePadding) + _chalk().default.blue('│'), _chalk().default.blue('├' + '─'.repeat(boxWidth) + '┤'), padLine(`${_chalk().default.gray('PID:')} ${_chalk().default.white.bold(String(pid))}`), padLine(''), padLine(_chalk().default.gray('Command:')), padLine(makeCommandDisplay(command)), padLine(''), padLine(_chalk().default.gray('Run the command above to start profiling.')), padLine(_chalk().default.gray(continueMessage)), _chalk().default.blue('└' + '─'.repeat(boxWidth) + '┘'), ''].join('\n');
|
|
95
|
+
|
|
96
|
+
// eslint-disable-next-line no-console
|
|
97
|
+
console.log(banner);
|
|
98
|
+
|
|
99
|
+
// In both interactive and non-interactive mode, detect when profiler is running
|
|
100
|
+
// In interactive mode, also allow user to press Enter to continue
|
|
101
|
+
if (!process.stdin.isTTY) {
|
|
102
|
+
return this.waitForProfiler(profilerType, pid);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Interactive mode: wait for either user to press Enter OR profiler to be detected
|
|
106
|
+
return new Promise(resolve => {
|
|
107
|
+
let resolved = false;
|
|
108
|
+
const doResolve = () => {
|
|
109
|
+
if (resolved) return;
|
|
110
|
+
resolved = true;
|
|
111
|
+
_logger().default.info({
|
|
112
|
+
origin: '@atlaspack/profiler',
|
|
113
|
+
message: 'Native profiling setup complete'
|
|
114
|
+
});
|
|
115
|
+
resolve();
|
|
116
|
+
};
|
|
117
|
+
const rl = _readline().default.createInterface({
|
|
118
|
+
input: process.stdin,
|
|
119
|
+
output: process.stdout
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// User presses Enter
|
|
123
|
+
rl.on('line', () => {
|
|
124
|
+
rl.close();
|
|
125
|
+
doResolve();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// Also poll for profiler in the background
|
|
129
|
+
this.pollForProfiler(profilerType, pid, doResolve);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
waitForProfiler(profilerType, pid) {
|
|
133
|
+
_logger().default.info({
|
|
134
|
+
origin: '@atlaspack/profiler',
|
|
135
|
+
message: 'Non-interactive mode: waiting for profiler to attach...'
|
|
136
|
+
});
|
|
137
|
+
return new Promise(resolve => {
|
|
138
|
+
this.pollForProfiler(profilerType, pid, () => {
|
|
139
|
+
_logger().default.info({
|
|
140
|
+
origin: '@atlaspack/profiler',
|
|
141
|
+
message: 'Native profiling setup complete'
|
|
142
|
+
});
|
|
143
|
+
resolve();
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
async pollForProfiler(profilerType, pid, onDetected) {
|
|
148
|
+
const maxAttempts = 60; // 60 attempts * 500ms = 30 seconds max
|
|
149
|
+
const pollInterval = 500; // 500ms between checks
|
|
150
|
+
|
|
151
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
152
|
+
const isRunning = await this.checkProfilerRunning(profilerType, pid);
|
|
153
|
+
if (isRunning) {
|
|
154
|
+
// Instruments takes longer to start up (~5s), samply needs ~1s
|
|
155
|
+
const waitTime = profilerType === 'instruments' ? 5000 : 1000;
|
|
156
|
+
_logger().default.info({
|
|
157
|
+
origin: '@atlaspack/profiler',
|
|
158
|
+
message: `Profiler detected, waiting ${waitTime}ms before continuing...`
|
|
159
|
+
});
|
|
160
|
+
await new Promise(resolve => setTimeout(resolve, waitTime));
|
|
161
|
+
onDetected();
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
await new Promise(resolve => setTimeout(resolve, pollInterval));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// If we couldn't detect the profiler after 30 seconds, log a warning and continue anyway
|
|
168
|
+
_logger().default.warn({
|
|
169
|
+
origin: '@atlaspack/profiler',
|
|
170
|
+
message: 'Could not detect profiler after 30 seconds, continuing anyway...'
|
|
171
|
+
});
|
|
172
|
+
onDetected();
|
|
173
|
+
}
|
|
174
|
+
async checkProfilerRunning(profilerType, pid) {
|
|
175
|
+
try {
|
|
176
|
+
// Get all processes and filter in JavaScript
|
|
177
|
+
const {
|
|
178
|
+
stdout
|
|
179
|
+
} = await execAsync('ps aux');
|
|
180
|
+
const lines = stdout.split('\n').filter(line => line.trim().length > 0);
|
|
181
|
+
|
|
182
|
+
// Use word boundaries to match the PID as a complete number
|
|
183
|
+
const pidRegex = new RegExp(`\\b${pid}\\b`);
|
|
184
|
+
|
|
185
|
+
// Determine the profiler process name to look for
|
|
186
|
+
const profilerName = profilerType === 'instruments' ? 'xctrace' : 'samply';
|
|
187
|
+
for (const line of lines) {
|
|
188
|
+
const lowerLine = line.toLowerCase();
|
|
189
|
+
|
|
190
|
+
// Skip lines that are part of our own process checking (avoid false positives)
|
|
191
|
+
// Skip lines containing "ps aux" or "grep" to avoid matching our own commands
|
|
192
|
+
if (lowerLine.includes('ps aux') || lowerLine.includes(' grep ')) {
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Skip our own process (the Atlaspack process itself)
|
|
197
|
+
// The PID column is the second field in ps aux output
|
|
198
|
+
const fields = line.trim().split(/\s+/);
|
|
199
|
+
if (fields.length >= 2 && fields[1] === String(pid)) {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Check if this line contains the profiler name as a command
|
|
204
|
+
const profilerRegex = new RegExp(`\\b${profilerName}\\b`);
|
|
205
|
+
if (!profilerRegex.test(lowerLine)) {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Now check if our PID appears in the command arguments (not in the PID column)
|
|
210
|
+
// The PID should appear after the profiler command, typically as --pid <pid> or --attach <pid>
|
|
211
|
+
// We need to check the command portion, which starts around column 11 in ps aux
|
|
212
|
+
// For safety, check if PID appears after the profiler name in the line
|
|
213
|
+
const profilerIndex = lowerLine.indexOf(profilerName);
|
|
214
|
+
if (profilerIndex === -1) {
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Check if PID appears in the command portion (after the profiler name)
|
|
219
|
+
const commandPortion = line.substring(profilerIndex);
|
|
220
|
+
if (pidRegex.test(commandPortion)) {
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return false;
|
|
225
|
+
} catch (error) {
|
|
226
|
+
// If the command fails, log and return false
|
|
227
|
+
_logger().default.warn({
|
|
228
|
+
origin: '@atlaspack/profiler',
|
|
229
|
+
message: `Error checking profiler status: ${error.message}`
|
|
230
|
+
});
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
exports.default = NativeProfiler;
|
package/lib/SamplingProfiler.js
CHANGED
|
@@ -20,10 +20,16 @@ function _diagnostic() {
|
|
|
20
20
|
}
|
|
21
21
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
22
|
// https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-Profile
|
|
23
|
+
|
|
23
24
|
// https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ProfileNode
|
|
25
|
+
|
|
24
26
|
// https://chromedevtools.github.io/devtools-protocol/tot/Runtime#type-CallFrame
|
|
27
|
+
|
|
25
28
|
// https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-PositionTickInfo
|
|
29
|
+
|
|
26
30
|
class SamplingProfiler {
|
|
31
|
+
// @ts-expect-error not initialized
|
|
32
|
+
|
|
27
33
|
startProfiling() {
|
|
28
34
|
let inspector;
|
|
29
35
|
try {
|
package/lib/Tracer.js
CHANGED
|
@@ -18,7 +18,7 @@ function _perf_hooks() {
|
|
|
18
18
|
};
|
|
19
19
|
return data;
|
|
20
20
|
}
|
|
21
|
-
// @ts-
|
|
21
|
+
// @ts-expect-error TS7034
|
|
22
22
|
let tid;
|
|
23
23
|
try {
|
|
24
24
|
tid = require('worker_threads').threadId;
|
|
@@ -32,7 +32,6 @@ class TraceMeasurement {
|
|
|
32
32
|
#pid;
|
|
33
33
|
#tid;
|
|
34
34
|
#start;
|
|
35
|
-
// $FlowFixMe
|
|
36
35
|
#data;
|
|
37
36
|
constructor(tracer, name, pid, tid, data) {
|
|
38
37
|
this.#name = name;
|
|
@@ -82,6 +81,7 @@ class Tracer {
|
|
|
82
81
|
};
|
|
83
82
|
}
|
|
84
83
|
if (typeof otherArgs === 'object') {
|
|
84
|
+
// @ts-expect-error TS2454
|
|
85
85
|
if (typeof args == 'undefined') {
|
|
86
86
|
args = {};
|
|
87
87
|
}
|
|
@@ -91,8 +91,11 @@ class Tracer {
|
|
|
91
91
|
}
|
|
92
92
|
const data = {
|
|
93
93
|
categories: [category],
|
|
94
|
+
// @ts-expect-error TS2454
|
|
94
95
|
args
|
|
95
96
|
};
|
|
97
|
+
|
|
98
|
+
// @ts-expect-error TS7005
|
|
96
99
|
return new TraceMeasurement(this, name, pid, tid, data);
|
|
97
100
|
}
|
|
98
101
|
get enabled() {
|
package/lib/index.js
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
Object.defineProperty(exports, "NativeProfiler", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _NativeProfiler.default;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
6
12
|
Object.defineProperty(exports, "PluginTracer", {
|
|
7
13
|
enumerable: true,
|
|
8
14
|
get: function () {
|
|
@@ -30,4 +36,5 @@ Object.defineProperty(exports, "tracer", {
|
|
|
30
36
|
var _SamplingProfiler = _interopRequireDefault(require("./SamplingProfiler"));
|
|
31
37
|
var _Trace = _interopRequireDefault(require("./Trace"));
|
|
32
38
|
var _Tracer = require("./Tracer");
|
|
39
|
+
var _NativeProfiler = _interopRequireDefault(require("./NativeProfiler"));
|
|
33
40
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Session } from
|
|
1
|
+
import type { Session } from 'inspector';
|
|
2
2
|
export type Profile = {
|
|
3
3
|
nodes: Array<ProfileNode>;
|
|
4
4
|
startTime: number;
|
|
@@ -28,7 +28,7 @@ type PositionTickInfo = {
|
|
|
28
28
|
export default class SamplingProfiler {
|
|
29
29
|
session: Session;
|
|
30
30
|
startProfiling(): Promise<unknown>;
|
|
31
|
-
sendCommand(method: string, params?:
|
|
31
|
+
sendCommand(method: string, params?: any): Promise<{
|
|
32
32
|
profile: Profile;
|
|
33
33
|
}>;
|
|
34
34
|
destroy(): void;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Profile } from
|
|
2
|
-
import type { Writable } from
|
|
3
|
-
import { Tracer } from
|
|
1
|
+
import type { Profile } from './SamplingProfiler';
|
|
2
|
+
import type { Writable } from 'stream';
|
|
3
|
+
import { Tracer } from 'chrome-trace-event';
|
|
4
4
|
export default class Trace {
|
|
5
5
|
tracer: Tracer;
|
|
6
6
|
tid: number;
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import type { TraceEvent, IDisposable, PluginTracer as IPluginTracer } from
|
|
2
|
-
import type { TraceMeasurement as ITraceMeasurement } from
|
|
1
|
+
import type { TraceEvent, IDisposable, PluginTracer as IPluginTracer } from '@atlaspack/types-internal';
|
|
2
|
+
import type { TraceMeasurement as ITraceMeasurement } from './types';
|
|
3
3
|
export default class Tracer {
|
|
4
|
-
|
|
4
|
+
#private;
|
|
5
5
|
onTrace(cb: (event: TraceEvent) => unknown): IDisposable;
|
|
6
6
|
wrap(name: string, fn: () => unknown): Promise<void>;
|
|
7
|
-
createMeasurement(name: string, category?: string, argumentName?: string, otherArgs?:
|
|
7
|
+
createMeasurement(name: string, category?: string, argumentName?: string, otherArgs?: {
|
|
8
|
+
[key: string]: unknown;
|
|
9
|
+
}): ITraceMeasurement | null;
|
|
8
10
|
get enabled(): boolean;
|
|
9
11
|
enable(): void;
|
|
10
12
|
disable(): void;
|
|
@@ -23,6 +25,8 @@ export declare class PluginTracer implements IPluginTracer {
|
|
|
23
25
|
/** @private */
|
|
24
26
|
constructor(opts: TracerOpts);
|
|
25
27
|
get enabled(): boolean;
|
|
26
|
-
createMeasurement(name: string, category?: string, argumentName?: string, otherArgs?:
|
|
28
|
+
createMeasurement(name: string, category?: string, argumentName?: string, otherArgs?: {
|
|
29
|
+
[key: string]: unknown;
|
|
30
|
+
}): ITraceMeasurement | null;
|
|
27
31
|
}
|
|
28
32
|
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { default as SamplingProfiler } from './SamplingProfiler';
|
|
2
|
+
export { default as Trace } from './Trace';
|
|
3
|
+
export { tracer, PluginTracer } from './Tracer';
|
|
4
|
+
export type { TraceMeasurement, TraceMeasurementData } from './types';
|
|
5
|
+
export { default as NativeProfiler } from './NativeProfiler';
|
|
6
|
+
export type { NativeProfilerType } from './NativeProfiler';
|