aboutsummaryrefslogtreecommitdiff
path: root/src/trace_processor/sched_slice_table_unittest.cc
blob: 7e624d08009be15d26e62aebdaf17b8c240b3ecf (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
/*
 * Copyright (C) 2018 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 "src/trace_processor/sched_slice_table.h"

#include "src/trace_processor/args_tracker.h"
#include "src/trace_processor/event_tracker.h"
#include "src/trace_processor/importers/ftrace/sched_event_tracker.h"
#include "src/trace_processor/process_tracker.h"
#include "src/trace_processor/sqlite/scoped_db.h"
#include "src/trace_processor/trace_processor_context.h"
#include "test/gtest_and_gmock.h"

namespace perfetto {
namespace trace_processor {
namespace {

using ::testing::ElementsAre;
using ::testing::IsEmpty;
using Column = SchedSliceTable::Column;

class SchedSliceTableTest : public ::testing::Test {
 public:
  SchedSliceTableTest() {
    sqlite3* db = nullptr;
    PERFETTO_CHECK(sqlite3_initialize() == SQLITE_OK);
    PERFETTO_CHECK(sqlite3_open(":memory:", &db) == SQLITE_OK);
    db_.reset(db);

    context_.storage.reset(new TraceStorage());
    context_.global_args_tracker.reset(new GlobalArgsTracker(&context_));
    context_.args_tracker.reset(new ArgsTracker(&context_));
    context_.process_tracker.reset(new ProcessTracker(&context_));
    context_.event_tracker.reset(new EventTracker(&context_));
    sched_tracker_ = SchedEventTracker::GetOrCreate(&context_);

    SchedSliceTable::RegisterTable(db_.get(), context_.storage.get());
  }

  void PrepareValidStatement(const std::string& sql) {
    int size = static_cast<int>(sql.size());
    sqlite3_stmt* stmt;
    ASSERT_EQ(sqlite3_prepare_v2(*db_, sql.c_str(), size, &stmt, nullptr),
              SQLITE_OK);
    stmt_.reset(stmt);
  }

 protected:
  TraceProcessorContext context_;
  ScopedDb db_;
  ScopedStmt stmt_;
  SchedEventTracker* sched_tracker_;
};

TEST_F(SchedSliceTableTest, RowsReturnedInCorrectOrderWithinCpu) {
  uint32_t cpu = 3;
  int64_t timestamp = 100;
  uint32_t pid_1 = 2;
  int64_t prev_state = 32;
  static const char kCommProc1[] = "process1";
  static const char kCommProc2[] = "process2";
  uint32_t pid_2 = 4;
  int32_t prio = 1024;
  sched_tracker_->PushSchedSwitch(cpu, timestamp, pid_1, kCommProc2, prio,
                                  prev_state, pid_2, kCommProc1, prio);
  sched_tracker_->PushSchedSwitch(cpu, timestamp + 3, pid_2, kCommProc1, prio,
                                  prev_state, pid_1, kCommProc2, prio);
  sched_tracker_->PushSchedSwitch(cpu, timestamp + 4, pid_1, kCommProc2, prio,
                                  prev_state, pid_2, kCommProc1, prio);
  sched_tracker_->PushSchedSwitch(cpu, timestamp + 10, pid_2, kCommProc1, prio,
                                  prev_state, pid_1, kCommProc2, prio);

  PrepareValidStatement(
      "SELECT dur, ts, cpu FROM sched where dur != 0 ORDER BY dur");

  ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_ROW);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 0), 1 /* duration */);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 1), timestamp + 3);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 2), cpu);

  ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_ROW);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 0), 3 /* duration */);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 1), timestamp);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 2), cpu);

  ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_ROW);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 0), 6 /* duration */);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 1), timestamp + 4);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 2), cpu);

  ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_DONE);
}

TEST_F(SchedSliceTableTest, RowsReturnedInCorrectOrderBetweenCpu) {
  uint32_t cpu_1 = 3;
  uint32_t cpu_2 = 8;
  uint32_t cpu_3 = 4;
  int64_t timestamp = 100;
  uint32_t pid_1 = 2;
  int64_t prev_state = 32;
  static const char kCommProc1[] = "process1";
  static const char kCommProc2[] = "process2";
  uint32_t pid_2 = 4;
  int32_t prio = 1024;
  sched_tracker_->PushSchedSwitch(cpu_3, timestamp - 2, pid_1, kCommProc2, prio,
                                  prev_state, pid_2, kCommProc1, prio);
  sched_tracker_->PushSchedSwitch(cpu_3, timestamp - 1, pid_2, kCommProc1, prio,
                                  prev_state, pid_1, kCommProc2, prio);
  sched_tracker_->PushSchedSwitch(cpu_1, timestamp, pid_1, kCommProc2, prio,
                                  prev_state, pid_2, kCommProc1, prio);
  sched_tracker_->PushSchedSwitch(cpu_2, timestamp + 3, pid_2, kCommProc1, prio,
                                  prev_state, pid_1, kCommProc2, prio);
  sched_tracker_->PushSchedSwitch(cpu_1, timestamp + 4, pid_2, kCommProc1, prio,
                                  prev_state, pid_1, kCommProc2, prio);
  sched_tracker_->PushSchedSwitch(cpu_2, timestamp + 10, pid_1, kCommProc2,
                                  prio, prev_state, pid_2, kCommProc1, prio);

  PrepareValidStatement(
      "SELECT dur, ts, cpu FROM sched where dur != 0 ORDER BY dur desc");

  ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_ROW);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 0), 7 /* duration */);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 1), timestamp + 3);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 2), cpu_2);

  ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_ROW);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 0), 4 /* duration */);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 1), timestamp);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 2), cpu_1);

  ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_ROW);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 0), 1 /* duration */);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 1), timestamp - 2);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 2), cpu_3);

  ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_DONE);
}

TEST_F(SchedSliceTableTest, FilterCpus) {
  uint32_t cpu_1 = 3;
  uint32_t cpu_2 = 8;
  int64_t timestamp = 100;
  uint32_t pid_1 = 2;
  uint32_t prev_state = 32;
  static const char kCommProc1[] = "process1";
  static const char kCommProc2[] = "process2";
  uint32_t pid_2 = 4;
  int32_t prio = 1024;
  sched_tracker_->PushSchedSwitch(cpu_1, timestamp, pid_1, kCommProc2, prio,
                                  prev_state, pid_2, kCommProc1, prio);
  sched_tracker_->PushSchedSwitch(cpu_2, timestamp + 3, pid_2, kCommProc1, prio,
                                  prev_state, pid_1, kCommProc2, prio);
  sched_tracker_->PushSchedSwitch(cpu_1, timestamp + 4, pid_2, kCommProc1, prio,
                                  prev_state, pid_1, kCommProc2, prio);
  sched_tracker_->PushSchedSwitch(cpu_2, timestamp + 10, pid_1, kCommProc2,
                                  prio, prev_state, pid_2, kCommProc1, prio);

  PrepareValidStatement(
      "SELECT dur, ts, cpu FROM sched WHERE dur != 0 and cpu = 3");

  ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_ROW);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 0), 4 /* duration */);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 1), timestamp);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 2), cpu_1);

  ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_DONE);
}

TEST_F(SchedSliceTableTest, UtidTest) {
  uint32_t cpu = 3;
  int64_t timestamp = 100;
  uint32_t pid_1 = 2;
  uint32_t prev_state = 32;
  static const char kCommProc1[] = "process1";
  static const char kCommProc2[] = "process2";
  uint32_t pid_2 = 4;
  int32_t prio = 1024;
  sched_tracker_->PushSchedSwitch(cpu, timestamp, pid_1, kCommProc2, prio,
                                  prev_state, pid_2, kCommProc1, prio);
  sched_tracker_->PushSchedSwitch(cpu, timestamp + 3, pid_2, kCommProc1, prio,
                                  prev_state, pid_1, kCommProc2, prio);
  sched_tracker_->PushSchedSwitch(cpu, timestamp + 4, pid_1, kCommProc2, prio,
                                  prev_state, pid_2, kCommProc1, prio);
  sched_tracker_->PushSchedSwitch(cpu, timestamp + 10, pid_2, kCommProc1, prio,
                                  prev_state, pid_1, kCommProc2, prio);

  PrepareValidStatement("SELECT utid FROM sched where dur != 0 ORDER BY utid");

  ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_ROW);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 0), 1 /* duration */);

  ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_ROW);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 0), 1 /* duration */);

  ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_ROW);
  ASSERT_EQ(sqlite3_column_int64(*stmt_, 0), 2 /* duration */);

  ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_DONE);
}

TEST_F(SchedSliceTableTest, TimestampFiltering) {
  uint32_t cpu_5 = 5;
  uint32_t cpu_7 = 7;
  uint32_t pid_1 = 1;
  uint32_t pid_2 = 2;
  int64_t prev_state = 32;
  int32_t prio = 1024;

  // Fill |cpu_5| and |cpu_7) with one sched switch per time unit starting,
  // respectively, @ T=50 and T=70.
  for (int64_t i = 0; i <= 11; i++) {
    sched_tracker_->PushSchedSwitch(cpu_5, 50 + i, pid_1, "pid_1", prio,
                                    prev_state, pid_1, "pid_1", prio);
  }
  for (int64_t i = 0; i <= 11; i++) {
    sched_tracker_->PushSchedSwitch(cpu_7, 70 + i, pid_2, "pid_2", prio,
                                    prev_state, pid_2, "pid_2", prio);
  }

  auto query = [this](const std::string& where_clauses) {
    PrepareValidStatement("SELECT ts from sched WHERE dur != 0 and " +
                          where_clauses);
    std::vector<int> res;
    while (sqlite3_step(*stmt_) == SQLITE_ROW) {
      res.push_back(sqlite3_column_int(*stmt_, 0));
    }
    return res;
  };

  ASSERT_THAT(query("ts > 55 and ts <= 60"), ElementsAre(56, 57, 58, 59, 60));
  ASSERT_THAT(query("ts >= 55 and ts < 52"), IsEmpty());
  ASSERT_THAT(query("ts >= 70 and ts < 71"), ElementsAre(70));
  ASSERT_THAT(query("ts >= 59 and ts < 73"), ElementsAre(59, 60, 70, 71, 72));
}

}  // namespace
}  // namespace trace_processor
}  // namespace perfetto