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