1 /*
2  * Copyright (C) 2019 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/android_logs_table.h"
18 
19 namespace perfetto {
20 namespace trace_processor {
21 
AndroidLogsTable(sqlite3 *,const TraceStorage * storage)22 AndroidLogsTable::AndroidLogsTable(sqlite3*, const TraceStorage* storage)
23     : storage_(storage) {}
24 
RegisterTable(sqlite3 * db,const TraceStorage * storage)25 void AndroidLogsTable::RegisterTable(sqlite3* db, const TraceStorage* storage) {
26   Table::Register<AndroidLogsTable>(db, storage, "android_logs");
27 }
28 
CreateStorageSchema()29 StorageSchema AndroidLogsTable::CreateStorageSchema() {
30   const auto& alog = storage_->android_logs();
31   // Note: the logs in the storage are NOT sorted by timestamp. We delegate
32   // that to the on-demand sorter by calling AddNumericColumn (instead of
33   // AddSortedNumericColumn).
34   return StorageSchema::Builder()
35       .AddNumericColumn("ts", &alog.timestamps())
36       .AddNumericColumn("utid", &alog.utids())
37       .AddNumericColumn("prio", &alog.prios())
38       .AddStringColumn("tag", &alog.tag_ids(), &storage_->string_pool())
39       .AddStringColumn("msg", &alog.msg_ids(), &storage_->string_pool())
40       .Build({"ts", "utid", "msg"});
41 }
42 
RowCount()43 uint32_t AndroidLogsTable::RowCount() {
44   return static_cast<uint32_t>(storage_->android_logs().size());
45 }
46 
BestIndex(const QueryConstraints & qc,BestIndexInfo * info)47 int AndroidLogsTable::BestIndex(const QueryConstraints& qc,
48                                 BestIndexInfo* info) {
49   info->estimated_cost = static_cast<uint32_t>(storage_->android_logs().size());
50 
51   info->order_by_consumed = true;
52 
53   // Only the string columns are handled by SQLite.
54   size_t tag_index = schema().ColumnIndexFromName("tag");
55   size_t msg_index = schema().ColumnIndexFromName("msg");
56   for (size_t i = 0; i < qc.constraints().size(); i++) {
57     info->omit[i] =
58         qc.constraints()[i].iColumn != static_cast<int>(tag_index) &&
59         qc.constraints()[i].iColumn != static_cast<int>(msg_index);
60   }
61 
62   return SQLITE_OK;
63 }
64 }  // namespace trace_processor
65 }  // namespace perfetto
66