1c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===-- OProfileWrapper.h - OProfile JIT API Wrapper ------------*- C++ -*-===//
2c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
3c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//                     The LLVM Compiler Infrastructure
4c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
5c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// This file is distributed under the University of Illinois Open Source
6c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// License. See LICENSE.TXT for details.
7c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
8c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===----------------------------------------------------------------------===//
9c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// This file defines a OProfileWrapper object that detects if the oprofile
10c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// daemon is running, and provides wrappers for opagent functions used to
11c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// communicate with the oprofile JIT interface. The dynamic library libopagent
12c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// does not need to be linked directly as this object lazily loads the library
13c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// when the first op_ function is called.
14c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
15c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// See http://oprofile.sourceforge.net/doc/devel/jit-interface.html for the
16c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// definition of the interface.
17c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
18c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===----------------------------------------------------------------------===//
19c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
20c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#ifndef LLVM_EXECUTIONENGINE_OPROFILEWRAPPER_H
21c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#define LLVM_EXECUTIONENGINE_OPROFILEWRAPPER_H
22c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
23c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/Support/DataTypes.h"
24c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <opagent.h>
25c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
26c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotnamespace llvm {
27c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
28c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
29c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass OProfileWrapper {
30c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  typedef  op_agent_t    (*op_open_agent_ptr_t)();
31c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  typedef  int           (*op_close_agent_ptr_t)(op_agent_t);
32c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  typedef  int           (*op_write_native_code_ptr_t)(op_agent_t,
33c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                                                const char*,
34c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                                                uint64_t,
35c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                                                void const*,
36c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                                                const unsigned int);
37c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  typedef  int           (*op_write_debug_line_info_ptr_t)(op_agent_t,
38c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                                                void const*,
39c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                                                size_t,
40c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                                                struct debug_line_info const*);
41c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  typedef  int           (*op_unload_native_code_ptr_t)(op_agent_t, uint64_t);
42c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
43c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Also used for op_minor_version function which has the same signature
44c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  typedef  int           (*op_major_version_ptr_t)();
45c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
46c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // This is not a part of the opagent API, but is useful nonetheless
47c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  typedef  bool          (*IsOProfileRunningPtrT)();
48c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
49c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
50c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  op_agent_t                      Agent;
51c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  op_open_agent_ptr_t             OpenAgentFunc;
52c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  op_close_agent_ptr_t            CloseAgentFunc;
53c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  op_write_native_code_ptr_t      WriteNativeCodeFunc;
54c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  op_write_debug_line_info_ptr_t  WriteDebugLineInfoFunc;
55c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  op_unload_native_code_ptr_t     UnloadNativeCodeFunc;
56c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  op_major_version_ptr_t          MajorVersionFunc;
57c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  op_major_version_ptr_t          MinorVersionFunc;
58c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  IsOProfileRunningPtrT           IsOProfileRunningFunc;
59c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
60c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  bool Initialized;
61c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
62c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic:
63c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  OProfileWrapper();
64c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
65c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // For testing with a mock opagent implementation, skips the dynamic load and
66c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // the function resolution.
67c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  OProfileWrapper(op_open_agent_ptr_t OpenAgentImpl,
68c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                  op_close_agent_ptr_t CloseAgentImpl,
69c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                  op_write_native_code_ptr_t WriteNativeCodeImpl,
70c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                  op_write_debug_line_info_ptr_t WriteDebugLineInfoImpl,
71c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                  op_unload_native_code_ptr_t UnloadNativeCodeImpl,
72c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                  op_major_version_ptr_t MajorVersionImpl,
73c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                  op_major_version_ptr_t MinorVersionImpl,
74c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                  IsOProfileRunningPtrT MockIsOProfileRunningImpl = 0)
75c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  : OpenAgentFunc(OpenAgentImpl),
76c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    CloseAgentFunc(CloseAgentImpl),
77c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    WriteNativeCodeFunc(WriteNativeCodeImpl),
78c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    WriteDebugLineInfoFunc(WriteDebugLineInfoImpl),
79c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    UnloadNativeCodeFunc(UnloadNativeCodeImpl),
80c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    MajorVersionFunc(MajorVersionImpl),
81c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    MinorVersionFunc(MinorVersionImpl),
82c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    IsOProfileRunningFunc(MockIsOProfileRunningImpl),
83c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    Initialized(true)
84c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  {
85c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
86c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
87c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Calls op_open_agent in the oprofile JIT library and saves the returned
88c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // op_agent_t handle internally so it can be used when calling all the other
89c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // op_* functions. Callers of this class do not need to keep track of
90c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // op_agent_t objects.
91c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  bool op_open_agent();
92c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
93c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  int op_close_agent();
94c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  int op_write_native_code(const char* name,
95c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                           uint64_t addr,
96c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                           void const* code,
97c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                           const unsigned int size);
98c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  int op_write_debug_line_info(void const* code,
99c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                               size_t num_entries,
100c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot                               struct debug_line_info const* info);
101c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  int op_unload_native_code(uint64_t addr);
102c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  int op_major_version();
103c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  int op_minor_version();
104c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
105c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Returns true if the oprofiled process is running, the opagent library is
106c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // loaded and a connection to the agent has been established, and false
107c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // otherwise.
108c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  bool isAgentAvailable();
109c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
110c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotprivate:
111c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Loads the libopagent library and initializes this wrapper if the oprofile
112c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // daemon is running
113c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  bool initialize();
114c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
115c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Searches /proc for the oprofile daemon and returns true if the process if
116c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // found, or false otherwise.
117c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  bool checkForOProfileProcEntry();
118c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
119c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  bool isOProfileRunning();
120c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot};
121c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
122c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot} // namespace llvm
123c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
124c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#endif // LLVM_EXECUTIONENGINE_OPROFILEWRAPPER_H
125