Skip to content

Commit 0b8a712

Browse files
committed
Refine file naming scheme for profiling data
- fix incorrect basename generated by dynamic profiler - force profiling data store in build directory regardless of the cwd - update README Close #336
1 parent bc54c7f commit 0b8a712

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Features:
3535
## Build and Verify
3636

3737
`rv32emu` relies on certain third-party packages for full functionality and access to all its features.
38-
To ensure proper operation, the target system should have the [SDL2 library](https://www.libsdl.org/)
38+
To ensure proper operation, the target system should have the [SDL2 library](https://www.libsdl.org/)
3939
and [SDL2_Mixer library](https://wiki.libsdl.org/SDL2_mixer) installed.
4040
* macOS: `brew install sdl2 sdl2_mixer`
4141
* 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:
269269
$ brew install graphviz
270270
```
271271

272-
First, users need to create a directory named prof and then build the profiling data by executing `rv32emu`.
272+
Build the profiling data by executing `rv32emu`.
273273
This can be done as follows:
274274
```shell
275275
$ build/rv32emu -p build/[test_program].elf

src/main.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* "LICENSE" for information on usage and redistribution of this file.
44
*/
55

6+
#include <assert.h>
7+
#include <libgen.h>
8+
#include <limits.h>
69
#include <stdio.h>
710
#include <stdlib.h>
811
#include <string.h>
@@ -32,7 +35,7 @@ static char *signature_out_file;
3235
static bool opt_quiet_outputs = false;
3336

3437
/* target executable */
35-
static const char *opt_prog_name = "a.out";
38+
static char *opt_prog_name = "a.out";
3639

3740
/* target argc and argv */
3841
static int prog_argc;
@@ -166,11 +169,21 @@ static bool parse_args(int argc, char **args)
166169
*/
167170
prog_args = &args[optind];
168171
opt_prog_name = prog_args[0];
172+
169173
if (opt_prof_data) {
170-
char *prog_name = malloc(strlen(opt_prog_name) - 11);
171-
strncpy(prog_name, opt_prog_name + 8, strlen(opt_prog_name) - 12);
172-
prof_out_file = malloc(strlen(opt_prog_name) + 1);
173-
sprintf(prof_out_file, "./prof/%s.prof", prog_name);
174+
char cwd_path[PATH_MAX] = {0};
175+
assert(getcwd(cwd_path, PATH_MAX));
176+
177+
char rel_path[PATH_MAX] = {0};
178+
memcpy(rel_path, args[0], strlen(args[0]) - 7 /* strlen("rv32emu")*/);
179+
180+
char *prog_basename = basename(opt_prog_name);
181+
prof_out_file = malloc(strlen(cwd_path) + 1 + strlen(rel_path) +
182+
strlen(prog_basename) + 5 + 1);
183+
assert(prof_out_file);
184+
185+
sprintf(prof_out_file, "%s/%s%s.prof", cwd_path, rel_path,
186+
prog_basename);
174187
}
175188
return true;
176189
}

tools/rv_profiler

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Block:
1414
def printTable(table):
1515
col_widths = getLongestWordLength(table)
1616
if len(table):
17-
if len(table[0]) == 9:
17+
if len(table[0]) == 9:
1818
print("PC_start".rjust(col_widths[0]), end=' ')
1919
print("PC_end".rjust(col_widths[0]), end=' ')
2020
print("frequency".rjust(col_widths[0]), end=' ')
@@ -95,7 +95,7 @@ parser.add_argument('--graph-ir', type=str,
9595
help='visualize graph IR')
9696
args = parser.parse_args()
9797

98-
f = open(f'prof/{args.filename}.prof', 'r')
98+
f = open(f'build/{args.filename}.prof', 'r')
9999
fileds = f.readline()
100100
raw_datas = f.read().split("\n")
101101
profile_datas = []
@@ -104,23 +104,23 @@ for data in raw_datas:
104104
if len(tmp) == 9:
105105
d = {
106106
"PC_start": tmp[0].strip(),
107-
"PC_end": tmp[1].strip(),
108-
"frequency": tmp[2].strip(),
107+
"PC_end": tmp[1].strip(),
108+
"frequency": tmp[2].strip(),
109109
"hot": tmp[3].strip(),
110-
"backward": tmp[4].strip(),
111-
"loop": tmp[5].strip(),
112-
"untaken": tmp[6].strip(),
113-
"taken": tmp[7].strip(),
110+
"backward": tmp[4].strip(),
111+
"loop": tmp[5].strip(),
112+
"untaken": tmp[6].strip(),
113+
"taken": tmp[7].strip(),
114114
"IR_list": tmp[8].strip(),
115115
}
116116
profile_datas.append(d)
117117
profile_dict[d["PC_start"]] = d;
118118
elif len(tmp) == 5:
119119
d = {
120120
"PC_start": tmp[0].strip(),
121-
"PC_end": tmp[1].strip(),
122-
"untaken": tmp[2].strip(),
123-
"taken": tmp[3].strip(),
121+
"PC_end": tmp[1].strip(),
122+
"untaken": tmp[2].strip(),
123+
"taken": tmp[3].strip(),
124124
"IR_list": tmp[4].strip(),
125125
}
126126
profile_datas.append(d)
@@ -144,4 +144,4 @@ if args.start_address or args.stop_address:
144144
if args.graph_ir:
145145
block = print_graph_IR(args.graph_ir)
146146
g = objviz(block)
147-
g.view() # render and show graphviz.files.Source object
147+
g.view() # render and show graphviz.files.Source object

0 commit comments

Comments
 (0)