Skip to content

Commit

Permalink
IDE: factored out IDESettings from CPUMonitor.
Browse files Browse the repository at this point in the history
This introduces a slight observable change: CPU monitoring
enabled/disabled is only read once per project execution.
  • Loading branch information
giuliomoro committed Nov 29, 2024
1 parent e1abc2a commit 23eb21e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 50 deletions.
38 changes: 17 additions & 21 deletions IDE/dist/CPUMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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*/];
}
});
});
Expand Down
52 changes: 32 additions & 20 deletions IDE/dist/ProcessManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 0 additions & 3 deletions IDE/src/CPUMonitor.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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){
Expand Down
15 changes: 9 additions & 6 deletions IDE/src/ProcessManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 23eb21e

Please sign in to comment.