1 //===-- IntelJITEventsWrapper.h - Intel JIT Events 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 //
9 // This file defines a wrapper for the Intel JIT Events API. It allows for the
10 // implementation of the jitprofiling library to be swapped with an alternative
11 // implementation (for testing). To include this file, you must have the
12 // jitprofiling.h header available; it is available in Intel(R) VTune(TM)
13 // Amplifier XE 2011.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef INTEL_JIT_EVENTS_WRAPPER_H
18 #define INTEL_JIT_EVENTS_WRAPPER_H
19 
20 #include "jitprofiling.h"
21 
22 namespace llvm {
23 
24 typedef enum {
25   LoadBinaryModule,
26   LoadBinarySection,
27   UnloadBinaryModule,
28   UnloadBinarySection
29 } IttEventType;
30 
31 class IntelJITEventsWrapper {
32   // Function pointer types for testing implementation of Intel jitprofiling
33   // library
34   typedef int (*NotifyEventPtr)(iJIT_JVM_EVENT, void*);
35   typedef int (*IttnotifyInfoPtr)(IttEventType, const char *, unsigned int);
36   typedef void (*RegisterCallbackExPtr)(void *, iJIT_ModeChangedEx );
37   typedef iJIT_IsProfilingActiveFlags (*IsProfilingActivePtr)(void);
38   typedef void (*FinalizeThreadPtr)(void);
39   typedef void (*FinalizeProcessPtr)(void);
40   typedef unsigned int (*GetNewMethodIDPtr)(void);
41 
42   NotifyEventPtr NotifyEventFunc;
43   IttnotifyInfoPtr IttnotifyInfoFunc;
44   RegisterCallbackExPtr RegisterCallbackExFunc;
45   IsProfilingActivePtr IsProfilingActiveFunc;
46   GetNewMethodIDPtr GetNewMethodIDFunc;
47 
48 public:
49   bool isAmplifierRunning() {
50     return iJIT_IsProfilingActive() == iJIT_SAMPLING_ON;
51   }
52 
53   IntelJITEventsWrapper()
54       : NotifyEventFunc(::iJIT_NotifyEvent), IttnotifyInfoFunc(0),
55         RegisterCallbackExFunc(::iJIT_RegisterCallbackEx),
56         IsProfilingActiveFunc(::iJIT_IsProfilingActive),
57         GetNewMethodIDFunc(::iJIT_GetNewMethodID) {}
58 
59   IntelJITEventsWrapper(NotifyEventPtr NotifyEventImpl,
60                         IttnotifyInfoPtr IttnotifyInfoImpl,
61                         RegisterCallbackExPtr RegisterCallbackExImpl,
62                         IsProfilingActivePtr IsProfilingActiveImpl,
63                         FinalizeThreadPtr FinalizeThreadImpl,
64                         FinalizeProcessPtr FinalizeProcessImpl,
65                         GetNewMethodIDPtr GetNewMethodIDImpl)
66       : NotifyEventFunc(NotifyEventImpl), IttnotifyInfoFunc(IttnotifyInfoImpl),
67         RegisterCallbackExFunc(RegisterCallbackExImpl),
68         IsProfilingActiveFunc(IsProfilingActiveImpl),
69         GetNewMethodIDFunc(GetNewMethodIDImpl) {}
70 
71   // Sends an event announcing that a function has been emitted
72   //   return values are event-specific.  See Intel documentation for details.
73   int  iJIT_NotifyEvent(iJIT_JVM_EVENT EventType, void *EventSpecificData) {
74     if (!NotifyEventFunc)
75       return -1;
76     return NotifyEventFunc(EventType, EventSpecificData);
77   }
78 
79   int iJitIttNotifyInfo(IttEventType EventType, const char *Name,
80                         unsigned int Size) {
81     if (!IttnotifyInfoFunc)
82       return -1;
83     return IttnotifyInfoFunc(EventType, Name, Size);
84   }
85 
86   // Registers a callback function to receive notice of profiling state changes
87   void iJIT_RegisterCallbackEx(void *UserData,
88                                iJIT_ModeChangedEx NewModeCallBackFuncEx) {
89     if (RegisterCallbackExFunc)
90       RegisterCallbackExFunc(UserData, NewModeCallBackFuncEx);
91   }
92 
93   // Returns the current profiler mode
94   iJIT_IsProfilingActiveFlags iJIT_IsProfilingActive(void) {
95     if (!IsProfilingActiveFunc)
96       return iJIT_NOTHING_RUNNING;
97     return IsProfilingActiveFunc();
98   }
99 
100   // Generates a locally unique method ID for use in code registration
101   unsigned int iJIT_GetNewMethodID(void) {
102     if (!GetNewMethodIDFunc)
103       return -1;
104     return GetNewMethodIDFunc();
105   }
106 };
107 
108 } //namespace llvm
109 
110 #endif //INTEL_JIT_EVENTS_WRAPPER_H
111