1 //===-- test_helpers.cc ---------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file is a part of XRay, a function call tracing system. 11 // 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> 18 19 // TODO: Move these to llvm/include/Testing/XRay/... 20 namespace llvm { 21 namespace xray { 22 23 std::string RecordTypeAsString(RecordTypes T) { 24 switch (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"; 37 } 38 return "<UNKNOWN>"; 39 } 40 41 void PrintTo(RecordTypes T, std::ostream *OS) { 42 *OS << RecordTypeAsString(T); 43 } 44 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) << " }"; 50 } 51 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) { 61 PrintTo(R, OS); 62 *OS << "\n"; 63 } 64 } 65 66 } // namespace xray 67 } // namespace llvm 68 69 namespace __xray { 70 71 std::string serialize(BufferQueue &Buffers, int32_t Version) { 72 std::string Serialized; 73 std::aligned_storage<sizeof(XRayFileHeader), alignof(XRayFileHeader)>::type 74 HeaderStorage; 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); 86 auto Extents = 87 createMetadataRecord<MetadataRecord::RecordKinds::BufferExtents>(Size); 88 Serialized.append(reinterpret_cast<const char *>(&Extents), 89 sizeof(Extents)); 90 Serialized.append(reinterpret_cast<const char *>(B.Data), Size); 91 }); 92 return Serialized; 93 } 94 95 } // namespace __xray 96