Skip to content

Commit b7c4786

Browse files
authored
Merge pull request #318 from graalvm/ni-demos-refactoring-14
[GR-60094] Refactor and move native-list-dir demo under native-image category.
2 parents 7e527e2 + dc8662e commit b7c4786

File tree

10 files changed

+110
-226
lines changed

10 files changed

+110
-226
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: native-image/list-files
2+
on:
3+
push:
4+
paths:
5+
- 'native-image/list-files/**'
6+
- '.github/workflows/native-image-list-files.yml'
7+
pull_request:
8+
paths:
9+
- 'native-image/list-files/**'
10+
- '.github/workflows/native-image-list-files.yml'
11+
schedule:
12+
- cron: "0 0 1 * *" # run every month
13+
workflow_dispatch:
14+
permissions:
15+
contents: read
16+
jobs:
17+
run:
18+
name: Run 'native-image/list-files'
19+
runs-on: ubuntu-latest
20+
timeout-minutes: 20
21+
strategy:
22+
matrix:
23+
java-version: ['21', '24-ea']
24+
steps:
25+
- uses: actions/checkout@v4
26+
- uses: graalvm/setup-graalvm@v1
27+
with:
28+
java-version: ${{ matrix.java-version }}
29+
distribution: 'graalvm'
30+
github-token: ${{ secrets.GITHUB_TOKEN }}
31+
native-image-job-reports: 'true'
32+
- name: Run 'native-image/list-files'
33+
run: |
34+
cd native-image/list-files
35+
./run.sh

.github/workflows/native-list-dir.yml

Lines changed: 0 additions & 32 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ cd graalvm-demos
4040
<td align="left" width="70%">Demonstrates how to build a modular Java application into a native executable<br><strong>Technologies: </strong>Native Image, Maven<br><strong>Reference: </strong><a href="https://www.graalvm.org/dev/reference-manual/native-image/guides/build-java-modules-into-native-executable/">Build Java Modules into a Native Executable</a></td>
4141
</tr>
4242
<tr>
43-
<td align="left" width="30%"><a href="/native-list-dir/">native-list-dir</a><br><a href="https://github.com/graalvm/graalvm-demos/actions/workflows/native-list-dir.yml"><img alt="native-list-dir" src="https://github.com/graalvm/graalvm-demos/actions/workflows/native-list-dir.yml/badge.svg" /></a></td>
43+
<td align="left" width="30%"><a href="/native-image/list-files/">native-image/list-files</a><br><a href="https://github.com/graalvm/graalvm-demos/actions/workflows/native-image-list-files.yml"><img alt="native-image/list-files" src="https://github.com/graalvm/graalvm-demos/actions/workflows/native-image-list-files.yml/badge.svg"/></a></td>
4444
<td align="left" width="70%">Demonstrates how to compile a CLI application into a native executable and then apply Profile-Guided Optimizations (PGO) for more performance gains<br><strong>Technologies: </strong>Native Image, PGO
4545
</tr>
4646
<tr>

native-image/list-files/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.class
2+
.iprof
3+
listdir
4+
listdir-instrumented
5+
listdir-optimized

native-list-dir/ListDir.java renamed to native-image/list-files/ListDir.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -67,4 +67,4 @@ public static void main(String[] args) throws java.io.IOException {
6767

6868
System.out.println("Total: " + count[0] + " files, total size = " + size[0] + " bytes");
6969
}
70-
}
70+
}

native-image/list-files/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Create a Native Executable and Apply Profile-Guided Optimization
2+
3+
This demo shows how to create a native executable with GraalVM Native Image and apply Profile-Guided Optimization (PGO) for more performance gains.
4+
It uses the Java application that counts files and their sizes in a specified directory.
5+
6+
## Run the Demo
7+
8+
1. Compile the application:
9+
```bash
10+
javac ListDir.java
11+
```
12+
13+
2. Generate a native executable from the class file:
14+
```bash
15+
native-image ListDir
16+
```
17+
The executable file _listdir_ is created in the directory.
18+
19+
3. Run the application by passing it a directory that actually contains some files, for example `..` (to count files in the parent of the current directory, containing all the demos in this repository). Check the time spent with the `time` utility.
20+
- On HotSpot:
21+
```bash
22+
time java ListDir ..
23+
```
24+
- From a native executable:
25+
```bash
26+
time ./listdir ..
27+
```
28+
29+
## Enable PGO
30+
31+
For more performance gains, you can can apply [Profile-Guided Optimization (PGO)](https://www.graalvm.org/latest/reference-manual/native-image/optimizations-and-performance/PGO/). (Not available with GraalVM Community Edition.)
32+
With PGO you can collect the profiling data, and then feed it to the `native-image` tool, which will use this information to further optimize the performance of the resulting executable.
33+
34+
1. Build an instrumented image from the _ListDir_ class and run it to collect profiles, specifying a different name for the native executable, for example _listdir-instrumented_:
35+
36+
```shell
37+
native-image --pgo-instrument ListDir -o listdir-instrumented
38+
```
39+
```bash
40+
./listdir-instrumented ..
41+
```
42+
Profiles collected from this run are now stored in the `default.iprof` file.
43+
44+
2. Use the profiles to build an optimized native executable, giving it a different name than in the previous runs:
45+
```shell
46+
native-image --pgo ListDir -o listdir-optimized
47+
```
48+
49+
3. Run that optimized executable:
50+
```shell
51+
time ./listdir-optimized ..
52+
```
53+
54+
Find more examples at the website:
55+
- [Optimize a Native Executable with Profile-Guided Optimizations](https://www.graalvm.org/latest/reference-manual/native-image/guides/optimize-native-executable-with-pgo/).
56+
- [Basic Usage of Profile-Guided Optimization](https://www.graalvm.org/latest/reference-manual/native-image/optimizations-and-performance/PGO/basic-usage/)

native-image/list-files/run.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
set -ex
3+
4+
javac ListDir.java
5+
native-image ListDir
6+
./listdir ..
7+
8+
native-image -Ob --pgo-instrument ListDir -o listdir-instrumented
9+
./listdir-instrumented ..
10+
native-image -Ob --pgo ListDir -o listdir-optimized
11+
./listdir-optimized ..

native-list-dir/ExtListDir.java

Lines changed: 0 additions & 74 deletions
This file was deleted.

native-list-dir/README.md

Lines changed: 0 additions & 101 deletions
This file was deleted.

native-list-dir/build.sh

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)