Skip to content

Commit

Permalink
IDE: use bela-cpu to detect CPU usage. This drastically simplifies th…
Browse files Browse the repository at this point in the history
…e frontend
  • Loading branch information
giuliomoro committed Nov 30, 2024
1 parent 63bafc1 commit cf07646
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 191 deletions.
70 changes: 26 additions & 44 deletions IDE/dist/CPUMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ var __values = (this && this.__values) || function (o) {
Object.defineProperty(exports, "__esModule", { value: true });
var child_process = require("child_process");
var pidtree = require("pidtree");
var bela_cpu = require("./bela-cpu");
// 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
var name;
var timeout;
var found_pid;
var root_pid;
var main_pid;
Expand All @@ -66,58 +66,49 @@ function start(pid, project, cb) {
stopped = false;
found_pid = false;
find_pid_count = 0;
timeout = setTimeout(function () { return timeout_func(); }, 1000);
setTimeout(loop, 1000);
}
exports.start = start;
function stop() {
if (timeout)
clearTimeout(timeout);
bela_cpu.stop();
stopped = true;
}
exports.stop = 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
function timeout_func() {
function loop() {
return __awaiter(this, void 0, void 0, function () {
var cpu, e_1;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
cpu = '0';
_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];
if (!!stopped) return [3 /*break*/, 7];
if (!!found_pid) return [3 /*break*/, 3];
if (!(find_pid_count++ < 5)) return [3 /*break*/, 2];
return [4 /*yield*/, find_pid()];
case 2:
case 1:
_a.sent();
_a.label = 2;
case 2: return [3 /*break*/, 5];
case 3: return [4 /*yield*/, bela_cpu.getCPU(main_pid, callback)];
case 4:
_a.sent();
_a.label = 3;
case 3: return [3 /*break*/, 6];
case 4: return [4 /*yield*/, getCPU()];
case 5:
cpu = _a.sent();
_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*/, 9];
case 8:
if (!stopped) {
callback(cpu);
timeout = setTimeout(timeout_func, 1000);
}
return [7 /*endfinally*/];
case 9: return [2 /*return*/];
_a.label = 5;
case 5: return [4 /*yield*/, (function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve) { setTimeout(resolve, 1000); })];
}); }); })()];
case 6:
_a.sent();
return [3 /*break*/, 0];
case 7: return [2 /*return*/];
}
});
});
}
function find_pid() {
return __awaiter(this, void 0, void 0, function () {
var pids, pids_1, pids_1_1, pid, test_name, e_2_1, e_2, _a;
var pids, pids_1, pids_1_1, pid, test_name, e_1_1, e_1, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, pidtree(root_pid, { root: true })];
Expand All @@ -144,14 +135,14 @@ function find_pid() {
return [3 /*break*/, 3];
case 6: return [3 /*break*/, 9];
case 7:
e_2_1 = _b.sent();
e_2 = { error: e_2_1 };
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 9];
case 8:
try {
if (pids_1_1 && !pids_1_1.done && (_a = pids_1.return)) _a.call(pids_1);
}
finally { if (e_2) throw e_2.error; }
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 9: return [2 /*return*/];
}
Expand All @@ -168,12 +159,3 @@ function name_from_pid(pid) {
});
});
}
function getCPU() {
return new Promise(function (resolve, reject) {
child_process.exec('ps -p ' + main_pid + ' -o %cpu --no-headers', function (err, stdout) {
if (err)
reject(err);
resolve(stdout);
});
});
}
17 changes: 3 additions & 14 deletions IDE/dist/ProcessManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,20 +315,9 @@ processes.run.on('start', function (pid, project) { return __awaiter(_this, void
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 __generator(this, function (_a) {
socket_manager.broadcast('cpu-usage', cpu);
return [2 /*return*/];
});
}); });
}
Expand Down
53 changes: 3 additions & 50 deletions IDE/frontend-dev/src/Views/ToolbarView.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ var View = require('./View');
var popup = require('../popup');
var json = require('../site-text.json');

// ohhhhh i am a comment

var modeswitches = 0;
var NORMAL_MSW = 1;
var nameIndex, CPUIndex, rootName, IRQName;
var mswGraceEnd = 0;
// avoid spurious MSWs when stopping or restarting
const mswGraceMsStop = 3000;
Expand Down Expand Up @@ -217,55 +213,12 @@ class ToolbarView extends View {
}

_CPU(data){
var bela = 0, rootCPU = 1;

if (data.bela != 0 && data.bela !== undefined){

// extract the data from the output
var lines = data.bela.split('\n');
var taskData = [];
for (var j=0; j<lines.length; j++){
taskData.push([]);
lines[j] = lines[j].split(' ');
for (var k=0; k<lines[j].length; k++){
if (lines[j][k]){
taskData[j].push(lines[j][k]);
}
}
}

var output = [];
for (var j=0; j<taskData.length; j++){
if (taskData[j].length){
var proc = {
'name' : taskData[j][nameIndex],
'cpu' : taskData[j][CPUIndex],
'msw' : taskData[j][2],
'csw' : taskData[j][3]
};
if (proc.name === rootName) rootCPU = proc.cpu*0.01;
if (proc.name === 'bela-audio') this.mode_switches(proc.msw-NORMAL_MSW);
// ignore uninteresting data
if (proc && proc.name && proc.name !== rootName && proc.name !== 'NAME' && proc.name !== IRQName){
output.push(proc);
}
}
}

for (var j=0; j<output.length; j++){
if (output[j].cpu){
bela += parseFloat(output[j].cpu);
}
}

if(data.belaLinux)
bela += data.belaLinux * rootCPU;

}
let bela = data.cpu;
this.mode_switches(data.msw);

$('[data-toolbar-bela-cpu]').html('CPU: '+( bela ? bela.toFixed(1)+'%' : '--'));

if (bela && bela > 80) {
if (bela > 80) {
$('[data-toolbar-bela-cpu]').css('color', 'red');
} else {
$('[data-toolbar-bela-cpu]').css('color', 'black');
Expand Down
52 changes: 3 additions & 49 deletions IDE/public/js/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 10 additions & 30 deletions IDE/src/CPUMonitor.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,48 @@
import * as child_process from 'child_process';
import * as pidtree from 'pidtree';
import * as bela_cpu from './bela-cpu';

// 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

let name: string;
let timeout: NodeJS.Timer;
let found_pid: boolean;
let root_pid: number;
let main_pid: number;
let callback: (cpu: any) => void;
let callbackError: () => void;
let stopped: boolean;
let find_pid_count: number;

export function start(pid: number, project: string, cb: (cpu: any)=>void, cbError: () => void){
export function start(pid: number, project: string, cb: (cpu: any)=>void){
root_pid = pid;
// the process name gets cut off at 15 chars
name = project.substring(0, 15) || project[0].substring(0, 15);
callback = cb;
callbackError = cbError;
stopped = false;
found_pid = false;
find_pid_count = 0;
timeout = setTimeout( () => timeout_func(), 1000);
setTimeout(loop, 1000);
}

export function stop(){
if (timeout) clearTimeout(timeout);
bela_cpu.stop();
stopped = true;
}

// 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(){
let cpu: any = '0';
try{
async function loop(){
while(!stopped) {
if (!found_pid){
if (find_pid_count++ < 3){
if (find_pid_count++ < 5){
await find_pid();
}
} else {
cpu = await getCPU();
}
}
catch(e){
callbackError();
found_pid = false;
}
finally{
if(!stopped){
callback(cpu);
timeout = setTimeout(timeout_func, 1000);
await bela_cpu.getCPU(main_pid, callback);
found_pid = false;
}
await (async () => { return new Promise((resolve) => { setTimeout(resolve, 1000) })})();
}
}

Expand All @@ -79,12 +68,3 @@ function name_from_pid(pid: number){
});
});
}

function getCPU(){
return new Promise((resolve, reject) => {
child_process.exec('ps -p '+main_pid+' -o %cpu --no-headers', (err, stdout) => {
if (err) reject(err);
resolve(stdout);
});
});
}
5 changes: 1 addition & 4 deletions IDE/src/ProcessManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,7 @@ processes.run.on('start', async (pid: number, project: string) => {
socket_manager.broadcast('status', get_status());
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
});
socket_manager.broadcast('cpu-usage', cpu);
});
}
});
Expand Down

0 comments on commit cf07646

Please sign in to comment.