Skip to content

Commit c308343

Browse files
committed
better formatting of stats
1 parent 8f550ae commit c308343

File tree

5 files changed

+55
-10
lines changed

5 files changed

+55
-10
lines changed

src/Compiler/Driver/fsc.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ let main1
569569
exiter.Exit 1
570570

571571
if tcConfig.showTimes then
572+
Caches.CacheMetrics.CaptureStatsAndWriteToConsole() |> disposables.Register
572573
Activity.Profiling.addConsoleListener () |> disposables.Register
573574

574575
tcConfig.writeTimesToFile

src/Compiler/Utilities/Caches.fs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ open System.Collections.Concurrent
77
open System.Threading
88
open System.Diagnostics
99
open System.Diagnostics.Metrics
10+
open System.IO
1011

1112
module CacheMetrics =
1213
let Meter = new Meter("FSharp.Compiler.Cache")
@@ -92,16 +93,55 @@ module CacheMetrics =
9293
| _ -> assert false)
9394

9495
listener.Start()
96+
listener :> IDisposable
9597

9698
let StatsToString () =
97-
let sb = Text.StringBuilder()
98-
sb.AppendLine "Cache Metrics:" |> ignore
99+
use sw = new StringWriter()
100+
101+
let nameColumnWidth =
102+
[ yield! statsByName.Keys; "Cache name" ] |> Seq.map String.length |> Seq.max
103+
104+
let ratioColumnWidth = "hit-ratio".Length
105+
let columns = allCounters |> List.map _.Name
106+
let columnWidths = columns |> List.map String.length |> List.map (max 8)
107+
108+
let totalWidth =
109+
1 + nameColumnWidth :: ratioColumnWidth :: columnWidths
110+
|> List.map ((+) 3)
111+
|> List.sum
112+
113+
sw.WriteLine(String('-', totalWidth))
114+
let cacheNameHeader = "Cache name".PadRight nameColumnWidth
115+
sw.Write $"| {cacheNameHeader} | hit-ratio |"
116+
117+
for w, c in (columnWidths, columns) ||> List.zip do
118+
sw.Write $" {c.PadLeft w} |"
119+
120+
sw.WriteLine()
121+
sw.WriteLine(String('-', totalWidth))
99122

100123
for kv in statsByName do
101-
sb.AppendLine $"Cache {kv.Key}: {kv.Value}" |> ignore
124+
let name = kv.Key
125+
let stats = kv.Value
126+
let totals = stats.GetTotals()
127+
sw.Write $"| {name.PadLeft nameColumnWidth} | {stats.Ratio, 9:P2} |"
102128

103-
sb.AppendLine() |> ignore
104-
string sb
129+
for w, c in (columnWidths, columns) ||> List.zip do
130+
sw.Write $" {totals[c].ToString().PadLeft(w)} |"
131+
132+
sw.WriteLine()
133+
134+
sw.WriteLine(String('-', totalWidth))
135+
string sw
136+
137+
let CaptureStatsAndWriteToConsole () =
138+
let listener = ListenToAll()
139+
140+
{ new IDisposable with
141+
member _.Dispose() =
142+
listener.Dispose()
143+
Console.WriteLine(StatsToString())
144+
}
105145

106146
// Currently the Cache emits telemetry for raw cache events: hits, misses, evictions etc.
107147
// This type observes those counters and keeps a snapshot of readings. It is used in tests and can be used to print cache stats in debug mode.

src/Compiler/Utilities/Caches.fsi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ module internal CacheMetrics =
88
/// Global telemetry Meter for all caches. Exposed for testing purposes.
99
/// Set FSHARP_OTEL_EXPORT environment variable to enable OpenTelemetry export to external collectors in tests.
1010
val Meter: Meter
11-
val ListenToAll: unit -> unit
11+
val ListenToAll: unit -> IDisposable
1212
val StatsToString: unit -> string
13+
val CaptureStatsAndWriteToConsole: unit -> IDisposable
1314

1415
/// A local listener that can be created for a specific Cache instance to get its metrics. For testing purposes only.
1516
[<Class>]

vsintegration/src/FSharp.Editor/Common/DebugHelpers.fs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ open Config
3131
open System.Diagnostics.Metrics
3232
open System.Text
3333
open Microsoft.VisualStudio.Threading
34+
open Microsoft.VisualStudio.FSharp.Editor.CancellableTasks
3435

3536
module FSharpOutputPane =
3637

@@ -119,14 +120,15 @@ module FSharpServiceTelemetry =
119120

120121
ActivitySource.AddActivityListener(listener)
121122

122-
let periodicallyDisplayCacheStats () =
123-
backgroundTask {
124-
CacheMetrics.ListenToAll()
123+
let periodicallyDisplayCacheStats (disposalToken: Threading.CancellationToken) =
124+
cancellableTask {
125+
use _ = CacheMetrics.ListenToAll()
125126

126127
while true do
127128
do! Task.Delay(TimeSpan.FromSeconds 10.0)
128129
FSharpOutputPane.logMsg (CacheMetrics.StatsToString())
129130
}
131+
|> CancellableTask.start disposalToken
130132

131133
#if DEBUG
132134
open OpenTelemetry.Resources

vsintegration/src/FSharp.Editor/LanguageService/LanguageService.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ type internal FSharpPackage() as this =
407407

408408
globalOptions.BlockForCompletionItems <- false
409409

410-
DebugHelpers.FSharpServiceTelemetry.periodicallyDisplayCacheStats () |> ignore
410+
DebugHelpers.FSharpServiceTelemetry.periodicallyDisplayCacheStats this.DisposalToken
411+
|> ignore
411412

412413
}
413414
|> CancellableTask.startAsTask cancellationToken)

0 commit comments

Comments
 (0)