-
Notifications
You must be signed in to change notification settings - Fork 840
Add Disk IO time metric for Windows in ResourceMonitoring #6338
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 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7e9dc88
Add Disk IO time metric for Windows in ResourceMonitoring
makazeu ef35ec4
Fix calculation of busy time in Windows disk I/O performance counter
makazeu 68517ad
Merge branch 'main' into win-disk-metrics-iotime
makazeu d5a34c9
Improve with feedback
makazeu a88ad1d
Use ticks instead of Milliseconds when calculating disk io metrics
makazeu 1e6b19f
update
makazeu 9dbc8fe
re-run the pipeline
makazeu 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
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
76 changes: 76 additions & 0 deletions
76
...ft.Extensions.Diagnostics.ResourceMonitoring/Windows/Disk/WindowsDiskIoTimePerfCounter.cs
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,76 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Disk; | ||
|
|
||
| internal sealed class WindowsDiskIoTimePerfCounter | ||
| { | ||
| private readonly List<IPerformanceCounter> _counters = []; | ||
| private readonly IPerformanceCounterFactory _performanceCounterFactory; | ||
| private readonly TimeProvider _timeProvider; | ||
| private readonly string _categoryName; | ||
| private readonly string _counterName; | ||
| private readonly string[] _instanceNames; | ||
| private long _lastTimestamp; | ||
|
|
||
| internal WindowsDiskIoTimePerfCounter( | ||
| IPerformanceCounterFactory performanceCounterFactory, | ||
| TimeProvider timeProvider, | ||
| string categoryName, | ||
| string counterName, | ||
| string[] instanceNames) | ||
| { | ||
| _performanceCounterFactory = performanceCounterFactory; | ||
| _timeProvider = timeProvider; | ||
| _categoryName = categoryName; | ||
| _counterName = counterName; | ||
| _instanceNames = instanceNames; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the disk time measurements. | ||
| /// Key: Disk name, Value: Real elapsed time used in busy state. | ||
| /// </summary> | ||
| internal IDictionary<string, double> TotalSeconds { get; } = new ConcurrentDictionary<string, double>(); | ||
|
|
||
| internal void InitializeDiskCounters() | ||
| { | ||
| foreach (string instanceName in _instanceNames) | ||
| { | ||
| // Create counters for each disk | ||
| _counters.Add(_performanceCounterFactory.Create(_categoryName, _counterName, instanceName)); | ||
| TotalSeconds[instanceName] = 0f; | ||
| } | ||
|
|
||
| // Initialize the counters to get the first value | ||
| foreach (IPerformanceCounter counter in _counters) | ||
| { | ||
| _ = counter.NextValue(); | ||
| } | ||
|
|
||
| _lastTimestamp = _timeProvider.GetUtcNow().ToUnixTimeMilliseconds(); | ||
| } | ||
|
|
||
| internal void UpdateDiskCounters() | ||
| { | ||
| long currentTimestamp = _timeProvider.GetUtcNow().ToUnixTimeMilliseconds(); | ||
makazeu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| double elapsedSeconds = (currentTimestamp - _lastTimestamp) / 1000.0; // Convert to seconds | ||
|
|
||
| // The real elapsed time ("wall clock") used in the I/O path (time from operations running in parallel are not counted). | ||
| // Measured as the complement of "Disk\% Idle Time" performance counter: uptime * (100 - "Disk\% Idle Time") / 100 | ||
| // See https://opentelemetry.io/docs/specs/semconv/system/system-metrics/#metric-systemdiskio_time | ||
| foreach (IPerformanceCounter counter in _counters) | ||
| { | ||
| // io busy time = (1 - (% idle time / 100)) * elapsed seconds | ||
| float idleTimePercentage = Math.Min(counter.NextValue(), 100f); | ||
| double busyTimeSeconds = (1 - (idleTimePercentage / 100f)) * elapsedSeconds; | ||
| TotalSeconds[counter.InstanceName] += busyTimeSeconds; | ||
| } | ||
|
|
||
| _lastTimestamp = currentTimestamp; | ||
| } | ||
| } | ||
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
61 changes: 61 additions & 0 deletions
61
...ns.Diagnostics.ResourceMonitoring.Tests/Windows/Disk/WindowsDiskIoTimePerfCounterTests.cs
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,61 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Runtime.Versioning; | ||
| using Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Test; | ||
| using Microsoft.Extensions.Time.Testing; | ||
| using Microsoft.TestUtilities; | ||
| using Moq; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Extensions.Diagnostics.ResourceMonitoring.Windows.Disk.Test; | ||
|
|
||
| [SupportedOSPlatform("windows")] | ||
| [OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX, SkipReason = "Windows specific.")] | ||
| public class WindowsDiskIoTimePerfCounterTests | ||
| { | ||
| private const string CategoryName = "LogicalDisk"; | ||
|
|
||
| [ConditionalFact] | ||
| public void DiskReadsPerfCounter_Per60Seconds() | ||
| { | ||
| const string CounterName = WindowsDiskPerfCounterNames.DiskReadsCounter; | ||
| var performanceCounterFactory = new Mock<IPerformanceCounterFactory>(); | ||
| var fakeTimeProvider = new FakeTimeProvider { AutoAdvanceAmount = TimeSpan.FromSeconds(60) }; | ||
|
|
||
| var perfCounters = new WindowsDiskIoTimePerfCounter( | ||
| performanceCounterFactory.Object, | ||
| fakeTimeProvider, | ||
| CategoryName, | ||
| CounterName, | ||
| instanceNames: ["C:", "D:"]); | ||
|
|
||
| // Set up | ||
| var counterC = new FakePerformanceCounter("C:", [100, 100, 0, 99.5f]); | ||
| var counterD = new FakePerformanceCounter("D:", [100, 99.9f, 88.8f, 66.6f]); | ||
| performanceCounterFactory.Setup(x => x.Create(CategoryName, CounterName, "C:")).Returns(counterC); | ||
| performanceCounterFactory.Setup(x => x.Create(CategoryName, CounterName, "D:")).Returns(counterD); | ||
|
|
||
| // Initialize the counters | ||
| perfCounters.InitializeDiskCounters(); | ||
| Assert.Equal(2, perfCounters.TotalSeconds.Count); | ||
| Assert.Equal(0, perfCounters.TotalSeconds["C:"]); | ||
| Assert.Equal(0, perfCounters.TotalSeconds["D:"]); | ||
|
|
||
| // Simulate the first tick | ||
| perfCounters.UpdateDiskCounters(); | ||
| Assert.Equal(0, perfCounters.TotalSeconds["C:"], precision: 2); // (100-100)% * 60 = 0 | ||
| Assert.Equal(0.06, perfCounters.TotalSeconds["D:"], precision: 2); // (100-99.9)% * 60 = 0.06 | ||
|
|
||
| // Simulate the second tick | ||
| perfCounters.UpdateDiskCounters(); | ||
| Assert.Equal(60, perfCounters.TotalSeconds["C:"], precision: 2); // 0 + (100-0)% * 60 = 60 | ||
| Assert.Equal(6.78, perfCounters.TotalSeconds["D:"], precision: 2); // 0.06 + (100-88.8)% * 60 = 6.78 | ||
|
|
||
| // Simulate the third tick | ||
| perfCounters.UpdateDiskCounters(); | ||
| Assert.Equal(60.3, perfCounters.TotalSeconds["C:"], precision: 2); // 60 + (100-99.5)% * 60 = 60.3 | ||
| Assert.Equal(26.82, perfCounters.TotalSeconds["D:"], precision: 2); // 6.78 + (100-66.6)% * 60 = 6.78 + 20.04 = 26.82 | ||
| } | ||
| } |
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.