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 TASK_MANAGER_H 17 #define TASK_MANAGER_H 18 19 #include "thread_pool.h" 20 #include "argument_parser.h" 21 #include "sp_profiler_factory.h" 22 #include "common.h" 23 #include <fstream> 24 25 namespace OHOS::SmartPerf { 26 class TaskManager; 27 class ThreadLocal { 28 public: 29 ThreadLocal(); 30 ~ThreadLocal(); 31 std::map<uint32_t, std::map<std::string, std::string>> datasA_; 32 std::map<uint32_t, std::map<std::string, std::string>> datasB_; 33 std::atomic_bool switch_ {true}; 34 }; 35 36 class TaskManager { 37 public: 38 explicit TaskManager(bool isIPC = false); 39 40 void AddTask(const std::unordered_map<std::string, ArgumentParser::ArgValue>& argv); 41 void AddTask(const std::string& argv); 42 void AddTask(SpProfiler* task, bool priority); 43 void AddTask(std::vector<std::string>& argv); 44 void SetFilePath(const std::string& fileName, bool removeCurrentFile = true); 45 void Start(bool record = true); 46 void Stop(bool pause = false); 47 void CollectData(std::map<uint32_t, std::map<std::string, std::string>>& datas); 48 void WriteToCSV(); 49 void Wait(); 50 void RegisterThreadLocal(ThreadLocal* local); 51 void EnableIPCCallback(); 52 void SetIPCCallback(std::function<void(const std::string&)> callback); 53 void DisableIPCCallback(); 54 void SetHapFlag(bool flag); 55 void SetNextTime(long long& nextTime); 56 void SetRecordState(bool record); 57 void DeleteTask(SpProfiler* task); 58 void SetFileTitle(); 59 void InitDataCsv(); 60 ArgumentParser& GetArgumentParser(); 61 62 private: 63 std::map<std::string, std::string> TaskFun(SpProfiler* pro, uint32_t batch, bool record); 64 void GetProcessInfo(CommandType type, const ArgumentParser::ArgValue& value); 65 std::string MapToString(std::map<std::string, std::string>& myMap); 66 void StartSaveFileThread(); 67 void CollectThreadsData(); 68 void SaveRegularly(std::chrono::steady_clock::time_point& loopEnd); 69 void ProcessCurrentBatch(std::map<std::string, std::string>& data); 70 void ProcessOnceTask(bool start); 71 void MainLoop(); 72 void GpuCounterProcess(const ArgumentParser::ArgValue& value); 73 void SpecialKeyProcess(const std::string specKey); 74 void HandleCommandType(CommandType cmdType, const ArgumentParser::ArgValue& val); 75 ThreadPool threadPool_ {4}; 76 int32_t collectCount_ {-1}; 77 std::atomic_bool running_ {false}; 78 std::thread mainLoop_; 79 std::set<SpProfiler *> normalTask_; 80 std::set<SpProfiler *> priorityTask_; 81 std::map<uint32_t, std::map<std::string, std::string>> datas_; 82 std::mutex mtx_; 83 std::string fileName_ {"/data/local/tmp/data.csv"}; 84 bool saveFlag_ {false}; 85 long long* nextTime_ {nullptr}; 86 87 std::condition_variable finishCond_; 88 std::mutex finishMtx_; 89 std::string processName_; 90 std::string processIds_; 91 bool printDataInfo_ {true}; 92 bool ipcDataRecv_ {false}; 93 std::vector<ThreadLocal*> threadLocals_ {4}; 94 std::ofstream dataFile_; 95 std::chrono::steady_clock::time_point currentTimePoint_; 96 std::set<std::string> titles_; 97 98 std::thread scheduleSaveDataTh_; 99 std::condition_variable scheduleSaveDataCond_; 100 std::mutex scheduleSaveDataMtx_; 101 std::function<void(const std::string&)> ipcCallback_ {nullptr}; 102 bool hapCollect_ {false}; 103 int32_t dataIndex_ {0}; 104 std::atomic_bool recordData_ {false}; 105 std::atomic_bool savingFile_ {false}; 106 std::atomic_bool isPause_ {false}; 107 bool firstSetTitle_ {true}; 108 ArgumentParser parameter_; 109 }; 110 } 111 #endif // TASK_MANAGER_H