• 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 package ohos.devtools.services.hiperf;
16 
17 import ohos.devtools.datasources.transport.hdc.HdcWrapper;
18 import ohos.devtools.datasources.utils.monitorconfig.entity.PerfConfig;
19 import ohos.devtools.datasources.utils.session.entity.SessionInfo;
20 import ohos.devtools.datasources.utils.session.service.SessionManager;
21 import org.jetbrains.annotations.NotNull;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26 
27 /**
28  * Perf Command Parent Class
29  *
30  * @since 2021/8/25
31  */
32 public abstract class PerfCommand {
33     /**
34      * PERF_TRACE_PATH
35      */
36     public static final String PERF_TRACE_PATH = "/data/local/tmp/perf_data";
37 
38     /**
39      * perf data path
40      */
41     protected static final String PERF_DATA_PATH = "/data/local/tmp/perf.data";
42 
43     /**
44      * deviceId
45      */
46     protected String deviceId;
47 
48     /**
49      * sessionInfo
50      */
51     protected SessionInfo sessionInfo;
52 
53     /**
54      * isLeakOhos
55      */
56     protected boolean isLeakOhos;
57 
58     /**
59      * outPath
60      */
61     protected String outPath;
62     private int lastSize;
63 
64     /**
65      * Construction.
66      *
67      * @param sessionId current session id
68      * @param isLeakOhos is leak ohos
69      * @param deviceId devices id
70      * @param fileStorePath out file path
71      */
PerfCommand(long sessionId, boolean isLeakOhos, String deviceId, String fileStorePath)72     public PerfCommand(long sessionId, boolean isLeakOhos, String deviceId, String fileStorePath) {
73         this(isLeakOhos, deviceId);
74         sessionInfo = SessionManager.getInstance().getSessionInfo(sessionId);
75         outPath = fileStorePath;
76     }
77 
78     /**
79      * PerfCommand
80      *
81      * @param isLeakOhos isLeakOhos
82      * @param deviceId deviceId
83      */
PerfCommand(boolean isLeakOhos, String deviceId)84     public PerfCommand(boolean isLeakOhos, String deviceId) {
85         this.isLeakOhos = isLeakOhos;
86         this.deviceId = deviceId;
87     }
88 
89     /**
90      * execute Record command
91      * hiperf record -p xxx --app xxx -o perf.data -f 1000 --offcpu
92      *
93      * @param config config
94      * @return List<String>
95      */
executeRecord(PerfConfig config)96     public abstract List<String> executeRecord(PerfConfig config);
97 
98     /**
99      * execute report command
100      * hiperf record --proto -i perf.data -o xxx.trace
101      *
102      * @return execute Command result
103      */
executeReport()104     public abstract List<String> executeReport();
105 
106     /**
107      * Stop Record by kill perf pid.
108      */
stopRecord()109     public abstract void stopRecord();
110 
111     /**
112      * get Support Event
113      *
114      * @return Map<String, List<String>>
115      */
getSupportEvents()116     public abstract Map<String, List<String>> getSupportEvents();
117 
118     /**
119      * wait record file generate done.
120      * adjust perf.data size will not increase.
121      */
checkData()122     public void checkData() {
123         while (true) {
124             ArrayList<String> checkData = HdcWrapper.getInstance().generateDeviceCmdHead(isLeakOhos, deviceId);
125             checkData.add("du");
126             checkData.add(PERF_DATA_PATH);
127             String execResult = HdcWrapper.getInstance().execCmdBy(checkData);
128             String result = execResult.trim().replaceAll("\\s+", " ");
129             try {
130                 int currentSize = Integer.parseInt(result.split(" ")[0]);
131                 // if perf.data current Size equals last 1s second size
132                 // generate perf.data is done
133                 if (currentSize == lastSize && currentSize != 0) {
134                     break;
135                 }
136                 lastSize = currentSize;
137             } catch (NumberFormatException exception) {
138                 break;
139             }
140             try {
141                 Thread.sleep(1000);
142             } catch (InterruptedException exception) {
143                 exception.printStackTrace();
144             }
145         }
146     }
147 
148     /**
149      * list to string
150      *
151      * @param command list
152      * @return string
153      */
154     @NotNull
getCmd(@otNull ArrayList<String> command)155     protected String getCmd(@NotNull ArrayList<String> command) {
156         StringBuilder cmd = new StringBuilder();
157         for (String line : command) {
158             cmd.append(line).append(" ");
159         }
160         return cmd.toString();
161     }
162 }
163