xref: /llvm-project/compiler-rt/test/xray/TestCases/Posix/profiling-single-threaded.cpp (revision f7624b080a6d1f379042261a61b4934f000fd2b1)
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: built-in-llvm-tree
15 
16 #include "xray/xray_interface.h"
17 #include "xray/xray_log_interface.h"
18 #include <cassert>
19 #include <cstdio>
20 #include <string>
21 
f2()22 [[clang::xray_always_instrument]] void f2() { return; }
f1()23 [[clang::xray_always_instrument]] void f1() { f2(); }
f0()24 [[clang::xray_always_instrument]] void f0() { f1(); }
25 
26 using namespace std;
27 
28 volatile int buffer_counter = 0;
29 
process_buffer(const char *,XRayBuffer)30 [[clang::xray_never_instrument]] void process_buffer(const char *, XRayBuffer) {
31   // FIXME: Actually assert the contents of the buffer.
32   ++buffer_counter;
33 }
34 
main(int,char **)35 [[clang::xray_always_instrument]] int main(int, char **) {
36   assert(__xray_log_select_mode("xray-profiling") ==
37          XRayLogRegisterStatus::XRAY_REGISTRATION_OK);
38   assert(__xray_log_get_current_mode() != nullptr);
39   std::string current_mode = __xray_log_get_current_mode();
40   assert(current_mode == "xray-profiling");
41   assert(__xray_patch() == XRayPatchingStatus::SUCCESS);
42   assert(__xray_log_init_mode("xray-profiling", "") ==
43          XRayLogInitStatus::XRAY_LOG_INITIALIZED);
44   f0();
45   assert(__xray_log_finalize() == XRayLogInitStatus::XRAY_LOG_FINALIZED);
46   f0();
47   assert(__xray_log_process_buffers(process_buffer) ==
48          XRayLogFlushStatus::XRAY_LOG_FLUSHED);
49   // There's always at least one buffer, containing the profile file header. We
50   // assert that we have two, to indicate that we're expecting exactly one
51   // thread's worth of data.
52   assert(buffer_counter == 2);
53   assert(__xray_log_flushLog() == XRayLogFlushStatus::XRAY_LOG_FLUSHED);
54 
55   // Let's reset the counter.
56   buffer_counter = 0;
57 
58   assert(__xray_log_init_mode("xray-profiling", "") ==
59          XRayLogInitStatus::XRAY_LOG_INITIALIZED);
60   f0();
61   assert(__xray_log_finalize() == XRayLogInitStatus::XRAY_LOG_FINALIZED);
62   f0();
63   assert(__xray_log_process_buffers(process_buffer) ==
64          XRayLogFlushStatus::XRAY_LOG_FLUSHED);
65   assert(buffer_counter == 2);
66   assert(__xray_log_flushLog() == XRayLogFlushStatus::XRAY_LOG_FLUSHED);
67 }
68