Unverified Commit f9479a5a authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Write CPU profiles for build workers

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 7657545d
...@@ -14,12 +14,47 @@ type FarmOptions = NonNullable<ConstructorParameters<typeof JestWorker>[1]> ...@@ -14,12 +14,47 @@ type FarmOptions = NonNullable<ConstructorParameters<typeof JestWorker>[1]>
const RESTARTED = Symbol('restarted') const RESTARTED = Symbol('restarted')
const cleanupWorkers = (worker: JestWorker) => { // Upper bound for how long `end()` waits for workers to write their CPU
// profiles before falling back to jest-worker's regular (force-kill) shutdown.
const CPU_PROFILE_FLUSH_TIMEOUT_MS = 60_000
const cleanupWorkers = (worker: JestWorker): Promise<void> | undefined => {
const children: ChildProcess[] = []
for (const curWorker of ((worker as any)._workerPool?._workers || []) as { for (const curWorker of ((worker as any)._workerPool?._workers || []) as {
_child?: ChildProcess _child?: ChildProcess
}[]) { }[]) {
curWorker._child?.kill('SIGINT') if (curWorker._child) {
children.push(curWorker._child)
curWorker._child.kill('SIGINT')
}
}
if (!process.env.NEXT_CPU_PROF) {
return undefined
} }
// With CPU profiling enabled, workers save their profile when they receive
// SIGINT (see `server/lib/cpu-profile.ts`). jest-worker's `end()` force-kills
// workers that haven't exited after ~1s, which can cut off writing a large
// profile, so wait for the workers to exit first.
return new Promise<void>((resolve) => {
const timer = setTimeout(resolve, CPU_PROFILE_FLUSH_TIMEOUT_MS)
Promise.all(
children.map(
(child) =>
new Promise<void>((resolveChild) => {
if (child.exitCode !== null || child.signalCode !== null) {
resolveChild()
} else {
child.once('exit', () => resolveChild())
}
})
)
).then(() => {
clearTimeout(timer)
resolve()
})
})
} }
export function getNextBuildDebuggerPortOffset(_: { export function getNextBuildDebuggerPortOffset(_: {
...@@ -297,9 +332,9 @@ export class Worker { ...@@ -297,9 +332,9 @@ export class Worker {
if (!worker) { if (!worker) {
throw new Error('Farm is ended, no more calls can be done to it') throw new Error('Farm is ended, no more calls can be done to it')
} }
cleanupWorkers(worker) const flushed = cleanupWorkers(worker)
this._worker = undefined this._worker = undefined
return worker.end() return flushed ? flushed.then(() => worker.end()) : worker.end()
} }
/** /**
...@@ -307,8 +342,14 @@ export class Worker { ...@@ -307,8 +342,14 @@ export class Worker {
*/ */
close(): void { close(): void {
if (this._worker) { if (this._worker) {
cleanupWorkers(this._worker) const worker = this._worker
this._worker.end() const flushed = cleanupWorkers(worker)
this._worker = undefined
if (flushed) {
flushed.then(() => worker.end())
} else {
worker.end()
}
} }
} }
} }
...@@ -17,6 +17,21 @@ if (isCpuProfileEnabled) { ...@@ -17,6 +17,21 @@ if (isCpuProfileEnabled) {
process.on('exit', () => { process.on('exit', () => {
saveCpuProfile() saveCpuProfile()
}) })
// Next.js workers (see `lib/worker.ts`) are stopped with a signal, which
// terminates the process without emitting 'exit'. Save the profile first,
// then re-raise the signal so the worker still dies by it and the parent
// observes the same exit as without profiling.
if (process.env.IS_NEXT_WORKER) {
const onSignal = (signal: NodeJS.Signals) => {
process.removeListener('SIGINT', onSignal)
process.removeListener('SIGTERM', onSignal)
saveCpuProfile()
process.kill(process.pid, signal)
}
process.on('SIGINT', onSignal)
process.on('SIGTERM', onSignal)
}
} }
/** /**
...@@ -46,7 +61,9 @@ export function saveCpuProfile(): void { ...@@ -46,7 +61,9 @@ export function saveCpuProfile(): void {
.replace(/[:.]/g, '-') .replace(/[:.]/g, '-')
.slice(0, 19) .slice(0, 19)
const baseName = privateCpuProfileName || 'cpu-profile' const baseName = privateCpuProfileName || 'cpu-profile'
const filename = `${baseName}-${timestamp}.cpuprofile` // Include the pid so concurrent processes (e.g. build workers) that save
// within the same second don't overwrite each other's profiles.
const filename = `${baseName}-${timestamp}-${process.pid}.cpuprofile`
let outputPath: string let outputPath: string
if (cpuProfileDir) { if (cpuProfileDir) {
......
...@@ -38,13 +38,27 @@ describe('CPU Profiling - next build', () => { ...@@ -38,13 +38,27 @@ describe('CPU Profiling - next build', () => {
// Main profile should always exist // Main profile should always exist
expect(cpuProfiles.some((f) => f.startsWith('build-main-'))).toBe(true) expect(cpuProfiles.some((f) => f.startsWith('build-main-'))).toBe(true)
// `next.config.js` pins `experimental.cpus` to 2, so the build starts
// exactly two static workers. Each must write its own profile; the PID in
// the filename keeps workers that exit in the same second from colliding.
const staticWorkerProfiles = cpuProfiles.filter((f) =>
f.startsWith('build-static-worker-')
)
expect(staticWorkerProfiles).toHaveLength(2)
const staticWorkerPids = staticWorkerProfiles.map(
(f) => /-(\d+)\.cpuprofile$/.exec(f)?.[1]
)
expect(staticWorkerPids).not.toContain(undefined)
expect(new Set(staticWorkerPids).size).toBe(2)
if (isTurbopack) { if (isTurbopack) {
// Turbopack builds in the main build process, so `build-main` is the only // Turbopack builds in the main build process, so the profiles are
// profile. // build-main plus the two static workers.
expect(cpuProfiles.length).toBe(1) expect(cpuProfiles.length).toBe(3)
} else { } else {
// Webpack mode generates: build-main, build-webpack-client, build-webpack-server, build-webpack-edge-server // Webpack mode additionally generates build-webpack-client,
expect(cpuProfiles.length).toBe(4) // build-webpack-server and build-webpack-edge-server.
expect(cpuProfiles.length).toBe(6)
expect( expect(
cpuProfiles.some((f) => f.startsWith('build-webpack-client-')) cpuProfiles.some((f) => f.startsWith('build-webpack-client-'))
).toBe(true) ).toBe(true)
...@@ -55,5 +69,11 @@ describe('CPU Profiling - next build', () => { ...@@ -55,5 +69,11 @@ describe('CPU Profiling - next build', () => {
cpuProfiles.some((f) => f.startsWith('build-webpack-edge-server-')) cpuProfiles.some((f) => f.startsWith('build-webpack-edge-server-'))
).toBe(true) ).toBe(true)
} }
for (const file of cpuProfiles) {
const profile = JSON.parse(await readFile(join(profileDir, file), 'utf8'))
expect(Array.isArray(profile.nodes)).toBe(true)
expect(profile.nodes.length).toBeGreaterThan(0)
}
}) })
}) })
...@@ -39,7 +39,7 @@ describe('CPU Profiling - next dev', () => { ...@@ -39,7 +39,7 @@ describe('CPU Profiling - next dev', () => {
// Verify profile names are meaningful // Verify profile names are meaningful
for (const profile of cpuProfiles) { for (const profile of cpuProfiles) {
expect(profile).toMatch( expect(profile).toMatch(
/^(dev-main|dev-server)-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}\.cpuprofile$/ /^(dev-main|dev-server)-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d+\.cpuprofile$/
) )
} }
}) })
......
...@@ -41,7 +41,7 @@ describe('CPU Profiling - next start', () => { ...@@ -41,7 +41,7 @@ describe('CPU Profiling - next start', () => {
// Verify profile name is meaningful (start-main) // Verify profile name is meaningful (start-main)
for (const profile of cpuProfiles) { for (const profile of cpuProfiles) {
expect(profile).toMatch( expect(profile).toMatch(
/^start-main-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}\.cpuprofile$/ /^start-main-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d+\.cpuprofile$/
) )
} }
}) })
......
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
// Pin the static worker count so the build test can assert exactly one
// `build-static-worker` profile per worker.
cpus: 2,
},
}
module.exports = nextConfig
...@@ -24,7 +24,7 @@ describe('CPU Profiling', () => { ...@@ -24,7 +24,7 @@ describe('CPU Profiling', () => {
for (const profile of cpuProfiles) { for (const profile of cpuProfiles) {
expect(profile).toMatch( expect(profile).toMatch(
/^(build-main|build-webpack-(server|client|edge-server)|build-turbopack|build-static-worker|build-trace-worker)-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}\.cpuprofile$/ /^(build-main|build-webpack-(server|client|edge-server)|build-turbopack|build-static-worker|build-trace-worker)-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d+\.cpuprofile$/
) )
} }
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment