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 "sdk_data_parser.h"
17 #include <cstdint>
18 #include <cstring>
19 #include <functional>
20 #include <sstream>
21 #include "clock_filter.h"
22 #include "gpu_counter_object_table.h"
23 #include "gpu_counter_table.h"
24 #include "json.hpp"
25 #include "log.h"
26 #include "demo_meta_table.h"
27 #include "sdk_plugin_data_parser.h"
28 #include "slice_object_table.h"
29 #include "slice_table.h"
30 #include "ts_common.h"
31 #include "ts_sdk_api.h"
32 #include "version.h"
33
34 namespace SysTuning {
35 namespace TraceStreamer {
SDKDataParser(TraceDataCache * dataCache)36 SDKDataParser::SDKDataParser(TraceDataCache* dataCache)
37 : traceDataCache_(dataCache), clockFilter_(std::make_unique<ClockFilter>())
38 {
39 }
40
GetPluginName(std::string pluginName)41 int32_t SDKDataParser::GetPluginName(std::string pluginName)
42 {
43 pluginName.replace(pluginName.find("-"), 1, "_");
44 counterTableName_ = pluginName + "_" + "counter_table";
45 counterObjectTableName_ = pluginName + "_" + "counterobj_table";
46 sliceTableName_ = pluginName + "_" + "slice_table";
47 sliceObjectName_ = pluginName + "_" + "sliceobj_table";
48 return 0;
49 }
ParseDataOver(TraceRangeCallbackFunction traceRangeCallbackFunction)50 int32_t SDKDataParser::ParseDataOver(TraceRangeCallbackFunction traceRangeCallbackFunction)
51 {
52 traceDataCache_->MixTraceTime(GetPluginStartTime(), GetPluginEndTime());
53 std::string traceRangeStr =
54 std::to_string(traceDataCache_->traceStartTime_) + ";" + std::to_string(traceDataCache_->traceEndTime_) + ";";
55 traceRangeCallbackFunction(traceRangeStr);
56 return 0;
57 }
58
GetJsonConfig(QueryResultCallbackFunction queryResultCallbackFunction)59 int32_t SDKDataParser::GetJsonConfig(QueryResultCallbackFunction queryResultCallbackFunction)
60 {
61 queryResultCallbackFunction(jsonConfig_, 1, 1);
62 return 0;
63 }
64
ParserData(const uint8_t * data,int32_t len,int32_t componentId)65 int32_t SDKDataParser::ParserData(const uint8_t* data, int32_t len, int32_t componentId)
66 {
67 if (componentId == DATA_TYPE_CLOCK) {
68 ParserClock(data, len);
69 return 0;
70 }
71 sdk_plugin_data_parser(data, len);
72 return 0;
73 }
74
ParserClock(const uint8_t * data,int32_t len)75 int32_t SDKDataParser::ParserClock(const uint8_t* data, int32_t len)
76 {
77 return clockFilter_->InitSnapShotTimeRange(data, len);
78 }
79
SetTableName(const char * counterTableName,const char * counterObjectTableName,const char * sliceTableName,const char * sliceObjectName)80 int32_t SDKDataParser::SetTableName(const char* counterTableName,
81 const char* counterObjectTableName,
82 const char* sliceTableName,
83 const char* sliceObjectName)
84 {
85 if (!g_isUseExternalModify) {
86 counterTableName_ = counterTableName;
87 counterObjectTableName_ = counterObjectTableName;
88 sliceTableName_ = sliceTableName;
89 sliceObjectName_ = sliceObjectName;
90 }
91 UpdateJson();
92 return 0;
93 }
94
UpdateJson()95 int32_t SDKDataParser::UpdateJson()
96 {
97 using json = nlohmann::json;
98 json jMessage = json::parse(jsonConfig_);
99 if (jMessage.is_discarded()) {
100 return -1;
101 }
102
103 jMessage["tableConfig"]["showType"][0]["tableName"] = counterTableName_;
104 jMessage["tableConfig"]["showType"][0]["inner"]["tableName"] = counterObjectTableName_;
105 jMessage["tableConfig"]["showType"][1]["tableName"] = sliceTableName_;
106 jMessage["tableConfig"]["showType"][1]["inner"]["tableName"] = sliceObjectName_;
107
108 jsonConfig_ = jMessage.dump();
109 return 0;
110 }
111
112 // 创建对应的表
CreateTableByJson()113 int32_t SDKDataParser::CreateTableByJson()
114 {
115 DemoTableBase::TableDeclare<DemoMetaTable>(*(traceDataCache_->demoDb_), traceDataCache_, "meta");
116 // 创建对应的表
117 CreateCounterObjectTable(counterObjectTableName_);
118 CreateCounterTable(counterTableName_);
119 CreateSliceObjectTable(sliceObjectName_);
120 CreateSliceTable(sliceTableName_);
121 return 0;
122 }
123
124 // 根据Json配置创建couter object表
CreateCounterObjectTable(const std::string & tableName)125 int32_t SDKDataParser::CreateCounterObjectTable(const std::string& tableName)
126 {
127 DemoTableBase::TableDeclare<GpuCounterObjectTable>(*(traceDataCache_->demoDb_), traceDataCache_, tableName);
128 return 0;
129 }
130
131 // 根据Json配置创建couter表
CreateCounterTable(const std::string & tableName)132 int32_t SDKDataParser::CreateCounterTable(const std::string& tableName)
133 {
134 DemoTableBase::TableDeclare<GpuCounterTable>(*(traceDataCache_->demoDb_), traceDataCache_, tableName);
135 return 0;
136 }
137
138 // 根据Json配置创建slice object表
CreateSliceObjectTable(const std::string & tableName)139 int32_t SDKDataParser::CreateSliceObjectTable(const std::string& tableName)
140 {
141 DemoTableBase::TableDeclare<SliceObjectTable>(*(traceDataCache_->demoDb_), traceDataCache_, tableName);
142 return 0;
143 }
144
145 // 根据Json配置创建slice表
CreateSliceTable(const std::string & tableName)146 int32_t SDKDataParser::CreateSliceTable(const std::string& tableName)
147 {
148 DemoTableBase::TableDeclare<SliceTable>(*(traceDataCache_->demoDb_), traceDataCache_, tableName);
149 return 0;
150 }
151
152 // Counter业务
AppendCounterObject(int32_t counterId,const char * columnName)153 int32_t SDKDataParser::AppendCounterObject(int32_t counterId, const char* columnName)
154 {
155 traceDataCache_->GetGpuCounterObjectData()->AppendNewData(counterId, columnName);
156 return 0;
157 }
158
AppendCounter(int32_t counterId,uint64_t ts,int32_t value)159 int32_t SDKDataParser::AppendCounter(int32_t counterId, uint64_t ts, int32_t value)
160 {
161 auto newTs = clockFilter_->ToPrimaryTraceTime(TS_CLOCK_REALTIME, ts);
162 UpdatePluginTimeRange(TS_CLOCK_BOOTTIME, ts, newTs);
163 traceDataCache_->GetGpuCounterData()->AppendNewData(newTs, counterId, value);
164 return 0;
165 }
166
167 // Slice业务
AppendSliceObject(int32_t sliceId,const char * columnName)168 int32_t SDKDataParser::AppendSliceObject(int32_t sliceId, const char* columnName)
169 {
170 traceDataCache_->GetSliceObjectData()->AppendNewData(sliceId, columnName);
171 return 0;
172 }
173
AppendSlice(int32_t sliceId,uint64_t ts,uint64_t endTs,int32_t value)174 int32_t SDKDataParser::AppendSlice(int32_t sliceId, uint64_t ts, uint64_t endTs, int32_t value)
175 {
176 auto newTs = clockFilter_->ToPrimaryTraceTime(TS_CLOCK_REALTIME, ts);
177 auto newEndTs = clockFilter_->ToPrimaryTraceTime(TS_CLOCK_REALTIME, endTs);
178 UpdatePluginTimeRange(TS_CLOCK_BOOTTIME, ts, newTs);
179 UpdatePluginTimeRange(TS_CLOCK_BOOTTIME, endTs, newEndTs);
180 traceDataCache_->GetSliceTableData()->AppendNewData(sliceId, newTs, newEndTs, value);
181 return 0;
182 }
183 } // namespace TraceStreamer
184 } // namespace SysTuning
185