Skip to content

Commit 8c1269d

Browse files
authored
trans_wasm_func_name.py: Correct function index during translation (bytecodealliance#3232)
Adding the N from "aot_func#N" with the import function count is the correct wasm function index.
1 parent ff296c1 commit 8c1269d

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

doc/perf_tune.md

+14-9
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,22 @@ You should only use this method for well tested wasm applications and make sure
9898
Linux perf is a powerful tool to analyze the performance of a program, developer can use it to find the hot functions and optimize them. It is one profiler supported by WAMR. In order to use it, you need to add `--perf-profile` while running _iwasm_. By default, it is disabled.
9999
100100
> [!CAUTION]
101-
> For now, only llvm-jit mode supports linux-perf.
101+
> For now, only llvm-jit mode and aot mode supports linux-perf.
102102
103103
Here is a basic example, if there is a Wasm application _foo.wasm_, you'll execute.
104104
105105
```
106-
$ perf record --output=perf.data.raw -- iwasm --perf-profile foo.wasm
106+
$ perf record --output=perf.data.raw -- iwasm --enable-linux-perf foo.wasm
107107
```
108108
109-
This will create a _perf.data_ and a _jit-xxx.dump_ under _~/.debug.jit/_ folder. This extra file is WAMR generated at runtime, and it contains the mapping between the JIT code and the original Wasm function names.
109+
This will create a _perf.data_ and
110+
- a _jit-xxx.dump_ under _~/.debug/jit/_ folder if running llvm-jit mode
111+
- or _/tmp/perf-<pid>.map_ if running AOT mode
110112
111-
The next thing need to do is to merge _jit-xxx.dump_ file into the _perf.data_.
113+
114+
This file is WAMR generated. It contains information which includes jitted(precompiled) code addresses in memory, names of jitted (precompiled) functions which are named as *aot_func#N* and so on.
115+
116+
If running with llvm-jit mode, the next thing is to merge _jit-xxx.dump_ file into the _perf.data_.
112117
113118
```
114119
$ perf inject --jit --input=perf.data.raw --output=perf.data
@@ -141,28 +146,28 @@ $ perf report --input=perf.data
141146
[Flamegraph](https://www.brendangregg.com/flamegraphs.html) is a powerful tool to visualize stack traces of profiled software so that the most frequent code-paths can be identified quickly and accurately. In order to use it, you need to [capture graphs](https://github.com/brendangregg/FlameGraph#1-capture-stacks) when running `perf record`
142147
143148
```
144-
$ perf record -k mono --call-graph=fp --output=perf.data.raw -- iwasm --perf-profile foo.wasm
149+
$ perf record -k mono --call-graph=fp --output=perf.data.raw -- iwasm --enable-linux-perf foo.wasm
145150
```
146151
147-
merge the _jit-xxx.dump_ file into the _perf.data.raw_.
152+
If running with llvm-jit mode, merge the _jit-xxx.dump_ file into the _perf.data.raw_.
148153
149154
```
150155
$ perf inject --jit --input=perf.data.raw --output=perf.data
151156
```
152157
153-
generate the stack trace file.
158+
Generate the stack trace file.
154159
155160
```
156161
$ perf script > out.perf
157162
```
158163
159-
[fold stacks](https://github.com/brendangregg/FlameGraph#2-fold-stacks).
164+
[Fold stacks](https://github.com/brendangregg/FlameGraph#2-fold-stacks).
160165
161166
```
162167
$ ./FlameGraph/stackcollapse-perf.pl out.perf > out.folded
163168
```
164169
165-
[render a flamegraph](https://github.com/brendangregg/FlameGraph#3-flamegraphpl)
170+
[Render a flamegraph](https://github.com/brendangregg/FlameGraph#3-flamegraphpl)
166171
167172
```
168173
$ ./FlameGraph/flamegraph.pl out.folded > perf.foo.wasm.svg

test-tools/trans-jitted-func-name/trans_wasm_func_name.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def collect_import_section_content(wasm_objdump_bin: Path, wasm_file: Path) -> d
6868
)
6969

7070
if p.stderr:
71+
print("No content in import section")
7172
return {}
7273

7374
import_section = {}
@@ -77,17 +78,19 @@ def collect_import_section_content(wasm_objdump_bin: Path, wasm_file: Path) -> d
7778
if not line:
7879
continue
7980

80-
if line.startswith(" - func"):
81-
import_section.update("function", import_section.get("function", 0) + 1)
81+
if re.search(r"^-\s+func", line):
82+
import_section.update(function=import_section.get("function", 0) + 1)
8283
else:
8384
pass
8485

86+
assert len(import_section) > 0, "failed to retrive content of import section"
8587
return import_section
8688

8789

8890
def collect_name_section_content(wasm_objdump_bin: Path, wasm_file: Path) -> dict:
8991
"""
90-
execute "wasm_objdump_bin -j name -x wasm_file" and store the output in a list
92+
execute "wasm_objdump_bin -j name -x wasm_file" and store the output in a dict
93+
{1: xxxx, 2: yyyy, 3: zzzz}
9194
"""
9295
assert wasm_objdump_bin.exists()
9396
assert wasm_file.exists()
@@ -117,7 +120,7 @@ def collect_name_section_content(wasm_objdump_bin: Path, wasm_file: Path) -> dic
117120
assert m
118121

119122
func_index, func_name = m.groups()
120-
name_section.update({func_index: func_name})
123+
name_section.update({int(func_index): func_name})
121124

122125
assert name_section
123126
return name_section
@@ -162,7 +165,7 @@ def replace_function_name(
162165
new_line.append(sym)
163166
continue
164167

165-
func_idx = m.groups()[-1]
168+
func_idx = int(m.groups()[-1]) + import_function_count
166169
if func_idx in name_section:
167170
wasm_func_name = f"[Wasm] {name_section[func_idx]}"
168171
else:

0 commit comments

Comments
 (0)