189 lines · cpp
1// Root autodetection test for contextual profiling2//3// Copy the header defining ContextNode.4// RUN: mkdir -p %t_include5// RUN: cp %llvm_src/include/llvm/ProfileData/CtxInstrContextNode.h %t_include/6//7// Compile with ctx instrumentation "on". We use -profile-context-root as signal8// that we want contextual profiling, but we can specify anything there, that9// won't be matched with any function, and result in the behavior we are aiming10// 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-dynamic14//15// Run the binary, and observe the profile fetch handler's output.16// RUN %t.bin | FileCheck %s17 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 mangling29extern "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 else37 mock2();38 anotherFunction();39}40 41// block inlining because the pre-inliner otherwise will inline this - it's42// too small.43__attribute__((noinline)) void theRoot() {44 someFunction(1);45#pragma nounroll46 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 anotherFunction108// Guid:6759619411192316602 is someFunction109// These are expected to be the auto-detected roots. This is because we cannot110// discern (with the current autodetection mechanism) if theRoot111// (Guid:8657661246551306189) is ever re-entered.112//113// CHECK: Entered Context Section114// CHECK-NEXT: Entering Root 6759619411192316602 with total entry count 12463157115// CHECK-NEXT: Guid: 6759619411192316602116// CHECK-NEXT: Entries: 5391142117// CHECK-NEXT: 2 counters and 3 callsites118// CHECK-NEXT: Counter values: 5391142 1832357119// CHECK-NEXT: At Index 0:120// CHECK-NEXT: Guid: 434762725428799310121// CHECK-NEXT: Entries: 3558785122// CHECK-NEXT: 1 counters and 0 callsites123// CHECK-NEXT: Counter values: 3558785124// CHECK-NEXT: At Index 1:125// CHECK-NEXT: Guid: 5578595117440393467126// CHECK-NEXT: Entries: 1832357127// CHECK-NEXT: 1 counters and 0 callsites128// CHECK-NEXT: Counter values: 1832357129// CHECK-NEXT: At Index 2:130// CHECK-NEXT: Guid: 3950394326069683896131// CHECK-NEXT: Entries: 5391142132// CHECK-NEXT: 1 counters and 0 callsites133// CHECK-NEXT: Counter values: 5391142134// CHECK-NEXT: Entering Root 3950394326069683896 with total entry count 11226401135// CHECK-NEXT: Guid: 3950394326069683896136// CHECK-NEXT: Entries: 10767423137// CHECK-NEXT: 1 counters and 0 callsites138// CHECK-NEXT: Counter values: 10767423139// CHECK-NEXT: Exited Context Section140// CHECK-NEXT: Entered Flat Section141// CHECK-NEXT: Flat: 2597020043743142491 1142// CHECK-NEXT: Flat: 4321328481998485159 1143// CHECK-NEXT: Flat: 8657661246551306189 9114175,18099613144// CHECK-NEXT: Flat: 434762725428799310 10574815145// CHECK-NEXT: Flat: 5578595117440393467 5265754146// CHECK-NEXT: Flat: 12566320182004153844 1147// CHECK-NEXT: Exited Flat Section148 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 call186 // it directly.187 return !profileWriter();188}189