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 sqlite3_result_text(context_, name.c_str(), static_cast<int>(name.length()), nullptr);
70 break;
71 }
72 case START_TS: {
73 const auto& thread = dataCache_->GetConstThreadData(CurrentRow());
74 if (thread.startT_) {
75 sqlite3_result_int64(context_, static_cast<int64_t>(thread.startT_));
76 }
77 break;
78 }
79 case END_TS: {
80 const auto& thread = dataCache_->GetConstThreadData(CurrentRow());
81 if (thread.endT_) {
82 sqlite3_result_int64(context_, static_cast<int64_t>(thread.endT_));
83 }
84 break;
85 }
86 case INTERNAL_PID: {
87 const auto& thread = dataCache_->GetConstThreadData(CurrentRow());
88 if (thread.internalPid_) {
89 sqlite3_result_int(context_, static_cast<int>(thread.internalPid_));
90 }
91 break;
92 }
93 case IS_MAIN_THREAD: {
94 const auto& thread = dataCache_->GetConstThreadData(CurrentRow());
95 // When it is not clear which process the thread belongs to, is_main_thread should be set to null
96 if (!thread.internalPid_) {
97 break;
98 }
99 const auto& process = dataCache_->GetConstProcessData(thread.internalPid_);
100 sqlite3_result_int(context_, thread.tid_ == process.pid_);
101 break;
102 }
103 default:
104 TS_LOGF("Unregistered column : %d", column);
105 break;
106 }
107 return SQLITE_OK;
108 }
109 } // namespace TraceStreamer
110 } // namespace SysTuning
111