1*6db8c59fSFangrui Song // RUN: rm -rf %t && mkdir %t
2*6db8c59fSFangrui Song // RUN: %clangxx_xray -g -std=c++11 %s -o %t.exe
3*6db8c59fSFangrui Song // RUN: XRAY_OPTIONS="patch_premain=false \
4*6db8c59fSFangrui Song // RUN:    xray_logfile_base=%t/ xray_mode=xray-fdr verbosity=1" \
5*6db8c59fSFangrui Song // RUN:    XRAY_FDR_OPTIONS=func_duration_threshold_us=0 %run %t.exe 2>&1 | \
6*6db8c59fSFangrui Song // RUN:    FileCheck %s
7*6db8c59fSFangrui Song // RUN: %llvm_xray convert --symbolize --output-format=yaml -instr_map=%t.exe %t/*
8*6db8c59fSFangrui Song // RUN: %llvm_xray convert --symbolize --output-format=yaml -instr_map=%t.exe %t/* | \
9*6db8c59fSFangrui Song // RUN:   FileCheck %s --check-prefix TRACE
10*6db8c59fSFangrui Song // FIXME: Make llvm-xray work on non-x86_64 as well.
11*6db8c59fSFangrui Song // REQUIRES: x86_64-target-arch
12*6db8c59fSFangrui Song // REQUIRES: built-in-llvm-tree
13*6db8c59fSFangrui Song 
14*6db8c59fSFangrui Song #include "xray/xray_log_interface.h"
15*6db8c59fSFangrui Song #include <atomic>
16*6db8c59fSFangrui Song #include <cassert>
17*6db8c59fSFangrui Song #include <thread>
18*6db8c59fSFangrui Song 
19*6db8c59fSFangrui Song std::atomic<uint64_t> var{0};
20*6db8c59fSFangrui Song 
f1()21*6db8c59fSFangrui Song [[clang::xray_always_instrument]] void __attribute__((noinline)) f1() {
22*6db8c59fSFangrui Song   for (auto i = 0; i < 1 << 20; ++i)
23*6db8c59fSFangrui Song     ++var;
24*6db8c59fSFangrui Song }
25*6db8c59fSFangrui Song 
f2()26*6db8c59fSFangrui Song [[clang::xray_always_instrument]] void __attribute__((noinline)) f2() {
27*6db8c59fSFangrui Song   for (auto i = 0; i < 1 << 20; ++i)
28*6db8c59fSFangrui Song     ++var;
29*6db8c59fSFangrui Song }
30*6db8c59fSFangrui Song 
main(int argc,char * argv[])31*6db8c59fSFangrui Song int main(int argc, char *argv[]) {
32*6db8c59fSFangrui Song   __xray_patch();
33*6db8c59fSFangrui Song   assert(__xray_log_init_mode("xray-fdr", "") ==
34*6db8c59fSFangrui Song          XRayLogInitStatus::XRAY_LOG_INITIALIZED);
35*6db8c59fSFangrui Song 
36*6db8c59fSFangrui Song   std::atomic_thread_fence(std::memory_order_acq_rel);
37*6db8c59fSFangrui Song 
38*6db8c59fSFangrui Song   {
39*6db8c59fSFangrui Song     std::thread t1([] { f1(); });
40*6db8c59fSFangrui Song     std::thread t2([] { f2(); });
41*6db8c59fSFangrui Song     t1.join();
42*6db8c59fSFangrui Song     t2.join();
43*6db8c59fSFangrui Song   }
44*6db8c59fSFangrui Song 
45*6db8c59fSFangrui Song   std::atomic_thread_fence(std::memory_order_acq_rel);
46*6db8c59fSFangrui Song   __xray_log_finalize();
47*6db8c59fSFangrui Song   __xray_log_flushLog();
48*6db8c59fSFangrui Song   __xray_unpatch();
49*6db8c59fSFangrui Song   return var > 0 ? 0 : 1;
50*6db8c59fSFangrui Song   // CHECK: {{.*}}XRay: Log file in '{{.*}}'
51*6db8c59fSFangrui Song   // CHECK-NOT: Failed
52*6db8c59fSFangrui Song }
53*6db8c59fSFangrui Song 
54*6db8c59fSFangrui Song // We want to make sure that the order of the function log doesn't matter.
55*6db8c59fSFangrui Song // TRACE-DAG: - { type: 0, func-id: [[FID1:[0-9]+]], function: {{.*f1.*}}, cpu: {{.*}}, thread: [[THREAD1:[0-9]+]], process: [[PROCESS:[0-9]+]], kind: function-enter, tsc: {{[0-9]+}}, data: '' }
56*6db8c59fSFangrui Song // TRACE-DAG: - { type: 0, func-id: [[FID2:[0-9]+]], function: {{.*f2.*}}, cpu: {{.*}}, thread: [[THREAD2:[0-9]+]], process: [[PROCESS]], kind: function-enter, tsc: {{[0-9]+}}, data: '' }
57*6db8c59fSFangrui Song // TRACE-DAG: - { type: 0, func-id: [[FID1]], function: {{.*f1.*}}, cpu: {{.*}}, thread: [[THREAD1]], process: [[PROCESS]], kind: {{function-exit|function-tail-exit}}, tsc: {{[0-9]+}}, data: '' }
58*6db8c59fSFangrui Song // TRACE-DAG: - { type: 0, func-id: [[FID2]], function: {{.*f2.*}}, cpu: {{.*}}, thread: [[THREAD2]], process: [[PROCESS]], kind: {{function-exit|function-tail-exit}}, tsc: {{[0-9]+}}, data: '' }
59