• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::string pid = "";
58     std::vector<std::string> taskConfig = {};
59     long long freq = 0;
60     StuckNotification stuckInfo;
61     bool isPrint = false;
62 };
63 
64 class SPTask {
65 public:
GetInstance()66     static SPTask &GetInstance()
67     {
68         static SPTask instance;
69         return instance;
70     }
71     ErrCode InitTask(const std::string &recvStr);
72     ErrCode StartTask(std::function<void(std::string data)> msgTask);
73     ErrCode StopTask();
74     std::string GetCurrentTimeAsString();
75     std::map<std::string, std::string> DetectionAndGrab();
76     bool CheckTcpParam(std::string str, std::string &errorInfo);
77     std::future<std::map<std::string, std::string>> AsyncCollectRam();
78     std::future<std::map<std::string, std::string>> AsyncCollectFps();
79     std::future<std::map<std::string, std::string>> AsyncCollectCpu();
80     std::future<std::map<std::string, std::string>> AsyncCollectFds();
81     void CheckFutureRam(std::future<std::map<std::string, std::string>> &ramResult,
82         std::map<std::string, std::string> &dataMap);
83     void CheckFutureFps(std::future<std::map<std::string, std::string>> &fpsResult,
84         std::map<std::string, std::string> &dataMap);
85     void CheckFutureCpu(std::future<std::map<std::string, std::string>> &cpuResult,
86         std::map<std::string, std::string> &dataMap);
87     void CheckFutureFds(std::future<std::map<std::string, std::string>> &fdsResult,
88         std::map<std::string, std::string> &dataMap);
89     void GetItemData(std::map<std::string, std::string> &dataMap);
90     void GetGpuRealtimeData(std::map<std::string, std::string> &dataMap);
91     void CreatPath(std::string path);
92     void ConfigDataThread();
93     void StopSdkRecv();
94     void StopGpuCounterRecv();
95     void InitDataFile();
96     void AsyncGetDataMap(std::function<void(std::string data)> msgTask);
97     void StopGetInfo();
98     ErrCode StartRecord();
99     ErrCode StopRecord();
100     bool GetRecordState();
101     void SaveScreenShot();
102     time_t GetRealStartTime() const;
103     void SetTcpToken(std::string token);
104     void SetRecordState(bool status);
105     std::string GetTcpToken();
106     void ProcessErrorData(SPData &spdata);
107     std::string GetCsvTitle();
108 
109 private:
110     std::thread ThreadGetHiperf(long long timeStamp);
111     void GetHiperf(const std::string &traceName);
112     std::string SetHiperf(const std::string &traceName);
113     bool CheckCounterId();
114     void KillHiperfCmd();
115     void ConfigureSdkData(std::string itConfig);
116     void PrintMap(std::map<std::string, std::string> &dataMap);
117     void RunSdkServer(SdkDataRecv &sdkDataRecv);
118     void ResetSdkParam();
119     int GetCurrentBattary();
120     void SetProfilerPid();
121     std::map<std::string, std::string> SetTaskInfo();
122     void EnablePrint();
123 
124 private:
125     TaskInfo curTaskInfo;
126     long long startTime = 0;
127     std::thread thread;
128     std::vector<SPData> vmap;
129     bool isRunning = false;
130     bool isInit = false;
131     std::mutex asyncDataMtx;
132     std::mutex sdkDataMtx;
133     const std::string baseOutPath = "/data/local/tmp/smartperf";
134     long long startCaptuerTime = 0;
135     int requestId = 1;
136     bool sdkData = false;
137     std::thread sdk;
138     std::thread lockFreqThread;
139     std::vector<std::string> sdkvec;
140     GpuCounter &gpuCounter = GpuCounter::GetInstance();
141     LockFrequency &lockFreq = LockFrequency::GetInstance();
142     bool recordState = false;
143     bool screenshotFlag = false;
144     bool recordTrace = false;
145     time_t realTimeStart = 0;
146     std::string tcpToken = "";
147     long long nextTime = 0;
148     int battaryStart = 0;
149     int battaryEnd = 0;
150     unsigned int dataSize = 0;
151     std::string csvTitle = "";
152     int stdOutLog = -1;
153 
154     std::string strOne = R"(hiprofiler_cmd \
155   -c - \
156   -o /data/local/tmp/)";
157     std::string strTwo = R"(.htrace \
158   -t 5 \
159   -s \
160   -k \
161 <<CONFIG)";
162 
163     std::string strThree = R"(request_id: )";
164     std::string strFour = R"( session_config {
165     buffers {
166     pages: 16384
167     })";
168     std::string strFive = R"( result_file: "/data/local/tmp/)";
169     std::string strSix = R"(.htrace"
170     sample_duration: 5000
171     })";
172     std::string strNine = R"( plugin_configs {
173   plugin_name: "ftrace-plugin"
174   sample_interval: 1000
175   config_data {
176     ftrace_events: "sched/sched_switch"
177     ftrace_events: "power/suspend_resume"
178     ftrace_events: "sched/sched_wakeup"
179     ftrace_events: "sched/sched_wakeup_new"
180     ftrace_events: "sched/sched_waking"
181     ftrace_events: "sched/sched_process_exit"
182     ftrace_events: "sched/sched_process_free"
183     ftrace_events: "task/task_newtask"
184     ftrace_events: "task/task_rename"
185     ftrace_events: "power/cpu_frequency"
186     ftrace_events: "power/cpu_idle"
187     hitrace_categories: "ace"
188     hitrace_categories: "app"
189     hitrace_categories: "ark"
190     hitrace_categories: "graphic"
191     hitrace_categories: "ohos"
192     hitrace_categories: "bin)";
193     std::string strEleven = R"(der"
194     hitrace_categories: "irq"
195     hitrace_categories: "pagecache"
196     hitrace_categories: "zaudio"
197     buffer_size_kb: 20480
198     flush_interval_ms: 1000
199     flush_threshold_kb: 4096
200     parse_ksyms: true
201     clock: "boot"
202     trace_period_ms: 200
203     debug_on: false
204     hitrace_time: 5
205     }
206     })";
207     std::string strSeven = R"( plugin_configs {
208   plugin_name: "hiperf-plugin"
209   sample_interval: 5000
210   config_data {
211     is_root: false
212    outfile_name: "/data/local/tmp/)";
213     std::string strEight = R"(.data"
214    record_args: "-f 1000 -a  --cpu-limit 100 -e hw-cpu-cycles,sched:sched_waking )";
215     std::string strTen = R"(--call-stack dwarf --clockid monotonic --offcpu -m 256"
216     }
217     })";
218     std::string conFig = R"(CONFIG)";
219 };
220 }
221 }
222 
223 #endif