mirror of
https://github.com/github/codeql.git
synced 2026-04-26 01:05:15 +02:00
C#: Respect $CODEQL_THREADS environment variable
This commit is contained in:
@@ -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
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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}");
|
||||
|
||||
19
csharp/extractor/Semmle.Util/EnvironmentVariables.cs
Normal file
19
csharp/extractor/Semmle.Util/EnvironmentVariables.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user