1 //===-- test_helpers.cc ---------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of XRay, a function call tracing system.
12 //===----------------------------------------------------------------------===//
13 #include "test_helpers.h"
14 #include "xray/xray_records.h"
15 #include "xray_buffer_queue.h"
16 #include "xray_fdr_log_writer.h"
17 #include <type_traits>
19 // TODO: Move these to llvm/include/Testing/XRay/...
23 std::string RecordTypeAsString(RecordTypes T) {
25 case RecordTypes::ENTER:
26 return "llvm::xray::RecordTypes::ENTER";
27 case RecordTypes::EXIT:
28 return "llvm::xray::RecordTypes::EXIT";
29 case RecordTypes::TAIL_EXIT:
30 return "llvm::xray::RecordTypes::TAIL_EXIT";
31 case RecordTypes::ENTER_ARG:
32 return "llvm::xray::RecordTypes::ENTER_ARG";
33 case RecordTypes::CUSTOM_EVENT:
34 return "llvm::xray::RecordTypes::CUSTOM_EVENT";
35 case RecordTypes::TYPED_EVENT:
36 return "llvm::xray::RecordTypes::TYPED_EVENT";
41 void PrintTo(RecordTypes T, std::ostream *OS) {
42 *OS << RecordTypeAsString(T);
45 void PrintTo(const XRayRecord &R, std::ostream *OS) {
46 *OS << "XRayRecord { CPU = " << R.CPU
47 << "; Type = " << RecordTypeAsString(R.Type) << "; FuncId = " << R.FuncId
48 << "; TSC = " << R.TSC << "; TId = " << R.TId << "; PId = " << R.PId
49 << " Args = " << ::testing::PrintToString(R.CallArgs) << " }";
52 void PrintTo(const Trace &T, std::ostream *OS) {
53 const auto &H = T.getFileHeader();
54 *OS << "XRay Trace:\nHeader: { Version = " << H.Version
55 << "; Type = " << H.Type
56 << "; ConstantTSC = " << ::testing::PrintToString(H.ConstantTSC)
57 << "; NonstopTSC = " << ::testing::PrintToString(H.NonstopTSC)
58 << "; CycleFrequency = " << H.CycleFrequency << "; FreeFormData = '"
59 << ::testing::PrintToString(H.FreeFormData) << "' }\n";
60 for (const auto &R : T) {
71 std::string serialize(BufferQueue &Buffers, int32_t Version) {
72 std::string Serialized;
73 std::aligned_storage<sizeof(XRayFileHeader), alignof(XRayFileHeader)>::type
75 auto *Header = reinterpret_cast<XRayFileHeader *>(&HeaderStorage);
76 new (Header) XRayFileHeader();
77 Header->Version = Version;
78 Header->Type = FileTypes::FDR_LOG;
79 Header->CycleFrequency = 3e9;
80 Header->ConstantTSC = 1;
81 Header->NonstopTSC = 1;
82 Serialized.append(reinterpret_cast<const char *>(&HeaderStorage),
83 sizeof(XRayFileHeader));
84 Buffers.apply([&](const BufferQueue::Buffer &B) {
85 auto Size = atomic_load_relaxed(B.Extents);
87 createMetadataRecord<MetadataRecord::RecordKinds::BufferExtents>(Size);
88 Serialized.append(reinterpret_cast<const char *>(&Extents),
90 Serialized.append(reinterpret_cast<const char *>(B.Data), Size);