Skip to content

Commit 6d5fd3c

Browse files
authored
Merge pull request github#15827 from tamasvajk/buildless/impr-progress-reporting
C#: Improve buildless progress reporting
2 parents 0ebe045 + c4f2bbd commit 6d5fd3c

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

csharp/extractor/Semmle.Extraction.CSharp.Standalone/Extractor.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,9 @@ private static void AnalyseStandalone(
6060
{
6161
try
6262
{
63-
FileUtils.TryDelete(output.FullName);
6463
if (shouldCleanUpContainingFolder)
6564
{
66-
output.Directory?.Delete(true);
65+
FileUtils.TryDelete(output.FullName);
6766
}
6867
}
6968
catch
@@ -105,12 +104,19 @@ public ExtractionProgress(ILogger output)
105104

106105
public void Analysed(int item, int total, string source, string output, TimeSpan time, AnalysisAction action)
107106
{
108-
logger.Log(Severity.Info, "[{0}/{1}] {2} ({3})", item, total, source,
109-
action == AnalysisAction.Extracted
110-
? time.ToString()
111-
: action == AnalysisAction.Excluded
112-
? "excluded"
113-
: "up to date");
107+
var extra = action switch
108+
{
109+
AnalysisAction.Extracted => time.ToString(),
110+
AnalysisAction.Excluded => "excluded",
111+
AnalysisAction.UpToDate => "up to date",
112+
_ => "unknown action"
113+
};
114+
logger.LogInfo($"[{item}/{total}] {source} ({extra})");
115+
}
116+
117+
public void Started(int item, int total, string source)
118+
{
119+
logger.LogInfo($"[{item}/{total}] {source} (processing started)");
114120
}
115121

116122
public void MissingType(string type)

csharp/extractor/Semmle.Extraction.CSharp/Extractor/Analyser.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.IO;
55
using System.Linq;
6+
using System.Threading;
67
using System.Threading.Tasks;
78
using Microsoft.CodeAnalysis;
89
using Microsoft.CodeAnalysis.CSharp;
@@ -23,8 +24,6 @@ public class Analyser : IDisposable
2324
private protected Entities.Compilation? compilationEntity;
2425
private IDisposable? compilationTrapFile;
2526

26-
private readonly object progressMutex = new object();
27-
2827
// The bulk of the extraction work, potentially executed in parallel.
2928
protected readonly List<Action> extractionTasks = new List<Action>();
3029
private int taskCount = 0;
@@ -131,6 +130,9 @@ private void DoAnalyseReferenceAssembly(PortableExecutableReference r)
131130

132131
var skipExtraction = options.Cache && File.Exists(trapWriter.TrapFile);
133132

133+
var currentTaskId = IncrementTaskCount();
134+
ReportProgressTaskStarted(currentTaskId, assemblyPath);
135+
134136
if (!skipExtraction)
135137
{
136138
/* Note on parallel builds:
@@ -167,7 +169,7 @@ private void DoAnalyseReferenceAssembly(PortableExecutableReference r)
167169
}
168170
}
169171

170-
ReportProgress(assemblyPath, trapWriter.TrapFile, stopwatch.Elapsed, skipExtraction ? AnalysisAction.UpToDate : AnalysisAction.Extracted);
172+
ReportProgressTaskDone(currentTaskId, assemblyPath, trapWriter.TrapFile, stopwatch.Elapsed, skipExtraction ? AnalysisAction.UpToDate : AnalysisAction.Extracted);
171173
}
172174
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
173175
{
@@ -177,11 +179,13 @@ private void DoAnalyseReferenceAssembly(PortableExecutableReference r)
177179

178180
private void DoExtractCIL(PortableExecutableReference r)
179181
{
182+
var currentTaskId = IncrementTaskCount();
183+
ReportProgressTaskStarted(currentTaskId, r.FilePath);
180184
var stopwatch = new Stopwatch();
181185
stopwatch.Start();
182186
CIL.Analyser.ExtractCIL(r.FilePath!, Logger, options, out var trapFile, out var extracted);
183187
stopwatch.Stop();
184-
ReportProgress(r.FilePath, trapFile, stopwatch.Elapsed, extracted ? AnalysisAction.Extracted : AnalysisAction.UpToDate);
188+
ReportProgressTaskDone(currentTaskId, r.FilePath, trapFile, stopwatch.Elapsed, extracted ? AnalysisAction.Extracted : AnalysisAction.UpToDate);
185189
}
186190

187191
private void DoExtractTree(SyntaxTree tree)
@@ -201,6 +205,9 @@ private void DoExtractTree(SyntaxTree tree)
201205

202206
upToDate = options.Fast && FileIsUpToDate(sourcePath, trapWriter.TrapFile);
203207

208+
var currentTaskId = IncrementTaskCount();
209+
ReportProgressTaskStarted(currentTaskId, sourcePath);
210+
204211
if (!upToDate)
205212
{
206213
var cx = new Context(extractor, compilation.Clone(), trapWriter, new SourceScope(tree), addAssemblyTrapPrefix);
@@ -221,7 +228,7 @@ private void DoExtractTree(SyntaxTree tree)
221228
cx.PopulateAll();
222229
}
223230

224-
ReportProgress(sourcePath, trapPath, stopwatch.Elapsed, upToDate ? AnalysisAction.UpToDate : AnalysisAction.Extracted);
231+
ReportProgressTaskDone(currentTaskId, sourcePath, trapPath, stopwatch.Elapsed, upToDate ? AnalysisAction.UpToDate : AnalysisAction.Extracted);
225232
}
226233
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
227234
{
@@ -234,6 +241,11 @@ private void DoAnalyseCompilation()
234241
try
235242
{
236243
var assemblyPath = extractor.OutputPath;
244+
var stopwatch = new Stopwatch();
245+
stopwatch.Start();
246+
var currentTaskId = IncrementTaskCount();
247+
ReportProgressTaskStarted(currentTaskId, assemblyPath);
248+
237249
var transformedAssemblyPath = PathTransformer.Transform(assemblyPath);
238250
var assembly = compilation.Assembly;
239251
var trapWriter = transformedAssemblyPath.CreateTrapWriter(Logger, options.TrapCompression, discardDuplicates: false);
@@ -243,6 +255,8 @@ private void DoAnalyseCompilation()
243255
compilationEntity = Entities.Compilation.Create(cx);
244256

245257
extractor.CompilationInfos.ForEach(ci => trapWriter.Writer.compilation_info(compilationEntity, ci.key, ci.value));
258+
259+
ReportProgressTaskDone(currentTaskId, assemblyPath, trapWriter.TrapFile, stopwatch.Elapsed, AnalysisAction.Extracted);
246260
}
247261
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
248262
{
@@ -279,10 +293,19 @@ private static void AnalyseNamespace(Context cx, INamespaceSymbol ns)
279293
}
280294
}
281295

282-
private void ReportProgress(string src, string output, TimeSpan time, AnalysisAction action)
296+
private int IncrementTaskCount()
297+
{
298+
return Interlocked.Increment(ref taskCount);
299+
}
300+
301+
private void ReportProgressTaskStarted(int currentCount, string src)
302+
{
303+
progressMonitor.Started(currentCount, extractionTasks.Count, src);
304+
}
305+
306+
private void ReportProgressTaskDone(int currentCount, string src, string output, TimeSpan time, AnalysisAction action)
283307
{
284-
lock (progressMutex)
285-
progressMonitor.Analysed(++taskCount, extractionTasks.Count, src, output, time, action);
308+
progressMonitor.Analysed(currentCount, extractionTasks.Count, src, output, time, action);
286309
}
287310

288311
/// <summary>

csharp/extractor/Semmle.Extraction.CSharp/Extractor/Extractor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public void Analysed(int item, int total, string source, string output, TimeSpan
4747
}
4848
}
4949

50+
public void Started(int item, int total, string source) { }
51+
5052
public void MissingNamespace(string @namespace) { }
5153

5254
public void MissingSummary(int types, int namespaces) { }

csharp/extractor/Semmle.Extraction.CSharp/Extractor/IProgressMonitor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public interface IProgressMonitor
1919
/// <param name="action">What action was taken for the file.</param>
2020
void Analysed(int item, int total, string source, string output, TimeSpan time, AnalysisAction action);
2121

22+
/// <summary>
23+
/// Callback that processing of a particular item has been started.
24+
/// </summary>
25+
void Started(int item, int total, string source);
26+
2227
/// <summary>
2328
/// A "using namespace" directive was seen but the given
2429
/// namespace could not be found.

0 commit comments

Comments
 (0)