|
161 | 161 | * </p> |
162 | 162 | * |
163 | 163 | * <p> |
164 | | - * Finally, the environment variables <code>LGTM_TRAP_CACHE</code> and |
165 | | - * <code>LGTM_TRAP_CACHE_BOUND</code> can optionally be used to specify the location and size |
166 | | - * of a trap cache to be used during extraction. |
| 164 | + * To customise the actual extraction (as opposed to determining which files to extract), |
| 165 | + * the following environment variables are available: |
167 | 166 | * </p> |
| 167 | + * <ul> |
| 168 | + * <li><code>LGTM_INDEX_THREADS</code> determines how many threads are used for parallel extraction |
| 169 | + * of JavaScript files (TypeScript files cannot currently be extracted in parallel). If left |
| 170 | + * unspecified, the extractor uses as many threads as there are cores.</li> |
| 171 | + * <li><code>LGTM_TRAP_CACHE</code> and <code>LGTM_TRAP_CACHE_BOUND</code> can be used to specify the |
| 172 | + * location and size of a trap cache to be used during extraction.</li> |
| 173 | + * </ul> |
168 | 174 | */ |
169 | 175 | public class AutoBuild { |
170 | 176 | private final ExtractorOutputConfig outputConfig; |
@@ -375,15 +381,29 @@ private boolean addPathPattern(Set<Path> patterns, Path base, String pattern) { |
375 | 381 | * Perform extraction. |
376 | 382 | */ |
377 | 383 | public void run() throws IOException { |
378 | | - threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); |
| 384 | + startThreadPool(); |
379 | 385 | try { |
380 | 386 | extractSource(); |
381 | 387 | extractExterns(); |
382 | 388 | } finally { |
383 | | - threadPool.shutdown(); |
| 389 | + shutdownThreadPool(); |
384 | 390 | } |
385 | 391 | } |
386 | 392 |
|
| 393 | + private void startThreadPool() { |
| 394 | + int defaultNumThreads = Runtime.getRuntime().availableProcessors(); |
| 395 | + int numThreads = Env.systemEnv().getInt("LGTM_INDEX_THREADS", defaultNumThreads); |
| 396 | + if (numThreads > 1) |
| 397 | + threadPool = Executors.newFixedThreadPool(numThreads); |
| 398 | + else |
| 399 | + threadPool = null; |
| 400 | + } |
| 401 | + |
| 402 | + private void shutdownThreadPool() { |
| 403 | + if (threadPool != null) |
| 404 | + threadPool.shutdown(); |
| 405 | + } |
| 406 | + |
387 | 407 | /** |
388 | 408 | * Extract all "*.js" files under <code>$SEMMLE_DIST/tools/data/externs</code> as |
389 | 409 | * externs. |
@@ -624,7 +644,7 @@ private SourceType getSourceType() { |
624 | 644 | * otherwise extraction will happen on the main thread. |
625 | 645 | */ |
626 | 646 | protected void extract(FileExtractor extractor, Path file, ExtractorState state) { |
627 | | - if (state == null) |
| 647 | + if (state == null && threadPool != null) |
628 | 648 | threadPool.submit(() -> doExtract(extractor, file, state)); |
629 | 649 | else |
630 | 650 | doExtract(extractor, file, state); |
|
0 commit comments