Unverified Commit b664cdb1 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Add option for debugging CPU usage (#51174)

Add a `NEXT_CPU_PROF` environment variable to programmatically profile CPU for the router worker and two render workers. This is helpful because the `--cpu-prof` isn't reliably writing the result (#27406) especially with our multi-process architecture.

![CleanShot-2023-06-12-GS9p3SbN@2x](https://github.com/vercel/next.js/assets/3676859/b7cf80d2-a35a-4e90-978d-dec70be89f90)
parent e5a45c8a
if (process.env.__NEXT_PRIVATE_CPU_PROFILE) {
const { Session } = require('inspector') as typeof import('inspector')
const fs = require('fs')
const session = new Session()
session.connect()
session.post('Profiler.enable')
session.post('Profiler.start')
function saveProfile() {
session.post('Profiler.stop', (error, param) => {
if (error) {
console.error('Cannot generate CPU profiling:', error)
return
}
// Write profile to disk
const filename = `${
process.env.__NEXT_PRIVATE_CPU_PROFILE
}.${Date.now()}.cpuprofile`
fs.writeFileSync(`./${filename}`, JSON.stringify(param.profile))
process.exit(0)
})
}
process.on('SIGINT', saveProfile)
process.on('SIGTERM', saveProfile)
}
import type { RequestHandler } from '../next' import type { RequestHandler } from '../next'
import './cpu-profile'
import v8 from 'v8' import v8 from 'v8'
import http from 'http' import http from 'http'
import { isIPv6 } from 'net' import { isIPv6 } from 'net'
......
...@@ -4,7 +4,7 @@ import { getNodeOptionsWithoutInspect } from '../utils' ...@@ -4,7 +4,7 @@ import { getNodeOptionsWithoutInspect } from '../utils'
import { deserializeErr, errorToJSON } from '../../render' import { deserializeErr, errorToJSON } from '../../render'
import crypto from 'crypto' import crypto from 'crypto'
import isError from '../../../lib/is-error' import isError from '../../../lib/is-error'
import { genRenderExecArgv, getFreePort } from '../worker-utils' import { genRenderExecArgv } from '../worker-utils'
// we can't use process.send as jest-worker relies on // we can't use process.send as jest-worker relies on
// it already and can cause unexpected message errors // it already and can cause unexpected message errors
...@@ -115,10 +115,11 @@ export const createWorker = async ( ...@@ -115,10 +115,11 @@ export const createWorker = async (
: 'next', : 'next',
} }
: {}), : {}),
...(process.env.NEXT_CPU_PROF
? { __NEXT_PRIVATE_CPU_PROFILE: `CPU.${type}-renderer` }
: {}),
}, },
execArgv: isNodeDebugging execArgv: await genRenderExecArgv(isNodeDebugging, type),
? genRenderExecArgv(await getFreePort(), type)
: undefined,
}, },
exposedMethods: [ exposedMethods: [
'initialize', 'initialize',
......
...@@ -7,8 +7,7 @@ import { isIPv6 } from 'net' ...@@ -7,8 +7,7 @@ import { isIPv6 } from 'net'
import * as Log from '../../build/output/log' import * as Log from '../../build/output/log'
import { normalizeRepeatedSlashes } from '../../shared/lib/utils' import { normalizeRepeatedSlashes } from '../../shared/lib/utils'
import { initialEnv } from '@next/env' import { initialEnv } from '@next/env'
import { genExecArgv, getNodeOptionsWithoutInspect } from './utils' import { genRouterWorkerExecArgv, getNodeOptionsWithoutInspect } from './utils'
import { getFreePort } from './worker-utils'
export interface StartServerOptions { export interface StartServerOptions {
dir: string dir: string
...@@ -187,17 +186,17 @@ export async function startServer({ ...@@ -187,17 +186,17 @@ export async function startServer({
// TODO: do we want to allow more than 10 OOM restarts? // TODO: do we want to allow more than 10 OOM restarts?
maxRetries: 10, maxRetries: 10,
forkOptions: { forkOptions: {
execArgv: isNodeDebugging execArgv: await genRouterWorkerExecArgv(
? genExecArgv( isNodeDebugging === undefined ? false : isNodeDebugging
isNodeDebugging === undefined ? false : isNodeDebugging, ),
await getFreePort()
)
: undefined,
env: { env: {
FORCE_COLOR: '1', FORCE_COLOR: '1',
...((initialEnv || process.env) as typeof process.env), ...((initialEnv || process.env) as typeof process.env),
PORT: port + '', PORT: port + '',
NODE_OPTIONS: getNodeOptionsWithoutInspect(), NODE_OPTIONS: getNodeOptionsWithoutInspect(),
...(process.env.NEXT_CPU_PROF
? { __NEXT_PRIVATE_CPU_PROFILE: `CPU.router` }
: {}),
}, },
}, },
exposedMethods: ['initialize'], exposedMethods: ['initialize'],
......
import type arg from 'next/dist/compiled/arg/index.js' import type arg from 'next/dist/compiled/arg/index.js'
import { getFreePort } from './worker-utils'
export function printAndExit(message: string, code = 1) { export function printAndExit(message: string, code = 1) {
if (code === 0) { if (code === 0) {
...@@ -10,24 +11,27 @@ export function printAndExit(message: string, code = 1) { ...@@ -10,24 +11,27 @@ export function printAndExit(message: string, code = 1) {
process.exit(code) process.exit(code)
} }
export const genExecArgv = (enabled: boolean | 'brk', debugPort: number) => { export const genRouterWorkerExecArgv = async (
isNodeDebugging: boolean | 'brk'
) => {
const execArgv = process.execArgv.filter((localArg) => { const execArgv = process.execArgv.filter((localArg) => {
return ( return (
!localArg.startsWith('--inspect') && !localArg.startsWith('--inspect-brk') !localArg.startsWith('--inspect') && !localArg.startsWith('--inspect-brk')
) )
}) })
if (enabled) { if (isNodeDebugging) {
const debugPort = await getFreePort()
execArgv.push( execArgv.push(
`--inspect${enabled === 'brk' ? '-brk' : ''}=${debugPort + 1}` `--inspect${isNodeDebugging === 'brk' ? '-brk' : ''}=${debugPort + 1}`
) )
} }
return execArgv return execArgv
} }
const NODE_INSPECT_RE = /--inspect(-brk)?(=\S+)?( |$)/
export function getNodeOptionsWithoutInspect() { export function getNodeOptionsWithoutInspect() {
const NODE_INSPECT_RE = /--inspect(-brk)?(=\S+)?( |$)/
return (process.env.NODE_OPTIONS || '').replace(NODE_INSPECT_RE, '') return (process.env.NODE_OPTIONS || '').replace(NODE_INSPECT_RE, '')
} }
......
...@@ -17,14 +17,26 @@ export const getFreePort = async (): Promise<number> => { ...@@ -17,14 +17,26 @@ export const getFreePort = async (): Promise<number> => {
}) })
} }
export const genRenderExecArgv = (debugPort: number, type: 'pages' | 'app') => { export const genRenderExecArgv = async (
isNodeDebugging: string | boolean | undefined,
type: 'pages' | 'app'
) => {
const execArgv = process.execArgv.filter((localArg) => {
return (
!localArg.startsWith('--inspect') && !localArg.startsWith('--inspect-brk')
)
})
if (isNodeDebugging) {
const debugPort = await getFreePort()
const isDebugging = const isDebugging =
process.execArgv.some((localArg) => localArg.startsWith('--inspect')) || process.execArgv.some((localArg) => localArg.startsWith('--inspect')) ||
process.env.NODE_OPTIONS?.match?.(/--inspect(=\S+)?( |$)/) process.env.NODE_OPTIONS?.match?.(/--inspect(=\S+)?( |$)/)
const isDebuggingWithBrk = const isDebuggingWithBrk =
process.execArgv.some((localArg) => localArg.startsWith('--inspect-brk')) || process.execArgv.some((localArg) =>
process.env.NODE_OPTIONS?.match?.(/--inspect-brk(=\S+)?( |$)/) localArg.startsWith('--inspect-brk')
) || process.env.NODE_OPTIONS?.match?.(/--inspect-brk(=\S+)?( |$)/)
if (isDebugging || isDebuggingWithBrk) { if (isDebugging || isDebuggingWithBrk) {
Log.info( Log.info(
...@@ -35,15 +47,11 @@ export const genRenderExecArgv = (debugPort: number, type: 'pages' | 'app') => { ...@@ -35,15 +47,11 @@ export const genRenderExecArgv = (debugPort: number, type: 'pages' | 'app') => {
} should be inspected at port ${debugPort}.` } should be inspected at port ${debugPort}.`
) )
} }
const execArgv = process.execArgv.filter((localArg) => {
return (
!localArg.startsWith('--inspect') && !localArg.startsWith('--inspect-brk')
)
})
if (isDebugging || isDebuggingWithBrk) { if (isDebugging || isDebuggingWithBrk) {
execArgv.push(`--inspect${isDebuggingWithBrk ? '-brk' : ''}=${debugPort}`) execArgv.push(`--inspect${isDebuggingWithBrk ? '-brk' : ''}=${debugPort}`)
} }
}
return execArgv return execArgv
} }
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