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 #include "sdk_plugin_data_parser.h"
16 #include <string>
17 #include "sdk/ts_sdk_api.h"
18
19 namespace SysTuning {
20 namespace TraceStreamer {
21 extern "C" {
SDKPluginIinitTableName()22 void SDKPluginIinitTableName()
23 {
24 SDKSetTableName("counter_table", "gpu_counter_object", "slice_table", "slice_object_table");
25 }
SDKPluginDataParser(const uint8_t * data,int32_t len)26 int32_t SDKPluginDataParser(const uint8_t *data, int32_t len)
27 {
28 std::unique_ptr<uint8_t[]> buf = std::make_unique<uint8_t[]>(len);
29 std::copy(data, data + len, buf.get());
30 MockDataArr mockDataArr;
31 mockDataArr.ParseFromArray(buf.get(), len);
32 int32_t size = mockDataArr.mockdata_size();
33 if (size > 1) {
34 for (auto m = 0; m < size; m++) {
35 auto mockData = mockDataArr.mockdata().at(m);
36 SDKPluginParser(data, len, mockData);
37 }
38 } else {
39 MockData mockData;
40 mockData.ParseFromArray(buf.get(), len);
41 SDKPluginParser(data, len, mockData);
42 }
43 return 0;
44 }
45
SDKPluginParser(const uint8_t * data,int32_t len,MockData mockData)46 int32_t SDKPluginParser(const uint8_t *data, int32_t len, MockData mockData)
47 {
48 // parser counterObject
49 for (auto i = 0; i < mockData.counterobj_size(); i++) {
50 int32_t counterId = mockData.counterobj(i).id();
51 std::string counterName = mockData.counterobj(i).name();
52 SDKAppendCounterObject(counterId, counterName.c_str());
53 }
54
55 // parsercounterInfo
56 for (auto i = 0; i < mockData.counterinfo_size(); i++) {
57 auto counterInfo = mockData.counterinfo(i);
58 SDKAppendCounter(counterInfo.key(), counterInfo.ts(), counterInfo.value());
59 }
60
61 // parserSliceObj
62 for (auto i = 0; i < mockData.sliceobj_size(); i++) {
63 int32_t sliceId = mockData.sliceobj(i).id();
64 std::string sliceName = mockData.sliceobj(i).name();
65 SDKAppendSliceObject(sliceId, sliceName.c_str());
66 }
67
68 // parserSliceInfo
69 for (auto i = 0; i < mockData.sliceinfo_size(); i++) {
70 int32_t sliceKey = mockData.sliceinfo(i).id();
71 int32_t sliceValue = mockData.sliceinfo(i).value();
72 uint64_t startTime = mockData.sliceinfo(i).start_time();
73 uint64_t endTime = mockData.sliceinfo(i).end_time();
74 SDKAppendSlice(sliceKey, startTime, endTime, sliceValue);
75 }
76 return 0;
77 }
78 }
79 } // namespace TraceStreamer
80 } // namespace SysTuning
81