115 lines · cpp
1// RUN: %clangxx_xray -g -std=c++11 %s -o %t2// RUN: rm -f fdr-logging-test-*3// RUN: rm -f fdr-unwrite-test-*4// RUN: env XRAY_OPTIONS="patch_premain=false xray_logfile_base=fdr-logging-test- \5// RUN: xray_mode=xray-fdr verbosity=1" \6// RUN: env XRAY_FDR_OPTIONS="func_duration_threshold_us=0" \7// RUN: %run %t 2>&1 | FileCheck %s8// RUN: env XRAY_OPTIONS="patch_premain=false \9// RUN: xray_logfile_base=fdr-unwrite-test- xray_mode=xray-fdr \10// RUN: verbosity=1" \11// RUN: env XRAY_FDR_OPTIONS="func_duration_threshold_us=5000" \12// RUN: %run %t 2>&1 | FileCheck %s13// RUN: ls fdr-logging-test-* | head -1 | tr -d '\n' > %t.log14// RUN: %llvm_xray convert --symbolize --output-format=yaml -instr_map=%t \15// RUN: "%{readfile:%t.log}" \16// RUN: | FileCheck %s --check-prefix=TRACE17// RUN: ls fdr-unwrite-test-* | head -1 | tr -d '\n' > %t.log18// RUN: %llvm_xray convert --symbolize --output-format=yaml -instr_map=%t \19// RUN: "%{readfile:%t.log}" \20// RUN: | FileCheck %s --check-prefix=UNWRITE21// RUN: rm fdr-logging-test-*22// RUN: rm fdr-unwrite-test-*23// UNSUPPORTED: target=powerpc64le-{{.*}}24/// TODO: FDR logging arg1 handler(__xray_ArgLoggerEntry) hasn't implemented yet on LoongArch25// UNSUPPORTED: target=loongarch64{{.*}}26// REQUIRES: built-in-llvm-tree27 28#include "xray/xray_log_interface.h"29#include <cassert>30#include <chrono>31#include <cstdio>32#include <iostream>33#include <stdlib.h>34#include <thread>35#include <time.h>36 37thread_local uint64_t var = 0;38[[clang::xray_always_instrument]] void __attribute__((noinline)) fC() { ++var; }39 40[[clang::xray_always_instrument]] void __attribute__((noinline)) fB() { fC(); }41 42[[clang::xray_always_instrument]] void __attribute__((noinline)) fA() { fB(); }43 44[[clang::xray_always_instrument, clang::xray_log_args(1)]]45void __attribute__((noinline)) fArg(int) { }46 47int main(int argc, char *argv[]) {48 std::cout << "Logging before init." << std::endl;49 // CHECK: Logging before init.50 assert(__xray_log_select_mode("xray-fdr") ==51 XRayLogRegisterStatus::XRAY_REGISTRATION_OK);52 auto status =53 __xray_log_init_mode("xray-fdr", "buffer_size=16384:buffer_max=10");54 assert(status == XRayLogInitStatus::XRAY_LOG_INITIALIZED);55 std::cout << "Init status " << status << std::endl;56 // CHECK: Init status {{.*}}57 std::cout << "Patching..." << std::endl;58 // CHECK: Patching...59 __xray_patch();60 fA();61 fC();62 fB();63 fA();64 fC();65 std::thread other_thread([]() {66 fC();67 fB();68 fA();69 fArg(1);70 });71 other_thread.join();72 std::cout << "Joined" << std::endl;73 // CHECK: Joined74 std::cout << "Finalize status " << __xray_log_finalize() << std::endl;75 // CHECK: Finalize status {{.*}}76 fC();77 std::cout << "Main execution var = " << var << std::endl;78 // CHECK: Main execution var = 679 std::cout << "Flush status " << __xray_log_flushLog() << std::endl;80 // CHECK: Flush status {{.*}}81 __xray_unpatch();82 return 0;83}84 85// Check that we're able to see two threads, each entering and exiting fA().86// TRACE-DAG: - { type: 0, func-id: [[FIDA:[0-9]+]], function: {{.*fA.*}}, cpu: {{.*}}, thread: [[THREAD1:[0-9]+]], process: [[PROCESS:[0-9]+]], kind: function-enter, tsc: {{[0-9]+}}, data: '' }87// TRACE: - { type: 0, func-id: [[FIDA]], function: {{.*fA.*}}, cpu: {{.*}}, thread: [[THREAD1]], process: [[PROCESS]], kind: function-{{exit|tail-exit}}, tsc: {{[0-9]+}}, data: '' }88// TRACE-DAG: - { type: 0, func-id: [[FIDA]], function: {{.*fA.*}}, cpu: {{.*}}, thread: [[THREAD2:[0-9]+]], process: [[PROCESS]], kind: function-enter, tsc: {{[0-9]+}}, data: '' }89// TRACE: - { type: 0, func-id: [[FIDA]], function: {{.*fA.*}}, cpu: {{.*}}, thread: [[THREAD2]], process: [[PROCESS]], kind: function-{{exit|tail-exit}}, tsc: {{[0-9]+}}, data: '' }90//91// Do the same as above for fC()92// TRACE-DAG: - { type: 0, func-id: [[FIDC:[0-9]+]], function: {{.*fC.*}}, cpu: {{.*}}, thread: [[THREAD1:[0-9]+]], process: [[PROCESS]], kind: function-enter, tsc: {{[0-9]+}}, data: '' }93// TRACE: - { type: 0, func-id: [[FIDC]], function: {{.*fC.*}}, cpu: {{.*}}, thread: [[THREAD1]], process: [[PROCESS]], kind: function-{{exit|tail-exit}}, tsc: {{[0-9]+}}, data: '' }94// TRACE-DAG: - { type: 0, func-id: [[FIDC]], function: {{.*fC.*}}, cpu: {{.*}}, thread: [[THREAD2:[0-9]+]], process: [[PROCESS]], kind: function-enter, tsc: {{[0-9]+}}, data: '' }95// TRACE: - { type: 0, func-id: [[FIDC]], function: {{.*fC.*}}, cpu: {{.*}}, thread: [[THREAD2]], process: [[PROCESS]], kind: function-{{exit|tail-exit}}, tsc: {{[0-9]+}}, data: '' }96 97// Do the same as above for fB()98// TRACE-DAG: - { type: 0, func-id: [[FIDB:[0-9]+]], function: {{.*fB.*}}, cpu: {{.*}}, thread: [[THREAD1:[0-9]+]], process: [[PROCESS]], kind: function-enter, tsc: {{[0-9]+}}, data: '' }99// TRACE: - { type: 0, func-id: [[FIDB]], function: {{.*fB.*}}, cpu: {{.*}}, thread: [[THREAD1]], process: [[PROCESS]], kind: function-{{exit|tail-exit}}, tsc: {{[0-9]+}}, data: '' }100// TRACE-DAG: - { type: 0, func-id: [[FIDB]], function: {{.*fB.*}}, cpu: {{.*}}, thread: [[THREAD2:[0-9]+]], process: [[PROCESS]], kind: function-enter, tsc: {{[0-9]+}}, data: '' }101// TRACE: - { type: 0, func-id: [[FIDB]], function: {{.*fB.*}}, cpu: {{.*}}, thread: [[THREAD2]], process: [[PROCESS]], kind: function-{{exit|tail-exit}}, tsc: {{[0-9]+}}, data: '' }102 103// TRACE-DAG: - { type: 0, func-id: [[FIDARG:[0-9]+]], function: 'fArg(int)', args: [ 1 ], cpu: {{.*}}, thread: [[THREAD2]], process: [[PROCESS]], kind: function-enter-arg, tsc: {{[0-9]+}}, data: '' }104// TRACE-DAG: - { type: 0, func-id: [[FIDARG]], function: 'fArg(int)', cpu: {{.*}}, thread: [[THREAD2]], process: [[PROCESS]], kind: function-exit, tsc: {{[0-9]+}}, data: '' }105 106// Assert that when unwriting is enabled with a high threshold time, all the function records are erased. A CPU switch could erroneously fail this test, but107// is unlikely given the test program.108// Even with a high threshold, arg1 logging is never unwritten.109// UNWRITE: header:110// UNWRITE: records:111// UNWRITE-NEXT: - { type: 0, func-id: [[FIDARG:[0-9]+]], function: 'fArg(int)', args: [ 1 ], cpu: {{.*}}, thread: [[THREAD2:[0-9]+]], process: [[PROCESS:[0-9]+]], kind: function-enter-arg, tsc: {{[0-9]+}}, data: '' }112// UNWRITE-NEXT: - { type: 0, func-id: [[FIDARG]], function: 'fArg(int)', cpu: {{.*}}, thread: [[THREAD2]], process: [[PROCESS]], kind: function-exit, tsc: {{[0-9]+}}, data: '' }113// UNWRITE-NOT: function-enter114// UNWRITE-NOT: function-{{exit|tail-exit}}115