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 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. 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. }
  105. if (maxTask.tryStealWork(maxSlice)) {
  106. return forThread.initWindow(maxSlice);
  107. }
  108. }
  109. }
  110. void partitionTasks() {
  111. ArrayList<WeightedPath> topPaths = computeTopPaths();
  112. Iterator<WeightedPath> topPathItr = topPaths.iterator();
  113. int nextTop = 0;
  114. long weightPerThread = Math.max(totalWeight / threads, 1);
  115. for (int i = beginIndex; i < endIndex;) {
  116. DeltaTask task = new DeltaTask(this);
  117. long w = 0;
  118. // Assign the thread one top path.
  119. if (topPathItr.hasNext()) {
  120. WeightedPath p = topPathItr.next();
  121. w += p.weight;
  122. task.add(p.slice);
  123. }
  124. // Assign the task thread ~average weight.
  125. int s = i;
  126. for (; w < weightPerThread && i < endIndex;) {
  127. if (nextTop < topPaths.size()
  128. && i == topPaths.get(nextTop).slice.beginIndex) {
  129. if (s < i) {
  130. task.add(new Slice(s, i));
  131. }
  132. s = i = topPaths.get(nextTop++).slice.endIndex;
  133. } else {
  134. w += getAdjustedWeight(list[i++]);
  135. }
  136. }
  137. // Round up the slice to the end of a path.
  138. if (s < i) {
  139. int h = list[i - 1].getPathHash();
  140. while (i < endIndex) {
  141. if (h == list[i].getPathHash()) {
  142. i++;
  143. } else {
  144. break;
  145. }
  146. }
  147. task.add(new Slice(s, i));
  148. }
  149. if (!task.slices.isEmpty()) {
  150. tasks.add(task);
  151. }
  152. }
  153. while (topPathItr.hasNext()) {
  154. WeightedPath p = topPathItr.next();
  155. DeltaTask task = new DeltaTask(this);
  156. task.add(p.slice);
  157. tasks.add(task);
  158. }
  159. topPaths = null;
  160. }
  161. private ArrayList<WeightedPath> computeTopPaths() {
  162. ArrayList<WeightedPath> topPaths = new ArrayList<WeightedPath>(
  163. threads);
  164. int cp = beginIndex;
  165. int ch = list[cp].getPathHash();
  166. long cw = getAdjustedWeight(list[cp]);
  167. totalWeight = cw;
  168. for (int i = cp + 1; i < endIndex; i++) {
  169. ObjectToPack o = list[i];
  170. if (ch != o.getPathHash()) {
  171. if (MIN_TOP_PATH < cw) {
  172. if (topPaths.size() < threads) {
  173. Slice s = new Slice(cp, i);
  174. topPaths.add(new WeightedPath(cw, s));
  175. if (topPaths.size() == threads) {
  176. Collections.sort(topPaths);
  177. }
  178. } else if (topPaths.get(0).weight < cw) {
  179. Slice s = new Slice(cp, i);
  180. WeightedPath p = new WeightedPath(cw, s);
  181. topPaths.set(0, p);
  182. if (p.compareTo(topPaths.get(1)) > 0) {
  183. Collections.sort(topPaths);
  184. }
  185. }
  186. }
  187. cp = i;
  188. ch = o.getPathHash();
  189. cw = 0;
  190. }
  191. int weight = getAdjustedWeight(o);
  192. cw += weight;
  193. totalWeight += weight;
  194. }
  195. // Sort by starting index to identify gaps later.
  196. Collections.sort(topPaths, new Comparator<WeightedPath>() {
  197. public int compare(WeightedPath a, WeightedPath b) {
  198. return a.slice.beginIndex - b.slice.beginIndex;
  199. }
  200. });
  201. bytesPerUnit = 1;
  202. while (MAX_METER <= (totalWeight / bytesPerUnit)) {
  203. bytesPerUnit <<= 10;
  204. }
  205. return topPaths;
  206. }
  207. }
  208. static int getAdjustedWeight(ObjectToPack o) {
  209. // Edge objects and those with reused deltas do not need to be
  210. // compressed. For compression calculations, ignore their weights.
  211. if (o.isEdge() || o.doNotAttemptDelta()) {
  212. return 0;
  213. }
  214. return o.getWeight();
  215. }
  216. static final class WeightedPath implements Comparable<WeightedPath> {
  217. final long weight;
  218. final Slice slice;
  219. WeightedPath(long weight, Slice s) {
  220. this.weight = weight;
  221. this.slice = s;
  222. }
  223. public int compareTo(WeightedPath o) {
  224. int cmp = Long.signum(weight - o.weight);
  225. if (cmp != 0) {
  226. return cmp;
  227. }
  228. return slice.beginIndex - o.slice.beginIndex;
  229. }
  230. }
  231. static final class Slice {
  232. final int beginIndex;
  233. final int endIndex;
  234. Slice(int b, int e) {
  235. beginIndex = b;
  236. endIndex = e;
  237. }
  238. final int size() {
  239. return endIndex - beginIndex;
  240. }
  241. }
  242. private final Block block;
  243. final LinkedList<Slice> slices;
  244. private ObjectReader or;
  245. private DeltaWindow dw;
  246. DeltaTask(Block b) {
  247. this.block = b;
  248. this.slices = new LinkedList<Slice>();
  249. }
  250. void add(Slice s) {
  251. if (!slices.isEmpty()) {
  252. Slice last = slices.getLast();
  253. if (last.endIndex == s.beginIndex) {
  254. slices.removeLast();
  255. slices.add(new Slice(last.beginIndex, s.endIndex));
  256. return;
  257. }
  258. }
  259. slices.add(s);
  260. }
  261. public Object call() throws Exception {
  262. or = block.templateReader.newReader();
  263. try {
  264. DeltaWindow w;
  265. for (;;) {
  266. synchronized (this) {
  267. if (slices.isEmpty()) {
  268. break;
  269. }
  270. w = initWindow(slices.removeFirst());
  271. }
  272. runWindow(w);
  273. }
  274. while ((w = block.stealWork(this)) != null) {
  275. runWindow(w);
  276. }
  277. } finally {
  278. block.pm.endWorker();
  279. or.close();
  280. or = null;
  281. }
  282. return null;
  283. }
  284. DeltaWindow initWindow(Slice s) {
  285. DeltaWindow w = new DeltaWindow(block.config, block.dc,
  286. or, block.pm, block.bytesPerUnit,
  287. block.list, s.beginIndex, s.endIndex);
  288. synchronized (this) {
  289. dw = w;
  290. }
  291. return w;
  292. }
  293. private void runWindow(DeltaWindow w) throws IOException {
  294. try {
  295. w.search();
  296. } finally {
  297. synchronized (this) {
  298. dw = null;
  299. }
  300. }
  301. }
  302. synchronized Slice remaining() {
  303. if (!slices.isEmpty()) {
  304. return slices.getLast();
  305. }
  306. DeltaWindow d = dw;
  307. return d != null ? d.remaining() : null;
  308. }
  309. synchronized boolean tryStealWork(Slice s) {
  310. if (!slices.isEmpty() && slices.getLast().beginIndex == s.beginIndex) {
  311. slices.removeLast();
  312. return true;
  313. }
  314. DeltaWindow d = dw;
  315. return d != null ? d.tryStealWork(s) : false;
  316. }
  317. }