109 changes: 37 additions & 72 deletions compiler-rt/lib/xray/xray_fdr_logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ static BufferQueue *BQ = nullptr;
static atomic_sint32_t LogFlushStatus = {
XRayLogFlushStatus::XRAY_LOG_NOT_FLUSHING};

static FDRLoggingOptions FDROptions;

static SpinMutex FDROptionsMutex;

// This function will initialize the thread-local data structure used by the FDR
// logging implementation and return a reference to it. The implementation
// details require a bit of care to maintain.
Expand Down Expand Up @@ -875,15 +871,7 @@ XRayLogFlushStatus fdrLoggingFlush() XRAY_NEVER_INSTRUMENT {
// (fixed-sized) and let the tools reading the buffers deal with the data
// afterwards.
//
int Fd = -1;
{
// FIXME: Remove this section of the code, when we remove the struct-based
// configuration API.
SpinMutexLock Guard(&FDROptionsMutex);
Fd = FDROptions.Fd;
}
if (Fd == -1)
Fd = getLogFD();
int Fd = getLogFD();
if (Fd == -1) {
auto Result = XRayLogFlushStatus::XRAY_LOG_NOT_FLUSHING;
atomic_store(&LogFlushStatus, Result, memory_order_release);
Expand Down Expand Up @@ -1089,8 +1077,8 @@ void fdrLoggingHandleTypedEvent(
endBufferIfFull();
}

XRayLogInitStatus fdrLoggingInit(size_t BufferSize, size_t BufferMax,
void *Options,
XRayLogInitStatus fdrLoggingInit(UNUSED size_t BufferSize,
UNUSED size_t BufferMax, void *Options,
size_t OptionsSize) XRAY_NEVER_INSTRUMENT {
if (Options == nullptr)
return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;
Expand All @@ -1104,65 +1092,42 @@ XRayLogInitStatus fdrLoggingInit(size_t BufferSize, size_t BufferMax,
return static_cast<XRayLogInitStatus>(CurrentStatus);
}

// Because of __xray_log_init_mode(...) which guarantees that this will be
// called with BufferSize == 0 and BufferMax == 0 we parse the configuration
// provided in the Options pointer as a string instead.
if (BufferSize == 0 && BufferMax == 0) {
if (Verbosity())
Report("Initializing FDR mode with options: %s\n",
static_cast<const char *>(Options));

// TODO: Factor out the flags specific to the FDR mode implementation. For
// now, use the global/single definition of the flags, since the FDR mode
// flags are already defined there.
FlagParser FDRParser;
FDRFlags FDRFlags;
registerXRayFDRFlags(&FDRParser, &FDRFlags);
FDRFlags.setDefaults();

// Override first from the general XRAY_DEFAULT_OPTIONS compiler-provided
// options until we migrate everyone to use the XRAY_FDR_OPTIONS
// compiler-provided options.
FDRParser.ParseString(useCompilerDefinedFlags());
FDRParser.ParseString(useCompilerDefinedFDRFlags());
auto *EnvOpts = GetEnv("XRAY_FDR_OPTIONS");
if (EnvOpts == nullptr)
EnvOpts = "";
FDRParser.ParseString(EnvOpts);

// FIXME: Remove this when we fully remove the deprecated flags.
if (internal_strlen(EnvOpts) == 0) {
FDRFlags.func_duration_threshold_us =
flags()->xray_fdr_log_func_duration_threshold_us;
FDRFlags.grace_period_ms = flags()->xray_fdr_log_grace_period_ms;
}

// The provided options should always override the compiler-provided and
// environment-variable defined options.
FDRParser.ParseString(static_cast<const char *>(Options));
*fdrFlags() = FDRFlags;
BufferSize = FDRFlags.buffer_size;
BufferMax = FDRFlags.buffer_max;
SpinMutexLock Guard(&FDROptionsMutex);
FDROptions.Fd = -1;
FDROptions.ReportErrors = true;
} else if (OptionsSize != sizeof(FDRLoggingOptions)) {
// FIXME: This is deprecated, and should really be removed.
// At this point we use the flag parser specific to the FDR mode
// implementation.
if (Verbosity())
Report("Cannot initialize FDR logging; wrong size for options: %d\n",
OptionsSize);
return static_cast<XRayLogInitStatus>(
atomic_load(&LoggingStatus, memory_order_acquire));
} else {
if (Verbosity())
Report("XRay FDR: struct-based init is deprecated, please use "
"string-based configuration instead.\n");
SpinMutexLock Guard(&FDROptionsMutex);
internal_memcpy(&FDROptions, Options, OptionsSize);
if (Verbosity())
Report("Initializing FDR mode with options: %s\n",
static_cast<const char *>(Options));

// TODO: Factor out the flags specific to the FDR mode implementation. For
// now, use the global/single definition of the flags, since the FDR mode
// flags are already defined there.
FlagParser FDRParser;
FDRFlags FDRFlags;
registerXRayFDRFlags(&FDRParser, &FDRFlags);
FDRFlags.setDefaults();

// Override first from the general XRAY_DEFAULT_OPTIONS compiler-provided
// options until we migrate everyone to use the XRAY_FDR_OPTIONS
// compiler-provided options.
FDRParser.ParseString(useCompilerDefinedFlags());
FDRParser.ParseString(useCompilerDefinedFDRFlags());
auto *EnvOpts = GetEnv("XRAY_FDR_OPTIONS");
if (EnvOpts == nullptr)
EnvOpts = "";
FDRParser.ParseString(EnvOpts);

// FIXME: Remove this when we fully remove the deprecated flags.
if (internal_strlen(EnvOpts) == 0) {
FDRFlags.func_duration_threshold_us =
flags()->xray_fdr_log_func_duration_threshold_us;
FDRFlags.grace_period_ms = flags()->xray_fdr_log_grace_period_ms;
}

// The provided options should always override the compiler-provided and
// environment-variable defined options.
FDRParser.ParseString(static_cast<const char *>(Options));
*fdrFlags() = FDRFlags;
BufferSize = FDRFlags.buffer_size;
BufferMax = FDRFlags.buffer_max;

bool Success = false;

if (BQ != nullptr) {
Expand Down
11 changes: 2 additions & 9 deletions compiler-rt/lib/xray/xray_profiling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,8 @@ XRayLogInitStatus profilingFinalize() XRAY_NEVER_INSTRUMENT {
}

XRayLogInitStatus
profilingLoggingInit(size_t BufferSize, size_t BufferMax, void *Options,
size_t OptionsSize) XRAY_NEVER_INSTRUMENT {
if (BufferSize != 0 || BufferMax != 0) {
if (Verbosity())
Report("__xray_log_init() being used, and is unsupported. Use "
"__xray_log_init_mode(...) instead. Bailing out.");
return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;
}

profilingLoggingInit(UNUSED size_t BufferSize, UNUSED size_t BufferMax,
void *Options, size_t OptionsSize) XRAY_NEVER_INSTRUMENT {
s32 CurrentStatus = XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;
if (!atomic_compare_exchange_strong(&ProfilerLogStatus, &CurrentStatus,
XRayLogInitStatus::XRAY_LOG_INITIALIZING,
Expand Down
1 change: 0 additions & 1 deletion compiler-rt/test/xray/TestCases/Posix/fdr-mode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ thread_local uint64_t var = 0;
void __attribute__((noinline)) fArg(int) { }

int main(int argc, char *argv[]) {
using namespace __xray;
std::cout << "Logging before init." << std::endl;
// CHECK: Logging before init.
assert(__xray_log_select_mode("xray-fdr") ==
Expand Down
6 changes: 1 addition & 5 deletions compiler-rt/test/xray/TestCases/Posix/fdr-single-thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@
#include "xray/xray_log_interface.h"
#include <cassert>

constexpr auto kBufferSize = 16384;
constexpr auto kBufferMax = 10;

[[clang::xray_always_instrument]] void __attribute__((noinline)) fn() { }

int main(int argc, char *argv[]) {
using namespace __xray;
FDRLoggingOptions Opts;

auto status = __xray_log_init(kBufferSize, kBufferMax, &Opts, sizeof(Opts));
auto status = __xray_log_init_mode("xray-fdr", "");
assert(status == XRayLogInitStatus::XRAY_LOG_INITIALIZED);

__xray_patch();
Expand Down
8 changes: 1 addition & 7 deletions compiler-rt/test/xray/TestCases/Posix/fdr-thread-order.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
#include <cassert>
#include <thread>

constexpr auto kBufferSize = 16384;
constexpr auto kBufferMax = 10;

std::atomic<uint64_t> var{0};

[[clang::xray_always_instrument]] void __attribute__((noinline)) f1() {
Expand All @@ -35,11 +32,8 @@ std::atomic<uint64_t> var{0};
}

int main(int argc, char *argv[]) {
using namespace __xray;
FDRLoggingOptions Options;
__xray_patch();
assert(__xray_log_init(kBufferSize, kBufferMax, &Options,
sizeof(FDRLoggingOptions)) ==
assert(__xray_log_init_mode("xray-fdr", "") ==
XRayLogInitStatus::XRAY_LOG_INITIALIZED);

std::atomic_thread_fence(std::memory_order_acq_rel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ volatile int buffer_counter = 0;
std::string current_mode = __xray_log_get_current_mode();
assert(current_mode == "xray-profiling");
assert(__xray_patch() == XRayPatchingStatus::SUCCESS);
assert(__xray_log_init(0, 0, nullptr, 0) ==
assert(__xray_log_init_mode("xray-profiling", "") ==
XRayLogInitStatus::XRAY_LOG_INITIALIZED);
std::thread t0([] { f0(); });
std::thread t1([] { f0(); });
Expand Down