2 // System.Diagnostics.CounterSampleCalculator.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 namespace System
.Diagnostics
{
38 public static class CounterSampleCalculator
{
40 public sealed class CounterSampleCalculator
{
42 private CounterSampleCalculator ()
47 public static float ComputeCounterValue (CounterSample newSample
)
49 switch (newSample
.CounterType
) {
50 case PerformanceCounterType
.RawFraction
:
51 case PerformanceCounterType
.NumberOfItems32
:
52 case PerformanceCounterType
.NumberOfItemsHEX32
:
53 case PerformanceCounterType
.NumberOfItems64
:
54 case PerformanceCounterType
.NumberOfItemsHEX64
:
55 return (float)newSample
.RawValue
;
61 [MonoTODO("What's the algorithm?")]
62 public static float ComputeCounterValue (CounterSample oldSample
,
63 CounterSample newSample
)
65 if (newSample
.CounterType
!= oldSample
.CounterType
)
66 throw new Exception ("The counter samples must be of the same type");
67 switch (newSample
.CounterType
) {
68 case PerformanceCounterType
.RawFraction
:
69 case PerformanceCounterType
.NumberOfItems32
:
70 case PerformanceCounterType
.NumberOfItemsHEX32
:
71 case PerformanceCounterType
.NumberOfItems64
:
72 case PerformanceCounterType
.NumberOfItemsHEX64
:
73 return (float)newSample
.RawValue
;
74 case PerformanceCounterType
.AverageCount64
:
75 return (float)(newSample
.RawValue
- oldSample
.RawValue
)/(float)(newSample
.BaseValue
- oldSample
.BaseValue
);
76 case PerformanceCounterType
.AverageTimer32
:
77 return (((float)(newSample
.RawValue
- oldSample
.RawValue
))/newSample
.SystemFrequency
)/(float)(newSample
.BaseValue
- oldSample
.BaseValue
);
78 case PerformanceCounterType
.CounterDelta32
:
79 case PerformanceCounterType
.CounterDelta64
:
80 return (float)(newSample
.RawValue
- oldSample
.RawValue
);
81 case PerformanceCounterType
.CounterMultiTimer
:
82 return ((float)(newSample
.RawValue
- oldSample
.RawValue
))/(float)(newSample
.TimeStamp
- oldSample
.TimeStamp
) * 100.0f
/newSample
.BaseValue
;
83 case PerformanceCounterType
.CounterMultiTimer100Ns
:
84 return ((float)(newSample
.RawValue
- oldSample
.RawValue
))/(float)(newSample
.TimeStamp100nSec
- oldSample
.TimeStamp100nSec
) * 100.0f
/newSample
.BaseValue
;
85 case PerformanceCounterType
.CounterMultiTimerInverse
:
86 return (newSample
.BaseValue
- ((float)(newSample
.RawValue
- oldSample
.RawValue
))/(float)(newSample
.TimeStamp
- oldSample
.TimeStamp
)) * 100.0f
;
87 case PerformanceCounterType
.CounterMultiTimer100NsInverse
:
88 return (newSample
.BaseValue
- ((float)(newSample
.RawValue
- oldSample
.RawValue
))/(float)(newSample
.TimeStamp100nSec
- oldSample
.TimeStamp100nSec
)) * 100.0f
;
89 case PerformanceCounterType
.CounterTimer
:
90 case PerformanceCounterType
.CountPerTimeInterval32
:
91 case PerformanceCounterType
.CountPerTimeInterval64
:
92 return ((float)(newSample
.RawValue
- oldSample
.RawValue
))/(float)(newSample
.TimeStamp
- oldSample
.TimeStamp
);
93 case PerformanceCounterType
.CounterTimerInverse
:
94 return (1.0f
- ((float)(newSample
.RawValue
- oldSample
.RawValue
))/(float)(newSample
.TimeStamp100nSec
- oldSample
.TimeStamp100nSec
)) * 100.0f
;
95 case PerformanceCounterType
.ElapsedTime
:
98 case PerformanceCounterType
.Timer100Ns
:
99 return ((float)(newSample
.RawValue
- oldSample
.RawValue
))/(float)(newSample
.TimeStamp
- oldSample
.TimeStamp
) * 100.0f
;
100 case PerformanceCounterType
.Timer100NsInverse
:
101 return (1f
- ((float)(newSample
.RawValue
- oldSample
.RawValue
))/(float)(newSample
.TimeStamp
- oldSample
.TimeStamp
)) * 100.0f
;
102 case PerformanceCounterType
.RateOfCountsPerSecond32
:
103 case PerformanceCounterType
.RateOfCountsPerSecond64
:
104 return ((float)(newSample
.RawValue
- oldSample
.RawValue
))/(float)(newSample
.TimeStamp
- oldSample
.TimeStamp
) * 10000000;
106 Console
.WriteLine ("Counter type {0} not handled", newSample
.CounterType
);