1 | // Simple integration test for contextual instrumentation |
2 | // |
3 | // Copy the header defining ContextNode. |
4 | // RUN: mkdir -p %t_include |
5 | // RUN: cp %llvm_src/include/llvm/ProfileData/CtxInstrContextNode.h %t_include/ |
6 | // |
7 | // Compile with ctx instrumentation "on". We treat "theRoot" as callgraph root. |
8 | // RUN: %clangxx %s %ctxprofilelib -I%t_include -O2 -o %t.bin -mllvm -profile-context-root=theRoot \ |
9 | // RUN: -mllvm -ctx-prof-skip-callsite-instr=skip_me |
10 | // |
11 | // Run the binary, and observe the profile fetch handler's output. |
12 | // RUN: %t.bin | FileCheck %s |
13 | |
14 | #include "CtxInstrContextNode.h" |
15 | #include <cstdio> |
16 | #include <iostream> |
17 | |
18 | using namespace llvm::ctx_profile; |
19 | extern "C" void |
20 | __llvm_ctx_profile_start_collection(unsigned AutoDetectDuration = 0); |
21 | extern "C" bool __llvm_ctx_profile_fetch(ProfileWriter &); |
22 | |
23 | // avoid name mangling |
24 | extern "C" { |
25 | __attribute__((noinline)) void skip_me() {} |
26 | |
27 | __attribute__((noinline)) void someFunction(int I) { |
28 | if (I % 2) |
29 | printf("check odd\n" ); |
30 | else |
31 | printf("check even\n" ); |
32 | skip_me(); |
33 | } |
34 | |
35 | // block inlining because the pre-inliner otherwise will inline this - it's |
36 | // too small. |
37 | __attribute__((noinline)) void theRoot() { |
38 | printf("check 1\n" ); |
39 | someFunction(I: 1); |
40 | #pragma nounroll |
41 | for (auto I = 0; I < 2; ++I) { |
42 | someFunction(I); |
43 | } |
44 | skip_me(); |
45 | } |
46 | |
47 | __attribute__((noinline)) void flatFct() { |
48 | printf("flat check 1\n" ); |
49 | someFunction(I: 1); |
50 | #pragma nounroll |
51 | for (auto I = 0; I < 2; ++I) { |
52 | someFunction(I); |
53 | } |
54 | } |
55 | } |
56 | |
57 | // Make sure the program actually ran correctly. |
58 | // CHECK: check 1 |
59 | // CHECK-NEXT: check odd |
60 | // CHECK-NEXT: check even |
61 | // CHECK-NEXT: check odd |
62 | // CHECK-NEXT: flat check 1 |
63 | // CHECK-NEXT: check odd |
64 | // CHECK-NEXT: check even |
65 | // CHECK-NEXT: check odd |
66 | |
67 | class TestProfileWriter : public ProfileWriter { |
68 | void printProfile(const ContextNode &Node, const std::string &Indent, |
69 | const std::string &Increment) { |
70 | std::cout << Indent << "Guid: " << Node.guid() << std::endl; |
71 | std::cout << Indent << "Entries: " << Node.entrycount() << std::endl; |
72 | std::cout << Indent << Node.counters_size() << " counters and " |
73 | << Node.callsites_size() << " callsites" << std::endl; |
74 | std::cout << Indent << "Counter values: " ; |
75 | for (uint32_t I = 0U; I < Node.counters_size(); ++I) |
76 | std::cout << Node.counters()[I] << " " ; |
77 | std::cout << std::endl; |
78 | for (uint32_t I = 0U; I < Node.callsites_size(); ++I) |
79 | for (const auto *N = Node.subContexts()[I]; N; N = N->next()) { |
80 | std::cout << Indent << "At Index " << I << ":" << std::endl; |
81 | printProfile(*N, Indent + Increment, Increment); |
82 | } |
83 | } |
84 | |
85 | void startContextSection() override { |
86 | std::cout << "Entered Context Section" << std::endl; |
87 | } |
88 | |
89 | void endContextSection() override { |
90 | std::cout << "Exited Context Section" << std::endl; |
91 | } |
92 | |
93 | void writeContextual(const ContextNode &RootNode, |
94 | const ContextNode *Unhandled, |
95 | uint64_t EntryCount) override { |
96 | std::cout << "Entering Root " << RootNode.guid() |
97 | << " with total entry count " << EntryCount << std::endl; |
98 | for (const auto *P = Unhandled; P; P = P->next()) |
99 | std::cout << "Unhandled GUID: " << P->guid() << " entered " |
100 | << P->entrycount() << " times" << std::endl; |
101 | printProfile(RootNode, " " , " " ); |
102 | } |
103 | |
104 | void startFlatSection() override { |
105 | std::cout << "Entered Flat Section" << std::endl; |
106 | } |
107 | |
108 | void writeFlat(GUID Guid, const uint64_t *Buffer, |
109 | size_t BufferSize) override { |
110 | std::cout << "Flat: " << Guid << " " << Buffer[0]; |
111 | for (size_t I = 1U; I < BufferSize; ++I) |
112 | std::cout << "," << Buffer[I]; |
113 | std::cout << std::endl; |
114 | }; |
115 | |
116 | void endFlatSection() override { |
117 | std::cout << "Exited Flat Section" << std::endl; |
118 | } |
119 | }; |
120 | |
121 | // 8657661246551306189 is theRoot. We expect 2 callsites and 2 counters - one |
122 | // for the entry basic block and one for the loop. |
123 | // 6759619411192316602 is someFunction. We expect all context instances to show |
124 | // the same nr of counters and callsites, but the counters will be different. |
125 | // The first context is for the first callsite with theRoot as parent, and the |
126 | // second counter in someFunction will be 0 (we pass an odd nr, and the other |
127 | // path gets instrumented). |
128 | // The second context is in the loop. We expect 2 entries and each of the |
129 | // branches would be taken once, so the second counter is 1. |
130 | // CHECK-NEXT: Entered Context Section |
131 | // CHECK-NEXT: Entering Root 8657661246551306189 with total entry count 1 |
132 | // skip_me is entered 4 times: 3 via `someFunction`, and once from `theRoot` |
133 | // CHECK-NEXT: Unhandled GUID: 17928815489886282963 entered 4 times |
134 | // CHECK-NEXT: Guid: 8657661246551306189 |
135 | // CHECK-NEXT: Entries: 1 |
136 | // CHECK-NEXT: 2 counters and 3 callsites |
137 | // CHECK-NEXT: Counter values: 1 2 |
138 | // CHECK-NEXT: At Index 1: |
139 | // CHECK-NEXT: Guid: 6759619411192316602 |
140 | // CHECK-NEXT: Entries: 1 |
141 | // CHECK-NEXT: 2 counters and 2 callsites |
142 | // CHECK-NEXT: Counter values: 1 0 |
143 | // CHECK-NEXT: At Index 2: |
144 | // CHECK-NEXT: Guid: 6759619411192316602 |
145 | // CHECK-NEXT: Entries: 2 |
146 | // CHECK-NEXT: 2 counters and 2 callsites |
147 | // CHECK-NEXT: Counter values: 2 1 |
148 | // CHECK-NEXT: Exited Context Section |
149 | // CHECK-NEXT: Entered Flat Section |
150 | // This is `skip_me`. Entered 3 times via `someFunction` |
151 | // CHECK-NEXT: Flat: 17928815489886282963 3 |
152 | // CHECK-NEXT: Flat: 6759619411192316602 3,1 |
153 | // This is flatFct (guid: 14569438697463215220) |
154 | // CHECK-NEXT: Flat: 14569438697463215220 1,2 |
155 | // CHECK-NEXT: Exited Flat Section |
156 | |
157 | bool profileWriter() { |
158 | TestProfileWriter W; |
159 | return __llvm_ctx_profile_fetch(W); |
160 | } |
161 | |
162 | int main(int argc, char **argv) { |
163 | __llvm_ctx_profile_start_collection(); |
164 | theRoot(); |
165 | flatFct(); |
166 | // This would be implemented in a specific RPC handler, but here we just call |
167 | // it directly. |
168 | return !profileWriter(); |
169 | } |
170 | |