1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 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 <fstream> 17 #include <sstream> 18 #include <csignal> 19 #include "data_collection.h" 20 21 namespace OHOS::perftest { 22 using namespace std; 23 const string BUNDLE_NAME_PARAM = "BUNDLE_NAME"; 24 SetPid(int32_t pid)25 void DataCollection::SetPid(int32_t pid) 26 { 27 pid_ = pid; 28 } 29 SetBundleName(string bundleName)30 void DataCollection::SetBundleName(string bundleName) 31 { 32 bundleName_ = bundleName; 33 } 34 GetPidByBundleName(string bundleName)35 int32_t DataCollection::GetPidByBundleName(string bundleName) 36 { 37 string getPidCmd = "pidof " + bundleName_; 38 string executeRes; 39 int32_t pid = -1; 40 if (!ExecuteCommand(getPidCmd, executeRes)) { 41 return pid; 42 } 43 LOG_I("Pid of process %{public}s is %{public}s", bundleName_.c_str(), executeRes.c_str()); 44 stringstream pidStream(executeRes); 45 pidStream >> pid; 46 return pid; 47 } 48 IsProcessExist(int32_t pid)49 bool DataCollection::IsProcessExist(int32_t pid) 50 { 51 std::string cmd = "ps -ef | grep -v grep | grep " + to_string(pid); 52 FILE *fd = popen(cmd.c_str(), "r"); 53 if (fd == nullptr) { 54 LOG_E("Execute popen to get process failed"); 55 return false; 56 } 57 char buf[4096] = {'\0'}; 58 while ((fgets(buf, sizeof(buf), fd)) != nullptr) { 59 std::string line(buf); 60 LOG_D("Get process info by pid, line = %{public}s, pid = %{public}d", line.c_str(), pid); 61 std::istringstream iss(line); 62 std::string field; 63 int count = 0; 64 while (iss >> field) { 65 LOG_D("Get process info by pid, count = %{public}d , field = '%{public}s'", count, field.c_str()); 66 if (count == 1 && field == to_string(pid)) { 67 pclose(fd); 68 return true; 69 } 70 count++; 71 } 72 } 73 pclose(fd); 74 return false; 75 } 76 ExecuteCommand(const string command,string & result)77 bool DataCollection::ExecuteCommand(const string command, string& result) 78 { 79 LOG_I("Execute command: %{public}s", command.c_str()); 80 result = ""; 81 FILE *fp = popen(command.c_str(), "r"); 82 if (fp == nullptr) { 83 LOG_E("Execute popen to execute command: %{public}s failed", command.c_str()); 84 return false; 85 } 86 char bufferInfo[1024] = { '\0' }; 87 while (fgets(bufferInfo, sizeof(bufferInfo), fp) != nullptr) { 88 result += bufferInfo; 89 } 90 pclose(fp); 91 LOG_I("Execute command res: %{public}s", result.c_str()); 92 return true; 93 } 94 OnEvent(shared_ptr<HiSysEventRecord> record)95 void TimeListener::OnEvent(shared_ptr<HiSysEventRecord> record) 96 { 97 LOG_I("TimeListener::OnEvent called"); 98 if (record == nullptr) { 99 return; 100 } 101 string bundleName = ""; 102 if (record->GetParamValue(BUNDLE_NAME_PARAM, bundleName) != 0) { 103 LOG_E("Get the bundlename of HiSysEventRecord failed"); 104 return; 105 } 106 if (bundleName != bundleName_) { 107 LOG_I("Not the specific bundlename"); 108 return; 109 } 110 lastEvent_ = record; 111 } 112 OnServiceDied()113 void TimeListener::OnServiceDied() 114 { 115 LOG_E("Service disconnect"); 116 } 117 GetEvent()118 shared_ptr<HiviewDFX::HiSysEventRecord> TimeListener::GetEvent() 119 { 120 return lastEvent_; 121 } 122 } // namespace OHOS::perftest 123