📄 sim-profile.c
字号:
if (prof_taddr) { /* text address profile (sparse profile), NOTE: a dense print format is used, its more difficult to read, but the profiles are *much* smaller, I've assumed that the profiles are read by programs, at least for your sake I hope this is the case!! */ taddr_prof = stat_reg_sdist(sdb, "sim_text_addr_prof", "text address profile", /* initial value */0, /* print format */(PF_COUNT|PF_PDF), /* format */"0x%p %u %.2f", /* print fn */NULL); } for (i=0; i<pcstat_nelt; i++) { char buf[512], buf1[512]; struct stat_stat_t *stat; /* track the named statistical variable by text address */ /* find it... */ stat = stat_find_stat(sdb, pcstat_vars[i]); if (!stat) fatal("cannot locate any statistic named `%s'", pcstat_vars[i]); /* stat must be an integral type */ if (stat->sc != sc_int && stat->sc != sc_uint && stat->sc != sc_counter) fatal("`-pcstat' statistical variable `%s' is not an integral type", stat->name); /* register this stat */ pcstat_stats[i] = stat; pcstat_lastvals[i] = STATVAL(stat); /* declare the sparce text distribution */ sprintf(buf, "%s_by_pc", stat->name); sprintf(buf1, "%s (by text address)", stat->desc); pcstat_sdists[i] = stat_reg_sdist(sdb, buf, buf1, /* initial value */0, /* print format */(PF_COUNT|PF_PDF), /* format */"0x%p %u %.2f", /* print fn */NULL); } ld_reg_stats(sdb); mem_reg_stats(mem, sdb);}/* initialize the simulator */voidsim_init(void){ sim_num_refs = 0; /* allocate and initialize register file */ regs_init(®s); /* allocate and initialize memory space */ mem = mem_create("mem"); mem_init(mem);}/* local machine state accessor */static char * /* err str, NULL for no err */profile_mstate_obj(FILE *stream, /* output stream */ char *cmd, /* optional command string */ struct regs_t *regs, /* registers to access */ struct mem_t *mem) /* memory to access */{ /* just dump intermediate stats */ sim_print_stats(stream); /* no error */ return NULL;}/* load program into simulated state */voidsim_load_prog(char *fname, /* program to load */ int argc, char **argv, /* program arguments */ char **envp) /* program environment */{ /* load program text and data, set up environment, memory, and regs */ ld_load_prog(fname, argc, argv, envp, ®s, mem, TRUE); /* initialize the DLite debugger */ dlite_init(md_reg_obj, dlite_mem_obj, profile_mstate_obj);}/* print simulator-specific configuration information */voidsim_aux_config(FILE *stream) /* output stream */{ /* nothing currently */}/* dump simulator-specific auxiliary simulator statistics */voidsim_aux_stats(FILE *stream) /* output stream */{}/* un-initialize simulator-specific state */voidsim_uninit(void){ /* nada */}/* * configure the execution engine *//* * precise architected register accessors *//* next program counter */#define SET_NPC(EXPR) (regs.regs_NPC = (EXPR))/* current program counter */#define CPC (regs.regs_PC)/* general purpose registers */#define GPR(N) (regs.regs_R[N])#define SET_GPR(N,EXPR) (regs.regs_R[N] = (EXPR))#if defined(TARGET_PISA)/* floating point registers, L->word, F->single-prec, D->double-prec */#define FPR_L(N) (regs.regs_F.l[(N)])#define SET_FPR_L(N,EXPR) (regs.regs_F.l[(N)] = (EXPR))#define FPR_F(N) (regs.regs_F.f[(N)])#define SET_FPR_F(N,EXPR) (regs.regs_F.f[(N)] = (EXPR))#define FPR_D(N) (regs.regs_F.d[(N) >> 1])#define SET_FPR_D(N,EXPR) (regs.regs_F.d[(N) >> 1] = (EXPR))/* miscellaneous register accessors */#define SET_HI(EXPR) (regs.regs_C.hi = (EXPR))#define HI (regs.regs_C.hi)#define SET_LO(EXPR) (regs.regs_C.lo = (EXPR))#define LO (regs.regs_C.lo)#define FCC (regs.regs_C.fcc)#define SET_FCC(EXPR) (regs.regs_C.fcc = (EXPR))#elif defined(TARGET_ALPHA)/* floating point registers, L->word, F->single-prec, D->double-prec */#define FPR_Q(N) (regs.regs_F.q[N])#define SET_FPR_Q(N,EXPR) (regs.regs_F.q[N] = (EXPR))#define FPR(N) (regs.regs_F.d[N])#define SET_FPR(N,EXPR) (regs.regs_F.d[N] = (EXPR))/* miscellaneous register accessors */#define FPCR (regs.regs_C.fpcr)#define SET_FPCR(EXPR) (regs.regs_C.fpcr = (EXPR))#define UNIQ (regs.regs_C.uniq)#define SET_UNIQ(EXPR) (regs.regs_C.uniq = (EXPR))#else#error No ISA target defined...#endif/* precise architected memory state accessor macros */#define READ_BYTE(SRC, FAULT) \ ((FAULT) = md_fault_none, addr = (SRC), MEM_READ_BYTE(mem, addr))#define READ_HALF(SRC, FAULT) \ ((FAULT) = md_fault_none, addr = (SRC), MEM_READ_HALF(mem, addr))#define READ_WORD(SRC, FAULT) \ ((FAULT) = md_fault_none, addr = (SRC), MEM_READ_WORD(mem, addr))#ifdef HOST_HAS_QWORD#define READ_QWORD(SRC, FAULT) \ ((FAULT) = md_fault_none, addr = (SRC), MEM_READ_QWORD(mem, addr))#endif /* HOST_HAS_QWORD */#define WRITE_BYTE(SRC, DST, FAULT) \ ((FAULT) = md_fault_none, addr = (DST), MEM_WRITE_BYTE(mem, addr, (SRC)))#define WRITE_HALF(SRC, DST, FAULT) \ ((FAULT) = md_fault_none, addr = (DST), MEM_WRITE_HALF(mem, addr, (SRC)))#define WRITE_WORD(SRC, DST, FAULT) \ ((FAULT) = md_fault_none, addr = (DST), MEM_WRITE_WORD(mem, addr, (SRC)))#ifdef HOST_HAS_QWORD#define WRITE_QWORD(SRC, DST, FAULT) \ ((FAULT) = md_fault_none, addr = (DST), MEM_WRITE_QWORD(mem, addr, (SRC)))#endif /* HOST_HAS_QWORD *//* system call handler macro */#define SYSCALL(INST) sys_syscall(®s, mem_access, mem, INST, TRUE)/* addressing mode FSM (dest of last LUI, used for decoding addr modes) */static unsigned int fsm = 0;/* start simulation, program loaded, processor precise state initialized */voidsim_main(void){ int i; md_inst_t inst; register md_addr_t addr; register int is_write; enum md_opcode op; unsigned int flags; enum md_fault_type fault; fprintf(stderr, "sim: ** starting functional simulation **\n"); /* set up initial default next PC */ regs.regs_NPC = regs.regs_PC + sizeof(md_inst_t); /* check for DLite debugger entry condition */ if (dlite_check_break(regs.regs_PC, /* no access */0, /* addr */0, 0, 0)) dlite_main(regs.regs_PC - sizeof(md_inst_t), regs.regs_PC, sim_num_insn, ®s, mem); while (TRUE) { /* maintain $r0 semantics */ regs.regs_R[MD_REG_ZERO] = 0;#ifdef TARGET_ALPHA regs.regs_F.d[MD_REG_ZERO] = 0.0;#endif /* TARGET_ALPHA */ /* get the next instruction to execute */ MD_FETCH_INST(inst, mem, regs.regs_PC); if (verbose) { myfprintf(stderr, "%10n @ 0x%08p: ", sim_num_insn, regs.regs_PC); md_print_insn(inst, regs.regs_PC, stderr); fprintf(stderr, "\n"); /* fflush(stderr); */ } /* keep an instruction count */ sim_num_insn++; /* set default reference address and access mode */ addr = 0; is_write = FALSE; /* set default fault - none */ fault = md_fault_none; /* decode the instruction */ MD_SET_OPCODE(op, inst); /* execute the instruction */ switch (op) {#define DEFINST(OP,MSK,NAME,OPFORM,RES,FLAGS,O1,O2,I1,I2,I3) \ case OP: \ SYMCAT(OP,_IMPL); \ break;#define DEFLINK(OP,MSK,NAME,MASK,SHIFT) \ case OP: \ panic("attempted to execute a linking opcode");#define CONNECT(OP)#define DECLARE_FAULT(FAULT) \ { fault = (FAULT); break; }#include "machine.def" default: panic("attempted to execute a bogus opcode"); } if (MD_OP_FLAGS(op) & F_MEM) { sim_num_refs++; if (MD_OP_FLAGS(op) & F_STORE) is_write = TRUE; } /* * profile this instruction */ flags = MD_OP_FLAGS(op); if (prof_ic) { enum inst_class_t ic; /* compute instruction class */ if (flags & F_LOAD) ic = ic_load; else if (flags & F_STORE) ic = ic_store; else if (flags & F_UNCOND) ic = ic_uncond; else if (flags & F_COND) ic = ic_cond; else if (flags & F_ICOMP) ic = ic_icomp; else if (flags & F_FCOMP) ic = ic_fcomp; else if (flags & F_TRAP) ic = ic_trap; else panic("instruction has no class"); /* update instruction class profile */ stat_add_sample(ic_prof, (int)ic); } if (prof_inst) { /* update instruction profile */ stat_add_sample(inst_prof, (int)op - /* skip NA */1); } if (prof_bc) { enum branch_class_t bc; /* compute instruction class */ if (flags & F_CTRL) { if ((flags & (F_CALL|F_DIRJMP)) == (F_CALL|F_DIRJMP)) bc = bc_call_dir; else if ((flags & (F_CALL|F_INDIRJMP)) == (F_CALL|F_INDIRJMP)) bc = bc_call_indir; else if ((flags & (F_UNCOND|F_DIRJMP)) == (F_UNCOND|F_DIRJMP)) bc = bc_uncond_dir; else if ((flags & (F_UNCOND|F_INDIRJMP))== (F_UNCOND|F_INDIRJMP)) bc = bc_uncond_indir; else if ((flags & (F_COND|F_DIRJMP)) == (F_COND|F_DIRJMP)) bc = bc_cond_dir; else if ((flags & (F_COND|F_INDIRJMP)) == (F_COND|F_INDIRJMP)) bc = bc_cond_indir; else panic("branch has no class"); /* update instruction class profile */ stat_add_sample(bc_prof, (int)bc); } } if (prof_am) { enum md_amode_type am; /* update addressing mode pre-probe FSM */ MD_AMODE_PREPROBE(op, fsm); /* compute addressing mode */ if (flags & F_MEM) { /* compute addressing mode */ MD_AMODE_PROBE(am, op, fsm); /* update the addressing mode profile */ stat_add_sample(am_prof, (int)am); /* addressing mode pre-probe FSM, after all loads and stores */ MD_AMODE_POSTPROBE(fsm); } } if (prof_seg) { if (flags & F_MEM) { /* update instruction profile */ stat_add_sample(seg_prof, (int)bind_to_seg(addr)); } } if (prof_tsyms) { int tindex; /* attempt to bind inst address to a text segment symbol */ sym_bind_addr(regs.regs_PC, &tindex, /* !exact */FALSE, sdb_text); if (tindex >= 0) { if (tindex > sym_ntextsyms) panic("bogus text symbol index"); stat_add_sample(tsym_prof, tindex); } /* else, could not bind to a symbol */ } if (prof_dsyms) { int dindex; if (flags & F_MEM) { /* attempt to bind inst address to a text segment symbol */ sym_bind_addr(addr, &dindex, /* !exact */FALSE, sdb_data); if (dindex >= 0) { if (dindex > sym_ndatasyms) panic("bogus data symbol index"); stat_add_sample(dsym_prof, dindex); } /* else, could not bind to a symbol */ } } if (prof_taddr) { /* add regs_PC exec event to text address profile */ stat_add_sample(taddr_prof, regs.regs_PC); } /* update any stats tracked by PC */ for (i=0; i<pcstat_nelt; i++) { counter_t newval; int delta; /* check if any tracked stats changed */ newval = STATVAL(pcstat_stats[i]); delta = newval - pcstat_lastvals[i]; if (delta != 0) { stat_add_samples(pcstat_sdists[i], regs.regs_PC, delta); pcstat_lastvals[i] = newval; } } /* check for DLite debugger entry condition */ if (dlite_check_break(regs.regs_NPC, is_write ? ACCESS_WRITE : ACCESS_READ, addr, sim_num_insn, sim_num_insn)) dlite_main(regs.regs_PC, regs.regs_NPC, sim_num_insn, ®s, mem); /* go to the next instruction */ regs.regs_PC = regs.regs_NPC; regs.regs_NPC += sizeof(md_inst_t); /* finish early? */ if (max_insts && sim_num_insn >= max_insts) return; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -