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 //
10 // Run the binary, and observe the profile fetch handler's output.
11 // RUN: %t.bin | FileCheck %s
12
13 #include "CtxInstrContextNode.h"
14 #include <cstdio>
15 #include <iostream>
16
17 using namespace llvm::ctx_profile;
18 extern "C" bool __llvm_ctx_profile_fetch(void *Data,
19 bool (*Writer)(void *,
20 const ContextNode &));
21
22 // avoid name mangling
23 extern "C" {
someFunction(int I)24 __attribute__((noinline)) void someFunction(int I) {
25 if (I % 2)
26 printf("check odd\n");
27 else
28 printf("check even\n");
29 }
30
31 // block inlining because the pre-inliner otherwise will inline this - it's
32 // too small.
theRoot()33 __attribute__((noinline)) void theRoot() {
34 printf("check 1\n");
35 someFunction(1);
36 #pragma nounroll
37 for (auto I = 0; I < 2; ++I) {
38 someFunction(I);
39 }
40 }
41 }
42
43 // Make sure the program actually ran correctly.
44 // CHECK: check 1
45 // CHECK-NEXT: check odd
46 // CHECK-NEXT: check even
47 // CHECK-NEXT: check odd
48
printProfile(const ContextNode & Node,const std::string & Indent,const std::string & Increment)49 void printProfile(const ContextNode &Node, const std::string &Indent,
50 const std::string &Increment) {
51 std::cout << Indent << "Guid: " << Node.guid() << std::endl;
52 std::cout << Indent << "Entries: " << Node.entrycount() << std::endl;
53 std::cout << Indent << Node.counters_size() << " counters and "
54 << Node.callsites_size() << " callsites" << std::endl;
55 std::cout << Indent << "Counter values: ";
56 for (uint32_t I = 0U; I < Node.counters_size(); ++I)
57 std::cout << Node.counters()[I] << " ";
58 std::cout << std::endl;
59 for (uint32_t I = 0U; I < Node.callsites_size(); ++I)
60 for (const auto *N = Node.subContexts()[I]; N; N = N->next()) {
61 std::cout << Indent << "At Index " << I << ":" << std::endl;
62 printProfile(*N, Indent + Increment, Increment);
63 }
64 }
65
66 // 8657661246551306189 is theRoot. We expect 2 callsites and 2 counters - one
67 // for the entry basic block and one for the loop.
68 // 6759619411192316602 is someFunction. We expect all context instances to show
69 // the same nr of counters and callsites, but the counters will be different.
70 // The first context is for the first callsite with theRoot as parent, and the
71 // second counter in someFunction will be 0 (we pass an odd nr, and the other
72 // path gets instrumented).
73 // The second context is in the loop. We expect 2 entries and each of the
74 // branches would be taken once, so the second counter is 1.
75 // CHECK-NEXT: Guid: 8657661246551306189
76 // CHECK-NEXT: Entries: 1
77 // CHECK-NEXT: 2 counters and 3 callsites
78 // CHECK-NEXT: Counter values: 1 2
79 // CHECK-NEXT: At Index 1:
80 // CHECK-NEXT: Guid: 6759619411192316602
81 // CHECK-NEXT: Entries: 1
82 // CHECK-NEXT: 2 counters and 2 callsites
83 // CHECK-NEXT: Counter values: 1 0
84 // CHECK-NEXT: At Index 2:
85 // CHECK-NEXT: Guid: 6759619411192316602
86 // CHECK-NEXT: Entries: 2
87 // CHECK-NEXT: 2 counters and 2 callsites
88 // CHECK-NEXT: Counter values: 2 1
89
profileWriter()90 bool profileWriter() {
91 return __llvm_ctx_profile_fetch(
92 nullptr, +[](void *, const ContextNode &Node) {
93 printProfile(Node, "", " ");
94 return true;
95 });
96 }
97
main(int argc,char ** argv)98 int main(int argc, char **argv) {
99 theRoot();
100 // This would be implemented in a specific RPC handler, but here we just call
101 // it directly.
102 return !profileWriter();
103 }
104