-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathConfigTest.cpp
More file actions
362 lines (321 loc) · 12.9 KB
/
Copy pathConfigTest.cpp
File metadata and controls
362 lines (321 loc) · 12.9 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "include/Config.h"
#include <fmt/format.h>
#include <gtest/gtest.h>
#include <chrono>
#include <ctime>
using namespace std::chrono;
using namespace KINETO_NAMESPACE;
TEST(ParseTest, Whitespace) {
Config cfg;
// Check that various types of whitespace is ignored
EXPECT_TRUE(cfg.parse(""));
EXPECT_TRUE(cfg.parse(" "));
EXPECT_TRUE(cfg.parse("\t"));
EXPECT_TRUE(cfg.parse("\n"));
EXPECT_TRUE(cfg.parse(" "));
EXPECT_TRUE(cfg.parse("\t \n \t\t\n\n"));
// Only the above characters are supported
EXPECT_FALSE(cfg.parse("\r\n"));
}
TEST(ParseTest, Comment) {
Config cfg;
// Anything following a '#' should be ignored, up to a newline
EXPECT_TRUE(cfg.parse("# comment"));
EXPECT_TRUE(cfg.parse(" # ~!@#$"));
EXPECT_TRUE(cfg.parse("\t#abc"));
EXPECT_TRUE(cfg.parse("###\n##"));
EXPECT_TRUE(cfg.parse("EVENTS=util ##ok"));
EXPECT_TRUE(cfg.parse("EVENTS=util ## EVENTS=instruction"));
// Whatever appears before the comment must be valid format
EXPECT_FALSE(cfg.parse("util ## not ok"));
EXPECT_FALSE(cfg.parse("## ok \n blah # not OK"));
// Check that a comment does not affect config parsing
EXPECT_TRUE(cfg.parse("SAMPLE_PERIOD_MSECS = 1 # Sample every millisecond"));
EXPECT_EQ(cfg.samplePeriod(), milliseconds(1));
}
TEST(ParseTest, Format) {
Config cfg;
// The basic format is just "name = value".
// Where both value and name can be almost anything.
// Leading and trailing whitespace should be removed
// for both 'name' and 'value', but internal whitespace is not.
EXPECT_FALSE(cfg.parse("events"));
EXPECT_TRUE(cfg.parse("events="));
EXPECT_FALSE(cfg.parse("=events="));
EXPECT_TRUE(cfg.parse("events=1,2,3"));
// Only one setting per line
EXPECT_FALSE(cfg.parse("events = 1,2,3 ; metrics = 4,5,6"));
// Names are case sensitive
EXPECT_TRUE(cfg.parse("EVENTS = 1,2,3 \n metrics = 4,5,6"));
EXPECT_EQ(cfg.eventNames(), std::set<std::string>({"1", "2", "3"}));
EXPECT_EQ(cfg.metricNames().size(), 0);
// Leading and trailing whitespace removed for event and metric names,
// but not internal.
EXPECT_TRUE(
cfg.parse("EVENTS = 1, 2, 3 \n \tMETRICS\t = \t4,\t5\t,\ts i x "));
EXPECT_EQ(cfg.eventNames(), std::set<std::string>({"1", "2", "3"}));
EXPECT_EQ(cfg.metricNames(), std::set<std::string>({"4", "5", "s i x"}));
}
TEST(ParseTest, DefaultActivityTypes) {
Config cfg;
cfg.validate(std::chrono::system_clock::now());
auto default_activities = defaultActivityTypes();
EXPECT_EQ(
cfg.selectedActivityTypes(),
std::set<ActivityType>(
default_activities.begin(), default_activities.end()));
}
TEST(ParseTest, ActivityTypes) {
Config cfg;
EXPECT_FALSE(cfg.parse("ACTIVITY_TYPES"));
EXPECT_TRUE(cfg.parse("ACTIVITY_TYPES="));
EXPECT_FALSE(cfg.parse("=ACTIVITY_TYPES="));
EXPECT_EQ(
cfg.selectedActivityTypes(),
std::set<ActivityType>(
{ActivityType::CPU_OP,
ActivityType::CPU_INSTANT_EVENT,
ActivityType::PYTHON_FUNCTION,
ActivityType::USER_ANNOTATION,
ActivityType::GPU_USER_ANNOTATION,
ActivityType::GPU_MEMCPY,
ActivityType::GPU_MEMSET,
ActivityType::CONCURRENT_KERNEL,
ActivityType::EXTERNAL_CORRELATION,
ActivityType::OVERHEAD,
ActivityType::CUDA_RUNTIME,
ActivityType::CUDA_DRIVER,
ActivityType::CUDA_SYNC,
ActivityType::CUDA_EVENT,
ActivityType::MTIA_RUNTIME,
ActivityType::MTIA_INSIGHT,
ActivityType::MTIA_CCP_EVENTS,
ActivityType::MTIA_COUNTERS}));
Config cfg2;
EXPECT_TRUE(cfg2.parse("ACTIVITY_TYPES=gpu_memcpy,gpu_MeMsEt,kernel"));
EXPECT_EQ(
cfg2.selectedActivityTypes(),
std::set<ActivityType>(
{ActivityType::GPU_MEMCPY,
ActivityType::GPU_MEMSET,
ActivityType::CONCURRENT_KERNEL}));
EXPECT_TRUE(cfg2.parse("ACTIVITY_TYPES = cuda_Runtime,"));
EXPECT_EQ(
cfg2.selectedActivityTypes(),
std::set<ActivityType>({ActivityType::CUDA_RUNTIME}));
// Should throw an exception because incorrect activity name
EXPECT_FALSE(cfg2.parse("ACTIVITY_TYPES = memcopy,cuda_runtime"));
EXPECT_TRUE(cfg2.parse("ACTIVITY_TYPES = cpu_op"));
EXPECT_EQ(
cfg2.selectedActivityTypes(),
std::set<ActivityType>({ActivityType::CPU_OP}));
EXPECT_TRUE(cfg2.parse("ACTIVITY_TYPES = xpu_Runtime"));
EXPECT_EQ(
cfg2.selectedActivityTypes(),
std::set<ActivityType>({ActivityType::XPU_RUNTIME}));
EXPECT_TRUE(cfg2.parse("ACTIVITY_TYPES = xpu_scope_profiler"));
EXPECT_EQ(
cfg2.selectedActivityTypes(),
std::set<ActivityType>({ActivityType::XPU_SCOPE_PROFILER}));
EXPECT_TRUE(
cfg2.parse("ACTIVITY_TYPES=privateuse1_Runtime,privateuse1_driver"));
EXPECT_EQ(
cfg2.selectedActivityTypes(),
std::set<ActivityType>(
{ActivityType::PRIVATEUSE1_RUNTIME,
ActivityType::PRIVATEUSE1_DRIVER}));
}
TEST(ParseTest, SamplePeriod) {
Config cfg;
EXPECT_TRUE(cfg.parse("SAMPLE_PERIOD_MSECS=10"));
EXPECT_EQ(cfg.samplePeriod(), milliseconds(10));
EXPECT_TRUE(cfg.parse("SAMPLE_PERIOD_MSECS=0"));
cfg.validate(std::chrono::system_clock::now());
// 0 should be adjustd up to 1
EXPECT_EQ(cfg.samplePeriod(), milliseconds(1));
// Negative and non-int values should fail
EXPECT_FALSE(cfg.parse("SAMPLE_PERIOD_MSECS=-10"));
EXPECT_FALSE(cfg.parse("SAMPLE_PERIOD_MSECS=1.5"));
EXPECT_FALSE(cfg.parse("SAMPLE_PERIOD_MSECS="));
EXPECT_FALSE(cfg.parse("SAMPLE_PERIOD_MSECS=string"));
EXPECT_EQ(cfg.samplePeriod(), milliseconds(1));
}
TEST(ParseTest, MultiplexPeriod) {
Config cfg;
auto now = std::chrono::system_clock::now();
EXPECT_TRUE(cfg.parse("SAMPLE_PERIOD_MSECS=100\nMULTIPLEX_PERIOD_MSECS=100"));
EXPECT_EQ(cfg.multiplexPeriod(), milliseconds(100));
EXPECT_TRUE(cfg.parse("MULTIPLEX_PERIOD_MSECS = 0"));
cfg.validate(now);
// Adjusted to match sample period
EXPECT_EQ(cfg.multiplexPeriod(), milliseconds(100));
EXPECT_TRUE(cfg.parse("MULTIPLEX_PERIOD_MSECS \t= \t 750 \n"));
cfg.validate(now);
// Adjusted to match multiple of sample period
EXPECT_EQ(cfg.multiplexPeriod(), milliseconds(800));
EXPECT_FALSE(cfg.parse("MULTIPLEX_PERIOD_MSECS=-10"));
EXPECT_FALSE(cfg.parse("MULTIPLEX_PERIOD_MSECS=1.5"));
EXPECT_FALSE(cfg.parse("MULTIPLEX_PERIOD_MSECS="));
EXPECT_FALSE(cfg.parse("MULTIPLEX_PERIOD_MSECS=string"));
// Previous value not affected
EXPECT_EQ(cfg.multiplexPeriod(), milliseconds(800));
}
TEST(ParseTest, ReportPeriod) {
Config cfg;
EXPECT_TRUE(cfg.parse("REPORT_PERIOD_SECS=1"));
EXPECT_EQ(cfg.reportPeriod(), seconds(1));
// Whitespace
EXPECT_TRUE(cfg.parse("REPORT_PERIOD_SECS = \t100"));
EXPECT_EQ(cfg.reportPeriod(), seconds(100));
// Invalid types
EXPECT_FALSE(cfg.parse("REPORT_PERIOD_SECS=-1"));
EXPECT_EQ(cfg.reportPeriod(), seconds(100));
}
TEST(ParseTest, SamplesPerReport) {
Config cfg;
auto now = std::chrono::system_clock::now();
EXPECT_TRUE(cfg.parse(R"(
SAMPLE_PERIOD_MSECS = 1000
REPORT_PERIOD_SECS = 1
SAMPLES_PER_REPORT = 10)"));
cfg.validate(now);
// Adjusted down to one sample per report
EXPECT_EQ(cfg.samplesPerReport(), 1);
EXPECT_TRUE(cfg.parse(R"(
SAMPLE_PERIOD_MSECS = 1000
REPORT_PERIOD_SECS = 10
SAMPLES_PER_REPORT = 10)"));
cfg.validate(now);
// No adjustment needed
EXPECT_EQ(cfg.samplesPerReport(), 10);
EXPECT_TRUE(cfg.parse(R"(
SAMPLE_PERIOD_MSECS = 1000
REPORT_PERIOD_SECS = 2
SAMPLES_PER_REPORT = 10)"));
cfg.validate(now);
// Adjusted to 2 samples per report
EXPECT_EQ(cfg.samplesPerReport(), 2);
EXPECT_TRUE(cfg.parse(R"(
SAMPLE_PERIOD_MSECS = 200
REPORT_PERIOD_SECS = 2
SAMPLES_PER_REPORT = 10)"));
cfg.validate(now);
// No adjustment needed
EXPECT_EQ(cfg.samplesPerReport(), 10);
EXPECT_TRUE(cfg.parse("SAMPLES_PER_REPORT=0"));
cfg.validate(now);
// Adjusted up to 1
EXPECT_EQ(cfg.samplesPerReport(), 1);
// Invalid value types
EXPECT_FALSE(cfg.parse("SAMPLES_PER_REPORT=-10"));
EXPECT_FALSE(cfg.parse("SAMPLES_PER_REPORT=1.5"));
EXPECT_EQ(cfg.samplesPerReport(), 1);
EXPECT_TRUE(cfg.parse(R"(
SAMPLE_PERIOD_MSECS=1000
MULTIPLEX_PERIOD_MSECS=500 # Must be a multiple of sample period
REPORT_PERIOD_SECS=0 # Must be non-zero multiple of multiplex period
SAMPLES_PER_REPORT=5 # Max report period / multiplex period)"));
cfg.validate(now);
// Multiple adjustments
EXPECT_EQ(cfg.samplePeriod(), milliseconds(1000));
EXPECT_EQ(cfg.multiplexPeriod(), milliseconds(1000));
EXPECT_EQ(cfg.reportPeriod(), seconds(1));
EXPECT_EQ(cfg.samplesPerReport(), 1);
}
TEST(ParseTest, DeviceMask) {
Config cfg;
// Single device
EXPECT_TRUE(cfg.parse("EVENTS_ENABLED_DEVICES = 0"));
EXPECT_TRUE(cfg.eventProfilerEnabledForDevice(0));
EXPECT_FALSE(cfg.eventProfilerEnabledForDevice(1));
// Two devices, internal whitespace
EXPECT_TRUE(cfg.parse("EVENTS_ENABLED_DEVICES = 1, 2"));
EXPECT_FALSE(cfg.eventProfilerEnabledForDevice(0));
EXPECT_TRUE(cfg.eventProfilerEnabledForDevice(1));
EXPECT_TRUE(cfg.eventProfilerEnabledForDevice(2));
EXPECT_FALSE(cfg.eventProfilerEnabledForDevice(3));
// Three devices, check that previous devices are ignored
EXPECT_TRUE(cfg.parse("EVENTS_ENABLED_DEVICES = 0, 2,4"));
EXPECT_TRUE(cfg.eventProfilerEnabledForDevice(0));
EXPECT_FALSE(cfg.eventProfilerEnabledForDevice(1));
EXPECT_TRUE(cfg.eventProfilerEnabledForDevice(2));
EXPECT_FALSE(cfg.eventProfilerEnabledForDevice(3));
EXPECT_TRUE(cfg.eventProfilerEnabledForDevice(4));
EXPECT_FALSE(cfg.eventProfilerEnabledForDevice(5));
// Repeated numbers have no effect
EXPECT_TRUE(cfg.parse("EVENTS_ENABLED_DEVICES = 0,1,1,1,2,3,2,1,3,7,7,3"));
EXPECT_TRUE(cfg.eventProfilerEnabledForDevice(0));
EXPECT_TRUE(cfg.eventProfilerEnabledForDevice(1));
EXPECT_TRUE(cfg.eventProfilerEnabledForDevice(2));
EXPECT_TRUE(cfg.eventProfilerEnabledForDevice(3));
EXPECT_FALSE(cfg.eventProfilerEnabledForDevice(4));
EXPECT_FALSE(cfg.eventProfilerEnabledForDevice(6));
EXPECT_TRUE(cfg.eventProfilerEnabledForDevice(7));
// 8 is larger than the max allowed
EXPECT_FALSE(cfg.parse("EVENTS_ENABLED_DEVICES = 3,8"));
// 300 cannot be held in an uint8_t
EXPECT_FALSE(cfg.parse("EVENTS_ENABLED_DEVICES = 300"));
// Various illegal cases
EXPECT_FALSE(cfg.parse("EVENTS_ENABLED_DEVICES = 0,1,two,three"));
EXPECT_FALSE(cfg.parse("EVENTS_ENABLED_DEVICES = 0,1,,2"));
EXPECT_FALSE(cfg.parse("EVENTS_ENABLED_DEVICES = -1"));
EXPECT_FALSE(cfg.parse("EVENTS_ENABLED_DEVICES = 1.0"));
}
TEST(ParseTest, ProfileStartTime) {
Config cfg;
system_clock::time_point now = system_clock::now();
int64_t tgood_ms =
duration_cast<milliseconds>(now.time_since_epoch()).count();
EXPECT_TRUE(cfg.parse(fmt::format("PROFILE_START_TIME = {}", tgood_ms)));
// Pass given PROFILE_START_TIME = 0, a timestamp is assigned.
tgood_ms = 0;
EXPECT_TRUE(cfg.parse(fmt::format("PROFILE_START_TIME = {}", tgood_ms)));
// Fail given PROFILE_START_TIME older than kMaxRequestAge from now.
int64_t tbad_ms =
duration_cast<milliseconds>((now - seconds(15)).time_since_epoch())
.count();
EXPECT_FALSE(cfg.parse(fmt::format("PROFILE_START_TIME = {}", tbad_ms)));
}
TEST(ParseTest, RequestTraceIds) {
Config cfg;
EXPECT_TRUE(cfg.parse("REQUEST_TRACE_ID=XYZ"));
EXPECT_EQ(cfg.requestTraceID(), "XYZ");
EXPECT_TRUE(cfg.parse("REQUEST_GROUP_TRACE_ID=ABC"));
EXPECT_EQ(cfg.requestGroupTraceID(), "ABC");
}
// Trusted base config may set any trace path.
TEST(ParseTest, BaseConfigLogFileUnrestricted) {
Config cfg;
EXPECT_TRUE(cfg.parse("ACTIVITIES_LOG_FILE=/home/user/custom/trace.json"));
EXPECT_EQ(cfg.activitiesLogFile(), "/home/user/custom/trace.json");
EXPECT_TRUE(cfg.parse("EVENTS_LOG_FILE=/home/user/custom/events.log"));
EXPECT_EQ(cfg.eventLogFile(), "/home/user/custom/events.log");
}
// On-demand path under the allowed dir is accepted.
TEST(ParseTest, OnDemandLogFileAllowed) {
Config cfg;
cfg.setOnDemand(true);
EXPECT_TRUE(cfg.parse("ACTIVITIES_LOG_FILE=/tmp/my_trace.json"));
EXPECT_EQ(cfg.activitiesLogFile(), "/tmp/my_trace.json");
}
// On-demand path outside the allowed dir (or with traversal) falls back.
TEST(ParseTest, OnDemandLogFileRejectedOutsideAllowedDir) {
Config cfg;
cfg.setOnDemand(true);
const std::string original = cfg.activitiesLogFile();
EXPECT_TRUE(cfg.parse("ACTIVITIES_LOG_FILE=/etc/cron.d/payload"));
EXPECT_EQ(cfg.activitiesLogFile(), original);
EXPECT_EQ(cfg.activitiesLogFile().rfind("/tmp/", 0), 0u);
EXPECT_TRUE(cfg.parse("ACTIVITIES_LOG_FILE=/tmp/../etc/cron.d/payload"));
EXPECT_EQ(cfg.activitiesLogFile(), original);
const std::string originalEvents = cfg.eventLogFile();
EXPECT_TRUE(cfg.parse("EVENTS_LOG_FILE=/etc/passwd"));
EXPECT_EQ(cfg.eventLogFile(), originalEvents);
}