|
| 1 | +package analysis; |
| 2 | + |
| 3 | +import analysis.monitoring.TaskCompletionMonitor; |
| 4 | +import analysis.strategies.AnalysisStrategy; |
| 5 | +import datasets.Repository; |
| 6 | +import diff.GitDiffer; |
| 7 | +import diff.difftree.serialize.DiffTreeLineGraphExportOptions; |
| 8 | +import metadata.Metadata; |
| 9 | +import mining.MiningTask; |
| 10 | +import org.eclipse.jgit.revwalk.RevCommit; |
| 11 | +import org.tinylog.Logger; |
| 12 | +import org.variantsync.functjonal.iteration.ClusteredIterator; |
| 13 | +import org.variantsync.functjonal.iteration.MappedIterator; |
| 14 | +import parallel.ScheduledTasksIterator; |
| 15 | +import util.Clock; |
| 16 | +import util.Diagnostics; |
| 17 | +import util.InvocationCounter; |
| 18 | + |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.util.Iterator; |
| 22 | +import java.util.List; |
| 23 | +import java.util.function.Consumer; |
| 24 | + |
| 25 | +public record HistoryAnalysis( |
| 26 | + List<Repository> repositoriesToAnalyze, |
| 27 | + Path outputDir, |
| 28 | + int commitsToProcessPerThread, |
| 29 | + CommitHistoryAnalysisTaskFactory whatToDo, |
| 30 | + Consumer<Path> postProcessingOnRepositoryOutputDir |
| 31 | +) { |
| 32 | + public static final String TOTAL_RESULTS_FILE_NAME = "totalresult" + AnalysisResult.EXTENSION; |
| 33 | + public static final int COMMITS_TO_PROCESS_PER_THREAD_DEFAULT = 1000; |
| 34 | + |
| 35 | + @Deprecated |
| 36 | + public static void mine( |
| 37 | + final Repository repo, |
| 38 | + final Path outputDir, |
| 39 | + final DiffTreeLineGraphExportOptions exportOptions, |
| 40 | + final AnalysisStrategy strategy) |
| 41 | + { |
| 42 | + AnalysisResult totalResult; |
| 43 | + final GitDiffer differ = new GitDiffer(repo); |
| 44 | + final Clock clock = new Clock(); |
| 45 | + |
| 46 | + // prepare tasks |
| 47 | + Logger.info(">>> Scheduling synchronous mining"); |
| 48 | + clock.start(); |
| 49 | + List<RevCommit> commitsToProcess = differ.yieldRevCommits().toList(); |
| 50 | + final CommitHistoryAnalysisTask task = new MiningTask(new CommitHistoryAnalysisTask.Options( |
| 51 | + repo, |
| 52 | + differ, |
| 53 | + outputDir.resolve(repo.getRepositoryName() + ".lg"), |
| 54 | + exportOptions, |
| 55 | + strategy, |
| 56 | + commitsToProcess |
| 57 | + )); |
| 58 | + Logger.info("Scheduled " + commitsToProcess.size() + " commits."); |
| 59 | + commitsToProcess = null; // free reference to enable garbage collection |
| 60 | + Logger.info("<<< done after " + clock.printPassedSeconds()); |
| 61 | + |
| 62 | + Logger.info(">>> Run mining"); |
| 63 | + clock.start(); |
| 64 | + try { |
| 65 | + totalResult = task.call(); |
| 66 | + } catch (Exception e) { |
| 67 | + Logger.error(e); |
| 68 | + Logger.info("<<< aborted after " + clock.printPassedSeconds()); |
| 69 | + return; |
| 70 | + } |
| 71 | + Logger.info("<<< done after " + clock.printPassedSeconds()); |
| 72 | + |
| 73 | + exportMetadata(outputDir, totalResult); |
| 74 | + } |
| 75 | + |
| 76 | + public static void mineAsync( |
| 77 | + final Repository repo, |
| 78 | + final Path outputDir, |
| 79 | + final CommitHistoryAnalysisTaskFactory taskFactory, |
| 80 | + int commitsToProcessPerThread) |
| 81 | + { |
| 82 | + final AnalysisResult totalResult = new AnalysisResult(repo.getRepositoryName()); |
| 83 | + final GitDiffer differ = new GitDiffer(repo); |
| 84 | + final Clock clock = new Clock(); |
| 85 | + |
| 86 | + // prepare tasks |
| 87 | + final int nThreads = Diagnostics.INSTANCE.run().getNumberOfAvailableProcessors(); |
| 88 | + Logger.info(">>> Scheduling asynchronous mining on " + nThreads + " threads."); |
| 89 | + clock.start(); |
| 90 | + final InvocationCounter<RevCommit, RevCommit> numberOfTotalCommits = InvocationCounter.justCount(); |
| 91 | + final Iterator<CommitHistoryAnalysisTask> tasks = new MappedIterator<>( |
| 92 | + /// 1.) Retrieve COMMITS_TO_PROCESS_PER_THREAD commits from the differ and cluster them into one list. |
| 93 | + new ClusteredIterator<>(differ.yieldRevCommitsAfter(numberOfTotalCommits), commitsToProcessPerThread), |
| 94 | + /// 2.) Create a MiningTask for the list of commits. This task will then be processed by one |
| 95 | + /// particular thread. |
| 96 | + commitList -> taskFactory.create( |
| 97 | + repo, |
| 98 | + differ, |
| 99 | + outputDir.resolve(commitList.get(0).getId().getName() + ".lg"), |
| 100 | + commitList) |
| 101 | + ); |
| 102 | + Logger.info("<<< done in " + clock.printPassedSeconds()); |
| 103 | + |
| 104 | + final TaskCompletionMonitor commitSpeedMonitor = new TaskCompletionMonitor(0, TaskCompletionMonitor.LogProgress("commits")); |
| 105 | + Logger.info(">>> Run mining"); |
| 106 | + clock.start(); |
| 107 | + commitSpeedMonitor.start(); |
| 108 | + try (final ScheduledTasksIterator<AnalysisResult> threads = new ScheduledTasksIterator<>(tasks, nThreads)) { |
| 109 | + while (threads.hasNext()) { |
| 110 | + final AnalysisResult threadsResult = threads.next(); |
| 111 | + totalResult.append(threadsResult); |
| 112 | + commitSpeedMonitor.addFinishedTasks(threadsResult.exportedCommits); |
| 113 | + } |
| 114 | + } catch (Exception e) { |
| 115 | + Logger.error("Failed to run all mining task!"); |
| 116 | + Logger.error(e); |
| 117 | + System.exit(0); |
| 118 | + } |
| 119 | + |
| 120 | + final double runtime = clock.getPassedSeconds(); |
| 121 | + Logger.info("<<< done in " + Clock.printPassedSeconds(runtime)); |
| 122 | + |
| 123 | + totalResult.runtimeWithMultithreadingInSeconds = runtime; |
| 124 | + totalResult.totalCommits = numberOfTotalCommits.invocationCount().get(); |
| 125 | + |
| 126 | + exportMetadata(outputDir, totalResult); |
| 127 | + } |
| 128 | + |
| 129 | + public static <T> void exportMetadata(final Path outputDir, final Metadata<T> totalResult) { |
| 130 | + exportMetadataToFile(outputDir.resolve(TOTAL_RESULTS_FILE_NAME), totalResult); |
| 131 | + } |
| 132 | + |
| 133 | + public static <T> void exportMetadataToFile(final Path outputFile, final Metadata<T> totalResult) { |
| 134 | + final String prettyMetadata = totalResult.exportTo(outputFile); |
| 135 | + Logger.info("Metadata:\n" + prettyMetadata); |
| 136 | + } |
| 137 | + |
| 138 | + public void runAsync() { |
| 139 | + for (final Repository repo : repositoriesToAnalyze) { |
| 140 | + Logger.info(" === Begin Processing " + repo.getRepositoryName() + " ==="); |
| 141 | + final Clock clock = new Clock(); |
| 142 | + clock.start(); |
| 143 | + |
| 144 | + final Path repoOutputDir = outputDir.resolve(repo.getRepositoryName()); |
| 145 | + /// Don't repeat work we already did: |
| 146 | + if (!Files.exists(repoOutputDir.resolve(TOTAL_RESULTS_FILE_NAME))) { |
| 147 | + mineAsync(repo, repoOutputDir, whatToDo, commitsToProcessPerThread); |
| 148 | +// mine(repo, repoOutputDir, ExportOptions(repo), MiningStrategy()); |
| 149 | + |
| 150 | + postProcessingOnRepositoryOutputDir.accept(repoOutputDir); |
| 151 | + } else { |
| 152 | + Logger.info(" Skipping repository " + repo.getRepositoryName() + " because it has already been processed."); |
| 153 | + } |
| 154 | + |
| 155 | + Logger.info(" === End Processing " + repo.getRepositoryName() + " after " + clock.printPassedSeconds() + " ==="); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments