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