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: x86_64-target-arch 12 // REQUIRES: built-in-llvm-tree 13 14 #include "xray/xray_log_interface.h" 15 #include <cassert> 16 #include <iostream> 17 18 uint64_t var = 0; 19 uint64_t buffers = 0; f()20[[clang::xray_always_instrument]] void __attribute__((noinline)) f() { ++var; } 21 main(int argc,char * argv[])22int main(int argc, char *argv[]) { 23 assert(__xray_log_select_mode("xray-fdr") == 24 XRayLogRegisterStatus::XRAY_REGISTRATION_OK); 25 auto status = __xray_log_init_mode( 26 "xray-fdr", 27 "buffer_size=4096:buffer_max=10:func_duration_threshold_us=0"); 28 assert(status == XRayLogInitStatus::XRAY_LOG_INITIALIZED); 29 __xray_patch(); 30 31 // Create enough entries. 32 for (int i = 0; i != 1 << 20; ++i) { 33 f(); 34 } 35 36 // Then we want to verify that we're getting 10 buffers outside of the initial 37 // header. 38 auto finalize_status = __xray_log_finalize(); 39 assert(finalize_status == XRayLogInitStatus::XRAY_LOG_FINALIZED); 40 auto process_status = 41 __xray_log_process_buffers([](const char *, XRayBuffer) { ++buffers; }); 42 std::cout << "buffers = " << buffers << std::endl; 43 assert(process_status == XRayLogFlushStatus::XRAY_LOG_FLUSHED); 44 auto flush_status = __xray_log_flushLog(); 45 assert(flush_status == XRayLogFlushStatus::XRAY_LOG_FLUSHED); 46 // We expect 11 buffers because 1 header buffer + 10 actual FDR buffers. 47 // CHECK: Buffers = 11 48 std::cout << "Buffers = " << buffers << std::endl; 49 50 // In this test we ensure that we can restart the cycle after the flush. 51 status = __xray_log_init_mode( 52 "xray-fdr", 53 "buffer_size=4096:buffer_max=10:func_duration_threshold_us=0"); 54 assert(status == XRayLogInitStatus::XRAY_LOG_INITIALIZED); 55 __xray_patch(); 56 57 // Create enough entries. 58 for (int i = 0; i != 1 << 20; ++i) { 59 f(); 60 } 61 62 // Then we want to verify that we're getting 10 buffers outside of the initial 63 // header. 64 finalize_status = __xray_log_finalize(); 65 assert(finalize_status == XRayLogInitStatus::XRAY_LOG_FINALIZED); 66 process_status = 67 __xray_log_process_buffers([](const char *, XRayBuffer) { ++buffers; }); 68 std::cout << "buffers = " << buffers << std::endl; 69 assert(process_status == XRayLogFlushStatus::XRAY_LOG_FLUSHED); 70 flush_status = __xray_log_flushLog(); 71 assert(flush_status == XRayLogFlushStatus::XRAY_LOG_FLUSHED); 72 // We expect 22 buffers because 1 header buffer + 10 actual FDR buffers, plus 73 // the number of buffers we got from the previous run (also 11). 74 // CHECK: Buffers = 22 75 std::cout << "Buffers = " << buffers << std::endl; 76 } 77