[compiler-rt] r298977 - [XRay][compiler-rt] Add an end-to-end test for FDR Logging

Dean Michael Berris via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 28 22:19:25 PDT 2017


Author: dberris
Date: Wed Mar 29 00:19:24 2017
New Revision: 298977

URL: http://llvm.org/viewvc/llvm-project?rev=298977&view=rev
Log:
[XRay][compiler-rt] Add an end-to-end test for FDR Logging

Summary:
This change exercises the end-to-end functionality defined in the FDR
logging implementation. We also prepare for being able to run traces
generated by the FDR logging implementation from being analysed with the
llvm-xray command that comes with the LLVM distribution.

This also unblocks D31385, D31384, and D31345.

Reviewers: kpw, pelikan

Subscribers: llvm-commits, mgorny

Differential Revision: https://reviews.llvm.org/D31452

Added:
    compiler-rt/trunk/test/xray/TestCases/Linux/fdr-mode.cc
Modified:
    compiler-rt/trunk/include/CMakeLists.txt
    compiler-rt/trunk/include/xray/xray_log_interface.h
    compiler-rt/trunk/lib/xray/xray_fdr_logging.cc
    compiler-rt/trunk/lib/xray/xray_fdr_logging.h
    compiler-rt/trunk/lib/xray/xray_log_interface.cc
    compiler-rt/trunk/lib/xray/xray_utils.cc
    compiler-rt/trunk/test/xray/lit.cfg

Modified: compiler-rt/trunk/include/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/include/CMakeLists.txt?rev=298977&r1=298976&r2=298977&view=diff
==============================================================================
--- compiler-rt/trunk/include/CMakeLists.txt (original)
+++ compiler-rt/trunk/include/CMakeLists.txt Wed Mar 29 00:19:24 2017
@@ -12,7 +12,8 @@ set(SANITIZER_HEADERS
   sanitizer/tsan_interface_atomic.h)
 
 set(XRAY_HEADERS
-  xray/xray_interface.h)
+  xray/xray_interface.h
+  xray/xray_log_interface.h)
 
 set(COMPILER_RT_HEADERS
   ${SANITIZER_HEADERS}

Modified: compiler-rt/trunk/include/xray/xray_log_interface.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/include/xray/xray_log_interface.h?rev=298977&r1=298976&r2=298977&view=diff
==============================================================================
--- compiler-rt/trunk/include/xray/xray_log_interface.h (original)
+++ compiler-rt/trunk/include/xray/xray_log_interface.h Wed Mar 29 00:19:24 2017
@@ -48,4 +48,13 @@ XRayLogFlushStatus __xray_log_flushLog()
 
 } // extern "C"
 
+namespace __xray {
+// Options used by the LLVM XRay FDR implementation.
+struct FDRLoggingOptions {
+  bool ReportErrors = false;
+  int Fd = -1;
+};
+
+} // namespace __xray
+
 #endif // XRAY_XRAY_LOG_INTERFACE_H

Modified: compiler-rt/trunk/lib/xray/xray_fdr_logging.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/xray/xray_fdr_logging.cc?rev=298977&r1=298976&r2=298977&view=diff
==============================================================================
--- compiler-rt/trunk/lib/xray/xray_fdr_logging.cc (original)
+++ compiler-rt/trunk/lib/xray/xray_fdr_logging.cc Wed Mar 29 00:19:24 2017
@@ -51,19 +51,18 @@ std::unique_ptr<FDRLoggingOptions> FDROp
 XRayLogInitStatus fdrLoggingInit(std::size_t BufferSize, std::size_t BufferMax,
                                  void *Options,
                                  size_t OptionsSize) XRAY_NEVER_INSTRUMENT {
-  assert(OptionsSize == sizeof(FDRLoggingOptions));
+  if (OptionsSize != sizeof(FDRLoggingOptions))
+    return static_cast<XRayLogInitStatus>(__sanitizer::atomic_load(
+        &LoggingStatus, __sanitizer::memory_order_acquire));
   s32 CurrentStatus = XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;
-  if (__sanitizer::atomic_compare_exchange_strong(
+  if (!__sanitizer::atomic_compare_exchange_strong(
           &LoggingStatus, &CurrentStatus,
           XRayLogInitStatus::XRAY_LOG_INITIALIZING,
           __sanitizer::memory_order_release))
     return static_cast<XRayLogInitStatus>(CurrentStatus);
 
   FDROptions.reset(new FDRLoggingOptions());
-  *FDROptions = *reinterpret_cast<FDRLoggingOptions *>(Options);
-  if (FDROptions->ReportErrors)
-    SetPrintfAndReportCallback(printToStdErr);
-
+  memcpy(FDROptions.get(), Options, OptionsSize);
   bool Success = false;
   BQ = std::make_shared<BufferQueue>(BufferSize, BufferMax, Success);
   if (!Success) {
@@ -77,6 +76,7 @@ XRayLogInitStatus fdrLoggingInit(std::si
   __sanitizer::atomic_store(&LoggingStatus,
                             XRayLogInitStatus::XRAY_LOG_INITIALIZED,
                             __sanitizer::memory_order_release);
+  Report("XRay FDR init successful.\n");
   return XRayLogInitStatus::XRAY_LOG_INITIALIZED;
 }
 
@@ -88,7 +88,7 @@ XRayLogFlushStatus fdrLoggingFlush() XRA
     return XRayLogFlushStatus::XRAY_LOG_NOT_FLUSHING;
 
   s32 Result = XRayLogFlushStatus::XRAY_LOG_NOT_FLUSHING;
-  if (__sanitizer::atomic_compare_exchange_strong(
+  if (!__sanitizer::atomic_compare_exchange_strong(
           &LogFlushStatus, &Result, XRayLogFlushStatus::XRAY_LOG_FLUSHING,
           __sanitizer::memory_order_release))
     return static_cast<XRayLogFlushStatus>(Result);
@@ -140,7 +140,7 @@ XRayLogFlushStatus fdrLoggingFlush() XRA
 
 XRayLogInitStatus fdrLoggingFinalize() XRAY_NEVER_INSTRUMENT {
   s32 CurrentStatus = XRayLogInitStatus::XRAY_LOG_INITIALIZED;
-  if (__sanitizer::atomic_compare_exchange_strong(
+  if (!__sanitizer::atomic_compare_exchange_strong(
           &LoggingStatus, &CurrentStatus,
           XRayLogInitStatus::XRAY_LOG_FINALIZING,
           __sanitizer::memory_order_release))
@@ -168,8 +168,7 @@ XRayLogInitStatus fdrLoggingReset() XRAY
   BQ.reset();
 
   // Spin until the flushing status is flushed.
-  s32 CurrentFlushingStatus =
-      XRayLogFlushStatus::XRAY_LOG_FLUSHED;
+  s32 CurrentFlushingStatus = XRayLogFlushStatus::XRAY_LOG_FLUSHED;
   while (__sanitizer::atomic_compare_exchange_weak(
       &LogFlushStatus, &CurrentFlushingStatus,
       XRayLogFlushStatus::XRAY_LOG_NOT_FLUSHING,

Modified: compiler-rt/trunk/lib/xray/xray_fdr_logging.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/xray/xray_fdr_logging.h?rev=298977&r1=298976&r2=298977&view=diff
==============================================================================
--- compiler-rt/trunk/lib/xray/xray_fdr_logging.h (original)
+++ compiler-rt/trunk/lib/xray/xray_fdr_logging.h Wed Mar 29 00:19:24 2017
@@ -26,14 +26,6 @@
 // default mode of always writing fixed-size records.
 
 namespace __xray {
-
-// Options used by the FDR implementation.
-struct FDRLoggingOptions {
-  bool ReportErrors = false;
-  int Fd = -1;
-};
-
-// Flight Data Recorder mode implementation interfaces.
 XRayLogInitStatus fdrLoggingInit(size_t BufferSize, size_t BufferMax,
                                  void *Options, size_t OptionsSize);
 XRayLogInitStatus fdrLoggingFinalize();

Modified: compiler-rt/trunk/lib/xray/xray_log_interface.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/xray/xray_log_interface.cc?rev=298977&r1=298976&r2=298977&view=diff
==============================================================================
--- compiler-rt/trunk/lib/xray/xray_log_interface.cc (original)
+++ compiler-rt/trunk/lib/xray/xray_log_interface.cc Wed Mar 29 00:19:24 2017
@@ -35,8 +35,9 @@ void __xray_set_log_impl(XRayLogImpl Imp
   *GlobalXRayImpl = Impl;
 }
 
-XRayLogInitStatus __xray_init(size_t BufferSize, size_t MaxBuffers, void *Args,
-                              size_t ArgsSize) XRAY_NEVER_INSTRUMENT {
+XRayLogInitStatus __xray_log_init(size_t BufferSize, size_t MaxBuffers,
+                                  void *Args,
+                                  size_t ArgsSize) XRAY_NEVER_INSTRUMENT {
   __sanitizer::SpinMutexLock Guard(&XRayImplMutex);
   if (!GlobalXRayImpl)
     return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;

Modified: compiler-rt/trunk/lib/xray/xray_utils.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/xray/xray_utils.cc?rev=298977&r1=298976&r2=298977&view=diff
==============================================================================
--- compiler-rt/trunk/lib/xray/xray_utils.cc (original)
+++ compiler-rt/trunk/lib/xray/xray_utils.cc Wed Mar 29 00:19:24 2017
@@ -93,8 +93,6 @@ bool readValueFromFile(const char *Filen
 }
 
 int getLogFD() XRAY_NEVER_INSTRUMENT {
-  // FIXME: Figure out how to make this less stderr-dependent.
-  SetPrintfAndReportCallback(printToStdErr);
   // Open a temporary file once for the log.
   static char TmpFilename[256] = {};
   static char TmpWildcardPattern[] = "XXXXXX";
@@ -119,8 +117,7 @@ int getLogFD() XRAY_NEVER_INSTRUMENT {
            TmpFilename);
     return -1;
   }
-  if (Verbosity())
-    fprintf(stderr, "XRay: Log file in '%s'\n", TmpFilename);
+  Report("XRay: Log file in '%s'\n", TmpFilename);
 
   return Fd;
 }

Added: compiler-rt/trunk/test/xray/TestCases/Linux/fdr-mode.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/xray/TestCases/Linux/fdr-mode.cc?rev=298977&view=auto
==============================================================================
--- compiler-rt/trunk/test/xray/TestCases/Linux/fdr-mode.cc (added)
+++ compiler-rt/trunk/test/xray/TestCases/Linux/fdr-mode.cc Wed Mar 29 00:19:24 2017
@@ -0,0 +1,62 @@
+// RUN: %clangxx_xray -std=c++11 %s -o %t
+// RUN: XRAY_OPTIONS="patch_premain=false xray_naive_log=false xray_logfile_base=fdr-logging-test- xray_fdr_log=true verbosity=1" %run %t 2>&1 | FileCheck %s
+// FIXME: %llvm_xray convert -instr_map=%t "`ls fdr-logging-test-* | head -1`" | FileCheck %s --check-prefix TRACE
+// RUN: rm fdr-logging-test-*
+
+#include "xray/xray_log_interface.h"
+#include <cassert>
+#include <chrono>
+#include <cstdio>
+#include <iostream>
+#include <stdlib.h>
+#include <thread>
+#include <time.h>
+
+constexpr auto kBufferSize = 16384;
+constexpr auto kBufferMax = 10;
+
+thread_local uint64_t var = 0;
+[[clang::xray_always_instrument]] void __attribute__((noinline)) fC() { ++var; }
+
+[[clang::xray_always_instrument]] void __attribute__((noinline)) fB() { fC(); }
+
+[[clang::xray_always_instrument]] void __attribute__((noinline)) fA() { fB(); }
+
+int main(int argc, char *argv[]) {
+  using namespace __xray;
+  FDRLoggingOptions Options;
+  std::cout << "Logging before init." << std::endl;
+  // CHECK: Logging before init.
+  auto status = __xray_log_init(kBufferSize, kBufferMax, &Options,
+                                sizeof(FDRLoggingOptions));
+  assert(status == XRayLogInitStatus::XRAY_LOG_INITIALIZED);
+  std::cout << "Init status " << status << std::endl;
+  // CHECK: Init status {{.*}}
+  std::cout << "Patching..." << std::endl;
+  // CHECK: Patching...
+  __xray_patch();
+  fA();
+  fC();
+  fB();
+  fA();
+  fC();
+  std::thread other_thread([]() {
+    fC();
+    fB();
+    fA();
+  });
+  other_thread.join();
+  std::cout << "Joined" << std::endl;
+  // CHECK: Joined
+  std::cout << "Finalize status " << __xray_log_finalize() << std::endl;
+  // CHECK: Finalize status {{.*}}
+  fC();
+  std::cout << "Main execution var = " << var << std::endl;
+  // CHECK: Main execution var = 6
+  std::cout << "Flush status " << __xray_log_flushLog() << std::endl;
+  // CHECK: Flush status {{.*}}
+  __xray_unpatch();
+  return 0;
+}
+
+// TRACE: { function }

Modified: compiler-rt/trunk/test/xray/lit.cfg
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/xray/lit.cfg?rev=298977&r1=298976&r2=298977&view=diff
==============================================================================
--- compiler-rt/trunk/test/xray/lit.cfg (original)
+++ compiler-rt/trunk/test/xray/lit.cfg Wed Mar 29 00:19:24 2017
@@ -16,6 +16,9 @@ clang_xray_cxxflags = config.cxx_mode_fl
 def build_invocation(compile_flags):
   return ' ' + ' '.join([config.clang] + compile_flags) + ' '
 
+# Assume that llvm-xray is in the config.llvm_tools_dir.
+llvm_xray = os.path.join(config.llvm_tools_dir, 'llvm-xray')
+
 # Setup substitutions.
 config.substitutions.append(
     ('%clang ', build_invocation([config.target_cflags])))
@@ -26,6 +29,8 @@ config.substitutions.append(
     ('%clang_xray ', build_invocation(clang_xray_cflags)))
 config.substitutions.append(
     ('%clangxx_xray', build_invocation(clang_xray_cxxflags)))
+config.substitutions.append(
+    ('%llvm_xray', llvm_xray))
 
 # Default test suffixes.
 config.suffixes = ['.c', '.cc', '.cpp']




More information about the llvm-commits mailing list