server: remove cluster cpu tracking

This commit is contained in:
Koushik Dutta
2024-12-03 00:21:46 -08:00
parent 3a77a3398d
commit 7b3ab501b2
11 changed files with 14 additions and 88 deletions

View File

@@ -1,18 +1,18 @@
{
"name": "@scrypted/server",
"version": "0.123.52",
"version": "0.123.53",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@scrypted/server",
"version": "0.123.52",
"version": "0.123.53",
"hasInstallScript": true,
"license": "ISC",
"dependencies": {
"@scrypted/ffmpeg-static": "^6.1.0-build3",
"@scrypted/node-pty": "^1.0.22",
"@scrypted/types": "^0.3.87",
"@scrypted/types": "^0.3.88",
"adm-zip": "^0.5.16",
"body-parser": "^1.20.3",
"cookie-parser": "^1.4.7",
@@ -557,9 +557,9 @@
}
},
"node_modules/@scrypted/types": {
"version": "0.3.87",
"resolved": "https://registry.npmjs.org/@scrypted/types/-/types-0.3.87.tgz",
"integrity": "sha512-67tY0JhRBmHQ1Qvu1CVr3byy13bir7OUhcVMCMf3QDDMfe7bwxXQ7wlsiwEpGn/BY49nioaYMT0CycDKz6gWYQ==",
"version": "0.3.88",
"resolved": "https://registry.npmjs.org/@scrypted/types/-/types-0.3.88.tgz",
"integrity": "sha512-Zw8ASZB4yUHcv9BPqvyOXTSTcRpVZY+1harH13YgHZXyGrsRda20donHpf/1k//357YorJdEng50XmSiOusbgA==",
"license": "ISC"
},
"node_modules/@types/adm-zip": {

View File

@@ -5,7 +5,7 @@
"dependencies": {
"@scrypted/ffmpeg-static": "^6.1.0-build3",
"@scrypted/node-pty": "^1.0.22",
"@scrypted/types": "^0.3.87",
"@scrypted/types": "^0.3.88",
"adm-zip": "^0.5.16",
"body-parser": "^1.20.3",
"cookie-parser": "^1.4.7",

View File

@@ -1,48 +0,0 @@
import { CpuInfo, cpus } from 'os';
function getIdleTotal(cpu: CpuInfo) {
const t = cpu.times;
const total = t.user + t.nice + t.sys + t.idle + t.irq;
const idle = t.idle;
return {
idle,
total,
}
}
export class CpuTimer {
previousSample: ReturnType<typeof cpus>;
maxSpeed = 0;
sample(): number {
const sample = cpus();
const previousSample = this.previousSample;
this.previousSample = sample;
// can cpu count change at runtime, who knows
if (!previousSample || previousSample.length !== sample.length)
return 0;
// cpu may be throttled in low power mode, so observe total speed to scale
let totalSpeed = 0;
const times = sample.map((v, i) => {
totalSpeed += v.speed;
const c = getIdleTotal(v);
const p = getIdleTotal(previousSample[i]);
const total = c.total - p.total;
const idle = c.idle - p.idle;
return 1 - idle / total;
});
this.maxSpeed = Math.max(this.maxSpeed, totalSpeed);
// will return a value between 0 and 1, where 1 is full cpu speed
// the cpu usage is scaled by the clock speed
// so if the cpu is running at 1ghz out of 3ghz, the cpu usage is scaled by 1/3
const clockScale = totalSpeed / this.maxSpeed;
const total = times.reduce((p, c) => p + c, 0);
return total / sample.length * clockScale;
}
}

View File

@@ -11,7 +11,6 @@ import { computeClusterObjectHash } from './cluster/cluster-hash';
import { getClusterLabels, getClusterWorkerWeight } from './cluster/cluster-labels';
import { getScryptedClusterMode, InitializeCluster, setupCluster } from './cluster/cluster-setup';
import type { ClusterObject } from './cluster/connect-rpc-object';
import { CpuTimer } from './cluster/cpu-timer';
import type { PluginAPI } from './plugin/plugin-api';
import { getPluginVolume, getScryptedVolume } from './plugin/plugin-volume';
import { prepareZip } from './plugin/runtime/node-worker-common';
@@ -85,7 +84,6 @@ export interface RunningClusterWorker extends ClusterWorkerProperties {
forks: Set<ClusterForkOptions>;
address: string;
weight: number;
cpuUsage: number;
}
export class PeerLiveness {
@@ -208,7 +206,6 @@ export function startClusterClient(mainFilename: string, serviceControl?: Servic
console.log('Cluster client starting.');
const envControl = new EnvControl();
const cpuTimer = new CpuTimer();
const originalClusterAddress = process.env.SCRYPTED_CLUSTER_ADDRESS;
const labels = getClusterLabels();
@@ -262,7 +259,6 @@ export function startClusterClient(mainFilename: string, serviceControl?: Servic
peer.params['service-control'] = serviceControl;
peer.params['env-control'] = envControl;
peer.params['info'] = new Info();
peer.params['cpu'] = async () => cpuTimer.sample();
const { localAddress, localPort } = socket;
console.log('Cluster server connected.', localAddress, localPort);
@@ -318,17 +314,9 @@ export function createClusterServer(mainFilename: string, scryptedRuntime: Scryp
address: process.env.SCRYPTED_CLUSTER_ADDRESS,
weight: getClusterWorkerWeight(),
forks: new Set(),
cpuUsage: 0,
};
scryptedRuntime.clusterWorkers.set(scryptedRuntime.serverClusterWorkerId, serverWorker);
{
const cpuTimer = new CpuTimer();
setInterval(() => {
serverWorker.cpuUsage = cpuTimer.sample();
}, 1000);
}
const server = tls.createServer({
key: certificate.serviceKey,
cert: certificate.certificate,
@@ -363,7 +351,6 @@ export function createClusterServer(mainFilename: string, scryptedRuntime: Scryp
name: auth.id,
address: socket.remoteAddress,
forks: new Set(),
cpuUsage: 0,
};
scryptedRuntime.clusterWorkers.set(id, worker);
peer.killedSafe.finally(() => {
@@ -373,16 +360,6 @@ export function createClusterServer(mainFilename: string, scryptedRuntime: Scryp
scryptedRuntime.clusterWorkers.delete(id);
});
console.log('Cluster client authenticated.', socket.remoteAddress, socket.remotePort, properties);
let cpu: Promise<() => Promise<number>>;
const cpuTimer = setInterval(async () => {
cpu ||= peer.getParam('cpu');
const usage = await (await cpu)?.();
worker.cpuUsage = usage;
}, 1000);
peer.killedSafe.finally(() => {
clearInterval(cpuTimer);
});
}
catch (e) {
peer.kill(e);

View File

@@ -107,7 +107,6 @@ export class ClusterForkService {
name: worker.name,
labels: worker.labels,
forks: [...worker.forks] as ClusterFork[],
cpuUsage: worker.cpuUsage,
};
}
return ret;