summaryrefslogtreecommitdiff
path: root/simpleperf/RecordFilter_test.cpp
blob: 1d5d1a067b02f7d1bdcc4c10045bc1d651d34856 (plain)
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
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "RecordFilter.h"

#include <gtest/gtest.h>

#if defined(__linux__)
#include <unistd.h>
#endif  // defined(__linux__)

#include <memory>

#include "event_attr.h"
#include "event_type.h"
#include "record.h"

using namespace simpleperf;

// @CddTest = 6.1/C-0-2
class RecordFilterTest : public ::testing::Test {
 public:
  RecordFilterTest() : filter(thread_tree) {}

 protected:
  void SetUp() override {
    const EventType* event_type = FindEventTypeByName("cpu-clock");
    attr = CreateDefaultPerfEventAttr(*event_type);
    record.reset(new SampleRecord(attr, 0, 0, 0, 0, 0, 0, 0, {}, {}, {}, 0));
  }

  SampleRecord& GetRecord(uint32_t pid, uint32_t tid) {
    record->tid_data.pid = pid;
    record->tid_data.tid = tid;
    return *record;
  }

  bool SetFilterData(const std::string& data) {
    TemporaryFile tmpfile;
    return android::base::WriteStringToFd(data, tmpfile.fd) && filter.SetFilterFile(tmpfile.path);
  }

  ThreadTree thread_tree;
  perf_event_attr attr;
  RecordFilter filter;
  std::unique_ptr<SampleRecord> record;
};

// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, no_filter) {
  ASSERT_TRUE(filter.Check(GetRecord(0, 0)));
}

// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, cpu) {
  filter.AddCpus({1});
  SampleRecord& r = GetRecord(0, 0);
  r.cpu_data.cpu = 1;
  ASSERT_TRUE(filter.Check(r));
  r.cpu_data.cpu = 2;
  ASSERT_FALSE(filter.Check(r));
}

// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, exclude_pid) {
  filter.AddPids({1}, true);
  ASSERT_FALSE(filter.Check(GetRecord(1, 1)));
  ASSERT_TRUE(filter.Check(GetRecord(2, 2)));
}

// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, exclude_tid) {
  filter.AddTids({1}, true);
  ASSERT_FALSE(filter.Check(GetRecord(1, 1)));
  ASSERT_TRUE(filter.Check(GetRecord(1, 2)));
}

// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, exclude_process_name_regex) {
  ASSERT_TRUE(filter.AddProcessNameRegex("processA", true));
  thread_tree.SetThreadName(1, 1, "processA1");
  thread_tree.SetThreadName(2, 2, "processB1");
  ASSERT_FALSE(filter.Check(GetRecord(1, 1)));
  ASSERT_TRUE(filter.Check(GetRecord(2, 2)));
}

// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, exclude_thread_name_regex) {
  ASSERT_TRUE(filter.AddThreadNameRegex("threadA", true));
  thread_tree.SetThreadName(1, 1, "processA_threadA");
  thread_tree.SetThreadName(1, 2, "processA_threadB");
  ASSERT_FALSE(filter.Check(GetRecord(1, 1)));
  ASSERT_TRUE(filter.Check(GetRecord(1, 2)));
}

#if defined(__linux__)
// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, exclude_uid) {
  pid_t pid = getpid();
  std::optional<uint32_t> uid = GetProcessUid(pid);
  ASSERT_TRUE(uid.has_value());
  filter.AddUids({uid.value()}, true);
  ASSERT_FALSE(filter.Check(GetRecord(pid, pid)));
  // The check fails if a process can't find its corresponding uid.
  uint32_t pid_not_exist = UINT32_MAX;
  ASSERT_FALSE(filter.Check(GetRecord(pid_not_exist, pid_not_exist)));
}
#endif  // defined(__linux__)

// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, include_pid) {
  filter.AddPids({1}, false);
  ASSERT_TRUE(filter.Check(GetRecord(1, 1)));
  ASSERT_FALSE(filter.Check(GetRecord(2, 2)));
}

// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, include_tid) {
  filter.AddTids({1}, false);
  ASSERT_TRUE(filter.Check(GetRecord(1, 1)));
  ASSERT_FALSE(filter.Check(GetRecord(1, 2)));
}

// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, include_process_name_regex) {
  ASSERT_TRUE(filter.AddProcessNameRegex("processA", false));
  thread_tree.SetThreadName(1, 1, "processA1");
  thread_tree.SetThreadName(2, 2, "processB1");
  ASSERT_TRUE(filter.Check(GetRecord(1, 1)));
  ASSERT_FALSE(filter.Check(GetRecord(2, 2)));
}

// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, include_thread_name_regex) {
  ASSERT_TRUE(filter.AddThreadNameRegex("threadA", false));
  thread_tree.SetThreadName(1, 1, "processA_threadA");
  thread_tree.SetThreadName(1, 2, "processA_threadB");
  ASSERT_TRUE(filter.Check(GetRecord(1, 1)));
  ASSERT_FALSE(filter.Check(GetRecord(1, 2)));
}

#if defined(__linux__)
// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, include_uid) {
  pid_t pid = getpid();
  std::optional<uint32_t> uid = GetProcessUid(pid);
  ASSERT_TRUE(uid.has_value());
  filter.AddUids({uid.value()}, false);
  ASSERT_TRUE(filter.Check(GetRecord(pid, pid)));
  uint32_t pid_not_exist = UINT32_MAX;
  ASSERT_FALSE(filter.Check(GetRecord(pid_not_exist, pid_not_exist)));
}
#endif  // defined(__linux__)

// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, global_time_filter) {
  ASSERT_TRUE(
      SetFilterData("GLOBAL_BEGIN 1000\n"
                    "GLOBAL_END 2000\n"
                    "GLOBAL_BEGIN 3000\n"
                    "GLOBAL_END 4000"));
  SampleRecord& r = GetRecord(1, 1);
  r.time_data.time = 0;
  ASSERT_FALSE(filter.Check(r));
  r.time_data.time = 999;
  ASSERT_FALSE(filter.Check(r));
  r.time_data.time = 1000;
  ASSERT_TRUE(filter.Check(r));
  r.time_data.time = 1001;
  ASSERT_TRUE(filter.Check(r));
  r.time_data.time = 1999;
  ASSERT_TRUE(filter.Check(r));
  r.time_data.time = 2000;
  ASSERT_FALSE(filter.Check(r));
  r.time_data.time = 2001;
  ASSERT_FALSE(filter.Check(r));
  r.time_data.time = 3000;
  ASSERT_TRUE(filter.Check(r));
  r.time_data.time = 4000;
  ASSERT_FALSE(filter.Check(r));
}

// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, process_time_filter) {
  ASSERT_TRUE(
      SetFilterData("PROCESS_BEGIN 1 1000\n"
                    "PROCESS_END 1 2000"));
  SampleRecord& r = GetRecord(1, 1);
  r.time_data.time = 0;
  ASSERT_FALSE(filter.Check(r));
  r.time_data.time = 999;
  ASSERT_FALSE(filter.Check(r));
  r.time_data.time = 1000;
  ASSERT_TRUE(filter.Check(r));
  r.time_data.time = 1001;
  ASSERT_TRUE(filter.Check(r));
  r.time_data.time = 1999;
  ASSERT_TRUE(filter.Check(r));
  r.time_data.time = 2000;
  ASSERT_FALSE(filter.Check(r));
  // When process time filters are used, not mentioned processes should be filtered.
  r.tid_data.pid = 2;
  r.time_data.time = 1000;
  ASSERT_FALSE(filter.Check(r));
}

// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, thread_time_filter) {
  ASSERT_TRUE(
      SetFilterData("THREAD_BEGIN 1 1000\n"
                    "THREAD_END 1 2000"));
  SampleRecord& r = GetRecord(1, 1);
  r.time_data.time = 0;
  ASSERT_FALSE(filter.Check(r));
  r.time_data.time = 999;
  ASSERT_FALSE(filter.Check(r));
  r.time_data.time = 1000;
  ASSERT_TRUE(filter.Check(r));
  r.time_data.time = 1001;
  ASSERT_TRUE(filter.Check(r));
  r.time_data.time = 1999;
  ASSERT_TRUE(filter.Check(r));
  r.time_data.time = 2000;
  ASSERT_FALSE(filter.Check(r));
  // When thread time filters are used, not mentioned threads should be filtered.
  r.tid_data.tid = 2;
  r.time_data.time = 1000;
  ASSERT_FALSE(filter.Check(r));
}

// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, clock_in_time_filter) {
  // If there is no filter data, any clock is fine.
  ASSERT_TRUE(filter.CheckClock("monotonic"));
  ASSERT_TRUE(filter.CheckClock("perf"));
  // If there is no clock command, monotonic clock is used.
  ASSERT_TRUE(SetFilterData(""));
  ASSERT_TRUE(filter.CheckClock("monotonic"));
  ASSERT_FALSE(filter.CheckClock("perf"));
  // If there is a clock command, use that clock.
  ASSERT_TRUE(SetFilterData("CLOCK realtime"));
  ASSERT_TRUE(filter.CheckClock("realtime"));
  ASSERT_FALSE(filter.CheckClock("monotonic"));
}

// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, error_in_time_filter) {
  // no timestamp error
  ASSERT_FALSE(SetFilterData("GLOBAL_BEGIN"));
  // time range error
  ASSERT_FALSE(
      SetFilterData("GLOBAL_BEGIN 1000\n"
                    "GLOBAL_END 999"));
  // time range error
  ASSERT_FALSE(
      SetFilterData("GLOBAL_BEGIN 1000\n"
                    "GLOBAL_END 1000"));
  // no timestamp error
  ASSERT_FALSE(SetFilterData("PROCESS_BEGIN 1"));
  // time range error
  ASSERT_FALSE(
      SetFilterData("PROCESS_BEGIN 1 1000\n"
                    "PROCESS_END 1 999"));
  // no timestamp error
  ASSERT_FALSE(SetFilterData("THREAD_BEGIN 1"));
  // time range error
  ASSERT_FALSE(
      SetFilterData("THREAD_BEGIN 1 1000\n"
                    "THREAD_END 1 999"));
}

namespace {

class ParseRecordFilterCommand : public Command {
 public:
  ParseRecordFilterCommand(RecordFilter& filter) : Command("", "", ""), filter_(filter) {}

  bool Run(const std::vector<std::string>& args) override {
    const auto option_formats = GetRecordFilterOptionFormats(for_recording);
    OptionValueMap options;
    std::vector<std::pair<OptionName, OptionValue>> ordered_options;

    if (!PreprocessOptions(args, option_formats, &options, &ordered_options, nullptr)) {
      return false;
    }
    filter_.Clear();
    return filter_.ParseOptions(options);
  }

  bool for_recording = true;

 private:
  RecordFilter& filter_;
};

}  // namespace

// @CddTest = 6.1/C-0-2
TEST_F(RecordFilterTest, parse_options) {
  ParseRecordFilterCommand filter_cmd(filter);

  for (bool exclude : {true, false}) {
    std::string prefix = exclude ? "--exclude-" : "--include-";

    ASSERT_TRUE(filter_cmd.Run({prefix + "pid", "1,2", prefix + "pid", "3"}));
    ASSERT_EQ(filter.Check(GetRecord(1, 1)), !exclude);
    ASSERT_EQ(filter.Check(GetRecord(2, 2)), !exclude);
    ASSERT_EQ(filter.Check(GetRecord(3, 3)), !exclude);

    ASSERT_TRUE(filter_cmd.Run({prefix + "tid", "1,2", prefix + "tid", "3"}));
    ASSERT_EQ(filter.Check(GetRecord(1, 1)), !exclude);
    ASSERT_EQ(filter.Check(GetRecord(1, 2)), !exclude);
    ASSERT_EQ(filter.Check(GetRecord(1, 3)), !exclude);

    ASSERT_TRUE(
        filter_cmd.Run({prefix + "process-name", "processA", prefix + "process-name", "processB"}));
    thread_tree.SetThreadName(1, 1, "processA");
    thread_tree.SetThreadName(2, 2, "processB");
    ASSERT_EQ(filter.Check(GetRecord(1, 1)), !exclude);
    ASSERT_EQ(filter.Check(GetRecord(2, 2)), !exclude);

    ASSERT_TRUE(
        filter_cmd.Run({prefix + "thread-name", "threadA", prefix + "thread-name", "threadB"}));
    thread_tree.SetThreadName(1, 11, "threadA");
    thread_tree.SetThreadName(1, 12, "threadB");
    ASSERT_EQ(filter.Check(GetRecord(1, 11)), !exclude);
    ASSERT_EQ(filter.Check(GetRecord(2, 12)), !exclude);

    ASSERT_TRUE(filter_cmd.Run({prefix + "uid", "1,2", prefix + "uid", "3"}));
#if defined(__linux__)
    pid_t pid = getpid();
    uid_t uid = getuid();
    ASSERT_TRUE(filter_cmd.Run({prefix + "uid", std::to_string(uid)}));
    ASSERT_EQ(filter.Check(GetRecord(pid, pid)), !exclude);
#endif  // defined(__linux__)
  }

  filter_cmd.for_recording = false;
  ASSERT_TRUE(filter_cmd.Run({"--cpu", "0", "--cpu", "1-3"}));
  SampleRecord& r = GetRecord(0, 0);
  r.cpu_data.cpu = 0;
  ASSERT_TRUE(filter.Check(r));
  r.cpu_data.cpu = 2;
  ASSERT_TRUE(filter.Check(r));
  r.cpu_data.cpu = 4;
  ASSERT_FALSE(filter.Check(r));
}