Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "src/trace_processor/thread_table.h" |
| 18 | #include "src/trace_processor/process_table.h" |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 19 | #include "src/trace_processor/process_tracker.h" |
Primiano Tucci | 0d72a31 | 2018-08-07 14:42:45 +0100 | [diff] [blame] | 20 | #include "src/trace_processor/proto_trace_parser.h" |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 21 | #include "src/trace_processor/sched_tracker.h" |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 22 | #include "src/trace_processor/scoped_db.h" |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 23 | #include "src/trace_processor/trace_processor_context.h" |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 24 | |
| 25 | #include "gmock/gmock.h" |
| 26 | #include "gtest/gtest.h" |
| 27 | |
| 28 | namespace perfetto { |
| 29 | namespace trace_processor { |
| 30 | namespace { |
| 31 | |
| 32 | class ThreadTableUnittest : public ::testing::Test { |
| 33 | public: |
| 34 | ThreadTableUnittest() { |
| 35 | sqlite3* db = nullptr; |
| 36 | PERFETTO_CHECK(sqlite3_open(":memory:", &db) == SQLITE_OK); |
| 37 | db_.reset(db); |
| 38 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 39 | context_.storage.reset(new TraceStorage()); |
| 40 | context_.process_tracker.reset(new ProcessTracker(&context_)); |
| 41 | context_.sched_tracker.reset(new SchedTracker(&context_)); |
| 42 | |
| 43 | ThreadTable::RegisterTable(db_.get(), context_.storage.get()); |
| 44 | ProcessTable::RegisterTable(db_.get(), context_.storage.get()); |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void PrepareValidStatement(const std::string& sql) { |
| 48 | int size = static_cast<int>(sql.size()); |
| 49 | sqlite3_stmt* stmt; |
| 50 | ASSERT_EQ(sqlite3_prepare_v2(*db_, sql.c_str(), size, &stmt, nullptr), |
| 51 | SQLITE_OK); |
| 52 | stmt_.reset(stmt); |
| 53 | } |
| 54 | |
| 55 | const char* GetColumnAsText(int colId) { |
| 56 | return reinterpret_cast<const char*>(sqlite3_column_text(*stmt_, colId)); |
| 57 | } |
| 58 | |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 59 | ~ThreadTableUnittest() override { context_.storage->ResetStorage(); } |
| 60 | |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 61 | protected: |
Isabelle Taylor | a0a2297 | 2018-08-03 12:06:12 +0100 | [diff] [blame] | 62 | TraceProcessorContext context_; |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 63 | ScopedDb db_; |
| 64 | ScopedStmt stmt_; |
| 65 | }; |
| 66 | |
| 67 | TEST_F(ThreadTableUnittest, Select) { |
| 68 | uint32_t cpu = 3; |
| 69 | uint64_t timestamp = 100; |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 70 | uint32_t prev_state = 32; |
Primiano Tucci | fbc1f60 | 2018-08-08 18:23:32 +0100 | [diff] [blame] | 71 | static const char kThreadName1[] = "thread1"; |
| 72 | static const char kThreadName2[] = "thread2"; |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 73 | |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 74 | context_.sched_tracker->PushSchedSwitch(cpu, timestamp, /*tid=*/1, prev_state, |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame^] | 75 | kThreadName1, /*tid=*/4); |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 76 | context_.sched_tracker->PushSchedSwitch(cpu, timestamp + 1, /*tid=*/4, |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame^] | 77 | prev_state, kThreadName2, /*tid=*/1); |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 78 | |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame^] | 79 | context_.process_tracker->UpdateProcess(2, "test"); |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 80 | context_.process_tracker->UpdateThread(4 /*tid*/, 2 /*pid*/); |
Primiano Tucci | fbc1f60 | 2018-08-08 18:23:32 +0100 | [diff] [blame] | 81 | PrepareValidStatement("SELECT utid, upid, tid, name FROM thread where tid=4"); |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 82 | |
| 83 | ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_ROW); |
| 84 | ASSERT_EQ(sqlite3_column_int(*stmt_, 0), 1 /* utid */); |
| 85 | ASSERT_EQ(sqlite3_column_int(*stmt_, 1), 1 /* upid */); |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 86 | ASSERT_EQ(sqlite3_column_int(*stmt_, 2), 4 /* tid */); |
Primiano Tucci | fbc1f60 | 2018-08-08 18:23:32 +0100 | [diff] [blame] | 87 | ASSERT_STREQ(GetColumnAsText(3), kThreadName2); |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 88 | |
| 89 | ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_DONE); |
| 90 | } |
| 91 | |
| 92 | TEST_F(ThreadTableUnittest, SelectWhere) { |
| 93 | uint32_t cpu = 3; |
| 94 | uint64_t timestamp = 100; |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 95 | uint32_t prev_state = 32; |
Primiano Tucci | fbc1f60 | 2018-08-08 18:23:32 +0100 | [diff] [blame] | 96 | static const char kThreadName1[] = "thread1"; |
| 97 | static const char kThreadName2[] = "thread2"; |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 98 | |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 99 | context_.sched_tracker->PushSchedSwitch(cpu, timestamp, /*tid=*/1, prev_state, |
Primiano Tucci | fbc1f60 | 2018-08-08 18:23:32 +0100 | [diff] [blame] | 100 | kThreadName1, |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 101 | /*tid=*/4); |
| 102 | context_.sched_tracker->PushSchedSwitch(cpu, timestamp + 1, /*tid=*/4, |
Primiano Tucci | fbc1f60 | 2018-08-08 18:23:32 +0100 | [diff] [blame] | 103 | prev_state, kThreadName2, |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 104 | /*tid=*/1); |
| 105 | context_.sched_tracker->PushSchedSwitch(cpu, timestamp + 2, /*tid=*/1, |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame^] | 106 | prev_state, kThreadName1, /*tid=*/4); |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 107 | |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame^] | 108 | context_.process_tracker->UpdateProcess(2, "test"); |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 109 | context_.process_tracker->UpdateThread(4 /*tid*/, 2 /*pid*/); |
| 110 | context_.process_tracker->UpdateThread(1 /*tid*/, 2 /*pid*/); |
| 111 | PrepareValidStatement( |
| 112 | "SELECT utid, upid, tid, name FROM thread where tid = 4"); |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 113 | |
| 114 | ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_ROW); |
| 115 | ASSERT_EQ(sqlite3_column_int(*stmt_, 0), 1 /* utid */); |
| 116 | ASSERT_EQ(sqlite3_column_int(*stmt_, 1), 1 /* upid */); |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 117 | ASSERT_EQ(sqlite3_column_int(*stmt_, 2), 4 /* tid */); |
Primiano Tucci | fbc1f60 | 2018-08-08 18:23:32 +0100 | [diff] [blame] | 118 | ASSERT_STREQ(GetColumnAsText(3), kThreadName2); |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 119 | |
| 120 | ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_DONE); |
| 121 | } |
| 122 | |
| 123 | TEST_F(ThreadTableUnittest, JoinWithProcess) { |
| 124 | uint32_t cpu = 3; |
| 125 | uint64_t timestamp = 100; |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 126 | uint32_t prev_state = 32; |
Primiano Tucci | fbc1f60 | 2018-08-08 18:23:32 +0100 | [diff] [blame] | 127 | static const char kThreadName1[] = "thread1"; |
| 128 | static const char kThreadName2[] = "thread2"; |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 129 | |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 130 | context_.sched_tracker->PushSchedSwitch(cpu, timestamp, /*tid=*/1, prev_state, |
Primiano Tucci | fbc1f60 | 2018-08-08 18:23:32 +0100 | [diff] [blame] | 131 | kThreadName1, |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame^] | 132 | |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 133 | /*tid=*/4); |
| 134 | context_.sched_tracker->PushSchedSwitch(cpu, timestamp + 1, /*tid=*/4, |
Primiano Tucci | fbc1f60 | 2018-08-08 18:23:32 +0100 | [diff] [blame] | 135 | prev_state, kThreadName2, |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame^] | 136 | |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 137 | /*tid=*/1); |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 138 | |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 139 | // Also create a process for which we haven't seen any thread. |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame^] | 140 | context_.process_tracker->UpdateProcess(7, "pid7"); |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 141 | |
Primiano Tucci | 2da5d2e | 2018-08-10 14:23:31 +0100 | [diff] [blame^] | 142 | context_.process_tracker->UpdateProcess(2, "pid2"); |
Primiano Tucci | fbc1f60 | 2018-08-08 18:23:32 +0100 | [diff] [blame] | 143 | context_.process_tracker->UpdateThread(/*tid=*/4, /*pid=*/2); |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 144 | |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 145 | PrepareValidStatement( |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 146 | "SELECT utid, thread.tid, thread.name, process.upid, process.pid, " |
Primiano Tucci | fbc1f60 | 2018-08-08 18:23:32 +0100 | [diff] [blame] | 147 | "process.name FROM thread INNER JOIN process USING (upid) WHERE pid = 2 " |
| 148 | "ORDER BY tid"); |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 149 | |
Primiano Tucci | fbc1f60 | 2018-08-08 18:23:32 +0100 | [diff] [blame] | 150 | // At this point we should see two threads bound to process with pid=2: |
| 151 | |
| 152 | // (1) The (implicitly created) main thread (tid=pid=2). |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 153 | ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_ROW); |
Primiano Tucci | fbc1f60 | 2018-08-08 18:23:32 +0100 | [diff] [blame] | 154 | auto utid_for_tid2 = sqlite3_column_int(*stmt_, 0); |
| 155 | ASSERT_GT(utid_for_tid2, 0); |
| 156 | ASSERT_EQ(sqlite3_column_int(*stmt_, 1), 2 /* tid */); |
Primiano Tucci | b75dcee | 2018-08-08 12:21:36 +0100 | [diff] [blame] | 157 | ASSERT_EQ(sqlite3_column_int(*stmt_, 4), 2 /* pid */); |
Primiano Tucci | fbc1f60 | 2018-08-08 18:23:32 +0100 | [diff] [blame] | 158 | ASSERT_EQ(sqlite3_column_int(*stmt_, 3), 2 /* upid */); |
| 159 | ASSERT_STREQ(GetColumnAsText(2), ""); // No name seen for main thread. |
| 160 | ASSERT_STREQ(GetColumnAsText(5), "pid2"); |
| 161 | |
| 162 | // (2) A thread with tid=4. |
| 163 | ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_ROW); |
| 164 | auto utid_for_tid4 = sqlite3_column_int(*stmt_, 0); |
| 165 | ASSERT_GT(utid_for_tid4, 0); |
| 166 | ASSERT_NE(utid_for_tid4, utid_for_tid2); |
| 167 | ASSERT_EQ(sqlite3_column_int(*stmt_, 1), 4 /* tid */); |
| 168 | ASSERT_EQ(sqlite3_column_int(*stmt_, 4), 2 /* pid */); |
| 169 | ASSERT_EQ(sqlite3_column_int(*stmt_, 3), 2 /* upid */); |
| 170 | ASSERT_STREQ(GetColumnAsText(2), kThreadName2); |
| 171 | ASSERT_STREQ(GetColumnAsText(5), "pid2"); |
Isabelle Taylor | da55162 | 2018-07-17 11:48:53 +0100 | [diff] [blame] | 172 | |
| 173 | ASSERT_EQ(sqlite3_step(*stmt_), SQLITE_DONE); |
| 174 | } |
| 175 | |
| 176 | } // namespace |
| 177 | } // namespace trace_processor |
| 178 | } // namespace perfetto |