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 "GpuCounter.h" 27 #include "GameEvent.h" 28 #include "task_manager.h" 29 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 = "1"; 57 std::string packageName = ""; 58 std::string pid = ""; 59 std::vector<std::string> taskConfig = {}; 60 long long freq = 998; 61 StuckNotification stuckInfo; 62 bool isPrint = false; 63 }; 64 65 class SPTask { 66 public: GetInstance()67 static SPTask &GetInstance() 68 { 69 static SPTask instance; 70 return instance; 71 } 72 ErrCode InitTask(const std::string &recvStr); 73 ErrCode StartTask(std::function<void(const std::string&)> msgTask); 74 ErrCode StopTask(); 75 bool CheckTcpParam(const std::string& str, std::string &errorInfo); 76 void CreatPath(const std::string& path); 77 void InitDataFile(); 78 void StopGetInfo(); 79 ErrCode StartRecord(); 80 ErrCode StopRecord(); 81 bool GetRecordState(); 82 void SetRecordState(bool status); 83 std::string GetCsvTitle(); 84 void SetAppCmd(const std::string &recvBuf); 85 TaskInfo GetCurTaskInfo(); 86 void SetAppInitFlag(); 87 void SetStartFlag(); 88 void ClearStopFlag(); 89 90 private: 91 void KillHiperfCmd(); 92 int GetCurrentBattary(); 93 void SetProfilerPid(); 94 std::map<std::string, std::string> SetTaskInfo(); 95 void EnablePrint(); 96 97 private: 98 TaskInfo curTaskInfo; 99 long long startTime = 0; 100 std::thread thread; 101 bool isRunning = false; 102 bool isInit = false; 103 std::mutex asyncDataMtx; 104 const std::string baseOutPath = "/data/local/tmp/smartperf"; 105 long long startCaptuerTime = 0; 106 GpuCounter &gpuCounter = GpuCounter::GetInstance(); 107 GameEvent &gameEvent = GameEvent::GetInstance(); 108 bool recordState = false; 109 long long nextTime = 0; 110 int battaryStart = 0; 111 int battaryEnd = 0; 112 std::string csvTitle = ""; 113 int stdOutLog = -1; 114 std::shared_ptr<TaskManager> taskMgr_ {nullptr}; 115 }; 116 } 117 } 118 119 #endif