Skip to content

Commit

Permalink
Refactor perf support
Browse files Browse the repository at this point in the history
- use WAMR_BUILD_PERF_SUPPORT as the compilation control
- use WASM_ENABLE_PERF_SUPPORT as the macro
- use `wamrc --perf-profile` to generate .aot contains fp operations
- use `iwasm --perf-profile` to create perf map for `perf record`
  • Loading branch information
lum1n0us committed Dec 27, 2023
1 parent 8318333 commit 335fa7a
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 36 deletions.
13 changes: 12 additions & 1 deletion build-scripts/config_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,20 @@ elseif (WAMR_BUILD_SANITIZER STREQUAL "asan")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fsanitize=address -fno-sanitize-recover=all" )
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
endif()
elseif (WAMR_BUILD_SANITIZER STREQUAL "tsan")
elseif (WAMR_BUILD_SANITIZER STREQUAL "tsan")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fno-omit-frame-pointer -fsanitize=thread -fno-sanitize-recover=all" )
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
elseif (NOT (WAMR_BUILD_SANITIZER STREQUAL "") )
message(SEND_ERROR "Unsupported sanitizer: ${WAMR_BUILD_SANITIZER}")
endif()

if (WAMR_BUILD_LINUX_PERF EQUAL 1)
if (NOT WAMR_BUILD_JIT AND NOT WAMR_BUILD_AOT)
message(WARNING "only support perf in aot and llvm-jit")
set(WAMR_BUILD_LINUX_PERF 0)
endif ()
endif ()

########################################

message ("-- Build Configurations:")
Expand Down Expand Up @@ -440,3 +447,7 @@ if (WAMR_CONFIGUABLE_BOUNDS_CHECKS EQUAL 1)
add_definitions (-DWASM_CONFIGURABLE_BOUNDS_CHECKS=1)
message (" Configurable bounds checks enabled")
endif ()
if (WAMR_BUILD_LINUX_PERF EQUAL 1)
add_definitions (-DWASM_ENABLE_LINUX_PERF=1)
message (" Enable linux perf support")
endif ()
5 changes: 5 additions & 0 deletions core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,9 @@
#define WASM_MAX_INSTANCE_CONTEXTS 8
#endif

/* linux perf support */
#ifndef WASM_ENABLE_PERF_SUPPORT
#define WASM_ENABLE_PERF_SUPPORT 0
#endif

#endif /* end of _CONFIG_H_ */
56 changes: 56 additions & 0 deletions core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2764,6 +2764,57 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
return ret;
}

#if WASM_ENABLE_LINUX_PERF != 0
static uintptr_t
get_func_addr(const AOTModule *module, uint32 func_idx_adj)
{
return (uintptr_t)module->func_ptrs[func_idx_adj];
}

static uint32
get_func_size(const AOTModule *module, uint32 func_idx_adj)
{
uint32 func_sz;
if (func_idx_adj + 1 < module->func_count) {
func_sz = module->func_ptrs[func_idx_adj + 1]
- module->func_ptrs[func_idx_adj];
}
else {
func_sz = module->code_size
- (module->func_ptrs[func_idx_adj] - module->code);
}

return func_sz;
}

static void
create_perf_map(const AOTModule *module)
{
uint32 i;
pid_t pid = getpid();
char perf_map_info[128] = { 0 };

snprintf(perf_map_info, 128, "/tmp/perf-%d.map", pid);
FILE *perf_map = fopen(perf_map_info, "w");
if (!perf_map) {
LOG_WARNING("can't create /tmp/perf-%d.map, because %s", pid,
strerror(errno));
return;
}

for (i = 0; i < module->func_count; i++) {
memset(perf_map_info, 0, 128);
snprintf(perf_map_info, 128, "%lx %x aot_func#%u\n",
get_func_addr(module, i), get_func_size(module, i), i);

fwrite(perf_map_info, 1, strlen(perf_map_info), perf_map);
}

fclose(perf_map);
LOG_VERBOSE("generate /tmp/perf-%d.map", pid);
}
#endif /* WASM_ENABLE_LINUX_PERF != 0*/

static bool
load_from_sections(AOTModule *module, AOTSection *sections,
bool is_load_from_file_buf, char *error_buf,
Expand Down Expand Up @@ -3224,6 +3275,11 @@ load(const uint8 *buf, uint32 size, AOTModule *module, char *error_buf,
}
#endif

#if WASM_ENABLE_LINUX_PERF != 0
if (wasm_runtime_get_linux_perf())
create_perf_map(module);
#endif

return ret;
fail:
return false;
Expand Down
8 changes: 6 additions & 2 deletions core/iwasm/common/wasm_c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,17 @@ wasm_config_set_mem_alloc_opt(wasm_config_t *config,
return config;
}

#if WASM_ENABLE_LINUX_PERF != 0
wasm_config_t *
wasm_config_set_linux_perf_opt(wasm_config_t *config, bool enable)
{
if (!config)
return NULL;

config->linux_perf_support = enable;
config->enable_linux_perf = enable;
return config;
}
#endif

static void
wasm_engine_delete_internal(wasm_engine_t *engine)
Expand Down Expand Up @@ -380,7 +382,9 @@ wasm_engine_new_internal(wasm_config_t *config)
init_args.mem_alloc_type = config->mem_alloc_type;
memcpy(&init_args.mem_alloc_option, &config->mem_alloc_option,
sizeof(MemAllocOption));
init_args.linux_perf_support = config->linux_perf_support;
#if WASM_ENABLE_LINUX_PERF != 0
init_args.enable_linux_perf = config->enable_linux_perf;
#endif

if (!wasm_runtime_full_init(&init_args)) {
LOG_DEBUG("wasm_runtime_full_init failed");
Expand Down
27 changes: 21 additions & 6 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static JitCompOptions jit_options = { 0 };
#endif

#if WASM_ENABLE_JIT != 0
static LLVMJITOptions llvm_jit_options = { 3, 3, 0, false };
static LLVMJITOptions llvm_jit_options = { 3, 3, 0 };
#endif

static RunningMode runtime_running_mode = Mode_Default;
Expand Down Expand Up @@ -662,14 +662,13 @@ wasm_runtime_full_init(RuntimeInitArgs *init_args)
#endif

#if WASM_ENABLE_JIT != 0
LOG_DEBUG("Start LLVM_JIT, opt_sz=%u, opt_lvl=%u, segue=%s, linux_perf=%s",
init_args->llvm_jit_size_level, init_args->llvm_jit_opt_level,
init_args->segue_flags ? "Yes" : "No",
init_args->linux_perf_support ? "Yes" : "No");
llvm_jit_options.size_level = init_args->llvm_jit_size_level;
llvm_jit_options.opt_level = init_args->llvm_jit_opt_level;
llvm_jit_options.segue_flags = init_args->segue_flags;
llvm_jit_options.linux_perf_support = init_args->linux_perf_support;
#endif

#if WASM_ENABLE_LINUX_PERF != 0
wasm_runtime_set_linux_perf(init_args->enable_linux_perf);
#endif

if (!wasm_runtime_env_init()) {
Expand Down Expand Up @@ -6146,3 +6145,19 @@ wasm_runtime_get_context(WASMModuleInstanceCommon *inst, void *key)
return wasm_native_get_context(inst, key);
}
#endif /* WASM_ENABLE_MODULE_INST_CONTEXT != 0 */

#if WASM_ENABLE_LINUX_PERF != 0
static bool enable_linux_perf = false;

bool
wasm_runtime_get_linux_perf(void)
{
return enable_linux_perf;
}

void
wasm_runtime_set_linux_perf(bool flag)
{
enable_linux_perf = flag;
}
#endif
9 changes: 8 additions & 1 deletion core/iwasm/common/wasm_runtime_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,6 @@ typedef struct LLVMJITOptions {
uint32 opt_level;
uint32 size_level;
uint32 segue_flags;
bool linux_perf_support;
} LLVMJITOptions;
#endif

Expand Down Expand Up @@ -1105,6 +1104,14 @@ wasm_runtime_end_blocking_op(WASMExecEnv *exec_env);
void
wasm_runtime_interrupt_blocking_op(WASMExecEnv *exec_env);

#if WASM_ENABLE_LINUX_PERF != 0
bool
wasm_runtime_get_linux_perf(void);

void
wasm_runtime_set_linux_perf(bool flag);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
14 changes: 9 additions & 5 deletions core/iwasm/compilation/aot_llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2174,7 +2174,7 @@ jit_stack_size_callback(void *user_data, const char *name, size_t namelen,
}

static bool
orc_jit_create(AOTCompContext *comp_ctx, bool linux_perf_support)
orc_jit_create(AOTCompContext *comp_ctx)
{
LLVMErrorRef err;
LLVMOrcLLLazyJITRef orc_jit = NULL;
Expand Down Expand Up @@ -2214,13 +2214,15 @@ orc_jit_create(AOTCompContext *comp_ctx, bool linux_perf_support)
/* Ownership transfer: LLVMOrcLLJITBuilderRef -> LLVMOrcLLJITRef */
builder = NULL;

if (linux_perf_support) {
LOG_DEBUG("Enable linux perf support");
#if WASM_ENABLE_LINUX_PERF != 0
if (wasm_runtime_get_linux_perf()) {
LOG_DEBUG("Enable linux perf support in JIT");
LLVMOrcObjectLayerRef obj_linking_layer =
(LLVMOrcObjectLayerRef)LLVMOrcLLLazyJITGetObjLinkingLayer(orc_jit);
LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener(
obj_linking_layer, LLVMCreatePerfJITEventListener());
}
#endif

/* Ownership transfer: local -> AOTCompContext */
comp_ctx->orc_jit = orc_jit;
Expand Down Expand Up @@ -2320,7 +2322,8 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)
goto fail;
}

if (option->linux_perf_support) {
#if WASM_ENABLE_LINUX_PERF != 0
if (wasm_runtime_get_linux_perf()) {
/* FramePointerKind.All */
LLVMMetadataRef val =
LLVMValueAsMetadata(LLVMConstInt(LLVMInt32Type(), 2, false));
Expand All @@ -2330,6 +2333,7 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)

comp_ctx->emit_frame_pointer = true;
}
#endif

if (BH_LIST_ERROR == bh_list_init(&comp_ctx->native_symbols)) {
goto fail;
Expand Down Expand Up @@ -2434,7 +2438,7 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)
goto fail;

/* Create LLJIT Instance */
if (!orc_jit_create(comp_ctx, option->linux_perf_support))
if (!orc_jit_create(comp_ctx))
goto fail;
}
else {
Expand Down
1 change: 0 additions & 1 deletion core/iwasm/compilation/aot_llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,6 @@ typedef struct AOTCompOption {
uint32 bounds_checks;
uint32 stack_bounds_checks;
uint32 segue_flags;
bool linux_perf_support;
char **custom_sections;
uint32 custom_sections_count;
const char *stack_usage_file;
Expand Down
1 change: 0 additions & 1 deletion core/iwasm/fast-jit/jit_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ typedef struct JitInterpSwitchInfo {
typedef struct JitCompOptions {
uint32 code_cache_size;
uint32 opt_level;
bool linux_perf_support;
} JitCompOptions;

bool
Expand Down
1 change: 0 additions & 1 deletion core/iwasm/include/aot_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ typedef struct AOTCompOption {
uint32_t bounds_checks;
uint32_t stack_bounds_checks;
uint32_t segue_flags;
bool linux_perf_support;
char **custom_sections;
uint32_t custom_sections_count;
const char *stack_usage_file;
Expand Down
8 changes: 6 additions & 2 deletions core/iwasm/include/wasm_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,24 +181,28 @@ typedef union MemAllocOption {
struct wasm_config_t {
mem_alloc_type_t mem_alloc_type;
MemAllocOption mem_alloc_option;
bool linux_perf_support;
#if WASM_ENABLE_LINUX_PERF != 0
bool enable_linux_perf;
#endif
/*TODO: wasi args*/
};

/*
* by default:
* - mem_alloc_type is Alloc_With_System_Allocator
* - mem_alloc_option is all 0
* - linux_perf_support is false
* - enable_linux_perf is false
*/
WASM_API_EXTERN own wasm_config_t* wasm_config_new(void);

// Embedders may provide custom functions for manipulating configs.
WASM_API_EXTERN own wasm_config_t*
wasm_config_set_mem_alloc_opt(wasm_config_t *, mem_alloc_type_t, MemAllocOption *);

#if WASM_ENABLE_LINUX_PERF != 0
WASM_API_EXTERN own wasm_config_t*
wasm_config_set_linux_perf_opt(wasm_config_t *, bool);
#endif

// Engine

Expand Down
10 changes: 6 additions & 4 deletions core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,17 @@ typedef struct RuntimeInitArgs {
uint32_t llvm_jit_size_level;
/* Segue optimization flags for LLVM JIT */
uint32_t segue_flags;
#if WASM_ENABLE_LINUX_PERF != 0
/**
* If enabled
* - llvm-jit will output a jitdump file for `perf inject`
* - aot. TBD
* - aot will output a perf-${pid}.map for `perf record`
* - fast-jit. TBD
* - multi-tier-jit. TBD
* - interpreter. TBD
*/
bool linux_perf_support;
bool enable_linux_perf;
#endif
} RuntimeInitArgs;

#ifndef WASM_VALKIND_T_DEFINED
Expand Down Expand Up @@ -243,8 +245,8 @@ WASM_RUNTIME_API_EXTERN bool
wasm_runtime_full_init(RuntimeInitArgs *init_args);

/**
* Set the log level. To be called after the runtime is initialized.
*
* Set the log level. To be called after the runtime is initialized.
*
* @param level the log level to set
*/
WASM_RUNTIME_API_EXTERN void
Expand Down
1 change: 0 additions & 1 deletion core/iwasm/interpreter/wasm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2892,7 +2892,6 @@ init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
option.opt_level = llvm_jit_options.opt_level;
option.size_level = llvm_jit_options.size_level;
option.segue_flags = llvm_jit_options.segue_flags;
option.linux_perf_support = llvm_jit_options.linux_perf_support;

#if WASM_ENABLE_BULK_MEMORY != 0
option.enable_bulk_memory = true;
Expand Down
1 change: 0 additions & 1 deletion core/iwasm/interpreter/wasm_mini_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1877,7 +1877,6 @@ init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
option.opt_level = llvm_jit_options.opt_level;
option.size_level = llvm_jit_options.size_level;
option.segue_flags = llvm_jit_options.segue_flags;
option.linux_perf_support = llvm_jit_options.linux_perf_support;

#if WASM_ENABLE_BULK_MEMORY != 0
option.enable_bulk_memory = true;
Expand Down
Loading

0 comments on commit 335fa7a

Please sign in to comment.