Linux CPU utilization: Difference between revisions

no edit summary
No edit summary
 
(One intermediate revision by one other user not shown)
Line 782:
{{out}}
<pre>CPU utilization = 7.9 %</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
Program CPU_Utilization;
 
{$mode ObjFPC}
{$H+}
 
uses
sysutils, strutils;
 
const
C_INFNAME = '/proc/stat';
 
var
filestr: ansistring;
tfIn: TextFile;
a: TStringArray;
i, total, idle: uint32;
PrevTotal: uint32 = 0;
PrevIdle: uint32 = 0;
 
begin
AssignFile(tfIn, C_INFNAME);
try
while True do
begin
Reset(tfIn);
ReadLn(tfIn, filestr); // ReadLn reads a line from the file
a := filestr.Split([' ']);
total := 0;
for i := 2 to High(a) do
Inc(total, StrToInt(a[i]));
idle := StrToInt(a[5]); // there is an empty field
Writeln((1.0 - (idle - PrevIdle) / (total - PrevTotal)) * 100:4:1, '%');
PrevIdle := idle;
PrevTotal := total;
Sleep(500);
end;
finally
CloseFile(tfIn); // Close the file when done
end;
end.
 
</syntaxhighlight>
{{out}}
<pre>
2.8%
2.0%
0.8%
1.0%
</pre>
 
=={{header|Perl}}==
Line 1,154 ⟶ 1,207:
 
The figure at time 0 is the cumulative CPU usage since boot time.
<syntaxhighlight lang="ecmascriptwren">/* linux_cpu_utilizationLinux_CPU_utilization.wren */
 
import "./fmt" for Fmt
Line 1,183 ⟶ 1,236:
<br>
We now embed this in the following C program, compile and run it.
<syntaxhighlight lang="c">/* gcc linux_cpu_utilizationLinux_CPU_utilization.c -o linux_cpu_utilizationLinux_CPU_utilization -lwren -lm */
 
#include <stdio.h>
Line 1,276 ⟶ 1,329:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "linux_cpu_utilizationLinux_CPU_utilization.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
44

edits