• 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 
16 package ohos.devtools.datasources.utils.monitorconfig.entity;
17 
18 import ohos.devtools.datasources.transport.hdc.HdcWrapper;
19 import ohos.devtools.services.hiperf.HiPerfCommand;
20 import ohos.devtools.services.hiperf.PerfCommand;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 
26 /**
27  * Perf config Struct.
28  *
29  * @since 2021/11/02
30  */
31 public class PerfConfig<T> {
32     private static final int DEFAULT_FREQUENCY = 1000;
33     private static final int DEFAULT_CPU_PERCENT = 100;
34     private static final int DEFAULT_PERIOD = 1;
35     private static final int DEFAULT_MEMORY = 256;
36     private static final String SOFT_EVENT_KEY = "software";
37     private static final String HARD_EVENT_KEY = "hardware";
38 
39     /**
40      * record assign cpu num such as 0,1,2;
41      */
42     private List<T> cpuList = new ArrayList<>();
43 
44     /**
45      * Set the max percent of cpu time used for recording. percent is in range [1-100]
46      */
47     private int cpuPercent = DEFAULT_CPU_PERCENT;
48 
49     /**
50      * Set event sampling frequency. default is 4000 samples every second.
51      */
52     private int frequency = DEFAULT_FREQUENCY;
53 
54     /**
55      * event type Default is cpu cycles
56      */
57     private List<T> eventList = new ArrayList<>();
58 
59     /**
60      * Set event sampling period for trace point events. recording one sample when <num> events happened.
61      */
62     private int period = DEFAULT_PERIOD;
63 
64     /**
65      * off cpu
66      */
67     private boolean isOffCpu = true;
68 
69     /**
70      * Don't trace child processes.
71      */
72     private boolean isInherit = false;
73 
74     /**
75      * Setup and enable call stack (stack chain/backtrace) recording,related with enum CallStack,
76      */
77     private int callStack = 0;
78 
79     /**
80      * taken branch stack sampling related with enum Branch
81      */
82     private int branch = 0;
83 
84     /**
85      * used to receiving record data from kernel, must be a power of two, rang[2,1024], default is 256.
86      */
87     private int mmapPages = DEFAULT_MEMORY;
88 
89     /**
90      * Set the clock id to use for the various time fields in the perf_event_type records.related enum Clock
91      */
92     private int clockId = 0;
93 
94     /**
95      * get Cpu list
96      *
97      * @return cpu list
98      */
getCpuList()99     public List<T> getCpuList() {
100         return cpuList;
101     }
102 
103     /**
104      * set Cpu List
105      *
106      * @param cpuList Cpu list
107      */
setCpuList(List<T> cpuList)108     public void setCpuList(List<T> cpuList) {
109         this.cpuList = cpuList;
110     }
111 
112     /**
113      * get set Cpu Percent
114      *
115      * @return cpu percent
116      */
getCpuPercent()117     public int getCpuPercent() {
118         return cpuPercent;
119     }
120 
121     /**
122      * set cpu percent
123      *
124      * @param cpuPercent percent
125      */
setCpuPercent(int cpuPercent)126     public void setCpuPercent(int cpuPercent) {
127         this.cpuPercent = cpuPercent;
128     }
129 
130     /**
131      * get frequency
132      *
133      * @return frequency
134      */
getFrequency()135     public int getFrequency() {
136         return frequency;
137     }
138 
139     /**
140      * set frequency
141      *
142      * @param frequency frequency
143      */
setFrequency(int frequency)144     public void setFrequency(int frequency) {
145         this.frequency = frequency;
146     }
147 
148     /**
149      * get Event list
150      *
151      * @return event list
152      */
getEventList()153     public List<T> getEventList() {
154         return eventList;
155     }
156 
157     /**
158      * set event list
159      *
160      * @param eventList event list
161      */
setEventList(List<T> eventList)162     public void setEventList(List<T> eventList) {
163         this.eventList = eventList;
164     }
165 
166     /**
167      * get sample period
168      *
169      * @return period
170      */
getPeriod()171     public int getPeriod() {
172         return period;
173     }
174 
175     /**
176      * set sample peroid
177      *
178      * @param period period
179      */
setPeriod(int period)180     public void setPeriod(int period) {
181         this.period = period;
182     }
183 
184     /**
185      * adjust is off cpu
186      *
187      * @return is off cpu
188      */
isOffCpu()189     public boolean isOffCpu() {
190         return isOffCpu;
191     }
192 
193     /**
194      * set off cpu
195      *
196      * @param offCpu off cpu
197      */
setOffCpu(boolean offCpu)198     public void setOffCpu(boolean offCpu) {
199         this.isOffCpu = offCpu;
200     }
201 
202     /**
203      * get Call stack type index of enum @CallStack
204      *
205      * @return enum CallStack index
206      */
getCallStack()207     public int getCallStack() {
208         return callStack;
209     }
210 
211     /**
212      * set enum CallStack index
213      *
214      * @param callStack index
215      */
setCallStack(int callStack)216     public void setCallStack(int callStack) {
217         this.callStack = callStack;
218     }
219 
220     /**
221      * get enum Branch index
222      *
223      * @return index of enum Branch
224      */
getBranch()225     public int getBranch() {
226         return branch;
227     }
228 
229     /**
230      * set enmu Branch index
231      *
232      * @param branch index
233      */
setBranch(int branch)234     public void setBranch(int branch) {
235         this.branch = branch;
236     }
237 
238     /**
239      * get memory map pages
240      *
241      * @return memory
242      */
getMmapPages()243     public int getMmapPages() {
244         return mmapPages;
245     }
246 
247     /**
248      * set memory map pages
249      *
250      * @param mmapPages memory
251      */
setMmapPages(int mmapPages)252     public void setMmapPages(int mmapPages) {
253         this.mmapPages = mmapPages;
254     }
255 
256     /**
257      * is No inherit
258      *
259      * @return is no Inherit
260      */
isInherit()261     public boolean isInherit() {
262         return isInherit;
263     }
264 
265     /**
266      * set no Inherit
267      *
268      * @param inherit noInherit
269      */
setInherit(boolean inherit)270     public void setInherit(boolean inherit) {
271         this.isInherit = inherit;
272     }
273 
274     /**
275      * get Clock id
276      *
277      * @return index of enum Clock
278      */
getClockId()279     public int getClockId() {
280         return clockId;
281     }
282 
283     /**
284      * set Clock id
285      *
286      * @param clockId index of clock
287      */
setClockId(int clockId)288     public void setClockId(int clockId) {
289         this.clockId = clockId;
290     }
291 
292     /**
293      * use to -j
294      *
295      * @since 2021/5/19 16:39
296      */
297     public enum Branch {
298         /**
299          * none
300          */
301         NONE("none"),
302         /**
303          * any type of branch
304          */
305         ANY("any"),
306         /**
307          * any function call or system call
308          */
309         ANY_CALL("any_call"),
310         /**
311          * any function return or system call return
312          */
313         ANY_RET("any_ret"),
314         /**
315          * any indirect branch
316          */
317         IND_CALL("ind_call"),
318         /**
319          * direct calls, including far (to/from kernel) calls
320          */
321         CALL("call"),
322         /**
323          * only when the branch target is at the user level
324          */
325         USER("u"),
326         /**
327          * only when the branch target is in the kernel
328          */
329         KERNEL("k");
330         private String name;
331 
Branch(String name)332         Branch(String name) {
333             this.name = name;
334         }
335 
336         /**
337          * get Name
338          *
339          * @return name
340          */
getName()341         public String getName() {
342             return name;
343         }
344     }
345 
346     /**
347      * use to --call-stack
348      *
349      * @since 2021/5/19 16:39
350      */
351     public enum CallStack {
352         /**
353          * none
354          */
355         NONE(0, "none"),
356         /**
357          * frame pointer
358          */
359         FP(1, "fp"),
360         /**
361          * DWARF's CFI - Call Frame Information
362          * 'dwarf,size' set sample stack size,
363          * size should be in 8~65528 and 8 byte aligned.
364          */
365         DWARF(2, "dwarf");
366         private String name;
367         private int index;
368 
CallStack(int index, String name)369         CallStack(int index, String name) {
370             this.name = name;
371         }
372 
373         /**
374          * get Name
375          *
376          * @return name
377          */
getName()378         public String getName() {
379             return name;
380         }
381 
382         /**
383          * get index
384          *
385          * @return index
386          */
getIndex()387         public int getIndex() {
388             return index;
389         }
390     }
391 
392     /**
393      * use --clock_id
394      *
395      * @since 2021/5/19 16:39
396      */
397     public enum Clock {
398         MONOTONIC("monotonic"), MONOTONIC_RAW("monitonic_row"), REALTIME("realtime"), BOOTTIME("boottime"),
399         PERF("perf");
400         private String name;
401 
Clock(String name)402         Clock(String name) {
403             this.name = name;
404         }
405 
406         /**
407          * getName
408          *
409          * @return name
410          */
getName()411         public String getName() {
412             return name;
413         }
414     }
415 
416     /**
417      * get device cpu count
418      *
419      * @param isLeakOhos isLeakOhos
420      * @param deviceId device id
421      * @return cpu count of device
422      */
getCpuCount(boolean isLeakOhos, String deviceId)423     public int getCpuCount(boolean isLeakOhos, String deviceId) {
424         ArrayList<String> cmd = HdcWrapper.getInstance().generateDeviceCmdHead(isLeakOhos, deviceId);
425         cmd.add("ls");
426         cmd.add("/sys/devices/system/cpu/");
427         cmd.add("|");
428         cmd.add("grep");
429         cmd.add("cpu");
430         cmd.add("|");
431         cmd.add("wc");
432         cmd.add("-l");
433         try {
434             String count = HdcWrapper.getInstance().execCmdBy(cmd).strip();
435             return Integer.parseInt(count);
436         } catch (NumberFormatException exception) {
437             return 0;
438         }
439     }
440 
441     /**
442      * get device support Events
443      *
444      * @param isLeakOhos isLeakOhos
445      * @param deviceId deviceId
446      * @return Map type is key , events is value
447      */
getSupportEvents(boolean isLeakOhos, String deviceId)448     public Map<String, List<String>> getSupportEvents(boolean isLeakOhos, String deviceId) {
449         PerfCommand perfCommand = new HiPerfCommand(isLeakOhos, deviceId);
450         return perfCommand.getSupportEvents();
451     }
452 
453     /**
454      * Filter Software and hardware Events
455      *
456      * @param isLeakOhos isLeakOhos
457      * @param deviceId deviceId
458      * @return sw, hw event list
459      */
getSoftHardWareEvents(boolean isLeakOhos, String deviceId)460     public List<String> getSoftHardWareEvents(boolean isLeakOhos, String deviceId) {
461         List<String> shEvents = new ArrayList<>();
462         Map<String, List<String>> events = getSupportEvents(isLeakOhos, deviceId);
463         if (events.containsKey(HARD_EVENT_KEY)) {
464             shEvents.addAll(events.get(HARD_EVENT_KEY));
465         }
466         if (events.containsKey(SOFT_EVENT_KEY)) {
467             shEvents.addAll(events.get(SOFT_EVENT_KEY));
468         }
469         return shEvents;
470     }
471 }
472