profile_collector_test.cpp
7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//===-- profile_collector_test.cpp ----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of XRay, a function call tracing system.
//
//===----------------------------------------------------------------------===//
#include "gtest/gtest.h"
#include "xray_profile_collector.h"
#include "xray_profiling_flags.h"
#include <cstdint>
#include <cstring>
#include <memory>
#include <thread>
#include <utility>
#include <vector>
namespace __xray {
namespace {
static constexpr auto kHeaderSize = 16u;
constexpr uptr ExpectedProfilingVersion = 0x20180424;
struct ExpectedProfilingFileHeader {
const u64 MagicBytes = 0x7872617970726f66; // Identifier for XRay profiling
// files 'xrayprof' in hex.
const u64 Version = ExpectedProfilingVersion;
u64 Timestamp = 0;
u64 PID = 0;
};
void ValidateFileHeaderBlock(XRayBuffer B) {
ASSERT_NE(static_cast<const void *>(B.Data), nullptr);
ASSERT_EQ(B.Size, sizeof(ExpectedProfilingFileHeader));
typename std::aligned_storage<sizeof(ExpectedProfilingFileHeader)>::type
FileHeaderStorage;
ExpectedProfilingFileHeader ExpectedHeader;
std::memcpy(&FileHeaderStorage, B.Data, B.Size);
auto &FileHeader =
*reinterpret_cast<ExpectedProfilingFileHeader *>(&FileHeaderStorage);
ASSERT_EQ(ExpectedHeader.MagicBytes, FileHeader.MagicBytes);
ASSERT_EQ(ExpectedHeader.Version, FileHeader.Version);
}
void ValidateBlock(XRayBuffer B) {
profilingFlags()->setDefaults();
ASSERT_NE(static_cast<const void *>(B.Data), nullptr);
ASSERT_NE(B.Size, 0u);
ASSERT_GE(B.Size, kHeaderSize);
// We look at the block size, the block number, and the thread ID to ensure
// that none of them are zero (or that the header data is laid out as we
// expect).
char LocalBuffer[kHeaderSize] = {};
internal_memcpy(LocalBuffer, B.Data, kHeaderSize);
u32 BlockSize = 0;
u32 BlockNumber = 0;
u64 ThreadId = 0;
internal_memcpy(&BlockSize, LocalBuffer, sizeof(u32));
internal_memcpy(&BlockNumber, LocalBuffer + sizeof(u32), sizeof(u32));
internal_memcpy(&ThreadId, LocalBuffer + (2 * sizeof(u32)), sizeof(u64));
ASSERT_NE(BlockSize, 0u);
ASSERT_GE(BlockNumber, 0u);
ASSERT_NE(ThreadId, 0u);
}
std::tuple<u32, u32, u64> ParseBlockHeader(XRayBuffer B) {
char LocalBuffer[kHeaderSize] = {};
internal_memcpy(LocalBuffer, B.Data, kHeaderSize);
u32 BlockSize = 0;
u32 BlockNumber = 0;
u64 ThreadId = 0;
internal_memcpy(&BlockSize, LocalBuffer, sizeof(u32));
internal_memcpy(&BlockNumber, LocalBuffer + sizeof(u32), sizeof(u32));
internal_memcpy(&ThreadId, LocalBuffer + (2 * sizeof(u32)), sizeof(u64));
return std::make_tuple(BlockSize, BlockNumber, ThreadId);
}
struct Profile {
int64_t CallCount;
int64_t CumulativeLocalTime;
std::vector<int32_t> Path;
};
std::tuple<Profile, const char *> ParseProfile(const char *P) {
Profile Result;
// Read the path first, until we find a sentinel 0.
int32_t F;
do {
internal_memcpy(&F, P, sizeof(int32_t));
P += sizeof(int32_t);
Result.Path.push_back(F);
} while (F != 0);
// Then read the CallCount.
internal_memcpy(&Result.CallCount, P, sizeof(int64_t));
P += sizeof(int64_t);
// Then read the CumulativeLocalTime.
internal_memcpy(&Result.CumulativeLocalTime, P, sizeof(int64_t));
P += sizeof(int64_t);
return std::make_tuple(std::move(Result), P);
}
TEST(profileCollectorServiceTest, PostSerializeCollect) {
profilingFlags()->setDefaults();
bool Success = false;
BufferQueue BQ(profilingFlags()->per_thread_allocator_max,
profilingFlags()->buffers_max, Success);
ASSERT_EQ(Success, true);
FunctionCallTrie::Allocators::Buffers Buffers;
ASSERT_EQ(BQ.getBuffer(Buffers.NodeBuffer), BufferQueue::ErrorCode::Ok);
ASSERT_EQ(BQ.getBuffer(Buffers.RootsBuffer), BufferQueue::ErrorCode::Ok);
ASSERT_EQ(BQ.getBuffer(Buffers.ShadowStackBuffer),
BufferQueue::ErrorCode::Ok);
ASSERT_EQ(BQ.getBuffer(Buffers.NodeIdPairBuffer), BufferQueue::ErrorCode::Ok);
auto Allocators = FunctionCallTrie::InitAllocatorsFromBuffers(Buffers);
FunctionCallTrie T(Allocators);
// Populate the trie with some data.
T.enterFunction(1, 1, 0);
T.enterFunction(2, 2, 0);
T.exitFunction(2, 3, 0);
T.exitFunction(1, 4, 0);
// Reset the collector data structures.
profileCollectorService::reset();
// Then we post the data to the global profile collector service.
profileCollectorService::post(&BQ, std::move(T), std::move(Allocators),
std::move(Buffers), 1);
// Then we serialize the data.
profileCollectorService::serialize();
// Then we go through two buffers to see whether we're getting the data we
// expect. The first block must always be as large as a file header, which
// will have a fixed size.
auto B = profileCollectorService::nextBuffer({nullptr, 0});
ValidateFileHeaderBlock(B);
B = profileCollectorService::nextBuffer(B);
ValidateBlock(B);
u32 BlockSize;
u32 BlockNum;
u64 ThreadId;
std::tie(BlockSize, BlockNum, ThreadId) = ParseBlockHeader(B);
// We look at the serialized buffer to see whether the Trie we're expecting
// to see is there.
auto DStart = static_cast<const char *>(B.Data) + kHeaderSize;
std::vector<char> D(DStart, DStart + BlockSize);
B = profileCollectorService::nextBuffer(B);
ASSERT_EQ(B.Data, nullptr);
ASSERT_EQ(B.Size, 0u);
Profile Profile1, Profile2;
auto P = static_cast<const char *>(D.data());
std::tie(Profile1, P) = ParseProfile(P);
std::tie(Profile2, P) = ParseProfile(P);
ASSERT_NE(Profile1.Path.size(), Profile2.Path.size());
auto &P1 = Profile1.Path.size() < Profile2.Path.size() ? Profile2 : Profile1;
auto &P2 = Profile1.Path.size() < Profile2.Path.size() ? Profile1 : Profile2;
std::vector<int32_t> P1Expected = {2, 1, 0};
std::vector<int32_t> P2Expected = {1, 0};
ASSERT_EQ(P1.Path.size(), P1Expected.size());
ASSERT_EQ(P2.Path.size(), P2Expected.size());
ASSERT_EQ(P1.Path, P1Expected);
ASSERT_EQ(P2.Path, P2Expected);
}
// We break out a function that will be run in multiple threads, one that will
// use a thread local allocator, and will post the FunctionCallTrie to the
// profileCollectorService. This simulates what the threads being profiled would
// be doing anyway, but through the XRay logging implementation.
void threadProcessing() {
static bool Success = false;
static BufferQueue BQ(profilingFlags()->per_thread_allocator_max,
profilingFlags()->buffers_max, Success);
thread_local FunctionCallTrie::Allocators::Buffers Buffers = [] {
FunctionCallTrie::Allocators::Buffers B;
BQ.getBuffer(B.NodeBuffer);
BQ.getBuffer(B.RootsBuffer);
BQ.getBuffer(B.ShadowStackBuffer);
BQ.getBuffer(B.NodeIdPairBuffer);
return B;
}();
thread_local auto Allocators =
FunctionCallTrie::InitAllocatorsFromBuffers(Buffers);
FunctionCallTrie T(Allocators);
T.enterFunction(1, 1, 0);
T.enterFunction(2, 2, 0);
T.exitFunction(2, 3, 0);
T.exitFunction(1, 4, 0);
profileCollectorService::post(&BQ, std::move(T), std::move(Allocators),
std::move(Buffers), GetTid());
}
TEST(profileCollectorServiceTest, PostSerializeCollectMultipleThread) {
profilingFlags()->setDefaults();
profileCollectorService::reset();
std::thread t1(threadProcessing);
std::thread t2(threadProcessing);
t1.join();
t2.join();
// At this point, t1 and t2 are already done with what they were doing.
profileCollectorService::serialize();
// Ensure that we see two buffers.
auto B = profileCollectorService::nextBuffer({nullptr, 0});
ValidateFileHeaderBlock(B);
B = profileCollectorService::nextBuffer(B);
ValidateBlock(B);
B = profileCollectorService::nextBuffer(B);
ValidateBlock(B);
}
} // namespace
} // namespace __xray