From b8f6858fcb762b47ca2ad30efd286bd203042f17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Fonseca?= Date: Wed, 17 Apr 2013 13:32:15 +0100 Subject: gallivm: JIT symbol resolution with linux perf. Details on docs/llvmpipe.html Reviewed-by: Brian Paul Reviewed-by: Roland Scheidegger --- src/gallium/auxiliary/gallivm/lp_bld_debug.cpp | 117 +++++++++++++++++++------ 1 file changed, 90 insertions(+), 27 deletions(-) (limited to 'src/gallium/auxiliary/gallivm/lp_bld_debug.cpp') diff --git a/src/gallium/auxiliary/gallivm/lp_bld_debug.cpp b/src/gallium/auxiliary/gallivm/lp_bld_debug.cpp index af50fcc..ac8e10b 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld_debug.cpp +++ b/src/gallium/auxiliary/gallivm/lp_bld_debug.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #if HAVE_LLVM >= 0x0300 @@ -60,6 +61,11 @@ #include "lp_bld_debug.h" +#ifdef __linux__ +#include +#include +#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 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 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 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 +} + + -- cgit v1.1