You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DiffAlgorithms.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.pgm.debug;
  44. import static java.lang.Integer.valueOf;
  45. import static java.lang.Long.valueOf;
  46. import java.io.File;
  47. import java.lang.management.ManagementFactory;
  48. import java.lang.management.ThreadMXBean;
  49. import java.lang.reflect.Field;
  50. import java.util.ArrayList;
  51. import java.util.Collections;
  52. import java.util.Comparator;
  53. import java.util.List;
  54. import org.eclipse.jgit.diff.DiffAlgorithm;
  55. import org.eclipse.jgit.diff.HistogramDiff;
  56. import org.eclipse.jgit.diff.MyersDiff;
  57. import org.eclipse.jgit.diff.RawText;
  58. import org.eclipse.jgit.diff.RawTextComparator;
  59. import org.eclipse.jgit.errors.LargeObjectException;
  60. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  61. import org.eclipse.jgit.lib.Constants;
  62. import org.eclipse.jgit.lib.FileMode;
  63. import org.eclipse.jgit.lib.MutableObjectId;
  64. import org.eclipse.jgit.lib.ObjectId;
  65. import org.eclipse.jgit.lib.ObjectReader;
  66. import org.eclipse.jgit.lib.Repository;
  67. import org.eclipse.jgit.lib.RepositoryBuilder;
  68. import org.eclipse.jgit.lib.RepositoryCache;
  69. import org.eclipse.jgit.pgm.Command;
  70. import org.eclipse.jgit.pgm.TextBuiltin;
  71. import org.eclipse.jgit.pgm.internal.CLIText;
  72. import org.eclipse.jgit.revwalk.RevCommit;
  73. import org.eclipse.jgit.revwalk.RevWalk;
  74. import org.eclipse.jgit.treewalk.TreeWalk;
  75. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  76. import org.eclipse.jgit.util.FS;
  77. import org.kohsuke.args4j.Option;
  78. @Command(usage = "usage_DiffAlgorithms")
  79. class DiffAlgorithms extends TextBuiltin {
  80. final Algorithm myers = new Algorithm() {
  81. @Override
  82. DiffAlgorithm create() {
  83. return MyersDiff.INSTANCE;
  84. }
  85. };
  86. final Algorithm histogram = new Algorithm() {
  87. @Override
  88. DiffAlgorithm create() {
  89. HistogramDiff d = new HistogramDiff();
  90. d.setFallbackAlgorithm(null);
  91. return d;
  92. }
  93. };
  94. final Algorithm histogram_myers = new Algorithm() {
  95. @Override
  96. DiffAlgorithm create() {
  97. HistogramDiff d = new HistogramDiff();
  98. d.setFallbackAlgorithm(MyersDiff.INSTANCE);
  99. return d;
  100. }
  101. };
  102. // -----------------------------------------------------------------------
  103. //
  104. // Implementation of the suite lives below this line.
  105. //
  106. //
  107. @Option(name = "--algorithm", multiValued = true, metaVar = "NAME", usage = "Enable algorithm(s)")
  108. List<String> algorithms = new ArrayList<>();
  109. @Option(name = "--text-limit", metaVar = "LIMIT", usage = "Maximum size in KiB to scan per file revision")
  110. int textLimit = 15 * 1024; // 15 MiB as later we do * 1024.
  111. @Option(name = "--repository", aliases = { "-r" }, multiValued = true, metaVar = "GIT_DIR", usage = "Repository to scan")
  112. List<File> gitDirs = new ArrayList<>();
  113. @Option(name = "--count", metaVar = "LIMIT", usage = "Number of file revisions to be compared")
  114. int count = 0; // unlimited
  115. private final RawTextComparator cmp = RawTextComparator.DEFAULT;
  116. private ThreadMXBean mxBean;
  117. @Override
  118. protected boolean requiresRepository() {
  119. return false;
  120. }
  121. @Override
  122. protected void run() throws Exception {
  123. mxBean = ManagementFactory.getThreadMXBean();
  124. if (!mxBean.isCurrentThreadCpuTimeSupported())
  125. throw die("Current thread CPU time not supported on this JRE"); //$NON-NLS-1$
  126. if (gitDirs.isEmpty()) {
  127. RepositoryBuilder rb = new RepositoryBuilder() //
  128. .setGitDir(new File(gitdir)) //
  129. .readEnvironment() //
  130. .findGitDir();
  131. if (rb.getGitDir() == null)
  132. throw die(CLIText.get().cantFindGitDirectory);
  133. gitDirs.add(rb.getGitDir());
  134. }
  135. for (File dir : gitDirs) {
  136. RepositoryBuilder rb = new RepositoryBuilder();
  137. if (RepositoryCache.FileKey.isGitRepository(dir, FS.DETECTED))
  138. rb.setGitDir(dir);
  139. else
  140. rb.findGitDir(dir);
  141. Repository repo = rb.build();
  142. try {
  143. run(repo);
  144. } finally {
  145. repo.close();
  146. }
  147. }
  148. }
  149. private void run(Repository repo) throws Exception {
  150. List<Test> all = init();
  151. long files = 0;
  152. int commits = 0;
  153. int minN = Integer.MAX_VALUE;
  154. int maxN = 0;
  155. AbbreviatedObjectId startId;
  156. try (ObjectReader or = repo.newObjectReader();
  157. RevWalk rw = new RevWalk(or)) {
  158. final MutableObjectId id = new MutableObjectId();
  159. TreeWalk tw = new TreeWalk(or);
  160. tw.setFilter(TreeFilter.ANY_DIFF);
  161. tw.setRecursive(true);
  162. ObjectId start = repo.resolve(Constants.HEAD);
  163. startId = or.abbreviate(start);
  164. rw.markStart(rw.parseCommit(start));
  165. for (;;) {
  166. final RevCommit c = rw.next();
  167. if (c == null)
  168. break;
  169. commits++;
  170. if (c.getParentCount() != 1)
  171. continue;
  172. RevCommit p = c.getParent(0);
  173. rw.parseHeaders(p);
  174. tw.reset(p.getTree(), c.getTree());
  175. while (tw.next()) {
  176. if (!isFile(tw, 0) || !isFile(tw, 1))
  177. continue;
  178. byte[] raw0;
  179. try {
  180. tw.getObjectId(id, 0);
  181. raw0 = or.open(id).getCachedBytes(textLimit * 1024);
  182. } catch (LargeObjectException tooBig) {
  183. continue;
  184. }
  185. if (RawText.isBinary(raw0))
  186. continue;
  187. byte[] raw1;
  188. try {
  189. tw.getObjectId(id, 1);
  190. raw1 = or.open(id).getCachedBytes(textLimit * 1024);
  191. } catch (LargeObjectException tooBig) {
  192. continue;
  193. }
  194. if (RawText.isBinary(raw1))
  195. continue;
  196. RawText txt0 = new RawText(raw0);
  197. RawText txt1 = new RawText(raw1);
  198. minN = Math.min(minN, txt0.size() + txt1.size());
  199. maxN = Math.max(maxN, txt0.size() + txt1.size());
  200. for (Test test : all)
  201. testOne(test, txt0, txt1);
  202. files++;
  203. }
  204. if (count > 0 && files > count)
  205. break;
  206. }
  207. }
  208. Collections.sort(all, new Comparator<Test>() {
  209. @Override
  210. public int compare(Test a, Test b) {
  211. int result = Long.signum(a.runningTimeNanos - b.runningTimeNanos);
  212. if (result == 0) {
  213. result = a.algorithm.name.compareTo(b.algorithm.name);
  214. }
  215. return result;
  216. }
  217. });
  218. File directory = repo.getDirectory();
  219. if (directory != null) {
  220. String name = directory.getName();
  221. File parent = directory.getParentFile();
  222. if (name.equals(Constants.DOT_GIT) && parent != null)
  223. name = parent.getName();
  224. outw.println(name + ": start at " + startId.name()); //$NON-NLS-1$
  225. }
  226. outw.format(" %12d files, %8d commits\n", valueOf(files), //$NON-NLS-1$
  227. valueOf(commits));
  228. outw.format(" N=%10d min lines, %8d max lines\n", valueOf(minN), //$NON-NLS-1$
  229. valueOf(maxN));
  230. outw.format("%-25s %12s ( %12s %12s )\n", //$NON-NLS-1$
  231. "Algorithm", "Time(ns)", "Time(ns) on", "Time(ns) on"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  232. outw.format("%-25s %12s ( %12s %12s )\n", //$NON-NLS-1$
  233. "", "", "N=" + minN, "N=" + maxN); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  234. outw.println("-----------------------------------------------------" //$NON-NLS-1$
  235. + "----------------"); //$NON-NLS-1$
  236. for (Test test : all) {
  237. outw.format("%-25s %12d ( %12d %12d )", // //$NON-NLS-1$
  238. test.algorithm.name, //
  239. valueOf(test.runningTimeNanos), //
  240. valueOf(test.minN.runningTimeNanos), //
  241. valueOf(test.maxN.runningTimeNanos));
  242. outw.println();
  243. }
  244. outw.println();
  245. outw.flush();
  246. }
  247. private static boolean isFile(TreeWalk tw, int ithTree) {
  248. FileMode fm = tw.getFileMode(ithTree);
  249. return FileMode.REGULAR_FILE.equals(fm)
  250. || FileMode.EXECUTABLE_FILE.equals(fm);
  251. }
  252. private static final int minCPUTimerTicks = 10;
  253. private void testOne(Test test, RawText a, RawText b) {
  254. final DiffAlgorithm da = test.algorithm.create();
  255. int cpuTimeChanges = 0;
  256. int cnt = 0;
  257. final long startTime = mxBean.getCurrentThreadCpuTime();
  258. long lastTime = startTime;
  259. while (cpuTimeChanges < minCPUTimerTicks) {
  260. da.diff(cmp, a, b);
  261. cnt++;
  262. long interimTime = mxBean.getCurrentThreadCpuTime();
  263. if (interimTime != lastTime) {
  264. cpuTimeChanges++;
  265. lastTime = interimTime;
  266. }
  267. }
  268. final long stopTime = mxBean.getCurrentThreadCpuTime();
  269. final long runTime = (stopTime - startTime) / cnt;
  270. test.runningTimeNanos += runTime;
  271. if (test.minN == null || a.size() + b.size() < test.minN.n) {
  272. test.minN = new Run();
  273. test.minN.n = a.size() + b.size();
  274. test.minN.runningTimeNanos = runTime;
  275. }
  276. if (test.maxN == null || a.size() + b.size() > test.maxN.n) {
  277. test.maxN = new Run();
  278. test.maxN.n = a.size() + b.size();
  279. test.maxN.runningTimeNanos = runTime;
  280. }
  281. }
  282. private List<Test> init() {
  283. List<Test> all = new ArrayList<>();
  284. try {
  285. for (Field f : DiffAlgorithms.class.getDeclaredFields()) {
  286. if (f.getType() == Algorithm.class) {
  287. f.setAccessible(true);
  288. Algorithm alg = (Algorithm) f.get(this);
  289. alg.name = f.getName();
  290. if (included(alg.name, algorithms)) {
  291. Test test = new Test();
  292. test.algorithm = alg;
  293. all.add(test);
  294. }
  295. }
  296. }
  297. } catch (IllegalArgumentException e) {
  298. throw die("Cannot determine names", e); //$NON-NLS-1$
  299. } catch (IllegalAccessException e) {
  300. throw die("Cannot determine names", e); //$NON-NLS-1$
  301. }
  302. return all;
  303. }
  304. private static boolean included(String name, List<String> want) {
  305. if (want.isEmpty())
  306. return true;
  307. for (String s : want) {
  308. if (s.equalsIgnoreCase(name))
  309. return true;
  310. }
  311. return false;
  312. }
  313. private static abstract class Algorithm {
  314. String name;
  315. abstract DiffAlgorithm create();
  316. }
  317. private static class Test {
  318. Algorithm algorithm;
  319. long runningTimeNanos;
  320. Run minN;
  321. Run maxN;
  322. }
  323. private static class Run {
  324. int n;
  325. long runningTimeNanos;
  326. }
  327. }