1 // Check that we can get a profile from a single-threaded application, on
2 // demand through the XRay logging implementation API.
3 //
4 // FIXME: Make -fxray-modes=xray-profiling part of the default?
5 // RUN: %clangxx_xray -std=c++11 %s -o %t -fxray-modes=xray-profiling
6 // RUN: rm -f xray-log.profiling-single-*
7 // RUN: XRAY_OPTIONS=verbosity=1 \
8 // RUN:     XRAY_PROFILING_OPTIONS=no_flush=true %run %t
9 // RUN: XRAY_OPTIONS=verbosity=1 %run %t
10 // RUN: PROFILES=`ls xray-log.profiling-single-* | wc -l`
11 // RUN: [ $PROFILES -eq 2 ]
12 // RUN: rm -f xray-log.profiling-single-*
13 //
14 // REQUIRES: x86_64-target-arch
15 // REQUIRES: built-in-llvm-tree
16 
17 #include "xray/xray_interface.h"
18 #include "xray/xray_log_interface.h"
19 #include <cassert>
20 #include <cstdio>
21 #include <string>
22 
23 [[clang::xray_always_instrument]] void f2() { return; }
24 [[clang::xray_always_instrument]] void f1() { f2(); }
25 [[clang::xray_always_instrument]] void f0() { f1(); }
26 
27 using namespace std;
28 
29 volatile int buffer_counter = 0;
30 
31 [[clang::xray_never_instrument]] void process_buffer(const char *, XRayBuffer) {
32   // FIXME: Actually assert the contents of the buffer.
33   ++buffer_counter;
34 }
35 
36 [[clang::xray_always_instrument]] int main(int, char **) {
37   assert(__xray_log_select_mode("xray-profiling") ==
38          XRayLogRegisterStatus::XRAY_REGISTRATION_OK);
39   assert(__xray_log_get_current_mode() != nullptr);
40   std::string current_mode = __xray_log_get_current_mode();
41   assert(current_mode == "xray-profiling");
42   assert(__xray_patch() == XRayPatchingStatus::SUCCESS);
43   assert(__xray_log_init_mode("xray-profiling", "") ==
44          XRayLogInitStatus::XRAY_LOG_INITIALIZED);
45   f0();
46   assert(__xray_log_finalize() == XRayLogInitStatus::XRAY_LOG_FINALIZED);
47   f0();
48   assert(__xray_log_process_buffers(process_buffer) ==
49          XRayLogFlushStatus::XRAY_LOG_FLUSHED);
50   // There's always at least one buffer, containing the profile file header. We
51   // assert that we have two, to indicate that we're expecting exactly one
52   // thread's worth of data.
53   assert(buffer_counter == 2);
54   assert(__xray_log_flushLog() == XRayLogFlushStatus::XRAY_LOG_FLUSHED);
55 
56   // Let's reset the counter.
57   buffer_counter = 0;
58 
59   assert(__xray_log_init_mode("xray-profiling", "") ==
60          XRayLogInitStatus::XRAY_LOG_INITIALIZED);
61   f0();
62   assert(__xray_log_finalize() == XRayLogInitStatus::XRAY_LOG_FINALIZED);
63   f0();
64   assert(__xray_log_process_buffers(process_buffer) ==
65          XRayLogFlushStatus::XRAY_LOG_FLUSHED);
66   assert(buffer_counter == 2);
67   assert(__xray_log_flushLog() == XRayLogFlushStatus::XRAY_LOG_FLUSHED);
68 }
69