Get CPU usage

Windows specific questions.
Post Reply
nake
Posts: 6
Joined: Jun 10, 2009 12:19

Get CPU usage

Post by nake »

Hi! This is my first post here ^^
The thing is that I would like to get the amount of CPU/RAM that my program uses. I use Windows XP, so I dont really know if I should use some kind of API or so... And if I do have to, I dont know how to implement it on my code...
If you could post an example :)

Thanks
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Post by 1000101 »

You can get the available system memory in an inspecific way with Fre but there is no built-in way to retrieve the CPU specifics. For that you will need to use specific APIs.

For win32 that happens to be GetSystemInfo and the MSDN page for it is here: http://msdn.microsoft.com/en-us/library ... S.85).aspx
nake
Posts: 6
Joined: Jun 10, 2009 12:19

Post by nake »

Ok, but how do I code that?
The examples are for C, C++ and VB.NET
Its the first time I try to use an API...

Thanks anyway :)
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Post by 1000101 »

The code for freebasic is very similar. There are already translated headers (.bi) for the windows api, all you need to do is include them (#include "windows.bi") and then just code the example like it says (except fb syntax, not C++).

Quick example:

Code: Select all

#include once "windows.bi"

Dim As SYSTEM_INFO  sysinfo

GetSystemInfo( @sysinfo )

Print "Processor Count: "; sysinfo.dwNumberOfProcessors


NOTE: This is a windows specific example and will not work in dos/linux/mac/ds/the moon/etc.
nake
Posts: 6
Joined: Jun 10, 2009 12:19

Post by nake »

^^ Thanks, it worked!
I wanted to know the CPU usage but I can figure out how to do that.
Hard
Posts: 135
Joined: Aug 29, 2008 21:13

Post by Hard »

this is the wrong section here btw

anyway this is very difficult. you have to use pdh (which isnt included in freebasic, so you have to wrap it manually).

as far as i know only two persons did this so far. me and someone else here in the forums working with hdd usage.
nake
Posts: 6
Joined: Jun 10, 2009 12:19

Post by nake »

Hard wrote:this is the wrong section here btw
Sorry, I thought CPU usage would be Hardware, but maybe its more a Windows related topic...mmm
Hard wrote:as far as i know only two persons did this so far. me and someone else here in the forums working with hdd usage.
hdd? Hard Disk? I wanted CPU usage.

I'm programing a 2D space simulator (with gravity and such things) and as I'm working with ~300-400 'planets' at the same time it requires a lot of calculations, and that means a lot of CPU usage. So I would like to show how much CPU is being used on my app.
Sorry if I confused you...
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Post by Zippy »

OP wants CPU/RAM usage for one process, his.

This is one approach to CPU usage, it probably works. I'm trying to "peg" the CPU with closed loops, for testing - fb is fast enough to make this diff on a fast processor(s).

Code: Select all

'get cpu usage for current process
'  Windows only, WinNT or newer
'
#include once "windows.bi"
'
dim as integer pid,res
dim as double bt,et,ntimeK,ntimeU,otimeK,otimeU
dim as HANDLE hproc
dim as FILETIME cTime,eTime,kTime,uTime
'
pid=GetCurrentProcessId()
hproc=OpenProcess(PROCESS_QUERY_INFORMATION,0,pid)
'
'get usage data near beginning of code
res=_
    GetProcessTimes(_
        hProc,_
        @cTime,_
        @eTime,_
        @kTime,_
        @uTime)
'
otimeK=kTime.dwLowDateTime/10000000
otimeU=uTime.dwLowDateTime/10000000
'
'start timing
bt=timer
'do something..
for c as integer=1 to 1000000
    var t = sin(rnd)
    var z = sin(rnd)
next
for c as integer=1 to 1000000
    var t = sin(rnd)
    var z = sin(rnd)
next
for c as integer=1 to 1000000
    var t = sin(rnd)
    var z = sin(rnd)
next
'stop timer
et=timer
'
'get new usage data
res=_
    GetProcessTimes(_
        hProc,_
        @cTime,_
        @eTime,_
        @kTime,_
        @uTime)
'
ntimeK=kTime.dwLowDateTime/10000000
ntimeU=uTime.dwLowDateTime/10000000
'
print using "ProcessID   : ###### ";pid
print using "Kernel Time :   #.###### seconds";ntimeK
print using "User Time   :   #.###### seconds";ntimeU
print using "CPU%        : ###.######";((ntimeK-otimeK)+(ntimeU-otimeU))/(et-bt)*100
'
CloseHandle(hproc)
sleep
I say it "probably" works because I'm probably uncertain..
Hard
Posts: 135
Joined: Aug 29, 2008 21:13

Post by Hard »

nake wrote:hdd? Hard Disk? I wanted CPU usage.
both is available via pdh.

checkout:
control panel - administrative tools - performance

there you can find anything you can do via pdh. anyway this is not really the best way for you, since you only want (unaccurate) cpu usage.
nake
Posts: 6
Joined: Jun 10, 2009 12:19

Post by nake »

I tried to implement it to my code, I had to divide the CPU% by 2 because I have 2 CPU's. It seems to work, more less...
I think its just too much work only for such a tiny thing, but thanks anyway :)
MOD
Posts: 555
Joined: Jun 11, 2009 20:15

Post by MOD »

Hi,
sometimes ago, i posted a project on german forum.
it's a processlogger and it can also get the CPU usage of every process.
therefore i created a dll with the same functions to build in other projects.

(...)

there are also readme's and examples in the package, but just in german.
i've no time to translate it now, so maybe google will translate it for you
i suppose i'll translate it ...someday...

and if someone speaks german, here the project-thread:
http://forum.qbasic.at/viewtopic.php?t=6274

hope it will help you
Last edited by MOD on Apr 03, 2010 12:04, edited 1 time in total.
nake
Posts: 6
Joined: Jun 10, 2009 12:19

Post by nake »

^^ Thanks. I dont have time to try it on my project right now :s
However your code seems to work and your library is easy to use.
sun
Posts: 6
Joined: Mar 07, 2009 23:33

Post by sun »

Great work Mod!
Klasse ;)

Will use it in my crashguarder!

SUN
Post Reply