Forum > Linux

Does anybody have CPU usage example code for Raspberry AND win32


Hey i am looking for example code / fucntion or procedure. To read the CPU usage. I do have running versions from this forum, but they only see to work for Win32.

I am cross compiling from win32 to ARM. So they need to work both.

Further i do not like txt file reading or a TProcess

Anybody got help?

Greets, Wouter van Wegen

PS: It is for a Raspberry model 3

It's less relevant that your compiling for ARM, but that your compiling for a different operating system, namely Linux (assuming you don't use Windows on ARM on your Pi), so you should search for how to retrieve the CPU usage on Linux (Hint: it's often called “system load” or similar, though that will be the total load across all cores).

And you'll simply have to ifdef your code and provide a suitable cross platform abstraction. Or maybe you'll find a cross platform abstraction, who knows. ;)


Please don't open duplicate posts. Somebody's already suggested a library in response to your earlier,59344.msg442391.html#msg442391

--- Quote from: pascalbythree on May 24, 2022, 08:53:25 pm ---I am cross compiling from win32 to ARM. So they need to work both.

--- End quote ---

Why? if you're /cross/ compiling then only the target matters so

--- Quote ---PS: It is for a Raspberry model 3

--- End quote ---

...that's what matters, the RPi /does/ have capabilities for getting at voltages etc. but if you start messing around with those all hope of something portable goes out of the window.

--- Quote ---Further i do not like txt file reading

--- End quote ---

Tough, since utilities like top and ps refer to items in the /proc filesystem rather than having a binary API. However that is NOT a real directory and you are NOT reading real text files.



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.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---useswindows,jwawinbase; Function getsystemtime_CPU(var totalTime:qword;var idleTime:qword): boolean;varftSysIdle, 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;vartotalprev:qword=0;totalcurr:qword=0;idleprev:qword=0;idlecurr:qword=0;total,idle:qword;usage:integer;s,use:ansistring;beginWhile True dobegins:='';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;   begincpu;end. 
Just click off the console when you are done.


[0] Message Index

Go to full version