Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory tuning #2619

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ReleaseHistory.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# SARIF Package Release History (SDK, Driver, Converters, and Multitool)

## **v4.1.0** UNRELEASED
* NEW: Add `PeakWorkingSet` to `--trace` command to report maximum working set value during analysis. [#2619](https://github.com/microsoft/sarif-sdk/pull/2619)

## **v4.0.0** [Sdk](https://www.nuget.org/packages/Sarif.Sdk/4.0.0) | [Driver](https://www.nuget.org/packages/Sarif.Driver/4.0.0) | [Converters](https://www.nuget.org/packages/Sarif.Converters/4.0.0) | [Multitool](https://www.nuget.org/packages/Sarif.Multitool/4.0.0) | [Multitool Library](https://www.nuget.org/packages/Sarif.Multitool.Library/4.0.0)
* BRK: `SarifLogger` no longer allows providing a `Tool` instance. Use the `run` parameter instead (and populate it with any custom `Tool` object). [#2614](https://github.com/microsoft/sarif-sdk/pull/2614)
* BRK: `SarifLogger` updates version details differently. [#2611](https://github.com/microsoft/sarif-sdk/pull/2611)
Expand Down
4 changes: 2 additions & 2 deletions src/Samples/Sarif.Sdk.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ private static void SerializeSarifResult(string filePath, int numResult, bool us
OptionallyEmittedData.Hashes |
OptionallyEmittedData.RegionSnippets,
run: run,
kinds: new ResultKind[] { ResultKind.Fail, ResultKind.Pass },
levels: new FailureLevel[] { FailureLevel.Error, FailureLevel.Warning, FailureLevel.Note },
kinds: BaseLogger.Fail,
levels: BaseLogger.ErrorWarning,
analysisTargets: null,
invocationTokensToRedact: null,
invocationPropertiesToLog: null,
Expand Down
4 changes: 4 additions & 0 deletions src/Samples/SarifTrim/SarifTrim.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<HintPath>..\..\..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Collections.Immutable, Version=1.2.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\System.Collections.Immutable.1.2.3\lib\netstandard2.0\System.Collections.Immutable.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down
1 change: 1 addition & 0 deletions src/Samples/SarifTrim/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net472" />
<package id="System.Collections.Immutable" version="1.5.0" targetFramework="net472" />
</packages>
60 changes: 7 additions & 53 deletions src/Sarif.Driver/Sdk/AnalyzeCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public abstract class AnalyzeCommandBase<TContext, TOptions> : PluginDriverComma
private Run _run;
private Tool _tool;
internal TContext _rootContext;
private CacheByFileHashLogger _cacheByFileHashLogger;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_cacheByFileHashLogger

I've clobbered caching by file hash in general with this change. It will not come back for the single-threaded analysis, as this engine is getting dropped entirely.

private IDictionary<string, HashData> _pathToHashDataMap;

public Exception ExecutionException { get; set; }
Expand Down Expand Up @@ -225,7 +224,7 @@ protected virtual bool ShouldEnqueue(string file, TContext context)

if (!shouldEnqueue)
{
Warnings.LogFileSkippedDueToSize(context, file, fileSizeInKb);
Notes.LogFileSkippedDueToSize(context, file, fileSizeInKb);
}

return shouldEnqueue;
Expand All @@ -239,16 +238,10 @@ internal AggregatingLogger InitializeLogger(AnalyzeOptionsBase analyzeOptions)

if (!analyzeOptions.Quiet)
{
_consoleLogger = new ConsoleLogger(analyzeOptions.Quiet, _tool.Driver.Name, analyzeOptions.Level, analyzeOptions.Kind) { CaptureOutput = _captureConsoleOutput };
_consoleLogger = new ConsoleLogger(analyzeOptions.Quiet, _tool.Driver.Name, analyzeOptions.FailureLevels, analyzeOptions.ResultKinds) { CaptureOutput = _captureConsoleOutput };
logger.Loggers.Add(_consoleLogger);
}

if ((analyzeOptions.DataToInsert.ToFlags() & OptionallyEmittedData.Hashes) != 0)
{
_cacheByFileHashLogger = new CacheByFileHashLogger(analyzeOptions.Level, analyzeOptions.Kind);
logger.Loggers.Add(_cacheByFileHashLogger);
}

return logger;
}

Expand Down Expand Up @@ -305,10 +298,7 @@ protected virtual TContext CreateContext(
Policy = policy ?? new PropertiesDictionary()
};

context.Traces =
options.Traces.Any() ?
(DefaultTraces)Enum.Parse(typeof(DefaultTraces), string.Join(",", options.Traces)) :
DefaultTraces.None;
context.Traces = options.Traces;

context.MaxFileSizeInKilobytes =
options.MaxFileSizeInKilobytes >= 0
Expand Down Expand Up @@ -427,8 +417,8 @@ private void InitializeOutputFile(TOptions analyzeOptions, TContext context)
quiet: analyzeOptions.Quiet,
invocationTokensToRedact: GenerateSensitiveTokensList(),
invocationPropertiesToLog: analyzeOptions.InvocationPropertiesToLog,
levels: analyzeOptions.Level,
kinds: analyzeOptions.Kind);
levels: analyzeOptions.FailureLevels,
kinds: analyzeOptions.ResultKinds);
}
else
{
Expand All @@ -441,8 +431,8 @@ private void InitializeOutputFile(TOptions analyzeOptions, TContext context)
analysisTargets: null,
invocationTokensToRedact: GenerateSensitiveTokensList(),
invocationPropertiesToLog: analyzeOptions.InvocationPropertiesToLog,
levels: analyzeOptions.Level,
kinds: analyzeOptions.Kind);
levels: analyzeOptions.FailureLevels,
kinds: analyzeOptions.ResultKinds);
}
_pathToHashDataMap = sarifLogger.AnalysisTargetToHashDataMap;
sarifLogger.AnalysisStarted();
Expand Down Expand Up @@ -628,42 +618,6 @@ protected virtual TContext DetermineApplicabilityAndAnalyze(
rootContext.Policy,
target);

if ((options.DataToInsert.ToFlags() & OptionallyEmittedData.Hashes) != 0)
{
_cacheByFileHashLogger.HashToResultsMap.TryGetValue(context.Hashes.Sha256, out List<Tuple<ReportingDescriptor, Result, int?>> cachedResultTuples);
_cacheByFileHashLogger.HashToNotificationsMap.TryGetValue(context.Hashes.Sha256, out List<Notification> cachedNotifications);

bool replayCachedData = (cachedResultTuples != null || cachedNotifications != null);

if (replayCachedData)
{
context.Logger.AnalyzingTarget(context);

if (cachedResultTuples != null)
{
foreach (Tuple<ReportingDescriptor, Result, int?> cachedResultTuple in cachedResultTuples)
{
Result clonedResult = cachedResultTuple.Item2.DeepClone();
ReportingDescriptor cachedReportingDescriptor = cachedResultTuple.Item1;

UpdateLocationsAndMessageWithCurrentUri(clonedResult.Locations, clonedResult.Message, context.TargetUri);
context.Logger.Log(cachedReportingDescriptor, clonedResult, cachedResultTuple.Item3);
}
}

if (cachedNotifications != null)
{
foreach (Notification cachedNotification in cachedNotifications)
{
Notification clonedNotification = cachedNotification.DeepClone();
UpdateLocationsAndMessageWithCurrentUri(clonedNotification.Locations, cachedNotification.Message, context.TargetUri);
context.Logger.LogConfigurationNotification(clonedNotification);
}
}
return context;
}
}

if (context.TargetLoadException != null)
{
Errors.LogExceptionLoadingTarget(context);
Expand Down
53 changes: 49 additions & 4 deletions src/Sarif.Driver/Sdk/AnalyzeOptionsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;

using CommandLine;

using Microsoft.CodeAnalysis.Sarif.Writers;

namespace Microsoft.CodeAnalysis.Sarif.Driver
{
[Verb("analyze", HelpText = "Analyze one or more binary files for security and correctness issues.")]
Expand All @@ -15,9 +19,9 @@ public AnalyzeOptionsBase()
{
// TODO: these defaults need to be converted to the configuration
// property pattern as followed by MaxFileSizeInKilobytes.
Traces = new string[] { };
Kind = new List<ResultKind> { ResultKind.Fail };
Level = new List<FailureLevel> { FailureLevel.Warning, FailureLevel.Error };
Trace = new string[] { };

Check warning

Code scanning / CodeQL

Virtual call in constructor or destructor

Avoid virtual calls in a constructor or destructor.
Kind = BaseLogger.Fail;
Level = BaseLogger.ErrorWarning;

MaxFileSizeInKilobytes = AnalyzeContextBase.MaxFileSizeInKilobytesDefaultValue;
}
Expand Down Expand Up @@ -100,7 +104,21 @@ public AnalyzeOptionsBase()
HelpText = "Execution traces, expressed as a semicolon-delimited list, that " +
"should be emitted to the console and log file (if appropriate). " +
"Valid values: ScanTime.")]
public virtual IEnumerable<string> Traces { get; set; }
public virtual IEnumerable<string> Trace { get; set; }

private DefaultTraces? defaultTraces;
public DefaultTraces Traces
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Traces

This pattern may be worth pursuing aggressively, i.e., some class surface area for interacting directly with the commandline library vs. reconstructed data more suitable for direct consumption by the engine.

I keep wondering whether options could be merged entirely with the context object...

{
get
{
defaultTraces ??=
this.Trace.Any()
? (DefaultTraces)Enum.Parse(typeof(DefaultTraces), string.Join(",", this.Trace))
: DefaultTraces.None;

return this.defaultTraces.Value;
}
}

[Option(
"level",
Expand All @@ -109,13 +127,33 @@ public AnalyzeOptionsBase()
HelpText = "A semicolon delimited list to filter output of scan results to one or more failure levels. Valid values: Error, Warning and Note.")]
public IEnumerable<FailureLevel> Level { get; set; }

private IImmutableSet<FailureLevel> failureLevels;
public IImmutableSet<FailureLevel> FailureLevels
{
get
{
this.failureLevels ??= new List<FailureLevel>(Level).ToImmutableHashSet();
return this.failureLevels;
}
}

[Option(
"kind",
Separator = ';',
Default = new ResultKind[] { ResultKind.Fail },
HelpText = "A semicolon delimited list to filter output to one or more result kinds. Valid values: Fail (for literal scan results), Pass, Review, Open, NotApplicable and Informational.")]
public IEnumerable<ResultKind> Kind { get; set; }

private IImmutableSet<ResultKind> resultKinds;
public IImmutableSet<ResultKind> ResultKinds
{
get
{
this.resultKinds ??= new List<ResultKind>(Kind).ToImmutableHashSet();
return this.resultKinds;
}
}

[Option(
"baseline",
HelpText = "A SARIF file to be used as baseline.")]
Expand All @@ -131,5 +169,12 @@ public AnalyzeOptionsBase()
HelpText = "The maximum file size (in kilobytes) that will be analyzed.",
Default = -1)]
public long MaxFileSizeInKilobytes { get; set; }

[Option(
"max-targets",
HelpText = "The maximum number of targets that will be prepared for analysis. Use this to limit memory usage.",
Default = 25000)]
public int MaxTargets { get; set; }

}
}
Loading