Skip to content

Build your personal knowledge base with Trilium Notes

chore(server): add opt-in CPU profiling for import analysis

Elian Doran57982d4f748b
diff --git a/apps/server/.gitignore b/apps/server/.gitignore
index 68c443533a..414815f21f 100644
--- a/apps/server/.gitignore
+++ b/apps/server/.gitignore
@@ -1,2 +1,3 @@
spec/db/*
-!spec/db/config.ini
\ No newline at end of file
+!spec/db/config.ini
+profiles/
\ No newline at end of file
diff --git a/apps/server/package.json b/apps/server/package.json
index 18f2aa5a77..4d436a351f 100644
--- a/apps/server/package.json
+++ b/apps/server/package.json
@@ -8,6 +8,7 @@
"dev": "cross-env NODE_ENV=development TRILIUM_ENV=dev TRILIUM_DATA_DIR=data TRILIUM_RESOURCE_DIR=src tsx watch --ignore '../client/node_modules/.vite-temp' ./src/main.ts",
"dev-alt": "cross-env NODE_ENV=development TRILIUM_ENV=dev TRILIUM_DATA_DIR=data2 TRILIUM_RESOURCE_DIR=src tsx watch --ignore '../client/node_modules/.vite-temp' ./src/main.ts",
"start-no-dir": "cross-env NODE_ENV=development TRILIUM_ENV=dev TRILIUM_RESOURCE_DIR=src tsx watch --ignore '../client/node_modules/.vite-temp' ./src/main.ts",
+ "profile": "cross-env NODE_ENV=development TRILIUM_ENV=dev TRILIUM_DATA_DIR=data TRILIUM_RESOURCE_DIR=src TRILIUM_PROFILE=1 node --import tsx ./src/main.ts",
"edit-integration-db": "cross-env NODE_ENV=development TRILIUM_PORT=8086 TRILIUM_ENV=dev TRILIUM_DATA_DIR=spec/db TRILIUM_INTEGRATION_TEST=edit TRILIUM_RESOURCE_DIR=src tsx watch --ignore '../client/node_modules/.vite-temp' ./src/main.ts",
"build": "tsx scripts/build.ts",
"package": "pnpm build && bash scripts/build-server.sh",
diff --git a/apps/server/src/www.ts b/apps/server/src/www.ts
index 1c3b3f80d8..4886dcf963 100644
--- a/apps/server/src/www.ts
+++ b/apps/server/src/www.ts
@@ -3,6 +3,8 @@ import type { Express } from "express";
import fs from "fs";
import http from "http";
import https from "https";
+import inspector from "inspector";
+import path from "path";
import tmp from "tmp";
import buildApp from "./app.js";
@@ -37,13 +39,33 @@ export default async function startTriliumServer(): Promise<Express> {
getLog().info(error);
});
- function exit() {
- console.log("Caught interrupt/termination signal. Exiting.");
- process.exit(0);
+ // When TRILIUM_PROFILE is set we drive the V8 CPU profiler ourselves rather than relying on Node's
+ // --cpu-prof flag: that flag flushes its file during the normal shutdown path, which the exit() handler
+ // below short-circuits with process.exit(0), so the profile is silently never written.
+ const profilerSession = startCpuProfiler();
+
+ let exiting = false;
+ function exit(reason: string) {
+ if (exiting) {
+ return;
+ }
+ exiting = true;
+ console.log(reason);
+ // Writing the profile needs an async inspector round-trip. On a signal this races the process
+ // teardown and often loses, so the reliable trigger is the Enter keypress wired up below; the
+ // signal path is best-effort.
+ writeCpuProfile(profilerSession, () => process.exit(0));
}
- process.on("SIGINT", exit);
- process.on("SIGTERM", exit);
+ process.on("SIGINT", () => exit("Caught interrupt/termination signal. Exiting."));
+ process.on("SIGTERM", () => exit("Caught interrupt/termination signal. Exiting."));
+
+ if (profilerSession) {
+ // Pressing Enter fires during normal event-loop operation, so the async profile flush completes
+ // before we exit — unlike Ctrl+C, which the V8 inspector round-trip can't reliably outrun.
+ process.stdin.resume();
+ process.stdin.on("data", () => exit("Writing CPU profile and exiting (TRILIUM_PROFILE)."));
+ }
if (utils.compareVersions(process.versions.node, MINIMUM_NODE_VERSION) < 0) {
console.error();
@@ -77,6 +99,60 @@ export default async function startTriliumServer(): Promise<Express> {
return app;
}
+/**
+ * Starts the V8 CPU profiler when TRILIUM_PROFILE is set, returning the inspector session (or null when
+ * profiling is disabled). The companion {@link writeCpuProfile} stops it and writes the `.cpuprofile`.
+ */
+function startCpuProfiler(): inspector.Session | null {
+ if (!process.env.TRILIUM_PROFILE) {
+ return null;
+ }
+
+ const session = new inspector.Session();
+ session.connect();
+ session.post("Profiler.enable", () => {
+ session.post("Profiler.start", () => {
+ console.log("CPU profiler started (TRILIUM_PROFILE). Press Enter in this terminal to write the profile and exit.");
+ });
+ });
+
+ return session;
+}
+
+/**
+ * Stops the profiler started by {@link startCpuProfiler}, writes the result into `profiles/` (relative to the
+ * working directory), then invokes `done`. When profiling is disabled, or if writing fails, `done` still runs
+ * so shutdown is never blocked.
+ */
+function writeCpuProfile(session: inspector.Session | null, done: () => void) {
+ if (!session) {
+ done();
+ return;
+ }
+
+ // Don't let a stuck profiler hang shutdown — exit anyway after a short grace period.
+ const fallback = setTimeout(done, 3000);
+
+ session.post("Profiler.stop", (err, result) => {
+ clearTimeout(fallback);
+ try {
+ if (err) {
+ throw err;
+ }
+ const dir = path.resolve("profiles");
+ fs.mkdirSync(dir, { recursive: true });
+ const file = path.join(dir, `import-${process.pid}.cpuprofile`);
+ fs.writeFileSync(file, JSON.stringify(result.profile));
+ console.log(`CPU profile written to ${file}`);
+ } catch (e) {
+ console.error("Failed to write CPU profile:", e);
+ } finally {
+ session.disconnect();
+ done();
+ }
+ });
+}
+
async function displayStartupMessage() {
getLog().info(`\n${LOGO.replace("[version]", appInfo.appVersion)}`);
getLog().info(`📦 Versions: app=${appInfo.appVersion} db=${appInfo.dbVersion} sync=${appInfo.syncVersion} clipper=${appInfo.clipperProtocolVersion}`);