C#: Respect $CODEQL_THREADS environment variable

This commit is contained in:
Tom Hvitved
2023-08-22 11:43:07 +02:00
parent 5192d7c137
commit 6bb37ca465
5 changed files with 31 additions and 6 deletions

View File

@@ -377,7 +377,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
private void AnalyseSolutions(IEnumerable<string> solutions)
{
Parallel.ForEach(solutions, new ParallelOptions { MaxDegreeOfParallelism = 4 }, solutionFile =>
Parallel.ForEach(solutions, new ParallelOptions { MaxDegreeOfParallelism = options.Threads }, solutionFile =>
{
try
{

View File

@@ -1,5 +1,7 @@
using System;
using System.Linq;
using System.Collections.Generic;
using Semmle.Util;
namespace Semmle.Extraction.CSharp.DependencyFetching
{
@@ -49,6 +51,11 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
/// <param name="path">The path to query.</param>
/// <returns>True iff the path matches an exclusion.</returns>
bool ExcludesFile(string path);
/// <summary>
/// The number of threads to use.
/// </summary>
int Threads { get; }
}
public class DependencyOptions : IDependencyOptions
@@ -71,5 +78,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
public bool ExcludesFile(string path) =>
Excludes.Any(path.Contains);
public int Threads { get; set; } = EnvironmentVariables.GetDefaultNumberOfThreads();
}
}

View File

@@ -13,7 +13,7 @@ namespace Semmle.Extraction
/// <summary>
/// The specified number of threads, or the default if unspecified.
/// </summary>
public int Threads { get; private set; } = System.Environment.ProcessorCount;
public int Threads { get; private set; } = EnvironmentVariables.GetDefaultNumberOfThreads();
/// <summary>
/// The verbosity used in output and logging.

View File

@@ -41,16 +41,13 @@ namespace Semmle.Util
public static class OptionsExtensions
{
private static readonly string[] ExtractorOptions = new[] { "trap_compression", "cil" };
private static string? GetExtractorOption(string name) =>
Environment.GetEnvironmentVariable($"CODEQL_EXTRACTOR_CSHARP_OPTION_{name.ToUpper()}");
private static List<string> GetExtractorOptions()
{
var extractorOptions = new List<string>();
foreach (var option in ExtractorOptions)
{
var value = GetExtractorOption(option);
var value = EnvironmentVariables.GetExtractorOption(option);
if (!string.IsNullOrEmpty(value))
{
extractorOptions.Add($"--{option}:{value}");

View File

@@ -0,0 +1,19 @@
using System;
namespace Semmle.Util
{
public class EnvironmentVariables
{
public static string? GetExtractorOption(string name) =>
Environment.GetEnvironmentVariable($"CODEQL_EXTRACTOR_CSHARP_OPTION_{name.ToUpper()}");
public static int GetDefaultNumberOfThreads()
{
if (!int.TryParse(Environment.GetEnvironmentVariable("CODEQL_THREADS"), out var threads) || threads == -1)
{
threads = Environment.ProcessorCount;
}
return threads;
}
}
}