Enable static PGO for Linux SGX (#2270)

Enable static PGO for Linux SGX and update the related benchmarks
test scripts and documents.
This commit is contained in:
TianlongLiang 2023-06-09 14:13:43 +08:00 committed by GitHub
parent cabcb177c8
commit 2f01cb7b7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 209 additions and 15 deletions

View File

@ -89,6 +89,11 @@ if (NOT DEFINED WAMR_BUILD_SGX_IPFS)
set (WAMR_BUILD_SGX_IPFS 0)
endif ()
if (NOT DEFINED WAMR_BUILD_STATIC_PGO)
# Disable static PGO by default
set (WAMR_BUILD_STATIC_PGO 0)
endif ()
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11 -ffunction-sections -fdata-sections \
-Wall -Wno-unused-parameter -Wno-pedantic \
@ -107,6 +112,18 @@ add_custom_command (
add_custom_target (vmlib_untrusted ALL DEPENDS libvmlib_untrusted.a)
if ((WAMR_BUILD_STATIC_PGO EQUAL 1) AND (WAMR_BUILD_AOT EQUAL 1))
execute_process(
COMMAND bash -c "sed -i -E 's/^WAMR_BUILD_STATIC_PGO = 0/WAMR_BUILD_STATIC_PGO = 1/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Makefile"
OUTPUT_VARIABLE cmdOutput
)
else()
execute_process(
COMMAND bash -c "sed -i -E 's/^WAMR_BUILD_STATIC_PGO = 1/WAMR_BUILD_STATIC_PGO = 0/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Makefile"
OUTPUT_VARIABLE cmdOutput
)
endif()
if (DEFINED WAMR_BUILD_GLOBAL_HEAP_POOL)
execute_process(
COMMAND bash -c "sed -i -E 's/^WAMR_BUILD_GLOBAL_HEAP_POOL = .*/WAMR_BUILD_GLOBAL_HEAP_POOL = ${WAMR_BUILD_GLOBAL_HEAP_POOL}/g' ${CMAKE_CURRENT_SOURCE_DIR}/enclave-sample/Makefile"

View File

@ -232,6 +232,9 @@ print_help()
printf(" for example:\n");
printf(" --addr-pool=1.2.3.4/15,2.3.4.5/16\n");
printf(" --max-threads=n Set maximum thread number per cluster, default is 4\n");
#if WASM_ENABLE_STATIC_PGO != 0
printf(" --gen-prof-file=<path> Generate LLVM PGO (Profile-Guided Optimization) profile file\n");
#endif
printf(" --version Show version information\n");
return 1;
}
@ -294,6 +297,10 @@ typedef enum EcallCmd {
CMD_SET_WASI_ARGS, /* wasm_runtime_set_wasi_args() */
CMD_SET_LOG_LEVEL, /* bh_log_set_verbose_level() */
CMD_GET_VERSION, /* wasm_runtime_get_version() */
#if WASM_ENABLE_STATIC_PGO != 0
CMD_GET_PGO_PROF_BUF_SIZE, /* wasm_runtime_get_pro_prof_data_size() */
CMD_DUMP_PGO_PROF_BUF_DATA, /* wasm_runtime_dump_pgo_prof_data_to_buf() */
#endif
} EcallCmd;
static void
@ -598,6 +605,64 @@ get_version(uint64_t *major, uint64_t *minor, uint64_t *patch)
*patch = ecall_args[2];
}
#if WASM_ENABLE_STATIC_PGO != 0
static void
dump_pgo_prof_data(void *module_inst, const char *path)
{
char *buf;
uint32_t len;
FILE *file;
uint64_t ecall_args[1];
ecall_args[0] = (uint64_t)(uintptr_t)module_inst;
if (SGX_SUCCESS
!= ecall_handle_command(g_eid, CMD_GET_PGO_PROF_BUF_SIZE,
(uint8_t *)ecall_args, sizeof(ecall_args))) {
printf("Call ecall_handle_command() failed.\n");
return;
}
if (!(len = ecall_args[0])) {
printf("failed to get LLVM PGO profile data size\n");
return;
}
if (!(buf = (char *)malloc(len))) {
printf("allocate memory failed\n");
return;
}
uint64_t ecall_args_2[3];
ecall_args_2[0] = (uint64_t)(uintptr_t)module_inst;
ecall_args_2[1] = (uint64_t)(uintptr_t)buf;
ecall_args_2[2] = len;
if (SGX_SUCCESS
!= ecall_handle_command(g_eid, CMD_DUMP_PGO_PROF_BUF_DATA,
(uint8_t *)ecall_args_2,
sizeof(ecall_args_2))) {
printf("Call ecall_handle_command() failed.\n");
free(buf);
return;
}
if (!(len = ecall_args_2[0])) {
printf("failed to dump LLVM PGO profile data\n");
free(buf);
return;
}
if (!(file = fopen(path, "wb"))) {
printf("failed to create file %s", path);
free(buf);
return;
}
fwrite(buf, len, 1, file);
fclose(file);
free(buf);
printf("LLVM raw profile file %s was generated.\n", path);
}
#endif
int
main(int argc, char *argv[])
{
@ -619,6 +684,9 @@ main(int argc, char *argv[])
const char *addr_pool[8] = { NULL };
uint32_t addr_pool_size = 0;
uint32_t max_thread_num = 4;
#if WASM_ENABLE_STATIC_PGO != 0
const char *gen_prof_file = NULL;
#endif
if (enclave_init(&g_eid) < 0) {
std::cout << "Fail to initialize enclave." << std::endl;
@ -718,6 +786,13 @@ main(int argc, char *argv[])
return print_help();
max_thread_num = atoi(argv[0] + 14);
}
#if WASM_ENABLE_STATIC_PGO != 0
else if (!strncmp(argv[0], "--gen-prof-file=", 16)) {
if (argv[0][16] == '\0')
return print_help();
gen_prof_file = argv[0] + 16;
}
#endif
else if (!strncmp(argv[0], "--version", 9)) {
uint64_t major = 0, minor = 0, patch = 0;
get_version(&major, &minor, &patch);
@ -779,6 +854,11 @@ main(int argc, char *argv[])
else
app_instance_main(wasm_module_inst, argc, argv);
#if WASM_ENABLE_STATIC_PGO != 0
if (gen_prof_file)
dump_pgo_prof_data(wasm_module_inst, gen_prof_file);
#endif
ret = 0;
/* Deinstantiate module */
@ -836,7 +916,7 @@ wamr_pal_create_process(struct wamr_pal_create_process_args *args)
int stdoutfd = -1;
int stderrfd = -1;
int argc = 2;
const int argc = 2;
char *argv[argc] = { (char *)"./iwasm", (char *)args->argv[0] };
uint8_t *wasm_files_buf = NULL;

View File

@ -49,6 +49,10 @@ typedef enum EcallCmd {
CMD_SET_WASI_ARGS, /* wasm_runtime_set_wasi_args() */
CMD_SET_LOG_LEVEL, /* bh_log_set_verbose_level() */
CMD_GET_VERSION, /* wasm_runtime_get_version() */
#if WASM_ENABLE_STATIC_PGO != 0
CMD_GET_PGO_PROF_BUF_SIZE, /* wasm_runtime_get_pro_prof_data_size() */
CMD_DUMP_PGO_PROF_BUF_DATA, /* wasm_runtime_dump_pgo_prof_data_to_buf() */
#endif
} EcallCmd;
typedef struct EnclaveModule {
@ -597,6 +601,36 @@ handle_cmd_get_version(uint64 *args, uint32 argc)
args[2] = patch;
}
#if WASM_ENABLE_STATIC_PGO != 0
static void
handle_cmd_get_pgo_prof_buf_size(uint64 *args, int32 argc)
{
wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args;
uint32 buf_len;
bh_assert(argc == 1);
buf_len = wasm_runtime_get_pgo_prof_data_size(module_inst);
args[0] = buf_len;
}
static void
handle_cmd_get_pro_prof_buf_data(uint64 *args, int32 argc)
{
uint64 *args_org = args;
wasm_module_inst_t module_inst = *(wasm_module_inst_t *)args++;
char *buf = *(char **)args++;
uint32 len = *(uint32 *)args++;
uint32 bytes_dumped;
bh_assert(argc == 3);
bytes_dumped =
wasm_runtime_dump_pgo_prof_data_to_buf(module_inst, buf, len);
args_org[0] = bytes_dumped;
}
#endif
void
ecall_handle_command(unsigned cmd, unsigned char *cmd_buf,
unsigned cmd_buf_size)
@ -647,6 +681,14 @@ ecall_handle_command(unsigned cmd, unsigned char *cmd_buf,
case CMD_GET_VERSION:
handle_cmd_get_version(args, argc);
break;
#if WASM_ENABLE_STATIC_PGO != 0
case CMD_GET_PGO_PROF_BUF_SIZE:
handle_cmd_get_pgo_prof_buf_size(args, argc);
break;
case CMD_DUMP_PGO_PROF_BUF_DATA:
handle_cmd_get_pro_prof_buf_data(args, argc);
break;
#endif
default:
LOG_ERROR("Unknown command %d\n", cmd);
break;

View File

@ -15,6 +15,7 @@ WAMR_BUILD_SGX_IPFS = 0
WAMR_BUILD_LIB_RATS = 0
WAMR_BUILD_GLOBAL_HEAP_POOL = 0
WAMR_BUILD_GLOBAL_HEAP_SIZE = 10485760
WAMR_BUILD_STATIC_PGO = 0
VMLIB_BUILD_DIR ?= $(CURDIR)/../build
LIB_RATS_SRC ?= $(VMLIB_BUILD_DIR)/_deps/librats-build
@ -65,7 +66,7 @@ ifeq ($(WAMR_BUILD_LIB_RATS), 1)
App_Include_Paths += -I$(LIB_RATS_INCLUDE_DIR)
endif
App_C_Flags := $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes $(App_Include_Paths)
App_C_Flags := $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes $(App_Include_Paths) -DWASM_ENABLE_STATIC_PGO=$(WAMR_BUILD_STATIC_PGO)
# Three configuration modes - Debug, prerelease, release
# Debug - Macro DEBUG enabled.
@ -134,7 +135,7 @@ ifeq ($(WAMR_BUILD_LIB_RATS), 1)
Enclave_Include_Paths += -I$(LIB_RATS_INCLUDE_DIR) -I$(SGX_SSL)/include
endif
Enclave_C_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector $(Enclave_Include_Paths) -DWASM_GLOBAL_HEAP_SIZE=$(WAMR_BUILD_GLOBAL_HEAP_SIZE) -DWASM_ENABLE_GLOBAL_HEAP_POOL=$(WAMR_BUILD_GLOBAL_HEAP_POOL) -DWASM_ENABLE_LIB_RATS=$(WAMR_BUILD_LIB_RATS)
Enclave_C_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector $(Enclave_Include_Paths) -DWASM_GLOBAL_HEAP_SIZE=$(WAMR_BUILD_GLOBAL_HEAP_SIZE) -DWASM_ENABLE_GLOBAL_HEAP_POOL=$(WAMR_BUILD_GLOBAL_HEAP_POOL) -DWASM_ENABLE_LIB_RATS=$(WAMR_BUILD_LIB_RATS) -DWASM_ENABLE_STATIC_PGO=$(WAMR_BUILD_STATIC_PGO)
ifeq ($(SPEC_TEST), 1)
Enclave_C_Flags += -DWASM_ENABLE_SPEC_TEST=1
else

View File

@ -19,3 +19,7 @@ And then run `./build.sh` to build the source code, file `coremark.exe`, `corema
Run `./run.sh` to test the benchmark, the native mode, iwasm aot mode and iwasm interpreter mode will be tested respectively.
Run `./test_pgo.sh` to test the benchmark with AOT static PGO (Profile-Guided Optimization) enabled, please refer [here](../README.md#install-llvm-profdata) to install tool `llvm-profdata` and build `iwasm` with `cmake -DWAMR_BUILD_STATIC_PGO=1`.
- For Linux, build `iwasm` with `cmake -DWAMR_BUILD_STATIC_PGO=1`, then run `./test_pgo.sh` to test the benchmark with AOT static PGO (Profile-Guided Optimization) enabled.
- For Linux-sgx, similarly, build `iwasm` with `cmake -DWAMR_BUILD_STATIC_PGO=1`, then `make` in the directory `enclave-sample`. And run `./test_pgo.sh --sgx` to test the benchmark.

View File

@ -5,8 +5,13 @@
PLATFORM=$(uname -s | tr A-Z a-z)
if [ "$1" = "--sgx" ] && [ "$PLATFORM" = "linux" ]; then
IWASM="../../../product-mini/platforms/${PLATFORM}-sgx/enclave-sample/iwasm"
WAMRC="../../../wamr-compiler/build/wamrc -sgx"
else
IWASM="../../../product-mini/platforms/${PLATFORM}/build/iwasm"
WAMRC="../../../wamr-compiler/build/wamrc"
fi
if [ ! -e "coremark.wasm" ]; then
echo "coremark.wasm doesn't exist, please run build.sh first"

View File

@ -5,8 +5,13 @@
PLATFORM=$(uname -s | tr A-Z a-z)
if [ "$1" = "--sgx" ] && [ "$PLATFORM" = "linux" ]; then
IWASM="../../../product-mini/platforms/${PLATFORM}-sgx/enclave-sample/iwasm"
WAMRC="../../../wamr-compiler/build/wamrc -sgx"
else
IWASM="../../../product-mini/platforms/${PLATFORM}/build/iwasm"
WAMRC="../../../wamr-compiler/build/wamrc"
fi
if [ ! -e "dhrystone.wasm" ]; then
echo "dhrystone.wasm doesn't exist, please run build.sh first"

View File

@ -29,3 +29,7 @@ And then run `./build.sh` to build the source code, the folder `out` will be cre
Run `./run_aot.sh` to test the benchmark, the native mode and iwasm aot mode will be tested for each workload, and the file `report.txt` will be generated.
Run `./test_pgo.sh` to test the benchmark with AOT static PGO (Profile-Guided Optimization) enabled, please refer [here](../README.md#install-llvm-profdata) to install tool `llvm-profdata` and build `iwasm` with `cmake -DWAMR_BUILD_STATIC_PGO=1`.
- For Linux, build `iwasm` with `cmake -DWAMR_BUILD_STATIC_PGO=1`, then run `./test_pgo.sh` to test the benchmark with AOT static PGO (Profile-Guided Optimization) enabled.
- For Linux-sgx, similarly, build `iwasm` with `cmake -DWAMR_BUILD_STATIC_PGO=1`, then `make` in the directory `enclave-sample`. And run `./test_pgo.sh --sgx` to test the benchmark.

View File

@ -9,8 +9,13 @@ REPORT=$CUR_DIR/report.txt
TIME=/usr/bin/time
PLATFORM=$(uname -s | tr A-Z a-z)
IWASM_CMD=$CUR_DIR/../../../product-mini/platforms/${PLATFORM}/build/iwasm
WAMRC_CMD=$CUR_DIR/../../../wamr-compiler/build/wamrc
if [ "$1" = "--sgx" ] && [ "$PLATFORM" = "linux" ]; then
IWASM_CMD="$CUR_DIR/../../../product-mini/platforms/${PLATFORM}-sgx/enclave-sample/iwasm"
WAMRC_CMD="$CUR_DIR/../../../wamr-compiler/build/wamrc -sgx"
else
IWASM_CMD="$CUR_DIR/../../../product-mini/platforms/${PLATFORM}/build/iwasm"
WAMRC_CMD="$CUR_DIR/../../../wamr-compiler/build/wamrc"
fi
BENCH_NAME_MAX_LEN=20

View File

@ -18,6 +18,12 @@ And then run `./build.sh` to build the source code, the libsodium source code wi
Run `./run_aot.sh` to test the benchmark, the native mode and iwasm aot mode will be tested respectively.
Run `./test_pgo.sh` to test the benchmark with AOT static PGO (Profile-Guided Optimization) enabled, please refer [here](../README.md#install-llvm-profdata) to install tool `llvm-profdata` and build `iwasm` with `cmake -DWAMR_BUILD_STATIC_PGO=1`.
- For Linux, build `iwasm` with `cmake -DWAMR_BUILD_STATIC_PGO=1`, then run `./test_pgo.sh` to test the benchmark with AOT static PGO (Profile-Guided Optimization) enabled.
- For Linux-sgx, similarly, build `iwasm` with `cmake -DWAMR_BUILD_STATIC_PGO=1`, then `make` in the directory `enclave-sample`. And run `./test_pgo.sh --sgx` to test the benchmark.
# Others
Refer to [Performance of WebAssembly runtimes in 2023](https://00f.net/2023/01/04/webassembly-benchmark-2023) for more about the performance comparison of wasm runtimes on running the libsodium benchmarks.

View File

@ -19,8 +19,13 @@ PLATFORM=$(uname -s | tr A-Z a-z)
readonly OUT_DIR=$PWD/libsodium/zig-out/bin
readonly REPORT=$PWD/report.txt
readonly IWASM_CMD=$PWD/../../../product-mini/platforms/${PLATFORM}/build/iwasm
readonly WAMRC_CMD=$PWD/../../../wamr-compiler/build/wamrc
if [ "$1" = "--sgx" ] && [ "$PLATFORM" = "linux" ]; then
readonly IWASM_CMD="$PWD/../../../product-mini/platforms/${PLATFORM}-sgx/enclave-sample/iwasm"
readonly WAMRC_CMD="$PWD/../../../wamr-compiler/build/wamrc -sgx"
else
readonly IWASM_CMD="$PWD/../../../product-mini/platforms/${PLATFORM}/build/iwasm"
readonly WAMRC_CMD="$PWD/../../../wamr-compiler/build/wamrc"
fi
readonly TIME=/usr/bin/time
BENCH_NAME_MAX_LEN=20

View File

@ -19,3 +19,9 @@ And then run `./build.sh` to build the source code, the folder `out` will be cre
Run `./run_aot.sh` to test the benchmark, the native mode and iwasm aot mode will be tested for each workload, and the file `report.txt` will be generated.
Run `./run_interp.sh` to test the benchmark, the native mode and iwasm interpreter mode will be tested for each workload, and the file `report.txt` will be generated.
Run `./test_pgo.sh` to test the benchmark with AOT static PGO (Profile-Guided Optimization) enabled, please refer [here](../README.md#install-llvm-profdata) to install tool `llvm-profdata` and build `iwasm` with `cmake -DWAMR_BUILD_STATIC_PGO=1`.
- For Linux, build `iwasm` with `cmake -DWAMR_BUILD_STATIC_PGO=1`, then run `./test_pgo.sh` to test the benchmark with AOT static PGO (Profile-Guided Optimization) enabled.
- For Linux-sgx, similarly, build `iwasm` with `cmake -DWAMR_BUILD_STATIC_PGO=1`, then `make` in the directory `enclave-sample`. And run `./test_pgo.sh --sgx` to test the benchmark.

View File

@ -9,8 +9,13 @@ REPORT=$CUR_DIR/report.txt
TIME=/usr/bin/time
PLATFORM=$(uname -s | tr A-Z a-z)
IWASM_CMD=$CUR_DIR/../../../product-mini/platforms/${PLATFORM}/build/iwasm
WAMRC_CMD=$CUR_DIR/../../../wamr-compiler/build/wamrc
if [ "$1" = "--sgx" ] && [ "$PLATFORM" = "linux" ]; then
IWASM_CMD="$CUR_DIR/../../../product-mini/platforms/${PLATFORM}-sgx/enclave-sample/iwasm"
WAMRC_CMD="$CUR_DIR/../../../wamr-compiler/build/wamrc -sgx"
else
IWASM_CMD="$CUR_DIR/../../../product-mini/platforms/${PLATFORM}/build/iwasm"
WAMRC_CMD="$CUR_DIR/../../../wamr-compiler/build/wamrc"
fi
BENCH_NAME_MAX_LEN=20

View File

@ -21,3 +21,7 @@ Run `./run_aot.sh` to test the benchmark, the native mode and iwasm aot mode wil
Run `./run_interp.sh` to test the benchmark, the native mode and iwasm interpreter mode will be tested for each workload, and the file `report.txt` will be generated.
Run `./test_pgo.sh` to test the benchmark with AOT static PGO (Profile-Guided Optimization) enabled, please refer [here](../README.md#install-llvm-profdata) to install tool `llvm-profdata` and build `iwasm` with `cmake -DWAMR_BUILD_STATIC_PGO=1`.
- For Linux, build `iwasm` with `cmake -DWAMR_BUILD_STATIC_PGO=1`, then run `./test_pgo.sh` to test the benchmark with AOT static PGO (Profile-Guided Optimization) enabled.
- For Linux-sgx, similarly, build `iwasm` with `cmake -DWAMR_BUILD_STATIC_PGO=1`, then `make` in the directory `enclave-sample`. And run `./test_pgo.sh --sgx` to test the benchmark.

View File

@ -9,8 +9,13 @@ REPORT=$CUR_DIR/report.txt
TIME=/usr/bin/time
PLATFORM=$(uname -s | tr A-Z a-z)
IWASM_CMD=$CUR_DIR/../../../product-mini/platforms/${PLATFORM}/build/iwasm
WAMRC_CMD=$CUR_DIR/../../../wamr-compiler/build/wamrc
if [ "$1" = "--sgx" ] && [ "$PLATFORM" = "linux" ]; then
IWASM_CMD="$CUR_DIR/../../../product-mini/platforms/${PLATFORM}-sgx/enclave-sample/iwasm"
WAMRC_CMD="$CUR_DIR/../../../wamr-compiler/build/wamrc -sgx"
else
IWASM_CMD="$CUR_DIR/../../../product-mini/platforms/${PLATFORM}/build/iwasm"
WAMRC_CMD="$CUR_DIR/../../../wamr-compiler/build/wamrc"
fi
BENCH_NAME_MAX_LEN=20