From 704b3e479daa5f34edb3a488d4a77ef8db2b990d Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Thu, 14 Sep 2023 11:30:30 +0200 Subject: [PATCH 1/2] Fix CPU profile generation --- packages/next/src/cli/next-build.ts | 2 ++ packages/next/src/cli/next-dev.ts | 2 ++ packages/next/src/cli/next-start.ts | 1 + packages/next/src/server/lib/cpu-profile.ts | 7 +++++-- packages/next/src/server/lib/setup-server-worker.ts | 1 - packages/next/src/server/lib/start-server.ts | 5 ----- 6 files changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/next/src/cli/next-build.ts b/packages/next/src/cli/next-build.ts index 223b772ff6578..d0b3334e55454 100755 --- a/packages/next/src/cli/next-build.ts +++ b/packages/next/src/cli/next-build.ts @@ -1,4 +1,6 @@ #!/usr/bin/env node + +import '../server/lib/cpu-profile' import { existsSync } from 'fs' import * as Log from '../build/output/log' import { CliCommand } from '../lib/commands' diff --git a/packages/next/src/cli/next-dev.ts b/packages/next/src/cli/next-dev.ts index 3ed202900d2c4..b96dc35450d1f 100644 --- a/packages/next/src/cli/next-dev.ts +++ b/packages/next/src/cli/next-dev.ts @@ -1,4 +1,6 @@ #!/usr/bin/env node + +import '../server/lib/cpu-profile' import type { StartServerOptions } from '../server/lib/start-server' import { getPort, printAndExit } from '../server/lib/utils' import * as Log from '../build/output/log' diff --git a/packages/next/src/cli/next-start.ts b/packages/next/src/cli/next-start.ts index 67c729042b5d7..06cddaed5a86f 100755 --- a/packages/next/src/cli/next-start.ts +++ b/packages/next/src/cli/next-start.ts @@ -1,5 +1,6 @@ #!/usr/bin/env node +import '../server/lib/cpu-profile' import { startServer } from '../server/lib/start-server' import { getPort, printAndExit } from '../server/lib/utils' import { getProjectDir } from '../lib/get-project-dir' diff --git a/packages/next/src/server/lib/cpu-profile.ts b/packages/next/src/server/lib/cpu-profile.ts index 2310513024a33..b0e042c2aa5cc 100644 --- a/packages/next/src/server/lib/cpu-profile.ts +++ b/packages/next/src/server/lib/cpu-profile.ts @@ -1,4 +1,7 @@ -if (process.env.__NEXT_PRIVATE_CPU_PROFILE) { +const privateCpuProfileName = process.env.__NEXT_PRIVATE_CPU_PROFILE +const isCpuProfileEnabled = process.env.NEXT_CPU_PROF || privateCpuProfileName + +if (isCpuProfileEnabled) { const { Session } = require('inspector') as typeof import('inspector') const fs = require('fs') @@ -17,7 +20,7 @@ if (process.env.__NEXT_PRIVATE_CPU_PROFILE) { // Write profile to disk const filename = `${ - process.env.__NEXT_PRIVATE_CPU_PROFILE + privateCpuProfileName || 'CPU.main' }.${Date.now()}.cpuprofile` fs.writeFileSync(`./${filename}`, JSON.stringify(param.profile)) process.exit(0) diff --git a/packages/next/src/server/lib/setup-server-worker.ts b/packages/next/src/server/lib/setup-server-worker.ts index eb08e1bc5d1da..169f9863cd45d 100644 --- a/packages/next/src/server/lib/setup-server-worker.ts +++ b/packages/next/src/server/lib/setup-server-worker.ts @@ -1,4 +1,3 @@ -import './cpu-profile' import v8 from 'v8' import http, { IncomingMessage, ServerResponse } from 'http' diff --git a/packages/next/src/server/lib/start-server.ts b/packages/next/src/server/lib/start-server.ts index 7adcc372f3e9f..5fb8dd76c71bb 100644 --- a/packages/next/src/server/lib/start-server.ts +++ b/packages/next/src/server/lib/start-server.ts @@ -25,11 +25,6 @@ import chalk from '../../lib/chalk' const debug = setupDebug('next:start-server') -if (process.env.NEXT_CPU_PROF) { - process.env.__NEXT_PRIVATE_CPU_PROFILE = `CPU.router` - require('./cpu-profile') -} - export interface StartServerOptions { dir: string port: number From 1f96acf15017449cc082dbc579d17b9bf1b9d271 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Thu, 14 Sep 2023 11:38:39 +0200 Subject: [PATCH 2/2] Ensure process exit writes profile --- packages/next/src/server/lib/cpu-profile.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/next/src/server/lib/cpu-profile.ts b/packages/next/src/server/lib/cpu-profile.ts index b0e042c2aa5cc..68ebe8eca5c70 100644 --- a/packages/next/src/server/lib/cpu-profile.ts +++ b/packages/next/src/server/lib/cpu-profile.ts @@ -28,4 +28,5 @@ if (isCpuProfileEnabled) { } process.on('SIGINT', saveProfile) process.on('SIGTERM', saveProfile) + process.on('exit', saveProfile) }