/* * Copyright (C) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef SP_TASK_H #define SP_TASK_H #include #include #include #include #include #include #include #include #include "parameters.h" #include "sp_csv_util.h" #include "sdk_data_recv.h" #include "GpuCounter.h" namespace OHOS { namespace SmartPerf { enum class ExceptionMsg { NO_ERR, SESSION_ID_NULL, TASK_CONFIG_NULL, PACKAGE_NULL, }; const std::map EXCEPTION_MSG_MAP = { { ExceptionMsg::NO_ERR, "NoErr" }, { ExceptionMsg::SESSION_ID_NULL, "SessionIdNull" }, { ExceptionMsg::TASK_CONFIG_NULL, "TaskConfigNull" }, { ExceptionMsg::PACKAGE_NULL, "PackageNull" }, }; enum class ErrCode { OK, FAILED, }; struct StuckNotification { bool isEffective = false; int fps = 0; long long frameTime = LLONG_MAX; }; struct TaskInfo { std::string sessionId = ""; std::string packageName = ""; std::vector taskConfig = {}; long long freq = 0; StuckNotification stuckInfo; }; class SPTask { public: static SPTask &GetInstance() { static SPTask instance; return instance; } ErrCode InitTask(const std::string &recvStr); ErrCode StartTask(std::function msgTask); void StopTask(); std::string GetCurrentTimeAsString(); std::map DetectionAndGrab(); bool CheckTcpParam(std::string str, std::string &errorInfo); std::future> AsyncCollectRam(); std::future> AsyncCollectFps(); std::future> AsyncCollectCpu(); void CheckFutureRam(std::future> &ramResult, std::map &dataMap); void CheckFutureFps(std::future> &fpsResult, std::map &dataMap); void CheckFutureCpu(std::future> &cpuResult, std::map &dataMap); void GetItemData(std::map &dataMap); void GetGpuRealtimeData(std::map &dataMap); void WritePath(std::string thisBasePath); void ConfigDataThread(); void StopSdkRecv(); void StopGpuCounterRecv(); void InitDataFile(); void AsyncGetDataMap(std::function msgTask); void StopGetInfo(); void StartRecord(); void StopRecord(); bool GetRecordState(); time_t GetRealStartTime() const; private: std::thread ThreadGetHiperf(long long timeStamp); void GetHiperf(const std::string &traceName); std::string SetHiperf(const std::string &traceName); bool CheckCounterId(); void KillHiperfCmd(); void ConfigureSdkData(std::string itConfig); void RunSdkServer(SdkDataRecv &sdkDataRecv); void ResetSdkParam(); private: TaskInfo curTaskInfo; long long startTime = 0; std::thread thread; std::vector vmap; bool isRunning = false; bool isInit = false; std::mutex asyncDataMtx; std::mutex sdkDataMtx; const std::string baseOutPath = "/data/local/tmp/smartperf"; long long startCaptuerTime = 0; int requestId = 1; bool sdkData = false; std::thread sdk; std::vector sdkvec; GpuCounter &gpuCounter = GpuCounter::GetInstance(); bool recordState = false; time_t realTimeStart = 0; std::string strOne = R"(hiprofiler_cmd \ -c - \ -o /data/local/tmp/)"; std::string strTwo = R"(.htrace \ -t 5 \ -s \ -k \ <