1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "thread_table.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 namespace {
21 enum Index { ID = 0, TYPE, TID, NAME, START_TS, END_TS, INTERNAL_PID, IS_MAIN_THREAD };
22 }
ThreadTable(const TraceDataCache * dataCache)23 ThreadTable::ThreadTable(const TraceDataCache* dataCache) : TableBase(dataCache)
24 {
25 tableColumn_.push_back(TableBase::ColumnInfo("id", "INT"));
26 tableColumn_.push_back(TableBase::ColumnInfo("type", "STRING"));
27 tableColumn_.push_back(TableBase::ColumnInfo("tid", "UNSIGNED INT"));
28 tableColumn_.push_back(TableBase::ColumnInfo("name", "STRING"));
29 tableColumn_.push_back(TableBase::ColumnInfo("start_ts", "UNSIGNED BIG INT"));
30 tableColumn_.push_back(TableBase::ColumnInfo("end_ts", "UNSIGNED BIG INT"));
31 tableColumn_.push_back(TableBase::ColumnInfo("ipid", "UNSIGNED INT"));
32 tableColumn_.push_back(TableBase::ColumnInfo("is_main_thread", "UNSIGNED INT"));
33 tablePriKey_.push_back("id");
34 }
35
~ThreadTable()36 ThreadTable::~ThreadTable() {}
37
CreateCursor()38 void ThreadTable::CreateCursor()
39 {
40 cursor_ = std::make_unique<Cursor>(dataCache_);
41 }
42
Cursor(const TraceDataCache * dataCache)43 ThreadTable::Cursor::Cursor(const TraceDataCache* dataCache)
44 : TableBase::Cursor(dataCache, 0, static_cast<uint32_t>(dataCache->ThreadSize()))
45 {
46 }
47
~Cursor()48 ThreadTable::Cursor::~Cursor() {}
49
Column(int column) const50 int ThreadTable::Cursor::Column(int column) const
51 {
52 switch (column) {
53 case ID: {
54 sqlite3_result_int64(context_, CurrentRow());
55 break;
56 }
57 case TYPE: {
58 sqlite3_result_text(context_, "thread", strlen("thread"), nullptr);
59 break;
60 }
61 case TID: {
62 const auto& process = dataCache_->GetConstThreadData(CurrentRow());
63 sqlite3_result_int64(context_, static_cast<int>(process.tid_));
64 break;
65 }
66 case NAME: {
67 const auto& thread = dataCache_->GetConstThreadData(CurrentRow());
68 const auto& name = dataCache_->GetDataFromDict(thread.nameIndex_);
69 if (name.size()) {
70 sqlite3_result_text(context_, name.c_str(), static_cast<int>(name.length()), nullptr);
71 }
72 break;
73 }
74 case START_TS: {
75 const auto& thread = dataCache_->GetConstThreadData(CurrentRow());
76 if (thread.startT_) {
77 sqlite3_result_int64(context_, static_cast<int64_t>(thread.startT_));
78 }
79 break;
80 }
81 case END_TS: {
82 const auto& thread = dataCache_->GetConstThreadData(CurrentRow());
83 if (thread.endT_) {
84 sqlite3_result_int64(context_, static_cast<int64_t>(thread.endT_));
85 }
86 break;
87 }
88 case INTERNAL_PID: {
89 const auto& thread = dataCache_->GetConstThreadData(CurrentRow());
90 if (thread.internalPid_) {
91 sqlite3_result_int(context_, static_cast<int>(thread.internalPid_));
92 }
93 break;
94 }
95 case IS_MAIN_THREAD: {
96 const auto& thread = dataCache_->GetConstThreadData(CurrentRow());
97 // When it is not clear which process the thread belongs to, is_main_thread should be set to null
98 if (!thread.internalPid_) {
99 break;
100 }
101 const auto& process = dataCache_->GetConstProcessData(thread.internalPid_);
102 sqlite3_result_int(context_, thread.tid_ == process.pid_);
103 break;
104 }
105 default:
106 TS_LOGF("Unregistered column : %d", column);
107 break;
108 }
109 return SQLITE_OK;
110 }
111 } // namespace TraceStreamer
112 } // namespace SysTuning
113