diff options
author | José Fonseca <jfonseca@vmware.com> | 2013-04-17 13:32:15 +0100 |
---|---|---|
committer | José Fonseca <jfonseca@vmware.com> | 2013-04-17 16:50:52 +0100 |
commit | b8f6858fcb762b47ca2ad30efd286bd203042f17 (patch) | |
tree | 366c92461157a380530f4de96a5dca5fe3c09b61 /src/gallium/auxiliary | |
parent | 35ef27d485367ffede9f478c9865515814e119b7 (diff) |
gallivm: JIT symbol resolution with linux perf.
Details on docs/llvmpipe.html
Reviewed-by: Brian Paul <brianp@vmware.com>
Reviewed-by: Roland Scheidegger <sroland@vmware.com>
Diffstat (limited to 'src/gallium/auxiliary')
-rw-r--r-- | src/gallium/auxiliary/gallivm/lp_bld_debug.cpp | 117 | ||||
-rw-r--r-- | src/gallium/auxiliary/gallivm/lp_bld_debug.h | 6 | ||||
-rw-r--r-- | src/gallium/auxiliary/gallivm/lp_bld_init.c | 11 | ||||
-rw-r--r-- | src/gallium/auxiliary/gallivm/lp_bld_misc.cpp | 23 | ||||
-rw-r--r-- | src/gallium/auxiliary/gallivm/lp_bld_misc.h | 3 |
5 files changed, 101 insertions, 59 deletions
diff --git a/src/gallium/auxiliary/gallivm/lp_bld_debug.cpp b/src/gallium/auxiliary/gallivm/lp_bld_debug.cpp index af50fcc142..ac8e10bbd5 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld_debug.cpp +++ b/src/gallium/auxiliary/gallivm/lp_bld_debug.cpp @@ -31,6 +31,7 @@ #include <llvm/Target/TargetMachine.h> #include <llvm/Target/TargetInstrInfo.h> #include <llvm/Support/raw_ostream.h> +#include <llvm/Support/Format.h> #include <llvm/Support/MemoryObject.h> #if HAVE_LLVM >= 0x0300 @@ -60,6 +61,11 @@ #include "lp_bld_debug.h" +#ifdef __linux__ +#include <sys/stat.h> +#include <fcntl.h> +#endif + /** @@ -174,8 +180,8 @@ public: * - http://blog.llvm.org/2010/01/x86-disassembler.html * - http://blog.llvm.org/2010/04/intro-to-llvm-mc-project.html */ -extern "C" void -lp_disassemble(const void* func) +static size_t +disassemble(const void* func, llvm::raw_ostream & Out) { #if HAVE_LLVM >= 0x0207 using namespace llvm; @@ -209,8 +215,8 @@ lp_disassemble(const void* func) #endif if (!AsmInfo) { - debug_printf("error: no assembly info for target %s\n", Triple.c_str()); - return; + Out << "error: no assembly info for target " << Triple << "\n"; + return 0; } #if HAVE_LLVM >= 0x0300 @@ -220,12 +226,10 @@ lp_disassemble(const void* func) OwningPtr<const MCDisassembler> DisAsm(T->createMCDisassembler()); #endif if (!DisAsm) { - debug_printf("error: no disassembler for target %s\n", Triple.c_str()); - return; + Out << "error: no disassembler for target " << Triple << "\n"; + return 0; } - raw_debug_ostream Out; - #if HAVE_LLVM >= 0x0300 unsigned int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); #else @@ -235,14 +239,14 @@ lp_disassemble(const void* func) #if HAVE_LLVM >= 0x0301 OwningPtr<const MCRegisterInfo> MRI(T->createMCRegInfo(Triple)); if (!MRI) { - debug_printf("error: no register info for target %s\n", Triple.c_str()); - return; + Out << "error: no register info for target " << Triple.c_str() << "\n"; + return 0; } OwningPtr<const MCInstrInfo> MII(T->createMCInstrInfo()); if (!MII) { - debug_printf("error: no instruction info for target %s\n", Triple.c_str()); - return; + Out << "error: no instruction info for target " << Triple.c_str() << "\n"; + return 0; } #endif @@ -260,8 +264,8 @@ lp_disassemble(const void* func) T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, Out)); #endif if (!Printer) { - debug_printf("error: no instruction printer for target %s\n", Triple.c_str()); - return; + Out << "error: no instruction printer for target " << Triple.c_str() << "\n"; + return 0; } #if HAVE_LLVM >= 0x0301 @@ -300,7 +304,7 @@ lp_disassemble(const void* func) * so that between runs. */ - debug_printf("%6lu:\t", (unsigned long)pc); + Out << llvm::format("%6lu:\t", (unsigned long)pc); if (!DisAsm->getInstruction(Inst, Size, memoryObject, pc, @@ -309,7 +313,7 @@ lp_disassemble(const void* func) #else nulls())) { #endif - debug_printf("invalid\n"); + Out << "invalid"; pc += 1; } @@ -320,25 +324,23 @@ lp_disassemble(const void* func) if (0) { unsigned i; for (i = 0; i < Size; ++i) { - debug_printf("%02x ", ((const uint8_t*)bytes)[pc + i]); + Out << llvm::format("%02x ", ((const uint8_t*)bytes)[pc + i]); } for (; i < 16; ++i) { - debug_printf(" "); + Out << " "; } } /* * Print the instruction. */ - #if HAVE_LLVM >= 0x0300 - Printer->printInst(&Inst, Out, ""); + Printer->printInst(&Inst, Out, ""); #elif HAVE_LLVM >= 0x208 - Printer->printInst(&Inst, Out); + Printer->printInst(&Inst, Out); #else - Printer->printInst(&Inst); + Printer->printInst(&Inst); #endif - Out.flush(); /* * Advance. @@ -386,7 +388,7 @@ lp_disassemble(const void* func) * Output the address relative to the function start, given * that MC will print the addresses relative the current pc. */ - debug_printf("\t\t; %lu", (unsigned long)jump); + Out << "\t\t; " << jump; /* * Ignore far jumps given it could be actually a tail return to @@ -401,7 +403,7 @@ lp_disassemble(const void* func) } } - debug_printf("\n"); + Out << "\n"; /* * Stop disassembling on return statements, if there is no record of a @@ -420,12 +422,73 @@ lp_disassemble(const void* func) */ if (0) { - debug_printf("disassemble %p %p\n", bytes, bytes + pc); + _debug_printf("disassemble %p %p\n", bytes, bytes + pc); } - debug_printf("\n"); + Out << "\n"; + Out.flush(); + + return pc; #else /* HAVE_LLVM < 0x0207 */ (void)func; + return 0; #endif /* HAVE_LLVM < 0x0207 */ } + +extern "C" void +lp_disassemble(LLVMValueRef func, const void *code) { + raw_debug_ostream Out; + disassemble(code, Out); +} + + +/* + * Linux perf profiler integration. + * + * See also: + * - http://penberg.blogspot.co.uk/2009/06/jato-has-profiler.html + * - https://github.com/penberg/jato/commit/73ad86847329d99d51b386f5aba692580d1f8fdc + * - http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=commitdiff;h=80d496be89ed7dede5abee5c057634e80a31c82d + */ +extern "C" void +lp_profile(LLVMValueRef func, const void *code) +{ +#if defined(__linux__) && (defined(DEBUG) || defined(PROFILE)) + static boolean first_time = TRUE; + static FILE *perf_map_file = NULL; + static int perf_asm_fd = -1; + if (first_time) { + /* + * We rely on the disassembler for determining a function's size, but + * the disassembly is a leaky and slow operation, so avoid running + * this except when running inside linux perf, which can be inferred + * by the PERF_BUILDID_DIR environment variable. + */ + if (getenv("PERF_BUILDID_DIR")) { + pid_t pid = getpid(); + char filename[256]; + util_snprintf(filename, sizeof filename, "/tmp/perf-%llu.map", (unsigned long long)pid); + perf_map_file = fopen(filename, "wt"); + util_snprintf(filename, sizeof filename, "/tmp/perf-%llu.map.asm", (unsigned long long)pid); + mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; + perf_asm_fd = open(filename, O_WRONLY | O_CREAT, mode); + } + first_time = FALSE; + } + if (perf_map_file) { + const char *symbol = LLVMGetValueName(func); + unsigned long addr = (uintptr_t)code; + llvm::raw_fd_ostream Out(perf_asm_fd, false); + Out << symbol << ":\n"; + unsigned long size = disassemble(code, Out); + fprintf(perf_map_file, "%lx %lx %s\n", addr, size, symbol); + fflush(perf_map_file); + } +#else + (void)func; + (void)code; +#endif +} + + diff --git a/src/gallium/auxiliary/gallivm/lp_bld_debug.h b/src/gallium/auxiliary/gallivm/lp_bld_debug.h index da873f30b2..ab83d98fee 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld_debug.h +++ b/src/gallium/auxiliary/gallivm/lp_bld_debug.h @@ -83,7 +83,11 @@ lp_check_alignment(const void *ptr, unsigned alignment); void -lp_disassemble(const void* func); +lp_disassemble(LLVMValueRef func, const void *code); + + +void +lp_profile(LLVMValueRef func, const void *code); #ifdef __cplusplus diff --git a/src/gallium/auxiliary/gallivm/lp_bld_init.c b/src/gallium/auxiliary/gallivm/lp_bld_init.c index 4fa5887e87..1153411dd5 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld_init.c +++ b/src/gallium/auxiliary/gallivm/lp_bld_init.c @@ -273,10 +273,6 @@ init_gallivm_engine(struct gallivm_state *gallivm) LLVMDisposeMessage(error); goto fail; } - -#if defined(DEBUG) || defined(PROFILE) - lp_register_oprofile_jit_event_listener(gallivm->engine); -#endif } LLVMAddModuleProvider(gallivm->engine, gallivm->provider);//new @@ -635,6 +631,7 @@ gallivm_compile_module(struct gallivm_state *gallivm) } + func_pointer gallivm_jit_function(struct gallivm_state *gallivm, LLVMValueRef func) @@ -650,9 +647,13 @@ gallivm_jit_function(struct gallivm_state *gallivm, jit_func = pointer_to_func(code); if (gallivm_debug & GALLIVM_DEBUG_ASM) { - lp_disassemble(code); + lp_disassemble(func, code); } +#if defined(PROFILE) + lp_profile(func, code); +#endif + /* Free the function body to save memory */ lp_func_delete_body(func); diff --git a/src/gallium/auxiliary/gallivm/lp_bld_misc.cpp b/src/gallium/auxiliary/gallivm/lp_bld_misc.cpp index 46cdbad268..c51279556b 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld_misc.cpp +++ b/src/gallium/auxiliary/gallivm/lp_bld_misc.cpp @@ -54,7 +54,6 @@ #include <llvm-c/ExecutionEngine.h> #include <llvm/Target/TargetOptions.h> #include <llvm/ExecutionEngine/ExecutionEngine.h> -#include <llvm/ExecutionEngine/JITEventListener.h> #if HAVE_LLVM >= 0x0301 #include <llvm/ADT/Triple.h> #include <llvm/ExecutionEngine/JITMemoryManager.h> @@ -75,28 +74,6 @@ #include "lp_bld_misc.h" -/** - * Register the engine with oprofile. - * - * This allows to see the LLVM IR function names in oprofile output. - * - * To actually work LLVM needs to be built with the --with-oprofile configure - * option. - * - * Also a oprofile:oprofile user:group is necessary. Which is not created by - * default on some distributions. - */ -extern "C" void -lp_register_oprofile_jit_event_listener(LLVMExecutionEngineRef EE) -{ -#if HAVE_LLVM >= 0x0301 - llvm::unwrap(EE)->RegisterJITEventListener(llvm::JITEventListener::createOProfileJITEventListener()); -#else - llvm::unwrap(EE)->RegisterJITEventListener(llvm::createOProfileJITEventListener()); -#endif -} - - extern "C" void lp_set_target_options(void) { diff --git a/src/gallium/auxiliary/gallivm/lp_bld_misc.h b/src/gallium/auxiliary/gallivm/lp_bld_misc.h index 9ed7c348bb..1f735fbcde 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld_misc.h +++ b/src/gallium/auxiliary/gallivm/lp_bld_misc.h @@ -41,9 +41,6 @@ extern "C" { extern void -lp_register_oprofile_jit_event_listener(LLVMExecutionEngineRef EE); - -extern void lp_set_target_options(void); |