How to get CPU usage?

Post Reply
nfirvine
Posts: 2
Joined: Fri Apr 10, 2009 12:38 am

How to get CPU usage?

Post by nfirvine » Fri Apr 10, 2009 12:43 am

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

eKid
Posts: 65
Joined: Sat Dec 06, 2008 6:07 pm
Contact:

Re: How to get CPU usage?

Post by eKid » Wed Apr 15, 2009 2:21 pm

Hello nfirvine!

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();
    }

}
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...)

nfirvine
Posts: 2
Joined: Fri Apr 10, 2009 12:38 am

Re: How to get CPU usage?

Post by nfirvine » Sat Apr 25, 2009 8:29 am

Alright; a bit over my head, but thanks, I'll give it a go.

RyouArashi
Posts: 29
Joined: Sun Mar 29, 2009 9:23 pm

Re: How to get CPU usage?

Post by RyouArashi » Sat May 02, 2009 11:11 am

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

weirdfox
Posts: 29
Joined: Thu Feb 19, 2009 8:45 am
Location: Montreal, Canada
Contact:

Re: How to get CPU usage?

Post by weirdfox » Sun May 03, 2009 6:02 pm

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

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
And for faster compilation, I transfered the nds includes to the implementation file (and include only the nds timer specific file):

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;
}
Relay programming time! Who's next ?
try, crash, debug and learn :)

mackinroj
Posts: 1
Joined: Thu Oct 22, 2009 2:11 pm

Re: How to get CPU usage?

Post by mackinroj » Fri Oct 23, 2009 1:26 pm

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...

Discostew
Posts: 103
Joined: Sun Mar 08, 2009 7:24 pm

Re: How to get CPU usage?

Post by Discostew » Fri Oct 23, 2009 6:25 pm

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...
1st post. Check.
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.

Son
Posts: 1
Joined: Fri Jul 23, 2010 8:21 am

Re: How to get CPU usage?

Post by Son » Fri Jul 23, 2010 8:24 am

THanks for taking time to help

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest