From fc5cad1e2b2e9dfb62fa7a7c84f8fc87bb9a563b Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Fri, 16 Nov 2018 00:17:28 +0100 Subject: [PATCH] Add Android profiling build type --- platforms/android/demo/build.gradle | 3 +- platforms/android/demo/profiling.gradle | 57 +++++++++++++++++++++ platforms/android/profiling.md | 12 +++++ platforms/android/tangram/build.gradle | 4 +- platforms/android/tangram/profiling.gradle | 58 ++++++++++++++++++++++ 5 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 platforms/android/demo/profiling.gradle create mode 100644 platforms/android/profiling.md create mode 100644 platforms/android/tangram/profiling.gradle diff --git a/platforms/android/demo/build.gradle b/platforms/android/demo/build.gradle index 3a59dd1e9d..e7e68f24d1 100644 --- a/platforms/android/demo/build.gradle +++ b/platforms/android/demo/build.gradle @@ -1,5 +1,7 @@ apply plugin: 'com.android.application' +apply from: 'profiling.gradle' + android { compileSdkVersion 27 @@ -26,7 +28,6 @@ android { minifyEnabled true } } - } dependencies { diff --git a/platforms/android/demo/profiling.gradle b/platforms/android/demo/profiling.gradle new file mode 100644 index 0000000000..f4c05d19c7 --- /dev/null +++ b/platforms/android/demo/profiling.gradle @@ -0,0 +1,57 @@ + +// Set when building only part of the abis in the apk. +def abiFiltersForWrapScript = [] + +android { + buildTypes { + profiling { + initWith debug + externalNativeBuild { + cmake { + // cmake Debug build type uses -O0, which makes the code slow. + arguments "-DCMAKE_BUILD_TYPE=Release" + } + } + packagingOptions { + // Exclude wrap.sh for architectures not built. + if (abiFiltersForWrapScript) { + def exclude_abis = ["armeabi", "armeabi-v7a", "arm64-v8a", + "x86", "x86_64", "mips", "mips64"] + .findAll{ !(it in abiFiltersForWrapScript) } + .collect{ "**/" + it + "/wrap.sh" } + excludes += exclude_abis + } + } + + // Add lib/xxx/wrap.sh in the apk. This is to enable java profiling on Android O + // devices. + sourceSets { + profiling { + resources { + srcDir { + "profiling_apk_add_dir" + } + } + } + } + } + } +} + +def writeWrapScriptToFullyCompileJavaApp(wrapFile) { + wrapFile.withWriter { writer -> + writer.write('#!/system/bin/sh\n') + writer.write('\$@\n') + } +} + +task createProfilingApkAddDir { + for (String abi : ["armeabi", "armeabi-v7a", "arm64-v8a", "x86", "x86_64", "mips", "mips64"]) { + def dir = new File("app/profiling_apk_add_dir/lib/" + abi) + dir.mkdirs() + def wrapFile = new File(dir, "wrap.sh") + writeWrapScriptToFullyCompileJavaApp(wrapFile) + println "write file " + wrapFile.path + } +} + diff --git a/platforms/android/profiling.md b/platforms/android/profiling.md new file mode 100644 index 0000000000..a3e2dc4ea2 --- /dev/null +++ b/platforms/android/profiling.md @@ -0,0 +1,12 @@ +# Android + +- https://android.googlesource.com/platform/system/extras/+/master/simpleperf/doc/ +- https://android.googlesource.com/platform/system/extras/+/master/simpleperf/demo/README.md#profiling-javac-application + +## Studio + +- Run 'Profile selected configuration' +- Click CPU row +- Select 'Sampled (Native)' +- Click 'Record a method trace', right next to it +- Select thread of interest to show Call-/Flame-chart diff --git a/platforms/android/tangram/build.gradle b/platforms/android/tangram/build.gradle index ff84596e9e..a008d73e2a 100644 --- a/platforms/android/tangram/build.gradle +++ b/platforms/android/tangram/build.gradle @@ -5,6 +5,7 @@ group = GROUP version = VERSION_NAME apply from: 'versioning.gradle' +apply from: 'profiling.gradle' android { compileSdkVersion 27 @@ -27,7 +28,8 @@ android { cmake { targets 'tangram' arguments '-DTANGRAM_PLATFORM=android', - '-DANDROID_STL=c++_shared' + '-DANDROID_STL=c++_shared', + '-DCMAKE_BUILD_TYPE=Release' cppFlags '-std=c++14', '-pedantic', '-fPIC', diff --git a/platforms/android/tangram/profiling.gradle b/platforms/android/tangram/profiling.gradle new file mode 100644 index 0000000000..c215362491 --- /dev/null +++ b/platforms/android/tangram/profiling.gradle @@ -0,0 +1,58 @@ + +// Set when building only part of the abis in the apk. +def abiFiltersForWrapScript = [] + +android { + buildTypes { + profiling { + initWith debug + externalNativeBuild { + cmake { + // cmake Debug build type uses -O0, which makes the code slow. + arguments "-DCMAKE_BUILD_TYPE=Release" + cppFlags '-fno-omit-frame-pointer' + } + } + packagingOptions { + // Exclude wrap.sh for architectures not built. + if (abiFiltersForWrapScript) { + def exclude_abis = ["armeabi", "armeabi-v7a", "arm64-v8a", + "x86", "x86_64", "mips", "mips64"] + .findAll{ !(it in abiFiltersForWrapScript) } + .collect{ "**/" + it + "/wrap.sh" } + excludes += exclude_abis + } + } + + // Add lib/xxx/wrap.sh in the apk. This is to enable java profiling on Android O + // devices. + sourceSets { + profiling { + resources { + srcDir { + "profiling_apk_add_dir" + } + } + } + } + } + } +} + +def writeWrapScriptToFullyCompileJavaApp(wrapFile) { + wrapFile.withWriter { writer -> + writer.write('#!/system/bin/sh\n') + writer.write('\$@\n') + } +} + +task createProfilingApkAddDir { + for (String abi : ["armeabi", "armeabi-v7a", "arm64-v8a", "x86", "x86_64", "mips", "mips64"]) { + def dir = new File("app/profiling_apk_add_dir/lib/" + abi) + dir.mkdirs() + def wrapFile = new File(dir, "wrap.sh") + writeWrapScriptToFullyCompileJavaApp(wrapFile) + println "write file " + wrapFile.path + } +} +