Add profiling support for Intel Parallel Amplifier XE (VTune) for JITted code in...
[oota-llvm.git] / unittests / ExecutionEngine / IntelJITEventListenerTest.cpp
1 //===- JITEventListenerTest.cpp - Tests for Intel JITEventListener --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "JITEventListenerTestCommon.h"
11
12 using namespace llvm;
13
14 #include "llvm/ExecutionEngine/IntelJITEventsWrapper.h"
15
16 #include <map>
17 #include <list>
18
19 namespace {
20
21 // map of function ("method") IDs to source locations
22 NativeCodeMap ReportedDebugFuncs;
23
24 } // namespace
25
26 /// Mock implementaion of Intel JIT API jitprofiling library
27 namespace test_jitprofiling {
28
29 int NotifyEvent(iJIT_JVM_EVENT EventType, void *EventSpecificData) {
30   switch (EventType) {
31     case iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED: {
32       EXPECT_TRUE(0 != EventSpecificData);
33       iJIT_Method_Load* msg = static_cast<iJIT_Method_Load*>(EventSpecificData);
34
35       ReportedDebugFuncs[msg->method_id];
36
37       for(unsigned int i = 0; i < msg->line_number_size; ++i) {
38         EXPECT_TRUE(0 != msg->line_number_table);
39         std::pair<std::string, unsigned int> loc(
40           std::string(msg->source_file_name),
41           msg->line_number_table[i].LineNumber);
42         ReportedDebugFuncs[msg->method_id].push_back(loc);
43       }
44     }
45     break;
46     case iJVM_EVENT_TYPE_METHOD_UNLOAD_START: {
47       EXPECT_TRUE(0 != EventSpecificData);
48       unsigned int UnloadId
49         = *reinterpret_cast<unsigned int*>(EventSpecificData);
50       EXPECT_TRUE(1 == ReportedDebugFuncs.erase(UnloadId));
51     }
52     default:
53       break;
54   }
55   return 0;
56 }
57
58 iJIT_IsProfilingActiveFlags IsProfilingActive(void) {
59   // for testing, pretend we have an Intel Parallel Amplifier XE 2011
60   // instance attached
61   return iJIT_SAMPLING_ON;
62 }
63
64 unsigned int GetNewMethodID(void) {
65   static unsigned int id = 0;
66   return ++id;
67 }
68
69 } //namespace test_jitprofiling
70
71 class IntelJITEventListenerTest
72   : public JITEventListenerTestBase<IntelJITEventsWrapper> {
73 public:
74   IntelJITEventListenerTest()
75   : JITEventListenerTestBase<IntelJITEventsWrapper>(
76       new IntelJITEventsWrapper(test_jitprofiling::NotifyEvent, 0,
77         test_jitprofiling::IsProfilingActive, 0, 0,
78         test_jitprofiling::GetNewMethodID))
79   {
80     EXPECT_TRUE(0 != MockWrapper);
81
82     Listener.reset(JITEventListener::createIntelJITEventListener(
83       MockWrapper.get()));
84     EXPECT_TRUE(0 != Listener);
85     EE->RegisterJITEventListener(Listener.get());
86   }
87 };
88
89 TEST_F(IntelJITEventListenerTest, NoDebugInfo) {
90   TestNoDebugInfo(ReportedDebugFuncs);
91 }
92
93 TEST_F(IntelJITEventListenerTest, SingleLine) {
94   TestSingleLine(ReportedDebugFuncs);
95 }
96
97 TEST_F(IntelJITEventListenerTest, MultipleLines) {
98   TestMultipleLines(ReportedDebugFuncs);
99 }
100
101 // This testcase is disabled because the Intel JIT API does not support a single
102 // JITted function with source lines associated with multiple files
103 /*
104 TEST_F(IntelJITEventListenerTest, MultipleFiles) {
105   TestMultipleFiles(ReportedDebugFuncs);
106 }
107 */
108
109 testing::Environment* const jit_env =
110   testing::AddGlobalTestEnvironment(new JITEnvironment);