Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "veryfront",
"version": "0.1.817",
"version": "0.1.818",
"license": "Apache-2.0",
"nodeModulesDir": "auto",
"minimumDependencyAge": {
Expand Down
59 changes: 59 additions & 0 deletions src/utils/memory/profiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import {
forceGC,
getCacheStats,
getHeapStats,
getInitialRapidHeapGrowthState,
getMemoryMonitoringLogContext,
getMemorySnapshot,
getRapidHeapGrowthEvaluation,
registerCache,
setHeapWarningThreshold,
startMemoryMonitoring,
Expand Down Expand Up @@ -136,6 +138,63 @@ describe("memory/profiler", () => {
});
});

describe("getRapidHeapGrowthEvaluation", () => {
it("seeds restart baselines from current heap so stable warm heaps do not warn", () => {
const initialState = getInitialRapidHeapGrowthState(153.01);

const flatSample = getRapidHeapGrowthEvaluation({
previousHeapUsedMB: initialState.lastHeapUsedMB,
currentHeapUsedMB: 153.01,
pending: initialState.pending,
thresholdMB: 100,
});

assertEquals(flatSample.shouldWarn, false);
assertEquals(flatSample.pending, undefined);
});

it("does not warn when a one-interval spike is reclaimed on the next sample", () => {
const first = getRapidHeapGrowthEvaluation({
previousHeapUsedMB: 99.16,
currentHeapUsedMB: 212.69,
pending: undefined,
thresholdMB: 100,
});

assertEquals(first.shouldWarn, false);
assertEquals(first.pending?.baselineHeapUsedMB, 99.16);

const settled = getRapidHeapGrowthEvaluation({
previousHeapUsedMB: 212.69,
currentHeapUsedMB: 153.01,
pending: first.pending,
thresholdMB: 100,
});

assertEquals(settled.shouldWarn, false);
assertEquals(settled.pending, undefined);
});

it("warns when rapid heap growth remains sustained after the next sample", () => {
const first = getRapidHeapGrowthEvaluation({
previousHeapUsedMB: 100,
currentHeapUsedMB: 225,
pending: undefined,
thresholdMB: 100,
});
const sustained = getRapidHeapGrowthEvaluation({
previousHeapUsedMB: 225,
currentHeapUsedMB: 235,
pending: first.pending,
thresholdMB: 100,
});

assertEquals(sustained.shouldWarn, true);
assertEquals(sustained.growthMB, 135);
assertEquals(sustained.pending, undefined);
});
});

describe("checkMemoryPressure", () => {
it("should return an object with critical, warning, and heapUsedPercent", () => {
const result = checkMemoryPressure();
Expand Down
85 changes: 82 additions & 3 deletions src/utils/memory/profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ let memoryCheckInterval: ReturnType<typeof setInterval> | undefined;
let memoryCheckIntervalMs: number | undefined;
let lastHeapUsed = 0;
let heapGrowthWarningThreshold = 0.8;
let pendingRapidHeapGrowth: PendingRapidHeapGrowth | undefined;

export interface PendingRapidHeapGrowth {
baselineHeapUsedMB: number;
observedGrowthMB: number;
}

export interface RapidHeapGrowthEvaluationInput {
previousHeapUsedMB: number;
currentHeapUsedMB: number;
pending: PendingRapidHeapGrowth | undefined;
thresholdMB: number;
}

export interface RapidHeapGrowthEvaluation {
shouldWarn: boolean;
growthMB?: number;
observedGrowthMB?: number;
pending?: PendingRapidHeapGrowth;
}

export interface RapidHeapGrowthState {
lastHeapUsedMB: number;
pending: PendingRapidHeapGrowth | undefined;
}

export function registerCache(name: string, getStats: () => CacheStats): void {
cacheRegistry.set(name, getStats);
Expand Down Expand Up @@ -213,6 +238,49 @@ export function getMemoryMonitoringConfig(env: MemoryMonitoringEnv): MemoryMonit
return { enabled, intervalMs };
}

function roundMemoryMB(value: number): number {
return Math.round(value * 100) / 100;
}

export function getInitialRapidHeapGrowthState(initialHeapUsedMB: number): RapidHeapGrowthState {
return {
lastHeapUsedMB: initialHeapUsedMB,
pending: undefined,
};
}

export function getRapidHeapGrowthEvaluation(
input: RapidHeapGrowthEvaluationInput,
): RapidHeapGrowthEvaluation {
const intervalGrowthMB = roundMemoryMB(input.currentHeapUsedMB - input.previousHeapUsedMB);

if (input.pending) {
const sustainedGrowthMB = roundMemoryMB(
input.currentHeapUsedMB - input.pending.baselineHeapUsedMB,
);
if (sustainedGrowthMB > input.thresholdMB) {
return {
shouldWarn: true,
growthMB: sustainedGrowthMB,
observedGrowthMB: input.pending.observedGrowthMB,
};
}
return { shouldWarn: false };
}

if (intervalGrowthMB > input.thresholdMB) {
return {
shouldWarn: false,
pending: {
baselineHeapUsedMB: input.previousHeapUsedMB,
observedGrowthMB: intervalGrowthMB,
},
};
}

return { shouldWarn: false };
}

export function getMemoryMonitoringState(): MemoryMonitoringState {
return {
active: memoryCheckInterval !== undefined,
Expand All @@ -237,6 +305,9 @@ export function startMemoryMonitoring(intervalMs = DEFAULT_MEMORY_MONITORING_INT

logger.info(`Starting memory monitoring (interval: ${intervalMs}ms)`);
memoryCheckIntervalMs = intervalMs;
const rapidGrowthState = getInitialRapidHeapGrowthState(getHeapStats().usedHeapSizeMB);
lastHeapUsed = rapidGrowthState.lastHeapUsedMB;
pendingRapidHeapGrowth = rapidGrowthState.pending;

memoryCheckInterval = setInterval(() => {
const snapshot = getMemorySnapshot();
Expand All @@ -255,10 +326,17 @@ export function startMemoryMonitoring(intervalMs = DEFAULT_MEMORY_MONITORING_INT
});
}

const heapGrowthMB = heap.usedHeapSizeMB - lastHeapUsed;
if (heapGrowthMB > HEAP_RAPID_GROWTH_THRESHOLD_MB) {
const rapidGrowthEvaluation = getRapidHeapGrowthEvaluation({
previousHeapUsedMB: lastHeapUsed,
currentHeapUsedMB: heap.usedHeapSizeMB,
pending: pendingRapidHeapGrowth,
thresholdMB: HEAP_RAPID_GROWTH_THRESHOLD_MB,
});
pendingRapidHeapGrowth = rapidGrowthEvaluation.pending;
if (rapidGrowthEvaluation.shouldWarn) {
logger.warn("Rapid heap growth detected", {
growthMB: heapGrowthMB,
growthMB: rapidGrowthEvaluation.growthMB,
observedGrowthMB: rapidGrowthEvaluation.observedGrowthMB,
intervalMs,
topCaches: monitoringContext.topCaches,
});
Expand Down Expand Up @@ -291,6 +369,7 @@ export function stopMemoryMonitoring(): void {
clearInterval(memoryCheckInterval);
memoryCheckInterval = undefined;
memoryCheckIntervalMs = undefined;
pendingRapidHeapGrowth = undefined;
logger.info("Memory monitoring stopped");
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/version-constant.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Keep in sync with deno.json version.
// scripts/release.ts updates this constant during releases.
/** Shared version value. */
export const VERSION = "0.1.817";
export const VERSION = "0.1.818";