1243789Sdim//===-- IntelJITEventsWrapper.h - Intel JIT Events API Wrapper --*- C++ -*-===//
2243789Sdim//
3243789Sdim//                     The LLVM Compiler Infrastructure
4243789Sdim//
5243789Sdim// This file is distributed under the University of Illinois Open Source
6243789Sdim// License. See LICENSE.TXT for details.
7243789Sdim//
8243789Sdim//===----------------------------------------------------------------------===//
9243789Sdim//
10243789Sdim// This file defines a wrapper for the Intel JIT Events API. It allows for the
11243789Sdim// implementation of the jitprofiling library to be swapped with an alternative
12243789Sdim// implementation (for testing). To include this file, you must have the
13243789Sdim// jitprofiling.h header available; it is available in Intel(R) VTune(TM)
14243789Sdim// Amplifier XE 2011.
15243789Sdim//
16243789Sdim//===----------------------------------------------------------------------===//
17243789Sdim
18243789Sdim#ifndef INTEL_JIT_EVENTS_WRAPPER_H
19243789Sdim#define INTEL_JIT_EVENTS_WRAPPER_H
20243789Sdim
21243789Sdim#include "jitprofiling.h"
22243789Sdim
23243789Sdimnamespace llvm {
24243789Sdim
25243789Sdimclass IntelJITEventsWrapper {
26243789Sdim  // Function pointer types for testing implementation of Intel jitprofiling
27243789Sdim  // library
28243789Sdim  typedef int (*NotifyEventPtr)(iJIT_JVM_EVENT, void*);
29243789Sdim  typedef void (*RegisterCallbackExPtr)(void *, iJIT_ModeChangedEx );
30243789Sdim  typedef iJIT_IsProfilingActiveFlags (*IsProfilingActivePtr)(void);
31243789Sdim  typedef void (*FinalizeThreadPtr)(void);
32243789Sdim  typedef void (*FinalizeProcessPtr)(void);
33243789Sdim  typedef unsigned int (*GetNewMethodIDPtr)(void);
34243789Sdim
35243789Sdim  NotifyEventPtr NotifyEventFunc;
36243789Sdim  RegisterCallbackExPtr RegisterCallbackExFunc;
37243789Sdim  IsProfilingActivePtr IsProfilingActiveFunc;
38243789Sdim  GetNewMethodIDPtr GetNewMethodIDFunc;
39243789Sdim
40243789Sdimpublic:
41243789Sdim  bool isAmplifierRunning() {
42243789Sdim    return iJIT_IsProfilingActive() == iJIT_SAMPLING_ON;
43243789Sdim  }
44243789Sdim
45243789Sdim  IntelJITEventsWrapper()
46243789Sdim  : NotifyEventFunc(::iJIT_NotifyEvent),
47243789Sdim    RegisterCallbackExFunc(::iJIT_RegisterCallbackEx),
48243789Sdim    IsProfilingActiveFunc(::iJIT_IsProfilingActive),
49243789Sdim    GetNewMethodIDFunc(::iJIT_GetNewMethodID) {
50243789Sdim  }
51243789Sdim
52243789Sdim  IntelJITEventsWrapper(NotifyEventPtr NotifyEventImpl,
53243789Sdim                   RegisterCallbackExPtr RegisterCallbackExImpl,
54243789Sdim                   IsProfilingActivePtr IsProfilingActiveImpl,
55243789Sdim                   FinalizeThreadPtr FinalizeThreadImpl,
56243789Sdim                   FinalizeProcessPtr FinalizeProcessImpl,
57243789Sdim                   GetNewMethodIDPtr GetNewMethodIDImpl)
58243789Sdim  : NotifyEventFunc(NotifyEventImpl),
59243789Sdim    RegisterCallbackExFunc(RegisterCallbackExImpl),
60243789Sdim    IsProfilingActiveFunc(IsProfilingActiveImpl),
61243789Sdim    GetNewMethodIDFunc(GetNewMethodIDImpl) {
62243789Sdim  }
63243789Sdim
64263509Sdim  // Sends an event announcing that a function has been emitted
65243789Sdim  //   return values are event-specific.  See Intel documentation for details.
66243789Sdim  int  iJIT_NotifyEvent(iJIT_JVM_EVENT EventType, void *EventSpecificData) {
67243789Sdim    if (!NotifyEventFunc)
68243789Sdim      return -1;
69243789Sdim    return NotifyEventFunc(EventType, EventSpecificData);
70243789Sdim  }
71243789Sdim
72243789Sdim  // Registers a callback function to receive notice of profiling state changes
73243789Sdim  void iJIT_RegisterCallbackEx(void *UserData,
74243789Sdim                               iJIT_ModeChangedEx NewModeCallBackFuncEx) {
75243789Sdim    if (RegisterCallbackExFunc)
76243789Sdim      RegisterCallbackExFunc(UserData, NewModeCallBackFuncEx);
77243789Sdim  }
78243789Sdim
79243789Sdim  // Returns the current profiler mode
80243789Sdim  iJIT_IsProfilingActiveFlags iJIT_IsProfilingActive(void) {
81243789Sdim    if (!IsProfilingActiveFunc)
82243789Sdim      return iJIT_NOTHING_RUNNING;
83243789Sdim    return IsProfilingActiveFunc();
84243789Sdim  }
85243789Sdim
86243789Sdim  // Generates a locally unique method ID for use in code registration
87243789Sdim  unsigned int iJIT_GetNewMethodID(void) {
88243789Sdim    if (!GetNewMethodIDFunc)
89243789Sdim      return -1;
90243789Sdim    return GetNewMethodIDFunc();
91243789Sdim  }
92243789Sdim};
93243789Sdim
94243789Sdim} //namespace llvm
95243789Sdim
96243789Sdim#endif //INTEL_JIT_EVENTS_WRAPPER_H
97