唐文駿
The primary goal of this project is to provide a visual representation of the behavior of RISC-V programs while incorporating hardware performance estimations, enabling deeper insights into program execution and system performance.
ama-riscv-sim use ./run_analysis.py to implement visulization.
This code aims to visualize the behavior of RISC-V instruction execution and memory access patterns through charts and data analysis. Below are the main functionalities implemented in the code, along with explanations.
The program parses user-provided command-line arguments and performs corresponding actions based on the parameters, such as processing instruction logs or execution trace files.
The provided execution trace file is read and parsed into a format suitable for analysis. Converts binary trace files into a DataFrame for further processing and analysis.
Two main types of visualizations are generated: a histogram of instruction execution frequency and a time series chart of instruction execution behavior.
Shows the dynamic changes in instruction execution during the program's runtime, enabling analysis of instruction access patterns.
what file record dynamic changed?
The main function orchestrates the above functionalities and, based on user options, saves the generated data or charts to files.
problem 1: can't make under src
The file cxxopts.hpp was not found because the
external/cxxopts
submodule might not have been initialized. cxxopts is a C++ library for handling command-line parameters and is usually included in projects as a git submodule.
Solution: Initialize git submodule and add cxxopts as a submodule:**
problem 2: In
cache.h
, there is an unused private member variablemem
.
In this project, the purpose of themem
pointer is to enable thecache
class to interact with the main memory. This is evident from the following key points in the code:
When the cache requires a write-back, dirty data needs to be written back to the main memory:
However, these operations are surrounded by conditional compilation directives:
This indicates that the
mem
pointer is only used in functional simulation mode (CACHE_MODE_FUNC
).
Solution:
First, add themem
member variable incache.h
:
Modify the constructor in
cache.cpp
:
Update the initialization in
main_memory.cpp
:
issue 2: can't build the sw
solution 2: manually build the main.c under
/sw/baremetal/
byriscv64-unknown-elf-gcc
Don't put screenshots which contain plaintext only.
and we would have following file for step 2.
exec.log records the detailed execution process of the program (timeline).
inst_profiler.json captures the usage frequency of various instructions.
hw_stats.json logs hardware performance metrics (e.g., cache hit rates).
figure 1 : instructions profiled
figure 2 : PC frequency profiled
We need to identify how the simulator generates the ISA count JSON file. Once this is clarified, we can adapt the approach for RV32emu. By applying the same Python script, we can visualize the instructions effectively.
exec.log / inst_profiler.json /hw_stats.json
these 3 simulation log file was produce by profilers.cpp
When executing ./ama-riscv-sim
to start the simulator, the program enters main()
and creates the memory and core:
The core
begins executing the program. Inside the core
class, the profiler
is initialized:
Key Points:
profiler
maintains counters for each instruction type and execution count.The profiler
records instruction types and counts during execution:
Key Points:
profiler
tracks all executed instructions and branch instruction details, including directions and taken counts.When the program completes execution, the profiler
outputs statistics:
inst_profiler.json
: Contains execution statistics for all instructions, including detailed branch prediction information.rv32emu already have it's own visulization, i want to know how it work, and apply python code on it.
rv_histogram
We will compile tests/nqueens.c
into an ELF file and analyze it using rv_histogram
. The steps are as follows:
issue 1:
pocoloco@wenjuntangdeMacBook-Pro-2 rv32emu % build/rv_histogram -a nqueens.elf
Failed to open nqueens.elf
solution:
From the code insrc/elf.c
, it is clear that the simulator expects a 32-bit RISC-V ELF file:
Recompile using RV32:
Compile nqueens.c
into an ELF file:
Analyze it using rv_histogram
:
figure 3: build/rv_histogram -a nqueens.elf
figure 4: build/rv_histogram -r nqueens.elf
The primary goal of rv_histogram.c
is to parse ELF files, collect and analyze the usage frequency of RV32 instructions or registers, and finally visualize the results using histograms.
The program begins by parsing command-line arguments using the parse_args()
function. It determines the target ELF file and whether to perform analysis on registers or instructions.
In the main()
function, the program loads the ELF file and extracts its program sections using the following logic:
The program iterates through each section in the ELF file, checking if the section type (sh_type
) is SHT_PROGBITS
and if the section contains executable instructions (sh_flags
with SHF_EXECINSTR
). Identified executable sections are further analyzed:
For each instruction, the program performs frequency analysis based on the show_reg
parameter. The logic updates the frequency statistics as follows:
rv_insn_stats
using insn_hist_incr()
.rv_reg_stats
using reg_hist_incr()
.The program calculates the highest instruction or register frequency using find_max_freq()
and generates histograms for each instruction or register based on the statistics:
issue2: bad address
% build/rv32emu -p build/nqueens.elf
Error: Bad address
rv_pyvisual.c
rv_pyvisual.c would output statistic file under build/pyvisual named output.json
, and would called run_analysis.py
to generate figure for the target input elf file.
make tool
to buildrun_analysis.py
Example:
Or Run instruction log analysis with highlight
Don't put screenshots which contain plaintext only!
gitbub-PochariChun / rv32emu visualization
gist:visualization:rv_pyvisual.c
gist:visualization:run_analysis.py