Get CPU usage
Get CPU usage
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
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
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
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
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:
NOTE: This is a windows specific example and will not work in dos/linux/mac/ds/the moon/etc.
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.
Sorry, I thought CPU usage would be Hardware, but maybe its more a Windows related topic...mmmHard wrote:this is the wrong section here btw
hdd? Hard Disk? I wanted CPU usage.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.
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...
Check the following post.
http://www.freebasic.net/forum/viewtopi ... =cpu+usage
http://www.freebasic.net/forum/viewtopi ... =cpu+usage
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).
I say it "probably" works because I'm probably uncertain..
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
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
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.