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