Calculate CPU Charge ?
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 ?
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 ?
CPU speed still on the second page where I asked about it:
http://forums.ps2dev.org/viewtopic.php?t=9815
http://forums.ps2dev.org/viewtopic.php?t=9815
If not actually, then potentially.
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.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 :)
Code: Select all
#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));
}
}
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?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.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.Code: Select all
#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)); } }
edit:
Hmm seems I have screwed something up, looks like you need the global profilers enabled correctly as well for some reason. Which sucks :)
P.s.: I'm on CF 3.90M33 and I used that function in a VSH plugin...
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.
Or simply there is a misusage of this profiler.
well the code is at the top for one..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.
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: Select all
#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.
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:
and here's main.c for the app:
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.
Here's main.c for the prx:
Code: Select all
#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;
}
Code: Select all
#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 */
}
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.
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.
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
but if i do "make" again, without changing anything.
it compiles... thats just odd.
so how can i fix this to get it to compile?
thanks again for the help.
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: Select all
$ 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
for the prx its weird, starting clean it fails with this
Code: Select all
$ 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
Code: Select all
$ make
psp-prxgen cpumanager.elf cpumanager.prx
so how can i fix this to get it to compile?
thanks again for the help.
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
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