Skip to content

Commit

Permalink
profile: Increase program memory segment usage analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaokamikami authored and shinezyy committed Jan 30, 2024
1 parent 7a5b3d0 commit 0825255
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,16 @@ config TIMER_CLOCK_GETTIME
bool "clock_gettime"
endchoice

config MEMORY_REGION_ANALYSIS
bool "Enable Program memory segment analysis"
default n

if MEMORY_REGION_ANALYSIS
config MEMORY_REGION_ANALYSIS_SIZE
int "Analysis block MB size"
default 32
endif

config REPORT_ILLEGAL_INSTR
bool "Let NEMU report illegal instruction"
default y
Expand Down
12 changes: 12 additions & 0 deletions include/memory/paddr.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ int check_store_commit(uint64_t *addr, uint64_t *data, uint8_t *mask);
uint64_t store_read_step();
#endif

//#define CONFIG_MEMORY_REGION_ANALYSIS
#ifdef CONFIG_MEMORY_REGION_ANALYSIS
#include <math.h>
#define PROGRAM_MEMORY_SIZE (CONFIG_MSIZE /1024 /1024) // MB
#define PROGRAM_ANALYSIS_PAGES (PROGRAM_MEMORY_SIZE / CONFIG_MEMORY_REGION_ANALYSIS_SIZE)
#define ALIGNMENT_SIZE ((int)log2(CONFIG_MEMORY_REGION_ANALYSIS_SIZE * 1024 * 1024))// set MB alig

void analysis_memory_commit(uint64_t addr);
void analysis_use_addr_display();
bool analysis_memory_isuse(uint64_t page);
#endif

#ifdef CONFIG_MULTICORE_DIFF
extern uint8_t* golden_pmem;

Expand Down
56 changes: 55 additions & 1 deletion src/memory/paddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ uint8_t* guest_to_host(paddr_t paddr) { return paddr + HOST_PMEM_OFFSET; }
paddr_t host_to_guest(uint8_t *haddr) { return haddr - HOST_PMEM_OFFSET; }

static inline word_t pmem_read(paddr_t addr, int len) {
#ifdef CONFIG_MEMORY_REGION_ANALYSIS
analysis_memory_commit(addr);
#endif
#ifdef CONFIG_USE_SPARSEMM
return sparse_mem_wread(sparse_mm, addr, len);
#else
Expand All @@ -91,7 +94,9 @@ static inline void pmem_write(paddr_t addr, int len, word_t data) {
#ifdef CONFIG_DIFFTEST_STORE_COMMIT
store_commit_queue_push(addr, data, len);
#endif

#ifdef CONFIG_MEMORY_REGION_ANALYSIS
analysis_memory_commit(addr);
#endif
#ifdef CONFIG_USE_SPARSEMM
sparse_mem_wwrite(sparse_mm, addr, len, data);
#else
Expand Down Expand Up @@ -271,6 +276,55 @@ void paddr_write(paddr_t addr, int len, word_t data, int mode, vaddr_t vaddr) {
#endif
}

#ifdef CONFIG_MEMORY_REGION_ANALYSIS
bool mem_addr_use[PROGRAM_ANALYSIS_PAGES];
char *memory_region_record_file = NULL;
uint64_t get_byte_alignment(uint64_t addr) {
addr = (addr - CONFIG_MBASE) >> ALIGNMENT_SIZE;
return addr;
}

void analysis_memory_commit(uint64_t addr) {
uint64_t alignment_addr = get_byte_alignment(addr);
Assert(alignment_addr < PROGRAM_ANALYSIS_PAGES,
"alignment set memory size is addr %lx %lx", addr, alignment_addr);
mem_addr_use[alignment_addr] = true;
}

void analysis_use_addr_display() {
bool display_file = false;
if (memory_region_record_file != NULL) {
display_file = true;
}

if (display_file == true) {
FILE* fp = fopen(memory_region_record_file,"wr");
for (int i = 0; i < PROGRAM_ANALYSIS_PAGES; i++) {
if (mem_addr_use[i]) {
char result[32];
if (display_file) {
sprintf(result, "%d\n", i);
fputs(result,fp);
}
}
}
fclose(fp);
} else {
for (int i = 0; i < PROGRAM_ANALYSIS_PAGES; i++) {
if (mem_addr_use[i]) {
uint64_t uaddr = i << ALIGNMENT_SIZE;
Log("use memory page%4d %lx - %lx", i, uaddr , uaddr + (1 << ALIGNMENT_SIZE) - 1);
}
}
}

}

bool analysis_memory_isuse(uint64_t page) {
assert(page < PROGRAM_ANALYSIS_PAGES);
return mem_addr_use[page];
}
#endif

#ifdef CONFIG_DIFFTEST_STORE_COMMIT
store_commit_t store_commit_queue[CONFIG_DIFFTEST_STORE_QUEUE_SIZE];
Expand Down
14 changes: 12 additions & 2 deletions src/monitor/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ extern char *mapped_cpt_file; // defined in paddr.c
extern bool map_image_as_output_cpt;
extern char *reg_dump_file;
extern char *mem_dump_file;

#ifdef CONFIG_MEMORY_REGION_ANALYSIS
extern char *memory_region_record_file;
#endif
int is_batch_mode() { return batch_mode; }

static inline void welcome() {
Expand Down Expand Up @@ -107,7 +109,7 @@ static inline int parse_args(int argc, char *argv[]) {
// profiling
{"simpoint-profile" , no_argument , NULL, 3},
{"dont-skip-boot" , no_argument , NULL, 6},

{"mem_use_record_file", required_argument, NULL, 'A'},
// restore cpt
{"cpt-id" , required_argument, NULL, 4},

Expand Down Expand Up @@ -172,6 +174,13 @@ static inline int parse_args(int argc, char *argv[]) {
case 'M':
mem_dump_file = optarg;
break;
case 'A':
#ifdef CONFIG_MEMORY_REGION_ANALYSIS
Log("Set mem analysis log path %s", optarg);
memory_region_record_file = optarg;
#else
Log("is set path but memory analysis is not turned on");
#endif

case 5: sscanf(optarg, "%lu", &checkpoint_interval); break;

Expand Down Expand Up @@ -247,6 +256,7 @@ static inline int parse_args(int argc, char *argv[]) {

printf("\t--simpoint-profile simpoint profiling\n");
printf("\t--dont-skip-boot profiling/checkpoint immediately after boot\n");
printf("\t--mem_use_record_file result output file for analyzing the memory use segment\n");
// printf("\t--cpt-id checkpoint id\n");
printf("\t-M,--dump-mem=DUMP_FILE dump memory into FILE\n");
printf("\t-R,--dump-reg=DUMP_FILE dump register value into FILE\n");
Expand Down
4 changes: 4 additions & 0 deletions src/monitor/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ void ui_mainloop() {
if (is_batch_mode()) {
extern char *max_instr;
cmd_c(max_instr);
#ifdef CONFIG_MEMORY_REGION_ANALYSIS
extern void analysis_use_addr_display();
analysis_use_addr_display();
#endif
return;
}

Expand Down

0 comments on commit 0825255

Please sign in to comment.