Adding basic example

This commit is contained in:
Francisco Javier Trujillo Mata
2024-08-24 16:20:52 +02:00
parent a033c3ad4e
commit 72c5c4ab25
5 changed files with 162 additions and 0 deletions

View File

@@ -12,6 +12,7 @@ SAMPLES = \
debug/debugkb \ debug/debugkb \
debug/sio \ debug/sio \
debug/gdb \ debug/gdb \
gprof/basic \
gu/beginobject \ gu/beginobject \
gu/blend \ gu/blend \
gu/blit \ gu/blit \

View File

@@ -9,6 +9,7 @@ SAMPLES = \
debug/debugkb \ debug/debugkb \
debug/sio \ debug/sio \
debug/gdb \ debug/gdb \
gprof/basic \
gu/beginobject \ gu/beginobject \
gu/blend \ gu/blend \
gu/blit \ gu/blit \

View File

@@ -0,0 +1,16 @@
TARGET = gprofbasic
OBJS = main.o
INCDIR =
CFLAGS = -O2 -Wall -pg -g
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
LIBDIR =
LDFLAGS = -pg -g
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = GProf Basic Example
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak

View File

@@ -0,0 +1,66 @@
Sample program to show how to use the `gprof` feature.
The requiremnts are quite easy, just adding `-g -pg` flags to the `CFLAGS` and `LDFLAGS` is enough to make things to work out of the box.
Firstly execute your program, then once program ends it will automatically generates a `gmon.out` file at CWD level.
In order to inspect the content of the generated file you need to use the `psp-gprof` binary.
For instance, following the next syntax:
```
psp-gprof -b {binary.elf} gmon.out
```
like:
```
psp-gprof -b gprofbasic.elf gmon.out
```
It will show something like:
```
Flat profile:
Each sample counts as 0.001 seconds.
% cumulative self self total
time seconds seconds calls ms/call ms/call name
95.98 0.17 0.17 104728 0.00 0.00 is_prime
4.02 0.17 0.01 1 7.00 7.00 dummy_function
0.00 0.17 0.00 1 0.00 174.00 main
0.00 0.17 0.00 1 0.00 167.00 sum_of_square_roots
Call graph
granularity: each sample hit covers 2 byte(s) for 0.57% of 0.17 seconds
index % time self children called name
0.00 0.17 1/1 _main [2]
[1] 100.0 0.00 0.17 1 main [1]
0.00 0.17 1/1 sum_of_square_roots [4]
0.01 0.00 1/1 dummy_function [5]
-----------------------------------------------
<spontaneous>
[2] 100.0 0.00 0.17 _main [2]
0.00 0.17 1/1 main [1]
-----------------------------------------------
0.17 0.00 104728/104728 sum_of_square_roots [4]
[3] 96.0 0.17 0.00 104728 is_prime [3]
-----------------------------------------------
0.00 0.17 1/1 main [1]
[4] 96.0 0.00 0.17 1 sum_of_square_roots [4]
0.17 0.00 104728/104728 is_prime [3]
-----------------------------------------------
0.01 0.00 1/1 main [1]
[5] 4.0 0.01 0.00 1 dummy_function [5]
-----------------------------------------------
Index by function name
[5] dummy_function [1] main
[3] is_prime [4] sum_of_square_roots
```
Cheers

View File

@@ -0,0 +1,78 @@
/*
* PSP Software Development Kit - https://github.com/pspdev
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in PSPSDK root for details.
*
* main.c - A basic example for checking the GProf profiler.
*
* Copyright (c) 2024 Francisco Javier Trujillo Mata - fjtrujy@gmail.com
*
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <pspmoduleinfo.h>
#include <pspthreadman.h>
PSP_MODULE_INFO("GProf Basic Example", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
// Function to check if a number is prime
int is_prime(int num)
{
if (num <= 1)
return 0;
if (num <= 3)
return 1;
if (num % 2 == 0 || num % 3 == 0)
return 0;
for (int i = 5; i * i <= num; i += 6)
{
if (num % i == 0 || num % (i + 2) == 0)
return 0;
}
return 1;
}
// Function to compute the sum of square roots of the first N prime numbers
double sum_of_square_roots(int N)
{
int count = 0;
int num = 2;
double sum = 0.0;
while (count < N)
{
if (is_prime(num))
{
sum += sqrt(num);
count++;
}
num++;
}
return sum;
}
int dummy_function()
{
int i;
for (i = 0; i < 10000; i++)
{
printf(".");
}
printf("\n");
return 0;
}
int main(int argc, char *argv[])
{
printf("Hello, world!\n");
dummy_function();
int N = 10000; // Large number of primes to compute
printf("Sum of square roots of the first %d prime numbers is %lf\n", N, sum_of_square_roots(N));
printf("Goodbye, world!\n");
return 0;
}