diff --git a/smartperf_device/device_command/cmds/include/client_control.h b/smartperf_device/device_command/cmds/include/client_control.h new file mode 100644 index 0000000000000000000000000000000000000000..d76df53e6fc8a8c9ad7d3b05bd00c68b6d97d8e0 --- /dev/null +++ b/smartperf_device/device_command/cmds/include/client_control.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef CLIENTCONTROL_H +#define CLIENTCONTROL_H +#include +#include +namespace OHOS { +namespace SmartPerf { +class ClientControl { +public: + int SocketStart(const std::string &args); + int SocketStop(); + int InitSocket(); + void StartSPDaemon() const; + int CloseSocket(); + +private: + int clientSocket = 0; + struct sockaddr_in serverAddress; + static const int arraySize = 1024; + char buffer[arraySize] = {0}; + int protNumber = 8284; + std::string message = "init:::-SESSIONID 1 -INTERVAL 1000 "; + const char *message1 = "start:::"; + const char *message2 = "stop::"; + int numBuff = 1024; +}; +} +} +#endif \ No newline at end of file diff --git a/smartperf_device/device_command/cmds/include/control_call_cmd.h b/smartperf_device/device_command/cmds/include/control_call_cmd.h new file mode 100644 index 0000000000000000000000000000000000000000..f2583cc2ad66aeb462e11a6f49ba094c84662bb3 --- /dev/null +++ b/smartperf_device/device_command/cmds/include/control_call_cmd.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef CONTROL_CALL_CMD_H +#define CONTROL_CALL_CMD_H +#include +#include +#include +#include +#include +namespace OHOS { +namespace SmartPerf { +class ControlCallCmd { +public: + double CompleteTime(); + double ResponseTime(); + std::string GetResult(const std::vector& v); + std::string GetFrame(); + std::string GetAppStartTime() const; + std::string SlideList(); + std::string TimeDelay(); + void IsohTest(const std::vector& v); + +private: + bool isOhTest = false; + std::string result = ""; + int ohType = 4; + int typeName = 2; + double time = 0.0; + double noNameType = -1.0; + std::ostringstream stream; + int two = 2; +}; +} +} +#endif // SMARTPERF_COMMAND_H \ No newline at end of file diff --git a/smartperf_device/device_command/cmds/include/editor_command.h b/smartperf_device/device_command/cmds/include/editor_command.h new file mode 100644 index 0000000000000000000000000000000000000000..7f4858718fc306d239673443acffc69a671afbd9 --- /dev/null +++ b/smartperf_device/device_command/cmds/include/editor_command.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef EDITOR_COMMAND_H +#define EDITOR_COMMAND_H +namespace OHOS { +namespace SmartPerf { +class EditorCommand { +public: + EditorCommand(int argc, const std::vector &v); + ~EditorCommand() {}; +}; +} +} +#endif // SMARTPERF_COMMAND_H \ No newline at end of file diff --git a/smartperf_device/device_command/cmds/include/smartperf_command.h b/smartperf_device/device_command/cmds/include/smartperf_command.h new file mode 100644 index 0000000000000000000000000000000000000000..e36069770bcc6e62ad4814310e68cb4ae3e71a81 --- /dev/null +++ b/smartperf_device/device_command/cmds/include/smartperf_command.h @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SMARTPERF_COMMAND_H +#define SMARTPERF_COMMAND_H + +#include +#include +#include "common.h" +#include "sp_utils.h" +#include "GpuCounter.h" +#include "GameEvent.h" +#include "task_manager.h" + +namespace OHOS { +namespace SmartPerf { +class SmartPerfCommand { +public: + const std::string smartPerfExeName = "SP_daemon"; + const std::string smartPerfMsgErr = "error input!\n use command '--help' get more information\n"; + const std::string smartPerfMsg = "OpenHarmony performance testing tool SmartPerf command-line version\n" + "Usage: SP_daemon [options] [arguments]\n\n" + "options:\n" + " -N set the collection times(default value is 0) range[1,2147483647], for example: -N 10 \n" + " -PKG set package name, must add, for example: -PKG ohos.samples.ecg \n" + " -PID set process pid, must add, for example: -PID 3568 \n" + " -threads get threads, must add -PID or -PKG for example: \n" + "\t\t -threads -PID 3568 or -threads -PKG ohos.samples.ecg \n" + " -fds get file descriptor, must add -PID or -PKG for example: \n" + "\t\t -fds -PID 3568 or -fds -PKG ohos.samples.ecg \n" + " -c get device CPU frequency and CPU usage, process CPU usage and CPU load .. \n" + " -ci get cpu instructions and cycles \n" + " -g get device GPU frequency and GPU load \n" + " -f get app refresh fps(frames per second) and fps jitters and refreshrate \n" + " -profilerfps get refresh fps and timestamp \n" + " -sections set collection time period(using with profilerfps)\n" + " -t get remaining battery power and temperature.. \n" + " -p get battery power consumption and voltage(Not supported by some devices) \n" + " -print start mode print log \n" + " -r get process memory and total memory \n" + " -snapshot get screen capture\n" + " -net get uplink and downlink traffic\n" + " -start collection start command \n" + " -stop collection stop command \n" + " -VIEW set layler, for example: -VIEW DisplayNode \n" + " -OUT set csv output path.\n" + " -d get device DDR information \n" + " -screen get screen resolution \n" + " -deviceinfo get device information \n" + " -server start a process to listen to the socket message of the start and stop commands \n" + " -clear clear the process ID \n" + " -ohtestfps used by the vilidator to obtain the fps, the collection times can be set \n" + " -editorServer start a process to listen to the socket message of the editor \n" + " -recordcapacity get the battery level difference \n" + " --version get version \n" + " --help get help \n" + " -editor scenario-based collection identifier, parameter configuration items can be added later \n" + " responseTime get the page response delay after an application is operated \n" + " completeTime get the page completion delay after an application is operated \n" + " fpsohtest used by the vilidator to obtain the fps \n" + "example1:\n" + "SP_daemon -N 20 -c -g -t -p -r -net -snapshot -d \n" + "SP_daemon -N 20 -PKG ohos.samples.ecg -c -g -t -p -f -r -net -snapshot -d \n" + "SP_daemon -start -c \n" + "SP_daemon -stop \n" + "example2: These parameters need to be used separately \n" + "SP_daemon -screen \n" + "SP_daemon -deviceinfo \n" + "SP_daemon -server \n" + "SP_daemon -clear \n" + "SP_daemon -ohtestfps 10 \n" + "SP_daemon -editorServer \n" + "SP_daemon -recordcapacity \n" + "example3: These parameters need to be used separately \n" + "SP_daemon -editor responseTime ohos.samples.ecg app name \n" + "SP_daemon -editor completeTime ohos.samples.ecg app name \n" + "SP_daemon -editor fpsohtest \n"; + + const size_t oneParam = 1; + const size_t twoParam = 2; + const size_t threeParamMore = 3; + const size_t serverCommandLength = 14; // -deviceServer: 与 -editorServer: 的长度 + explicit SmartPerfCommand(std::vector& argv); + ~SmartPerfCommand() {}; + static void InitSomething(); + std::string ExecCommand(); + void HelpCommand(CommandHelp type, const std::string& token) const; + void CreateSocketThread() const; + void DeviceServer(int isNeedDaemon) const; + TaskManager taskMgr_; +}; +} +} +#endif // SMARTPERF_COMMAND_H \ No newline at end of file diff --git a/smartperf_device/device_command/cmds/src/client_control.cpp b/smartperf_device/device_command/cmds/src/client_control.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11af5dce3284cbdee29832b2a67cd7a86bbda485 --- /dev/null +++ b/smartperf_device/device_command/cmds/src/client_control.cpp @@ -0,0 +1,119 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include +#include +#include "include/client_control.h" +#include "include/sp_utils.h" +#include "include/startup_delay.h" +#include "include/sp_log.h" +#include "include/common.h" +namespace OHOS { +namespace SmartPerf { +int ClientControl::SocketStart(const std::string &args) +{ + std::string messageInit = message + args; + int resultId = OHOS::SmartPerf::ClientControl::InitSocket(); + if (resultId == 1) { + LOGE("ClientControl::InitSocket() error(%d)", resultId); + return resultId; + } + send(clientSocket, messageInit.c_str(), strlen(messageInit.c_str()), 0); + LOGD("start-stop messageInit : %s", messageInit.c_str()); + read(clientSocket, buffer, numBuff); + LOGD("start-stop recv : %s", buffer); + send(clientSocket, message1, strlen(message1), 0); + LOGD("start-stop send : %s", message1); + read(clientSocket, buffer, numBuff); + LOGD("start-stop recv : %s", buffer); + char dest[arraySize] = {0}; + size_t i; + for (i = 0; buffer[i] != '\0' && i < arraySize - 1; i++) { + dest[i] = buffer[i]; + } + dest[i] = '\0'; + if (strcmp(dest, "start::True") == 0) { + std::cout << "SP_daemon Collection begins" << std::endl; + OHOS::SmartPerf::ClientControl::CloseSocket(); + } else { + std::cout << "SP_daemon Collection begins failed" << std::endl; + OHOS::SmartPerf::StartUpDelay sd; + sd.GetSpClear(false); + } + LOGD("ClientControl::SocketStart() ok"); + return 0; +} +int ClientControl::SocketStop() +{ + OHOS::SmartPerf::ClientControl::InitSocket(); + send(clientSocket, message2, strlen(message2), 0); + read(clientSocket, buffer, numBuff); + char dest[arraySize] = {0}; + size_t i; + for (i = 0; buffer[i] != '\0' && i < arraySize - 1; i++) { + dest[i] = buffer[i]; + } + dest[i] = '\0'; + if (strcmp(dest, "stop::True") == 0) { + std::cout << "SP_daemon Collection ended" << std::endl; + std::cout << "Output Path: data/local/tmp/smartperf/1/t_index_info.csv" << std::endl; + OHOS::SmartPerf::StartUpDelay sd; + OHOS::SmartPerf::ClientControl::CloseSocket(); + sd.GetSpClear(false); + } else { + std::cout << "SP_daemon Collection ended failed" << std::endl; + } + LOGD("ClientControl::SocketStop() ok"); + return 0; +} +int ClientControl::InitSocket() +{ + clientSocket = socket(AF_INET, SOCK_STREAM, 0); + if (clientSocket == -1) { + LOGE("Faild to create socket"); + return -1; + } + serverAddress.sin_family = AF_INET; + serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); + serverAddress.sin_port = htons(protNumber); + if (connect(clientSocket, reinterpret_cast(&serverAddress), sizeof(serverAddress)) < 0) { + OHOS::SmartPerf::StartUpDelay sd; + sd.GetSpClear(false); + LOGE("Failed to connect to server"); + return 1; + } + LOGD("ClientControl::SocketInit() ok"); + return 0; +} +void ClientControl::StartSPDaemon() const +{ + std::string result = ""; + std::string server = CMD_COMMAND_MAP.at(CmdCommand::SERVER); + SPUtils::LoadCmd(server, result); + sleep(1); +} +int ClientControl::CloseSocket() +{ + shutdown(clientSocket, SHUT_RD); + close(clientSocket); + clientSocket = -1; + LOGD("ClientControl::CloseSocket() ok"); + return 0; +} +} +} \ No newline at end of file diff --git a/smartperf_device/device_command/cmds/src/control_call_cmd.cpp b/smartperf_device/device_command/cmds/src/control_call_cmd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cdf194ba0b6f05b06ad7671e8e0f37119b528864 --- /dev/null +++ b/smartperf_device/device_command/cmds/src/control_call_cmd.cpp @@ -0,0 +1,271 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "unistd.h" +#include +#include +#include +#include +#include +#include +#include +#include "include/control_call_cmd.h" +#include "include/startup_delay.h" +#include "include/sp_utils.h" +#include "include/parse_click_complete_trace.h" +#include "include/parse_click_response_trace.h" +#include "include/parse_radar.h" +#include "include/parse_slide_fps_trace.h" +#include "include/sp_log.h" +#include "include/stalling_rate_trace.h" +#include "common.h" + +namespace OHOS { +namespace SmartPerf { +std::string ControlCallCmd::GetResult(const std::vector& v) +{ + IsohTest(v); + if (v[typeName] == "responseTime") { + time = SmartPerf::ControlCallCmd::ResponseTime(); + } else if (v[typeName] == "completeTime") { + time = SmartPerf::ControlCallCmd::CompleteTime(); + } else if (v[typeName] == "fpsohtest") { + std::string ohTestFps = CMD_COMMAND_MAP.at(CmdCommand::OHTESTFPS); + SPUtils::LoadCmd(ohTestFps, result); + } else if (v[typeName] == "frameLoss") { + result = SmartPerf::ControlCallCmd::GetFrame(); + } else if (v[typeName] == "appStartTime") { + result = ControlCallCmd::GetAppStartTime(); + } else if (v[typeName] == "slideList") { + result = ControlCallCmd::SlideList(); + } else if (v[typeName] == "timeDelay") { + result = ControlCallCmd::TimeDelay(); + } + if (time == noNameType) { + std::cout << "Startup error, unknown application or application not responding" << std::endl; + } else { + if (time != 0) { + stream << time; + result = "time:" + stream.str() + "ms"; + } + std::cout << result << std::endl; + } + return result; +} +std::string ControlCallCmd::TimeDelay() +{ + OHOS::SmartPerf::ParseClickResponseTrace pcrt; + OHOS::SmartPerf::StartUpDelay sd; + std::string cmdResult; + OHOS::SmartPerf::ParseRadar radar; + OHOS::SmartPerf::StallingRateTrace srt; + std::string rmTrace = CMD_COMMAND_MAP.at(CmdCommand::RM_FILE) + std::string("sp_trace_") + "delay" + ".ftrace"; + SPUtils::LoadCmd(rmTrace, cmdResult); + std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "delay" + ".ftrace"; + std::thread thGetTrace = std::thread([&sd, traceName]() { sd.GetTrace(traceName); }); + std::thread thGetHisysId = std::thread([&sd]() { sd.GetHisysIdAndKill(); }); + std::promise promResponse; + std::promise promComplete; + std::promise promRadarFrame; + std::promise promResponseMoved = std::move(promResponse); + std::promise promCompleteMoved = std::move(promComplete); + std::promise promRadarFrameMoved = std::move(promRadarFrame); + std::future futureResponse = promResponseMoved.get_future(); + std::thread([promiseResponse = std::move(promResponseMoved)]() mutable { + promiseResponse.set_value(SPUtils::GetRadarResponse()); + }).detach(); + std::future futureComplete = promCompleteMoved.get_future(); + std::thread([promiseComplete = std::move(promCompleteMoved)]() mutable { + promiseComplete.set_value(SPUtils::GetRadarComplete()); + }).detach(); + std::future futureRadarFrame = promRadarFrameMoved.get_future(); + std::thread([promiseRadarFrame = std::move(promRadarFrameMoved)]() mutable { + promiseRadarFrame.set_value(SPUtils::GetRadarFrame()); + }).detach(); + std::string responseStr = futureResponse.get(); + std::string completeStr = futureComplete.get(); + std::string radarFrameStr = futureRadarFrame.get(); + thGetTrace.join(); + thGetHisysId.join(); + double strResponseTime = radar.ParseRadarResponse(responseStr); + stream << strResponseTime; + double strCompleteTime = radar.ParseRadarComplete(completeStr); + std::ostringstream streamComplete; + streamComplete << strCompleteTime; + std::string maxFrame = radar.ParseRadarMaxFrame(radarFrameStr); + std::string resultTime = "ResponseTime:" + stream.str() + "ms\n" + "CompleteTime:" + streamComplete.str() + "ms\n"; + double rateResult = srt.StallingRateResult(traceName); + std::ostringstream ss; + ss << std::fixed << std::setprecision(two) << rateResult; + return resultTime + "HitchTimeRate:" + ss.str() + "ms/s \n" + maxFrame; +} +std::string ControlCallCmd::SlideList() +{ + OHOS::SmartPerf::ParseClickResponseTrace pcrt; + OHOS::SmartPerf::StartUpDelay sd; + OHOS::SmartPerf::ParseSlideFpsTrace slideFpsTrace; + std::string cmdResult; + OHOS::SmartPerf::ParseRadar radar; + OHOS::SmartPerf::StallingRateTrace srt; + std::string resultStream = ""; + std::string rmTrace = CMD_COMMAND_MAP.at(CmdCommand::RM_FILE) + std::string("sp_trace_") + "fps" + ".ftrace"; + SPUtils::LoadCmd(rmTrace, cmdResult); + std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "fps" + ".ftrace"; + if (isOhTest) { + std::thread thGetTrace = std::thread([&sd, traceName]() { sd.GetTrace(traceName); }); + thGetTrace.join(); + time = pcrt.ParseResponseTrace(traceName); + } else { + std::thread thGetTrace = std::thread([&sd, traceName]() { sd.GetTrace(traceName); }); + std::thread thGetHisysId = std::thread([&sd]() { sd.GetHisysIdAndKill(); }); + std::promise promResponse; + std::promise promRadarFrame; + std::promise promResponseMoved = std::move(promResponse); + std::promise promRadarFrameMoved = std::move(promRadarFrame); + std::future futureResponse = promResponseMoved.get_future(); + std::thread([promiseResponse = std::move(promResponseMoved)]() mutable { + promiseResponse.set_value(SPUtils::GetRadarResponse()); + }).detach(); + std::future futureRadarFrame = promRadarFrameMoved.get_future(); + std::thread([promiseRadarFrame = std::move(promRadarFrameMoved)]() mutable { + promiseRadarFrame.set_value(SPUtils::GetRadarFrame()); + }).detach(); + std::string responseStr = futureResponse.get(); + std::string radarFrameStr = futureRadarFrame.get(); + thGetTrace.join(); + thGetHisysId.join(); + double responseTime = radar.ParseRadarResponse(responseStr); + stream << responseTime; + std::string maxFrame = radar.ParseRadarMaxFrame(radarFrameStr); + std::string responseSlide = "ResponseTime:" + stream.str() + "ms\n"; + double sFps = slideFpsTrace.ParseSlideFpsTraceNoh(traceName); + std::ostringstream streamFps; + streamFps << sFps; + double stallingRateResult = srt.StallingRateResult(traceName); + std::ostringstream ss; + ss << std::fixed << std::setprecision(two) << stallingRateResult; + std::string ssResult = ss.str(); + std::string hitchTimeRate = "HitchTimeRate:" + ssResult + "ms/s \n"; + resultStream = "FPS:" + streamFps.str() + "fps\n" + responseSlide + hitchTimeRate + maxFrame; + } + return resultStream; +} +std::string ControlCallCmd::GetFrame() +{ + OHOS::SmartPerf::StartUpDelay sd; + std::string cmdResult; + OHOS::SmartPerf::ParseRadar radar; + std::string rmTrace = CMD_COMMAND_MAP.at(CmdCommand::RM_FILE) + std::string("sp_trace_") + "frame" + ".ftrace"; + SPUtils::LoadCmd(rmTrace, cmdResult); + std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "frame" + ".ftrace"; + std::thread thGetTrace = std::thread([&sd, traceName]() { sd.GetTrace(traceName); }); + std::thread thGetHisysId = std::thread([&sd]() { sd.GetHisysIdAndKill(); }); + std::string str = SPUtils::GetRadarFrame(); + thGetTrace.join(); + thGetHisysId.join(); + std::string reslut = radar.ParseRadarFrame(str); + return result; +} +double ControlCallCmd::ResponseTime() +{ + OHOS::SmartPerf::ParseClickResponseTrace pcrt; + OHOS::SmartPerf::StartUpDelay sd; + std::string cmdResult; + OHOS::SmartPerf::ParseRadar radar; + std::string rmTrace = CMD_COMMAND_MAP.at(CmdCommand::RM_FILE) + "*" + ".ftrace"; + SPUtils::LoadCmd(rmTrace, cmdResult); + std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "response" + ".ftrace"; + if (isOhTest) { + std::thread([&sd, traceName]() { sd.GetTrace(traceName); }).join(); + time = pcrt.ParseResponseTrace(traceName); + } else { + std::thread thGetTrace = std::thread([&sd, traceName]() { sd.GetTrace(traceName); }); + std::thread thGetHisysId = std::thread([&sd]() { sd.GetHisysId(); }); + std::string str = SPUtils::GetRadarResponse(); + thGetTrace.join(); + thGetHisysId.join(); + time = radar.ParseRadarResponse(str); + } + LOGD("ResponseTime = %d", time); + return time; +} +double ControlCallCmd::CompleteTime() +{ + OHOS::SmartPerf::StartUpDelay sd; + OHOS::SmartPerf::ParseClickCompleteTrace pcct; + std::string cmdResult; + OHOS::SmartPerf::ParseRadar radar; + std::string rmTrace = CMD_COMMAND_MAP.at(CmdCommand::RM_FILE) + "*" + ".ftrace"; + SPUtils::LoadCmd(rmTrace, cmdResult); + std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "complete" + ".ftrace"; + if (isOhTest) { + std::thread([&sd, traceName]() { sd.GetTrace(traceName); }).join(); + time = pcct.ParseCompleteTrace(traceName); + } else { + std::thread thGetTrace = std::thread([&sd, traceName]() { sd.GetTrace(traceName); }); + std::thread thGetHisysId = std::thread([&sd]() { sd.GetHisysId(); }); + std::string str = SPUtils::GetRadarComplete(); + thGetTrace.join(); + thGetHisysId.join(); + time = radar.ParseRadarComplete(str); + } + LOGD("CompleteTime = %d", time); + return time; +} +std::string ControlCallCmd::GetAppStartTime() const +{ + OHOS::SmartPerf::StartUpDelay sd; + std::string cmdResult; + OHOS::SmartPerf::ParseRadar radar; + OHOS::SmartPerf::StallingRateTrace srt; + std::string rmTrace = CMD_COMMAND_MAP.at(CmdCommand::RM_FILE) + std::string("sp_trace_") + "start" + ".ftrace"; + SPUtils::LoadCmd(rmTrace, cmdResult); + std::string traceName = std::string("/data/local/tmp/") + std::string("sp_trace_") + "start" + ".ftrace"; + std::thread thGetTrace = std::thread([&sd, traceName]() { sd.GetTrace(traceName); }); + std::thread thGetHisysId = std::thread([&sd]() { sd.GetHisysIdAndKill(); }); + + std::promise promRadar; + std::promise promRadarFrame; + std::promise promRadarMoved = std::move(promRadar); + std::promise promRadarFrameMoved = std::move(promRadarFrame); + std::future futureRadar = promRadarMoved.get_future(); + std::thread([promiseRadar = std::move(promRadarMoved)]() mutable { + promiseRadar.set_value(SPUtils::GetRadar()); + }).detach(); + std::future futureRadarFrame = promRadarFrameMoved.get_future(); + std::thread([promiseRadarFrame = std::move(promRadarFrameMoved)]() mutable { + promiseRadarFrame.set_value(SPUtils::GetRadarFrame()); + }).detach(); + std::string radarStr = futureRadar.get(); + std::string radarFrameStr = futureRadarFrame.get(); + thGetTrace.join(); + thGetHisysId.join(); + std::string resultStream = radar.ParseRadarAppStrart(radarStr); + std::string resultStream2 = radar.ParseRadarMaxFrame(radarFrameStr); + double stallingRateResult2 = srt.StallingRateResult(traceName); + std::ostringstream ss; + ss << std::fixed << std::setprecision(two) << stallingRateResult2; + std::string ssResult = ss.str(); + std::string hitchTimeRate = "HitchTimeRate:" + ssResult + "ms/s \n"; + resultStream = resultStream + hitchTimeRate + resultStream2; + return resultStream; +} +void ControlCallCmd::IsohTest(const std::vector& v) +{ + if (v[ohType] == "ohtest") { + isOhTest = true; + } +} +} +} diff --git a/smartperf_device/device_command/cmds/src/editor_command.cpp b/smartperf_device/device_command/cmds/src/editor_command.cpp new file mode 100644 index 0000000000000000000000000000000000000000..faa6872a47b70e5b9d22755b0ff7dfe94f85c0f5 --- /dev/null +++ b/smartperf_device/device_command/cmds/src/editor_command.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "unistd.h" +#include +#include +#include +#include "include/editor_command.h" +#include "include/control_call_cmd.h" + + +namespace OHOS { +namespace SmartPerf { +EditorCommand::EditorCommand(int argc, const std::vector &v) +{ + OHOS::SmartPerf::ControlCallCmd controlCallCmd; + controlCallCmd.GetResult(v); +} +} +} diff --git a/smartperf_device/device_command/cmds/src/smartperf_command.cpp b/smartperf_device/device_command/cmds/src/smartperf_command.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ed209593e2d6e1ce7424431bdd7a01f2fec2138 --- /dev/null +++ b/smartperf_device/device_command/cmds/src/smartperf_command.cpp @@ -0,0 +1,172 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include "include/GameEvent.h" +#include "unistd.h" +#include "include/heartbeat.h" +#include "include/sp_utils.h" +#include "include/sp_csv_util.h" +#include "include/sp_profiler_factory.h" +#include "include/sp_thread_socket.h" +#include "include/startup_delay.h" +#include "include/ByTrace.h" +#include "include/smartperf_command.h" +#include "include/sp_log.h" +#include "include/RAM.h" +#include "include/common.h" +#include "include/FPS.h" +#include "include/sp_task.h" +#include "cpu_info.h" +#include "AI_schedule.h" + +namespace OHOS { +namespace SmartPerf { +SmartPerfCommand::SmartPerfCommand(std::vector& argv) +{ + taskMgr_.AddTask(argv); + LOGD("SmartPerfCommand::SmartPerfCommand size(%u)", argv.size()); + if (argv.size() == oneParam) { + SpThreadSocket::GetInstance().SetNeedUdpToken(false); + DeviceServer(true); + } + if (argv.size() == twoParam) { + auto iterator = COMMAND_HELP_MAP.begin(); + while (iterator != COMMAND_HELP_MAP.end()) { + if (iterator->second.compare(argv[1]) == 0) { + HelpCommand(iterator->first, ""); + break; + } + if (argv[1].find("-editorServer") != std::string::npos) { + WLOGI("############################# Found '-editorServer' argument in argv"); + SPUtils::KillStartDaemon(); + const size_t tokenStartPosition = 14; + std::string token = argv[1].substr(tokenStartPosition, argv[1].length() - tokenStartPosition); + HelpCommand(CommandHelp::EDITORSERVER, token); + } else if (argv[1].find("-deviceServer") != std::string::npos) { + WLOGI("############################# Found '-deviceServer' argument in argv"); + std::string token = argv[1].substr(serverCommandLength, argv[1].length() - serverCommandLength); + HelpCommand(CommandHelp::DEVICESERVER, token); + } + ++iterator; + } + } + LOGD("SmartPerfCommand::SmartPerfCommand complete"); +} +void SmartPerfCommand::DeviceServer(int isNeedDaemon) const +{ + EnableWriteLogAndDeleteOldLogFiles(); + OHOS::SmartPerf::StartUpDelay sd; + sd.KillSpProcess(); + std::string pidStr = sd.GetPidByPkg("SP_daemon"); + std::string cmdStr = CMD_COMMAND_MAP.at(CmdCommand::TASKSET); + std::string result = ""; + SPUtils::LoadCmd(cmdStr + pidStr, result); + if (isNeedDaemon) { + daemon(0, 0); + } + CreateSocketThread(); +} +void SmartPerfCommand::HelpCommand(CommandHelp type, const std::string& token) const +{ + LOGD("SmartPerfCommand::HelpCommand type(%d)", type); + if (type == CommandHelp::HELP) { + std::cout << smartPerfMsg << std::endl; + } + if (type == CommandHelp::VERSION) { + std::cout << "Version: " << SPUtils::GetVersion() << std::endl; + } + if (type == CommandHelp::SCREEN) { + std::string result = SPUtils::GetScreen(); + std::cout << result << std::endl; + } + OHOS::SmartPerf::StartUpDelay sd; + if (type == CommandHelp::CLEAR || type == CommandHelp::CLEARALL) { + bool isClearTestServer = (type == CommandHelp::CLEARALL); + sd.GetSpClear(isClearTestServer); + } + if (type == CommandHelp::SERVER || type == CommandHelp::EDITORSERVER) { + sd.ClearOldServer(); + SPUtils::GetTtyDeviceFd(); + std::string pidStr = sd.GetPidByPkg("SP_daemon"); + std::string cmdStr = CMD_COMMAND_MAP.at(CmdCommand::TASKSET); + std::string result = ""; + SPUtils::LoadCmd(cmdStr + pidStr, result); + if (type == CommandHelp::SERVER) { + daemon(0, 0); + } else { + // Editor 拉起 daemon 测试 + EnableWriteLogAndDeleteOldLogFiles(); + if (token.empty()) { + WLOGE("Error: token is empty when setting TCP token."); + return; + } + SpThreadSocket::GetInstance().SetToken(token); + WLOGI("############################# EditorServer Socket Create Start, Ready to Start Collector..."); + } + CreateSocketThread(); + } + if (type == CommandHelp::DEVICESERVER) { + // device 拉起 daemon 测试 + if (token.empty()) { + WLOGE("Error: token is empty when setting UDP token."); + return; + } + SpThreadSocket::GetInstance().SetToken(token); + DeviceServer(false); + WLOGI("############################# DeviceServer Socket Create Start, Ready to Start Collector..."); + } +} + +void SmartPerfCommand::CreateSocketThread() const +{ + InitSomething(); + auto tcpSocket = std::thread([]() { SpThreadSocket::GetInstance().Process(ProtoType::TCP); }); + sleep(1); + auto udpSocket = std::thread([]() { SpThreadSocket::GetInstance().Process(ProtoType::UDP); }); + sleep(1); + auto udpexSocket = std::thread([]() { SpThreadSocket::GetInstance().Process(ProtoType::UDPEX); }); + Heartbeat::GetInstance().UpdatestartTime(); + std::thread([]() { Heartbeat::GetInstance().HeartbeatRule(); }).detach(); + tcpSocket.join(); + udpSocket.join(); + udpexSocket.join(); +} + +std::string SmartPerfCommand::ExecCommand() +{ + RAM &ram = RAM::GetInstance(); + ram.SetFirstFlag(); + taskMgr_.DeleteTask(&AISchedule::GetInstance()); + taskMgr_.InitDataCsv(); + taskMgr_.Start(); + taskMgr_.Wait(); + taskMgr_.Stop(); + taskMgr_.WriteToCSV(); + return std::string("command exec finished!"); +} + +void SmartPerfCommand::InitSomething() +{ + std::string cmdResult; + std::string stat = CMD_COMMAND_MAP.at(CmdCommand::PROC_STAT); + if (SPUtils::LoadCmd(stat, cmdResult)) { + LOGE("SmartPerfCommand::InitSomething Privilege escalation!"); + }; +} +} +}