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 "command_poller.h" 1606f6ba60Sopenharmony_ci#include "buffer_writer.h" 1706f6ba60Sopenharmony_ci#include "plugin_manager.h" 1806f6ba60Sopenharmony_ci#include "socket_context.h" 1906f6ba60Sopenharmony_ci 2006f6ba60Sopenharmony_ci#include <fcntl.h> 2106f6ba60Sopenharmony_ci#include <unistd.h> 2206f6ba60Sopenharmony_ci 2306f6ba60Sopenharmony_cinamespace { 2406f6ba60Sopenharmony_ciconstexpr int SLEEP_TIME = 10; 2506f6ba60Sopenharmony_ci} 2606f6ba60Sopenharmony_ci 2706f6ba60Sopenharmony_ciCommandPoller::CommandPoller(const ManagerInterfacePtr& p) : requestIdAutoIncrease_(1), pluginManager_(p) {} 2806f6ba60Sopenharmony_ci 2906f6ba60Sopenharmony_ciCommandPoller::~CommandPoller() {} 3006f6ba60Sopenharmony_ci 3106f6ba60Sopenharmony_ciuint32_t CommandPoller::GetRequestId() 3206f6ba60Sopenharmony_ci{ 3306f6ba60Sopenharmony_ci return requestIdAutoIncrease_++; 3406f6ba60Sopenharmony_ci} 3506f6ba60Sopenharmony_ci 3606f6ba60Sopenharmony_cibool CommandPoller::OnConnect() 3706f6ba60Sopenharmony_ci{ 3806f6ba60Sopenharmony_ci return Connect(DEFAULT_UNIX_SOCKET_FULL_PATH); 3906f6ba60Sopenharmony_ci} 4006f6ba60Sopenharmony_ci 4106f6ba60Sopenharmony_cibool CommandPoller::OnCreateSessionCmd(const CreateSessionCmd& cmd, SocketContext& context) const 4206f6ba60Sopenharmony_ci{ 4306f6ba60Sopenharmony_ci PROFILER_LOG_DEBUG(LOG_CORE, "%s:proc", __func__); 4406f6ba60Sopenharmony_ci CHECK_TRUE(cmd.buffer_sizes().size() != 0 && cmd.plugin_configs().size() != 0, false, "%s:cmd invalid!", __func__); 4506f6ba60Sopenharmony_ci 4606f6ba60Sopenharmony_ci uint32_t bufferSize = cmd.buffer_sizes(0); 4706f6ba60Sopenharmony_ci ProfilerPluginConfig config = cmd.plugin_configs(0); 4806f6ba60Sopenharmony_ci std::vector<ProfilerPluginConfig> configVec; 4906f6ba60Sopenharmony_ci configVec.push_back(config); 5006f6ba60Sopenharmony_ci 5106f6ba60Sopenharmony_ci auto pluginManager = pluginManager_.lock(); // promote to shared_ptr 5206f6ba60Sopenharmony_ci CHECK_NOTNULL(pluginManager, false, "promote FAILED!"); 5306f6ba60Sopenharmony_ci 5406f6ba60Sopenharmony_ci CHECK_TRUE(pluginManager->LoadPlugin(config.name()), false, "%s:fail 1", __func__); 5506f6ba60Sopenharmony_ci 5606f6ba60Sopenharmony_ci int smbFd = -1; 5706f6ba60Sopenharmony_ci int eventFd = -1; 5806f6ba60Sopenharmony_ci if (bufferSize != 0) { 5906f6ba60Sopenharmony_ci PROFILER_LOG_DEBUG(LOG_CORE, "%s:bufferSize = %d", __func__, bufferSize); 6006f6ba60Sopenharmony_ci smbFd = context.ReceiveFileDiscriptor(); 6106f6ba60Sopenharmony_ci eventFd = context.ReceiveFileDiscriptor(); 6206f6ba60Sopenharmony_ci int flags = fcntl(eventFd, F_GETFL); 6306f6ba60Sopenharmony_ci PROFILER_LOG_DEBUG(LOG_CORE, "%s:smbFd = %d, eventFd = %d", __func__, smbFd, eventFd); 6406f6ba60Sopenharmony_ci PROFILER_LOG_DEBUG(LOG_CORE, "%s:eventFd flags = %X", __func__, flags); 6506f6ba60Sopenharmony_ci } 6606f6ba60Sopenharmony_ci CHECK_TRUE(pluginManager->CreateWriter(config.name(), bufferSize, smbFd, eventFd, config.is_protobuf_serialize()), 6706f6ba60Sopenharmony_ci false, "%s:createWriter failed!", __func__); 6806f6ba60Sopenharmony_ci CHECK_TRUE(pluginManager->CreatePluginSession(configVec), false, 6906f6ba60Sopenharmony_ci "%s:createPluginSession failed!", __func__); 7006f6ba60Sopenharmony_ci PROFILER_LOG_DEBUG(LOG_CORE, "%s:ok", __func__); 7106f6ba60Sopenharmony_ci return true; 7206f6ba60Sopenharmony_ci} 7306f6ba60Sopenharmony_ci 7406f6ba60Sopenharmony_cibool CommandPoller::OnDestroySessionCmd(const DestroySessionCmd& cmd) const 7506f6ba60Sopenharmony_ci{ 7606f6ba60Sopenharmony_ci PROFILER_LOG_DEBUG(LOG_CORE, "%s:proc", __func__); 7706f6ba60Sopenharmony_ci CHECK_TRUE(cmd.plugin_ids().size() != 0, false, "%s:cmd invalid!", __func__); 7806f6ba60Sopenharmony_ci uint32_t pluginId = cmd.plugin_ids(0); 7906f6ba60Sopenharmony_ci std::vector<uint32_t> pluginIdVec; 8006f6ba60Sopenharmony_ci pluginIdVec.push_back(pluginId); 8106f6ba60Sopenharmony_ci 8206f6ba60Sopenharmony_ci auto pluginManager = pluginManager_.lock(); // promote to shared_ptr 8306f6ba60Sopenharmony_ci CHECK_NOTNULL(pluginManager, false, "promote FAILED!"); 8406f6ba60Sopenharmony_ci CHECK_TRUE(pluginManager->DestroyPluginSession(pluginIdVec), false, 8506f6ba60Sopenharmony_ci "%s:destroyPluginSession failed!", __func__); 8606f6ba60Sopenharmony_ci CHECK_TRUE(pluginManager->ResetWriter(pluginId), false, "%s:resetWriter failed!", __func__); 8706f6ba60Sopenharmony_ci CHECK_TRUE(pluginManager->UnloadPlugin(pluginId), false, "%s:unloadPlugin failed!", __func__); 8806f6ba60Sopenharmony_ci PROFILER_LOG_DEBUG(LOG_CORE, "%s:ok", __func__); 8906f6ba60Sopenharmony_ci return true; 9006f6ba60Sopenharmony_ci} 9106f6ba60Sopenharmony_ci 9206f6ba60Sopenharmony_cibool CommandPoller::OnStartSessionCmd(const StartSessionCmd& cmd, PluginResult& result) const 9306f6ba60Sopenharmony_ci{ 9406f6ba60Sopenharmony_ci PROFILER_LOG_DEBUG(LOG_CORE, "%s:proc", __func__); 9506f6ba60Sopenharmony_ci CHECK_TRUE(cmd.plugin_ids().size() != 0 && cmd.plugin_configs().size() != 0, false, 9606f6ba60Sopenharmony_ci "%s:cmd invalid!", __func__); 9706f6ba60Sopenharmony_ci std::vector<uint32_t> pluginIds; 9806f6ba60Sopenharmony_ci pluginIds.push_back(cmd.plugin_ids(0)); 9906f6ba60Sopenharmony_ci std::vector<ProfilerPluginConfig> configVec; 10006f6ba60Sopenharmony_ci configVec.push_back(cmd.plugin_configs(0)); 10106f6ba60Sopenharmony_ci 10206f6ba60Sopenharmony_ci auto pluginManager = pluginManager_.lock(); // promote to shared_ptr 10306f6ba60Sopenharmony_ci CHECK_NOTNULL(pluginManager, false, "promote FAILED!"); 10406f6ba60Sopenharmony_ci CHECK_TRUE(pluginManager->StartPluginSession(pluginIds, configVec, result), false, 10506f6ba60Sopenharmony_ci "%s:start Session failed!", __func__); 10606f6ba60Sopenharmony_ci PROFILER_LOG_DEBUG(LOG_CORE, "%s:OK", __func__); 10706f6ba60Sopenharmony_ci return true; 10806f6ba60Sopenharmony_ci} 10906f6ba60Sopenharmony_ci 11006f6ba60Sopenharmony_cibool CommandPoller::OnStopSessionCmd(const StopSessionCmd& cmd) const 11106f6ba60Sopenharmony_ci{ 11206f6ba60Sopenharmony_ci PROFILER_LOG_DEBUG(LOG_CORE, "%s:proc", __func__); 11306f6ba60Sopenharmony_ci CHECK_TRUE(cmd.plugin_ids().size() != 0, false, "%s:cmd invalid!", __func__); 11406f6ba60Sopenharmony_ci std::vector<uint32_t> pluginIds; 11506f6ba60Sopenharmony_ci pluginIds.push_back(cmd.plugin_ids(0)); 11606f6ba60Sopenharmony_ci 11706f6ba60Sopenharmony_ci auto pluginManager = pluginManager_.lock(); // promote to shared_ptr 11806f6ba60Sopenharmony_ci CHECK_NOTNULL(pluginManager, false, "promote FAILED!"); 11906f6ba60Sopenharmony_ci CHECK_TRUE(pluginManager->StopPluginSession(pluginIds), false, "%s:stop Session failed!", __func__); 12006f6ba60Sopenharmony_ci PROFILER_LOG_DEBUG(LOG_CORE, "%s:ok", __func__); 12106f6ba60Sopenharmony_ci return true; 12206f6ba60Sopenharmony_ci} 12306f6ba60Sopenharmony_ci 12406f6ba60Sopenharmony_cibool CommandPoller::OnReportBasicDataCmd(const RefreshSessionCmd& cmd) const 12506f6ba60Sopenharmony_ci{ 12606f6ba60Sopenharmony_ci PROFILER_LOG_DEBUG(LOG_CORE, "%s:proc", __func__); 12706f6ba60Sopenharmony_ci CHECK_TRUE(cmd.plugin_ids().size() != 0, false, "%s:cmd invalid!", __func__); 12806f6ba60Sopenharmony_ci std::vector<uint32_t> pluginIds; 12906f6ba60Sopenharmony_ci pluginIds.push_back(cmd.plugin_ids(0)); 13006f6ba60Sopenharmony_ci 13106f6ba60Sopenharmony_ci auto pluginManager = pluginManager_.lock(); // promote to shared_ptr 13206f6ba60Sopenharmony_ci CHECK_NOTNULL(pluginManager, false, "promote FAILED!"); 13306f6ba60Sopenharmony_ci CHECK_TRUE(pluginManager->ReportPluginBasicData(pluginIds), false, "%s:report basic data failed!", __func__); 13406f6ba60Sopenharmony_ci PROFILER_LOG_INFO(LOG_CORE, "%s:ok", __func__); 13506f6ba60Sopenharmony_ci return true; 13606f6ba60Sopenharmony_ci} 13706f6ba60Sopenharmony_ci 13806f6ba60Sopenharmony_cibool CommandPoller::OnGetCommandResponse(SocketContext& context, ::GetCommandResponse& response) 13906f6ba60Sopenharmony_ci{ 14006f6ba60Sopenharmony_ci PROFILER_LOG_DEBUG(LOG_CORE, "%s:proc", __func__); 14106f6ba60Sopenharmony_ci std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME)); 14206f6ba60Sopenharmony_ci NotifyResultRequest nrr; 14306f6ba60Sopenharmony_ci nrr.set_request_id(1); 14406f6ba60Sopenharmony_ci nrr.set_command_id(response.command_id()); 14506f6ba60Sopenharmony_ci PluginResult* pr = nrr.add_result(); 14606f6ba60Sopenharmony_ci ProfilerPluginState* status = pr->mutable_status(); 14706f6ba60Sopenharmony_ci 14806f6ba60Sopenharmony_ci if (response.has_create_session_cmd()) { 14906f6ba60Sopenharmony_ci if (OnCreateSessionCmd(response.create_session_cmd(), context)) { 15006f6ba60Sopenharmony_ci status->set_state(ProfilerPluginState::LOADED); 15106f6ba60Sopenharmony_ci } else { 15206f6ba60Sopenharmony_ci status->set_state(ProfilerPluginState::REGISTERED); 15306f6ba60Sopenharmony_ci } 15406f6ba60Sopenharmony_ci } else if (response.has_destroy_session_cmd()) { 15506f6ba60Sopenharmony_ci if (OnDestroySessionCmd(response.destroy_session_cmd())) { 15606f6ba60Sopenharmony_ci status->set_state(ProfilerPluginState::REGISTERED); 15706f6ba60Sopenharmony_ci } else { 15806f6ba60Sopenharmony_ci status->set_state(ProfilerPluginState::LOADED); 15906f6ba60Sopenharmony_ci } 16006f6ba60Sopenharmony_ci } else if (response.has_start_session_cmd()) { 16106f6ba60Sopenharmony_ci if (OnStartSessionCmd(response.start_session_cmd(), *pr)) { 16206f6ba60Sopenharmony_ci status->set_state(ProfilerPluginState::IN_SESSION); 16306f6ba60Sopenharmony_ci } else { 16406f6ba60Sopenharmony_ci status->set_state(ProfilerPluginState::LOADED); 16506f6ba60Sopenharmony_ci } 16606f6ba60Sopenharmony_ci } else if (response.has_stop_session_cmd()) { 16706f6ba60Sopenharmony_ci if (OnStopSessionCmd(response.stop_session_cmd())) { 16806f6ba60Sopenharmony_ci status->set_state(ProfilerPluginState::LOADED); 16906f6ba60Sopenharmony_ci } else { 17006f6ba60Sopenharmony_ci status->set_state(ProfilerPluginState::IN_SESSION); 17106f6ba60Sopenharmony_ci } 17206f6ba60Sopenharmony_ci } else if (response.has_refresh_session_cmd()) { 17306f6ba60Sopenharmony_ci OnReportBasicDataCmd(response.refresh_session_cmd()); 17406f6ba60Sopenharmony_ci status->set_state(ProfilerPluginState::IN_SESSION); 17506f6ba60Sopenharmony_ci } else { 17606f6ba60Sopenharmony_ci PROFILER_LOG_DEBUG(LOG_CORE, "%s:command Response failed!", __func__); 17706f6ba60Sopenharmony_ci return false; 17806f6ba60Sopenharmony_ci } 17906f6ba60Sopenharmony_ci PROFILER_LOG_DEBUG(LOG_CORE, "%s:ok id = %d", __func__, nrr.command_id()); 18006f6ba60Sopenharmony_ci NotifyResult(nrr); 18106f6ba60Sopenharmony_ci return true; 18206f6ba60Sopenharmony_ci} 183