mirror of
				https://github.com/android/ndk-samples
				synced 2025-11-04 14:27:06 +08:00 
			
		
		
		
	I took the PGO and Camera example and added order file functionalities to them. I added steps so people can figure out how to add order file to their own Android application.
		
			
				
	
	
	
		
			3.0 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	
			3.0 KiB
		
	
	
	
	
	
	
	
Order file demo
Order files are text files containing symbols representing functions names. Linkers (lld) uses order files to layout functions in a specific order. These binaries with ordered symbols will reduce page faults and improve a program's launch time due to the efficient loading of symbols during a program’s cold-start.
Files
- app/src/main/cpp/orderfile.cpp: The source code for the orderfile library that is used by the Kotlin app.
 - app/src/main/cpp/CMakeLists.txt: The CMakeLists either sets the orderfile library as generating profiles or loading the orderfile.
 - app/src/main/java/MainActivity.kt: The Kotlin app source code.
 
Profile Steps
- For simplicity, we have setup the 
CMakeLists.txtand you just need make sureset(GENERATE_PROFILES ON)is not commented. You need to pass any optimization flag except-O0. The mapping file is not generated and the profile instrumentation does not work without an optimization flag. - Run the app on Android Studio. You can either run it on a physical or virtual device. You will see "Hello World" on the screen.
 - To pull the data from the device, you'll need to move it from an app-writable directory to a shell readable directory for adb pull. We also need to transfer the output into hexadecimal format.
 
adb shell "run-as com.example.orderfiledemo sh -c 'cat /data/user/0/com.example.orderfiledemo/cache/demo.output.order' | cat > /data/local/tmp/demo.output.order"
adb pull /data/local/tmp/demo.output.order .
# Convert to hexdeciaml format on Linux, Mac, or ChromeOS
hexdump -C demo.output.order > demo.prof
# Convert to hexdecimal format on Windows
certutil -f -encodeHex demo.output.order demo.prof
- Once you get both mapping file and profile file, you can use this script to create the order file:
 
python3 create_orderfile.py --profile-file demo.prof --mapping-file mapping.txt --output app/src/main/cpp/demo.orderfile
Load Steps
- 
For load, you need to uncomment
set(USE_PROFILE "${CMAKE_SOURCE_DIR}/demo.orderfile")and make sureset(GENERATE_PROFILES ON)is commented. - 
If you want to validate the shared library's layout is different, you need to find
liborderfiledemo.soand runnm 
nm -n liborderfiledemo.so
Difference between Java and Kotlin App
The main difference between a Java app and a Kotlin app is the syntax. You can easily change this Kotlin example into a Java example.
- Load Library
 
# Kotlin
companion object {
    init {
        System.loadLibrary("orderfiledemo")
    }
}
# Java
static {
    System.loadLibrary("orderfiledemo");
}
- Recognize an external method
 
# Kotlin
external fun runWorkload(tempDir: String)
# Java
private native void runWorkload(String tempDir);
- Get the cache directory
 
# Kotlin
runWorkload(applicationContext.cacheDir.toString())
# Java
runWorkload(getcacheDir().toString())