From 23eb21e674c6628a4ff9e8d1a25a4e07aaa96d68 Mon Sep 17 00:00:00 2001 From: Giulio Moro Date: Fri, 29 Nov 2024 10:20:52 -0500 Subject: [PATCH] IDE: factored out IDESettings from CPUMonitor. This introduces a slight observable change: CPU monitoring enabled/disabled is only read once per project execution. --- IDE/dist/CPUMonitor.js | 38 +++++++++++++--------------- IDE/dist/ProcessManager.js | 52 +++++++++++++++++++++++--------------- IDE/src/CPUMonitor.ts | 3 --- IDE/src/ProcessManager.ts | 15 ++++++----- 4 files changed, 58 insertions(+), 50 deletions(-) diff --git a/IDE/dist/CPUMonitor.js b/IDE/dist/CPUMonitor.js index 0766e2d33..c10e3bed1 100644 --- a/IDE/dist/CPUMonitor.js +++ b/IDE/dist/CPUMonitor.js @@ -47,7 +47,6 @@ var __values = (this && this.__values) || function (o) { Object.defineProperty(exports, "__esModule", { value: true }); var child_process = require("child_process"); var pidtree = require("pidtree"); -var ide_settings = require("./IDESettings"); // this module monitors the linux-domain CPU usage of a running bela process // once it has found the correct pid it calls the callback passed to start() // every second with the cpu usage as a parameter @@ -83,38 +82,35 @@ function timeout_func() { var cpu, e_1; return __generator(this, function (_a) { switch (_a.label) { - case 0: return [4 /*yield*/, ide_settings.get_setting('cpuMonitoring')]; - case 1: - if (!(_a.sent())) - return [2 /*return*/]; + case 0: cpu = '0'; - _a.label = 2; - case 2: - _a.trys.push([2, 8, 9, 10]); - if (!!found_pid) return [3 /*break*/, 5]; - if (!(find_pid_count++ < 3)) return [3 /*break*/, 4]; + _a.label = 1; + case 1: + _a.trys.push([1, 7, 8, 9]); + if (!!found_pid) return [3 /*break*/, 4]; + if (!(find_pid_count++ < 3)) return [3 /*break*/, 3]; return [4 /*yield*/, find_pid()]; - case 3: + case 2: _a.sent(); - _a.label = 4; - case 4: return [3 /*break*/, 7]; - case 5: return [4 /*yield*/, getCPU()]; - case 6: + _a.label = 3; + case 3: return [3 /*break*/, 6]; + case 4: return [4 /*yield*/, getCPU()]; + case 5: cpu = _a.sent(); - _a.label = 7; - case 7: return [3 /*break*/, 10]; - case 8: + _a.label = 6; + case 6: return [3 /*break*/, 9]; + case 7: e_1 = _a.sent(); console.log('Failed to get CPU usage'); found_pid = false; - return [3 /*break*/, 10]; - case 9: + return [3 /*break*/, 9]; + case 8: if (!stopped) { callback(cpu); timeout = setTimeout(timeout_func, 1000); } return [7 /*endfinally*/]; - case 10: return [2 /*return*/]; + case 9: return [2 /*return*/]; } }); }); diff --git a/IDE/dist/ProcessManager.js b/IDE/dist/ProcessManager.js index ef0d866b9..0f14f4641 100644 --- a/IDE/dist/ProcessManager.js +++ b/IDE/dist/ProcessManager.js @@ -56,6 +56,7 @@ var cpu_monitor = require("./CPUMonitor"); var path = require("path"); var MostRecentQueue_1 = require("./MostRecentQueue"); var globals = require("./globals"); +var ide_settings = require("./IDESettings"); var lock = new Lock_1.Lock("ProcessManager"); var syntaxTimeout; // storing the value returned by setTimeout var syntaxTimeoutMs = 300; // ms between received data and start of syntax checking @@ -304,26 +305,37 @@ processes.build.on('finish', function (stderr, killed) { socket_manager.broadcast('std-warn', stderr); }); processes.build.on('stdout', function (data) { return socket_manager.broadcast('status', { buildLog: data }); }); -processes.run.on('start', function (pid, project) { - socket_manager.broadcast('status', get_status()); - cpu_monitor.start(pid, project, function (cpu) { return __awaiter(_this, void 0, void 0, function () { - var _a, _b, _c, _d; - return __generator(this, function (_e) { - switch (_e.label) { - case 0: - _b = (_a = socket_manager).broadcast; - _c = ['cpu-usage']; - _d = {}; - return [4 /*yield*/, file_manager.read_file(paths.xenomai_stat).catch(function (e) { return console.log('error reading xenomai stats', e); })]; - case 1: - _b.apply(_a, _c.concat([(_d.bela = _e.sent(), - _d.belaLinux = cpu, - _d)])); - return [2 /*return*/]; - } - }); - }); }); -}); +processes.run.on('start', function (pid, project) { return __awaiter(_this, void 0, void 0, function () { + var _this = this; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + socket_manager.broadcast('status', get_status()); + return [4 /*yield*/, ide_settings.get_setting('cpuMonitoring')]; + case 1: + if ((_a.sent())) { + cpu_monitor.start(pid, project, function (cpu) { return __awaiter(_this, void 0, void 0, function () { + var _a, _b, _c, _d; + return __generator(this, function (_e) { + switch (_e.label) { + case 0: + _b = (_a = socket_manager).broadcast; + _c = ['cpu-usage']; + _d = {}; + return [4 /*yield*/, file_manager.read_file(paths.xenomai_stat).catch(function (e) { return console.log('error reading xenomai stats', e); })]; + case 1: + _b.apply(_a, _c.concat([(_d.bela = _e.sent(), + _d.belaLinux = cpu, + _d)])); + return [2 /*return*/]; + } + }); + }); }); + } + return [2 /*return*/]; + } + }); +}); }); processes.run.on('finish', function (project) { socket_manager.broadcast('status', get_status()); cpu_monitor.stop(); diff --git a/IDE/src/CPUMonitor.ts b/IDE/src/CPUMonitor.ts index 2660606d4..d26273531 100644 --- a/IDE/src/CPUMonitor.ts +++ b/IDE/src/CPUMonitor.ts @@ -1,6 +1,5 @@ import * as child_process from 'child_process'; import * as pidtree from 'pidtree'; -import * as ide_settings from './IDESettings'; // this module monitors the linux-domain CPU usage of a running bela process // once it has found the correct pid it calls the callback passed to start() @@ -34,8 +33,6 @@ export function stop(){ // this function keeps trying every second to find the correct pid // once it has, it uses ps to get the cpu usage, and calls the callback async function timeout_func(){ - if (!(await ide_settings.get_setting('cpuMonitoring'))) - return; let cpu: any = '0'; try{ if (!found_pid){ diff --git a/IDE/src/ProcessManager.ts b/IDE/src/ProcessManager.ts index 558f40540..e7d19ae4f 100644 --- a/IDE/src/ProcessManager.ts +++ b/IDE/src/ProcessManager.ts @@ -9,6 +9,7 @@ import * as cpu_monitor from './CPUMonitor'; import * as path from 'path'; import { MostRecentQueue } from './MostRecentQueue'; import * as globals from './globals'; +import * as ide_settings from './IDESettings'; const lock: Lock = new Lock("ProcessManager"); let syntaxTimeout : NodeJS.Timer; // storing the value returned by setTimeout @@ -206,14 +207,16 @@ processes.build.on('finish', (stderr: string, killed: boolean) => { }); processes.build.on('stdout', (data) => socket_manager.broadcast('status', {buildLog: data}) ); -processes.run.on('start', (pid: number, project: string) => { +processes.run.on('start', async (pid: number, project: string) => { socket_manager.broadcast('status', get_status()); - cpu_monitor.start(pid, project, async cpu => { - socket_manager.broadcast('cpu-usage', { - bela: await file_manager.read_file(paths.xenomai_stat).catch(e => console.log('error reading xenomai stats', e)), - belaLinux: cpu + if ((await ide_settings.get_setting('cpuMonitoring'))) { + cpu_monitor.start(pid, project, async cpu => { + socket_manager.broadcast('cpu-usage', { + bela: await file_manager.read_file(paths.xenomai_stat).catch(e => console.log('error reading xenomai stats', e)), + belaLinux: cpu + }); }); - }); + } }); processes.run.on('finish', (project: string) => { socket_manager.broadcast('status', get_status());