1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2024. 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 16 #include <iostream> 17 #include <chrono> 18 #include <cstdlib> 19 #include <unistd.h> 20 #include <mutex> 21 #include "include/heartbeat.h" 22 #include "include/sp_utils.h" 23 #include "sp_log.h" 24 #include "include/startup_delay.h" 25 #include "include/common.h" 26 namespace OHOS { 27 namespace SmartPerf { KillSpId()28void Heartbeat::KillSpId() 29 { 30 std::string str; 31 std::string resultPid; 32 std::string cmd = CMD_COMMAND_MAP.at(CmdCommand::PIDOF_SP); 33 SPUtils::LoadCmd(cmd, resultPid); 34 std::vector<std::string> vec; 35 std::string token; 36 size_t pos = resultPid.find(' '); 37 do { 38 token = resultPid.substr(0, pos); 39 vec.push_back(token); 40 resultPid.erase(0, pos + 1); 41 } while ((pos = resultPid.find(' ')) != std::string::npos); 42 if (vec.size() > 0) { 43 std::string killCmd = CMD_COMMAND_MAP.at(CmdCommand::KILL_CMD); 44 for (size_t i = 0; i < vec.size(); i++) { 45 SPUtils::LoadCmd(killCmd + vec[i], str); 46 } 47 } 48 } 49 HeartbeatRule()50void Heartbeat::HeartbeatRule() 51 { 52 while (isrunning) { 53 auto end = std::chrono::steady_clock::now(); 54 auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - updateStart).count(); 55 if (duration > timeout) { 56 LOGD("Socket disconnected!"); 57 KillSpId(); 58 } 59 sleep(checkMessageTime); 60 } 61 } 62 UpdatestartTime()63void Heartbeat::UpdatestartTime() 64 { 65 std::unique_lock<std::mutex> lock(mtx); 66 updateStart = std::chrono::steady_clock::now(); 67 } 68 69 } 70 }