From b983a5553bb42fb381b0df7e6ce033cf07c2ff3a Mon Sep 17 00:00:00 2001 From: Matt Godbolt Date: Wed, 26 Feb 2025 09:20:32 -0600 Subject: [PATCH] Add stats tracking Also makes the default cleanup time much more often: there's not too much harm in it as best I can see, and logging into the boxes shows a decent amount of `/tmp/` crap is being left and cleaned up every 10m. --- lib/handlers/compile.ts | 34 ++++++++++++++++++++++++++++------ lib/temp.ts | 41 ++++++++++++++++++++++++++++++++++++++++- test/temp-tests.ts | 10 +++++++++- 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/lib/handlers/compile.ts b/lib/handlers/compile.ts index 9736a03ba..a67866db3 100644 --- a/lib/handlers/compile.ts +++ b/lib/handlers/compile.ts @@ -28,7 +28,7 @@ import fs from 'node:fs/promises'; import * as Sentry from '@sentry/node'; import express from 'express'; import Server from 'http-proxy'; -import PromClient, {Counter} from 'prom-client'; +import {Counter, Gauge} from 'prom-client'; import _ from 'underscore'; import which from 'which'; @@ -70,7 +70,27 @@ let hasSetUpAutoClean = false; function initialise(compilerEnv: CompilationEnvironment) { if (hasSetUpAutoClean) return; hasSetUpAutoClean = true; - const tempDirCleanupSecs = compilerEnv.ceProps('tempDirCleanupSecs', 600); + + new Gauge({ + name: 'ce_compilation_temp_dirs_created', + help: 'Total number of temporary directories created', + async collect() { + this.set(temp.getStats().numCreated); + }, + }); + new Gauge({ + name: 'ce_compilation_temp_dirs_removed', + help: 'Total number of temporary directories that had to be removed by the temp system', + async collect() { + this.set(temp.getStats().numRemoved); + }, + }); + const tempDirsBusy = new Counter({ + name: 'ce_compilation_temp_dirs_busy', + help: 'Total number of times the temp dir system was busy', + }); + + const tempDirCleanupSecs = compilerEnv.ceProps('tempDirCleanupSecs', 30); logger.info(`Cleaning temp dirs every ${tempDirCleanupSecs} secs`); let cyclesBusy = 0; @@ -78,6 +98,7 @@ function initialise(compilerEnv: CompilationEnvironment) { const status = compilerEnv.compilationQueue!.status(); if (status.busy) { cyclesBusy++; + tempDirsBusy.inc(); logger.warn( `temp cleanup skipped, pending: ${status.pending}, waiting: ${status.size}, cycles: ${cyclesBusy}`, ); @@ -87,6 +108,7 @@ function initialise(compilerEnv: CompilationEnvironment) { cyclesBusy = 0; await temp.cleanup(); + logger.debug('Temp cleanup stats', temp.getStats()); }, tempDirCleanupSecs * 1000); } @@ -108,22 +130,22 @@ export class CompileHandler implements ICompileHandler { private readonly proxy: Server; private readonly awsProps: PropertyGetter; private clientOptions: ClientOptionsType | null = null; - private readonly compileCounter: Counter = new PromClient.Counter({ + private readonly compileCounter = new Counter({ name: 'ce_compilations_total', help: 'Number of compilations', labelNames: ['language'], }); - private readonly executeCounter: Counter = new PromClient.Counter({ + private readonly executeCounter = new Counter({ name: 'ce_executions_total', help: 'Number of executions', labelNames: ['language'], }); - private readonly cmakeCounter: Counter = new PromClient.Counter({ + private readonly cmakeCounter = new Counter({ name: 'ce_cmake_compilations_total', help: 'Number of CMake compilations', labelNames: ['language'], }); - private readonly cmakeExecuteCounter: Counter = new PromClient.Counter({ + private readonly cmakeExecuteCounter = new Counter({ name: 'ce_cmake_executions_total', help: 'Number of executions after CMake', labelNames: ['language'], diff --git a/lib/temp.ts b/lib/temp.ts index 9f8391748..53026b4e5 100644 --- a/lib/temp.ts +++ b/lib/temp.ts @@ -26,8 +26,38 @@ import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import {logger} from './logger.js'; +import * as utils from './utils.js'; const pendingRemoval: string[] = []; +export type Stats = { + numCreated: number; + numActive: number; + numRemoved: number; + numAlreadyGone: number; +}; + +const stats = { + numCreated: 0, + numRemoved: 0, + numAlreadyGone: 0, +}; + +/** + * Get the current stats for temporary directories. + */ +export function getStats(): Stats { + return { + ...stats, + numActive: pendingRemoval.length, + }; +} + +// Reset stats, for tests only. +export function resetStats() { + stats.numCreated = 0; + stats.numRemoved = 0; + stats.numAlreadyGone = 0; +} /** * Create a temporary directory, always in the operating systems' temporary directory. @@ -35,6 +65,7 @@ const pendingRemoval: string[] = []; */ export async function mkdir(prefix: string) { const result = await fs.promises.mkdtemp(path.join(os.tmpdir(), prefix)); + ++stats.numCreated; pendingRemoval.push(result); return result; } @@ -45,6 +76,7 @@ export async function mkdir(prefix: string) { */ export function mkdirSync(prefix: string) { const result = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); + ++stats.numCreated; pendingRemoval.push(result); return result; } @@ -56,15 +88,22 @@ export async function cleanup() { // "Atomically" take a copy of the things to remove and set it to an empty array. const toRemove = pendingRemoval.splice(0, pendingRemoval.length); let numRemoved = 0; + let numAlreadyGone = 0; for (const dir of toRemove) { + if (!(await utils.dirExists(dir))) { + ++stats.numAlreadyGone; + ++numAlreadyGone; + continue; + } try { await fs.promises.rm(dir, {recursive: true, force: true}); ++numRemoved; + ++stats.numRemoved; } catch (e) { logger.error(`Failed to remove ${dir}: ${e}`); } } - logger.debug(`Removed ${numRemoved} of ${toRemove.length} temporary directories`); + logger.debug(`Removed ${numRemoved} (${numAlreadyGone} already gone) of ${toRemove.length} temporary directories`); } process.on('exit', async () => { diff --git a/test/temp-tests.ts b/test/temp-tests.ts index 6657cce24..a0bbc8698 100644 --- a/test/temp-tests.ts +++ b/test/temp-tests.ts @@ -31,12 +31,17 @@ import * as utils from '../lib/utils.js'; describe('Creates and tracks temporary directories', () => { const osTemp = os.tmpdir(); - afterEach(async () => await temp.cleanup()); + afterEach(async () => { + await temp.cleanup(); + temp.resetStats(); + }); it('creates directories under $TMPDIR', async () => { + expect(temp.getStats()).toEqual({numCreated: 0, numActive: 0, numRemoved: 0}); const newTemp = await temp.mkdir('prefix'); expect(newTemp).toContain(osTemp); expect(await utils.dirExists(newTemp)).toBe(true); + expect(temp.getStats()).toEqual({numCreated: 1, numActive: 1, numRemoved: 0}); }); it('creates directories with prefix', async () => { const newTemp = await temp.mkdir('prefix'); @@ -49,13 +54,16 @@ describe('Creates and tracks temporary directories', () => { expect(temp1).not.toEqual(temp2); expect(temp1).not.toEqual(temp3); expect(temp2).not.toEqual(temp3); + expect(temp.getStats()).toEqual({numCreated: 3, numActive: 3, numRemoved: 0}); }); it('cleans up directories even if not empty', async () => { const newTemp1 = await temp.mkdir('prefix'); await utils.ensureFileExists(path.join(newTemp1, 'some', 'dirs', 'under', 'file')); const newTemp2 = await temp.mkdir('prefix'); const newTemp3 = await temp.mkdir('prefix'); + expect(temp.getStats()).toEqual({numCreated: 3, numActive: 3, numRemoved: 0}); await temp.cleanup(); + expect(temp.getStats()).toEqual({numCreated: 3, numActive: 0, numRemoved: 3}); expect(await utils.dirExists(newTemp1)).toBe(false); expect(await utils.dirExists(newTemp2)).toBe(false); expect(await utils.dirExists(newTemp3)).toBe(false);