LLVM API Documentation

OProfileJITEventListener.cpp

Go to the documentation of this file.
00001 //===-- OProfileJITEventListener.cpp - Tell OProfile about JITted code ----===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a JITEventListener object that calls into OProfile to tell
00011 // it about JITted functions.  For now, we only record function names and sizes,
00012 // but eventually we'll also record line number information.
00013 //
00014 // See http://oprofile.sourceforge.net/doc/devel/jit-interface.html for the
00015 // definition of the interface we're using.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #define DEBUG_TYPE "oprofile-jit-event-listener"
00020 #include "llvm/Function.h"
00021 #include "llvm/Analysis/DebugInfo.h"
00022 #include "llvm/CodeGen/MachineFunction.h"
00023 #include "llvm/ExecutionEngine/JITEventListener.h"
00024 #include "llvm/Support/Debug.h"
00025 #include "llvm/Support/raw_ostream.h"
00026 #include "llvm/System/Errno.h"
00027 #include "llvm/Config/config.h"
00028 #include <stddef.h>
00029 using namespace llvm;
00030 
00031 #if USE_OPROFILE
00032 
00033 #include <opagent.h>
00034 
00035 namespace {
00036 
00037 class OProfileJITEventListener : public JITEventListener {
00038   op_agent_t Agent;
00039 public:
00040   OProfileJITEventListener();
00041   ~OProfileJITEventListener();
00042 
00043   virtual void NotifyFunctionEmitted(const Function &F,
00044                                      void *FnStart, size_t FnSize,
00045                                      const EmittedFunctionDetails &Details);
00046   virtual void NotifyFreeingMachineCode(const Function &F, void *OldPtr);
00047 };
00048 
00049 OProfileJITEventListener::OProfileJITEventListener()
00050     : Agent(op_open_agent()) {
00051   if (Agent == NULL) {
00052     const std::string err_str = sys::StrError();
00053     DOUT << "Failed to connect to OProfile agent: " << err_str << "\n";
00054   } else {
00055     DOUT << "Connected to OProfile agent.\n";
00056   }
00057 }
00058 
00059 OProfileJITEventListener::~OProfileJITEventListener() {
00060   if (Agent != NULL) {
00061     if (op_close_agent(Agent) == -1) {
00062       const std::string err_str = sys::StrError();
00063       DOUT << "Failed to disconnect from OProfile agent: " << err_str << "\n";
00064     } else {
00065       DOUT << "Disconnected from OProfile agent.\n";
00066     }
00067   }
00068 }
00069 
00070 class FilenameCache {
00071   // Holds the filename of each CompileUnit, so that we can pass the
00072   // pointer into oprofile.  These char*s are freed in the destructor.
00073   DenseMap<GlobalVariable*, char*> Filenames;
00074   // Used as the scratch space in DICompileUnit::getFilename().
00075   std::string TempFilename;
00076 
00077  public:
00078   const char* getFilename(GlobalVariable *CompileUnit) {
00079     char *&Filename = Filenames[CompileUnit];
00080     if (Filename == NULL) {
00081       DICompileUnit CU(CompileUnit);
00082       Filename = strdup(CU.getFilename(TempFilename).c_str());
00083     }
00084     return Filename;
00085   }
00086   ~FilenameCache() {
00087     for (DenseMap<GlobalVariable*, char*>::iterator
00088              I = Filenames.begin(), E = Filenames.end(); I != E;++I) {
00089       free(I->second);
00090     }
00091   }
00092 };
00093 
00094 static debug_line_info LineStartToOProfileFormat(
00095     const MachineFunction &MF, FilenameCache &Filenames,
00096     uintptr_t Address, DebugLoc Loc) {
00097   debug_line_info Result;
00098   Result.vma = Address;
00099   const DebugLocTuple& tuple = MF.getDebugLocTuple(Loc);
00100   Result.lineno = tuple.Line;
00101   Result.filename = Filenames.getFilename(tuple.CompileUnit);
00102   DOUT << "Mapping " << reinterpret_cast<void*>(Result.vma) << " to "
00103        << Result.filename << ":" << Result.lineno << "\n";
00104   return Result;
00105 }
00106 
00107 // Adds the just-emitted function to the symbol table.
00108 void OProfileJITEventListener::NotifyFunctionEmitted(
00109     const Function &F, void *FnStart, size_t FnSize,
00110     const EmittedFunctionDetails &Details) {
00111   assert(F.hasName() && FnStart != 0 && "Bad symbol to add");
00112   if (op_write_native_code(Agent, F.getName().data(),
00113                            reinterpret_cast<uint64_t>(FnStart),
00114                            FnStart, FnSize) == -1) {
00115     DEBUG(errs() << "Failed to tell OProfile about native function " 
00116           << F.getName() << " at [" 
00117           << FnStart << "-" << ((char*)FnStart + FnSize) << "]\n");
00118     return;
00119   }
00120 
00121   // Now we convert the line number information from the address/DebugLoc format
00122   // in Details to the address/filename/lineno format that OProfile expects.
00123   // OProfile 0.9.4 (and maybe later versions) has a bug that causes it to
00124   // ignore line numbers for addresses above 4G.
00125   FilenameCache Filenames;
00126   std::vector<debug_line_info> LineInfo;
00127   LineInfo.reserve(1 + Details.LineStarts.size());
00128   if (!Details.MF->getDefaultDebugLoc().isUnknown()) {
00129     LineInfo.push_back(LineStartToOProfileFormat(
00130         *Details.MF, Filenames,
00131         reinterpret_cast<uintptr_t>(FnStart),
00132         Details.MF->getDefaultDebugLoc()));
00133   }
00134   for (std::vector<EmittedFunctionDetails::LineStart>::const_iterator
00135            I = Details.LineStarts.begin(), E = Details.LineStarts.end();
00136        I != E; ++I) {
00137     LineInfo.push_back(LineStartToOProfileFormat(
00138         *Details.MF, Filenames, I->Address, I->Loc));
00139   }
00140   if (!LineInfo.empty()) {
00141     if (op_write_debug_line_info(Agent, FnStart,
00142                                  LineInfo.size(), &*LineInfo.begin()) == -1) {
00143       DEBUG(errs() 
00144             << "Failed to tell OProfile about line numbers for native function "
00145             << F.getName() << " at [" 
00146             << FnStart << "-" << ((char*)FnStart + FnSize) << "]\n");
00147     }
00148   }
00149 }
00150 
00151 // Removes the to-be-deleted function from the symbol table.
00152 void OProfileJITEventListener::NotifyFreeingMachineCode(
00153     const Function &F, void *FnStart) {
00154   assert(FnStart && "Invalid function pointer");
00155   if (op_unload_native_code(Agent, reinterpret_cast<uint64_t>(FnStart)) == -1) {
00156     DEBUG(errs() << "Failed to tell OProfile about unload of native function "
00157                  << F.getName() << " at " << FnStart << "\n");
00158   }
00159 }
00160 
00161 }  // anonymous namespace.
00162 
00163 namespace llvm {
00164 JITEventListener *createOProfileJITEventListener() {
00165   return new OProfileJITEventListener;
00166 }
00167 }
00168 
00169 #else  // USE_OPROFILE
00170 
00171 namespace llvm {
00172 // By defining this to return NULL, we can let clients call it unconditionally,
00173 // even if they haven't configured with the OProfile libraries.
00174 JITEventListener *createOProfileJITEventListener() {
00175   return NULL;
00176 }
00177 }  // namespace llvm
00178 
00179 #endif  // USE_OPROFILE