2018-09-20 13:22:37 +08:00
|
|
|
//===-- xray_fdr_log_writer.h ---------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-09-20 13:22:37 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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>
|
2018-12-18 23:17:01 +08:00
|
|
|
static void serializeTo(char *, Tuple &&) {}
|
2018-09-20 13:22:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
using Serializer = SerializerImpl<0>;
|
|
|
|
|
2018-11-09 15:16:45 +08:00
|
|
|
template <class Tuple, size_t Index> struct AggregateSizesImpl {
|
|
|
|
static constexpr size_t value =
|
|
|
|
sizeof(typename std::tuple_element<Index, Tuple>::type) +
|
|
|
|
AggregateSizesImpl<Tuple, Index - 1>::value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Tuple> struct AggregateSizesImpl<Tuple, 0> {
|
|
|
|
static constexpr size_t value =
|
|
|
|
sizeof(typename std::tuple_element<0, Tuple>::type);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Tuple> struct AggregateSizes {
|
|
|
|
static constexpr size_t value =
|
|
|
|
AggregateSizesImpl<Tuple, std::tuple_size<Tuple>::value - 1>::value;
|
|
|
|
};
|
|
|
|
|
2018-09-20 13:22:37 +08:00
|
|
|
template <MetadataRecord::RecordKinds Kind, class... DataTypes>
|
|
|
|
MetadataRecord createMetadataRecord(DataTypes &&... Ds) {
|
2018-11-09 15:16:45 +08:00
|
|
|
static_assert(AggregateSizes<std::tuple<DataTypes...>>::value <=
|
|
|
|
sizeof(MetadataRecord) - 1,
|
|
|
|
"Metadata payload longer than metadata buffer!");
|
2018-09-20 13:22:37 +08:00
|
|
|
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);
|
2018-11-09 14:39:45 +08:00
|
|
|
// We need this atomic fence here to ensure that other threads attempting to
|
|
|
|
// read the bytes in the buffer will see the writes committed before the
|
|
|
|
// extents are updated.
|
|
|
|
atomic_thread_fence(memory_order_release);
|
2018-11-20 09:00:26 +08:00
|
|
|
atomic_fetch_add(Buffer.Extents, sizeof(T), memory_order_acq_rel);
|
2018-09-20 13:22:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-11-09 14:39:45 +08:00
|
|
|
// We need this atomic fence here to ensure that other threads attempting to
|
|
|
|
// read the bytes in the buffer will see the writes committed before the
|
|
|
|
// extents are updated.
|
|
|
|
atomic_thread_fence(memory_order_release);
|
2018-11-20 09:00:26 +08:00
|
|
|
atomic_fetch_add(Buffer.Extents, Size, memory_order_acq_rel);
|
2018-09-20 13:22:37 +08:00
|
|
|
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);
|
2018-11-09 14:39:45 +08:00
|
|
|
// We need this atomic fence here to ensure that other threads attempting to
|
|
|
|
// read the bytes in the buffer will see the writes committed before the
|
|
|
|
// extents are updated.
|
|
|
|
atomic_thread_fence(memory_order_release);
|
2018-11-20 09:00:26 +08:00
|
|
|
atomic_fetch_add(Buffer.Extents, sizeof(R) + sizeof(A),
|
[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
|
|
|
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;
|
2018-11-09 14:39:45 +08:00
|
|
|
|
|
|
|
// We need this atomic fence here to ensure that other threads attempting to
|
|
|
|
// read the bytes in the buffer will see the writes committed before the
|
|
|
|
// extents are updated.
|
|
|
|
atomic_thread_fence(memory_order_release);
|
2018-11-20 09:00:26 +08:00
|
|
|
atomic_fetch_add(Buffer.Extents, sizeof(R) + EventSize,
|
2018-11-02 06:57:50 +08:00
|
|
|
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-11-09 14:39:45 +08:00
|
|
|
|
|
|
|
// We need this atomic fence here to ensure that other threads attempting to
|
|
|
|
// read the bytes in the buffer will see the writes committed before the
|
|
|
|
// extents are updated.
|
|
|
|
atomic_thread_fence(memory_order_release);
|
2018-11-20 09:00:26 +08:00
|
|
|
atomic_fetch_add(Buffer.Extents, EventSize, memory_order_acq_rel);
|
2018-10-30 12:35:48 +08:00
|
|
|
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);
|
2018-11-20 09:00:26 +08:00
|
|
|
atomic_store(Buffer.Extents, 0, memory_order_release);
|
[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 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;
|
2018-11-20 09:00:26 +08:00
|
|
|
atomic_fetch_sub(Buffer.Extents, B, memory_order_acq_rel);
|
[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
|
|
|
}
|
|
|
|
|
2018-09-20 13:22:37 +08:00
|
|
|
}; // namespace __xray
|
|
|
|
|
|
|
|
} // namespace __xray
|
|
|
|
|
|
|
|
#endif // COMPILER-RT_LIB_XRAY_XRAY_FDR_LOG_WRITER_H_
|