GP2X Advanced Optimization Via Profiling With Gcc4


Not sure if this has been posted before or not, but you can get profiling to work with a cross compiling windows dev environment.

Caution: Messing with binaries is not very safe, I do not take any responsibility for your actions.

Heres what i do with my port of hexen 2:
  • 1. Compile Program with the compiler & linker flag "-fprofile-generate". On my compiler (the one that comes with devkitGP2X) it doesnt seem to like using this flag with goto statements.... you can either rewrite the sources without goto or remove the flag from them. I also needed to add -lgcov as a linker flag.
    2. Using a string search and replace program (notepad is enough) open your binary and replace all instances of the string *your source directory* with a linux style directory on your SD card which is the exact same string length and already exists. For Example:

    I compiled in "C:\2xHexen2 v0.02 Src\hexen2" so i replaced this string with "/mnt/sd/Hexen2ProfilingTemp"

    3. Transfer the binary to anywhere on your sd card and run.
    4. After a sufficently intense run through, exit your program and check the "same length" directory on your SD card. There should be *.gcda files there.
    5. Copy these .gcda files to your compile directory on your PC.
    6. Recompile with "-fprofile-use" compiler and linker flag.
This may also be possible using cygwin but i find this easier, automating it shouldn't be hard either.
 
Hi, i've seen good improvements with uae4all as well. If you get -gcov errors at link-time make sure you're adding -fprofile-generate to your LDFLAGS.

I haven't been able to find answers to some questions though:

When I create a binary with -fprofile-generate for the profiling run, should I include optimization CFLAGS like
-fomit-frame-pointer -fforce-addr -fforce-mem
-falign-loops=2 -falign-functions=2 -falign-jumps=2
-funroll-loops
to the intial build?

Or will gcc decide whether and where to use such optimizations in the -fprofile-use stage, based on the run-time .gcda data? In other words, if I specify for e.g. -funroll-loops in either the 'generate' or 'use' stage, am I forcing gcc to apply a potentially slower flag?

I'm using gcc-3.4 codesourcery - haven't gotten a 4.x running yet.

Thanks - cheers.
 
Thanks for the great guide to profiling. Just gone through the motions on the A320 Dingoo and all went well. Speed up was about the 20% mark - which is fantastic! My executable is also smaller - interestingly by about 20%. Another bonus for the memory starved Dingoo.

A couple pointers to other people doing this:

The .gcda files are written on the host's filesystem to the same place where the equivalent .o file would have gone.
I've cunningly compiled my code to all go to the one place.

Once compiled with -fprofile-generate, you can see where they will be written to by running something like the following commands:

strings emulator.exe | grep path_to_exe_objects

where "path_to_exec_objects" is the path where you built your executable. As notaz and others have already mentioned this path is always built in to your new profiling executable.

Now that my proof-of-concept run was successful, I will do some more comprehensive profiling.
 
I have found that later versions of gcc (>= 4.4.0) allow you to specify the path where .gcda files are generated - very handy!

ie. -fprofile-generate=/usr/local/profile

This means you don't have to change your compiling directory or create links or hand-edit your .o files.
 
Hi all,
I'm using devkitGP2X (gcc4.0.2), when I enabe profiling (-lprofile-generate) I get the following error:
SRC/bz_3D.c: In function 'drawtritex16b_FixPoint_split2':
SRC/bz_3D.c:1893: internal compiler error: in int_mode_for_mode, at stor-layout.c:251

If I comment out the function (which isn't used anyway) it says the same thing on the next one.

Only info google gave me is that some other projects had the same error, but it was "only with gcc4.0.x".

When looking for other compilers, the only one I found was the official GP2X dev kit (gcc 3.4.6). I tried compilig with DevCPP's profiling switch (-lgmon -pg). It complained about not finding libgmon (or something like that). So I tried compiling with the -pg switch alone, but then the app crashes on loading. (as a side note, my app is 3 times slower with this compiler)

What compiler are you guys using? Any equivalent available on Win32?

All I want is the timings and calls so I can know where to optimize and/or cut.
thanks.
 
barzoule said:
What compiler are you guys using? Any equivalent available on Win32?

All I want is the timings and calls so I can know where to optimize and/or cut.
thanks.

As Notaz posted, the Open2x toolchain:
http://wiki.open2x.org/open2x/wiki/index.php?title=Toolchain#Linux_users

Note the section for Windows users too.

Good luck!
 
Last edited by a moderator:
Thanks for this great Tutorial, notaz.

Today I tried a bit with the profiles, but it doesn't matter, what I do, the profiler isn't faster than my normal optimization. Sometimes it is even slower (just a few fps, but still slower). But nevertheless thanks a lot, I hope, I am able to use it in another project. In my current game, it is senseless. :-\
 
Critical: thanks I'll give it a try. Somehow I was sure it was Linux-only

Ziz: If you see it as the compiler not being able to optimize better than yourself, that it's a good thing :) Losing perfs when using profiling-aided optimization is scary tho..
 
barzoule: It is not really slower, but always smaller. It is only slower, if I use "static" instead of inline. ;-)
 
Back
Top