Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for debugging CPU usage #51174

Merged
merged 5 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/next/src/server/lib/cpu-profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't you need to await those?


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)
}
1 change: 1 addition & 0 deletions packages/next/src/server/lib/render-server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { RequestHandler } from '../next'

import './cpu-profile'
import v8 from 'v8'
import http from 'http'
import { isIPv6 } from 'net'
Expand Down
9 changes: 5 additions & 4 deletions packages/next/src/server/lib/server-ipc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getNodeOptionsWithoutInspect } from '../utils'
import { deserializeErr, errorToJSON } from '../../render'
import crypto from 'crypto'
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
// it already and can cause unexpected message errors
Expand Down Expand Up @@ -115,10 +115,11 @@ export const createWorker = async (
: 'next',
}
: {}),
...(process.env.NEXT_CPU_PROF
? { __NEXT_PRIVATE_CPU_PROFILE: `CPU.${type}-renderer` }
: {}),
},
execArgv: isNodeDebugging
? genRenderExecArgv(await getFreePort(), type)
: undefined,
execArgv: await genRenderExecArgv(isNodeDebugging, type),
},
exposedMethods: [
'initialize',
Expand Down
15 changes: 7 additions & 8 deletions packages/next/src/server/lib/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { isIPv6 } from 'net'
import * as Log from '../../build/output/log'
import { normalizeRepeatedSlashes } from '../../shared/lib/utils'
import { initialEnv } from '@next/env'
import { genExecArgv, getNodeOptionsWithoutInspect } from './utils'
import { getFreePort } from './worker-utils'
import { genRouterWorkerExecArgv, getNodeOptionsWithoutInspect } from './utils'

export interface StartServerOptions {
dir: string
Expand Down Expand Up @@ -187,17 +186,17 @@ export async function startServer({
// TODO: do we want to allow more than 10 OOM restarts?
maxRetries: 10,
forkOptions: {
execArgv: isNodeDebugging
? genExecArgv(
isNodeDebugging === undefined ? false : isNodeDebugging,
await getFreePort()
)
: undefined,
execArgv: await genRouterWorkerExecArgv(
isNodeDebugging === undefined ? false : isNodeDebugging
),
env: {
FORCE_COLOR: '1',
...((initialEnv || process.env) as typeof process.env),
PORT: port + '',
NODE_OPTIONS: getNodeOptionsWithoutInspect(),
...(process.env.NEXT_CPU_PROF
? { __NEXT_PRIVATE_CPU_PROFILE: `CPU.router` }
: {}),
},
},
exposedMethods: ['initialize'],
Expand Down
12 changes: 8 additions & 4 deletions packages/next/src/server/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type arg from 'next/dist/compiled/arg/index.js'
import { getFreePort } from './worker-utils'

export function printAndExit(message: string, code = 1) {
if (code === 0) {
Expand All @@ -10,24 +11,27 @@ export function printAndExit(message: string, code = 1) {
process.exit(code)
}

export const genExecArgv = (enabled: boolean | 'brk', debugPort: number) => {
export const genRouterWorkerExecArgv = async (
isNodeDebugging: boolean | 'brk'
) => {
const execArgv = process.execArgv.filter((localArg) => {
return (
!localArg.startsWith('--inspect') && !localArg.startsWith('--inspect-brk')
)
})

if (enabled) {
if (isNodeDebugging) {
const debugPort = await getFreePort()
execArgv.push(
`--inspect${enabled === 'brk' ? '-brk' : ''}=${debugPort + 1}`
`--inspect${isNodeDebugging === 'brk' ? '-brk' : ''}=${debugPort + 1}`
)
}

return execArgv
}

const NODE_INSPECT_RE = /--inspect(-brk)?(=\S+)?( |$)/
export function getNodeOptionsWithoutInspect() {
const NODE_INSPECT_RE = /--inspect(-brk)?(=\S+)?( |$)/
return (process.env.NODE_OPTIONS || '').replace(NODE_INSPECT_RE, '')
}

Expand Down
48 changes: 28 additions & 20 deletions packages/next/src/server/lib/worker-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,40 @@ export const getFreePort = async (): Promise<number> => {
})
}

export const genRenderExecArgv = (debugPort: number, type: 'pages' | 'app') => {
const isDebugging =
process.execArgv.some((localArg) => localArg.startsWith('--inspect')) ||
process.env.NODE_OPTIONS?.match?.(/--inspect(=\S+)?( |$)/)

const isDebuggingWithBrk =
process.execArgv.some((localArg) => localArg.startsWith('--inspect-brk')) ||
process.env.NODE_OPTIONS?.match?.(/--inspect-brk(=\S+)?( |$)/)

if (isDebugging || isDebuggingWithBrk) {
Log.info(
`the --inspect${
isDebuggingWithBrk ? '-brk' : ''
} option was detected, the Next.js server${
type === 'pages' ? ' for pages' : type === 'app' ? ' for app' : ''
} should be inspected at port ${debugPort}.`
)
}
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 (isDebugging || isDebuggingWithBrk) {
execArgv.push(`--inspect${isDebuggingWithBrk ? '-brk' : ''}=${debugPort}`)
if (isNodeDebugging) {
const debugPort = await getFreePort()
const isDebugging =
process.execArgv.some((localArg) => localArg.startsWith('--inspect')) ||
process.env.NODE_OPTIONS?.match?.(/--inspect(=\S+)?( |$)/)

const isDebuggingWithBrk =
process.execArgv.some((localArg) =>
localArg.startsWith('--inspect-brk')
) || process.env.NODE_OPTIONS?.match?.(/--inspect-brk(=\S+)?( |$)/)

if (isDebugging || isDebuggingWithBrk) {
Log.info(
`the --inspect${
isDebuggingWithBrk ? '-brk' : ''
} option was detected, the Next.js server${
type === 'pages' ? ' for pages' : type === 'app' ? ' for app' : ''
} should be inspected at port ${debugPort}.`
)
}

if (isDebugging || isDebuggingWithBrk) {
execArgv.push(`--inspect${isDebuggingWithBrk ? '-brk' : ''}=${debugPort}`)
}
}

return execArgv
Expand Down