diff --git a/orderfile/README.md b/orderfile/README.md index 8fb0474aa..d0d43f9e1 100644 --- a/orderfile/README.md +++ b/orderfile/README.md @@ -20,29 +20,17 @@ cold-start. sure `set(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. -1. Run the app on Android Studio. You can either run it on a physical or virtual +2. 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. -1. 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. +3. 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. +4. Use `llvm-profdata` to merge all the raw files and create an orderfile. ``` -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 -``` - -4. Once you get both mapping file and profile file, you can use - [this script](https://android.googlesource.com/toolchain/pgo-profiles/+/refs/heads/main/scripts/create_orderfile.py) - to create the order file: - -``` -python3 create_orderfile.py --profile-file demo.prof --mapping-file mapping.txt --output app/src/main/cpp/demo.orderfile +adb shell "run-as com.example.orderfiledemo sh -c 'cat /data/user/0/com.example.orderfiledemo/cache/demo.profraw' | cat > /data/local/tmp/demo.profraw" +adb pull /data/local/tmp/demo.profraw . +/toolchains/llvm/prebuilt//bin/llvm-profdata merge demo.profraw -o demo.profdata +/toolchains/llvm/prebuilt//bin/llvm-profdata order demo.profdata -o demo.orderfile ``` ## Load Steps @@ -51,10 +39,11 @@ python3 create_orderfile.py --profile-file demo.prof --mapping-file mapping.txt `set(USE_PROFILE "${CMAKE_SOURCE_DIR}/demo.orderfile")` and make sure `set(GENERATE_PROFILES ON)` is commented. -1. If you want to validate the shared library's layout is different, you need to +2. If you want to validate the shared library's layout is different, you need to find `liborderfiledemo.so` and run `nm` ``` +mv demo.orderfile app/src/main/cpp nm -n liborderfiledemo.so ``` diff --git a/orderfile/app/src/main/cpp/CMakeLists.txt b/orderfile/app/src/main/cpp/CMakeLists.txt index 6089a3242..a5d78df48 100644 --- a/orderfile/app/src/main/cpp/CMakeLists.txt +++ b/orderfile/app/src/main/cpp/CMakeLists.txt @@ -18,12 +18,10 @@ target_link_libraries(orderfiledemo PRIVATE base::base log) if(GENERATE_PROFILES) # Generating profiles requires any optimization flag aside from -O0. - # The mapping file will not generate and the profile instrumentation does not work without an optimization flag. - # Temporarily pass "-Wno-deprecated" since the order-file flags will be updated in a future PR - target_compile_options(orderfiledemo PRIVATE -forder-file-instrumentation -O1 -mllvm -orderfile-write-mapping=mapping.txt -Wno-deprecated) - target_link_options(orderfiledemo PRIVATE -forder-file-instrumentation) + target_compile_options(orderfiledemo PRIVATE -fprofile-generate -ftemporal-profile -O1) + target_link_options(orderfiledemo PRIVATE -fprofile-generate -ftemporal-profile) target_compile_definitions(orderfiledemo PRIVATE GENERATE_PROFILES) elseif(USE_PROFILE) - target_compile_options(orderfiledemo PRIVATE -Wl,--symbol-ordering-file=${USE_PROFILE} -Wl,--no-warn-symbol-ordering) + target_compile_options(orderfiledemo PRIVATE) target_link_options(orderfiledemo PRIVATE -Wl,--symbol-ordering-file=${USE_PROFILE} -Wl,--no-warn-symbol-ordering) -endif() \ No newline at end of file +endif() diff --git a/orderfile/app/src/main/cpp/demo.orderfile b/orderfile/app/src/main/cpp/demo.orderfile new file mode 100644 index 000000000..2c4165164 --- /dev/null +++ b/orderfile/app/src/main/cpp/demo.orderfile @@ -0,0 +1,5 @@ +# Ordered 4 functions +JNI_OnLoad +_Z11RunWorkloadP7_JNIEnvP8_jobjectP8_jstring +_Z23DumpProfileDataIfNeededPKc +_ZL8snprintfPcU17pass_object_size1mPKcz diff --git a/orderfile/app/src/main/cpp/orderfile.cpp b/orderfile/app/src/main/cpp/orderfile.cpp index 9fdd69f42..f275dd81d 100644 --- a/orderfile/app/src/main/cpp/orderfile.cpp +++ b/orderfile/app/src/main/cpp/orderfile.cpp @@ -11,13 +11,13 @@ const char kLogTag[] = "orderfiledemo"; #ifdef GENERATE_PROFILES extern "C" int __llvm_profile_set_filename(const char*); extern "C" int __llvm_profile_initialize_file(void); -extern "C" int __llvm_orderfile_dump(void); +extern "C" int __llvm_profile_dump(void); #endif void DumpProfileDataIfNeeded(const char* temp_dir) { #ifdef GENERATE_PROFILES char profile_location[PATH_MAX] = {}; - snprintf(profile_location, sizeof(profile_location), "%s/demo.output", + snprintf(profile_location, sizeof(profile_location), "%s/demo.profraw", temp_dir); if (__llvm_profile_set_filename(profile_location) == -1) { __android_log_print(ANDROID_LOG_ERROR, kLogTag, @@ -33,14 +33,15 @@ void DumpProfileDataIfNeeded(const char* temp_dir) { return; } - if (__llvm_orderfile_dump() == -1) { + if (__llvm_profile_dump() == -1) { __android_log_print(ANDROID_LOG_ERROR, kLogTag, - "__llvm_orderfile_dump() failed: %s", strerror(errno)); + "__llvm_profile_dump() failed: %s", strerror(errno)); return; } __android_log_print(ANDROID_LOG_DEBUG, kLogTag, "Wrote profile data to %s", profile_location); #else + (void)temp_dir; // To avoid unused-parameter warning __android_log_print(ANDROID_LOG_DEBUG, kLogTag, "Did not write profile data because the app was not " "built for profile generation");