mirror of
https://github.com/compiler-explorer/compiler-explorer.git
synced 2026-12-02 03:19:47 -06:00
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.
This commit is contained in:
+28
-6
@@ -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<string> = new PromClient.Counter({
|
||||
private readonly compileCounter = new Counter({
|
||||
name: 'ce_compilations_total',
|
||||
help: 'Number of compilations',
|
||||
labelNames: ['language'],
|
||||
});
|
||||
private readonly executeCounter: Counter<string> = new PromClient.Counter({
|
||||
private readonly executeCounter = new Counter({
|
||||
name: 'ce_executions_total',
|
||||
help: 'Number of executions',
|
||||
labelNames: ['language'],
|
||||
});
|
||||
private readonly cmakeCounter: Counter<string> = new PromClient.Counter({
|
||||
private readonly cmakeCounter = new Counter({
|
||||
name: 'ce_cmake_compilations_total',
|
||||
help: 'Number of CMake compilations',
|
||||
labelNames: ['language'],
|
||||
});
|
||||
private readonly cmakeExecuteCounter: Counter<string> = new PromClient.Counter({
|
||||
private readonly cmakeExecuteCounter = new Counter({
|
||||
name: 'ce_cmake_executions_total',
|
||||
help: 'Number of executions after CMake',
|
||||
labelNames: ['language'],
|
||||
|
||||
+40
-1
@@ -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 () => {
|
||||
|
||||
+9
-1
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user