diff --git a/platforms/android/demo/build.gradle b/platforms/android/demo/build.gradle index 8984443c82..bc3a00ef7b 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 28 @@ -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 795c866ceb..13ccdbe5b3 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 28 @@ -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', '-fvisibility=hidden', // export only JNIEXPORT '-pedantic', 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 + } +} +