For windows, using the command line (wmic for example), itself takes a chunk of cpu every second.
The winapi method uses very little.
Sorry I don't have Linux, or know the Linux method, but I have a notion it might be easier than the Windows method.
But, if it is of any use to you, here is a winapi method.
uses
windows,jwawinbase;
Function getsystemtime_CPU(var totalTime:qword;var idleTime:qword): boolean;
var
ftSysIdle, ftSysKernel, ftSysUser:filetime;
sysKernel, sysUser, sysIdle:ularge_integer;
begin
If ((GetSystemTimes(@ftSysIdle, @ftSysKernel, @ftSysUser))=false) Then exit (false);
sysKernel.HighPart := ftSysKernel.dwHighDateTime;
sysKernel.LowPart := ftSysKernel.dwLowDateTime;
sysUser.HighPart := ftSysUser.dwHighDateTime;
sysUser.LowPart := ftSysUser.dwLowDateTime;
sysIdle.HighPart := ftSysIdle.dwHighDateTime;
sysIdle.LowPart := ftSysIdle.dwLowDateTime;
totalTime := sysKernel.QuadPart + sysUser.QuadPart;
idleTime := sysIdle.QuadPart;
exit(True);
End;
procedure cpu;
var
totalprev:qword=0;
totalcurr:qword=0;
idleprev:qword=0;
idlecurr:qword=0;
total,idle:qword;
usage:integer;
s,use:ansistring;
begin
While True do
begin
s:='';
total:=0;
idle:=0;
Sleep(1000);
If ((getSystemtime_CPU(totalCurr, idleCurr))=true) Then
begin
total := totalCurr - totalPrev;
if (total>0) then
begin
idle := idleCurr - idlePrev;
usage:=trunc(100 * (total - idle) / (total));
Setlength(S,usage);
FillChar(S[1],usage,'*');
str(usage :3,use);
writeln(' ',use,' %',' ',s);
end;
totalPrev := totalCurr;
idlePrev := idleCurr;
end;
end;
end;
begin
cpu;
end.
Just click off the console when you are done.