CpuProfilerStage extends StreamingStage implements CodeNavigator.Listener { public void startCapturing() { ProfilingConfiguration config = myProfilerConfigModel.getProfilingConfiguration(); assert getStudioProfilers().getProcess() != null; Common.Process process = getStudioProfilers().getProcess(); String traceFilePath = String.format("%s/%s-%d.trace", DAEMON_DEVICE_DIR_PATH, process.getName(), System.nanoTime()); if (config.isDisableLiveAllocation()) { getStudioProfilers().setMemoryLiveAllocationEnabled(false); } setCaptureState(CaptureState.STARTING); Cpu.CpuTraceConfiguration configuration = Cpu.CpuTraceConfiguration.newBuilder() .setAppName(process.getName()) .setAbiCpuArch(process.getAbiCpuArch()) .setInitiationType(TraceInitiationType.INITIATED_BY_UI) .setTempPath(traceFilePath) .setUserOptions(config.toProto()) .addAllSymbolDirs(getStudioProfilers().getIdeServices().getNativeSymbolsDirectories()) .build(); if (getStudioProfilers().getIdeServices().getFeatureConfig().isUnifiedPipelineEnabled()) { Commands.Command startCommand = Commands.Command.newBuilder() .setStreamId(mySession.getStreamId()) .setPid(mySession.getPid()) .setType(Commands.Command.CommandType.START_CPU_TRACE) .setStartCpuTrace(Cpu.StartCpuTrace.newBuilder().setConfiguration(configuration).build()) .build(); Transport.ExecuteResponse response = getStudioProfilers().getClient().getTransportClient().execute( Transport.ExecuteRequest.newBuilder().setCommand(startCommand).build()); TransportEventListener statusListener = new TransportEventListener(Common.Event.Kind.CPU_TRACE_STATUS, getStudioProfilers().getIdeServices().getMainExecutor(), event -> event.getCommandId() == response.getCommandId(), () -> mySession.getStreamId(), () -> mySession.getPid(), event -> { startCapturingCallback( event.getCpuTraceStatus().getTraceStartStatus()); return true; }); getStudioProfilers().getTransportPoller().registerListener(statusListener); } else { CpuProfilingAppStartRequest request = CpuProfilingAppStartRequest.newBuilder() .setSession(mySession) .setConfiguration(configuration) .build(); CompletableFuture.supplyAsync( () -> getCpuClient().startProfilingApp(request), getStudioProfilers().getIdeServices().getPoolExecutor()) .thenAcceptAsync(response -> this.startCapturingCallback(response.getStatus()), getStudioProfilers().getIdeServices().getMainExecutor()); } getStudioProfilers().getIdeServices().getTemporaryProfilerPreferences().setBoolean(HAS_USED_CPU_CAPTURE, true); myInstructionsEaseOutModel.setCurrentPercentage(1); } CpuProfilerStage(@NotNull StudioProfilers profilers);  CpuProfilerStage(@NotNull StudioProfilers profilers, @Nullable File importedTrace); @VisibleForTesting  CpuProfilerStage(@NotNull StudioProfilers profilers, @Nullable File importedTrace, @NotNull CpuCaptureParser captureParser); boolean isImportTraceMode(); boolean hasUserUsedCpuCapture(); @NotNull RangeSelectionModel getRangeSelectionModel(); @NotNull EaseOutModel getInstructionsEaseOutModel(); AxisComponentModel getCpuUsageAxis(); AxisComponentModel getThreadCountAxis(); AxisComponentModel getTimeAxisGuide(); DetailedCpuUsage getCpuUsage(); CpuStageLegends getLegends(); DurationDataModel<CpuTraceInfo> getTraceDurations(); String getName(); EventMonitor getEventMonitor(); @NotNull CpuProfilerConfigModel getProfilerConfigModel(); @Override void enter(); @Override void exit(); @NotNull UpdatableManager getUpdatableManager(); AspectModel<CpuProfilerAspect> getAspect(); void toggleCapturing(); void startCapturing(); long getCaptureStartTimeNs(); long getCaptureStopTimeNs(); @NotNull TraceIdsIterator getTraceIdsIterator(); @NotNull CpuCaptureParser getCaptureParser(); long currentTimeNs(); @VisibleForTesting void setCapture(@Nullable CpuCapture capture); void setAndSelectCapture(long traceId); @VisibleForTesting void setAndSelectCapture(@NotNull CpuCapture capture); int getSelectedThread(); void setSelectedThread(int id); @NotNull List<ClockType> getClockTypes(); @NotNull ClockType getClockType(); void setClockType(@NotNull ClockType clockType); @Nullable CpuCapture getCapture(); @NotNull CaptureState getCaptureState(); @NotNull TraceInitiationType getCaptureInitiationType(); void setCaptureState(@NotNull CaptureState captureState); @NotNull FilterResult applyCaptureFilter(@NotNull Filter filter); boolean isApiInitiatedTracingInProgress(); @NotNull CpuTraceDataSeries getCpuTraceDataSeries(); @NotNull CpuThreadsModel getThreadStates(); @NotNull CpuKernelModel getCpuKernelModel(); @NotNull CpuFramesModel getFramesModel(); @NotNull CaptureModel getCaptureModel(); void setCaptureDetails(@Nullable CaptureDetails.Type type); @Nullable CaptureDetails getCaptureDetails(); @Override void onNavigated(@NotNull CodeLocation location); static final int CPU_ART_STOP_TIMEOUT_SEC; }
@Test public void testStartCapturing() throws InterruptedException, IOException { assertThat(myStage.getCaptureState()).isEqualTo(CpuProfilerStage.CaptureState.IDLE); CpuProfilerTestUtils.startCapturing(myStage, myCpuService, myTransportService, true); CpuProfilerTestUtils.stopCapturing(myStage, myCpuService, myTransportService, true, CpuProfilerTestUtils.readValidTrace()); CpuProfilerTestUtils.startCapturing(myStage, myCpuService, myTransportService, false); }