| View previous topic :: View next topic |
| Author |
Message |
Alree
Joined: 26 Feb 2008 Posts: 33
|
Posted: Sat Mar 08, 2008 9:30 am Post subject: Calculate CPU Charge ? |
|
|
Hi,
I just want to know if anyone has already make a function in C or in ASM to show the PSP's Cpu charge, like the little plugin HUD ?
or any lib, include in sdk ? |
|
| Back to top |
|
 |
jimparis
Joined: 10 Jun 2005 Posts: 1179 Location: Boston
|
Posted: Sat Mar 08, 2008 6:07 pm Post subject: |
|
|
Are you talking about CPU speed or battery charge?
Either way, check the pspsdk documentation, there are simple functions to get both. |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
|
| Back to top |
|
 |
Cpasjuste
Joined: 29 May 2005 Posts: 214
|
Posted: Sat Mar 08, 2008 10:00 pm Post subject: |
|
|
| I do not think he want to know the cpu speed, but the cpu usage. |
|
| Back to top |
|
 |
Alree
Joined: 26 Feb 2008 Posts: 33
|
Posted: Sat Mar 08, 2008 11:51 pm Post subject: |
|
|
yes it's a mistake I want to know if anyone has already make a function to get "cpu load"
not frequency, but % of cpu load.
I know we need to enumerate all thread to calculate this value, but I don"t want to waste time if a lib or function already exist. |
|
| Back to top |
|
 |
TyRaNiD
Joined: 18 Jan 2004 Posts: 918
|
Posted: Sun Mar 09, 2008 1:33 am Post subject: |
|
|
| It is certainly doable but totally meaningless on a general basis which is what I assume you want it for, some leet VSH plugin crap... It might have some sort of benefit for actual dev I guess. And it doesn't require thread enumeration at all though you can probably do it with that :) |
|
| Back to top |
|
 |
Alree
Joined: 26 Feb 2008 Posts: 33
|
Posted: Sun Mar 09, 2008 2:14 am Post subject: |
|
|
| it's for a benchmark, not a VSH plugin. |
|
| Back to top |
|
 |
TyRaNiD
Joined: 18 Jan 2004 Posts: 918
|
Posted: Sun Mar 09, 2008 2:46 am Post subject: |
|
|
Okay then :P This would have to be done from kernel mode, you could do it in user mode but you would have to enable the global profiler before it would work. But even with the thread approach you would have to do it kernel mode anyway. | Code: | #define PROFILER_REG_BASE 0xBC400000
void print_cpuusage(void)
{
PspDebugProfilerRegs *regs = (PspDebugProfilerRegs*) PROFILER_REG_BASE;
unsigned int *p = (unsigned int*) regs;
int i;
for(i = 0; i < (sizeof(PspDebugProfilerRegs) / sizeof(unsigned int)); i++)
{
p[i] = 0;
}
regs->enable = 1;
while(1)
{
unsigned int tempck;
unsigned int tempsl;
sceKernelDelayThread(1000000);
tempck = regs->cpuck;
tempsl = regs->sleep;
regs->cpuck = 0;
regs->sleep = 0;
printf("CPU Usage: %d%%\n", (int) (100.0f * ((float) tempck - (float) tempsl) / (float) tempck));
}
}
| So that will print the CPU usage every second, the sampling time needs to be short enough to ensure the counters don't wrap. All that calculation is doing is measuring how many active cycles occur vs the number of cycles where the CPU is halted (i.e. in the idle thread). Which is kinda what you want I would expect. However you probably should take that value more as a relative measure than a cast iron percentage.
edit:
Hmm seems I have screwed something up, looks like you need the global profilers enabled correctly as well for some reason. Which sucks :) |
|
| Back to top |
|
 |
Alree
Joined: 26 Feb 2008 Posts: 33
|
Posted: Sun Mar 09, 2008 3:31 am Post subject: |
|
|
thanks TyRaNiD, I'll try to couple this with another method to reduce error margin.
what do you mean by "enabled correctly" there is a wrong way to enable it ? |
|
| Back to top |
|
 |
TyRaNiD
Joined: 18 Jan 2004 Posts: 918
|
Posted: Sun Mar 09, 2008 5:46 am Post subject: |
|
|
| Urm you have to enable the global profiler switch to get it to look nice, in psplink you can do 'profmode g' in your own code you are on your own ;) |
|
| Back to top |
|
 |
Stewie87
Joined: 03 Apr 2008 Posts: 39
|
Posted: Sun Jun 15, 2008 10:56 am Post subject: |
|
|
| TyRaNiD wrote: | Okay then :P This would have to be done from kernel mode, you could do it in user mode but you would have to enable the global profiler before it would work. But even with the thread approach you would have to do it kernel mode anyway. | Code: | #define PROFILER_REG_BASE 0xBC400000
void print_cpuusage(void)
{
PspDebugProfilerRegs *regs = (PspDebugProfilerRegs*) PROFILER_REG_BASE;
unsigned int *p = (unsigned int*) regs;
int i;
for(i = 0; i < (sizeof(PspDebugProfilerRegs) / sizeof(unsigned int)); i++)
{
p[i] = 0;
}
regs->enable = 1;
while(1)
{
unsigned int tempck;
unsigned int tempsl;
sceKernelDelayThread(1000000);
tempck = regs->cpuck;
tempsl = regs->sleep;
regs->cpuck = 0;
regs->sleep = 0;
printf("CPU Usage: %d%%\n", (int) (100.0f * ((float) tempck - (float) tempsl) / (float) tempck));
}
}
| So that will print the CPU usage every second, the sampling time needs to be short enough to ensure the counters don't wrap. All that calculation is doing is measuring how many active cycles occur vs the number of cycles where the CPU is halted (i.e. in the idle thread). Which is kinda what you want I would expect. However you probably should take that value more as a relative measure than a cast iron percentage.
edit:
Hmm seems I have screwed something up, looks like you need the global profilers enabled correctly as well for some reason. Which sucks :) |
I tried it but it gives me always 100%... why?
P.s.: I'm on CF 3.90M33 and I used that function in a VSH plugin... |
|
| Back to top |
|
 |
hlide
Joined: 10 Sep 2006 Posts: 750
|
Posted: Sun Jun 15, 2008 6:53 pm Post subject: |
|
|
well even if your plugin is running by interval with an active sleep, it may be still 100% because other threads are still running so there may be no perceptible idle. It suffices there is one plugin of lowest priority with an active events polling or an active idle thread (which means you need to remove its own CPU usage) to make CPU usage 100%.
Or simply there is a misusage of this profiler. |
|
| Back to top |
|
 |
Torch

Joined: 28 May 2008 Posts: 842
|
Posted: Thu Jun 19, 2008 5:54 pm Post subject: |
|
|
Badly coded plugins can lead to 100% usage (not to mention lowered battery life). Thats why I normally recode simple plugins I find useful for my personal use so that I know its coded right and not wasting battery.
PSPFiler has a CPU usage monitor. |
|
| Back to top |
|
 |
pspjoke
Joined: 23 Jun 2008 Posts: 14
|
Posted: Mon Jun 23, 2008 10:14 am Post subject: |
|
|
i have a use for this function in a prx, but of course it always returns 100% in a prx. so can somebody help me to get this working?
oh and its not a "badly coded plugin" in case any one is wondering. |
|
| Back to top |
|
 |
Torch

Joined: 28 May 2008 Posts: 842
|
Posted: Mon Jun 23, 2008 5:21 pm Post subject: |
|
|
| pspjoke wrote: | i have a use for this function in a prx, but of course it always returns 100% in a prx. so can somebody help me to get this working?
oh and its not a "badly coded plugin" in case any one is wondering. |
I was referring to any other plugins you might have installed and are running. |
|
| Back to top |
|
 |
pspjoke
Joined: 23 Jun 2008 Posts: 14
|
Posted: Tue Jun 24, 2008 3:16 am Post subject: |
|
|
| oh, well i know there not running the cpu to 100%, its the same with all others off. so does anyone got any advice on how to get this working for a prx? |
|
| Back to top |
|
 |
pspjoke
Joined: 23 Jun 2008 Posts: 14
|
Posted: Tue Jun 24, 2008 7:17 am Post subject: |
|
|
| so nobody can help me with this? |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Tue Jun 24, 2008 8:33 am Post subject: |
|
|
| The thread has all the info you need. We aren't here to write your program for you. You don't even bother to post code while still asking for help... that doesn't fly around here. It pretty much guarantees no one will help. That and bumping the thread after four hours. |
|
| Back to top |
|
 |
pspjoke
Joined: 23 Jun 2008 Posts: 14
|
Posted: Tue Jun 24, 2008 8:43 am Post subject: |
|
|
| J.F. wrote: | | The thread has all the info you need. We aren't here to write your program for you. You don't even bother to post code while still asking for help... that doesn't fly around here. It pretty much guarantees no one will help. That and bumping the thread after four hours. |
well the code is at the top for one..
however i did change it very slightly so it would function like i need it to.(however still returning 100% making it.. useless) here is the code.
| Code: | #define PROFILER_REG_BASE 0xBC400000
int print_cpuusage(void)
{
PspDebugProfilerRegs *regs = (PspDebugProfilerRegs*) PROFILER_REG_BASE;
unsigned int *p = (unsigned int*) regs;
int i;
for(i = 0; i < (sizeof(PspDebugProfilerRegs) / sizeof(unsigned int)); i++)
{
p[i] = 0;}
regs->enable = 1;
unsigned int tempck;
unsigned int tempsl;
sceKernelDelayThread(1000000);
tempck = regs->cpuck;
tempsl = regs->sleep;
regs->cpuck = 0;
regs->sleep = 0;
return((int) (100.0f * ((float) tempck - (float) tempsl) / (float) tempck));
}
|
2 you wouldn't be writing the program for me, just helping me to get one function of the program working.
as far as bumping the thread, well.. yea my bad. |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Tue Jun 24, 2008 11:22 am Post subject: |
|
|
Well, I did my own test based partly on the code at the top, and partly on pspdebug.c. I made a kernel mode prx for the profiler and then used it from a 3.xx user mode app.
Here's main.c for the prx:
| Code: | #include <pspsdk.h>
#include <pspkernel.h>
#include <pspdebug.h>
#define VERS 1
#define REVS 0
PSP_MODULE_INFO("Profiler", 0x1006, VERS, REVS);
PSP_MAIN_THREAD_ATTR(0);
#define PROFILER_REG_BASE 0xBC400000
#define PROFILER_REG_COUNT 20
void InitProfiler(void)
{
u32 k1;
volatile PspDebugProfilerRegs *regs = (volatile PspDebugProfilerRegs *)PROFILER_REG_BASE;
k1 = pspSdkSetK1(0);
regs->enable = 1;
pspSdkSetK1(k1);
}
void ExitProfiler(void)
{
u32 k1;
volatile PspDebugProfilerRegs *regs = (volatile PspDebugProfilerRegs *)PROFILER_REG_BASE;
k1 = pspSdkSetK1(0);
regs->enable = 0;
asm("sync\r\n");
pspSdkSetK1(k1);
}
void ClrProfileRegs(void)
{
u32 k1, i;
volatile u32 *regs = (volatile u32 *)PROFILER_REG_BASE;
k1 = pspSdkSetK1(0);
/* Don't clear the enable register */
for(i = 1; i < PROFILER_REG_COUNT; i++)
regs[i] = 0;
pspSdkSetK1(k1);
}
void GetProfileRegs(PspDebugProfilerRegs *dest)
{
u32 k1, i;
volatile u32 *regs = (volatile u32 *)PROFILER_REG_BASE;
u32 *u_regs = (u32 *)dest;
k1 = pspSdkSetK1(0);
for(i = 0; i < PROFILER_REG_COUNT; i++)
u_regs[i] = regs[i];
pspSdkSetK1(k1);
}
int module_start(SceSize args, void *argp)
{
return 0;
}
int module_stop()
{
return 0;
}
|
and here's main.c for the app:
| Code: | #include <pspsdk.h>
#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define printf pspDebugScreenPrintf
#define VERS 1
#define REVS 0
PSP_MODULE_INFO("ProfilerTest", 0, VERS, REVS);
PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);
void InitProfiler(void);
void ExitProfiler(void);
void ClrProfileRegs(void);
void GetProfileRegs(PspDebugProfilerRegs *dest);
int main(void)
{
int i;
PspDebugProfilerRegs regs;
pspDebugScreenInit();
pspDebugScreenSetBackColor(0x00000000);
pspDebugScreenSetTextColor(0x00ffffff);
pspDebugScreenClear();
sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(PSP_CTRL_MODE_DIGITAL);
printf("\n Profiler Test\n\n");
SceUID mod = pspSdkLoadStartModule("profiler.prx", PSP_MEMORY_PARTITION_KERNEL);
if (mod < 0)
{
printf(" Error 0x%08X loading/starting profiler.prx.\n", mod);
sceKernelDelayThread(3*1000*1000);
sceKernelExitGame();
}
InitProfiler();
for (i=0; i<10; i++)
{
ClrProfileRegs();
sceKernelDelayThread(100*1000);
GetProfileRegs(®s);
printf(" CPU usage: %d\n", 100 * (regs.cpuck - regs.sleep) / regs.cpuck);
}
ExitProfiler();
sceKernelDelayThread(3*1000*1000);
sceKernelExitGame();
return 0; /* never reaches here, again, just to suppress warning */
}
|
It consistently gives me 79% CPU usage with the occasional 80%. Guess sceKernelDelayThread() doesn't do much waiting if there are no other threads.
By the way - remember that these regs are only 32 bits wide. Don't make the measuring period too big or they might overflow, and then who knows what you'd get for a result. |
|
| Back to top |
|
 |
pspjoke
Joined: 23 Jun 2008 Posts: 14
|
Posted: Tue Jun 24, 2008 12:06 pm Post subject: |
|
|
thank you for your help, and again sorry that my first post on these forums was sucky..
but just so i can be that much more annoying. (jk, not trying to be annoying :( )
when i try to compile the eboot/app. i get this error in my compiler.
| Code: | $ make
psp-gcc -I. -I/usr/local/pspdev/psp/sdk/include -Os -G0 -Wall -g -D_PSP_FW_VERSI
ON=150 -L. -L/usr/local/pspdev/psp/sdk/lib main.o -lpspdebug -lpspdisplay -l
pspge -lpspctrl -lpspsdk -lc -lpspnet -lpspnet_inet -lpspnet_apctl -lpspnet_reso
lver -lpsputility -lpspuser -lpspkernel -o test.elf
main.o: In function `main':
/home/Christian/projects/1cpuloadeboot/main.c:49: undefined reference to `InitPr
ofiler'
/home/Christian/projects/1cpuloadeboot/main.c:52: undefined reference to `ClrPro
fileRegs'
/home/Christian/projects/1cpuloadeboot/main.c:54: undefined reference to `GetPro
fileRegs'
/home/Christian/projects/1cpuloadeboot/main.c:58: undefined reference to `ExitPr
ofiler'
collect2: ld returned 1 exit status
make: *** [test.elf] Error 1
|
so im guessing i need to add a something to my LIBS, however if thats right.. idk what.
for the prx its weird, starting clean it fails with this
| Code: | $ make
psp-gcc -I/usr/local/pspdev/psp/sdk/include/libc -I. -I/usr/local/pspdev/psp/sdk
/include -Os -G0 -Wall -g -D_PSP_FW_VERSION=150 -c -o main.o main.c
psp-gcc -I/usr/local/pspdev/psp/sdk/include/libc -I. -I/usr/local/pspdev/psp/sdk
/include -Os -G0 -Wall -g -D_PSP_FW_VERSION=150 -L. -L/usr/local/pspdev/psp/sdk
/lib -Wl,-q,-T/usr/local/pspdev/psp/sdk/lib/linkfile.prx -mno-crt0 -nostartfiles
main.o -lpspdebug -lpspdisplay -lpspge -lpspctrl -lpspsdk -lpsplibc -lpsputi
lity -lpspuser -lpspkernel -o cpumanager.elf
psp-fixup-imports cpumanager.elf
Error, no .lib.stub section found
make: *** [cpumanager.elf] Error 1 |
but if i do "make" again, without changing anything.
| Code: | $ make
psp-prxgen cpumanager.elf cpumanager.prx |
it compiles... thats just odd.
so how can i fix this to get it to compile?
thanks again for the help. |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Tue Jun 24, 2008 1:34 pm Post subject: |
|
|
My post above didn't include all the needed files... it was example code to look at. If you wish to actually try it yourself, you'll need more than just the two mains.
Here's all the code and files... the last compile of the main app was done with 10 sec delay just to see what would happen. You can change that and recompile if you want a shorter test time.
http://www.mediafire.com/download.php?80fn1cnqzem |
|
| Back to top |
|
 |
pspjoke
Joined: 23 Jun 2008 Posts: 14
|
Posted: Tue Jun 24, 2008 1:37 pm Post subject: |
|
|
| ok, and thanks.ill give it a shot if i can get this working ill put you in the credits of my app lol :P |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Wed Jun 25, 2008 2:53 am Post subject: |
|
|
I don't need credit... the code is just a mix of already existing code. :)
As long as it's helpful to somebody, that's enough. |
|
| Back to top |
|
 |
|