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 #ifndef CPU_INFO_H 17 #define CPU_INFO_H 18 19 #include "sp_profiler.h" 20 #include "sp_utils.h" 21 #include <string> 22 #include <vector> 23 #include <thread> 24 #include <fstream> 25 #include <atomic> 26 #include <condition_variable> 27 28 namespace OHOS { 29 namespace SmartPerf { 30 class CPUInfo : public SpProfiler { 31 public: GetInstance()32 static CPUInfo &GetInstance() 33 { 34 static CPUInfo instance; 35 return instance; 36 } 37 std::map<std::string, std::string> ItemData() override; 38 void StartExecutionOnce(bool isPause) override; 39 void FinishtExecutionOnce(bool isPause) override; 40 void SetPids(const std::string& pids); 41 void Start(); 42 void Stop(); 43 private: 44 void CalculateCPIInfo(const std::string line, double &cpi_total, size_t &cpi_count); 45 std::vector<std::string> pids_; 46 std::thread th_; 47 std::string hiperfCmd_ = "/bin/hiperf stat -e " + SPUtils::GetProductName() + "-instructions," + 48 SPUtils::GetProductName() + "-cpu-cycles -d 1 -i 500 "; 49 std::atomic_bool running_ {false}; 50 std::condition_variable cond_; 51 std::mutex mtx_; 52 std::string buffer_; 53 }; 54 } 55 } 56 #endif // CPU_INFO_H