• 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.transport.grpc;
17 
18 import com.google.protobuf.ByteString;
19 import ohos.devtools.datasources.transport.grpc.service.CommonTypes;
20 import ohos.devtools.datasources.transport.grpc.service.ProfilerServiceTypes;
21 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager;
22 import org.apache.commons.lang3.StringUtils;
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 
26 import java.util.List;
27 
28 /**
29  * Create the request object of the profiler Client
30  *
31  * @since 2021/5/19 16:39
32  */
33 public final class ProfilerServiceHelper {
34     private static final Logger LOGGER = LogManager.getLogger(ProfilerServiceHelper.class);
35 
ProfilerServiceHelper()36     private ProfilerServiceHelper() {
37     }
38 
39     /**
40      * Create a Create Session Request object for grpc request.
41      *
42      * @param requestId requestId
43      * @param sessionConfig Session config
44      * @param pluginConfigs Plugin configs
45      * @return ProfilerServiceTypes.CreateSessionRequest
46      */
createSessionRequest(int requestId, ProfilerServiceTypes.ProfilerSessionConfig sessionConfig, List<CommonTypes.ProfilerPluginConfig> pluginConfigs)47     public static ProfilerServiceTypes.CreateSessionRequest createSessionRequest(int requestId,
48         ProfilerServiceTypes.ProfilerSessionConfig sessionConfig,
49         List<CommonTypes.ProfilerPluginConfig> pluginConfigs) {
50         if (ProfilerLogManager.isInfoEnabled()) {
51             LOGGER.info("createSessionRequest");
52         }
53         ProfilerServiceTypes.CreateSessionRequest.Builder createBuilder =
54             ProfilerServiceTypes.CreateSessionRequest.newBuilder();
55         createBuilder.setRequestId(requestId);
56         if (sessionConfig != null) {
57             createBuilder.setSessionConfig(sessionConfig);
58         }
59         if (pluginConfigs != null) {
60             for (CommonTypes.ProfilerPluginConfig profilerPluginConfig : pluginConfigs) {
61                 createBuilder.addPluginConfigs(profilerPluginConfig);
62             }
63         }
64         return createBuilder.build();
65     }
66 
67     /**
68      * Create a start Session Request object for grpc request.
69      *
70      * @param requestId requestId
71      * @param sessionId sessionId
72      * @param pluginConfigs Plugin configs
73      * @return ProfilerServiceTypes.StartSessionRequest
74      */
startSessionRequest(int requestId, int sessionId, List<CommonTypes.ProfilerPluginConfig> pluginConfigs)75     public static ProfilerServiceTypes.StartSessionRequest startSessionRequest(int requestId, int sessionId,
76         List<CommonTypes.ProfilerPluginConfig> pluginConfigs) {
77         if (ProfilerLogManager.isInfoEnabled()) {
78             LOGGER.info("startSessionRequest");
79         }
80         ProfilerServiceTypes.StartSessionRequest.Builder startBuilder =
81             ProfilerServiceTypes.StartSessionRequest.newBuilder();
82         startBuilder.setRequestId(requestId);
83         startBuilder.setSessionId(sessionId);
84         if (pluginConfigs != null) {
85             pluginConfigs.forEach(profilerPluginConfig -> {
86                 startBuilder.addUpdateConfigs(profilerPluginConfig);
87             });
88         }
89         return startBuilder.build();
90     }
91 
92     /**
93      * Create a fetch Data Request object for grpc request
94      *
95      * @param requestId requestId
96      * @param sessionId sessionId
97      * @param addtionData addtionData not used temporarily, you can pass null
98      * @return ProfilerServiceTypes.FetchDataRequest
99      */
fetchDataRequest(int requestId, int sessionId, ByteString addtionData)100     public static ProfilerServiceTypes.FetchDataRequest fetchDataRequest(int requestId, int sessionId,
101         ByteString addtionData) {
102         if (ProfilerLogManager.isInfoEnabled()) {
103             LOGGER.info("fetchDataRequest");
104         }
105         ProfilerServiceTypes.FetchDataRequest.Builder builder = ProfilerServiceTypes.FetchDataRequest.newBuilder();
106         builder.setRequestId(requestId);
107         builder.setSessionId(sessionId);
108         if (addtionData != null) {
109             builder.setAddtionData(addtionData);
110         }
111         return builder.build();
112     }
113 
114     /**
115      * Create a stop Session Request object for grpc request.
116      *
117      * @param requestId requestId
118      * @param sessionId sessionId
119      * @return ProfilerServiceTypes.StopSessionRequest
120      */
stopSessionRequest(int requestId, int sessionId)121     public static ProfilerServiceTypes.StopSessionRequest stopSessionRequest(int requestId, int sessionId) {
122         if (ProfilerLogManager.isInfoEnabled()) {
123             LOGGER.info("stopSessionRequest");
124         }
125         ProfilerServiceTypes.StopSessionRequest.Builder builder = ProfilerServiceTypes.StopSessionRequest.newBuilder();
126         builder.setRequestId(requestId);
127         builder.setSessionId(sessionId);
128         return builder.build();
129     }
130 
131     /**
132      * Create a destroy Session Request object for grpc request
133      *
134      * @param requestId requestId
135      * @param sessionId sessionId
136      * @return ProfilerServiceTypes.DestroySessionRequest
137      */
destroySessionRequest(int requestId, int sessionId)138     public static ProfilerServiceTypes.DestroySessionRequest destroySessionRequest(int requestId, int sessionId) {
139         if (ProfilerLogManager.isInfoEnabled()) {
140             LOGGER.info("destroySessionRequest");
141         }
142         ProfilerServiceTypes.DestroySessionRequest.Builder builder =
143             ProfilerServiceTypes.DestroySessionRequest.newBuilder();
144         builder.setRequestId(requestId);
145         builder.setSessionId(sessionId);
146         return builder.build();
147     }
148 
149     /**
150      * Construct Session Config object
151      *
152      * @param online online Whether it is online mode, true online false offline
153      * @param resultFile resultFile
154      * @param pages pages
155      * @param value value
156      * @param keepTime keepTime
157      * @return ProfilerServiceTypes.ProfilerSessionConfig
158      */
profilerSessionConfig(boolean online, String resultFile, int pages, ProfilerServiceTypes.ProfilerSessionConfig.BufferConfig.Policy value, int keepTime)159     public static ProfilerServiceTypes.ProfilerSessionConfig profilerSessionConfig(boolean online, String resultFile,
160         int pages, ProfilerServiceTypes.ProfilerSessionConfig.BufferConfig.Policy value, int keepTime) {
161         if (ProfilerLogManager.isInfoEnabled()) {
162             LOGGER.info("profilerSessionConfig");
163         }
164         ProfilerServiceTypes.ProfilerSessionConfig.Builder builder =
165             ProfilerServiceTypes.ProfilerSessionConfig.newBuilder();
166         if (online) {
167             builder.setSessionMode(ProfilerServiceTypes.ProfilerSessionConfig.Mode.ONLINE);
168         } else {
169             builder.setSessionMode(ProfilerServiceTypes.ProfilerSessionConfig.Mode.OFFLINE);
170             builder.setResultFile(resultFile);
171         }
172         builder.setKeepAliveTime(keepTime);
173         ProfilerServiceTypes.ProfilerSessionConfig.BufferConfig.Builder build =
174             ProfilerServiceTypes.ProfilerSessionConfig.BufferConfig.newBuilder();
175         if (value != null && value.getNumber() > 0) {
176             build.setPolicy(value);
177         }
178         if (pages > 0) {
179             build.setPages(pages);
180         }
181         return builder.addBuffers(build.build()).build();
182     }
183 
184     /**
185      * Construct profiler Plugin Config object
186      *
187      * @param name name
188      * @param pluginSha256 pluginSha256
189      * @param sampleInterval sampleInterval
190      * @param confData confData Objects serialized by each plug-in
191      * @return CommonTypes.ProfilerPluginConfig
192      */
profilerPluginConfig(String name, String pluginSha256, int sampleInterval, ByteString confData)193     public static CommonTypes.ProfilerPluginConfig profilerPluginConfig(String name, String pluginSha256,
194         int sampleInterval, ByteString confData) {
195         if (ProfilerLogManager.isInfoEnabled()) {
196             LOGGER.info("profilerPluginConfig");
197         }
198         CommonTypes.ProfilerPluginConfig.Builder builder = CommonTypes.ProfilerPluginConfig.newBuilder();
199         if (StringUtils.isNotBlank(name)) {
200             builder.setName(name);
201         }
202         if (StringUtils.isNotBlank(pluginSha256)) {
203             builder.setPluginSha256(pluginSha256);
204         }
205         if (sampleInterval > 0) {
206             builder.setSampleInterval(sampleInterval);
207         }
208         if (confData != null) {
209             builder.setConfigData(confData);
210         }
211         return builder.build();
212     }
213 }
214