1 /* 2 * Copyright (C) 2021 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 #ifndef SP_TASK_H 16 #define SP_TASK_H 17 #include <iostream> 18 #include <functional> 19 #include <vector> 20 #include <thread> 21 #include <future> 22 #include <map> 23 #include <mutex> 24 #include <climits> 25 #include "parameters.h" 26 #include "sdk_data_recv.h" 27 #include "GpuCounter.h" 28 #include "lock_frequency.h" 29 namespace OHOS { 30 namespace SmartPerf { 31 enum class ExceptionMsg { 32 NO_ERR, 33 SESSION_ID_NULL, 34 TASK_CONFIG_NULL, 35 PACKAGE_NULL, 36 }; 37 38 const std::map<ExceptionMsg, std::string> EXCEPTION_MSG_MAP = { 39 { ExceptionMsg::NO_ERR, "NoErr" }, 40 { ExceptionMsg::SESSION_ID_NULL, "SessionIdNull" }, 41 { ExceptionMsg::TASK_CONFIG_NULL, "TaskConfigNull" }, 42 { ExceptionMsg::PACKAGE_NULL, "PackageNull" }, 43 }; 44 45 enum class ErrCode { 46 OK, 47 FAILED, 48 }; 49 struct StuckNotification { 50 bool isEffective = false; 51 int fps = 0; 52 long long frameTime = LLONG_MAX; 53 }; 54 struct TaskInfo { 55 std::string sessionId = ""; 56 std::string packageName = ""; 57 std::vector<std::string> taskConfig = {}; 58 long long freq = 0; 59 StuckNotification stuckInfo; 60 }; 61 62 class SPTask { 63 public: GetInstance()64 static SPTask &GetInstance() 65 { 66 static SPTask instance; 67 return instance; 68 } 69 ErrCode InitTask(const std::string &recvStr); 70 ErrCode StartTask(std::function<void(std::string data)> msgTask); 71 ErrCode StopTask(); 72 std::string GetCurrentTimeAsString(); 73 std::map<std::string, std::string> DetectionAndGrab(); 74 bool CheckTcpParam(std::string str, std::string &errorInfo); 75 std::future<std::map<std::string, std::string>> AsyncCollectRam(); 76 std::future<std::map<std::string, std::string>> AsyncCollectFps(); 77 std::future<std::map<std::string, std::string>> AsyncCollectCpu(); 78 void CheckFutureRam(std::future<std::map<std::string, std::string>> &ramResult, 79 std::map<std::string, std::string> &dataMap); 80 void CheckFutureFps(std::future<std::map<std::string, std::string>> &fpsResult, 81 std::map<std::string, std::string> &dataMap); 82 void CheckFutureCpu(std::future<std::map<std::string, std::string>> &cpuResult, 83 std::map<std::string, std::string> &dataMap); 84 void GetItemData(std::map<std::string, std::string> &dataMap); 85 void GetGpuRealtimeData(std::map<std::string, std::string> &dataMap); 86 void CreatPath(std::string path); 87 void ConfigDataThread(); 88 void StopSdkRecv(); 89 void StopGpuCounterRecv(); 90 void InitDataFile(); 91 void AsyncGetDataMap(std::function<void(std::string data)> msgTask); 92 void StopGetInfo(); 93 ErrCode StartRecord(); 94 ErrCode StopRecord(); 95 bool GetRecordState(); 96 void SaveScreenShot(); 97 time_t GetRealStartTime() const; 98 void SetTcpToken(std::string token); 99 void SetRecordState(bool status); 100 std::string GetTcpToken(); 101 void ProcessErrorData(SPData &spdata); 102 std::string GetCsvTitle(); 103 104 private: 105 std::thread ThreadGetHiperf(long long timeStamp); 106 void GetHiperf(const std::string &traceName); 107 std::string SetHiperf(const std::string &traceName); 108 bool CheckCounterId(); 109 void KillHiperfCmd(); 110 void ConfigureSdkData(std::string itConfig); 111 void RunSdkServer(SdkDataRecv &sdkDataRecv); 112 void ResetSdkParam(); 113 int GetCurrentBattary(); 114 void SetProfilerPid(); 115 std::map<std::string, std::string> SetTaskInfo(); 116 117 private: 118 TaskInfo curTaskInfo; 119 long long startTime = 0; 120 std::thread thread; 121 std::vector<SPData> vmap; 122 bool isRunning = false; 123 bool isInit = false; 124 std::mutex asyncDataMtx; 125 std::mutex sdkDataMtx; 126 const std::string baseOutPath = "/data/local/tmp/smartperf"; 127 long long startCaptuerTime = 0; 128 int requestId = 1; 129 bool sdkData = false; 130 std::thread sdk; 131 std::thread lockFreqThread; 132 std::vector<std::string> sdkvec; 133 GpuCounter &gpuCounter = GpuCounter::GetInstance(); 134 LockFrequency &lockFreq = LockFrequency::GetInstance(); 135 bool recordState = false; 136 bool screenshotFlag = false; 137 bool recordTrace = false; 138 time_t realTimeStart = 0; 139 std::string tcpToken = ""; 140 long long nextTime = 0; 141 int battaryStart = 0; 142 int battaryEnd = 0; 143 unsigned int dataSize = 0; 144 std::string csvTitle = ""; 145 146 std::string strOne = R"(hiprofiler_cmd \ 147 -c - \ 148 -o /data/local/tmp/)"; 149 std::string strTwo = R"(.htrace \ 150 -t 5 \ 151 -s \ 152 -k \ 153 <<CONFIG)"; 154 155 std::string strThree = R"(request_id: )"; 156 std::string strFour = R"( session_config { 157 buffers { 158 pages: 16384 159 })"; 160 std::string strFive = R"( result_file: "/data/local/tmp/)"; 161 std::string strSix = R"(.htrace" 162 sample_duration: 5000 163 })"; 164 std::string strNine = R"( plugin_configs { 165 plugin_name: "ftrace-plugin" 166 sample_interval: 1000 167 config_data { 168 ftrace_events: "sched/sched_switch" 169 ftrace_events: "power/suspend_resume" 170 ftrace_events: "sched/sched_wakeup" 171 ftrace_events: "sched/sched_wakeup_new" 172 ftrace_events: "sched/sched_waking" 173 ftrace_events: "sched/sched_process_exit" 174 ftrace_events: "sched/sched_process_free" 175 ftrace_events: "task/task_newtask" 176 ftrace_events: "task/task_rename" 177 ftrace_events: "power/cpu_frequency" 178 ftrace_events: "power/cpu_idle" 179 hitrace_categories: "ace" 180 hitrace_categories: "app" 181 hitrace_categories: "ark" 182 hitrace_categories: "graphic" 183 hitrace_categories: "ohos" 184 hitrace_categories: "bin)"; 185 std::string strEleven = R"(der" 186 hitrace_categories: "irq" 187 hitrace_categories: "pagecache" 188 hitrace_categories: "zaudio" 189 buffer_size_kb: 20480 190 flush_interval_ms: 1000 191 flush_threshold_kb: 4096 192 parse_ksyms: true 193 clock: "boot" 194 trace_period_ms: 200 195 debug_on: false 196 hitrace_time: 5 197 } 198 })"; 199 std::string strSeven = R"( plugin_configs { 200 plugin_name: "hiperf-plugin" 201 sample_interval: 5000 202 config_data { 203 is_root: false 204 outfile_name: "/data/local/tmp/)"; 205 std::string strEight = R"(.data" 206 record_args: "-f 1000 -a --cpu-limit 100 -e hw-cpu-cycles,sched:sched_waking )"; 207 std::string strTen = R"(--call-stack dwarf --clockid monotonic --offcpu -m 256" 208 } 209 })"; 210 std::string conFig = R"(CONFIG)"; 211 }; 212 } 213 } 214 215 #endif