Skip to content

Commit 988d73b

Browse files
Fix orderfile demo to use ftemporal-profile (#1168)
The original orderfile flag (`-forder-file-instrumentation`) was deprecated in llvm/llvm-project#130192 and they have moved to using `ftemporal-profile` now. I have updated the orderfile demo to work with `ftemporal-profile` and updated the README with the new steps.
1 parent 1f70391 commit 988d73b

4 files changed

Lines changed: 24 additions & 31 deletions

File tree

orderfile/README.md

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,17 @@ cold-start.
2020
sure `set(GENERATE_PROFILES ON)` is not commented. You need to pass any
2121
optimization flag except `-O0`. The mapping file is not generated and the
2222
profile instrumentation does not work without an optimization flag.
23-
1. Run the app on Android Studio. You can either run it on a physical or virtual
23+
2. Run the app on Android Studio. You can either run it on a physical or virtual
2424
device. You will see "Hello World" on the screen.
25-
1. To pull the data from the device, you'll need to move it from an app-writable
26-
directory to a shell readable directory for adb pull. We also need to
27-
transfer the output into hexadecimal format.
25+
3. To pull the data from the device, you'll need to move it from an app-writable
26+
directory to a shell readable directory for adb pull.
27+
4. Use `llvm-profdata` to merge all the raw files and create an orderfile.
2828

2929
```
30-
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"
31-
adb pull /data/local/tmp/demo.output.order .
32-
33-
# Convert to hexdeciaml format on Linux, Mac, or ChromeOS
34-
hexdump -C demo.output.order > demo.prof
35-
36-
# Convert to hexdecimal format on Windows
37-
certutil -f -encodeHex demo.output.order demo.prof
38-
```
39-
40-
4. Once you get both mapping file and profile file, you can use
41-
[this script](https://android.googlesource.com/toolchain/pgo-profiles/+/refs/heads/main/scripts/create_orderfile.py)
42-
to create the order file:
43-
44-
```
45-
python3 create_orderfile.py --profile-file demo.prof --mapping-file mapping.txt --output app/src/main/cpp/demo.orderfile
30+
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"
31+
adb pull /data/local/tmp/demo.profraw .
32+
<NDK_PATH>/toolchains/llvm/prebuilt/<ARCH>/bin/llvm-profdata merge demo.profraw -o demo.profdata
33+
<NDK_PATH>/toolchains/llvm/prebuilt/<ARCH>/bin/llvm-profdata order demo.profdata -o demo.orderfile
4634
```
4735

4836
## Load Steps
@@ -51,10 +39,11 @@ python3 create_orderfile.py --profile-file demo.prof --mapping-file mapping.txt
5139
`set(USE_PROFILE "${CMAKE_SOURCE_DIR}/demo.orderfile")` and make sure
5240
`set(GENERATE_PROFILES ON)` is commented.
5341

54-
1. If you want to validate the shared library's layout is different, you need to
42+
2. If you want to validate the shared library's layout is different, you need to
5543
find `liborderfiledemo.so` and run `nm`
5644

5745
```
46+
mv demo.orderfile app/src/main/cpp
5847
nm -n liborderfiledemo.so
5948
```
6049

orderfile/app/src/main/cpp/CMakeLists.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ target_link_libraries(orderfiledemo PRIVATE base::base log)
1818

1919
if(GENERATE_PROFILES)
2020
# Generating profiles requires any optimization flag aside from -O0.
21-
# The mapping file will not generate and the profile instrumentation does not work without an optimization flag.
22-
# Temporarily pass "-Wno-deprecated" since the order-file flags will be updated in a future PR
23-
target_compile_options(orderfiledemo PRIVATE -forder-file-instrumentation -O1 -mllvm -orderfile-write-mapping=mapping.txt -Wno-deprecated)
24-
target_link_options(orderfiledemo PRIVATE -forder-file-instrumentation)
21+
target_compile_options(orderfiledemo PRIVATE -fprofile-generate -ftemporal-profile -O1)
22+
target_link_options(orderfiledemo PRIVATE -fprofile-generate -ftemporal-profile)
2523
target_compile_definitions(orderfiledemo PRIVATE GENERATE_PROFILES)
2624
elseif(USE_PROFILE)
27-
target_compile_options(orderfiledemo PRIVATE -Wl,--symbol-ordering-file=${USE_PROFILE} -Wl,--no-warn-symbol-ordering)
25+
target_compile_options(orderfiledemo PRIVATE)
2826
target_link_options(orderfiledemo PRIVATE -Wl,--symbol-ordering-file=${USE_PROFILE} -Wl,--no-warn-symbol-ordering)
29-
endif()
27+
endif()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ordered 4 functions
2+
JNI_OnLoad
3+
_Z11RunWorkloadP7_JNIEnvP8_jobjectP8_jstring
4+
_Z23DumpProfileDataIfNeededPKc
5+
_ZL8snprintfPcU17pass_object_size1mPKcz

orderfile/app/src/main/cpp/orderfile.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ const char kLogTag[] = "orderfiledemo";
1111
#ifdef GENERATE_PROFILES
1212
extern "C" int __llvm_profile_set_filename(const char*);
1313
extern "C" int __llvm_profile_initialize_file(void);
14-
extern "C" int __llvm_orderfile_dump(void);
14+
extern "C" int __llvm_profile_dump(void);
1515
#endif
1616

1717
void DumpProfileDataIfNeeded(const char* temp_dir) {
1818
#ifdef GENERATE_PROFILES
1919
char profile_location[PATH_MAX] = {};
20-
snprintf(profile_location, sizeof(profile_location), "%s/demo.output",
20+
snprintf(profile_location, sizeof(profile_location), "%s/demo.profraw",
2121
temp_dir);
2222
if (__llvm_profile_set_filename(profile_location) == -1) {
2323
__android_log_print(ANDROID_LOG_ERROR, kLogTag,
@@ -33,14 +33,15 @@ void DumpProfileDataIfNeeded(const char* temp_dir) {
3333
return;
3434
}
3535

36-
if (__llvm_orderfile_dump() == -1) {
36+
if (__llvm_profile_dump() == -1) {
3737
__android_log_print(ANDROID_LOG_ERROR, kLogTag,
38-
"__llvm_orderfile_dump() failed: %s", strerror(errno));
38+
"__llvm_profile_dump() failed: %s", strerror(errno));
3939
return;
4040
}
4141
__android_log_print(ANDROID_LOG_DEBUG, kLogTag, "Wrote profile data to %s",
4242
profile_location);
4343
#else
44+
(void)temp_dir; // To avoid unused-parameter warning
4445
__android_log_print(ANDROID_LOG_DEBUG, kLogTag,
4546
"Did not write profile data because the app was not "
4647
"built for profile generation");

0 commit comments

Comments
 (0)