106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License. 506f6ba60Sopenharmony_ci * You may obtain a copy of the License at 606f6ba60Sopenharmony_ci * 706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 806f6ba60Sopenharmony_ci * 906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and 1306f6ba60Sopenharmony_ci * limitations under the License. 1406f6ba60Sopenharmony_ci */ 1506f6ba60Sopenharmony_ci#include "sample_plugin.h" 1606f6ba60Sopenharmony_ci 1706f6ba60Sopenharmony_ci#include <ctime> 1806f6ba60Sopenharmony_ci 1906f6ba60Sopenharmony_ci#include "sample_plugin_result.pbencoder.h" 2006f6ba60Sopenharmony_ci#include "securec.h" 2106f6ba60Sopenharmony_ci 2206f6ba60Sopenharmony_cinamespace { 2306f6ba60Sopenharmony_ciusing namespace OHOS::Developtools::Profiler; 2406f6ba60Sopenharmony_ciconstexpr int MAX_INT = 10; 2506f6ba60Sopenharmony_ciconstexpr int MAX_DOUBLE = 100; 2606f6ba60Sopenharmony_ciconstexpr int CONST_NUMBER = 1024; 2706f6ba60Sopenharmony_ci} // namespace 2806f6ba60Sopenharmony_ci 2906f6ba60Sopenharmony_ciSamplePlugin::SamplePlugin() {} 3006f6ba60Sopenharmony_ci 3106f6ba60Sopenharmony_ciSamplePlugin::~SamplePlugin() {} 3206f6ba60Sopenharmony_ci 3306f6ba60Sopenharmony_ciuint64_t SamplePlugin::GetTimeMS() 3406f6ba60Sopenharmony_ci{ 3506f6ba60Sopenharmony_ci const int MS_PER_S = 1000; 3606f6ba60Sopenharmony_ci const int NS_PER_MS = 1000000; 3706f6ba60Sopenharmony_ci struct timespec ts; 3806f6ba60Sopenharmony_ci clock_gettime(CLOCK_BOOTTIME, &ts); 3906f6ba60Sopenharmony_ci return ts.tv_sec * MS_PER_S + ts.tv_nsec / NS_PER_MS; 4006f6ba60Sopenharmony_ci} 4106f6ba60Sopenharmony_ci 4206f6ba60Sopenharmony_ciint SamplePlugin::Start(const uint8_t* configData, uint32_t configSize) 4306f6ba60Sopenharmony_ci{ 4406f6ba60Sopenharmony_ci PROFILER_LOG_INFO(LOG_CORE, "%s:config data -->configSize=%d", __func__, configSize); 4506f6ba60Sopenharmony_ci CHECK_TRUE(configData != nullptr, -1, "SamplePlugin: param invalid!!!"); 4606f6ba60Sopenharmony_ci for (uint32_t i = 0; i < configSize; i++) { 4706f6ba60Sopenharmony_ci PROFILER_LOG_INFO(LOG_CORE, "%s:configData[%d] = 0x%02x", __func__, i, configData[i]); 4806f6ba60Sopenharmony_ci } 4906f6ba60Sopenharmony_ci 5006f6ba60Sopenharmony_ci // 反序列化 5106f6ba60Sopenharmony_ci CHECK_TRUE(protoConfig_.ParseFromArray(configData, configSize) > 0, -1, "%s:parseFromArray failed!", __func__); 5206f6ba60Sopenharmony_ci PROFILER_LOG_INFO(LOG_CORE, "%s:pid = %d", __func__, protoConfig_.pid()); 5306f6ba60Sopenharmony_ci // 插件准备工作 5406f6ba60Sopenharmony_ci 5506f6ba60Sopenharmony_ci return 0; 5606f6ba60Sopenharmony_ci} 5706f6ba60Sopenharmony_ci 5806f6ba60Sopenharmony_ciint SamplePlugin::ReportOptimize(RandomWriteCtx* randomWrite) 5906f6ba60Sopenharmony_ci{ 6006f6ba60Sopenharmony_ci ProtoEncoder::SampleData dataProto(randomWrite); 6106f6ba60Sopenharmony_ci 6206f6ba60Sopenharmony_ci // 回填数据 6306f6ba60Sopenharmony_ci int intData = CONST_NUMBER % MAX_INT; 6406f6ba60Sopenharmony_ci double doubleData = CONST_NUMBER % MAX_DOUBLE; 6506f6ba60Sopenharmony_ci dataProto.set_time_ms(GetTimeMS()); 6606f6ba60Sopenharmony_ci dataProto.set_int_data(intData); 6706f6ba60Sopenharmony_ci dataProto.set_double_data(doubleData); 6806f6ba60Sopenharmony_ci 6906f6ba60Sopenharmony_ci int msgSize = dataProto.Finish(); 7006f6ba60Sopenharmony_ci return msgSize; 7106f6ba60Sopenharmony_ci} 7206f6ba60Sopenharmony_ci 7306f6ba60Sopenharmony_ciint SamplePlugin::Report(uint8_t* data, uint32_t dataSize) 7406f6ba60Sopenharmony_ci{ 7506f6ba60Sopenharmony_ci SampleData dataProto; 7606f6ba60Sopenharmony_ci 7706f6ba60Sopenharmony_ci // 回填数据 7806f6ba60Sopenharmony_ci int intData = CONST_NUMBER % MAX_INT; 7906f6ba60Sopenharmony_ci double doubleData = CONST_NUMBER % MAX_DOUBLE; 8006f6ba60Sopenharmony_ci dataProto.set_time_ms(GetTimeMS()); 8106f6ba60Sopenharmony_ci dataProto.set_int_data(intData); 8206f6ba60Sopenharmony_ci dataProto.set_double_data(doubleData); 8306f6ba60Sopenharmony_ci 8406f6ba60Sopenharmony_ci uint32_t length = dataProto.ByteSizeLong(); 8506f6ba60Sopenharmony_ci CHECK_TRUE(length <= dataSize, -length, "%s:Report failed!", __func__); 8606f6ba60Sopenharmony_ci // 序列化 8706f6ba60Sopenharmony_ci if (dataProto.SerializeToArray(data, length) > 0) { 8806f6ba60Sopenharmony_ci return length; 8906f6ba60Sopenharmony_ci } 9006f6ba60Sopenharmony_ci return 0; 9106f6ba60Sopenharmony_ci} 9206f6ba60Sopenharmony_ci 9306f6ba60Sopenharmony_ciint SamplePlugin::Stop() 9406f6ba60Sopenharmony_ci{ 9506f6ba60Sopenharmony_ci PROFILER_LOG_INFO(LOG_CORE, "%s:stop success!", __func__); 9606f6ba60Sopenharmony_ci return 0; 9706f6ba60Sopenharmony_ci}