Add a --with-oprofile flag to configure, which uses OProfile's agent
[oota-llvm.git] / lib / ExecutionEngine / JIT / OProfileJITEventListener.cpp
1 //===-- OProfileJITEventListener.cpp - Tell OProfile about JITted code ----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a JITEventListener object that calls into OProfile to tell
11 // it about JITted functions.  For now, we only record function names and sizes,
12 // but eventually we'll also record line number information.
13 //
14 // See http://oprofile.sourceforge.net/doc/devel/jit-interface.html for the
15 // definition of the interface we're using.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #define DEBUG_TYPE "oprofile-jit-event-listener"
20 #include "llvm/Function.h"
21 #include "llvm/ExecutionEngine/JITEventListener.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/System/Errno.h"
24 #include "llvm/Config/config.h"
25 #include <stddef.h>
26 using namespace llvm;
27
28 #if defined(USE_OPROFILE)
29
30 #include <opagent.h>
31
32 namespace {
33
34 class OProfileJITEventListener : public JITEventListener {
35   op_agent_t Agent;
36 public:
37   OProfileJITEventListener();
38   ~OProfileJITEventListener();
39
40   virtual void NotifyFunctionEmitted(const Function &F,
41                                      void *FnStart, size_t FnSize,
42                                      const EmittedFunctionDetails &Details);
43   virtual void NotifyFreeingMachineCode(const Function &F, void *OldPtr);
44 };
45
46 OProfileJITEventListener::OProfileJITEventListener()
47     : Agent(op_open_agent()) {
48   if (Agent == NULL) {
49     const std::string err_str = sys::StrError();
50     DOUT << "Failed to connect to OProfile agent: " << err_str << "\n";
51   } else {
52     DOUT << "Connected to OProfile agent.\n";
53   }
54 }
55
56 OProfileJITEventListener::~OProfileJITEventListener() {
57   if (Agent != NULL) {
58     if (op_close_agent(Agent) == -1) {
59       const std::string err_str = sys::StrError();
60       DOUT << "Failed to disconnect from OProfile agent: " << err_str << "\n";
61     } else {
62       DOUT << "Disconnected from OProfile agent.\n";
63     }
64   }
65 }
66
67 // Adds the just-emitted function to the symbol table.
68 void OProfileJITEventListener::NotifyFunctionEmitted(
69     const Function &F, void *FnStart, size_t FnSize,
70     const EmittedFunctionDetails &) {
71   const char *const FnName = F.getNameStart();
72   assert(FnName != 0 && FnStart != 0 && "Bad symbol to add");
73   if (op_write_native_code(Agent, FnName,
74                            reinterpret_cast<uint64_t>(FnStart),
75                            FnStart, FnSize) == -1) {
76     DOUT << "Failed to tell OProfile about native function " << FnName
77          << " at [" << FnStart << "-" << ((char*)FnStart + FnSize) << "]\n";
78   }
79 }
80
81 // Removes the to-be-deleted function from the symbol table.
82 void OProfileJITEventListener::NotifyFreeingMachineCode(
83     const Function &F, void *FnStart) {
84   assert(FnStart && "Invalid function pointer");
85   if (op_unload_native_code(Agent, reinterpret_cast<uint64_t>(FnStart)) == -1) {
86     DOUT << "Failed to tell OProfile about unload of native function "
87          << F.getName() << " at " << FnStart << "\n";
88   }
89 }
90
91 }  // anonymous namespace.
92
93 namespace llvm {
94 JITEventListener *createOProfileJITEventListener() {
95   return new OProfileJITEventListener;
96 }
97 }
98
99 #else  // !defined(USE_OPROFILE)
100
101 namespace llvm {
102 // By defining this to return NULL, we can let clients call it unconditionally,
103 // even if they haven't configured with the OProfile libraries.
104 JITEventListener *createOProfileJITEventListener() {
105   return NULL;
106 }
107 }  // namespace llvm
108
109 #endif  // defined(USE_OPROFILE)