1// Root autodetection test for contextual profiling
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 use -profile-context-root as signal
8// that we want contextual profiling, but we can specify anything there, that
9// won't be matched with any function, and result in the behavior we are aiming
10// for here.
11//
12// RUN: %clangxx %s %ctxprofilelib -I%t_include -O2 -o %t.bin \
13// RUN: -mllvm -profile-context-root="<autodetect>" -g -Wl,-export-dynamic
14//
15// Run the binary, and observe the profile fetch handler's output.
16// RUN %t.bin | FileCheck %s
17
18#include "CtxInstrContextNode.h"
19#include <atomic>
20#include <cstdio>
21#include <iostream>
22#include <thread>
23
24using namespace llvm::ctx_profile;
25extern "C" void __llvm_ctx_profile_start_collection(unsigned);
26extern "C" bool __llvm_ctx_profile_fetch(ProfileWriter &);
27
28// avoid name mangling
29extern "C" {
30__attribute__((noinline)) void anotherFunction() {}
31__attribute__((noinline)) void mock1() {}
32__attribute__((noinline)) void mock2() {}
33__attribute__((noinline)) void someFunction(int I) {
34 if (I % 2)
35 mock1();
36 else
37 mock2();
38 anotherFunction();
39}
40
41// block inlining because the pre-inliner otherwise will inline this - it's
42// too small.
43__attribute__((noinline)) void theRoot() {
44 someFunction(I: 1);
45#pragma nounroll
46 for (auto I = 0; I < 2; ++I) {
47 someFunction(I);
48 }
49 anotherFunction();
50}
51}
52
53class TestProfileWriter : public ProfileWriter {
54 void printProfile(const ContextNode &Node, const std::string &Indent,
55 const std::string &Increment) {
56 std::cout << Indent << "Guid: " << Node.guid() << std::endl;
57 std::cout << Indent << "Entries: " << Node.entrycount() << std::endl;
58 std::cout << Indent << Node.counters_size() << " counters and "
59 << Node.callsites_size() << " callsites" << std::endl;
60 std::cout << Indent << "Counter values: ";
61 for (uint32_t I = 0U; I < Node.counters_size(); ++I)
62 std::cout << Node.counters()[I] << " ";
63 std::cout << std::endl;
64 for (uint32_t I = 0U; I < Node.callsites_size(); ++I)
65 for (const auto *N = Node.subContexts()[I]; N; N = N->next()) {
66 std::cout << Indent << "At Index " << I << ":" << std::endl;
67 printProfile(*N, Indent + Increment, Increment);
68 }
69 }
70
71 void startContextSection() override {
72 std::cout << "Entered Context Section" << std::endl;
73 }
74
75 void endContextSection() override {
76 std::cout << "Exited Context Section" << std::endl;
77 }
78
79 void writeContextual(const ContextNode &RootNode,
80 const ContextNode *Unhandled,
81 uint64_t EntryCount) override {
82 std::cout << "Entering Root " << RootNode.guid()
83 << " with total entry count " << EntryCount << std::endl;
84 for (const auto *P = Unhandled; P; P = P->next())
85 std::cout << "Unhandled GUID: " << P->guid() << " entered "
86 << P->entrycount() << " times" << std::endl;
87 printProfile(RootNode, " ", " ");
88 }
89
90 void startFlatSection() override {
91 std::cout << "Entered Flat Section" << std::endl;
92 }
93
94 void writeFlat(GUID Guid, const uint64_t *Buffer,
95 size_t BufferSize) override {
96 std::cout << "Flat: " << Guid << " " << Buffer[0];
97 for (size_t I = 1U; I < BufferSize; ++I)
98 std::cout << "," << Buffer[I];
99 std::cout << std::endl;
100 };
101
102 void endFlatSection() override {
103 std::cout << "Exited Flat Section" << std::endl;
104 }
105};
106
107// Guid:3950394326069683896 is anotherFunction
108// Guid:6759619411192316602 is someFunction
109// These are expected to be the auto-detected roots. This is because we cannot
110// discern (with the current autodetection mechanism) if theRoot
111// (Guid:8657661246551306189) is ever re-entered.
112//
113// CHECK: Entered Context Section
114// CHECK-NEXT: Entering Root 6759619411192316602 with total entry count 12463157
115// CHECK-NEXT: Guid: 6759619411192316602
116// CHECK-NEXT: Entries: 5391142
117// CHECK-NEXT: 2 counters and 3 callsites
118// CHECK-NEXT: Counter values: 5391142 1832357
119// CHECK-NEXT: At Index 0:
120// CHECK-NEXT: Guid: 434762725428799310
121// CHECK-NEXT: Entries: 3558785
122// CHECK-NEXT: 1 counters and 0 callsites
123// CHECK-NEXT: Counter values: 3558785
124// CHECK-NEXT: At Index 1:
125// CHECK-NEXT: Guid: 5578595117440393467
126// CHECK-NEXT: Entries: 1832357
127// CHECK-NEXT: 1 counters and 0 callsites
128// CHECK-NEXT: Counter values: 1832357
129// CHECK-NEXT: At Index 2:
130// CHECK-NEXT: Guid: 3950394326069683896
131// CHECK-NEXT: Entries: 5391142
132// CHECK-NEXT: 1 counters and 0 callsites
133// CHECK-NEXT: Counter values: 5391142
134// CHECK-NEXT: Entering Root 3950394326069683896 with total entry count 11226401
135// CHECK-NEXT: Guid: 3950394326069683896
136// CHECK-NEXT: Entries: 10767423
137// CHECK-NEXT: 1 counters and 0 callsites
138// CHECK-NEXT: Counter values: 10767423
139// CHECK-NEXT: Exited Context Section
140// CHECK-NEXT: Entered Flat Section
141// CHECK-NEXT: Flat: 2597020043743142491 1
142// CHECK-NEXT: Flat: 4321328481998485159 1
143// CHECK-NEXT: Flat: 8657661246551306189 9114175,18099613
144// CHECK-NEXT: Flat: 434762725428799310 10574815
145// CHECK-NEXT: Flat: 5578595117440393467 5265754
146// CHECK-NEXT: Flat: 12566320182004153844 1
147// CHECK-NEXT: Exited Flat Section
148
149bool profileWriter() {
150 TestProfileWriter W;
151 return __llvm_ctx_profile_fetch(W);
152}
153
154int main(int argc, char **argv) {
155 std::atomic<bool> Stop = false;
156 std::atomic<int> Started = 0;
157 std::thread T1([&]() {
158 ++Started;
159 while (!Stop) {
160 theRoot();
161 }
162 });
163
164 std::thread T2([&]() {
165 ++Started;
166 while (!Stop) {
167 theRoot();
168 }
169 });
170
171 std::thread T3([&]() {
172 while (Started < 2) {
173 }
174 __llvm_ctx_profile_start_collection(5);
175 });
176
177 T3.join();
178 using namespace std::chrono_literals;
179
180 std::this_thread::sleep_for(10s);
181 Stop = true;
182 T1.join();
183 T2.join();
184
185 // This would be implemented in a specific RPC handler, but here we just call
186 // it directly.
187 return !profileWriter();
188}
189

source code of compiler-rt/test/ctx_profile/TestCases/autodetect-roots.cpp