Skip to content

Commit

Permalink
Merge pull request #658 from fjtrujy/gprof_improvements
Browse files Browse the repository at this point in the history
Some `gprof` improvements
  • Loading branch information
uyjulian authored Sep 4, 2024
2 parents 6c21a21 + b4cde5f commit f121e9b
Show file tree
Hide file tree
Showing 12 changed files with 450 additions and 23 deletions.
41 changes: 41 additions & 0 deletions ee/libprofglue/include/ps2prof.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
# _____ ___ ____ ___ ____
# ____| | ____| | | |____|
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
#-----------------------------------------------------------------------
# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
# Licenced under Academic Free License version 2.0
# Review ps2sdk README & LICENSE files for further details.
*/

#ifndef __PS2PROF_H__
#define __PS2PROF_H__

#ifdef __cplusplus
extern "C" {
#endif


/**
* Start the profiler.
* If the profiler is already running, this function stop previous one,
* and ignore the result.
* Finally, it initializes a new profiler session.
*/
__attribute__((__no_instrument_function__, __no_profile_instrument_function__))
void gprof_start(void);
/**
* Stop the profiler.
* If the profiler is not running, this function does nothing.
* @param filename The name of the file to write the profiling data to.
* @param should_dump If 1, the profiling data will be written to the file.
* If 0, the profiling data will be discarded.
*/
__attribute__((__no_instrument_function__, __no_profile_instrument_function__))
void gprof_stop(const char* filename, int should_dump);

#ifdef __cplusplus
}
#endif

#endif /* __PS2PROF_H__ */
12 changes: 12 additions & 0 deletions ee/libprofglue/samples/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# _____ ___ ____ ___ ____
# ____| | ____| | | |____|
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
#-----------------------------------------------------------------------
# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
# Licenced under Academic Free License version 2.0
# Review ps2sdk README & LICENSE files for further details.

SUBDIRS = basic custom

include $(PS2SDKSRC)/Defs.make
include $(PS2SDKSRC)/Rules.make
12 changes: 12 additions & 0 deletions ee/libprofglue/samples/basic/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# _____ ___ ____ ___ ____
# ____| | ____| | | |____|
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
#-----------------------------------------------------------------------
# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
# Licenced under Academic Free License version 2.0
# Review ps2sdk README & LICENSE files for further details.

SAMPLE_DIR = libprofglue/basic

include $(PS2SDKSRC)/Defs.make
include $(PS2SDKSRC)/samples/Rules.samples
27 changes: 27 additions & 0 deletions ee/libprofglue/samples/basic/Makefile.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# _____ ___ ____ ___ ____
# ____| | ____| | | |____|
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
#-----------------------------------------------------------------------
# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
# Licenced under Academic Free License version 2.0
# Review ps2sdk README & LICENSE files for further details.

EE_BIN = gprofbasic.elf
EE_OBJS = main.o

EE_CFLAGS = -Os -pg -g
EE_LDFLAGS += -pg -g

all: $(EE_BIN)

clean:
rm -rf $(EE_BIN) $(EE_OBJS)

run: $(EE_BIN)
ps2client execee host:$(EE_BIN)

reset:
ps2client reset

include $(PS2SDK)/samples/Makefile.pref
include $(PS2SDK)/samples/Makefile.eeglobal
66 changes: 66 additions & 0 deletions ee/libprofglue/samples/basic/README.md
Original file line number Diff line number Diff line change
@@ -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 `EE_CFLAGS` and `EE_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 `mips64r5900el-ps2-elf-gprof` binary.

For instance, following the next syntax:
```
mips64r5900el-ps2-elf-gprof -b {binary.elf} gmon.out
```

like:
```
mips64r5900el-ps2-elf-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
83.26 0.19 0.19 104728 0.00 0.00 is_prime
16.74 0.23 0.04 1 39.00 39.00 dummy_function
0.00 0.23 0.00 1 0.00 233.00 main
0.00 0.23 0.00 1 0.00 194.00 sum_of_square_roots
Call graph
granularity: each sample hit covers 2 byte(s) for 0.43% of 0.23 seconds
index % time self children called name
0.00 0.23 1/1 _ftext [2]
[1] 100.0 0.00 0.23 1 main [1]
0.00 0.19 1/1 sum_of_square_roots [4]
0.04 0.00 1/1 dummy_function [5]
-----------------------------------------------
<spontaneous>
[2] 100.0 0.00 0.23 _ftext [2]
0.00 0.23 1/1 main [1]
-----------------------------------------------
0.19 0.00 104728/104728 sum_of_square_roots [4]
[3] 83.3 0.19 0.00 104728 is_prime [3]
-----------------------------------------------
0.00 0.19 1/1 main [1]
[4] 83.3 0.00 0.19 1 sum_of_square_roots [4]
0.19 0.00 104728/104728 is_prime [3]
-----------------------------------------------
0.04 0.00 1/1 main [1]
[5] 16.7 0.04 0.00 1 dummy_function [5]
-----------------------------------------------
Index by function name
[5] dummy_function [1] main
[3] is_prime [4] sum_of_square_roots
```

Cheers
70 changes: 70 additions & 0 deletions ee/libprofglue/samples/basic/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
# _____ ___ ____ ___ ____
# ____| | ____| | | |____|
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
#-----------------------------------------------------------------------
# Copyright 2001-2005, ps2dev - http://www.ps2dev.org
# Licenced under Academic Free License version 2.0
# Review ps2sdk README & LICENSE files for further details.
#
# A basic example for checking the GProf profiler.
*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

// 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;
}
12 changes: 12 additions & 0 deletions ee/libprofglue/samples/custom/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# _____ ___ ____ ___ ____
# ____| | ____| | | |____|
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
#-----------------------------------------------------------------------
# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
# Licenced under Academic Free License version 2.0
# Review ps2sdk README & LICENSE files for further details.

SAMPLE_DIR = libprofglue/custom

include $(PS2SDKSRC)/Defs.make
include $(PS2SDKSRC)/samples/Rules.samples
27 changes: 27 additions & 0 deletions ee/libprofglue/samples/custom/Makefile.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# _____ ___ ____ ___ ____
# ____| | ____| | | |____|
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
#-----------------------------------------------------------------------
# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
# Licenced under Academic Free License version 2.0
# Review ps2sdk README & LICENSE files for further details.

EE_BIN = gprofcustom.elf
EE_OBJS = main.o

EE_CFLAGS = -Os -pg -g
EE_LDFLAGS += -pg -g

all: $(EE_BIN)

clean:
rm -rf $(EE_BIN) $(EE_OBJS)

run: $(EE_BIN)
ps2client execee host:$(EE_BIN)

reset:
ps2client reset

include $(PS2SDK)/samples/Makefile.pref
include $(PS2SDK)/samples/Makefile.eeglobal
57 changes: 57 additions & 0 deletions ee/libprofglue/samples/custom/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
More advance example about how to use the `gprof` feature.

The requiremnts are quite easy, just adding `-g -pg` flags to the `EE_CFLAGS` and `EE_LDFLAGS` is enough to make things to work out of the box.

This example shows how to enable profiling just around some specific piece of code.
How `gprof` by default start profiling from the very beginning of the app we must discard that result, this is why we do `gprof_stop(NULL, false);`.
Then we just need to call `gprof_start();` whenever we want to start meassuring our piece of code and `gprof_stop("gmon_custom.out", true);` whenver we want to stop the profiling.

Firstly execute your program, then once program ends it will automatically generates the output with the given names.

In order to inspect the content of the generated file you need to use the `mips64r5900el-ps2-elf-gprof` binary.

For instance, following the next syntax:
```
mips64r5900el-ps2-elf-gprof -b {binary.elf} {gmon_custom.out}
```
like:
```
mips64r5900el-ps2-elf-gprof -b gprofcustom.elf gmon_custom.out
```

Output in this example:
```
Flat profile:
Each sample counts as 0.001 seconds.
% cumulative self self total
time seconds seconds calls s/call s/call name
87.37 5.18 5.18 1 5.18 5.18 heavy_operation_3
12.63 5.93 0.75 1 0.75 0.75 heavy_operation_2
Call graph
granularity: each sample hit covers 2 byte(s) for 0.02% of 5.93 seconds
index % time self children called name
<spontaneous>
[1] 100.0 0.00 5.93 main [1]
5.18 0.00 1/1 heavy_operation_3 [2]
0.75 0.00 1/1 heavy_operation_2 [3]
-----------------------------------------------
5.18 0.00 1/1 main [1]
[2] 87.4 5.18 0.00 1 heavy_operation_3 [2]
-----------------------------------------------
0.75 0.00 1/1 main [1]
[3] 12.6 0.75 0.00 1 heavy_operation_2 [3]
-----------------------------------------------
Index by function name
[3] heavy_operation_2 [2] heavy_operation_3
```

Cheers.
Loading

0 comments on commit f121e9b

Please sign in to comment.