-
Notifications
You must be signed in to change notification settings - Fork 15
Publish runtime metrics and byte-weighted progress via System.Diagnostics.Metrics #865
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b8912ea
Publish runtime metrics and byte-weighted progress via Metrics
ptr727 355c767
Address review: scope metrics to processed files, ASCII, consistent c…
ptr727 b665807
Cache State flags and use a bitwise check in EnumerateSetStates
ptr727 a5c638e
Pre-build State tags and use bitwise flag checks to avoid per-file al…
ptr727 3f70cb0
Refine runtime-metrics comments and docs
ptr727 3909e20
Cache tool tags to avoid a per-invocation alloc in RecordToolDuration
ptr727 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Run dotnet-counters against the in-container PlexCleaner process. | ||
| # The single-file tool needs a writable extract dir, and the app is PID 1 in the container. | ||
| # Usage from the host: | ||
| # docker exec <container> counters # live monitor of the PlexCleaner.Process meter | ||
| # docker exec <container> counters collect ... # any other dotnet-counters verb/args, passed through | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| export DOTNET_BUNDLE_EXTRACT_BASE_DIR="${DOTNET_BUNDLE_EXTRACT_BASE_DIR:-/tmp}" | ||
|
|
||
| if [[ $# -eq 0 ]]; then | ||
| set -- monitor -p 1 --counters PlexCleaner.Process | ||
| fi | ||
|
|
||
| exec /dotnet-tools/dotnet-counters "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| using System.Diagnostics; | ||
| using System.Diagnostics.Metrics; | ||
|
|
||
| namespace PlexCleaner; | ||
|
|
||
| internal static class Metrics | ||
| { | ||
| private static readonly Meter s_meter = new("PlexCleaner.Process"); | ||
|
|
||
| // Cumulative for the process lifetime, not reset between runs. | ||
| private static readonly Counter<long> s_filesCompleted = s_meter.CreateCounter<long>( | ||
| "plexcleaner.files.completed", | ||
| description: "Files finished, any outcome" | ||
| ); | ||
| private static readonly Counter<long> s_filesModified = s_meter.CreateCounter<long>( | ||
| "plexcleaner.files.modified", | ||
| description: "Files whose media was changed" | ||
| ); | ||
| private static readonly Counter<long> s_filesErrors = s_meter.CreateCounter<long>( | ||
| "plexcleaner.files.errors", | ||
| description: "Files that errored" | ||
| ); | ||
| private static readonly Counter<long> s_filesVerifyFailed = s_meter.CreateCounter<long>( | ||
| "plexcleaner.files.verifyfailed", | ||
| description: "Files that failed verification" | ||
| ); | ||
| private static readonly Counter<long> s_filesProcessed = s_meter.CreateCounter<long>( | ||
| "plexcleaner.files.processed", | ||
| description: "Per-outcome tally, tagged by each State flag set" | ||
| ); | ||
|
|
||
| private static readonly Histogram<double> s_fileDuration = s_meter.CreateHistogram<double>( | ||
| "plexcleaner.file.duration", | ||
| unit: "ms", | ||
| description: "Per-file wall-clock time" | ||
| ); | ||
| private static readonly Histogram<double> s_toolDuration = s_meter.CreateHistogram<double>( | ||
| "plexcleaner.tool.duration", | ||
| unit: "ms", | ||
| description: "Per media-tool invocation time, tagged by tool" | ||
| ); | ||
|
|
||
| // Run-scoped state, reset by BeginRun, read by the observable gauges. | ||
| // All access is via Interlocked so the parallel loop needs no lock. | ||
| private static long s_runFilesTotal; | ||
| private static long s_runBytesTotal; | ||
| private static long s_runBytesCompleted; | ||
| private static long s_runInflight; | ||
| private static long s_runStartTimestamp; | ||
|
|
||
| // Each State flag (minus None) with its tag, pre-built once so RecordStates allocates nothing per file. | ||
| private static readonly ( | ||
| SidecarFile.StatesType Flag, | ||
| KeyValuePair<string, object?> Tag | ||
| )[] s_stateTags = | ||
| [ | ||
| .. Enum.GetValues<SidecarFile.StatesType>() | ||
| .Where(flag => flag != SidecarFile.StatesType.None) | ||
| .Select(flag => (flag, new KeyValuePair<string, object?>("state", flag.ToString()))), | ||
| ]; | ||
|
|
||
| // Each tool with its pre-built tag, so RecordToolDuration allocates nothing per invocation. | ||
| private static readonly Dictionary< | ||
| MediaTool.ToolType, | ||
| KeyValuePair<string, object?> | ||
| > s_toolTags = Enum.GetValues<MediaTool.ToolType>() | ||
| .ToDictionary( | ||
| tool => tool, | ||
| tool => new KeyValuePair<string, object?>("tool", tool.ToString()) | ||
| ); | ||
|
|
||
| static Metrics() | ||
| { | ||
| _ = s_meter.CreateObservableGauge( | ||
| "plexcleaner.files.total", | ||
| () => Interlocked.Read(ref s_runFilesTotal), | ||
| description: "Files in the current run" | ||
| ); | ||
| _ = s_meter.CreateObservableGauge( | ||
| "plexcleaner.files.inflight", | ||
| () => Interlocked.Read(ref s_runInflight), | ||
| description: "Files currently processing" | ||
| ); | ||
| _ = s_meter.CreateObservableGauge( | ||
| "plexcleaner.threads.active", | ||
| () => (long)(Program.Options?.ThreadCount ?? 0), | ||
| description: "Configured worker threads" | ||
| ); | ||
| _ = s_meter.CreateObservableGauge( | ||
| "plexcleaner.bytes.total", | ||
| () => Interlocked.Read(ref s_runBytesTotal), | ||
| unit: "By", | ||
| description: "Sum of input sizes in the current run" | ||
| ); | ||
| _ = s_meter.CreateObservableGauge( | ||
| "plexcleaner.bytes.completed", | ||
| () => Interlocked.Read(ref s_runBytesCompleted), | ||
| unit: "By", | ||
| description: "Bytes of finished files (no partial credit for in-flight files)" | ||
| ); | ||
| _ = s_meter.CreateObservableGauge( | ||
| "plexcleaner.progress.ratio", | ||
| ComputeProgress, | ||
| description: "Byte-weighted overall progress [0..1]" | ||
| ); | ||
| _ = s_meter.CreateObservableGauge( | ||
| "plexcleaner.eta.seconds", | ||
| ComputeEtaSeconds, | ||
| unit: "s", | ||
| description: "Estimated time remaining" | ||
| ); | ||
| } | ||
|
|
||
| // Reset the run-scoped gauges and ETA clock, called once per ProcessFiles run. | ||
| internal static void BeginRun(long totalFiles, long totalBytes) | ||
| { | ||
| _ = Interlocked.Exchange(ref s_runFilesTotal, totalFiles); | ||
| _ = Interlocked.Exchange(ref s_runBytesTotal, totalBytes); | ||
| _ = Interlocked.Exchange(ref s_runBytesCompleted, 0); | ||
| _ = Interlocked.Exchange(ref s_runInflight, 0); | ||
| _ = Interlocked.Exchange(ref s_runStartTimestamp, Stopwatch.GetTimestamp()); | ||
| } | ||
|
|
||
| internal static void FileStarted() => Interlocked.Increment(ref s_runInflight); | ||
|
|
||
| internal static void FileInflightDone() => Interlocked.Decrement(ref s_runInflight); | ||
|
|
||
| // A finished file credits its whole size (no partial credit in v1) and its wall-clock time. | ||
| internal static void FileCompleted(long sizeBytes, TimeSpan wall) | ||
| { | ||
| _ = Interlocked.Add(ref s_runBytesCompleted, sizeBytes); | ||
| s_filesCompleted.Add(1); | ||
| s_fileDuration.Record(wall.TotalMilliseconds); | ||
| } | ||
|
|
||
| internal static void FileErrored() => s_filesErrors.Add(1); | ||
|
|
||
| internal static void RecordModified() => s_filesModified.Add(1); | ||
|
|
||
| internal static void RecordVerifyFailed() => s_filesVerifyFailed.Add(1); | ||
|
|
||
| internal static void RecordStates(SidecarFile.StatesType state) | ||
| { | ||
| foreach ((SidecarFile.StatesType flag, KeyValuePair<string, object?> tag) in s_stateTags) | ||
| { | ||
| if ((state & flag) == flag) | ||
| { | ||
| s_filesProcessed.Add(1, tag); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal static void RecordToolDuration(MediaTool.ToolType tool, double milliseconds) => | ||
| s_toolDuration.Record(milliseconds, s_toolTags[tool]); | ||
|
|
||
| internal static void Dispose() => s_meter.Dispose(); | ||
|
|
||
| // Byte-weighted progress, guards a zero (or not-yet-started) total. | ||
| internal static double ComputeProgress() | ||
| { | ||
| long total = Interlocked.Read(ref s_runBytesTotal); | ||
| return total <= 0 ? 0.0 : (double)Interlocked.Read(ref s_runBytesCompleted) / total; | ||
| } | ||
|
|
||
| // Linear extrapolation from weighted progress and elapsed time. | ||
| // Returns 0 before any progress and never a non-finite value. | ||
| internal static double ComputeEtaSeconds() | ||
| { | ||
| double ratio = ComputeProgress(); | ||
| if (ratio <= 0.0) | ||
| { | ||
| return 0.0; | ||
| } | ||
| double elapsed = Stopwatch | ||
| .GetElapsedTime(Interlocked.Read(ref s_runStartTimestamp)) | ||
| .TotalSeconds; | ||
| double eta = elapsed * (1.0 - ratio) / ratio; | ||
| return double.IsFinite(eta) ? eta : 0.0; | ||
| } | ||
|
|
||
| internal static IEnumerable<SidecarFile.StatesType> EnumerateSetStates( | ||
| SidecarFile.StatesType state | ||
| ) | ||
| { | ||
| foreach ((SidecarFile.StatesType flag, _) in s_stateTags) | ||
| { | ||
| if ((state & flag) == flag) | ||
| { | ||
| yield return flag; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.