Thursday, March 7, 2013

android-ndk-profiler tweaks

Recently I need to check our software with profiler. I think everyone knows about gprof and Android as Linux small brother has it too. So android-ndk-profiler really usefull thing, but if one use ndk-build script (standart build scripts from NDK) soon one will see that sources stripped...


/opt/android-ndk-r8/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi-strip --strip-unneeded  ./libs/armeabi-v7a/libgeneric_host.so

and check again

nika@nika_gt ~/work/Android/Host $ arm-linux-androideabi-gprof libs/armeabi-v7a/libgeneric_host.so
arm-linux-androideabi-gprof: file `libs/armeabi-v7a/libgeneric_host.so' has no symbols

not so good, but let's check where is inside Android build scripts we strip a binary

nika@nika_gt /opt/android-ndk-r8 $ grep -r "strip-unneeded" *
build/tools/build-gdbserver.sh:run $TOOLCHAIN_PREFIX-objcopy --strip-unneeded $BUILD_OUT/gdbserver $DEST/$DSTFILE
build/core/default-build-commands.mk:# It is thus safe to use --strip-unneeded, which is only dangerous
build/core/default-build-commands.mk:cmd-strip = $(PRIVATE_STRIP) --strip-unneeded $(call host-path,$1)
docs/CHANGES.html:- Reduced the size of generated binaries by using --strip-unneeded


Ok, now we know that all problems inside default-build-commands.mk

cmd-strip = $(PRIVATE_STRIP) --strip-unneeded $(call host-path,$1)



this command inside default-build-commands.mk will strip our binary. oK we almost there. As we know from manual we need to add special flags when we want to profile file. So will be cool to automate this process.


Let's add into Android.mk (makefile for native sources) some strings


# profiler needed
ifdef USE_PROFILER
LOCAL_LDLIBS += -Lprofiler -landprof
endif
 
include $(BUILD_SHARED_LIBRARY)

# profiler needed
ifdef USE_PROFILER
LOCAL_CFLAGS += $(filter-out -fomit-frame-pointer,$(LOCAL_CFLAGS))    \
        $(filter-out -ffunction-sections,$(LOCAL_CFLAGS))    \
        -pg -DUSE_PROFILER

# we dont want strip
cmd-strip=
endif
 
We added 2 blocks, with "profiler needed" comment, include was in the file. Let's check

when we just run ndk-build

Install        : libgeneric_host.so => libs/armeabi-v7a/libgeneric_host.so
install -p ./obj/local/armeabi-v7a/libgeneric_host.so ./libs/armeabi-v7a/libgeneric_host.so
/opt/android-ndk-r8/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi-strip --strip-unneeded  ./libs/armeabi-v7a/libgeneric_host.so


When we run it with
USE_PROFILER=1 ndk-build

Install        : libgeneric_host.so => libs/armeabi-v7a/libgeneric_host.so
install -p ./obj/local/armeabi-v7a/libgeneric_host.so ./libs/armeabi-v7a/libgeneric_host.so

Last one doesn't have strip. Cool))

No comments:

Post a Comment