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