LLVM API Documentation

OProfileWrapper.h
Go to the documentation of this file.
00001 //===-- OProfileWrapper.h - OProfile JIT API Wrapper ------------*- C++ -*-===//
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 // This file defines a OProfileWrapper object that detects if the oprofile
00010 // daemon is running, and provides wrappers for opagent functions used to
00011 // communicate with the oprofile JIT interface. The dynamic library libopagent
00012 // does not need to be linked directly as this object lazily loads the library
00013 // when the first op_ function is called.
00014 //
00015 // See http://oprofile.sourceforge.net/doc/devel/jit-interface.html for the
00016 // definition of the interface.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #ifndef LLVM_EXECUTIONENGINE_OPROFILEWRAPPER_H
00021 #define LLVM_EXECUTIONENGINE_OPROFILEWRAPPER_H
00022 
00023 #include "llvm/Support/DataTypes.h"
00024 #include <opagent.h>
00025 
00026 namespace llvm {
00027 
00028 
00029 class OProfileWrapper {
00030   typedef  op_agent_t    (*op_open_agent_ptr_t)();
00031   typedef  int           (*op_close_agent_ptr_t)(op_agent_t);
00032   typedef  int           (*op_write_native_code_ptr_t)(op_agent_t,
00033                                                 const char*,
00034                                                 uint64_t,
00035                                                 void const*,
00036                                                 const unsigned int);
00037   typedef  int           (*op_write_debug_line_info_ptr_t)(op_agent_t,
00038                                                 void const*,
00039                                                 size_t,
00040                                                 struct debug_line_info const*);
00041   typedef  int           (*op_unload_native_code_ptr_t)(op_agent_t, uint64_t);
00042 
00043   // Also used for op_minor_version function which has the same signature
00044   typedef  int           (*op_major_version_ptr_t)();
00045 
00046   // This is not a part of the opagent API, but is useful nonetheless
00047   typedef  bool          (*IsOProfileRunningPtrT)();
00048 
00049 
00050   op_agent_t                      Agent;
00051   op_open_agent_ptr_t             OpenAgentFunc;
00052   op_close_agent_ptr_t            CloseAgentFunc;
00053   op_write_native_code_ptr_t      WriteNativeCodeFunc;
00054   op_write_debug_line_info_ptr_t  WriteDebugLineInfoFunc;
00055   op_unload_native_code_ptr_t     UnloadNativeCodeFunc;
00056   op_major_version_ptr_t          MajorVersionFunc;
00057   op_major_version_ptr_t          MinorVersionFunc;
00058   IsOProfileRunningPtrT           IsOProfileRunningFunc;
00059 
00060   bool Initialized;
00061 
00062 public:
00063   OProfileWrapper();
00064 
00065   // For testing with a mock opagent implementation, skips the dynamic load and
00066   // the function resolution.
00067   OProfileWrapper(op_open_agent_ptr_t OpenAgentImpl,
00068                   op_close_agent_ptr_t CloseAgentImpl,
00069                   op_write_native_code_ptr_t WriteNativeCodeImpl,
00070                   op_write_debug_line_info_ptr_t WriteDebugLineInfoImpl,
00071                   op_unload_native_code_ptr_t UnloadNativeCodeImpl,
00072                   op_major_version_ptr_t MajorVersionImpl,
00073                   op_major_version_ptr_t MinorVersionImpl,
00074                   IsOProfileRunningPtrT MockIsOProfileRunningImpl = 0)
00075   : OpenAgentFunc(OpenAgentImpl),
00076     CloseAgentFunc(CloseAgentImpl),
00077     WriteNativeCodeFunc(WriteNativeCodeImpl),
00078     WriteDebugLineInfoFunc(WriteDebugLineInfoImpl),
00079     UnloadNativeCodeFunc(UnloadNativeCodeImpl),
00080     MajorVersionFunc(MajorVersionImpl),
00081     MinorVersionFunc(MinorVersionImpl),
00082     IsOProfileRunningFunc(MockIsOProfileRunningImpl),
00083     Initialized(true)
00084   {
00085   }
00086 
00087   // Calls op_open_agent in the oprofile JIT library and saves the returned
00088   // op_agent_t handle internally so it can be used when calling all the other
00089   // op_* functions. Callers of this class do not need to keep track of
00090   // op_agent_t objects.
00091   bool op_open_agent();
00092 
00093   int op_close_agent();
00094   int op_write_native_code(const char* name,
00095                            uint64_t addr,
00096                            void const* code,
00097                            const unsigned int size);
00098   int op_write_debug_line_info(void const* code,
00099                                size_t num_entries,
00100                                struct debug_line_info const* info);
00101   int op_unload_native_code(uint64_t addr);
00102   int op_major_version();
00103   int op_minor_version();
00104 
00105   // Returns true if the oprofiled process is running, the opagent library is
00106   // loaded and a connection to the agent has been established, and false
00107   // otherwise.
00108   bool isAgentAvailable();
00109 
00110 private:
00111   // Loads the libopagent library and initializes this wrapper if the oprofile
00112   // daemon is running
00113   bool initialize();
00114 
00115   // Searches /proc for the oprofile daemon and returns true if the process if
00116   // found, or false otherwise.
00117   bool checkForOProfileProcEntry();
00118 
00119   bool isOProfileRunning();
00120 };
00121 
00122 } // namespace llvm
00123 
00124 #endif // LLVM_EXECUTIONENGINE_OPROFILEWRAPPER_H