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_state_table.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 namespace {
21 enum Index { ID = 0, TYPE, TS, DUR, CPU, INTERNAL_TID, STATE };
22 }
ThreadStateTable(const TraceDataCache * dataCache)23 ThreadStateTable::ThreadStateTable(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("ts", "INT"));
28 tableColumn_.push_back(TableBase::ColumnInfo("dur", "UNSIGNED BIG INT"));
29 tableColumn_.push_back(TableBase::ColumnInfo("cpu", "UNSIGNED BIG INT"));
30 tableColumn_.push_back(TableBase::ColumnInfo("itid", "INT"));
31 tableColumn_.push_back(TableBase::ColumnInfo("state", "STRING"));
32 tablePriKey_.push_back("id");
33 }
34
~ThreadStateTable()35 ThreadStateTable::~ThreadStateTable() {}
36
CreateCursor()37 void ThreadStateTable::CreateCursor()
38 {
39 cursor_ = std::make_unique<Cursor>(dataCache_);
40 }
41
Cursor(const TraceDataCache * dataCache)42 ThreadStateTable::Cursor::Cursor(const TraceDataCache* dataCache)
43 : TableBase::Cursor(dataCache, 0, static_cast<uint32_t>(dataCache->GetConstThreadStateData().Size())),
44 threadStateObj_(dataCache->GetConstThreadStateData())
45 {
46 }
47
~Cursor()48 ThreadStateTable::Cursor::~Cursor() {}
49
Column(int col) const50 int ThreadStateTable::Cursor::Column(int col) const
51 {
52 switch (col) {
53 case ID:
54 sqlite3_result_int64(context_, static_cast<sqlite3_int64>(CurrentRow()));
55 break;
56 case TYPE:
57 sqlite3_result_text(context_, "thread_state", STR_DEFAULT_LEN, nullptr);
58 break;
59 case TS:
60 sqlite3_result_int64(context_, static_cast<sqlite3_int64>(threadStateObj_.TimeStamData()[CurrentRow()]));
61 break;
62 case DUR:
63 if (static_cast<sqlite3_int64>(threadStateObj_.DursData()[CurrentRow()]) >= 0) {
64 sqlite3_result_int64(context_, static_cast<sqlite3_int64>(threadStateObj_.DursData()[CurrentRow()]));
65 }
66 break;
67 case CPU:
68 if (static_cast<int32_t>(threadStateObj_.CpusData()[CurrentRow()]) >= 0) {
69 sqlite3_result_int64(context_, static_cast<sqlite3_int64>(threadStateObj_.CpusData()[CurrentRow()]));
70 }
71 break;
72 case INTERNAL_TID:
73 sqlite3_result_int64(context_,
74 static_cast<sqlite3_int64>(threadStateObj_.InternalTidsData()[CurrentRow()]));
75 break;
76 case STATE: {
77 const std::string& str = dataCache_->GetConstSchedStateData(threadStateObj_.StatesData()[CurrentRow()]);
78 sqlite3_result_text(context_, str.c_str(), STR_DEFAULT_LEN, nullptr);
79 break;
80 }
81 default:
82 TS_LOGF("Unregistered column : %d", col);
83 break;
84 }
85 return SQLITE_OK;
86 }
87 } // namespace TraceStreamer
88 } // namespace SysTuning
89