Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System / System.Diagnostics / CounterSampleCalculator.cs
1 //
2 // System.Diagnostics.CounterSampleCalculator.cs
3 //
4 // Authors:
5 //   Jonathan Pryor (jonpryor@vt.edu)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) 2002
9 // (C) 2003 Andreas Nahr
10 //
11
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 using System;
34
35 namespace System.Diagnostics {
36
37         public static class CounterSampleCalculator {
38
39                 public static float ComputeCounterValue (CounterSample newSample)
40                 {
41                         switch (newSample.CounterType) {
42                         case PerformanceCounterType.RawFraction:
43                         case PerformanceCounterType.NumberOfItems32:
44                         case PerformanceCounterType.NumberOfItemsHEX32:
45                         case PerformanceCounterType.NumberOfItems64:
46                         case PerformanceCounterType.NumberOfItemsHEX64:
47                                 return (float)newSample.RawValue;
48                         default:
49                                 return 0;
50                         }
51                 }
52
53                 [MonoTODO("What's the algorithm?")]
54                 public static float ComputeCounterValue (CounterSample oldSample,
55                         CounterSample newSample)
56                 {
57                         if (newSample.CounterType != oldSample.CounterType)
58                                 throw new Exception ("The counter samples must be of the same type");
59                         switch (newSample.CounterType) {
60                         case PerformanceCounterType.RawFraction:
61                         case PerformanceCounterType.NumberOfItems32:
62                         case PerformanceCounterType.NumberOfItemsHEX32:
63                         case PerformanceCounterType.NumberOfItems64:
64                         case PerformanceCounterType.NumberOfItemsHEX64:
65                                 return (float)newSample.RawValue;
66                         case PerformanceCounterType.AverageCount64:
67                                 return (float)(newSample.RawValue - oldSample.RawValue)/(float)(newSample.BaseValue - oldSample.BaseValue);
68                         case PerformanceCounterType.AverageTimer32:
69                                 return (((float)(newSample.RawValue - oldSample.RawValue))/newSample.SystemFrequency)/(float)(newSample.BaseValue - oldSample.BaseValue);
70                         case PerformanceCounterType.CounterDelta32:
71                         case PerformanceCounterType.CounterDelta64:
72                                 return (float)(newSample.RawValue - oldSample.RawValue);
73                         case PerformanceCounterType.CounterMultiTimer:
74                                 return ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp - oldSample.TimeStamp) * 100.0f/newSample.BaseValue;
75                         case PerformanceCounterType.CounterMultiTimer100Ns:
76                                 return ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp100nSec - oldSample.TimeStamp100nSec) * 100.0f/newSample.BaseValue;
77                         case PerformanceCounterType.CounterMultiTimerInverse:
78                                 return (newSample.BaseValue - ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp - oldSample.TimeStamp)) * 100.0f;
79                         case PerformanceCounterType.CounterMultiTimer100NsInverse:
80                                 return (newSample.BaseValue - ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp100nSec - oldSample.TimeStamp100nSec)) * 100.0f;
81                         case PerformanceCounterType.CounterTimer:
82                         case PerformanceCounterType.CountPerTimeInterval32:
83                         case PerformanceCounterType.CountPerTimeInterval64:
84                                 return ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp - oldSample.TimeStamp);
85                         case PerformanceCounterType.CounterTimerInverse:
86                                 return (1.0f - ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp100nSec - oldSample.TimeStamp100nSec)) * 100.0f;
87                         case PerformanceCounterType.ElapsedTime:
88                                 // FIXME
89                                 return 0;
90                         case PerformanceCounterType.Timer100Ns:
91                                 return ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp - oldSample.TimeStamp) * 100.0f;
92                         case PerformanceCounterType.Timer100NsInverse:
93                                 return (1f - ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp - oldSample.TimeStamp)) * 100.0f;
94                         case PerformanceCounterType.RateOfCountsPerSecond32:
95                         case PerformanceCounterType.RateOfCountsPerSecond64:
96                                 return ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp - oldSample.TimeStamp) * 10000000;
97                         default:
98                                 Console.WriteLine ("Counter type {0} not handled", newSample.CounterType);
99                                 return 0;
100                         }
101                 }
102         }
103 }
104