• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. 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 "sample_plugin.h"
16 
17 #include <ctime>
18 
19 #include "sample_plugin_result.pbencoder.h"
20 #include "securec.h"
21 
22 namespace {
23 using namespace OHOS::Developtools::Profiler;
24 constexpr int MAX_INT = 10;
25 constexpr int MAX_DOUBLE = 100;
26 constexpr int CONST_NUMBER = 1024;
27 } // namespace
28 
SamplePlugin()29 SamplePlugin::SamplePlugin() {}
30 
~SamplePlugin()31 SamplePlugin::~SamplePlugin() {}
32 
GetTimeMS()33 uint64_t SamplePlugin::GetTimeMS()
34 {
35     const int MS_PER_S = 1000;
36     const int NS_PER_MS = 1000000;
37     struct timespec ts;
38     clock_gettime(CLOCK_BOOTTIME, &ts);
39     return ts.tv_sec * MS_PER_S + ts.tv_nsec / NS_PER_MS;
40 }
41 
Start(const uint8_t * configData,uint32_t configSize)42 int SamplePlugin::Start(const uint8_t* configData, uint32_t configSize)
43 {
44     PROFILER_LOG_INFO(LOG_CORE, "%s:config data -->configSize=%d", __func__, configSize);
45     CHECK_TRUE(configData != nullptr, -1, "SamplePlugin: param invalid!!!");
46     for (uint32_t i = 0; i < configSize; i++) {
47         PROFILER_LOG_INFO(LOG_CORE, "%s:configData[%d] = 0x%02x", __func__, i, configData[i]);
48     }
49 
50     // 反序列化
51     CHECK_TRUE(protoConfig_.ParseFromArray(configData, configSize) > 0, -1, "%s:parseFromArray failed!", __func__);
52     PROFILER_LOG_INFO(LOG_CORE, "%s:pid = %d", __func__, protoConfig_.pid());
53     // 插件准备工作
54 
55     return 0;
56 }
57 
ReportOptimize(RandomWriteCtx * randomWrite)58 int SamplePlugin::ReportOptimize(RandomWriteCtx* randomWrite)
59 {
60     ProtoEncoder::SampleData dataProto(randomWrite);
61 
62     // 回填数据
63     int intData = CONST_NUMBER % MAX_INT;
64     double doubleData = CONST_NUMBER % MAX_DOUBLE;
65     dataProto.set_time_ms(GetTimeMS());
66     dataProto.set_int_data(intData);
67     dataProto.set_double_data(doubleData);
68 
69     int msgSize = dataProto.Finish();
70     return msgSize;
71 }
72 
Report(uint8_t * data,uint32_t dataSize)73 int SamplePlugin::Report(uint8_t* data, uint32_t dataSize)
74 {
75     SampleData dataProto;
76 
77     // 回填数据
78     int intData = CONST_NUMBER % MAX_INT;
79     double doubleData = CONST_NUMBER % MAX_DOUBLE;
80     dataProto.set_time_ms(GetTimeMS());
81     dataProto.set_int_data(intData);
82     dataProto.set_double_data(doubleData);
83 
84     uint32_t length = dataProto.ByteSizeLong();
85     CHECK_TRUE(length <= dataSize, -length, "%s:Report failed!", __func__);
86     // 序列化
87     if (dataProto.SerializeToArray(data, length) > 0) {
88         return length;
89     }
90     return 0;
91 }
92 
Stop()93 int SamplePlugin::Stop()
94 {
95     PROFILER_LOG_INFO(LOG_CORE, "%s:stop success!", __func__);
96     return 0;
97 }