Skip to content

Commit

Permalink
Issue #76 (Support JIT-Profiling API)
Browse files Browse the repository at this point in the history
* Moved JIT profiling rule up in order to hit earlier rules wrt DFLAGS/IFLAGS (Makefile).
* Refactored code into internal_get_code_name and internal_get_vtune_jitdesc
* Unregister JIT code from profiling engine (during libxsmm_finalize)
  • Loading branch information
hfp committed May 7, 2016
1 parent f836efa commit 7f2524c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 43 deletions.
38 changes: 19 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,25 @@ endif
@mv $(TMPFILE) $@
endif

ifneq (0,$(JIT))
ifneq (0,$(SYM))
ifeq (,$(filter Darwin Windows_NT,$(UNAME)))
VTUNEROOT = $(shell env | grep VTUNE_AMPLIFIER | grep -m1 _DIR | cut -d= -f2-)
ifneq (,$(wildcard $(VTUNEROOT)/lib64/libjitprofiling.$(SLIBEXT)))
LIBJITPROFILING = $(BLDDIR)/jitprofiling/libjitprofiling.$(SLIBEXT)
OBJJITPROFILING = $(BLDDIR)/jitprofiling/*.o
DFLAGS += -DLIBXSMM_VTUNE
IFLAGS += -I$(VTUNEROOT)/include
$(LIBJITPROFILING): $(BLDDIR)/jitprofiling/.make
@cp $(VTUNEROOT)/lib64/libjitprofiling.$(SLIBEXT) $(BLDDIR)/jitprofiling
@cd $(BLDDIR)/jitprofiling; $(AR) x libjitprofiling.$(SLIBEXT)
else
.PHONY: $(LIBJITPROFILING)
endif
endif
endif
endif

define DEFINE_COMPILE_RULE
$(1): $(2) $(3) $(dir $(1))/.make
$(CC) $(CFLAGS) $(DFLAGS) $(IFLAGS) $(4) -c $(2) -o $(1)
Expand Down Expand Up @@ -587,25 +606,6 @@ endif
endif
endif

ifneq (0,$(JIT))
ifneq (0,$(SYM))
ifeq (,$(filter Darwin Windows_NT,$(UNAME)))
VTUNEROOT = $(shell env | grep VTUNE_AMPLIFIER | grep -m1 _DIR | cut -d= -f2-)
ifneq (,$(wildcard $(VTUNEROOT)/lib64/libjitprofiling.$(SLIBEXT)))
LIBJITPROFILING = $(BLDDIR)/jitprofiling/libjitprofiling.$(SLIBEXT)
OBJJITPROFILING = $(BLDDIR)/jitprofiling/*.o
DFLAGS += -DLIBXSMM_VTUNE
IFLAGS += -I$(VTUNEROOT)/include
$(LIBJITPROFILING): $(BLDDIR)/jitprofiling/.make
@cp $(VTUNEROOT)/lib64/libjitprofiling.$(SLIBEXT) $(BLDDIR)/jitprofiling
@cd $(BLDDIR)/jitprofiling; $(AR) x libjitprofiling.$(SLIBEXT)
else
.PHONY: $(LIBJITPROFILING)
endif
endif
endif
endif

.PHONY: clib_hst
clib_hst: $(OUTDIR)/libxsmm.$(LIBEXT)
$(OUTDIR)/libxsmm.$(LIBEXT): $(OUTDIR)/.make $(OBJFILES_HST) $(OBJFILES_GEN_LIB) $(KERNELOBJS_HST) $(LIBJITPROFILING)
Expand Down
80 changes: 57 additions & 23 deletions src/libxsmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ typedef struct LIBXSMM_RETARGETABLE internal_regentry {
} function;
/* statically generated code (=0), dynamically generated code (>0). */
unsigned int size;
#if defined(LIBXSMM_VTUNE)
unsigned int id;
#endif
} internal_regentry;

LIBXSMM_DEBUG(LIBXSMM_RETARGETABLE LIBXSMM_VISIBILITY_INTERNAL unsigned int internal_ncollisions = 0;)
Expand Down Expand Up @@ -446,6 +449,36 @@ LIBXSMM_INLINE LIBXSMM_RETARGETABLE void internal_register_static_code(
}


LIBXSMM_INLINE LIBXSMM_RETARGETABLE void internal_get_code_name(const char* archid,
const libxsmm_gemm_descriptor* desc, unsigned int buffer_size, char* name)
{
assert((0 != desc && 0 != name) || 0 == buffer_size);
snprintf(name, buffer_size, "libxsmm_%s_%c%c%c_%ux%ux%u_%u_%u_%u_a%i_b%i_p%i.jit",
archid /* code path name */,
0 == (LIBXSMM_GEMM_FLAG_F32PREC & desc->flags) ? 'd' : 's',
0 == (LIBXSMM_GEMM_FLAG_TRANS_A & desc->flags) ? 'n' : 't',
0 == (LIBXSMM_GEMM_FLAG_TRANS_B & desc->flags) ? 'n' : 't',
desc->m, desc->n, desc->k, desc->lda, desc->ldb, desc->ldc,
desc->alpha, desc->beta, desc->prefetch);
}


#if defined(LIBXSMM_VTUNE)
LIBXSMM_INLINE LIBXSMM_RETARGETABLE void internal_get_vtune_jitdesc(const internal_regentry* code, const char* name, iJIT_Method_Load* desc)
{
assert(0 != code && 0 != code->id && 0 != code->size && 0 != desc);
desc->method_id = code->id;
desc->method_name = name;
desc->method_load_address = code->function.pmm;
desc->method_size = code->size;
desc->line_number_size = 0;
desc->line_number_table = NULL;
desc->class_file_name = NULL;
desc->source_file_name = NULL;
}
#endif


LIBXSMM_INLINE LIBXSMM_RETARGETABLE internal_regentry* internal_init(void)
{
/*const*/internal_regentry* result;
Expand Down Expand Up @@ -719,6 +752,16 @@ LIBXSMM_RETARGETABLE void libxsmm_finalize(void)
if (0 != code.size/*JIT: actually allocated*/) {
/* make address valid by clearing an eventual collision flag */
code.function.imm &= ~LIBXSMM_HASH_COLLISION;
#if defined(LIBXSMM_VTUNE)
if (0 != code.id && iJIT_SAMPLING_ON == iJIT_IsProfilingActive()) {
char jit_code_name[256];
iJIT_Method_Load vtune_jit_desc;
internal_get_code_name(internal_target_archid, internal_registry_keys + i,
sizeof(jit_code_name), jit_code_name);
internal_get_vtune_jitdesc(&code, jit_code_name, &vtune_jit_desc);
iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_UNLOAD_START, &vtune_jit_desc);
}
#endif
#if defined(_WIN32)
/* TODO: executable memory buffer under Windows */
#else
Expand Down Expand Up @@ -867,42 +910,33 @@ LIBXSMM_INLINE LIBXSMM_RETARGETABLE void internal_build(const libxsmm_gemm_descr

if (0/*ok*/ == mprotect(code->function.pmm, generated_code.code_size, PROT_EXEC | PROT_READ)) {
# if (!defined(NDEBUG) && defined(_DEBUG)) || defined(LIBXSMM_VTUNE)
char jit_code_name[512];
sprintf(jit_code_name, "libxsmm_%s_f%i_%c%c_%u_%u_%u_ld%u_%u_%u_a%i_b%i_pf%i.jit",
internal_target_archid /* code path name */,
0 == (LIBXSMM_GEMM_FLAG_F32PREC & desc->flags) ? 64 : 32,
0 == (LIBXSMM_GEMM_FLAG_TRANS_A & desc->flags) ? 'n' : 't',
0 == (LIBXSMM_GEMM_FLAG_TRANS_B & desc->flags) ? 'n' : 't',
desc->m, desc->n, desc->k, desc->lda, desc->ldb, desc->ldc,
desc->alpha, desc->beta, desc->prefetch);
char jit_code_name[256];
internal_get_code_name(internal_target_archid, desc, sizeof(jit_code_name), jit_code_name);
# endif
/* finalize code generation */
code->size = generated_code.code_size;
/* free temporary/initial code buffer */
free(generated_code.generated_code);
# if !defined(NDEBUG) && defined(_DEBUG)
{ /* dump byte-code into file */
FILE *const byte_code = fopen(jit_code_name, "wb");
if (0 != byte_code) {
fwrite(generated_code.generated_code, 1, generated_code.code_size, byte_code);
fwrite(code->function.pmm, 1, code->size, byte_code);
fclose(byte_code);
}
}
# endif /*!defined(NDEBUG) && defined(_DEBUG)*/
# if defined(LIBXSMM_VTUNE)
if (iJIT_SAMPLING_ON == iJIT_IsProfilingActive()) {
iJIT_Method_Load vtune_jit_method;
vtune_jit_method.method_id = iJIT_GetNewMethodID();
vtune_jit_method.method_name = jit_code_name;
vtune_jit_method.method_load_address = code->function.pmm;
vtune_jit_method.method_size = generated_code.code_size;
vtune_jit_method.line_number_size = 0;
vtune_jit_method.line_number_table = NULL;
vtune_jit_method.class_file_name = NULL;
vtune_jit_method.source_file_name = NULL;
iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED, &vtune_jit_method);
iJIT_Method_Load vtune_jit_desc;
code->id = iJIT_GetNewMethodID();
internal_get_vtune_jitdesc(code, jit_code_name, &vtune_jit_desc);
iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED, &vtune_jit_desc);
}
else {
code->id = 0;
}
# endif
/* free temporary/initial code buffer */
free(generated_code.generated_code);
/* finalize code generation */
code->size = generated_code.code_size;
}
else { /* there was an error with mprotect */
# if defined(NDEBUG)
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
master-1.4.1-10
master-1.4.1-11

0 comments on commit 7f2524c

Please sign in to comment.