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.

DeltaTask.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.internal.storage.pack;
  44. import java.io.IOException;
  45. import java.util.ArrayList;
  46. import java.util.Collections;
  47. import java.util.Comparator;
  48. import java.util.Iterator;
  49. import java.util.LinkedList;
  50. import java.util.List;
  51. import java.util.concurrent.Callable;
  52. import org.eclipse.jgit.lib.ObjectReader;
  53. import org.eclipse.jgit.lib.ThreadSafeProgressMonitor;
  54. import org.eclipse.jgit.storage.pack.PackConfig;
  55. final class DeltaTask implements Callable<Object> {
  56. static final long MAX_METER = 9 << 20;
  57. static final class Block {
  58. private static final int MIN_TOP_PATH = 50 << 20;
  59. final List<DeltaTask> tasks;
  60. final int threads;
  61. final PackConfig config;
  62. final ObjectReader templateReader;
  63. final DeltaCache dc;
  64. final ThreadSafeProgressMonitor pm;
  65. final ObjectToPack[] list;
  66. final int beginIndex;
  67. final int endIndex;
  68. private long totalWeight;
  69. private long bytesPerUnit;
  70. Block(int threads, PackConfig config, ObjectReader reader,
  71. DeltaCache dc, ThreadSafeProgressMonitor pm,
  72. ObjectToPack[] list, int begin, int end) {
  73. this.tasks = new ArrayList<DeltaTask>(threads);
  74. this.threads = threads;
  75. this.config = config;
  76. this.templateReader = reader;
  77. this.dc = dc;
  78. this.pm = pm;
  79. this.list = list;
  80. this.beginIndex = begin;
  81. this.endIndex = end;
  82. }
  83. int cost() {
  84. int d = (int) (totalWeight / bytesPerUnit);
  85. if (totalWeight % bytesPerUnit != 0)
  86. d++;
  87. return d;
  88. }
  89. synchronized DeltaWindow stealWork(DeltaTask forThread) {
  90. for (;;) {
  91. DeltaTask maxTask = null;
  92. Slice maxSlice = null;
  93. int maxWork = 0;
  94. for (DeltaTask task : tasks) {
  95. Slice s = task.remaining();
  96. if (s != null && maxWork < s.size()) {
  97. maxTask = task;
  98. maxSlice = s;
  99. maxWork = s.size();
  100. }
  101. }
  102. if (maxTask == null)
  103. return null;
  104. if (maxTask.tryStealWork(maxSlice))
  105. return forThread.initWindow(maxSlice);
  106. }
  107. }
  108. void partitionTasks() {
  109. ArrayList<WeightedPath> topPaths = computeTopPaths();
  110. Iterator<WeightedPath> topPathItr = topPaths.iterator();
  111. int nextTop = 0;
  112. long weightPerThread = totalWeight / threads;
  113. for (int i = beginIndex; i < endIndex;) {
  114. DeltaTask task = new DeltaTask(this);
  115. long w = 0;
  116. // Assign the thread one top path.
  117. if (topPathItr.hasNext()) {
  118. WeightedPath p = topPathItr.next();
  119. w += p.weight;
  120. task.add(p.slice);
  121. }
  122. // Assign the task thread ~average weight.
  123. int s = i;
  124. for (; w < weightPerThread && i < endIndex;) {
  125. if (nextTop < topPaths.size()
  126. && i == topPaths.get(nextTop).slice.beginIndex) {
  127. if (s < i)
  128. task.add(new Slice(s, i));
  129. s = i = topPaths.get(nextTop++).slice.endIndex;
  130. } else
  131. w += list[i++].getWeight();
  132. }
  133. // Round up the slice to the end of a path.
  134. if (s < i) {
  135. int h = list[i - 1].getPathHash();
  136. while (i < endIndex) {
  137. if (h == list[i].getPathHash())
  138. i++;
  139. else
  140. break;
  141. }
  142. task.add(new Slice(s, i));
  143. }
  144. if (!task.slices.isEmpty())
  145. tasks.add(task);
  146. }
  147. while (topPathItr.hasNext()) {
  148. WeightedPath p = topPathItr.next();
  149. DeltaTask task = new DeltaTask(this);
  150. task.add(p.slice);
  151. tasks.add(task);
  152. }
  153. topPaths = null;
  154. }
  155. private ArrayList<WeightedPath> computeTopPaths() {
  156. ArrayList<WeightedPath> topPaths = new ArrayList<WeightedPath>(
  157. threads);
  158. int cp = beginIndex;
  159. int ch = list[cp].getPathHash();
  160. long cw = list[cp].getWeight();
  161. totalWeight = list[cp].getWeight();
  162. for (int i = cp + 1; i < endIndex; i++) {
  163. ObjectToPack o = list[i];
  164. if (ch != o.getPathHash()) {
  165. if (MIN_TOP_PATH < cw) {
  166. if (topPaths.size() < threads) {
  167. Slice s = new Slice(cp, i);
  168. topPaths.add(new WeightedPath(cw, s));
  169. if (topPaths.size() == threads)
  170. Collections.sort(topPaths);
  171. } else if (topPaths.get(0).weight < cw) {
  172. Slice s = new Slice(cp, i);
  173. WeightedPath p = new WeightedPath(cw, s);
  174. topPaths.set(0, p);
  175. if (p.compareTo(topPaths.get(1)) > 0)
  176. Collections.sort(topPaths);
  177. }
  178. }
  179. cp = i;
  180. ch = o.getPathHash();
  181. cw = 0;
  182. }
  183. if (o.isEdge() || o.doNotAttemptDelta())
  184. continue;
  185. cw += o.getWeight();
  186. totalWeight += o.getWeight();
  187. }
  188. // Sort by starting index to identify gaps later.
  189. Collections.sort(topPaths, new Comparator<WeightedPath>() {
  190. public int compare(WeightedPath a, WeightedPath b) {
  191. return a.slice.beginIndex - b.slice.beginIndex;
  192. }
  193. });
  194. bytesPerUnit = 1;
  195. while (MAX_METER <= (totalWeight / bytesPerUnit))
  196. bytesPerUnit <<= 10;
  197. return topPaths;
  198. }
  199. }
  200. static final class WeightedPath implements Comparable<WeightedPath> {
  201. final long weight;
  202. final Slice slice;
  203. WeightedPath(long weight, Slice s) {
  204. this.weight = weight;
  205. this.slice = s;
  206. }
  207. public int compareTo(WeightedPath o) {
  208. int cmp = Long.signum(weight - o.weight);
  209. if (cmp != 0)
  210. return cmp;
  211. return slice.beginIndex - o.slice.beginIndex;
  212. }
  213. }
  214. static final class Slice {
  215. final int beginIndex;
  216. final int endIndex;
  217. Slice(int b, int e) {
  218. beginIndex = b;
  219. endIndex = e;
  220. }
  221. final int size() {
  222. return endIndex - beginIndex;
  223. }
  224. }
  225. private final Block block;
  226. private final LinkedList<Slice> slices;
  227. private ObjectReader or;
  228. private DeltaWindow dw;
  229. DeltaTask(Block b) {
  230. this.block = b;
  231. this.slices = new LinkedList<Slice>();
  232. }
  233. void add(Slice s) {
  234. if (!slices.isEmpty()) {
  235. Slice last = slices.getLast();
  236. if (last.endIndex == s.beginIndex) {
  237. slices.removeLast();
  238. slices.add(new Slice(last.beginIndex, s.endIndex));
  239. return;
  240. }
  241. }
  242. slices.add(s);
  243. }
  244. public Object call() throws Exception {
  245. or = block.templateReader.newReader();
  246. try {
  247. DeltaWindow w;
  248. for (;;) {
  249. synchronized (this) {
  250. if (slices.isEmpty())
  251. break;
  252. w = initWindow(slices.removeFirst());
  253. }
  254. runWindow(w);
  255. }
  256. while ((w = block.stealWork(this)) != null)
  257. runWindow(w);
  258. } finally {
  259. block.pm.endWorker();
  260. or.release();
  261. or = null;
  262. }
  263. return null;
  264. }
  265. DeltaWindow initWindow(Slice s) {
  266. DeltaWindow w = new DeltaWindow(block.config, block.dc,
  267. or, block.pm, block.bytesPerUnit,
  268. block.list, s.beginIndex, s.endIndex);
  269. synchronized (this) {
  270. dw = w;
  271. }
  272. return w;
  273. }
  274. private void runWindow(DeltaWindow w) throws IOException {
  275. try {
  276. w.search();
  277. } finally {
  278. synchronized (this) {
  279. dw = null;
  280. }
  281. }
  282. }
  283. synchronized Slice remaining() {
  284. if (!slices.isEmpty())
  285. return slices.getLast();
  286. DeltaWindow d = dw;
  287. return d != null ? d.remaining() : null;
  288. }
  289. synchronized boolean tryStealWork(Slice s) {
  290. if (!slices.isEmpty() && slices.getLast().beginIndex == s.beginIndex) {
  291. slices.removeLast();
  292. return true;
  293. }
  294. DeltaWindow d = dw;
  295. return d != null ? d.tryStealWork(s) : false;
  296. }
  297. }