1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 "smaps_table.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 enum class Index : int32_t {
21 ID = 0,
22 TIME_STAMP,
23 START_ADDRESS,
24 END_ADDRESS,
25 DIRTY,
26 SWAPPER,
27 RSS,
28 PSS,
29 SIZE,
30 RESIDE,
31 PROTECTION,
32 PATH,
33 SHARED_CLEAN,
34 SHARED_DIRTY,
35 PRIVATE_CLEAN,
36 PRIVATE_DIRTY,
37 SWAP,
38 SWAP_PSS,
39 TYPE
40 };
SmapsTable(const TraceDataCache * dataCache)41 SmapsTable::SmapsTable(const TraceDataCache *dataCache) : TableBase(dataCache)
42 {
43 tableColumn_.push_back(TableBase::ColumnInfo("id", "INTEGER"));
44 tableColumn_.push_back(TableBase::ColumnInfo("timeStamp", "INTEGER"));
45 tableColumn_.push_back(TableBase::ColumnInfo("start_addr", "TEXT"));
46 tableColumn_.push_back(TableBase::ColumnInfo("end_addr", "TEXT"));
47 tableColumn_.push_back(TableBase::ColumnInfo("dirty", "INTEGER"));
48 tableColumn_.push_back(TableBase::ColumnInfo("swapper", "INTEGER"));
49 tableColumn_.push_back(TableBase::ColumnInfo("resident_size", "INTEGER"));
50 tableColumn_.push_back(TableBase::ColumnInfo("pss", "INTEGER"));
51 tableColumn_.push_back(TableBase::ColumnInfo("virtaul_size", "INTEGER"));
52 tableColumn_.push_back(TableBase::ColumnInfo("reside", "REAL"));
53 tableColumn_.push_back(TableBase::ColumnInfo("protection_id", "INTEGER"));
54 tableColumn_.push_back(TableBase::ColumnInfo("path_id", "INTEGER"));
55 tableColumn_.push_back(TableBase::ColumnInfo("shared_clean", "INTEGER"));
56 tableColumn_.push_back(TableBase::ColumnInfo("shared_dirty", "INTEGER"));
57 tableColumn_.push_back(TableBase::ColumnInfo("private_clean", "INTEGER"));
58 tableColumn_.push_back(TableBase::ColumnInfo("private_dirty", "INTEGER"));
59 tableColumn_.push_back(TableBase::ColumnInfo("swap", "INTEGER"));
60 tableColumn_.push_back(TableBase::ColumnInfo("swap_pss", "INTEGER"));
61 tableColumn_.push_back(TableBase::ColumnInfo("type", "INTEGER"));
62 tablePriKey_.push_back("id");
63 }
64
~SmapsTable()65 SmapsTable::~SmapsTable() {}
66
CreateCursor()67 std::unique_ptr<TableBase::Cursor> SmapsTable::CreateCursor()
68 {
69 return std::make_unique<Cursor>(dataCache_, this);
70 }
71
Cursor(const TraceDataCache * dataCache,TableBase * table)72 SmapsTable::Cursor::Cursor(const TraceDataCache *dataCache, TableBase *table)
73 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstSmapsData().Size())),
74 smapsObj_(dataCache->GetConstSmapsData())
75 {
76 }
77
~Cursor()78 SmapsTable::Cursor::~Cursor() {}
79
Column(int32_t col) const80 int32_t SmapsTable::Cursor::Column(int32_t col) const
81 {
82 switch (static_cast<Index>(col)) {
83 case Index::ID:
84 sqlite3_result_int64(context_, smapsObj_.IdsData()[CurrentRow()]);
85 break;
86 case Index::TIME_STAMP:
87 sqlite3_result_int64(context_, smapsObj_.TimeStamps()[CurrentRow()]);
88 break;
89 case Index::START_ADDRESS:
90 sqlite3_result_text(context_, smapsObj_.StartAddrs()[CurrentRow()].c_str(), STR_DEFAULT_LEN, nullptr);
91 break;
92 case Index::END_ADDRESS:
93 sqlite3_result_text(context_, smapsObj_.EndAddrs()[CurrentRow()].c_str(), STR_DEFAULT_LEN, nullptr);
94 break;
95 case Index::DIRTY:
96 sqlite3_result_int64(context_, smapsObj_.Dirtys()[CurrentRow()]);
97 break;
98 case Index::SWAPPER:
99 sqlite3_result_int64(context_, smapsObj_.Swappers()[CurrentRow()]);
100 break;
101 case Index::RSS:
102 sqlite3_result_int64(context_, smapsObj_.Rss()[CurrentRow()]);
103 break;
104 case Index::PSS:
105 sqlite3_result_int64(context_, smapsObj_.Pss()[CurrentRow()]);
106 break;
107 case Index::SIZE:
108 sqlite3_result_int64(context_, smapsObj_.Sizes()[CurrentRow()]);
109 break;
110 default:
111 HandleTypeColumns(col);
112 }
113 return SQLITE_OK;
114 }
HandleTypeColumns(int32_t col) const115 void SmapsTable::Cursor::HandleTypeColumns(int32_t col) const
116 {
117 switch (static_cast<Index>(col)) {
118 case Index::RESIDE:
119 sqlite3_result_double(context_, smapsObj_.Resides()[CurrentRow()]);
120 break;
121 case Index::PROTECTION:
122 sqlite3_result_int64(context_, smapsObj_.ProtectionIds()[CurrentRow()]);
123 break;
124 case Index::PATH:
125 sqlite3_result_int64(context_, smapsObj_.PathIds()[CurrentRow()]);
126 break;
127 case Index::SHARED_CLEAN:
128 sqlite3_result_int64(context_, smapsObj_.SharedClean()[CurrentRow()]);
129 break;
130 case Index::SHARED_DIRTY:
131 sqlite3_result_int64(context_, smapsObj_.SharedDirty()[CurrentRow()]);
132 break;
133 case Index::PRIVATE_CLEAN:
134 sqlite3_result_int64(context_, smapsObj_.PrivateClean()[CurrentRow()]);
135 break;
136 case Index::PRIVATE_DIRTY:
137 sqlite3_result_int64(context_, smapsObj_.PrivateDirty()[CurrentRow()]);
138 break;
139 case Index::SWAP:
140 sqlite3_result_int64(context_, smapsObj_.Swap()[CurrentRow()]);
141 break;
142 case Index::SWAP_PSS:
143 sqlite3_result_int64(context_, smapsObj_.SwapPss()[CurrentRow()]);
144 break;
145 case Index::TYPE:
146 sqlite3_result_int64(context_, smapsObj_.Type()[CurrentRow()]);
147 break;
148 default:
149 TS_LOGF("Unregistered column : %d", col);
150 break;
151 }
152 }
153 } // namespace TraceStreamer
154 } // namespace SysTuning
155