xref: /llvm-project/compiler-rt/lib/xray/tests/unit/test_helpers.cpp (revision cac7821438f625d6c8a36dd9363f9acd3a3d93de)
1 //===-- test_helpers.cpp --------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of XRay, a function call tracing system.
10 //
11 //===----------------------------------------------------------------------===//
12 #include "test_helpers.h"
13 #include "xray/xray_records.h"
14 #include "xray_buffer_queue.h"
15 #include "xray_fdr_log_writer.h"
16 #include <type_traits>
17 
18 // TODO: Move these to llvm/include/Testing/XRay/...
19 namespace llvm {
20 namespace xray {
21 
RecordTypeAsString(RecordTypes T)22 std::string RecordTypeAsString(RecordTypes T) {
23   switch (T) {
24   case RecordTypes::ENTER:
25     return "llvm::xray::RecordTypes::ENTER";
26   case RecordTypes::EXIT:
27     return "llvm::xray::RecordTypes::EXIT";
28   case RecordTypes::TAIL_EXIT:
29     return "llvm::xray::RecordTypes::TAIL_EXIT";
30   case RecordTypes::ENTER_ARG:
31     return "llvm::xray::RecordTypes::ENTER_ARG";
32   case RecordTypes::CUSTOM_EVENT:
33     return "llvm::xray::RecordTypes::CUSTOM_EVENT";
34   case RecordTypes::TYPED_EVENT:
35     return "llvm::xray::RecordTypes::TYPED_EVENT";
36   }
37   return "<UNKNOWN>";
38 }
39 
PrintTo(RecordTypes T,std::ostream * OS)40 void PrintTo(RecordTypes T, std::ostream *OS) {
41   *OS << RecordTypeAsString(T);
42 }
43 
PrintTo(const XRayRecord & R,std::ostream * OS)44 void PrintTo(const XRayRecord &R, std::ostream *OS) {
45   *OS << "XRayRecord { CPU = " << R.CPU
46       << "; Type = " << RecordTypeAsString(R.Type) << "; FuncId = " << R.FuncId
47       << "; TSC = " << R.TSC << "; TId = " << R.TId << "; PId = " << R.PId
48       << " Args = " << ::testing::PrintToString(R.CallArgs) << " }";
49 }
50 
PrintTo(const Trace & T,std::ostream * OS)51 void PrintTo(const Trace &T, std::ostream *OS) {
52   const auto &H = T.getFileHeader();
53   *OS << "XRay Trace:\nHeader: { Version = " << H.Version
54       << "; Type = " << H.Type
55       << "; ConstantTSC = " << ::testing::PrintToString(H.ConstantTSC)
56       << "; NonstopTSC = " << ::testing::PrintToString(H.NonstopTSC)
57       << "; CycleFrequency = " << H.CycleFrequency << "; FreeFormData = '"
58       << ::testing::PrintToString(H.FreeFormData) << "' }\n";
59   for (const auto &R : T) {
60     PrintTo(R, OS);
61     *OS << "\n";
62   }
63 }
64 
65 } // namespace xray
66 } // namespace llvm
67 
68 namespace __xray {
69 
serialize(BufferQueue & Buffers,int32_t Version)70 std::string serialize(BufferQueue &Buffers, int32_t Version) {
71   std::string Serialized;
72   alignas(XRayFileHeader) std::byte HeaderStorage[sizeof(XRayFileHeader)];
73   auto *Header = reinterpret_cast<XRayFileHeader *>(&HeaderStorage);
74   new (Header) XRayFileHeader();
75   Header->Version = Version;
76   Header->Type = FileTypes::FDR_LOG;
77   Header->CycleFrequency = 3e9;
78   Header->ConstantTSC = 1;
79   Header->NonstopTSC = 1;
80   Serialized.append(reinterpret_cast<const char *>(&HeaderStorage),
81                     sizeof(XRayFileHeader));
82   Buffers.apply([&](const BufferQueue::Buffer &B) {
83     auto Size = atomic_load_relaxed(B.Extents);
84     auto Extents =
85         createMetadataRecord<MetadataRecord::RecordKinds::BufferExtents>(Size);
86     Serialized.append(reinterpret_cast<const char *>(&Extents),
87                       sizeof(Extents));
88     Serialized.append(reinterpret_cast<const char *>(B.Data), Size);
89   });
90   return Serialized;
91 }
92 
93 } // namespace __xray
94