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 | |
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 | |
40 | void PrintTo(RecordTypes T, std::ostream *OS) { |
41 | *OS << RecordTypeAsString(T); |
42 | } |
43 | |
44 | void PrintTo(const XRayRecord &R, std::ostream *OS) { |
45 | *OS << "XRayRecord { CPU = " << R.CPU |
46 | << "; Type = " << RecordTypeAsString(T: R.Type) << "; FuncId = " << R.FuncId |
47 | << "; TSC = " << R.TSC << "; TId = " << R.TId << "; PId = " << R.PId |
48 | << " Args = " << ::testing::PrintToString(R.CallArgs) << " }" ; |
49 | } |
50 | |
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 | |
70 | std::string serialize(BufferQueue &Buffers, int32_t Version) { |
71 | std::string Serialized; |
72 | std::aligned_storage<sizeof(XRayFileHeader), alignof(XRayFileHeader)>::type |
73 | ; |
74 | auto * = reinterpret_cast<XRayFileHeader *>(&HeaderStorage); |
75 | new (Header) XRayFileHeader(); |
76 | Header->Version = Version; |
77 | Header->Type = FileTypes::FDR_LOG; |
78 | Header->CycleFrequency = 3e9; |
79 | Header->ConstantTSC = 1; |
80 | Header->NonstopTSC = 1; |
81 | Serialized.append(s: reinterpret_cast<const char *>(&HeaderStorage), |
82 | n: sizeof(XRayFileHeader)); |
83 | Buffers.apply(Fn: [&](const BufferQueue::Buffer &B) { |
84 | auto Size = atomic_load_relaxed(a: B.Extents); |
85 | auto Extents = |
86 | createMetadataRecord<MetadataRecord::RecordKinds::BufferExtents>(Ds&: Size); |
87 | Serialized.append(s: reinterpret_cast<const char *>(&Extents), |
88 | n: sizeof(Extents)); |
89 | Serialized.append(s: reinterpret_cast<const char *>(B.Data), n: Size); |
90 | }); |
91 | return Serialized; |
92 | } |
93 | |
94 | } // namespace __xray |
95 | |