Profiler and profiling on Android NDK

Profiler and profiling on Android NDK
0.0 0


use someone some profiling tool for Android and NDK?

Tutorial for settings and use will be perfect.


So I found solution and it works :).


Using android-ndk-profiler

Summary (based on detailed changes listed below)
Changes to code and build scripts:
* Set ANDK_PROFILE to 1 in HelloWorldScene to enable. See associated code change below.
* Application needs permissions to write to sdcard (AndroidManifest.xml)
* Additional NDK_MODULE_PATH needs to be set to the location of android-ndk-profile (in
* Additional compiler flags need to be set and android-ndk-profiler needs to be linked. (in

* Switch to your android project’s directory, eg:
cd /cygdrive/d/Projects/ThirdParty/cocos2d-x/HelloWorld/
* Build the native code
* Rebuild android project (in eclipse etc) and Run the app on a device
* Then collect profiling data on these lines (in cygwin/bash etc depending on your platform):
adb’s path (specific to your machine/platform) would be something like [/cygdrive/c/android-sdk-windows/platform-tools]
Copy the profiling data file from the sdcard to your android project’s directory.
adb pull /sdcard/gmon.out .
* Analyze the profiling data to get formatted output
gprof’s path (specific to your machine/platform): export PATH=$PATH:/cygdrive/d/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin
arm-linux-androideabi-gprof obj/local/armeabi/ -PprofCount -QprofCount -P__gnu_mcount_nc -Q__gnu_mcount_nc
* Read through the formatted data to reveal bottlenecks.

For more usage details, see

Detailed changes:
Towards the top of %COCOS2DX_ROOT%\HelloWorld\Classes\HelloWorldScene.cpp

// Set this to 1 to enable profiling using the android-ndk-profiler
// Build/link settings may also require changes (,
#define ANDK_PROFILE 0
extern "C" void monstartup(char const*);
extern "C" void moncleanup();

You need to add a call to monstartup and moncleanup depending on what part of the code you wish to profile.
The following example may be to profile an entire scene.
Implement a virtual void HelloWorld::cleanup in case you haven’t to end the profiling and generate the data file gmon.out:

void HelloWorld::cleanup()
    /* Other cleanup code specific to your Scene */
    /* ... */

Add a call to monstartup in your init to begin profiling when the scene begins:

bool HelloWorld::init()
    /* Other code in your init function */
    /* ... */
    CC_BREAK_IF(! CCLayer::init());
    //setenv("CPUPROFILE_FREQUENCY", "500", 1); /* Change to 500 interrupts per second. Default is 100. */

    /* More code in your init function */
    /* ... */

Add to HelloWorld/

Set the location of where you’ve extracted android-ndk-profiler
Add near the top of HelloWorld/

# To add more module paths, set below. Start with a colon.

Towards the end of HelloWorld/, where NDK_MODULE_PATH is set (two locations), append the ADD_TO_NDK_MODULE_PATH variable



Set compiler flags and link android-ndk-profiler
Add near the location where LOCAL_WHOLE_STATIC_LIBRARIES is set in HelloWorld/

# compile with profiling
LOCAL_STATIC_LIBRARIES := android-ndk-profiler

And near the end of the file, add

$(call import-module,android-ndk-profiler)


if you are looking into ndk profiling then here are some free options for you :

you can either use the free android-ndk-profiler (less accurate) but works on any device what so ever and as for how to setup with cocos2d-x then you can go here

or you can use simpleperf witch requires a device runing android 5.1.1 (API 22) or higher in order to work and as for the setup you can go here and for more details on how to use it go here

the difference between the two is that simpleperf already comes with python scripts that automates a lot of command line operations for you witch makes things easier also simpleperf profiles without interfering with app execution witch gives more accurate results unlike android-ndk-profiler

also having tried both there is not much difference in terms of the report quality i personally find it a bit harder to read both of the generated reports either by the android-ndk-profiler or simpleperf so that’s another thing to take into account before trying any of them but having said that i have seen some who have been able to generate svg pictures which looks very nice using simpleperf … more on that in here

now if you are looking for a more polished easier to read reports then there is a profiler made by google called fplutil i never tried it myself but from what i seen in the main page it generates very easy to read nait html reports but sadly all of that comes at a cost of supporting only two devices which i gotta say is very disappointing

there is still a another option if you have a Nexus 9 and only a Nexus 9 then you are in luck because
you can benefit from nvidia high end profiler (Tegra System Profiler) and all at a cost of nothing

with the surprisingly lack of ndk profilers i figured this could help someone in the future.