Skip to content

Commit

Permalink
Formatted files based on clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharjeel-Khan committed Aug 4, 2023
1 parent e3bfdb1 commit 80c27fe
Show file tree
Hide file tree
Showing 18 changed files with 153 additions and 109 deletions.
67 changes: 47 additions & 20 deletions orderfile/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
Order file demo
============================
# 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.
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.
## 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
---------------
1. For simplicity, we have setup the `CMakeLists.txt` and you just need make 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.
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.
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. We also need to transfer the output into hexadecimal format.
## Profile Steps

1. For simplicity, we have setup the `CMakeLists.txt` and you just need make
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
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.

```
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 .
Expand All @@ -24,24 +36,35 @@ 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:

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
```

Load Steps
---------------
1. For load, you need to uncomment `set(USE_PROFILE "${CMAKE_SOURCE_DIR}/demo.orderfile")` and make sure `set(GENERATE_PROFILES ON)` is commented.
## Load Steps

1. For load, you need to uncomment
`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
find `liborderfiledemo.so` and run `nm`

2. If you want to validate the shared library's layout is different, you need to find `liborderfiledemo.so` and run `nm`
```
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.
## 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 {
Expand All @@ -55,15 +78,19 @@ 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

```agsl
# Kotlin
runWorkload(applicationContext.cacheDir.toString())
Expand Down
2 changes: 1 addition & 1 deletion orderfile/app/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/build
/build
2 changes: 1 addition & 1 deletion orderfile/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ dependencies {
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
}
2 changes: 1 addition & 1 deletion orderfile/app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ class ExampleInstrumentedTest {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.orderfiledemo", appContext.packageName)
}
}
}
2 changes: 1 addition & 1 deletion orderfile/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
</activity>
</application>

</manifest>
</manifest>
81 changes: 34 additions & 47 deletions orderfile/app/src/main/cpp/orderfile.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include <android/log.h>
#include <errno.h>
#include <jni.h>
#include <linux/limits.h>
#include <stdlib.h>
#include <string.h>

#include <jni.h>

#include <android/log.h>

const char kLogTag[] = "orderfiledemo";

#ifdef GENERATE_PROFILES
Expand All @@ -15,53 +13,42 @@ extern "C" int __llvm_profile_initialize_file(void);
extern "C" int __llvm_orderfile_dump(void);
#endif

void DumpProfileDataIfNeeded(const char* temp_dir) {
void DumpProfileDataIfNeeded(const char *temp_dir) {
#ifdef GENERATE_PROFILES
char profile_location[PATH_MAX] = {};
snprintf(profile_location, sizeof(profile_location), "%s/demo.output", temp_dir);
if (__llvm_profile_set_filename(profile_location) == -1) {
__android_log_print(
ANDROID_LOG_ERROR,
kLogTag,
"__llvm_profile_set_filename(\"%s\") failed: %s",
profile_location,
strerror(errno));
return;
}

if (__llvm_profile_initialize_file() == -1) {
__android_log_print(
ANDROID_LOG_ERROR,
kLogTag,
"__llvm_profile_initialize_file failed: %s",
strerror(errno));
return;
}

if (__llvm_orderfile_dump() == -1) {
__android_log_print(
ANDROID_LOG_ERROR,
kLogTag,
"__llvm_orderfile_dump() failed: %s",
strerror(errno));
return;
}
__android_log_print(
ANDROID_LOG_DEBUG,
kLogTag,
"Wrote profile data to %s",
profile_location);
char profile_location[PATH_MAX] = {};
snprintf(profile_location, sizeof(profile_location), "%s/demo.output",
temp_dir);
if (__llvm_profile_set_filename(profile_location) == -1) {
__android_log_print(ANDROID_LOG_ERROR, kLogTag,
"__llvm_profile_set_filename(\"%s\") failed: %s",
profile_location, strerror(errno));
return;
}

if (__llvm_profile_initialize_file() == -1) {
__android_log_print(ANDROID_LOG_ERROR, kLogTag,
"__llvm_profile_initialize_file failed: %s",
strerror(errno));
return;
}

if (__llvm_orderfile_dump() == -1) {
__android_log_print(ANDROID_LOG_ERROR, kLogTag,
"__llvm_orderfile_dump() failed: %s", strerror(errno));
return;
}
__android_log_print(ANDROID_LOG_DEBUG, kLogTag, "Wrote profile data to %s",
profile_location);
#else
__android_log_print(
ANDROID_LOG_DEBUG, kLogTag,
"Did not write profile data because the app was not built for profile generation");
__android_log_print(ANDROID_LOG_DEBUG, kLogTag,
"Did not write profile data because the app was not "
"built for profile generation");
#endif
}

extern "C" JNIEXPORT void JNICALL
Java_com_example_orderfiledemo_MainActivity_runWorkload(
JNIEnv *env,
jobject /* this */,
jstring temp_dir) {
DumpProfileDataIfNeeded(env->GetStringUTFChars(temp_dir, 0));
Java_com_example_orderfiledemo_MainActivity_runWorkload(JNIEnv *env,
jobject /* this */,
jstring temp_dir) {
DumpProfileDataIfNeeded(env->GetStringUTFChars(temp_dir, 0));
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ class MainActivity : AppCompatActivity() {
System.loadLibrary("orderfiledemo")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
android:strokeWidth="1"
android:strokeColor="#00000000" />
</vector>
</vector>
2 changes: 1 addition & 1 deletion orderfile/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
6 changes: 3 additions & 3 deletions orderfile/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
2 changes: 1 addition & 1 deletion orderfile/app/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
</resources>
30 changes: 22 additions & 8 deletions orderfile/app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
</resources>
<color name="purple_200">
#FFBB86FC
</color>
<color name="purple_500">
#FF6200EE
</color>
<color name="purple_700">
#FF3700B3
</color>
<color name="teal_200">
#FF03DAC5
</color>
<color name="teal_700">
#FF018786
</color>
<color name="black">
#FF000000
</color>
<color name="white">
#FFFFFFFF
</color>
</resources>
6 changes: 4 additions & 2 deletions orderfile/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<resources>
<string name="app_name">Orderfile Demo</string>
</resources>
<string name="app_name">
Orderfile Demo
</string>
</resources>
44 changes: 29 additions & 15 deletions orderfile/app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.OrderfileDemo" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
<!-- Base application theme. -->
<style name="Theme.OrderfileDemo" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">
@color/purple_500
</item>
<item name="colorPrimaryVariant">
@color/purple_700
</item>
<item name="colorOnPrimary">
@color/white
</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">
@color/teal_200
</item>
<item name="colorSecondaryVariant">
@color/teal_700
</item>
<item name="colorOnSecondary">
@color/black
</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">
?attr/colorPrimaryVariant
</item>
<!-- Customize your theme here. -->
</style>
</resources>
2 changes: 1 addition & 1 deletion orderfile/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ buildscript {

task clean(type: Delete) {
delete rootProject.buildDir
}
}
2 changes: 1 addition & 1 deletion orderfile/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ kotlin.code.style=official
android.defaults.buildfeatures.buildconfig=true
android.nonTransitiveRClass=false
android.nonFinalResIds=false
org.gradle.unsafe.configuration-cache=true
org.gradle.unsafe.configuration-cache=true

0 comments on commit 80c27fe

Please sign in to comment.