1 | // RUN: %clangxx_xray -g -std=c++11 %s -o %t -fxray-modes=xray-fdr |
2 | // RUN: rm -f fdr-inmemory-test-* |
3 | // RUN: XRAY_OPTIONS="patch_premain=false xray_logfile_base=fdr-inmemory-test- \ |
4 | // RUN: verbosity=1" \ |
5 | // RUN: XRAY_FDR_OPTIONS="no_file_flush=true func_duration_threshold_us=0" \ |
6 | // RUN: %run %t 2>&1 | FileCheck %s |
7 | // RUN: FILES=`find %T -name 'fdr-inmemory-test-*' | wc -l` |
8 | // RUN: [ $FILES -eq 0 ] |
9 | // RUN: rm -f fdr-inmemory-test-* |
10 | // |
11 | // REQUIRES: built-in-llvm-tree |
12 | |
13 | #include "xray/xray_log_interface.h" |
14 | #include <cassert> |
15 | #include <iostream> |
16 | |
17 | uint64_t var = 0; |
18 | uint64_t buffers = 0; |
19 | [[clang::xray_always_instrument]] void __attribute__((noinline)) f() { ++var; } |
20 | |
21 | int main(int argc, char *argv[]) { |
22 | assert(__xray_log_select_mode("xray-fdr" ) == |
23 | XRayLogRegisterStatus::XRAY_REGISTRATION_OK); |
24 | auto status = __xray_log_init_mode( |
25 | Mode: "xray-fdr" , |
26 | Config: "buffer_size=4096:buffer_max=10:func_duration_threshold_us=0" ); |
27 | assert(status == XRayLogInitStatus::XRAY_LOG_INITIALIZED); |
28 | __xray_patch(); |
29 | |
30 | // Create enough entries. |
31 | for (int i = 0; i != 1 << 20; ++i) { |
32 | f(); |
33 | } |
34 | |
35 | // Then we want to verify that we're getting 10 buffers outside of the initial |
36 | // header. |
37 | auto finalize_status = __xray_log_finalize(); |
38 | assert(finalize_status == XRayLogInitStatus::XRAY_LOG_FINALIZED); |
39 | auto process_status = |
40 | __xray_log_process_buffers(Processor: [](const char *, XRayBuffer) { ++buffers; }); |
41 | std::cout << "buffers = " << buffers << std::endl; |
42 | assert(process_status == XRayLogFlushStatus::XRAY_LOG_FLUSHED); |
43 | auto flush_status = __xray_log_flushLog(); |
44 | assert(flush_status == XRayLogFlushStatus::XRAY_LOG_FLUSHED); |
45 | // We expect 11 buffers because 1 header buffer + 10 actual FDR buffers. |
46 | // CHECK: Buffers = 11 |
47 | std::cout << "Buffers = " << buffers << std::endl; |
48 | |
49 | // In this test we ensure that we can restart the cycle after the flush. |
50 | status = __xray_log_init_mode( |
51 | Mode: "xray-fdr" , |
52 | Config: "buffer_size=4096:buffer_max=10:func_duration_threshold_us=0" ); |
53 | assert(status == XRayLogInitStatus::XRAY_LOG_INITIALIZED); |
54 | __xray_patch(); |
55 | |
56 | // Create enough entries. |
57 | for (int i = 0; i != 1 << 20; ++i) { |
58 | f(); |
59 | } |
60 | |
61 | // Then we want to verify that we're getting 10 buffers outside of the initial |
62 | // header. |
63 | finalize_status = __xray_log_finalize(); |
64 | assert(finalize_status == XRayLogInitStatus::XRAY_LOG_FINALIZED); |
65 | process_status = |
66 | __xray_log_process_buffers(Processor: [](const char *, XRayBuffer) { ++buffers; }); |
67 | std::cout << "buffers = " << buffers << std::endl; |
68 | assert(process_status == XRayLogFlushStatus::XRAY_LOG_FLUSHED); |
69 | flush_status = __xray_log_flushLog(); |
70 | assert(flush_status == XRayLogFlushStatus::XRAY_LOG_FLUSHED); |
71 | // We expect 22 buffers because 1 header buffer + 10 actual FDR buffers, plus |
72 | // the number of buffers we got from the previous run (also 11). |
73 | // CHECK: Buffers = 22 |
74 | std::cout << "Buffers = " << buffers << std::endl; |
75 | } |
76 | |