How to get CPU usage?
How to get CPU usage?
I'm wondering how to get the CPU usage of a particular function (if possible) or the code as a whole. In the maxmod demo (http://maxmod.org/demo/maxmod_ds.zip), they have a nice CPU usage graph. But I can't find the source for that particular maxmod demo.
Cheers
Cheers
Re: How to get CPU usage?
Hello nfirvine!
Here are some codes,
What is happening is start_test() starts up 2 timers...
1 timer "TIMER1" is in CASCADE or "count-up" mode, in this mode it will increment its counter when the timer below it overflows (ds has 4 hardware TIMERS).
TIMER0 is in full speed normal mode, so it will increment its counter every clock cycle (33mhz clock).
Together, these timers form a 32-bit cycle counter. end_test() stops the timers and returns the total number of clock cycles that have passed between the calls.
The code measures the time it takes for 5 frames to pass, there's 560190 cycles per frame, so the result should be 500%.
(btw you probably don't want to see the maxmod demo's profiler because it is a bit wonky...)
Here are some codes,
Code: Select all
/*---------------------------------------------------------------------------------
Basic template code for starting a DS app
---------------------------------------------------------------------------------*/
#include <nds.h>
#include <stdio.h>
void start_test() {
TIMER0_CR = 0;
TIMER1_CR = 0;
TIMER0_DATA = 0;
TIMER1_DATA = 0;
TIMER1_CR = TIMER_CASCADE | TIMER_ENABLE;
TIMER0_CR = TIMER_ENABLE;
}
u32 end_test() {
TIMER0_CR = 0;
TIMER1_CR = 0;
return TIMER0_DATA | (TIMER1_DATA<<16);
}
//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------
consoleDemoInit();
iprintf( "Profiling function......\n" );
swiWaitForVBlank();
start_test();
swiWaitForVBlank();
swiWaitForVBlank();
swiWaitForVBlank();
swiWaitForVBlank();
swiWaitForVBlank();
u32 result = end_test();
// about 560190 clock cycles (timer ticks) per frame, get percentage
iprintf( "5 vblanks = %i%% frame usage\n", (result * 100 + (560190/2)) / 560190 );
while(1) {
swiWaitForVBlank();
}
}
1 timer "TIMER1" is in CASCADE or "count-up" mode, in this mode it will increment its counter when the timer below it overflows (ds has 4 hardware TIMERS).
TIMER0 is in full speed normal mode, so it will increment its counter every clock cycle (33mhz clock).
Together, these timers form a 32-bit cycle counter. end_test() stops the timers and returns the total number of clock cycles that have passed between the calls.
The code measures the time it takes for 5 frames to pass, there's 560190 cycles per frame, so the result should be 500%.
(btw you probably don't want to see the maxmod demo's profiler because it is a bit wonky...)
Re: How to get CPU usage?
Alright; a bit over my head, but thanks, I'll give it a go.
-
- Posts: 29
- Joined: Sun Mar 29, 2009 9:23 pm
Re: How to get CPU usage?
Nice.
I created separate source and header files for the functions.
Feel free to use them (credit goes to eKid):
http://ryouarashi.ry.funpic.de/cpu_usage.c
http://ryouarashi.ry.funpic.de/cpu_usage.h
RyouArashi
I created separate source and header files for the functions.
Feel free to use them (credit goes to eKid):
http://ryouarashi.ry.funpic.de/cpu_usage.c
http://ryouarashi.ry.funpic.de/cpu_usage.h
RyouArashi
Re: How to get CPU usage?
Thanks for the files
For better C++ integration I changed the header to use the keywords : extern "C"
I added arguments to allow the user to choose which timers to use as in my software timer0 was already used for timing events.
cpu_usage.h
And for faster compilation, I transfered the nds includes to the implementation file (and include only the nds timer specific file):
cpu_usage.c
Relay programming time! Who's next ?
For better C++ integration I changed the header to use the keywords : extern "C"
I added arguments to allow the user to choose which timers to use as in my software timer0 was already used for timing events.
cpu_usage.h
Code: Select all
/*
CPU Usage - http://forums.devkitpro.org/viewtopic.php?f=6&t=415
original Source by eKid
adapted by Ryouarashi and Weirdfox
---
Usage:
// Main Loop
while(1)
{
int CPUPercent;
// The arguments represent the two timers to use, make sure they are not already
// used by your software.
CPU_StartTest( 0, 1 );
// do you stuff...
CPU_Percent = CPU_EndTest();
swiWaitForVBL();
}
*/
#ifndef CPU_USAGE_H_INCLUDED
#define CPU_USAGE_H_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif
void CPU_StartTest(int timer1, int timer2);
int CPU_EndTest();
#ifdef __cplusplus
}
#endif
#endif
cpu_usage.c
Code: Select all
/*
CPU Usage - http://forums.devkitpro.org/viewtopic.php?f=6&t=415
original Source by eKid
adapted by Ryouarashi and Weirdfox
*/
#include "tools/cpu_usage.h"
#include "nds/timers.h"
int localTimer1 = 0;
int localTimer2 = 1;
void CPU_StartTest(int timer1, int timer2) {
localTimer1 = timer1;
localTimer2 = timer2;
TIMER_CR(timer1) = 0;
TIMER_CR(timer2) = 0;
TIMER_DATA(timer1) = 0;
TIMER_DATA(timer2) = 0;
TIMER_CR(timer2) = TIMER_CASCADE | TIMER_ENABLE;
TIMER_CR(timer1) = TIMER_ENABLE;
}
int CPU_EndTest() {
TIMER_CR(localTimer1) = 0;
TIMER_CR(localTimer2) = 0;
//return TIMER0_DATA | (localTimer1_DATA<<16);
return ((TIMER_DATA(localTimer1) | (TIMER_DATA(localTimer2)<<16)) * 100 + (560190/2)) / 560190;
}
try, crash, debug and learn
Re: How to get CPU usage?
Hey Nfirvine,
Myself Macki. I have read your problem regarding cpu usage. you have
to do some simple process just press CTRL+ATL+DELT in that window
you can see the all info about cpu ok or if you want mem check just
goto my computer icon press right click goto properties.
Thanks...
Myself Macki. I have read your problem regarding cpu usage. you have
to do some simple process just press CTRL+ATL+DELT in that window
you can see the all info about cpu ok or if you want mem check just
goto my computer icon press right click goto properties.
Thanks...
Re: How to get CPU usage?
1st post. Check.mackinroj wrote:Hey Nfirvine,
Myself Macki. I have read your problem regarding cpu usage. you have
to do some simple process just press CTRL+ATL+DELT in that window
you can see the all info about cpu ok or if you want mem check just
goto my computer icon press right click goto properties.
Thanks...
Bumping a thread 1/2 a year old. Check.
Replying with something completely unrelated to the subject. Check.
Result? You must be new here.
Please make sure you know what you are replying to when posting in the forums, as I can see you missed the part that this is the libnds forums, which is about programming for the Nintendo DS, which has nothing to do with Windows. Also, please read the follow-ups, as they will also tell you whether or not the answer has already been received.
Re: How to get CPU usage?
THanks for taking time to help
Who is online
Users browsing this forum: No registered users and 0 guests