Skip to content

Commit

Permalink
C#: Add aggregated compiler and extractor message counts to extractio…
Browse files Browse the repository at this point in the history
…n telemetry query
  • Loading branch information
tamasvajk committed Aug 22, 2024
1 parent a1688f6 commit 6827bed
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Semmle.Extraction.Entities;
using Semmle.Util;

namespace Semmle.Extraction.CSharp.Entities
Expand Down Expand Up @@ -89,13 +90,21 @@ public void PopulatePerformance(PerformanceMetrics p)
trapFile.compilation_finished(this, (float)p.Total.Cpu.TotalSeconds, (float)p.Total.Elapsed.TotalSeconds);
}

public void PopulateAggregatedMessages()
{
ExtractionMessage.groupedMessageCounts.ForEach(pair =>
{
Context.TrapWriter.Writer.compilation_info(this, $"Extractor message count for group '{pair.Key}'", pair.Value.ToString());
});
}

public override void WriteId(EscapingTextWriter trapFile)
{
trapFile.Write(hashCode);
trapFile.Write(";compilation");
}

public override Location ReportingLocation => throw new NotImplementedException();
public override Microsoft.CodeAnalysis.Location ReportingLocation => throw new NotImplementedException();

public override bool NeedsPopulation => Context.IsAssemblyScope;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ private void DoAnalyseCompilation()

public void LogPerformance(Entities.PerformanceMetrics p) => compilationEntity.PopulatePerformance(p);

public void ExtractAggregatedMessages() => compilationEntity.PopulateAggregatedMessages();

#nullable restore warnings

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ public static ExitCode Analyse(Stopwatch stopwatch, Analyser analyser, CommonOpt

sw.Restart();
analyser.PerformExtraction(options.Threads);
analyser.ExtractAggregatedMessages();
sw.Stop();
var cpuTime2 = currentProcess.TotalProcessorTime;
var userTime2 = currentProcess.UserProcessorTime;
Expand Down
1 change: 1 addition & 0 deletions csharp/extractor/Semmle.Extraction/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ codeql_csharp_library(
],
"//conditions:default": [],
}),
internals_visible_to = ["Semmle.Extraction.CSharp"],
visibility = ["//csharp:__subpackages__"],
deps = [
"//csharp/extractor/Semmle.Util",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Collections.Concurrent;
using System.IO;
using System.Threading;
using Semmle.Util;

Expand All @@ -7,6 +8,8 @@ namespace Semmle.Extraction.Entities
internal class ExtractionMessage : FreshEntity
{
private static readonly int limit = EnvironmentVariables.TryGetExtractorNumberOption<int>("MESSAGE_LIMIT") ?? 10000;

internal static readonly ConcurrentDictionary<string, int> groupedMessageCounts = [];
private static int messageCount = 0;

private readonly Message msg;
Expand All @@ -25,6 +28,10 @@ private ExtractionMessage(Context cx, Message msg, bool bypassLimit) : base(cx)

protected override void Populate(TextWriter trapFile)
{
// For the time being we're counting the number of messages per severity, we could introduce other groupings in the future
var key = msg.Severity.ToString();
groupedMessageCounts.AddOrUpdate(key, 1, (_, c) => c + 1);

if (!bypassLimit)
{
var val = Interlocked.Increment(ref messageCount);
Expand Down
2 changes: 2 additions & 0 deletions csharp/extractor/Semmle.Extraction/Semmle.Extraction.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Semmle.Util\Semmle.Util.csproj" />

<InternalsVisibleTo Include="Semmle.Extraction.CSharp" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ extractorMessagesLeachedLimit
compilationInfo
| Compiler diagnostic count for CS0103 | 3.0 |
| Compiler diagnostic count for CS8019 | 7.0 |
| Extractor message count for group 'Error' | 8.0 |
| Extractor message count for group 'Warning' | 1.0 |
3 changes: 2 additions & 1 deletion csharp/ql/integration-tests/all-platforms/standalone/Diag.ql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ query predicate extractorMessagesLeachedLimit(ExtractorMessage msg) {

query predicate compilationInfo(string key, float value) {
exists(Compilation c, string infoValue |
infoValue = c.getInfo(key) and key.matches("Compiler diagnostic count for%")
infoValue = c.getInfo(key) and
key.matches(["Compiler diagnostic count for%", "Extractor message count for group%"])
|
value = infoValue.toFloat()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import semmle.code.csharp.commons.Diagnostics
query predicate compilationInfo(string key, float value) {
key != "Resolved references" and
key != "Resolved assembly conflicts" and
not key.matches("Compiler diagnostic count for%") and
not key.matches(["Compiler diagnostic count for%", "Extractor message count for group%"]) and
exists(Compilation c, string infoKey, string infoValue | infoValue = c.getInfo(infoKey) |
key = infoKey and
value = infoValue.toFloat()
Expand Down
13 changes: 13 additions & 0 deletions csharp/ql/src/Telemetry/ExtractorInformation.ql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import DatabaseQuality

predicate compilationInfo(string key, float value) {
not key.matches("Compiler diagnostic count for%") and
not key.matches("Extractor message count for group%") and
exists(Compilation c, string infoKey, string infoValue | infoValue = c.getInfo(infoKey) |
key = infoKey and
value = infoValue.toFloat()
Expand All @@ -22,6 +23,16 @@ predicate compilationInfo(string key, float value) {
)
}

predicate compilerDiagnostics(string key, int value) {
key.matches("Compiler diagnostic count for%") and
strictsum(Compilation c | | c.getInfo(key).toInt()) = value
}

predicate extractorMessages(string key, int value) {
key.matches("Extractor message count for group%") and
strictsum(Compilation c | | c.getInfo(key).toInt()) = value
}

predicate fileCount(string key, int value) {
key = "Number of files" and
value = strictcount(File f)
Expand Down Expand Up @@ -140,6 +151,8 @@ from string key, float value
where
(
compilationInfo(key, value) or
compilerDiagnostics(key, value) or
extractorMessages(key, value) or
fileCount(key, value) or
fileCountByExtension(key, value) or
totalNumberOfLines(key, value) or
Expand Down

0 comments on commit 6827bed

Please sign in to comment.