-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
cpu_profiler.ts
175 lines (163 loc) · 5.99 KB
/
cpu_profiler.ts
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
164
165
166
167
168
169
170
171
172
173
174
175
import { arch as _arch, platform as _platform } from 'os';
import { join, resolve } from 'path';
import { familySync } from 'detect-libc';
import { getAbi } from 'node-abi';
import { env, versions } from 'process';
import { threadId } from 'worker_threads';
import { GLOBAL_OBJ, logger } from '@sentry/utils';
import { DEBUG_BUILD } from './debug-build';
import type { PrivateV8CpuProfilerBindings, V8CpuProfilerBindings } from './types';
const stdlib = familySync();
const platform = process.env['BUILD_PLATFORM'] || _platform();
const arch = process.env['BUILD_ARCH'] || _arch();
const abi = getAbi(versions.node, 'node');
const identifier = [platform, arch, stdlib, abi].filter(c => c !== undefined && c !== null).join('-');
const built_from_source_path = resolve(__dirname, `./sentry_cpu_profiler-${identifier}`);
/**
* Imports cpp bindings based on the current platform and architecture.
*/
// eslint-disable-next-line complexity
export function importCppBindingsModule(): PrivateV8CpuProfilerBindings {
// If a binary path is specified, use that.
if (env['SENTRY_PROFILER_BINARY_PATH']) {
const envPath = env['SENTRY_PROFILER_BINARY_PATH'];
return require(envPath);
}
// If a user specifies a different binary dir, they are in control of the binaries being moved there
if (env['SENTRY_PROFILER_BINARY_DIR']) {
const binaryPath = join(resolve(env['SENTRY_PROFILER_BINARY_DIR']), `sentry_cpu_profiler-${identifier}`);
return require(`${binaryPath}.node`);
}
/* eslint-disable no-fallthrough */
// We need the fallthrough so that in the end, we can fallback to the require dynamice require.
// This is for cases where precompiled binaries were not provided, but may have been compiled from source.
if (platform === 'darwin') {
if (arch === 'x64') {
if (abi === '93') {
return require('./sentry_cpu_profiler-darwin-x64-93.node');
}
if (abi === '108') {
return require('./sentry_cpu_profiler-darwin-x64-108.node');
}
if (abi === '115') {
return require('./sentry_cpu_profiler-darwin-x64-115.node');
}
if (abi === '127') {
return require('./sentry_cpu_profiler-darwin-x64-127.node');
}
}
if (arch === 'arm64') {
if (abi === '93') {
return require('./sentry_cpu_profiler-darwin-arm64-93.node');
}
if (abi === '108') {
return require('./sentry_cpu_profiler-darwin-arm64-108.node');
}
if (abi === '115') {
return require('./sentry_cpu_profiler-darwin-arm64-115.node');
}
if (abi === '127') {
return require('./sentry_cpu_profiler-darwin-arm64-127.node');
}
}
}
if (platform === 'win32') {
if (arch === 'x64') {
if (abi === '93') {
return require('./sentry_cpu_profiler-win32-x64-93.node');
}
if (abi === '108') {
return require('./sentry_cpu_profiler-win32-x64-108.node');
}
if (abi === '115') {
return require('./sentry_cpu_profiler-win32-x64-115.node');
}
if (abi === '127') {
return require('./sentry_cpu_profiler-win32-x64-127.node');
}
}
}
if (platform === 'linux') {
if (arch === 'x64') {
if (stdlib === 'musl') {
if (abi === '93') {
return require('./sentry_cpu_profiler-linux-x64-musl-93.node');
}
if (abi === '108') {
return require('./sentry_cpu_profiler-linux-x64-musl-108.node');
}
if (abi === '115') {
return require('./sentry_cpu_profiler-linux-x64-musl-115.node');
}
if (abi === '127') {
return require('./sentry_cpu_profiler-linux-x64-musl-127.node');
}
}
if (stdlib === 'glibc') {
if (abi === '93') {
return require('./sentry_cpu_profiler-linux-x64-glibc-93.node');
}
if (abi === '108') {
return require('./sentry_cpu_profiler-linux-x64-glibc-108.node');
}
if (abi === '115') {
return require('./sentry_cpu_profiler-linux-x64-glibc-115.node');
}
if (abi === '127') {
return require('./sentry_cpu_profiler-linux-x64-glibc-127.node');
}
}
}
if (arch === 'arm64') {
if (stdlib === 'musl') {
if (abi === '93') {
return require('./sentry_cpu_profiler-linux-arm64-musl-93.node');
}
if (abi === '108') {
return require('./sentry_cpu_profiler-linux-arm64-musl-108.node');
}
if (abi === '115') {
return require('./sentry_cpu_profiler-linux-arm64-musl-115.node');
}
if (abi === '127') {
return require('./sentry_cpu_profiler-linux-arm64-musl-127.node');
}
}
if (stdlib === 'glibc') {
if (abi === '93') {
return require('./sentry_cpu_profiler-linux-arm64-glibc-93.node');
}
if (abi === '108') {
return require('./sentry_cpu_profiler-linux-arm64-glibc-108.node');
}
if (abi === '115') {
return require('./sentry_cpu_profiler-linux-arm64-glibc-115.node');
}
if (abi === '127') {
return require('./sentry_cpu_profiler-linux-arm64-glibc-127.node');
}
}
}
}
return require(`${built_from_source_path}.node`);
}
const PrivateCpuProfilerBindings: PrivateV8CpuProfilerBindings = importCppBindingsModule();
const CpuProfilerBindings: V8CpuProfilerBindings = {
startProfiling(name: string) {
if (!PrivateCpuProfilerBindings) {
DEBUG_BUILD && logger.log('[Profiling] Bindings not loaded, ignoring call to startProfiling.');
return;
}
return PrivateCpuProfilerBindings.startProfiling(name);
},
stopProfiling(name: string) {
if (!PrivateCpuProfilerBindings) {
DEBUG_BUILD &&
logger.log('[Profiling] Bindings not loaded or profile was never started, ignoring call to stopProfiling.');
return null;
}
return PrivateCpuProfilerBindings.stopProfiling(name, threadId, !!GLOBAL_OBJ._sentryDebugIds);
},
};
export { PrivateCpuProfilerBindings };
export { CpuProfilerBindings };