2016-12-06 14:24:08 +08:00
|
|
|
//===-- xray_buffer_queue.cc -----------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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 dynamic runtime instruementation system.
|
|
|
|
//
|
|
|
|
// Defines the interface for a buffer queue implementation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "xray_buffer_queue.h"
|
2017-03-22 12:40:32 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
|
|
|
|
2017-10-04 13:20:13 +08:00
|
|
|
#include <algorithm>
|
2016-12-06 14:24:08 +08:00
|
|
|
#include <cstdlib>
|
2017-03-22 12:40:32 +08:00
|
|
|
#include <tuple>
|
2016-12-06 14:24:08 +08:00
|
|
|
|
|
|
|
using namespace __xray;
|
2017-03-22 12:40:32 +08:00
|
|
|
using namespace __sanitizer;
|
2016-12-06 14:24:08 +08:00
|
|
|
|
[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: 293015
2017-01-25 11:50:46 +08:00
|
|
|
BufferQueue::BufferQueue(std::size_t B, std::size_t N, bool &Success)
|
2017-10-04 13:20:13 +08:00
|
|
|
: BufferSize(B), Buffers(new std::tuple<Buffer, bool>[N]()),
|
|
|
|
BufferCount(N), Finalizing{0}, OwnedBuffers(new void *[N]()),
|
|
|
|
Next(Buffers.get()), First(Buffers.get()), LiveBuffers(0) {
|
|
|
|
for (size_t i = 0; i < N; ++i) {
|
|
|
|
auto &T = Buffers[i];
|
2016-12-06 14:24:08 +08:00
|
|
|
void *Tmp = malloc(BufferSize);
|
[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: 293015
2017-01-25 11:50:46 +08:00
|
|
|
if (Tmp == nullptr) {
|
|
|
|
Success = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto &Buf = std::get<0>(T);
|
2017-10-04 13:20:13 +08:00
|
|
|
std::get<1>(T) = false;
|
2016-12-06 14:24:08 +08:00
|
|
|
Buf.Buffer = Tmp;
|
|
|
|
Buf.Size = B;
|
2017-10-04 13:20:13 +08:00
|
|
|
OwnedBuffers[i] = Tmp;
|
2016-12-06 14:24:08 +08:00
|
|
|
}
|
[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: 293015
2017-01-25 11:50:46 +08:00
|
|
|
Success = true;
|
2016-12-06 14:24:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 12:40:32 +08:00
|
|
|
BufferQueue::ErrorCode BufferQueue::getBuffer(Buffer &Buf) {
|
2017-03-27 15:13:35 +08:00
|
|
|
if (__sanitizer::atomic_load(&Finalizing, __sanitizer::memory_order_acquire))
|
2017-03-22 12:40:32 +08:00
|
|
|
return ErrorCode::QueueFinalizing;
|
2017-10-04 13:20:13 +08:00
|
|
|
__sanitizer::SpinMutexLock Guard(&Mutex);
|
|
|
|
if (LiveBuffers == BufferCount)
|
2017-03-22 12:40:32 +08:00
|
|
|
return ErrorCode::NotEnoughMemory;
|
2017-10-04 13:20:13 +08:00
|
|
|
|
|
|
|
auto &T = *Next;
|
[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: 293015
2017-01-25 11:50:46 +08:00
|
|
|
auto &B = std::get<0>(T);
|
|
|
|
Buf = B;
|
2017-10-04 13:20:13 +08:00
|
|
|
++LiveBuffers;
|
|
|
|
|
|
|
|
if (++Next == (Buffers.get() + BufferCount))
|
|
|
|
Next = Buffers.get();
|
|
|
|
|
2017-03-22 12:40:32 +08:00
|
|
|
return ErrorCode::Ok;
|
2016-12-06 14:24:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 12:40:32 +08:00
|
|
|
BufferQueue::ErrorCode BufferQueue::releaseBuffer(Buffer &Buf) {
|
2017-10-04 13:20:13 +08:00
|
|
|
// Blitz through the buffers array to find the buffer.
|
|
|
|
if (std::none_of(OwnedBuffers.get(), OwnedBuffers.get() + BufferCount,
|
|
|
|
[&Buf](void *P) { return P == Buf.Buffer; }))
|
2017-03-22 12:40:32 +08:00
|
|
|
return ErrorCode::UnrecognizedBuffer;
|
2017-10-04 13:20:13 +08:00
|
|
|
__sanitizer::SpinMutexLock Guard(&Mutex);
|
|
|
|
|
|
|
|
// This points to a semantic bug, we really ought to not be releasing more
|
|
|
|
// buffers than we actually get.
|
|
|
|
if (LiveBuffers == 0)
|
|
|
|
return ErrorCode::NotEnoughMemory;
|
[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: 293015
2017-01-25 11:50:46 +08:00
|
|
|
|
|
|
|
// Now that the buffer has been released, we mark it as "used".
|
2017-10-04 13:20:13 +08:00
|
|
|
*First = std::make_tuple(Buf, true);
|
2016-12-06 14:24:08 +08:00
|
|
|
Buf.Buffer = nullptr;
|
[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: 293015
2017-01-25 11:50:46 +08:00
|
|
|
Buf.Size = 0;
|
2017-10-04 13:20:13 +08:00
|
|
|
--LiveBuffers;
|
|
|
|
if (++First == (Buffers.get() + BufferCount))
|
|
|
|
First = Buffers.get();
|
2017-10-04 14:02:12 +08:00
|
|
|
|
2017-03-22 12:40:32 +08:00
|
|
|
return ErrorCode::Ok;
|
2016-12-06 14:24:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 12:40:32 +08:00
|
|
|
BufferQueue::ErrorCode BufferQueue::finalize() {
|
2017-03-27 15:13:35 +08:00
|
|
|
if (__sanitizer::atomic_exchange(&Finalizing, 1,
|
|
|
|
__sanitizer::memory_order_acq_rel))
|
2017-03-22 12:40:32 +08:00
|
|
|
return ErrorCode::QueueFinalizing;
|
|
|
|
return ErrorCode::Ok;
|
2016-12-06 14:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BufferQueue::~BufferQueue() {
|
2017-10-04 13:20:13 +08:00
|
|
|
for (auto I = Buffers.get(), E = Buffers.get() + BufferCount; I != E; ++I) {
|
|
|
|
auto &T = *I;
|
[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: 293015
2017-01-25 11:50:46 +08:00
|
|
|
auto &Buf = std::get<0>(T);
|
2016-12-06 14:24:08 +08:00
|
|
|
free(Buf.Buffer);
|
|
|
|
}
|
|
|
|
}
|