Skip to content

Commit 34308ee

Browse files
committed
C#: Improve buildless progress reporting
1 parent 66d2a84 commit 34308ee

File tree

4 files changed

+55
-13
lines changed

4 files changed

+55
-13
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: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ private void DoAnalyseReferenceAssembly(PortableExecutableReference r)
131131

132132
var skipExtraction = options.Cache && File.Exists(trapWriter.TrapFile);
133133

134+
var currentTaskId = IncrementTaskCount();
135+
ReportProgressTaskStarted(currentTaskId, assemblyPath);
136+
134137
if (!skipExtraction)
135138
{
136139
/* Note on parallel builds:
@@ -167,7 +170,7 @@ private void DoAnalyseReferenceAssembly(PortableExecutableReference r)
167170
}
168171
}
169172

170-
ReportProgress(assemblyPath, trapWriter.TrapFile, stopwatch.Elapsed, skipExtraction ? AnalysisAction.UpToDate : AnalysisAction.Extracted);
173+
ReportProgressTaskDone(currentTaskId, assemblyPath, trapWriter.TrapFile, stopwatch.Elapsed, skipExtraction ? AnalysisAction.UpToDate : AnalysisAction.Extracted);
171174
}
172175
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
173176
{
@@ -177,11 +180,13 @@ private void DoAnalyseReferenceAssembly(PortableExecutableReference r)
177180

178181
private void DoExtractCIL(PortableExecutableReference r)
179182
{
183+
var currentTaskId = IncrementTaskCount();
184+
ReportProgressTaskStarted(currentTaskId, r.FilePath);
180185
var stopwatch = new Stopwatch();
181186
stopwatch.Start();
182187
CIL.Analyser.ExtractCIL(r.FilePath!, Logger, options, out var trapFile, out var extracted);
183188
stopwatch.Stop();
184-
ReportProgress(r.FilePath, trapFile, stopwatch.Elapsed, extracted ? AnalysisAction.Extracted : AnalysisAction.UpToDate);
189+
ReportProgressTaskDone(currentTaskId, r.FilePath, trapFile, stopwatch.Elapsed, extracted ? AnalysisAction.Extracted : AnalysisAction.UpToDate);
185190
}
186191

187192
private void DoExtractTree(SyntaxTree tree)
@@ -201,6 +206,9 @@ private void DoExtractTree(SyntaxTree tree)
201206

202207
upToDate = options.Fast && FileIsUpToDate(sourcePath, trapWriter.TrapFile);
203208

209+
var currentTaskId = IncrementTaskCount();
210+
ReportProgressTaskStarted(currentTaskId, sourcePath);
211+
204212
if (!upToDate)
205213
{
206214
var cx = new Context(extractor, compilation.Clone(), trapWriter, new SourceScope(tree), addAssemblyTrapPrefix);
@@ -221,7 +229,7 @@ private void DoExtractTree(SyntaxTree tree)
221229
cx.PopulateAll();
222230
}
223231

224-
ReportProgress(sourcePath, trapPath, stopwatch.Elapsed, upToDate ? AnalysisAction.UpToDate : AnalysisAction.Extracted);
232+
ReportProgressTaskDone(currentTaskId, sourcePath, trapPath, stopwatch.Elapsed, upToDate ? AnalysisAction.UpToDate : AnalysisAction.Extracted);
225233
}
226234
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
227235
{
@@ -234,6 +242,13 @@ private void DoAnalyseCompilation()
234242
try
235243
{
236244
var assemblyPath = extractor.OutputPath;
245+
246+
247+
var stopwatch = new Stopwatch();
248+
stopwatch.Start();
249+
var currentTaskId = IncrementTaskCount();
250+
ReportProgressTaskStarted(currentTaskId, assemblyPath);
251+
237252
var transformedAssemblyPath = PathTransformer.Transform(assemblyPath);
238253
var assembly = compilation.Assembly;
239254
var trapWriter = transformedAssemblyPath.CreateTrapWriter(Logger, options.TrapCompression, discardDuplicates: false);
@@ -243,6 +258,8 @@ private void DoAnalyseCompilation()
243258
compilationEntity = Entities.Compilation.Create(cx);
244259

245260
extractor.CompilationInfos.ForEach(ci => trapWriter.Writer.compilation_info(compilationEntity, ci.key, ci.value));
261+
262+
ReportProgressTaskDone(currentTaskId, assemblyPath, trapWriter.TrapFile, stopwatch.Elapsed, AnalysisAction.Extracted);
246263
}
247264
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
248265
{
@@ -279,10 +296,22 @@ private static void AnalyseNamespace(Context cx, INamespaceSymbol ns)
279296
}
280297
}
281298

282-
private void ReportProgress(string src, string output, TimeSpan time, AnalysisAction action)
299+
private int IncrementTaskCount()
283300
{
284301
lock (progressMutex)
285-
progressMonitor.Analysed(++taskCount, extractionTasks.Count, src, output, time, action);
302+
{
303+
return ++taskCount;
304+
}
305+
}
306+
307+
private void ReportProgressTaskStarted(int currentCount, string src)
308+
{
309+
progressMonitor.Started(currentCount, extractionTasks.Count, src);
310+
}
311+
312+
private void ReportProgressTaskDone(int currentCount, string src, string output, TimeSpan time, AnalysisAction action)
313+
{
314+
progressMonitor.Analysed(currentCount, extractionTasks.Count, src, output, time, action);
286315
}
287316

288317
/// <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)