Файли
openidea/src/features/perf/format.ts
T
l0ng-ai d5cd5e7a55 feat(perf): CPU, PID and End Process in the memory popup (#3437)
The Memory & Diagnostics popup now shows each row's CPU use since the previous measurement (percent of one core; proc_pid_rusage / /proc/<pid>/stat / GetProcessTimes via ide-workspace CpuSampler), the PID in the row tooltip, and an End Process button for language server, run and terminal rows. end_process_tree sends SIGTERM, then SIGKILL after a grace period (TerminateProcess on Windows), and refuses pids that don't descend from the backend; owners see the exit as usual (language servers restart, terminals close).
2026-11-15 02:55:37 +08:00

67 рядки
2.5 KiB
TypeScript
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
/** IDEA-style compact numbers for the memory indicator and diagnostics. */
const KB = 1024;
const MB = KB * 1024;
const GB = MB * 1024;
function trim(n: number): string {
return n.toFixed(1).replace(/\.0$/, "");
}
/** `512B`, `12K`, `412M`, `1.9G`, `2G` (binary units, like IDEA's indicator). */
export function formatBytes(bytes: number | null | undefined): string {
if (bytes == null || !Number.isFinite(bytes) || bytes < 0) return "–";
if (bytes < KB) return `${Math.round(bytes)}B`;
if (bytes < MB) return `${Math.round(bytes / KB)}K`;
// Round first so 1023.6M shows as 1G, not 1024M.
const m = Math.round(bytes / MB);
if (m < 1024) return `${m}M`;
return `${trim(bytes / GB)}G`;
}
/** `560 MB`, `1.1 GB`, `12 KB` (binary units, as Activity Monitor and the Task Manager show them). */
export function formatSize(bytes: number | null | undefined): string {
if (bytes == null || !Number.isFinite(bytes) || bytes < 0) return "–";
if (bytes < KB) return `${Math.round(bytes)} B`;
if (bytes < MB) return `${Math.round(bytes / KB)} KB`;
const m = Math.round(bytes / MB);
if (m < 1024) return `${m} MB`;
return `${trim(bytes / GB)} GB`;
}
/** `0%`, `3.2%`, `45%`, `180%` (percent of one core, so several busy cores exceed 100). */
export function formatPercent(p: number | null | undefined): string {
if (p == null || !Number.isFinite(p) || p < 0) return "–";
if (p < 0.05) return "0%";
return p < 10 ? `${trim(p)}%` : `${Math.round(p)}%`;
}
/** `0.4 ms`, `16 ms`, `2.3 s`. */
export function formatMs(ms: number | null | undefined): string {
if (ms == null || !Number.isFinite(ms)) return "–";
if (ms < 10) return `${trim(ms)} ms`;
if (ms < 1000) return `${Math.round(ms)} ms`;
return `${trim(ms / 1000)} s`;
}
/** `42s`, `5m 03s`, `2h 07m`, `3d 4h`. */
export function formatUptime(ms: number | null | undefined): string {
if (ms == null || !Number.isFinite(ms) || ms < 0) return "–";
const s = Math.floor(ms / 1000);
const pad = (n: number) => String(n).padStart(2, "0");
if (s < 60) return `${s}s`;
const m = Math.floor(s / 60);
if (m < 60) return `${m}m ${pad(s % 60)}s`;
const h = Math.floor(m / 60);
if (h < 24) return `${h}h ${pad(m % 60)}m`;
return `${Math.floor(h / 24)}d ${h % 24}h`;
}
/** `1,234` style grouping without locale surprises. */
export function formatCount(n: number | null | undefined): string {
if (n == null || !Number.isFinite(n)) return "–";
return Math.round(n)
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}