diff --git a/README.md b/README.md index 328608f5..aeadef49 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Features: ## Build and Verify `rv32emu` relies on certain third-party packages for full functionality and access to all its features. -To ensure proper operation, the target system should have the [SDL2 library](https://www.libsdl.org/) +To ensure proper operation, the target system should have the [SDL2 library](https://www.libsdl.org/) and [SDL2_Mixer library](https://wiki.libsdl.org/SDL2_mixer) installed. * macOS: `brew install sdl2 sdl2_mixer` * Ubuntu Linux / Debian: `sudo apt install libsdl2-dev libsdl2-mixer-dev` @@ -269,7 +269,7 @@ For macOS users, it might be necessary to install additional dependencies: $ brew install graphviz ``` -First, users need to create a directory named prof and then build the profiling data by executing `rv32emu`. +Build the profiling data by executing `rv32emu`. This can be done as follows: ```shell $ build/rv32emu -p build/[test_program].elf diff --git a/src/main.c b/src/main.c index 2e658f44..71d900b7 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,9 @@ * "LICENSE" for information on usage and redistribution of this file. */ +#include +#include +#include #include #include #include @@ -32,7 +35,7 @@ static char *signature_out_file; static bool opt_quiet_outputs = false; /* target executable */ -static const char *opt_prog_name = "a.out"; +static char *opt_prog_name = "a.out"; /* target argc and argv */ static int prog_argc; @@ -166,11 +169,21 @@ static bool parse_args(int argc, char **args) */ prog_args = &args[optind]; opt_prog_name = prog_args[0]; + if (opt_prof_data) { - char *prog_name = malloc(strlen(opt_prog_name) - 11); - strncpy(prog_name, opt_prog_name + 8, strlen(opt_prog_name) - 12); - prof_out_file = malloc(strlen(opt_prog_name) + 1); - sprintf(prof_out_file, "./prof/%s.prof", prog_name); + char cwd_path[PATH_MAX] = {0}; + assert(getcwd(cwd_path, PATH_MAX)); + + char rel_path[PATH_MAX] = {0}; + memcpy(rel_path, args[0], strlen(args[0]) - 7 /* strlen("rv32emu")*/); + + char *prog_basename = basename(opt_prog_name); + prof_out_file = malloc(strlen(cwd_path) + 1 + strlen(rel_path) + + strlen(prog_basename) + 5 + 1); + assert(prof_out_file); + + sprintf(prof_out_file, "%s/%s%s.prof", cwd_path, rel_path, + prog_basename); } return true; } diff --git a/tools/rv_profiler b/tools/rv_profiler index 3f677b2f..6a3877cf 100755 --- a/tools/rv_profiler +++ b/tools/rv_profiler @@ -14,7 +14,7 @@ class Block: def printTable(table): col_widths = getLongestWordLength(table) if len(table): - if len(table[0]) == 9: + if len(table[0]) == 9: print("PC_start".rjust(col_widths[0]), end=' ') print("PC_end".rjust(col_widths[0]), end=' ') print("frequency".rjust(col_widths[0]), end=' ') @@ -95,7 +95,7 @@ parser.add_argument('--graph-ir', type=str, help='visualize graph IR') args = parser.parse_args() -f = open(f'prof/{args.filename}.prof', 'r') +f = open(f'build/{args.filename}.prof', 'r') fileds = f.readline() raw_datas = f.read().split("\n") profile_datas = [] @@ -104,13 +104,13 @@ for data in raw_datas: if len(tmp) == 9: d = { "PC_start": tmp[0].strip(), - "PC_end": tmp[1].strip(), - "frequency": tmp[2].strip(), + "PC_end": tmp[1].strip(), + "frequency": tmp[2].strip(), "hot": tmp[3].strip(), - "backward": tmp[4].strip(), - "loop": tmp[5].strip(), - "untaken": tmp[6].strip(), - "taken": tmp[7].strip(), + "backward": tmp[4].strip(), + "loop": tmp[5].strip(), + "untaken": tmp[6].strip(), + "taken": tmp[7].strip(), "IR_list": tmp[8].strip(), } profile_datas.append(d) @@ -118,9 +118,9 @@ for data in raw_datas: elif len(tmp) == 5: d = { "PC_start": tmp[0].strip(), - "PC_end": tmp[1].strip(), - "untaken": tmp[2].strip(), - "taken": tmp[3].strip(), + "PC_end": tmp[1].strip(), + "untaken": tmp[2].strip(), + "taken": tmp[3].strip(), "IR_list": tmp[4].strip(), } profile_datas.append(d) @@ -144,4 +144,4 @@ if args.start_address or args.stop_address: if args.graph_ir: block = print_graph_IR(args.graph_ir) g = objviz(block) - g.view() # render and show graphviz.files.Source object \ No newline at end of file + g.view() # render and show graphviz.files.Source object