Skip to content

Commit 31738a3

Browse files
authored
Merge pull request github#14016 from hvitved/csharp/codeql-threads
C#: Respect `$CODEQL_THREADS` environment variable
2 parents 00c0ebe + 6bb37ca commit 31738a3

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ private void DownloadMissingPackages()
377377

378378
private void AnalyseSolutions(IEnumerable<string> solutions)
379379
{
380-
Parallel.ForEach(solutions, new ParallelOptions { MaxDegreeOfParallelism = 4 }, solutionFile =>
380+
Parallel.ForEach(solutions, new ParallelOptions { MaxDegreeOfParallelism = options.Threads }, solutionFile =>
381381
{
382382
try
383383
{

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyOptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System;
12
using System.Linq;
23
using System.Collections.Generic;
4+
using Semmle.Util;
35

46
namespace Semmle.Extraction.CSharp.DependencyFetching
57
{
@@ -49,6 +51,11 @@ public interface IDependencyOptions
4951
/// <param name="path">The path to query.</param>
5052
/// <returns>True iff the path matches an exclusion.</returns>
5153
bool ExcludesFile(string path);
54+
55+
/// <summary>
56+
/// The number of threads to use.
57+
/// </summary>
58+
int Threads { get; }
5259
}
5360

5461
public class DependencyOptions : IDependencyOptions
@@ -71,5 +78,7 @@ public class DependencyOptions : IDependencyOptions
7178

7279
public bool ExcludesFile(string path) =>
7380
Excludes.Any(path.Contains);
81+
82+
public int Threads { get; set; } = EnvironmentVariables.GetDefaultNumberOfThreads();
7483
}
7584
}

csharp/extractor/Semmle.Extraction/Options.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public abstract class CommonOptions : ICommandLineOptions
1313
/// <summary>
1414
/// The specified number of threads, or the default if unspecified.
1515
/// </summary>
16-
public int Threads { get; private set; } = System.Environment.ProcessorCount;
16+
public int Threads { get; private set; } = EnvironmentVariables.GetDefaultNumberOfThreads();
1717

1818
/// <summary>
1919
/// The verbosity used in output and logging.

csharp/extractor/Semmle.Util/CommandLineOptions.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,13 @@ public interface ICommandLineOptions
4141
public static class OptionsExtensions
4242
{
4343
private static readonly string[] ExtractorOptions = new[] { "trap_compression", "cil" };
44-
private static string? GetExtractorOption(string name) =>
45-
Environment.GetEnvironmentVariable($"CODEQL_EXTRACTOR_CSHARP_OPTION_{name.ToUpper()}");
46-
4744
private static List<string> GetExtractorOptions()
4845
{
4946
var extractorOptions = new List<string>();
5047

5148
foreach (var option in ExtractorOptions)
5249
{
53-
var value = GetExtractorOption(option);
50+
var value = EnvironmentVariables.GetExtractorOption(option);
5451
if (!string.IsNullOrEmpty(value))
5552
{
5653
extractorOptions.Add($"--{option}:{value}");
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace Semmle.Util
4+
{
5+
public class EnvironmentVariables
6+
{
7+
public static string? GetExtractorOption(string name) =>
8+
Environment.GetEnvironmentVariable($"CODEQL_EXTRACTOR_CSHARP_OPTION_{name.ToUpper()}");
9+
10+
public static int GetDefaultNumberOfThreads()
11+
{
12+
if (!int.TryParse(Environment.GetEnvironmentVariable("CODEQL_THREADS"), out var threads) || threads == -1)
13+
{
14+
threads = Environment.ProcessorCount;
15+
}
16+
return threads;
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)