We now have a pretty fast implementation, probably about as fast as we can get on a single core machine. However, on multi-core systems the cache line locking for the atomic operations becomes a bottle neck. To get around that we can use thread-private counts and aggregate them. With thread-private counts no locked instructions are needed, however the extra indirection does has its own cost of extra added instructions to spill a register and get the tls pointer so on single core this will be slower.
+ #include <stddef.h>
#ifdef WINDOWS
# define DISPLAY_STRING(msg) dr_messagebox(msg)
#else
# define DISPLAY_STRING(msg) dr_printf("%s\n", msg)
#endif
#define TESTALL(mask, var) (((mask) & (var)) == (mask))
#define TESTANY(mask, var) (((mask) & (var)) != 0)
typedef struct bb_counts {
uint64 blocks;
uint64 total_size;
} bb_counts;
static bb_counts counts_as_built;
void *as_built_lock;
static bb_counts counts_dynamic;
+ void *count_lock;
static uint64 bbs_eflags_saved;
static void
event_exit(void);
event_basic_block(
void *drcontext,
void *tag,
instrlist_t *bb,
bool for_trace, bool translating);
+ static void
+ event_thread_init(void *drcontext);
+ static void
+ event_thread_exit(void *drcontext);
DR_EXPORT void
{
dr_register_bb_event(event_basic_block);
+ dr_register_thread_init_event(event_thread_init);
+ dr_register_thread_exit_event(event_thread_exit);
}
static void
event_exit(void)
{
char msg[512];
int len;
len = snprintf(msg, sizeof(msg)/sizeof(msg[0]),
"Number of blocks built : %"UINT64_FORMAT_CODE"\n"
" Average size : %5.2lf instructions\n"
" Num saved eflags : %"UINT64_FORMAT_CODE"\n"
"Number of blocks executed : %"UINT64_FORMAT_CODE"\n"
" Average weighted size : %5.2lf instructions\n",
counts_as_built.blocks,
counts_as_built.total_size / (double)counts_as_built.blocks,
bbs_eflags_saved,
counts_dynamic.blocks,
counts_dynamic.total_size / (double)counts_dynamic.blocks);
msg[sizeof(msg)/sizeof(msg[0])-1] = '\0';
DISPLAY_STRING(msg);
}
+ static void
+ event_thread_init(void *drcontext)
+ {
+
+ bb_counts *counts = (bb_counts *)
dr_thread_alloc(drcontext,
sizeof(bb_counts));
+
+ dr_set_tls_field(drcontext, counts);
+ memset(counts, 0, sizeof(bb_counts));
+ }
+
+ static void
+ event_thread_exit(void *drcontext)
+ {
+ bb_counts *counts = (bb_counts *) dr_get_tls_field(drcontext);
+
+ counts_dynamic.blocks += counts->blocks;
+ counts_dynamic.total_size += counts->total_size;
+ }
event_basic_block(
void *drcontext,
void *tag,
instrlist_t *bb,
bool for_trace, bool translating)
{
uint num_instructions = 0;
bool eflags_saved = true;
+ bb_counts *counts = (bb_counts *) dr_get_tls_field(drcontext);
where = instr;
eflags_saved = false;
}
num_instructions++;
}
counts_as_built.blocks++;
counts_as_built.total_size += num_instructions;
if (eflags_saved)
bbs_eflags_saved++;
if (eflags_saved) {
}
+
+ dr_insert_read_tls_field(drcontext, bb, where,
DR_REG_XDI);
#ifdef X86_32
#else
#endif
+
if (eflags_saved) {
}
}
Top-level include file for DynamoRIO API.
DR_EXPORT void dr_client_main(client_id_t id, int argc, const char *argv[])
struct _instrlist_t instrlist_t
Definition dr_defines.h:842
uint client_id_t
Definition dr_defines.h:357
DR_API void dr_register_exit_event(void(*func)(void))
dr_emit_flags_t
Definition dr_events.h:138
@ DR_EMIT_DEFAULT
Definition dr_events.h:140
DR_API uint instr_get_arith_flags(instr_t *instr, dr_opnd_query_flags_t flags)
DR_API INSTR_INLINE instr_t * instr_get_next(instr_t *instr)
#define EFLAGS_WRITE_6
Definition dr_ir_instr.h:2528
#define EFLAGS_READ_6
Definition dr_ir_instr.h:2526
@ DR_QUERY_DEFAULT
Definition dr_ir_instr.h:252
DR_API instr_t * instrlist_first(instrlist_t *ilist)
#define OPND_CREATE_MEM32(base_reg, disp)
Definition dr_ir_macros.h:82
#define OPND_CREATE_INT8(val)
Definition dr_ir_macros.h:136
#define OPND_CREATE_INT_32OR8(val)
Definition dr_ir_macros.h:143
#define OPND_CREATE_MEM64(base_reg, disp)
Definition dr_ir_macros.h:79
#define INSTR_CREATE_add(dc, Rd, Rn, Rm_or_imm)
Definition dr_ir_macros_arm.h:1201
#define INSTR_CREATE_adc(dc, Rd, Rn, Rm_or_imm)
Definition dr_ir_macros_arm.h:1191
#define INSTR_CREATE_inc(dc, d)
Definition dr_ir_macros_x86.h:1580
#define DR_REG_XDI
Definition dr_ir_opnd.h:1325
DR_API void dr_restore_arith_flags(void *drcontext, instrlist_t *ilist, instr_t *where, dr_spill_slot_t slot)
DR_API void instrlist_meta_preinsert(instrlist_t *ilist, instr_t *where, instr_t *instr)
@ SPILL_SLOT_2
Definition dr_ir_utils.h:68
DR_API void dr_restore_reg(void *drcontext, instrlist_t *ilist, instr_t *where, reg_id_t reg, dr_spill_slot_t slot)
DR_API void dr_save_arith_flags(void *drcontext, instrlist_t *ilist, instr_t *where, dr_spill_slot_t slot)
DR_API void dr_save_reg(void *drcontext, instrlist_t *ilist, instr_t *where, reg_id_t reg, dr_spill_slot_t slot)
#define TESTALL(mask, var)
Definition droption.h:55
Definition dr_defines.h:378