• 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.process.service;
17 
18 import io.grpc.StatusRuntimeException;
19 import ohos.devtools.datasources.transport.grpc.HiProfilerClient;
20 import ohos.devtools.datasources.transport.grpc.ProfilerClient;
21 import ohos.devtools.datasources.transport.grpc.ProfilerServiceHelper;
22 import ohos.devtools.datasources.transport.grpc.service.CommonTypes;
23 import ohos.devtools.datasources.transport.grpc.service.ProcessPluginConfig;
24 import ohos.devtools.datasources.transport.grpc.service.ProfilerServiceTypes;
25 import ohos.devtools.datasources.utils.common.util.CommonUtil;
26 import ohos.devtools.datasources.utils.common.util.DateTimeUtil;
27 import ohos.devtools.datasources.utils.device.dao.DeviceDao;
28 import ohos.devtools.datasources.utils.device.entity.DeviceIPPortInfo;
29 import ohos.devtools.datasources.utils.device.entity.DeviceType;
30 import ohos.devtools.datasources.utils.process.entity.ProcessInfo;
31 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager;
32 import ohos.devtools.views.common.LayoutConstants;
33 import org.apache.commons.lang3.StringUtils;
34 import org.apache.logging.log4j.LogManager;
35 import org.apache.logging.log4j.Logger;
36 
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Objects;
40 import java.util.Optional;
41 
42 import static ohos.devtools.datasources.transport.grpc.HiProfilerClient.getSTDSha256;
43 import static ohos.devtools.datasources.transport.grpc.HiProfilerClient.getSha256;
44 import static ohos.devtools.datasources.utils.device.entity.DeviceType.LEAN_HOS_DEVICE;
45 import static ohos.devtools.views.common.Constant.IS_SUPPORT_NEW_HDC;
46 
47 /**
48  * processManager
49  *
50  * @since 2021/11/22
51  */
52 public class ProcessManager {
53     private static final Logger LOGGER = LogManager.getLogger(ProcessManager.class);
54     private static final String PROCESS_PLUGIN_NAME = "/data/local/tmp/libprocessplugin.z.so";
55 
56     private boolean isRequestProcess = false;
57 
ProcessManager()58     private ProcessManager() {
59     }
60 
61     /**
62      * SingletonClassInstance
63      */
64     private static class SingletonClassInstance {
65         private static final ProcessManager INSTANCE = new ProcessManager();
66     }
67 
68     /**
69      * getInstance
70      *
71      * @return ProcessManager
72      */
getInstance()73     public static ProcessManager getInstance() {
74         return ProcessManager.SingletonClassInstance.INSTANCE;
75     }
76 
77     /**
78      * getProcessList
79      *
80      * @param deviceInfo deviceInfo
81      * @return List <ProcessInfo>
82      */
getProcessList(DeviceIPPortInfo deviceInfo)83     public List<ProcessInfo> getProcessList(DeviceIPPortInfo deviceInfo) {
84         if (ProfilerLogManager.isInfoEnabled()) {
85             LOGGER.info("getProcessList");
86         }
87         if (deviceInfo == null || StringUtils.isBlank(deviceInfo.getIp())) {
88             return new ArrayList<ProcessInfo>();
89         }
90         String deviceId = deviceInfo.getDeviceID();
91         Optional<DeviceIPPortInfo> deviceIPPortInfoOpt = new DeviceDao().getDeviceIPPortInfo(deviceId);
92         List<ProcessInfo> processInfos = new ArrayList<>();
93         if (deviceIPPortInfoOpt.isPresent()) {
94             DeviceIPPortInfo deviceIPPortInfo = deviceIPPortInfoOpt.get();
95             ProfilerServiceTypes.GetCapabilitiesResponse response = HiProfilerClient.getInstance()
96                 .getCapabilities(deviceIPPortInfo.getIp(), deviceIPPortInfo.getForwardPort());
97             if (Objects.nonNull(response)) {
98                 List<ProfilerServiceTypes.ProfilerPluginCapability> capabilities =
99                     Objects.requireNonNull(response).getCapabilitiesList();
100                 Optional<ProfilerServiceTypes.ProfilerPluginCapability> libPlugin = getLibPlugin(capabilities);
101                 if (libPlugin.isPresent()) {
102                     isRequestProcess = true;
103                     int sessionId = processListCreateSession(deviceIPPortInfo.getForwardPort(),
104                         libPlugin.get().getName(), 0, true, deviceInfo.getDeviceType());
105                     if (sessionId == -1) {
106                         if (ProfilerLogManager.isErrorEnabled()) {
107                             LOGGER.error("createSession failed");
108                         }
109                         return processInfos;
110                     }
111                     HiProfilerClient.getInstance()
112                         .keepSession(deviceIPPortInfo.getIp(), deviceIPPortInfo.getForwardPort(), sessionId);
113                     boolean startResult = HiProfilerClient.getInstance()
114                         .requestStartSession(deviceIPPortInfo.getIp(), deviceIPPortInfo.getForwardPort(), sessionId);
115                     if (startResult) {
116                         processInfos = HiProfilerClient.getInstance()
117                             .fetchProcessData(deviceIPPortInfo.getIp(), deviceIPPortInfo.getForwardPort(), sessionId);
118                     }
119                     HiProfilerClient.getInstance().requestStopSession(deviceIPPortInfo.getIp(), deviceIPPortInfo
120                         .getForwardPort(), sessionId, false);
121                     HiProfilerClient.getInstance()
122                         .requestDestroySession(deviceIPPortInfo.getIp(), deviceIPPortInfo.getForwardPort(), sessionId);
123                     return processInfos;
124                 }
125             }
126         }
127         return processInfos;
128     }
129 
130     /**
131      * createSession for ListProcess
132      *
133      * @param port port number
134      * @param name name
135      * @param pid pid
136      * @param reportProcessTree report process tree
137      * @param deviceType DeviceType
138      * @return int
139      */
processListCreateSession(int port, String name, int pid, boolean reportProcessTree, DeviceType deviceType)140     public int processListCreateSession(int port, String name, int pid, boolean reportProcessTree,
141         DeviceType deviceType) {
142         if (ProfilerLogManager.isInfoEnabled()) {
143             LOGGER.info("processListCreateSession port {}", port);
144         }
145         if (port <= 0 || port > LayoutConstants.PORT) {
146             return -1;
147         }
148         ProcessPluginConfig.ProcessConfig plug =
149             ProcessPluginConfig.ProcessConfig.newBuilder().setReportProcessTree(true).build();
150         ProfilerServiceTypes.ProfilerSessionConfig sessionConfig = ProfilerServiceHelper
151             .profilerSessionConfig(true, null, 10,
152                 ProfilerServiceTypes.ProfilerSessionConfig.BufferConfig.Policy.RECYCLE, 5000);
153         String sha256 = "";
154         CommonTypes.ProfilerPluginConfig plugConfig =
155             ProfilerServiceHelper.profilerPluginConfig(name, sha256, 2, plug.toByteString());
156         List<CommonTypes.ProfilerPluginConfig> plugs = new ArrayList();
157         plugs.add(plugConfig);
158         ProfilerServiceTypes.CreateSessionRequest request =
159             ProfilerServiceHelper.createSessionRequest(CommonUtil.getRequestId(), sessionConfig, plugs);
160         ProfilerServiceTypes.CreateSessionResponse response = null;
161         ProfilerClient client = HiProfilerClient.getInstance().getProfilerClient("", port);
162         try {
163             response = client.createSession(request);
164             LOGGER.info("process Session start444 {} ", DateTimeUtil.getNowTimeLong());
165         } catch (StatusRuntimeException exception) {
166             if (ProfilerLogManager.isErrorEnabled()) {
167                 LOGGER.error("processListCreateSession ", exception);
168             }
169             return -1;
170         }
171         return response.getSessionId();
172     }
173 
getLibPlugin( List<ProfilerServiceTypes.ProfilerPluginCapability> capabilities)174     private Optional<ProfilerServiceTypes.ProfilerPluginCapability> getLibPlugin(
175         List<ProfilerServiceTypes.ProfilerPluginCapability> capabilities) {
176         if (ProfilerLogManager.isInfoEnabled()) {
177             LOGGER.info("getLibPlugin {}", capabilities);
178         }
179         return capabilities.stream()
180             .filter(profilerPluginCapability -> profilerPluginCapability.getName().contains(PROCESS_PLUGIN_NAME))
181             .findFirst();
182     }
183 
setIsRequest(boolean isRequestProcess)184     public void setIsRequest(boolean isRequestProcess) {
185         this.isRequestProcess = isRequestProcess;
186     }
187 
188     /**
189      * getDebuggerProcessList
190      *
191      * @param deviceInfo deviceInfo
192      * @return List<ProcessInfo>
193      */
getDebuggerProcessList(DeviceIPPortInfo deviceInfo)194     public List<ProcessInfo> getDebuggerProcessList(DeviceIPPortInfo deviceInfo) {
195         return new ArrayList<>();
196     }
197 
198     /**
199      * check Process is Debug
200      *
201      * @param deviceInfo deviceInfo
202      * @param processInfo processInfo
203      * @return boolean
204      */
checkIsDebuggerProcess(DeviceIPPortInfo deviceInfo, ProcessInfo processInfo)205     public boolean checkIsDebuggerProcess(DeviceIPPortInfo deviceInfo, ProcessInfo processInfo) {
206         return false;
207     }
208 
209     /**
210      * isRequestProcess
211      *
212      * @return boolean
213      */
isRequestProcess()214     public boolean isRequestProcess() {
215         return isRequestProcess;
216     }
217 }
218