Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[XRay][compiler-rt] XRay Flight Data Recorder Mode
Summary: In this change we introduce the notion of a "flight data recorder" mode for XRay logging, where XRay logs in-memory first, and write out data on-demand as required (as opposed to the naive implementation that keeps logging while tracing is "on"). This depends on D26232 where we implement the core data structure for holding the buffers that threads will be using to write out records of operation. This implementation only currently works on x86_64 and depends heavily on the TSC math to write out smaller records to the inmemory buffers. Also, this implementation defines two different kinds of records with different sizes (compared to the current naive implementation): a MetadataRecord (16 bytes) and a FunctionRecord (8 bytes). MetadataRecord entries are meant to write out information like the thread ID for which the metadata record is defined for, whether the execution of a thread moved to a different CPU, etc. while a FunctionRecord represents the different kinds of function call entry/exit records we might encounter in the course of a thread's execution along with a delta from the last time the logging handler was called. While this implementation is not exactly what is described in the original XRay whitepaper, this one gives us an initial implementation that we can iterate and build upon. Reviewers: echristo, rSerge, majnemer Subscribers: mehdi_amini, llvm-commits, mgorny Differential Revision: https://reviews.llvm.org/D27038 llvm-svn: 290852
- Loading branch information
1 parent
edd09b3
commit 33d305b
Showing
18 changed files
with
1,189 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//===-- xray_log_interface.h ----------------------------------------------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file is a part of XRay, a function call tracing system. | ||
// | ||
// APIs for installing a new logging implementation. | ||
//===----------------------------------------------------------------------===// | ||
#ifndef XRAY_XRAY_LOG_INTERFACE_H | ||
#define XRAY_XRAY_LOG_INTERFACE_H | ||
|
||
#include "xray/xray_interface.h" | ||
#include <stddef.h> | ||
|
||
extern "C" { | ||
|
||
enum XRayLogInitStatus { | ||
XRAY_LOG_UNINITIALIZED, | ||
XRAY_LOG_INITIALIZING, | ||
XRAY_LOG_INITIALIZED, | ||
XRAY_LOG_FINALIZING, | ||
XRAY_LOG_FINALIZED, | ||
}; | ||
|
||
enum XRayLogFlushStatus { | ||
XRAY_LOG_NOT_FLUSHING, | ||
XRAY_LOG_FLUSHING, | ||
XRAY_LOG_FLUSHED | ||
}; | ||
|
||
struct XRayLogImpl { | ||
XRayLogInitStatus (*log_init)(size_t, size_t, void *, size_t); | ||
XRayLogInitStatus (*log_finalize)(); | ||
void (*handle_arg0)(int32_t, XRayEntryType); | ||
XRayLogFlushStatus (*flush_log)(); | ||
}; | ||
|
||
void __xray_set_log_impl(XRayLogImpl Impl); | ||
XRayLogInitStatus __xray_log_init(size_t BufferSize, size_t MaxBuffers, | ||
void *Args, size_t ArgsSize); | ||
XRayLogInitStatus __xray_log_finalize(); | ||
XRayLogFlushStatus __xray_log_flushLog(); | ||
|
||
} // extern "C" | ||
|
||
#endif // XRAY_XRAY_LOG_INTERFACE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
add_xray_unittest(XRayBufferQueueTest SOURCES | ||
buffer_queue_test.cc xray_unit_test_main.cc) | ||
add_xray_unittest(XRayFDRLoggingTest SOURCES | ||
fdr_logging_test.cc xray_unit_test_main.cc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
//===-- fdr_logging_test.cc -----------------------------------------------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file is a part of XRay, a function call tracing system. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#include "xray_fdr_logging.h" | ||
#include "gtest/gtest.h" | ||
|
||
#include <fcntl.h> | ||
#include <iostream> | ||
#include <sys/mman.h> | ||
#include <sys/stat.h> | ||
#include <sys/types.h> | ||
#include <system_error> | ||
#include <unistd.h> | ||
|
||
#include "xray/xray_records.h" | ||
|
||
namespace __xray { | ||
namespace { | ||
|
||
constexpr auto kBufferSize = 16384; | ||
constexpr auto kBufferMax = 10; | ||
|
||
struct ScopedFileCloserAndDeleter { | ||
explicit ScopedFileCloserAndDeleter(int Fd, const char *Filename) | ||
: Fd(Fd), Filename(Filename) {} | ||
|
||
~ScopedFileCloserAndDeleter() { | ||
if (Fd) { | ||
close(Fd); | ||
unlink(Filename); | ||
} | ||
} | ||
|
||
int Fd; | ||
const char *Filename; | ||
}; | ||
|
||
TEST(FDRLoggingTest, Simple) { | ||
FDRLoggingOptions Options; | ||
Options.ReportErrors = true; | ||
char TmpFilename[] = "fdr-logging-test.XXXXXX"; | ||
Options.Fd = mkstemp(TmpFilename); | ||
ASSERT_NE(Options.Fd, -1); | ||
ASSERT_EQ(FDRLogging_init(kBufferSize, kBufferMax, &Options, | ||
sizeof(FDRLoggingOptions)), | ||
XRayLogInitStatus::XRAY_LOG_INITIALIZED); | ||
FDRLogging_handleArg0(1, XRayEntryType::ENTRY); | ||
FDRLogging_handleArg0(1, XRayEntryType::EXIT); | ||
ASSERT_EQ(FDRLogging_finalize(), XRayLogInitStatus::XRAY_LOG_FINALIZED); | ||
ASSERT_EQ(FDRLogging_flush(), XRayLogFlushStatus::XRAY_LOG_FLUSHED); | ||
ASSERT_EQ(FDRLogging_reset(), XRayLogInitStatus::XRAY_LOG_UNINITIALIZED); | ||
|
||
// To do this properly, we have to close the file descriptor then re-open the | ||
// file for reading this time. | ||
ASSERT_EQ(close(Options.Fd), 0); | ||
int Fd = open(TmpFilename, O_RDONLY); | ||
ASSERT_NE(-1, Fd); | ||
ScopedFileCloserAndDeleter Guard(Fd, TmpFilename); | ||
auto Size = lseek(Fd, 0, SEEK_END); | ||
ASSERT_NE(Size, 0); | ||
// Map the file contents. | ||
const char *Contents = static_cast<const char *>( | ||
mmap(NULL, Size, PROT_READ, MAP_PRIVATE, Fd, 0)); | ||
ASSERT_NE(Contents, nullptr); | ||
|
||
XRayFileHeader H; | ||
memcpy(&H, Contents, sizeof(XRayFileHeader)); | ||
ASSERT_EQ(H.Version, 1); | ||
ASSERT_EQ(H.Type, FileTypes::FDR_LOG); | ||
|
||
// We require one buffer at least to have the "start of buffer" metadata | ||
// record. | ||
MetadataRecord MDR; | ||
memcpy(&MDR, Contents + sizeof(XRayFileHeader), sizeof(MetadataRecord)); | ||
ASSERT_EQ(MDR.RecordKind, MetadataRecord::RecordKinds::NewBuffer); | ||
} | ||
|
||
TEST(FDRLoggingTest, Multiple) { | ||
FDRLoggingOptions Options; | ||
char TmpFilename[] = "fdr-logging-test.XXXXXX"; | ||
Options.Fd = mkstemp(TmpFilename); | ||
ASSERT_NE(Options.Fd, -1); | ||
ASSERT_EQ(FDRLogging_init(kBufferSize, kBufferMax, &Options, | ||
sizeof(FDRLoggingOptions)), | ||
XRayLogInitStatus::XRAY_LOG_INITIALIZED); | ||
for (uint64_t I = 0; I < 100; ++I) { | ||
FDRLogging_handleArg0(1, XRayEntryType::ENTRY); | ||
FDRLogging_handleArg0(1, XRayEntryType::EXIT); | ||
} | ||
ASSERT_EQ(FDRLogging_finalize(), XRayLogInitStatus::XRAY_LOG_FINALIZED); | ||
ASSERT_EQ(FDRLogging_flush(), XRayLogFlushStatus::XRAY_LOG_FLUSHED); | ||
ASSERT_EQ(FDRLogging_reset(), XRayLogInitStatus::XRAY_LOG_UNINITIALIZED); | ||
|
||
// To do this properly, we have to close the file descriptor then re-open the | ||
// file for reading this time. | ||
ASSERT_EQ(close(Options.Fd), 0); | ||
int Fd = open(TmpFilename, O_RDONLY); | ||
ASSERT_NE(-1, Fd); | ||
ScopedFileCloserAndDeleter Guard(Fd, TmpFilename); | ||
auto Size = lseek(Fd, 0, SEEK_END); | ||
ASSERT_NE(Size, 0); | ||
// Map the file contents. | ||
const char *Contents = static_cast<const char *>( | ||
mmap(NULL, Size, PROT_READ, MAP_PRIVATE, Fd, 0)); | ||
ASSERT_NE(Contents, nullptr); | ||
|
||
XRayFileHeader H; | ||
memcpy(&H, Contents, sizeof(XRayFileHeader)); | ||
ASSERT_EQ(H.Version, 1); | ||
ASSERT_EQ(H.Type, FileTypes::FDR_LOG); | ||
|
||
MetadataRecord MDR0; | ||
memcpy(&MDR0, Contents + sizeof(XRayFileHeader), sizeof(MetadataRecord)); | ||
ASSERT_EQ(MDR0.RecordKind, MetadataRecord::RecordKinds::NewBuffer); | ||
} | ||
|
||
} // namespace | ||
} // namespace __xray |
Oops, something went wrong.