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.views.layout.dialog; 17 18 import com.intellij.ui.JBColor; 19 import com.intellij.ui.components.JBLabel; 20 import com.intellij.ui.components.JBPanel; 21 import ohos.devtools.datasources.transport.grpc.SystemTraceHelper; 22 import ohos.devtools.datasources.transport.hdc.HdcWrapper; 23 import ohos.devtools.datasources.utils.device.entity.DeviceIPPortInfo; 24 import ohos.devtools.datasources.utils.device.entity.DeviceType; 25 import ohos.devtools.datasources.utils.quartzmanager.QuartzManager; 26 import ohos.devtools.services.distribute.DistributedManager; 27 import ohos.devtools.views.common.LayoutConstants; 28 import ohos.devtools.views.distributed.DistributedPanel; 29 import ohos.devtools.views.distributed.bean.DistributedParams; 30 import ohos.devtools.views.layout.SystemPanel; 31 import ohos.devtools.views.layout.TaskPanel; 32 import ohos.devtools.views.layout.utils.EventTrackUtils; 33 import ohos.devtools.views.layout.utils.TraceStreamerUtils; 34 import ohos.devtools.views.trace.component.SysAnalystPanel; 35 import org.apache.logging.log4j.LogManager; 36 import org.apache.logging.log4j.Logger; 37 38 import javax.swing.JButton; 39 import javax.swing.JPanel; 40 import javax.swing.SwingUtilities; 41 import javax.swing.SwingWorker; 42 import javax.swing.Timer; 43 import java.awt.BorderLayout; 44 import java.awt.Dimension; 45 import java.awt.event.ActionEvent; 46 import java.awt.event.ActionListener; 47 import java.util.ArrayList; 48 import java.util.Locale; 49 import java.util.Objects; 50 import java.util.concurrent.ExecutionException; 51 import java.util.concurrent.ExecutorService; 52 import java.util.concurrent.LinkedBlockingQueue; 53 import java.util.concurrent.ThreadPoolExecutor; 54 import java.util.concurrent.TimeUnit; 55 56 import static ohos.devtools.datasources.transport.hdc.HdcCmdList.HDC_GET_TRACE_FILE; 57 import static ohos.devtools.datasources.transport.hdc.HdcCmdList.HDC_GET_TRACE_FILE_INFO; 58 import static ohos.devtools.datasources.transport.hdc.HdcCmdList.TRACE_STREAMER_LOAD; 59 import static ohos.devtools.datasources.transport.hdc.HdcStdCmdList.HDC_STD_GET_TRACE_FILE; 60 import static ohos.devtools.datasources.transport.hdc.HdcStdCmdList.HDC_STD_GET_TRACE_FILE_INFO; 61 import static ohos.devtools.datasources.transport.hdc.HdcWrapper.conversionCommand; 62 import static ohos.devtools.views.common.Constant.DISTRIBUTED_REFRESH; 63 import static ohos.devtools.views.common.Constant.IS_SUPPORT_NEW_HDC; 64 65 /** 66 * TraceRecordDialog 67 * 68 * @since 2021/10/25 69 */ 70 public class TraceRecordDialog { 71 private static final Logger LOGGER = LogManager.getLogger(TraceRecordDialog.class); 72 73 private int bytraceFileSize; 74 private Timer timerLoading; 75 private DeviceIPPortInfo deviceIPPortInfo; 76 private String sessionId; 77 private String fileExtension; 78 private Boolean analysisState = false; 79 private Boolean pullBytraceFileState = false; 80 private Boolean chooseMode; 81 private DistributedManager distributedManager; 82 83 private JBLabel statusAnalysisJLabel = new JBLabel("Status"); 84 private JBLabel durationAnalysisJLabel = new JBLabel("Duration"); 85 private JBLabel loadingJLabel = new JBLabel("Loading"); 86 private JBLabel loadingInitTimeJLabel = new JBLabel(" 00:00:00"); 87 private int hoursLoading = 0; 88 private int minutesLoading = 0; 89 private int secondsLoading = 0; 90 private TaskPanel taskPanel = null; 91 private JPanel countPanel = new JPanel(null); 92 private JBLabel statusJLabel = new JBLabel("Status"); 93 private JBLabel durationJLabel = new JBLabel("Duration"); 94 private JBLabel recordingJLabel = new JBLabel("Recording"); 95 private JBLabel timeJLabel = new JBLabel(); 96 private JButton stopJButton = new JButton("Stop"); 97 private TraceLoadDialog traceLoadDialog; 98 private AnalysisDialog analysisDialogEvent; 99 100 /** 101 * load 102 * 103 * @param taskPanel taskPanel 104 * @param maxDurationParam maxDurationParam 105 * @param sessionIdParam sessionIdParam 106 * @param deviceIPPortInfoParam deviceIPPortInfoParam 107 * @param chooseMode chooseMode 108 */ load(TaskPanel taskPanel, int maxDurationParam, String sessionIdParam, DeviceIPPortInfo deviceIPPortInfoParam, boolean chooseMode)109 public void load(TaskPanel taskPanel, int maxDurationParam, String sessionIdParam, 110 DeviceIPPortInfo deviceIPPortInfoParam, boolean chooseMode) { 111 this.taskPanel = taskPanel; 112 this.sessionId = sessionIdParam; 113 this.deviceIPPortInfo = deviceIPPortInfoParam; 114 this.chooseMode = chooseMode; 115 timeJLabel.setText(" 00:00:00"); 116 this.fileExtension = chooseMode ? ".bytrace" : ".htrace"; 117 traceLoadDialog = new TraceLoadDialog(countPanel, timeJLabel, maxDurationParam); 118 stopJButton.addActionListener(new ActionListener() { 119 @Override 120 public void actionPerformed(ActionEvent actionEvent) { 121 traceLoadDialog.getTimer().stop(); 122 traceLoadDialog.doCancelAction(); 123 } 124 }); 125 countPanel.setPreferredSize(new Dimension(300, 150)); 126 statusJLabel.setForeground(JBColor.foreground().brighter()); 127 durationJLabel.setForeground(JBColor.foreground().brighter()); 128 recordingJLabel.setForeground(JBColor.foreground().brighter()); 129 timeJLabel.setForeground(JBColor.foreground().brighter()); 130 traceLoadDialog.setLableAttribute(statusJLabel, durationJLabel, recordingJLabel, timeJLabel, stopJButton); 131 countPanel.add(statusJLabel); 132 countPanel.add(durationJLabel); 133 countPanel.add(recordingJLabel); 134 countPanel.add(timeJLabel); 135 countPanel.add(stopJButton); 136 traceLoadDialog.show(); 137 int exitCode = traceLoadDialog.getExitCode(); 138 traceLoadDialog.getTimer().stop(); 139 if (exitCode == 0) { 140 this.loading(); 141 ExecutorService executorCancel = 142 new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); 143 executorCancel.execute(new Runnable() { 144 @Override 145 public void run() { 146 SystemTraceHelper.getSingleton().stopAndDestroySession(deviceIPPortInfo, sessionId); 147 } 148 }); 149 } else { 150 SystemTraceHelper.getSingleton().stopAndDestroySession(deviceIPPortInfo, sessionId); 151 } 152 } 153 154 /** 155 * load 156 * 157 * @param taskPanel taskPanel 158 * @param distributedManager distributedManager 159 * @param chooseMode chooseMode 160 */ load(TaskPanel taskPanel, DistributedManager distributedManager, boolean chooseMode)161 public void load(TaskPanel taskPanel, DistributedManager distributedManager, boolean chooseMode) { 162 this.taskPanel = taskPanel; 163 this.chooseMode = chooseMode; 164 this.distributedManager = distributedManager; 165 timeJLabel.setText(" 00:00:00"); 166 this.fileExtension = chooseMode ? ".bytrace" : ".htrace"; 167 traceLoadDialog = new TraceLoadDialog(countPanel, timeJLabel, distributedManager.getMaxDurationParam()); 168 stopJButton.addActionListener(new ActionListener() { 169 @Override 170 public void actionPerformed(ActionEvent actionEvent) { 171 traceLoadDialog.getTimer().stop(); 172 traceLoadDialog.doCancelAction(); 173 distributedManager.cancelCollection(); 174 } 175 }); 176 countPanel.setPreferredSize(new Dimension(300, 150)); 177 statusJLabel.setForeground(JBColor.foreground().brighter()); 178 durationJLabel.setForeground(JBColor.foreground().brighter()); 179 recordingJLabel.setForeground(JBColor.foreground().brighter()); 180 timeJLabel.setForeground(JBColor.foreground().brighter()); 181 traceLoadDialog.setLableAttribute(statusJLabel, durationJLabel, recordingJLabel, timeJLabel, stopJButton); 182 countPanel.add(statusJLabel); 183 countPanel.add(durationJLabel); 184 countPanel.add(recordingJLabel); 185 countPanel.add(timeJLabel); 186 countPanel.add(stopJButton); 187 traceLoadDialog.show(); 188 int exitCode = traceLoadDialog.getExitCode(); 189 traceLoadDialog.getTimer().stop(); 190 if (exitCode == 0) { 191 distributedLoading(); 192 ExecutorService executorCancel = 193 new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); 194 executorCancel.execute(new Runnable() { 195 @Override 196 public void run() { 197 distributedManager.stopCollection(); 198 } 199 }); 200 } else { 201 distributedManager.cancelCollection(); 202 } 203 } 204 205 /** 206 * loading 207 */ distributedLoading()208 public void distributedLoading() { 209 pullBytraceFileState = true; 210 analysisDialogEvent = new AnalysisDialog(null, countPanel) { 211 @Override 212 public void doCancelAction() { 213 super.doCancelAction(); 214 if (Objects.nonNull(timerLoading)) { 215 timerLoading.stop(); 216 } 217 } 218 }; 219 timerLoading = new Timer(LayoutConstants.NUMBER_THREAD, this::distributedActionPerformedLoading); 220 countPanel.setPreferredSize(new Dimension(300, 150)); 221 statusAnalysisJLabel.setForeground(JBColor.foreground().brighter()); 222 durationAnalysisJLabel.setForeground(JBColor.foreground().brighter()); 223 loadingJLabel.setForeground(JBColor.foreground().brighter()); 224 loadingInitTimeJLabel.setForeground(JBColor.foreground().brighter()); 225 traceLoadDialog 226 .setLableAttribute(statusAnalysisJLabel, durationAnalysisJLabel, loadingJLabel, loadingInitTimeJLabel, 227 null); 228 countPanel.removeAll(); 229 countPanel.add(statusAnalysisJLabel); 230 countPanel.add(durationAnalysisJLabel); 231 countPanel.add(loadingJLabel); 232 countPanel.add(loadingInitTimeJLabel); 233 countPanel.repaint(); 234 timerLoading.start(); 235 analysisDialogEvent.show(); 236 } 237 238 /** 239 * actionLoadingPerformed 240 * 241 * @param actionEvent actionEvent 242 */ distributedActionPerformedLoading(ActionEvent actionEvent)243 public void distributedActionPerformedLoading(ActionEvent actionEvent) { 244 if (secondsLoading <= LayoutConstants.NUMBER_SECONDS) { 245 loadingInitTimeJLabel.setText(" " + String.format(Locale.ENGLISH, "%02d", hoursLoading) + ":" + String 246 .format(Locale.ENGLISH, "%02d", minutesLoading) + ":" + String 247 .format(Locale.ENGLISH, "%02d", secondsLoading)); 248 secondsLoading++; 249 if (secondsLoading > LayoutConstants.NUMBER_SECONDS) { 250 secondsLoading = 0; 251 minutesLoading++; 252 if (minutesLoading > LayoutConstants.NUMBER_SECONDS) { 253 minutesLoading = 0; 254 hoursLoading++; 255 } 256 } 257 int num = secondsLoading % 2; 258 if (pullBytraceFileState && num == 0) { 259 boolean has = distributedManager.hasFile(); 260 if (has) { 261 pullBytraceFileState = false; 262 distributedPullAndAnalysisFile(); 263 } 264 } 265 } 266 if (analysisState) { 267 timerLoading.stop(); 268 analysisDialogEvent.close(1); 269 } 270 } 271 272 /** 273 * pull and analysis bytrace file 274 */ distributedPullAndAnalysisFile()275 public void distributedPullAndAnalysisFile() { 276 ExecutorService executor = 277 new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); 278 executor.execute(new Runnable() { 279 @Override 280 public void run() { 281 new SwingWorker<Boolean, Object>() { 282 @Override 283 protected Boolean doInBackground() { 284 boolean result = distributedManager.pullTraceFile(); 285 if (result) { 286 return distributedManager.analysisFileTraceFile(); 287 } 288 return false; 289 } 290 291 @Override 292 protected void done() { 293 SwingUtilities.invokeLater(new Runnable() { 294 @Override 295 public void run() { 296 try { 297 if (get()) { 298 analysisState = true; 299 if (chooseMode) { 300 distributedManager.cancelCollection(); 301 } 302 if (analysisDialogEvent.isShowing()) { 303 JBPanel tabContainer = taskPanel.getTabContainer(); 304 tabContainer.removeAll(); 305 DistributedPanel component = new DistributedPanel(); 306 DistributedParams distributedParams = 307 distributedManager.getDistributedParams(); 308 component.load(distributedParams); 309 EventTrackUtils.getInstance().trackDistributedPage(); 310 tabContainer.setBackground(JBColor.background()); 311 tabContainer.add(component, BorderLayout.CENTER); 312 taskPanel.getTabContainer().add(component, BorderLayout.CENTER); 313 taskPanel.repaint(); 314 } 315 } 316 } catch (InterruptedException | ExecutionException exception) { 317 LOGGER.error(" ExecutionException {}", exception.getMessage()); 318 } 319 } 320 }); 321 } 322 }.execute(); 323 } 324 }); 325 QuartzManager.getInstance().deleteExecutor(DISTRIBUTED_REFRESH); 326 } 327 328 /** 329 * loading 330 */ loading()331 public void loading() { 332 pullBytraceFileState = true; 333 analysisDialogEvent = new AnalysisDialog(deviceIPPortInfo, countPanel) { 334 @Override 335 public void doCancelAction() { 336 super.doCancelAction(); 337 if (Objects.nonNull(timerLoading)) { 338 timerLoading.stop(); 339 } 340 } 341 }; 342 timerLoading = new Timer(LayoutConstants.NUMBER_THREAD, this::actionPerformedLoading); 343 countPanel.setPreferredSize(new Dimension(300, 150)); 344 statusAnalysisJLabel.setForeground(JBColor.foreground().brighter()); 345 durationAnalysisJLabel.setForeground(JBColor.foreground().brighter()); 346 loadingJLabel.setForeground(JBColor.foreground().brighter()); 347 loadingInitTimeJLabel.setForeground(JBColor.foreground().brighter()); 348 traceLoadDialog 349 .setLableAttribute(statusAnalysisJLabel, durationAnalysisJLabel, loadingJLabel, loadingInitTimeJLabel, 350 null); 351 countPanel.removeAll(); 352 countPanel.add(statusAnalysisJLabel); 353 countPanel.add(durationAnalysisJLabel); 354 countPanel.add(loadingJLabel); 355 countPanel.add(loadingInitTimeJLabel); 356 countPanel.repaint(); 357 timerLoading.start(); 358 analysisDialogEvent.show(); 359 } 360 361 /** 362 * actionLoadingPerformed 363 * 364 * @param actionEvent actionEvent 365 */ actionPerformedLoading(ActionEvent actionEvent)366 public void actionPerformedLoading(ActionEvent actionEvent) { 367 if (secondsLoading <= LayoutConstants.NUMBER_SECONDS) { 368 loadingInitTimeJLabel.setText(" " + String.format(Locale.ENGLISH, "%02d", hoursLoading) + ":" + String 369 .format(Locale.ENGLISH, "%02d", minutesLoading) + ":" + String 370 .format(Locale.ENGLISH, "%02d", secondsLoading)); 371 secondsLoading++; 372 if (secondsLoading > LayoutConstants.NUMBER_SECONDS) { 373 secondsLoading = 0; 374 minutesLoading++; 375 if (minutesLoading > LayoutConstants.NUMBER_SECONDS) { 376 minutesLoading = 0; 377 hoursLoading++; 378 } 379 } 380 int num = secondsLoading % 2; 381 if (pullBytraceFileState && num == 0) { 382 String filePath = "/data/local/tmp/hiprofiler_data" + fileExtension; 383 ArrayList getBytraceFileInfoCmd; 384 if (IS_SUPPORT_NEW_HDC && deviceIPPortInfo.getDeviceType() == DeviceType.LEAN_HOS_DEVICE) { 385 getBytraceFileInfoCmd = 386 conversionCommand(HDC_STD_GET_TRACE_FILE_INFO, deviceIPPortInfo.getDeviceID(), filePath); 387 } else { 388 getBytraceFileInfoCmd = 389 conversionCommand(HDC_GET_TRACE_FILE_INFO, deviceIPPortInfo.getDeviceID(), filePath); 390 } 391 String bytraceFileInfo = HdcWrapper.getInstance().getHdcStringResult(getBytraceFileInfoCmd); 392 if (bytraceFileInfo != null && bytraceFileInfo.length() > 0) { 393 String[] bytraceFileInfoArray = bytraceFileInfo.split("\t"); 394 LOGGER.info("trace file size: {}", bytraceFileInfoArray[0]); 395 if (bytraceFileSize != 0 && bytraceFileSize == Integer.valueOf(bytraceFileInfoArray[0])) { 396 pullBytraceFileState = false; 397 pullAndAnalysisBytraceFile(); 398 } else { 399 bytraceFileSize = Integer.valueOf(bytraceFileInfoArray[0]); 400 } 401 } 402 } 403 } 404 if (analysisState) { 405 timerLoading.stop(); 406 } 407 } 408 409 /** 410 * pull and analysis bytrace file 411 */ pullAndAnalysisBytraceFile()412 public void pullAndAnalysisBytraceFile() { 413 ExecutorService executor = 414 new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); 415 executor.execute(new Runnable() { 416 @Override 417 public void run() { 418 new SwingWorker<String, Object>() { 419 @Override 420 protected String doInBackground() { 421 getBytraceFile(); 422 String baseDir = TraceStreamerUtils.getInstance().getBaseDir(); 423 String fileDir = TraceStreamerUtils.getInstance().getCreateFileDir(); 424 String dir = fileDir + "hiprofiler_data" + fileExtension; 425 String dbPath = TraceStreamerUtils.getInstance().getDbPath(); 426 ArrayList arrayList = conversionCommand(TRACE_STREAMER_LOAD, 427 baseDir + TraceStreamerUtils.getInstance().getTraceStreamerApp(), dir, dbPath); 428 HdcWrapper.getInstance().getHdcStringResult(arrayList); 429 return TraceStreamerUtils.getInstance().getDbPath(); 430 } 431 432 @Override 433 protected void done() { 434 SwingUtilities.invokeLater(new Runnable() { 435 @Override 436 public void run() { 437 try { 438 analysisState = true; 439 analysisDialogEvent.close(1); 440 JBPanel tabContainer = taskPanel.getTabContainer(); 441 tabContainer.removeAll(); 442 SysAnalystPanel component = new SysAnalystPanel(); 443 String dbPath = get(); 444 component.load(dbPath, true); 445 tabContainer.setBackground(JBColor.background()); 446 SystemPanel systemTuningPanel = new SystemPanel(tabContainer, component); 447 tabContainer.add(systemTuningPanel, BorderLayout.NORTH); 448 tabContainer.add(component, BorderLayout.CENTER); 449 taskPanel.getTabContainer().add(component, BorderLayout.CENTER); 450 taskPanel.repaint(); 451 } catch (InterruptedException | ExecutionException exception) { 452 LOGGER.error(" ExecutionException ", exception); 453 } 454 } 455 }); 456 } 457 }.execute(); 458 } 459 }); 460 } 461 462 /** 463 * get bytrace file 464 */ getBytraceFile()465 public void getBytraceFile() { 466 String filePath = "/data/local/tmp/hiprofiler_data" + fileExtension; 467 ArrayList cmd; 468 if (IS_SUPPORT_NEW_HDC && deviceIPPortInfo.getDeviceType() == DeviceType.LEAN_HOS_DEVICE) { 469 cmd = conversionCommand(HDC_STD_GET_TRACE_FILE, deviceIPPortInfo.getDeviceID(), filePath, 470 TraceStreamerUtils.getInstance().getCreateFileDir()); 471 } else { 472 cmd = conversionCommand(HDC_GET_TRACE_FILE, deviceIPPortInfo.getDeviceID(), filePath, 473 TraceStreamerUtils.getInstance().getCreateFileDir()); 474 } 475 HdcWrapper.getInstance().execCmdBy(cmd); 476 LOGGER.info(cmd); 477 } 478 } 479