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 "process_table.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 namespace {
21 enum Index { ID = 0, TYPE, PID, NAME, START_TS, END_TS, PARENT_ID, UID, APP_ID };
22 }
ProcessTable(const TraceDataCache * dataCache)23 ProcessTable::ProcessTable(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("pid", "UNSIGNED INT"));
28 tableColumn_.push_back(TableBase::ColumnInfo("name", "STRING"));
29 tableColumn_.push_back(TableBase::ColumnInfo("start_ts", "UNSIGNED INT"));
30 tablePriKey_.push_back("id");
31 }
32
~ProcessTable()33 ProcessTable::~ProcessTable() {}
34
CreateCursor()35 void ProcessTable::CreateCursor()
36 {
37 cursor_ = std::make_unique<Cursor>(dataCache_);
38 }
39
Cursor(const TraceDataCache * dataCache)40 ProcessTable::Cursor::Cursor(const TraceDataCache* dataCache)
41 : TableBase::Cursor(dataCache, 0, static_cast<uint32_t>(dataCache->ProcessSize()))
42 {
43 }
44
~Cursor()45 ProcessTable::Cursor::~Cursor() {}
46
Column(int column) const47 int ProcessTable::Cursor::Column(int column) const
48 {
49 const auto& process = dataCache_->GetConstProcessData(CurrentRow());
50 switch (column) {
51 case ID:
52 sqlite3_result_int64(context_, CurrentRow());
53 break;
54 case TYPE:
55 sqlite3_result_text(context_, "process", STR_DEFAULT_LEN, nullptr);
56 break;
57 case PID:
58 sqlite3_result_int64(context_, process.pid_);
59 break;
60 case NAME:
61 if (process.cmdLine_.size()) {
62 sqlite3_result_text(context_, process.cmdLine_.c_str(), static_cast<int>(process.cmdLine_.length()),
63 nullptr);
64 }
65 break;
66 case START_TS:
67 if (process.startT_) {
68 sqlite3_result_int64(context_, static_cast<int64_t>(process.startT_));
69 }
70 break;
71 default:
72 TS_LOGF("Unregistered column : %d", column);
73 break;
74 }
75 return SQLITE_OK;
76 }
77 } // namespace TraceStreamer
78 } // namespace SysTuning
79