This repository was archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathCallstackDataTest.cpp
More file actions
345 lines (281 loc) · 13.8 KB
/
Copy pathCallstackDataTest.cpp
File metadata and controls
345 lines (281 loc) · 13.8 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright (c) 2020 The Orbit Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <absl/algorithm/container.h>
#include <absl/container/flat_hash_map.h>
#include <absl/functional/bind_front.h>
#include <absl/functional/function_ref.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <algorithm>
#include <cstdint>
#include <iterator>
#include <limits>
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "ClientData/CallstackData.h"
#include "ClientData/CallstackEvent.h"
#include "ClientData/CallstackInfo.h"
#include "ClientData/CallstackType.h"
using ::testing::AnyOfArray;
using ::testing::Pointwise;
using ::testing::TestParamInfo;
using ::testing::TestWithParam;
using ::testing::ValuesIn;
using orbit_client_data::CallstackEvent;
using orbit_client_data::CallstackInfo;
using orbit_client_data::CallstackType;
namespace orbit_client_data {
namespace {
MATCHER(CallstackEventEq, "") {
const CallstackEvent& a = std::get<0>(arg);
const CallstackEvent& b = std::get<1>(arg);
return a.timestamp_ns() == b.timestamp_ns() && a.callstack_id() == b.callstack_id() &&
a.thread_id() == b.thread_id();
}
TEST(CallstackData, FilterCallstackEventsBasedOnMajorityStart) {
CallstackData callstack_data;
const uint32_t tid = 42;
const uint32_t tid_with_no_complete = 43;
const uint32_t tid_without_supermajority = 44;
const uint64_t cs1_id = 12;
const uint64_t cs1_outer = 0x10;
const uint64_t cs1_inner = 0x11;
CallstackInfo cs1{{cs1_inner, cs1_outer}, CallstackType::kComplete};
callstack_data.AddUniqueCallstack(cs1_id, std::move(cs1));
const uint64_t cs2_id = 13;
const uint64_t cs2_outer = 0x10;
const uint64_t cs2_inner = 0x21;
CallstackInfo cs2{{cs2_inner, cs2_outer}, CallstackType::kComplete};
callstack_data.AddUniqueCallstack(cs2_id, std::move(cs2));
const uint64_t broken_cs_id = 81;
const uint64_t broken_cs_outer = 0x30;
const uint64_t broken_cs_inner = 0x31;
CallstackInfo broken_cs{{broken_cs_inner, broken_cs_outer}, CallstackType::kComplete};
callstack_data.AddUniqueCallstack(broken_cs_id, std::move(broken_cs));
const uint64_t non_complete_cs_id = 91;
const uint64_t non_complete_cs_outer = 0x40;
const uint64_t non_complete_cs_inner = 0x41;
CallstackInfo non_complete_cs{{non_complete_cs_inner, non_complete_cs_outer},
CallstackType::kDwarfUnwindingError};
callstack_data.AddUniqueCallstack(non_complete_cs_id, std::move(non_complete_cs));
const uint64_t time1 = 142;
CallstackEvent event1{time1, cs1_id, tid};
callstack_data.AddCallstackEvent(event1);
const uint64_t time2 = 242;
CallstackEvent event2{time2, broken_cs_id, tid};
callstack_data.AddCallstackEvent(event2);
const uint64_t time3 = 342;
CallstackEvent event3{time3, cs2_id, tid};
callstack_data.AddCallstackEvent(event3);
const uint64_t time4 = 442;
CallstackEvent event4{time4, cs1_id, tid};
callstack_data.AddCallstackEvent(event4);
const uint64_t time5 = 542;
CallstackEvent event5{time5, non_complete_cs_id, tid};
callstack_data.AddCallstackEvent(event5);
const uint64_t time6 = 143;
CallstackEvent event6{time6, broken_cs_id, tid_with_no_complete};
callstack_data.AddCallstackEvent(event6);
const uint64_t time7 = 243;
CallstackEvent event7{time7, non_complete_cs_id, tid_with_no_complete};
callstack_data.AddCallstackEvent(event7);
const uint64_t time8 = 144;
CallstackEvent event8{time8, cs1_id, tid_without_supermajority};
callstack_data.AddCallstackEvent(event8);
const uint64_t time9 = 244;
CallstackEvent event9{time9, broken_cs_id, tid_without_supermajority};
callstack_data.AddCallstackEvent(event9);
const uint64_t time10 = 344;
CallstackEvent event10{time10, non_complete_cs_id, tid_without_supermajority};
callstack_data.AddCallstackEvent(event10);
callstack_data.UpdateCallstackTypeBasedOnMajorityStart({});
EXPECT_EQ(callstack_data.GetCallstack(cs1_id)->type(), CallstackType::kComplete);
EXPECT_EQ(callstack_data.GetCallstack(cs2_id)->type(), CallstackType::kComplete);
EXPECT_EQ(callstack_data.GetCallstack(broken_cs_id)->type(),
CallstackType::kFilteredByMajorityOutermostFrame);
EXPECT_EQ(callstack_data.GetCallstack(non_complete_cs_id)->type(),
CallstackType::kDwarfUnwindingError);
EXPECT_THAT(callstack_data.GetCallstackEventsOfTidInTimeRange(
tid, 0, std::numeric_limits<uint64_t>::max()),
Pointwise(CallstackEventEq(),
std::vector<CallstackEvent>{event1, event2, event3, event4, event5}));
EXPECT_THAT(callstack_data.GetCallstackEventsOfTidInTimeRange(
tid_with_no_complete, 0, std::numeric_limits<uint64_t>::max()),
Pointwise(CallstackEventEq(), std::vector<CallstackEvent>{event6, event7}));
EXPECT_THAT(callstack_data.GetCallstackEventsOfTidInTimeRange(
tid_without_supermajority, 0, std::numeric_limits<uint64_t>::max()),
Pointwise(CallstackEventEq(), std::vector<CallstackEvent>{event8, event9, event10}));
}
constexpr uint32_t kTid = 42;
constexpr uint32_t kAnotherTid = 43;
inline constexpr uint64_t kCallstackId1 = 12;
constexpr uint64_t kCallstackId2 = 13;
constexpr uint64_t kCloneAddress = 0x10;
constexpr uint64_t kBrokenAddress = 0x30;
constexpr uint64_t kFunctionToStopUnwindingAtAddress = 0x40;
TEST(CallstackData, FilterCallstackEventsBasedOnMajorityStartExcludesFunctionToStopUnwindingAt) {
CallstackData callstack_data;
const uint64_t cs1_outer = kCloneAddress;
const uint64_t cs1_inner = 0x11;
CallstackInfo cs1{{cs1_inner, cs1_outer}, CallstackType::kComplete};
callstack_data.AddUniqueCallstack(kCallstackId1, std::move(cs1));
const uint64_t cs2_outer = kCloneAddress;
const uint64_t cs2_inner = 0x21;
CallstackInfo cs2{{cs2_inner, cs2_outer}, CallstackType::kComplete};
callstack_data.AddUniqueCallstack(kCallstackId2, std::move(cs2));
const uint64_t broken_cs_id = 81;
const uint64_t broken_cs_outer = kBrokenAddress;
const uint64_t broken_cs_inner = 0x31;
CallstackInfo broken_cs{{broken_cs_inner, broken_cs_outer}, CallstackType::kComplete};
callstack_data.AddUniqueCallstack(broken_cs_id, std::move(broken_cs));
const uint64_t function_to_stop_unwinding_at_cs_id = 91;
const uint64_t function_to_stop_unwinding_at_cs_outer = kFunctionToStopUnwindingAtAddress;
const uint64_t function_to_stop_unwinding_at_cs_inner = 0x41;
CallstackInfo function_to_stop_unwinding_at_cs{
{function_to_stop_unwinding_at_cs_inner, function_to_stop_unwinding_at_cs_outer},
CallstackType::kComplete};
callstack_data.AddUniqueCallstack(function_to_stop_unwinding_at_cs_id,
std::move(function_to_stop_unwinding_at_cs));
const uint64_t time1 = 142;
CallstackEvent event1{time1, kCallstackId1, kTid};
callstack_data.AddCallstackEvent(event1);
const uint64_t time2 = 242;
CallstackEvent event2{time2, broken_cs_id, kTid};
callstack_data.AddCallstackEvent(event2);
const uint64_t time3 = 342;
CallstackEvent event3{time3, kCallstackId2, kTid};
callstack_data.AddCallstackEvent(event3);
const uint64_t time4 = 442;
CallstackEvent event4{time4, kCallstackId1, kTid};
callstack_data.AddCallstackEvent(event4);
const uint64_t time5 = 542;
CallstackEvent event5{time5, function_to_stop_unwinding_at_cs_id, kTid};
callstack_data.AddCallstackEvent(event5);
const uint64_t time6 = 642;
CallstackEvent event6{time6, kCallstackId2, kTid};
callstack_data.AddCallstackEvent(event6);
callstack_data.UpdateCallstackTypeBasedOnMajorityStart({{kFunctionToStopUnwindingAtAddress, 10}});
EXPECT_EQ(callstack_data.GetCallstack(kCallstackId1)->type(), CallstackType::kComplete);
EXPECT_EQ(callstack_data.GetCallstack(kCallstackId2)->type(), CallstackType::kComplete);
EXPECT_EQ(callstack_data.GetCallstack(broken_cs_id)->type(),
CallstackType::kFilteredByMajorityOutermostFrame);
EXPECT_EQ(callstack_data.GetCallstack(function_to_stop_unwinding_at_cs_id)->type(),
CallstackType::kComplete);
EXPECT_THAT(callstack_data.GetCallstackEventsOfTidInTimeRange(
kTid, 0, std::numeric_limits<uint64_t>::max()),
Pointwise(CallstackEventEq(), std::vector<CallstackEvent>{event1, event2, event3,
event4, event5, event6}));
}
const std::vector<uint32_t> kTids = {kTid, kTid, kAnotherTid, kTid};
const std::vector<uint64_t> kTimestamps = {142, 242, 342, 442};
const std::vector<CallstackEvent> kAllEvents = [] {
std::vector<CallstackEvent> events;
absl::c_transform(kTids, kTimestamps, std::back_inserter(events),
[](uint32_t tid, uint64_t timestamp) {
return CallstackEvent{timestamp, kCallstackId1, tid};
});
return events;
}();
constexpr auto kGetTestName = [](const auto& info) { return info.param.test_name; };
std::unique_ptr<CallstackData> kCallstackDataWithEvents = [] {
auto result = std::make_unique<CallstackData>();
CallstackInfo cs{{0x11, 0x10}, CallstackType::kComplete};
result->AddUniqueCallstack(kCallstackId1, std::move(cs));
absl::c_for_each(kAllEvents, absl::bind_front(&CallstackData::AddCallstackEvent, result.get()));
return result;
}();
template <typename T>
[[nodiscard]] static std::vector<T> Slice(const std::vector<T>& v, std::vector<int> indices) {
std::vector<T> slice;
absl::c_transform(indices, std::back_inserter(slice), [&](int i) { return v[i]; });
return slice;
}
struct ForEachCallstackEventOfTidInTimeRangeDiscretizedTestCase {
std::string test_name;
uint32_t tid;
uint64_t start_ns;
uint64_t end_ns;
uint32_t resolution;
std::vector<int> expected_event_ids;
};
using ForEachCallstackEventOfTidInTimeRangeDiscretizedTest =
TestWithParam<ForEachCallstackEventOfTidInTimeRangeDiscretizedTestCase>;
TEST_P(ForEachCallstackEventOfTidInTimeRangeDiscretizedTest, IterationIsCorrect) {
const ForEachCallstackEventOfTidInTimeRangeDiscretizedTestCase& test_case = GetParam();
const CallstackData* callstack_data = kCallstackDataWithEvents.get();
std::vector<CallstackEvent> visited_callstack_list;
auto visit_callstack = [&](const CallstackEvent& event) {
visited_callstack_list.push_back(event);
};
callstack_data->ForEachCallstackEventOfTidInTimeRangeDiscretized(
test_case.tid, test_case.start_ns, test_case.end_ns, test_case.resolution, visit_callstack);
EXPECT_THAT(visited_callstack_list,
Pointwise(CallstackEventEq(), Slice(kAllEvents, test_case.expected_event_ids)));
}
constexpr uint64_t kStartNs = 0;
constexpr uint64_t kEndNs = 1000;
constexpr uint64_t kMaxNs = std::numeric_limits<uint64_t>::max();
constexpr uint32_t kResolution = 2000;
INSTANTIATE_TEST_SUITE_P(
ForEachCallstackEventOfTidInTimeRangeDiscretizedTests,
ForEachCallstackEventOfTidInTimeRangeDiscretizedTest,
ValuesIn<ForEachCallstackEventOfTidInTimeRangeDiscretizedTestCase>({
{"NormalTimeRange", kTid, kStartNs, kEndNs, kResolution, {0, 1, 3}},
{"DifferentTid", kAnotherTid, kStartNs, kEndNs, kResolution, {2}},
{"SmallTimeRange", kTid, kStartNs, kTimestamps[2] - 1, kResolution, {0, 1}},
// When max_timestamp is std::numeric_limits<uint64_t>::max(), each callstack will be drawn
// in the first pixel, and therefore only one will be visible.
{"InfiniteTimeRange", kTid, kStartNs, kMaxNs, kResolution, {0}},
// With one pixel on the screen we should only see one event.
{"OnePixel", kTid, kStartNs, kEndNs, /*resolution=*/1, {0}},
}),
kGetTestName);
constexpr auto kExpectAll = [](const auto& actual, const auto& expected) {
EXPECT_THAT(actual, Pointwise(CallstackEventEq(), expected));
};
constexpr auto kExpectAny = [](const auto& actual, const auto& expected) {
EXPECT_EQ(actual.size(), 1);
EXPECT_THAT(actual[0], AnyOfArray(expected));
};
struct ForEachCallstackEventInTimeRangeDiscretizedTestCase {
std::string test_name;
absl::FunctionRef<void(std::vector<CallstackEvent>, std::vector<CallstackEvent>)> expect;
uint64_t start_ns;
uint64_t end_ns;
uint32_t resolution;
std::vector<int> expected_event_ids;
};
using ForEachCallstackEventInTimeRangeDiscretizedTest =
TestWithParam<ForEachCallstackEventInTimeRangeDiscretizedTestCase>;
TEST_P(ForEachCallstackEventInTimeRangeDiscretizedTest, IterationIsCorrect) {
const ForEachCallstackEventInTimeRangeDiscretizedTestCase& test_case = GetParam();
const CallstackData* callstack_data = kCallstackDataWithEvents.get();
std::vector<CallstackEvent> visited_callstack_list;
auto visit_callstack = [&](const CallstackEvent& event) {
visited_callstack_list.push_back(event);
};
callstack_data->ForEachCallstackEventInTimeRangeDiscretized(
test_case.start_ns, test_case.end_ns, test_case.resolution, visit_callstack);
test_case.expect(visited_callstack_list, Slice(kAllEvents, test_case.expected_event_ids));
}
INSTANTIATE_TEST_SUITE_P(
ForEachCallstackEventInTimeRangeDiscretizedTests,
ForEachCallstackEventInTimeRangeDiscretizedTest,
ValuesIn<ForEachCallstackEventInTimeRangeDiscretizedTestCase>({
{"NormalTimeRange", kExpectAll, kStartNs, kEndNs, kResolution, {0, 1, 2, 3}},
{"SmallTimeRange", kExpectAll, kStartNs, kTimestamps[2] - 1, kResolution, {0, 1}},
// When max_timestamp is std::numeric_limits<uint64_t>::max(), each callstack should be draw
// in the first pixel, and therefore only one will be visible. It should be the first of
// some of the threads.
{"InfiniteTimeRange", kExpectAny, kStartNs, kMaxNs, kResolution, {0, 2}},
// With one pixel on the screen we should only see one event. It should be the first of some
// of the threads.
{"OnePixel", kExpectAny, kStartNs, kEndNs, /*resolution=*/1, {0, 2}},
}),
kGetTestName);
} // namespace
} // namespace orbit_client_data