llvm-project/compiler-rt/lib/xray/xray_fdr_log_writer.h

192 lines
6.9 KiB
C
Raw Normal View History

[XRay][compiler-rt] FDRLogWriter Abstraction Summary: This change introduces an `FDRLogWriter` type which is responsible for serialising metadata and function records to character buffers. This is the first step in a refactoring of the implementation of the FDR runtime to allow for more granular testing of the individual components of the implementation. The main contribution of this change is a means of hiding the details of how specific records are written to a buffer, and for managing the extents of these buffers. We make use of C++ features (templates and some metaprogramming) to reduce repetition in the act of writing out specific kinds of records to the buffer. In this process, we make a number of changes across both LLVM and compiler-rt to allow us to use the `Trace` abstraction defined in the LLVM project in the testing of the runtime implementation. This gives us a closer end-to-end test which version-locks the runtime implementation with the loading implementation in LLVM. We also allow using gmock in compiler-rt unit tests, by adding the requisite definitions in the `AddCompilerRT.cmake` module. We also add the terminfo library detection along with inclusion of the appropriate compiler flags for header include lookup. Finally, we've gone ahead and updated the FDR logging implementation to use the FDRLogWriter for the lowest-level record-writing details. Following patches will isolate the state machine transitions which manage the set-up and tear-down of the buffers we're using in multiple threads. Reviewers: mboerger, eizan Subscribers: mgorny, jfb, llvm-commits Differential Revision: https://reviews.llvm.org/D52220 llvm-svn: 342617
2018-09-20 13:22:37 +08:00
//===-- xray_fdr_log_writer.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.
//
//===----------------------------------------------------------------------===//
#ifndef COMPILER_RT_LIB_XRAY_XRAY_FDR_LOG_WRITER_H_
#define COMPILER_RT_LIB_XRAY_XRAY_FDR_LOG_WRITER_H_
#include "xray_buffer_queue.h"
#include "xray_fdr_log_records.h"
#include <functional>
#include <tuple>
#include <type_traits>
#include <utility>
namespace __xray {
template <size_t Index> struct SerializerImpl {
template <class Tuple,
typename std::enable_if<
Index<std::tuple_size<
typename std::remove_reference<Tuple>::type>::value,
int>::type = 0> static void serializeTo(char *Buffer,
Tuple &&T) {
auto P = reinterpret_cast<const char *>(&std::get<Index>(T));
constexpr auto Size = sizeof(std::get<Index>(T));
internal_memcpy(Buffer, P, Size);
SerializerImpl<Index + 1>::serializeTo(Buffer + Size,
std::forward<Tuple>(T));
}
template <class Tuple,
typename std::enable_if<
Index >= std::tuple_size<typename std::remove_reference<
Tuple>::type>::value,
int>::type = 0>
static void serializeTo(char *, Tuple &&){};
};
using Serializer = SerializerImpl<0>;
template <MetadataRecord::RecordKinds Kind, class... DataTypes>
MetadataRecord createMetadataRecord(DataTypes &&... Ds) {
MetadataRecord R;
R.Type = 1;
R.RecordKind = static_cast<uint8_t>(Kind);
Serializer::serializeTo(R.Data,
std::make_tuple(std::forward<DataTypes>(Ds)...));
return R;
}
class FDRLogWriter {
BufferQueue::Buffer &Buffer;
char *NextRecord = nullptr;
template <class T> void writeRecord(const T &R) {
internal_memcpy(NextRecord, reinterpret_cast<const char *>(&R), sizeof(T));
NextRecord += sizeof(T);
atomic_fetch_add(&Buffer.Extents, sizeof(T), memory_order_acq_rel);
}
public:
explicit FDRLogWriter(BufferQueue::Buffer &B, char *P)
: Buffer(B), NextRecord(P) {
DCHECK_NE(Buffer.Data, nullptr);
DCHECK_NE(NextRecord, nullptr);
}
explicit FDRLogWriter(BufferQueue::Buffer &B)
: FDRLogWriter(B, static_cast<char *>(B.Data)) {}
template <MetadataRecord::RecordKinds Kind, class... Data>
bool writeMetadata(Data &&... Ds) {
// TODO: Check boundary conditions:
// 1) Buffer is full, and cannot handle one metadata record.
// 2) Buffer queue is finalising.
writeRecord(createMetadataRecord<Kind>(std::forward<Data>(Ds)...));
return true;
}
template <size_t N> size_t writeMetadataRecords(MetadataRecord (&Recs)[N]) {
constexpr auto Size = sizeof(MetadataRecord) * N;
internal_memcpy(NextRecord, reinterpret_cast<const char *>(Recs), Size);
NextRecord += Size;
atomic_fetch_add(&Buffer.Extents, Size, memory_order_acq_rel);
return Size;
}
enum class FunctionRecordKind : uint8_t {
Enter = 0x00,
Exit = 0x01,
TailExit = 0x02,
EnterArg = 0x03,
};
bool writeFunction(FunctionRecordKind Kind, int32_t FuncId, int32_t Delta) {
FunctionRecord R;
R.Type = 0;
R.RecordKind = uint8_t(Kind);
R.FuncId = FuncId;
R.TSCDelta = Delta;
writeRecord(R);
return true;
}
[XRay] Improve FDR trace handling and error messaging Summary: This change covers a number of things spanning LLVM and compiler-rt, which are related in a non-trivial way. In LLVM, we have a library that handles the FDR mode even log loading, which uses C++'s runtime polymorphism feature to better faithfully represent the events that are written down by the FDR mode runtime. We do this by interpreting a trace that's serliased in a common format agreed upon by both the trace loading library and the FDR mode runtime. This library is under active development, which consists of features allowing us to reconstitute a higher-level event log. This event log is used by the conversion and visualisation tools we have for interpreting XRay traces. One of the tools we have is a diagnostic tool in llvm-xray called `fdr-dump` which we've been using to debug our expectations of what the FDR runtime should be writing and what the logical FDR event log structures are. We use this fairly extensively to reason about why some non-trivial traces we're generating with FDR mode runtimes fail to convert or fail to parse correctly. One of these failures we've found in manual debugging of some of the traces we've seen involve an inconsistency between the buffer extents (a record indicating how many bytes to follow are part of a logical thread's event log) and the record of the bytes written into the log -- sometimes it turns out the data could be garbage, due to buffers being recycled, but sometimes we're seeing the buffer extent indicating a log is "shorter" than the actual records associated with the buffer. This case happens particularly with function entry records with a call argument. This change for now updates the FDR mode runtime to write the bytes for the function call and arg record before updating the buffer extents atomically, allowing multiple threads to see a consistent view of the data in the buffer using the atomic counter associated with a buffer. What we're trying to prevent here is partial updates where we see the intermediary updates to the buffer extents (function record size then call argument record size) becoming observable from another thread, for instance, one doing the serialization/flushing. To do both diagnose this issue properly, we need to be able to honour the extents being set in the `BufferExtents` records marking the beginning of the logical buffers when reading an FDR trace. Since LLVM doesn't use C++'s RTTI mechanism, we instead follow the advice in the documentation for LLVM Style RTTI (https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html). We then rely on this RTTI feature to ensure that our file-based record producer (our streaming "deserializer") can honour the extents of individual buffers as we interpret traces. This also sets us up to be able to eventually do smart skipping/continuation of FDR logs, seeking instead to find BufferExtents records in cases where we find potentially recoverable errors. In the meantime, we make this change to operate in a strict mode when reading logical buffers with extent records. Reviewers: mboerger Subscribers: hiraditya, llvm-commits, jfb Differential Revision: https://reviews.llvm.org/D54201 llvm-svn: 346473
2018-11-09 14:26:48 +08:00
bool writeFunctionWithArg(FunctionRecordKind Kind, int32_t FuncId,
int32_t Delta, uint64_t Arg) {
// We need to write the function with arg into the buffer, and then
// atomically update the buffer extents. This ensures that any reads
// synchronised on the buffer extents record will always see the writes
// that happen before the atomic update.
FunctionRecord R;
R.Type = 0;
R.RecordKind = uint8_t(Kind);
R.FuncId = FuncId;
R.TSCDelta = Delta;
MetadataRecord A =
createMetadataRecord<MetadataRecord::RecordKinds::CallArgument>(Arg);
NextRecord = reinterpret_cast<char *>(internal_memcpy(
NextRecord, reinterpret_cast<char *>(&R), sizeof(R))) +
sizeof(R);
NextRecord = reinterpret_cast<char *>(internal_memcpy(
NextRecord, reinterpret_cast<char *>(&A), sizeof(A))) +
sizeof(A);
atomic_fetch_add(&Buffer.Extents, sizeof(R) + sizeof(A),
memory_order_acq_rel);
return true;
}
bool writeCustomEvent(int32_t Delta, const void *Event, int32_t EventSize) {
// We write the metadata record and the custom event data into the buffer
// first, before we atomically update the extents for the buffer. This
// allows us to ensure that any threads reading the extents of the buffer
// will only ever see the full metadata and custom event payload accounted
// (no partial writes accounted).
MetadataRecord R =
createMetadataRecord<MetadataRecord::RecordKinds::CustomEventMarker>(
EventSize, Delta);
NextRecord = reinterpret_cast<char *>(internal_memcpy(
NextRecord, reinterpret_cast<char *>(&R), sizeof(R))) +
sizeof(R);
NextRecord = reinterpret_cast<char *>(
internal_memcpy(NextRecord, Event, EventSize)) +
EventSize;
atomic_fetch_add(&Buffer.Extents, sizeof(R) + EventSize,
memory_order_acq_rel);
return true;
}
bool writeTypedEvent(int32_t Delta, uint16_t EventType, const void *Event,
int32_t EventSize) {
// We do something similar when writing out typed events, see
// writeCustomEvent(...) above for details.
MetadataRecord R =
createMetadataRecord<MetadataRecord::RecordKinds::TypedEventMarker>(
EventSize, Delta, EventType);
NextRecord = reinterpret_cast<char *>(internal_memcpy(
NextRecord, reinterpret_cast<char *>(&R), sizeof(R))) +
sizeof(R);
NextRecord = reinterpret_cast<char *>(
internal_memcpy(NextRecord, Event, EventSize)) +
EventSize;
atomic_fetch_add(&Buffer.Extents, EventSize, memory_order_acq_rel);
return true;
}
[XRay][compiler-rt] FDRLogWriter Abstraction Summary: This change introduces an `FDRLogWriter` type which is responsible for serialising metadata and function records to character buffers. This is the first step in a refactoring of the implementation of the FDR runtime to allow for more granular testing of the individual components of the implementation. The main contribution of this change is a means of hiding the details of how specific records are written to a buffer, and for managing the extents of these buffers. We make use of C++ features (templates and some metaprogramming) to reduce repetition in the act of writing out specific kinds of records to the buffer. In this process, we make a number of changes across both LLVM and compiler-rt to allow us to use the `Trace` abstraction defined in the LLVM project in the testing of the runtime implementation. This gives us a closer end-to-end test which version-locks the runtime implementation with the loading implementation in LLVM. We also allow using gmock in compiler-rt unit tests, by adding the requisite definitions in the `AddCompilerRT.cmake` module. We also add the terminfo library detection along with inclusion of the appropriate compiler flags for header include lookup. Finally, we've gone ahead and updated the FDR logging implementation to use the FDRLogWriter for the lowest-level record-writing details. Following patches will isolate the state machine transitions which manage the set-up and tear-down of the buffers we're using in multiple threads. Reviewers: mboerger, eizan Subscribers: mgorny, jfb, llvm-commits Differential Revision: https://reviews.llvm.org/D52220 llvm-svn: 342617
2018-09-20 13:22:37 +08:00
char *getNextRecord() const { return NextRecord; }
void resetRecord() {
NextRecord = reinterpret_cast<char *>(Buffer.Data);
atomic_store(&Buffer.Extents, 0, memory_order_release);
}
void undoWrites(size_t B) {
DCHECK_GE(NextRecord - B, reinterpret_cast<char *>(Buffer.Data));
NextRecord -= B;
atomic_fetch_sub(&Buffer.Extents, B, memory_order_acq_rel);
}
[XRay][compiler-rt] FDRLogWriter Abstraction Summary: This change introduces an `FDRLogWriter` type which is responsible for serialising metadata and function records to character buffers. This is the first step in a refactoring of the implementation of the FDR runtime to allow for more granular testing of the individual components of the implementation. The main contribution of this change is a means of hiding the details of how specific records are written to a buffer, and for managing the extents of these buffers. We make use of C++ features (templates and some metaprogramming) to reduce repetition in the act of writing out specific kinds of records to the buffer. In this process, we make a number of changes across both LLVM and compiler-rt to allow us to use the `Trace` abstraction defined in the LLVM project in the testing of the runtime implementation. This gives us a closer end-to-end test which version-locks the runtime implementation with the loading implementation in LLVM. We also allow using gmock in compiler-rt unit tests, by adding the requisite definitions in the `AddCompilerRT.cmake` module. We also add the terminfo library detection along with inclusion of the appropriate compiler flags for header include lookup. Finally, we've gone ahead and updated the FDR logging implementation to use the FDRLogWriter for the lowest-level record-writing details. Following patches will isolate the state machine transitions which manage the set-up and tear-down of the buffers we're using in multiple threads. Reviewers: mboerger, eizan Subscribers: mgorny, jfb, llvm-commits Differential Revision: https://reviews.llvm.org/D52220 llvm-svn: 342617
2018-09-20 13:22:37 +08:00
}; // namespace __xray
} // namespace __xray
#endif // COMPILER-RT_LIB_XRAY_XRAY_FDR_LOG_WRITER_H_