public static GradleBuildVariant.Builder addVariant( @NonNull String projectPath, @NonNull String variantName) { GradleBuildVariant.Builder properties = GradleBuildVariant.newBuilder(); get().addVariant(projectPath, variantName, properties); return properties; }
synchronized ProcessProfileWriter get() { if (processProfileWriter == null) { Preconditions.checkState( profileOutputFile != null, "call setProfileOutputFile() first"); if (mLogger == null) { mLogger = new StdLogger(StdLogger.Level.INFO); } initializeAnalytics(mLogger, mScheduledExecutorService); processProfileWriter = new ProcessProfileWriter(profileOutputFile); } return processProfileWriter; } }
public static void recordMemorySample() { get().createAndRecordMemorySample(); }
/** Properties and statistics global to this build invocation. */ @NonNull public static GradleBuildProfile.Builder getGlobalProperties() { return get().getProperties(); }
@NonNull public static GradleBuildProject.Builder getProject(@NonNull String projectPath) { return get().mProjects.getUnchecked(projectPath).properties; }
ProcessProfileWriter.addVariant(project.getPath(), variantData.getName()) .setIsDebug(variantConfig.getBuildType().isDebuggable()) .setUseJack(variantConfig.getJackOptions().isEnabled())
ProcessProfileWriter(@NonNull Path benchmarkProfileOutputFile) { mBenchmarkProfileOutputFile = benchmarkProfileOutputFile; mNameAnonymizer = new NameAnonymizer(); mBuild = GradleBuildProfile.newBuilder(); mStartMemoryStats = createAndRecordMemorySample(); mProjects = CacheBuilder.newBuilder().build(new ProjectCacheLoader(mNameAnonymizer)); spans = new ConcurrentLinkedQueue<>(); }
private void recordMetrics() { long metricsStartTime = System.nanoTime(); GradleBuildProjectMetrics.Builder metrics = GradleBuildProjectMetrics.newBuilder(); Long apkSize = getSize(getOutputFile()); if (apkSize != null) { metrics.setApkSize(apkSize); } Long resourcesApSize = getSize(getResourceFile()); if (resourcesApSize != null) { metrics.setResourcesApSize(resourcesApSize); } metrics.setMetricsTimeNs(System.nanoTime() - metricsStartTime); ProcessProfileWriter.getProject(getProject().getPath()).setMetrics(metrics); }
public static void shutdown() throws InterruptedException { synchronized (LOCK) { if (sINSTANCE.isInitialized()) { sINSTANCE.processProfileWriter.finish(); } sINSTANCE.processProfileWriter = null; } }
private static void setGlobalProperties( @NonNull ProcessProfileWriter recorder, @NonNull File projectPath, @NonNull String gradleVersion, @NonNull ILogger logger) { recorder.getProperties() .setOsName(Strings.nullToEmpty(System.getProperty("os.name"))) .setOsVersion(Strings.nullToEmpty(System.getProperty("os.version"))) .setJavaVersion(Strings.nullToEmpty(System.getProperty("java.version"))) .setJavaVmVersion(Strings.nullToEmpty(System.getProperty("java.vm.version"))) .setMaxMemory(Runtime.getRuntime().maxMemory()) .setGradleVersion(Strings.nullToEmpty(gradleVersion)); String anonymizedProjectId; try { anonymizedProjectId = Anonymizer.anonymizeUtf8(logger, projectPath.getAbsolutePath()); } catch (IOException e) { anonymizedProjectId = "*ANONYMIZATION_ERROR*"; } recorder.getProperties().setProjectId(anonymizedProjectId); }
@VisibleForTesting public static void initializeForTests(@NonNull Path profileOutputFile) { sINSTANCE = new ProcessProfileWriterFactory(); sINSTANCE.setProfileOutputFile(profileOutputFile); ProcessProfileWriter recorder = sINSTANCE.get(); // Initialize the ProcessProfileWriter instance recorder.resetForTests(); setGlobalProperties(recorder, new File("fake/path/to/test_project/"), "2.10", new StdLogger(StdLogger.Level.VERBOSE)); }
@TaskAction public void executeAction() { if (instantRunBuildContext.getBuildHasFailed()) { try { instantRunBuildContext.writeTmpBuildInfo(tmpBuildInfoFile); } catch (ParserConfigurationException | IOException e) { throw new RuntimeException("Exception while saving temp-build-info.xml", e); } return; } // done with the instant run context. instantRunBuildContext.close(); try { String xml = instantRunBuildContext.toXml(); if (logger.isEnabled(LogLevel.DEBUG)) { logger.debug("build-id $1$l, build-info.xml : %2$s", instantRunBuildContext.getBuildId(), xml); } Files.createParentDirs(buildInfoFile); Files.write(xml, buildInfoFile, Charsets.UTF_8); } catch (Exception e) { throw new RuntimeException("Exception while saving build-info.xml", e); } // Record instant run status in analytics for this build ProcessProfileWriter.getGlobalProperties() .setInstantRunStatus( InstantRunAnalyticsHelper.generateAnalyticsProto(instantRunBuildContext)); }
@Override public void afterExecute(@NonNull Task task, @NonNull TaskState taskState) { GradleBuildProfileSpan.Builder record = taskRecords.remove(task.getPath()); record.setDurationInMs(System.currentTimeMillis() - record.getStartTimeInMs()); //noinspection ThrowableResultOfMethodCallIgnored Just logging the failure. record.setTask( GradleTaskExecution.newBuilder() .setType(AnalyticsUtil.getTaskExecutionType(task.getClass())) .setDidWork(taskState.getDidWork()) .setSkipped(taskState.getSkipped()) .setUpToDate(taskState.getUpToDate()) .setFailed(taskState.getFailure() != null)); recordWriter.writeRecord(task.getProject().getPath(), getVariantName(task), record); ProcessProfileWriter.recordMemorySample(); }
@Override public void record( @NonNull ExecutionType executionType, @NonNull String projectPath, @Nullable String variant, @NonNull VoidBlock block) { ProfileRecordWriter profileRecordWriter = ProcessProfileWriter.get(); GradleBuildProfileSpan.Builder currentRecord = create(profileRecordWriter, executionType, null); try { block.call(); } catch (IOException e) { throw new UncheckedIOException(e); } finally { write(profileRecordWriter, currentRecord, projectPath, variant); } }
GradleBuildMemorySample memoryStats = createAndRecordMemorySample(); mBuild.setBuildTime( memoryStats.getTimestamp() - mStartMemoryStats.getTimestamp());
ProcessProfileWriter.getProject(projectPath) .setAtoms( variantData
/** * Initialize the {@link ProcessProfileWriterFactory}. Idempotent. * * @param project the current Gradle {@link Project}. */ public static void init(@NonNull Project project) { synchronized (LOCK) { //noinspection VariableNotUsedInsideIf if (recordingBuildListener != null) { return; } ProcessProfileWriterFactory.initialize( project.getRootProject().getProjectDir(), project.getGradle().getGradleVersion(), new LoggerWrapper(project.getLogger()), new File(project.getRootProject().getBuildDir(), "android-profile")); recordingBuildListener = new RecordingBuildListener(ProcessProfileWriter.get()); project.getGradle().addListener(recordingBuildListener); } project.getGradle().addListener(new ProfileShutdownListener(project)); }
@Nullable @Override public <T> T record( @NonNull ExecutionType executionType, @Nullable GradleTransformExecution transform, @NonNull String projectPath, @Nullable String variant, @NonNull Block<T> block) { ProfileRecordWriter profileRecordWriter = ProcessProfileWriter.get(); GradleBuildProfileSpan.Builder currentRecord = create(profileRecordWriter, executionType, transform); try { return block.call(); } catch (Exception e) { block.handleException(e); } finally { write(profileRecordWriter, currentRecord, projectPath, variant); } // we always return null when an exception occurred and was not rethrown. return null; }