Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
f9479a5a
Unverified
Commit
f9479a5a
authored
Oct 22, 2026
by
vercel-fleet-prod[bot]
Committed by
GitHub
Oct 22, 2026
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Write CPU profiles for build workers
Co-authored-by:
Tobias Koppers
<
1365881+sokra@users.noreply.github.com
>
parent
7657545d
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
105 additions
and
15 deletions
+105
-15
worker.ts
packages/next/src/lib/worker.ts
+47
-6
cpu-profile.ts
packages/next/src/server/lib/cpu-profile.ts
+18
-1
cpu-profiling-build.test.ts
test/e2e/cpu-profiling/cpu-profiling-build.test.ts
+25
-5
cpu-profiling-dev.test.ts
test/e2e/cpu-profiling/cpu-profiling-dev.test.ts
+1
-1
cpu-profiling.test.ts
test/e2e/cpu-profiling/cpu-profiling.test.ts
+1
-1
next.config.js
test/e2e/cpu-profiling/next.config.js
+12
-0
cpu-profiling.test.ts
test/production/cpu-profiling/cpu-profiling.test.ts
+1
-1
No files found.
packages/next/src/lib/worker.ts
View file @
f9479a5a
...
@@ -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
)
c
onst
flushed
=
c
leanupWorkers
(
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
()
}
}
}
}
}
}
}
packages/next/src/server/lib/cpu-profile.ts
View file @
f9479a5a
...
@@ -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
)
{
...
...
test/e2e/cpu-profiling/cpu-profiling-build.test.ts
View file @
f9479a5a
...
@@ -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
)
}
})
})
})
})
test/e2e/cpu-profiling/cpu-profiling-dev.test.ts
View file @
f9479a5a
...
@@ -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$/
)
)
}
}
})
})
...
...
test/e2e/cpu-profiling/cpu-profiling.test.ts
View file @
f9479a5a
...
@@ -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$/
)
)
}
}
})
})
...
...
test/e2e/cpu-profiling/next.config.js
0 → 100644
View file @
f9479a5a
/**
* @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
test/production/cpu-profiling/cpu-profiling.test.ts
View file @
f9479a5a
...
@@ -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$/
)
)
}
}
})
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment