LLVM  14.0.0git
OProfileWrapper.h
Go to the documentation of this file.
1 //===-- OProfileWrapper.h - OProfile JIT API Wrapper ------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // This file defines a OProfileWrapper object that detects if the oprofile
9 // daemon is running, and provides wrappers for opagent functions used to
10 // communicate with the oprofile JIT interface. The dynamic library libopagent
11 // does not need to be linked directly as this object lazily loads the library
12 // when the first op_ function is called.
13 //
14 // See http://oprofile.sourceforge.net/doc/devel/jit-interface.html for the
15 // definition of the interface.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef LLVM_EXECUTIONENGINE_OPROFILEWRAPPER_H
20 #define LLVM_EXECUTIONENGINE_OPROFILEWRAPPER_H
21 
22 #include "llvm/Support/DataTypes.h"
23 #include <opagent.h>
24 
25 namespace llvm {
26 
27 
29  typedef op_agent_t (*op_open_agent_ptr_t)();
30  typedef int (*op_close_agent_ptr_t)(op_agent_t);
31  typedef int (*op_write_native_code_ptr_t)(op_agent_t,
32  const char*,
33  uint64_t,
34  void const*,
35  const unsigned int);
36  typedef int (*op_write_debug_line_info_ptr_t)(op_agent_t,
37  void const*,
38  size_t,
39  struct debug_line_info const*);
40  typedef int (*op_unload_native_code_ptr_t)(op_agent_t, uint64_t);
41 
42  // Also used for op_minor_version function which has the same signature
43  typedef int (*op_major_version_ptr_t)();
44 
45  // This is not a part of the opagent API, but is useful nonetheless
46  typedef bool (*IsOProfileRunningPtrT)();
47 
48 
49  op_agent_t Agent;
50  op_open_agent_ptr_t OpenAgentFunc;
51  op_close_agent_ptr_t CloseAgentFunc;
52  op_write_native_code_ptr_t WriteNativeCodeFunc;
53  op_write_debug_line_info_ptr_t WriteDebugLineInfoFunc;
54  op_unload_native_code_ptr_t UnloadNativeCodeFunc;
55  op_major_version_ptr_t MajorVersionFunc;
56  op_major_version_ptr_t MinorVersionFunc;
57  IsOProfileRunningPtrT IsOProfileRunningFunc;
58 
59  bool Initialized;
60 
61 public:
63 
64  // For testing with a mock opagent implementation, skips the dynamic load and
65  // the function resolution.
66  OProfileWrapper(op_open_agent_ptr_t OpenAgentImpl,
67  op_close_agent_ptr_t CloseAgentImpl,
68  op_write_native_code_ptr_t WriteNativeCodeImpl,
69  op_write_debug_line_info_ptr_t WriteDebugLineInfoImpl,
70  op_unload_native_code_ptr_t UnloadNativeCodeImpl,
71  op_major_version_ptr_t MajorVersionImpl,
72  op_major_version_ptr_t MinorVersionImpl,
73  IsOProfileRunningPtrT MockIsOProfileRunningImpl = 0)
74  : OpenAgentFunc(OpenAgentImpl),
75  CloseAgentFunc(CloseAgentImpl),
76  WriteNativeCodeFunc(WriteNativeCodeImpl),
77  WriteDebugLineInfoFunc(WriteDebugLineInfoImpl),
78  UnloadNativeCodeFunc(UnloadNativeCodeImpl),
79  MajorVersionFunc(MajorVersionImpl),
80  MinorVersionFunc(MinorVersionImpl),
81  IsOProfileRunningFunc(MockIsOProfileRunningImpl),
82  Initialized(true)
83  {
84  }
85 
86  // Calls op_open_agent in the oprofile JIT library and saves the returned
87  // op_agent_t handle internally so it can be used when calling all the other
88  // op_* functions. Callers of this class do not need to keep track of
89  // op_agent_t objects.
90  bool op_open_agent();
91 
92  int op_close_agent();
93  int op_write_native_code(const char* name,
94  uint64_t addr,
95  void const* code,
96  const unsigned int size);
97  int op_write_debug_line_info(void const* code,
98  size_t num_entries,
99  struct debug_line_info const* info);
101  int op_major_version();
102  int op_minor_version();
103 
104  // Returns true if the oprofiled process is running, the opagent library is
105  // loaded and a connection to the agent has been established, and false
106  // otherwise.
107  bool isAgentAvailable();
108 
109 private:
110  // Loads the libopagent library and initializes this wrapper if the oprofile
111  // daemon is running
112  bool initialize();
113 
114  // Searches /proc for the oprofile daemon and returns true if the process if
115  // found, or false otherwise.
116  bool checkForOProfileProcEntry();
117 
118  bool isOProfileRunning();
119 };
120 
121 } // namespace llvm
122 
123 #endif // LLVM_EXECUTIONENGINE_OPROFILEWRAPPER_H
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::OProfileWrapper::op_open_agent
bool op_open_agent()
Definition: OProfileWrapper.cpp:181
llvm::OProfileWrapper::op_close_agent
int op_close_agent()
Definition: OProfileWrapper.cpp:193
initialize
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringLiteral > StandardNames)
Initialize the set of available library functions based on the specified target triple.
Definition: TargetLibraryInfo.cpp:116
size_t
llvm::OProfileWrapper::OProfileWrapper
OProfileWrapper(op_open_agent_ptr_t OpenAgentImpl, op_close_agent_ptr_t CloseAgentImpl, op_write_native_code_ptr_t WriteNativeCodeImpl, op_write_debug_line_info_ptr_t WriteDebugLineInfoImpl, op_unload_native_code_ptr_t UnloadNativeCodeImpl, op_major_version_ptr_t MajorVersionImpl, op_major_version_ptr_t MinorVersionImpl, IsOProfileRunningPtrT MockIsOProfileRunningImpl=0)
Definition: OProfileWrapper.h:66
llvm::OProfileWrapper::op_unload_native_code
int op_unload_native_code(uint64_t addr)
Definition: OProfileWrapper.cpp:257
int
Clang compiles this i1 i64 store i64 i64 store i64 i64 store i64 i64 store i64 align Which gets codegen d xmm0 movaps rbp movaps rbp movaps rbp movaps rbp rbp rbp rbp rbp It would be better to have movq s of instead of the movaps s LLVM produces ret int
Definition: README.txt:536
llvm::OProfileWrapper::op_major_version
int op_major_version()
Definition: OProfileWrapper.cpp:237
uint64_t
const
aarch64 promote const
Definition: AArch64PromoteConstant.cpp:232
llvm::OProfileWrapper::op_minor_version
int op_minor_version()
Definition: OProfileWrapper.cpp:247
llvm::OProfileWrapper::isAgentAvailable
bool isAgentAvailable()
Definition: OProfileWrapper.cpp:207
info
lazy value info
Definition: LazyValueInfo.cpp:59
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1532
llvm::OProfileWrapper
Definition: OProfileWrapper.h:28
name
static const char * name
Definition: SVEIntrinsicOpts.cpp:78
code
*Add support for compiling functions in both ARM and Thumb then taking the smallest *Add support for compiling individual basic blocks in thumb when in a larger ARM function This can be used for presumed cold code
Definition: README-Thumb.txt:9
llvm::OProfileWrapper::op_write_debug_line_info
int op_write_debug_line_info(void const *code, size_t num_entries, struct debug_line_info const *info)
Definition: OProfileWrapper.cpp:224
llvm::OProfileWrapper::OProfileWrapper
OProfileWrapper()
Definition: OProfileWrapper.cpp:40
llvm::OProfileWrapper::op_write_native_code
int op_write_native_code(const char *name, uint64_t addr, void const *code, const unsigned int size)
Definition: OProfileWrapper.cpp:211
DataTypes.h
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1903