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;
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:37:42 +08:00
|
|
|
bool writeCustomEvent(int32_t Delta, const void *Event, int32_t EventSize) {
|
2018-11-02 06:57:50 +08:00
|
|
|
// 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>(
|
2018-11-07 12:37:42 +08:00
|
|
|
EventSize, Delta);
|
2018-11-02 06:57:50 +08:00
|
|
|
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);
|
2018-10-30 12:35:48 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:37:42 +08:00
|
|
|
bool writeTypedEvent(int32_t Delta, uint16_t EventType, const void *Event,
|
2018-10-30 12:35:48 +08:00
|
|
|
int32_t EventSize) {
|
2018-11-02 06:57:50 +08:00
|
|
|
// We do something similar when writing out typed events, see
|
|
|
|
// writeCustomEvent(...) above for details.
|
|
|
|
MetadataRecord R =
|
|
|
|
createMetadataRecord<MetadataRecord::RecordKinds::TypedEventMarker>(
|
2018-11-07 12:37:42 +08:00
|
|
|
EventSize, Delta, EventType);
|
2018-11-02 06:57:50 +08:00
|
|
|
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;
|
2018-10-30 12:35:48 +08:00
|
|
|
atomic_fetch_add(&Buffer.Extents, EventSize, memory_order_acq_rel);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-20 13:22:37 +08:00
|
|
|
char *getNextRecord() const { return NextRecord; }
|
|
|
|
|
[XRay][compiler-rt] FDR Mode Controller
Summary:
This change implements a controller for abstracting away the details of
what happens when tracing with FDR mode. This controller type allows us
to test in isolation the various cases where we're encountering function
entry, exit, and other kinds of events we are handling when FDR mode is
enabled.
This change introduces a number of testing facilities we've needed to
better support expressing the conditions we need for the unit tests. We
leave some TODOs for moving those utilities into the LLVM project,
sitting in the `Testing` library, to make matching conditions on XRay
`Trace` instances through googlemock more manageable and declarative.
We don't wire in the controller right away, to allow us to incrementally
update the implementation(s) as we increase testing coverage of the
controller type. There's a need to re-think the way we're managing
buffers in a multi-threaded environment, which is more invasive than
this implementation.
This step in the process allows us to encode our assumptions in the
implementation of the controller, and then evolve the buffer queue
implementation to support generational buffer management to ensure we
can continue to support the cases we're already supporting with the
controller.
Reviewers: mboerger, eizan
Subscribers: mgorny, llvm-commits, jfb
Differential Revision: https://reviews.llvm.org/D52588
llvm-svn: 344488
2018-10-15 10:57:06 +08:00
|
|
|
void resetRecord() {
|
|
|
|
NextRecord = reinterpret_cast<char *>(Buffer.Data);
|
|
|
|
atomic_store(&Buffer.Extents, 0, memory_order_release);
|
|
|
|
}
|
|
|
|
|
|
|
|
void undoWrites(size_t B) {
|
2018-10-30 12:35:48 +08:00
|
|
|
DCHECK_GE(NextRecord - B, reinterpret_cast<char *>(Buffer.Data));
|
[XRay][compiler-rt] FDR Mode Controller
Summary:
This change implements a controller for abstracting away the details of
what happens when tracing with FDR mode. This controller type allows us
to test in isolation the various cases where we're encountering function
entry, exit, and other kinds of events we are handling when FDR mode is
enabled.
This change introduces a number of testing facilities we've needed to
better support expressing the conditions we need for the unit tests. We
leave some TODOs for moving those utilities into the LLVM project,
sitting in the `Testing` library, to make matching conditions on XRay
`Trace` instances through googlemock more manageable and declarative.
We don't wire in the controller right away, to allow us to incrementally
update the implementation(s) as we increase testing coverage of the
controller type. There's a need to re-think the way we're managing
buffers in a multi-threaded environment, which is more invasive than
this implementation.
This step in the process allows us to encode our assumptions in the
implementation of the controller, and then evolve the buffer queue
implementation to support generational buffer management to ensure we
can continue to support the cases we're already supporting with the
controller.
Reviewers: mboerger, eizan
Subscribers: mgorny, llvm-commits, jfb
Differential Revision: https://reviews.llvm.org/D52588
llvm-svn: 344488
2018-10-15 10:57:06 +08:00
|
|
|
NextRecord -= B;
|
|
|
|
atomic_fetch_sub(&Buffer.Extents, B, memory_order_acq_rel);
|
|
|
|
}
|
|
|
|
|
2018-09-20 13:22:37 +08:00
|
|
|
}; // namespace __xray
|
|
|
|
|
|
|
|
} // namespace __xray
|
|
|
|
|
|
|
|
#endif // COMPILER-RT_LIB_XRAY_XRAY_FDR_LOG_WRITER_H_
|