• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "callstack_table.h"
17 
18 namespace SysTuning {
19 namespace TraceStreamer {
20 namespace {
21 enum Index {
22     ID = 0,
23     TS,
24     DUR,
25     CALL_ID,
26     CAT,
27     NAME,
28     DEPTH,
29     COOKIE_ID,
30     PARENT_ID,
31     ARGSET,
32     CHAIN_ID,
33     SPAN_ID,
34     PARENT_SPAN_ID,
35     FLAG,
36     ARGS
37 };
38 }
CallStackTable(const TraceDataCache * dataCache)39 CallStackTable::CallStackTable(const TraceDataCache* dataCache) : TableBase(dataCache)
40 {
41     tableColumn_.push_back(TableBase::ColumnInfo("id", "UNSIGNED BIG INT"));
42     tableColumn_.push_back(TableBase::ColumnInfo("ts", "UNSIGNED BIG INT"));
43     tableColumn_.push_back(TableBase::ColumnInfo("dur", "UNSIGNED BIG INT"));
44     tableColumn_.push_back(TableBase::ColumnInfo("callid", "UNSIGNED INT"));
45     tableColumn_.push_back(TableBase::ColumnInfo("cat", "STRING"));
46     tableColumn_.push_back(TableBase::ColumnInfo("name", "STRING"));
47     tableColumn_.push_back(TableBase::ColumnInfo("depth", "UNSIGNED INT"));
48     tableColumn_.push_back(TableBase::ColumnInfo("cookie", "UNSIGNED BIG INT"));
49     tableColumn_.push_back(TableBase::ColumnInfo("parent_id", "UNSIGNED INT"));
50     tableColumn_.push_back(TableBase::ColumnInfo("argsetid", "UNSIGNED INT"));
51     tableColumn_.push_back(TableBase::ColumnInfo("chainId", "STRING"));
52     tableColumn_.push_back(TableBase::ColumnInfo("spanId", "STRING"));
53     tableColumn_.push_back(TableBase::ColumnInfo("parentSpanId", "STRING"));
54     tableColumn_.push_back(TableBase::ColumnInfo("flag", "STRING"));
55     tableColumn_.push_back(TableBase::ColumnInfo("args", "STRING"));
56     tablePriKey_.push_back("callid");
57     tablePriKey_.push_back("ts");
58     tablePriKey_.push_back("depth");
59 }
60 
~CallStackTable()61 CallStackTable::~CallStackTable() {}
62 
CreateCursor()63 void CallStackTable::CreateCursor()
64 {
65     cursor_ = std::make_unique<Cursor>(dataCache_);
66 }
67 
Cursor(const TraceDataCache * dataCache)68 CallStackTable::Cursor::Cursor(const TraceDataCache* dataCache)
69     : TableBase::Cursor(dataCache, 0, static_cast<uint32_t>(dataCache->GetConstInternalSlicesData().Size())),
70       slicesObj_(dataCache->GetConstInternalSlicesData())
71 {
72 }
73 
~Cursor()74 CallStackTable::Cursor::~Cursor() {}
75 
Column(int column) const76 int CallStackTable::Cursor::Column(int column) const
77 {
78     switch (column) {
79         case ID:
80             sqlite3_result_int64(context_, CurrentRow());
81             break;
82         case TS:
83             sqlite3_result_int64(context_, static_cast<int64_t>(slicesObj_.TimeStamData()[CurrentRow()]));
84             break;
85         case DUR:
86             sqlite3_result_int64(context_, static_cast<int64_t>(slicesObj_.DursData()[CurrentRow()]));
87             break;
88         case CALL_ID:
89             sqlite3_result_int64(context_, static_cast<int64_t>(slicesObj_.CallIds()[CurrentRow()]));
90             break;
91         case CAT: {
92             if (slicesObj_.CatsData()[CurrentRow()] != INVALID_UINT64) {
93                 auto catsDataIndex = static_cast<size_t>(slicesObj_.CatsData()[CurrentRow()]);
94                 sqlite3_result_text(context_, dataCache_->GetDataFromDict(catsDataIndex).c_str(), STR_DEFAULT_LEN,
95                                     nullptr);
96             }
97             break;
98         }
99         case NAME: {
100             if (slicesObj_.NamesData()[CurrentRow()] != INVALID_UINT64) {
101                 auto nameDataIndex = static_cast<size_t>(slicesObj_.NamesData()[CurrentRow()]);
102                 sqlite3_result_text(context_, dataCache_->GetDataFromDict(nameDataIndex).c_str(), STR_DEFAULT_LEN,
103                                     nullptr);
104             }
105             break;
106         }
107         case DEPTH:
108             sqlite3_result_int64(context_, static_cast<int64_t>(slicesObj_.Depths()[CurrentRow()]));
109             break;
110         case COOKIE_ID:
111             if (slicesObj_.Cookies()[CurrentRow()] != INVALID_UINT64) {
112                 sqlite3_result_int64(context_, static_cast<int64_t>(slicesObj_.Cookies()[CurrentRow()]));
113             }
114             break;
115         case PARENT_ID: {
116             if (slicesObj_.ParentIdData()[CurrentRow()].has_value()) {
117                 sqlite3_result_int64(context_, static_cast<int64_t>(slicesObj_.ParentIdData()[CurrentRow()].value()));
118             }
119             break;
120         }
121         case ARGSET:
122             if (slicesObj_.ArgSetIdsData()[CurrentRow()] != INVALID_UINT32) {
123                 sqlite3_result_int64(context_, static_cast<int64_t>(slicesObj_.ArgSetIdsData()[CurrentRow()]));
124             }
125             break;
126         case CHAIN_ID:
127             sqlite3_result_text(context_, slicesObj_.ChainIds()[CurrentRow()].c_str(), STR_DEFAULT_LEN, nullptr);
128             break;
129         case SPAN_ID:
130             sqlite3_result_text(context_, slicesObj_.SpanIds()[CurrentRow()].c_str(), STR_DEFAULT_LEN, nullptr);
131             break;
132         case PARENT_SPAN_ID:
133             sqlite3_result_text(context_, slicesObj_.ParentSpanIds()[CurrentRow()].c_str(), STR_DEFAULT_LEN, nullptr);
134             break;
135         case FLAG:
136             sqlite3_result_text(context_, slicesObj_.Flags()[CurrentRow()].c_str(), STR_DEFAULT_LEN, nullptr);
137             break;
138         case ARGS:
139             sqlite3_result_text(context_, slicesObj_.ArgsData()[CurrentRow()].c_str(), STR_DEFAULT_LEN, nullptr);
140             break;
141         default:
142             TS_LOGF("Unregistered column : %d", column);
143             break;
144     }
145     return SQLITE_OK;
146 }
147 } // namespace TraceStreamer
148 } // namespace SysTuning
149