Skip to content

Commit 6b67ff0

Browse files
committedNov 1, 2018
[XRay] Add CPU ID in Custom Event FDR Records
Summary: This change cuts across compiler-rt and llvm, to increment the FDR log version number to 4, and include the CPU ID in the custom event records. This is a step towards allowing us to change the `llvm::xray::Trace` object to start representing both custom and typed events in the stream of records. Follow-on changes will allow us to change the kinds of records we're presenting in the stream of traces, to incorporate the data in custom/typed events. A follow-on change will handle the typed event case, where it may not fit within the 15-byte buffer for metadata records. This work is part of the larger effort to enable writing analysis and processing tools using a common in-memory representation of the events found in traces. The work will focus on porting existing tools in LLVM to use the common representation and informing the design of a library/framework for expressing trace event analysis as C++ programs. Reviewers: mboerger, eizan Subscribers: hiraditya, mgrang, llvm-commits Differential Revision: https://reviews.llvm.org/D53920 llvm-svn: 345798
1 parent d4891a1 commit 6b67ff0

11 files changed

+47
-24
lines changed
 

‎compiler-rt/lib/xray/xray_fdr_controller.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ template <size_t Version = 3> class FDRController {
327327
LatestTSC = 0;
328328
UndoableFunctionEnters = 0;
329329
UndoableTailExits = 0;
330-
return W.writeCustomEvent(TSC, Event, EventSize);
330+
return W.writeCustomEvent(TSC, CPU, Event, EventSize);
331331
}
332332

333333
bool typedEvent(uint64_t TSC, uint16_t CPU, uint16_t EventType,

‎compiler-rt/lib/xray/xray_fdr_log_writer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ class FDRLogWriter {
110110
return true;
111111
}
112112

113-
bool writeCustomEvent(uint64_t TSC, const void *Event, int32_t EventSize) {
113+
bool writeCustomEvent(uint64_t TSC, uint16_t CPU, const void *Event,
114+
int32_t EventSize) {
114115
writeMetadata<MetadataRecord::RecordKinds::CustomEventMarker>(EventSize,
115-
TSC);
116+
TSC, CPU);
116117
internal_memcpy(NextRecord, Event, EventSize);
117118
NextRecord += EventSize;
118119
atomic_fetch_add(&Buffer.Extents, EventSize, memory_order_acq_rel);

‎compiler-rt/lib/xray/xray_fdr_logging.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ static XRayFileHeader &fdrCommonHeaderInfo() {
148148
// Version 2 of the log writes the extents of the buffer, instead of
149149
// relying on an end-of-buffer record.
150150
// Version 3 includes PID metadata record
151-
H.Version = 3;
151+
// Version 4 includes CPU data in the custom event records
152+
H.Version = 4;
152153
H.Type = FileTypes::FDR_LOG;
153154

154155
// Test for required CPU features and cache the cycle frequency

‎llvm/include/llvm/XRay/FDRRecords.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,22 @@ class TSCWrapRecord : public MetadataRecord {
153153
class CustomEventRecord : public MetadataRecord {
154154
int32_t Size = 0;
155155
uint64_t TSC = 0;
156+
uint16_t CPU = 0;
156157
std::string Data{};
157158
friend class RecordInitializer;
158159

159160
public:
160161
CustomEventRecord() = default;
161-
explicit CustomEventRecord(uint64_t S, uint64_t T, std::string D)
162-
: MetadataRecord(), Size(S), TSC(T), Data(std::move(D)) {}
162+
explicit CustomEventRecord(uint64_t S, uint64_t T, uint16_t C, std::string D)
163+
: MetadataRecord(), Size(S), TSC(T), CPU(C), Data(std::move(D)) {}
163164

164165
MetadataType metadataType() const override {
165166
return MetadataType::CustomEvent;
166167
}
167168

168169
int32_t size() const { return Size; }
169170
uint64_t tsc() const { return TSC; }
171+
uint16_t cpu() const { return CPU; }
170172
StringRef data() const { return Data; }
171173

172174
Error apply(RecordVisitor &V) override;
@@ -272,10 +274,16 @@ class RecordVisitor {
272274
class RecordInitializer : public RecordVisitor {
273275
DataExtractor &E;
274276
uint32_t &OffsetPtr;
277+
uint16_t Version;
275278

276279
public:
280+
static constexpr uint16_t DefaultVersion = 4u;
281+
282+
explicit RecordInitializer(DataExtractor &DE, uint32_t &OP, uint16_t V)
283+
: RecordVisitor(), E(DE), OffsetPtr(OP), Version(V) {}
284+
277285
explicit RecordInitializer(DataExtractor &DE, uint32_t &OP)
278-
: RecordVisitor(), E(DE), OffsetPtr(OP) {}
286+
: RecordInitializer(DE, OP, DefaultVersion) {}
279287

280288
Error visit(BufferExtents &) override;
281289
Error visit(WallclockRecord &) override;

‎llvm/lib/XRay/FDRTraceWriter.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ Error FDRTraceWriter::visit(TSCWrapRecord &R) {
9494
}
9595

9696
Error FDRTraceWriter::visit(CustomEventRecord &R) {
97-
if (auto E = writeMetadata<5u>(OS, R.size(), R.tsc()))
97+
if (auto E = writeMetadata<5u>(OS, R.size(), R.tsc(), R.cpu()))
9898
return E;
99-
ArrayRef<char> Bytes(R.data().data(), R.data().size());
99+
auto D = R.data();
100+
ArrayRef<char> Bytes(D.data(), D.size());
100101
OS.write(Bytes);
101102
return Error::success();
102103
}
@@ -127,7 +128,7 @@ Error FDRTraceWriter::visit(FunctionRecord &R) {
127128
OS.write(TypeRecordFuncId);
128129
OS.write(R.delta());
129130
return Error::success();
130-
} // namespace xray
131+
}
131132

132133
} // namespace xray
133134
} // namespace llvm

‎llvm/lib/XRay/FileHeaderReader.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ Expected<XRayFileHeader> readBinaryFormatHeader(DataExtractor &HeaderExtractor,
6363
// Manually advance the offset pointer 16 bytes, after getting a raw memcpy
6464
// from the underlying data.
6565
OffsetPtr += 16;
66-
if (FileHeader.Version != 1 && FileHeader.Version != 2 &&
67-
FileHeader.Version != 3)
66+
if (FileHeader.Version < 1 || FileHeader.Version > 4)
6867
return createStringError(std::make_error_code(std::errc::invalid_argument),
6968
"Unsupported XRay file version: %d at offset %d",
7069
FileHeader.Version, OffsetPtr);

‎llvm/lib/XRay/RecordInitializer.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ Error RecordInitializer::visit(CustomEventRecord &R) {
118118
std::make_error_code(std::errc::invalid_argument),
119119
"Cannot read a custom event TSC field at offset %d.", OffsetPtr);
120120

121+
// For version 4 onwards, of the FDR log, we want to also capture the CPU ID
122+
// of the custom event.
123+
if (Version >= 4) {
124+
PreReadOffset = OffsetPtr;
125+
R.CPU = E.getU16(&OffsetPtr);
126+
if (PreReadOffset == OffsetPtr)
127+
return createStringError(
128+
std::make_error_code(std::errc::invalid_argument),
129+
"Missing CPU field at offset %d", OffsetPtr);
130+
}
131+
132+
assert(OffsetPtr > BeginOffset &&
133+
OffsetPtr - BeginOffset <= MetadataRecord::kMetadataBodySize);
121134
OffsetPtr += MetadataRecord::kMetadataBodySize - (OffsetPtr - BeginOffset);
122135

123136
// Next we read in a fixed chunk of data from the given offset.

‎llvm/lib/XRay/RecordPrinter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ Error RecordPrinter::visit(TSCWrapRecord &R) {
3535
}
3636

3737
Error RecordPrinter::visit(CustomEventRecord &R) {
38-
OS << formatv("<Custom Event: tsc = {0}, size = {1}, data = '{2}'>", R.tsc(),
39-
R.size(), R.data())
38+
OS << formatv(
39+
"<Custom Event: tsc = {0}, cpu = {1}, size = {2}, data = '{3}'>",
40+
R.tsc(), R.cpu(), R.size(), R.data())
4041
<< Delim;
4142
return Error::success();
4243
}

‎llvm/lib/XRay/Trace.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,11 @@ Error loadFDRLog(StringRef Data, bool IsLittleEndian,
310310
{
311311
for (auto &PTB : Index) {
312312
auto &Blocks = PTB.second;
313-
llvm::sort(
314-
Blocks,
315-
[](const BlockIndexer::Block &L, const BlockIndexer::Block &R) {
316-
return (L.WallclockTime->seconds() < R.WallclockTime->seconds() &&
317-
L.WallclockTime->nanos() < R.WallclockTime->nanos());
318-
});
313+
llvm::sort(Blocks, [](const BlockIndexer::Block &L,
314+
const BlockIndexer::Block &R) {
315+
return (L.WallclockTime->seconds() < R.WallclockTime->seconds() &&
316+
L.WallclockTime->nanos() < R.WallclockTime->nanos());
317+
});
319318
auto Adder = [&](const XRayRecord &R) { Records.push_back(R); };
320319
TraceExpander Expander(Adder, FileHeader.Version);
321320
for (auto &B : Blocks) {
@@ -435,7 +434,7 @@ Expected<Trace> llvm::xray::loadTrace(const DataExtractor &DE, bool Sort) {
435434
}
436435
break;
437436
case FLIGHT_DATA_RECORDER_FORMAT:
438-
if (Version == 1 || Version == 2 || Version == 3) {
437+
if (Version >= 1 && Version <= 4) {
439438
if (auto E = loadFDRLog(DE.getData(), DE.isLittleEndian(), T.FileHeader,
440439
T.Records))
441440
return std::move(E);

‎llvm/unittests/XRay/FDRProducerConsumerTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ template <> std::unique_ptr<Record> MakeRecord<WallclockRecord>() {
5454
}
5555

5656
template <> std::unique_ptr<Record> MakeRecord<CustomEventRecord>() {
57-
return make_unique<CustomEventRecord>(4, 1, "data");
57+
return make_unique<CustomEventRecord>(4, 1, 2, "data");
5858
}
5959

6060
template <> std::unique_ptr<Record> MakeRecord<CallArgRecord>() {

‎llvm/unittests/XRay/FDRRecordPrinterTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ template <> struct Helper<TSCWrapRecord> {
5555

5656
template <> struct Helper<CustomEventRecord> {
5757
static std::unique_ptr<Record> construct() {
58-
return make_unique<CustomEventRecord>(4, 1, "data");
58+
return make_unique<CustomEventRecord>(4, 1, 2, "data");
5959
}
6060

6161
static const char *expected() {
62-
return "<Custom Event: tsc = 1, size = 4, data = 'data'>";
62+
return "<Custom Event: tsc = 1, cpu = 2, size = 4, data = 'data'>";
6363
}
6464
};
6565

0 commit comments

Comments
 (0)
Please sign in to comment.