Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DeltaTask.java 8.3KB

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