jsos
college code for operating system fundamentals in js
git clone https://9o.is/git/jsos.git
commit 3b0e9a3223f104d40fd9022ee0c8462af9d5df6a parent a52ae1905239d3cb384cbd0b79c1be5cda60b28e Author: Jul <jul@9o.is> Date: Wed, 7 Nov 2012 13:36:04 -0500 Implemented cpu stats to interface. (cpu utilization and throughput). Diffstat:
| M | index.html | | | 12 | ++++++++++-- |
| M | scripts/os/kernel.js | | | 1 | + |
| M | scripts/os/scheduler.js | | | 39 | ++++++++++++++++++++++++++++++++++----- |
3 files changed, 45 insertions(+), 7 deletions(-)
diff --git a/index.html b/index.html @@ -110,12 +110,20 @@ </div> <div class="row"> - <div class="twelvecol last"> + <div class="tencol"> <canvas id="display" tabindex="0" - width="1138" + width="950" height="450"></canvas> </div> + <div class="twocol last"> + <table> + <tr><th>CPU Utilization</th></tr> + <tr><td id="cpu_utilization">0%</td></tr> + <tr><th>CPU Throughput</th></tr> + <tr><td id="cpu_throughput">0%</td></tr> + </table> + </div> </div> <div class="row"> diff --git a/scripts/os/kernel.js b/scripts/os/kernel.js @@ -178,6 +178,7 @@ function krnSystemCall(operator) { " terminated successfully."); _KernelCPUScheduler. running.setState(PCB_STATE_TERMINATED); + _KernelCPUScheduler.completedProcesses++; _Console.advanceLine(); _OsShell.putPrompt(); break; diff --git a/scripts/os/scheduler.js b/scripts/os/scheduler.js @@ -15,12 +15,14 @@ function scheduler() { */ this.running = undefined; + /* The amount of clock pulses when the CPU was executing. */ + this.busyTime = 0; + + /* The amount of completed processes. */ + this.completedProcesses = 0; + this.run = function() { - // if process is not running, and ready queue is empty - // don't do anything - if(this.processTerminated() && _KernelReadyQueue.isEmpty()) { - return; - } + this.busyTime++; // if ready queue is not empty, dequeue if(this.processTerminated()) { @@ -68,6 +70,7 @@ function scheduler() { * Whether the scheduler is ready to run. */ this.ready = function() { + this.output(); return (!this.processTerminated() || _KernelReadyQueue.getSize() > 0); } @@ -81,4 +84,30 @@ function scheduler() { } } + /* + * CPU Utilization + */ + this.utilization = function() { + var n = this.busyTime / _OSclock; + return Math.round(n*100); + } + + /* + * CPU Throughput + */ + this.throughput = function() { + var n = this.completedProcesses / this.busyTime; + return Math.round(n*100); + } + + //TODO: would prefer to measure portion of performance + // instead of the whole OSclock since the OS is mostly + // always idle. + this.output = function() { + document.getElementById("cpu_utilization").innerHTML = + this.utilization()+"%"; + + document.getElementById("cpu_throughput").innerHTML = + this.throughput()+"%"; + } }