System.Drawing: added email to icon and test file headers
[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 #if NET_2_0
38         public static class CounterSampleCalculator {
39 #else
40         public sealed class CounterSampleCalculator {
41
42                 private CounterSampleCalculator ()
43                 {
44                 }
45 #endif
46
47                 public static float ComputeCounterValue (CounterSample newSample)
48                 {
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;
56                         default:
57                                 return 0;
58                         }
59                 }
60
61                 [MonoTODO("What's the algorithm?")]
62                 public static float ComputeCounterValue (CounterSample oldSample,
63                         CounterSample newSample)
64                 {
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:
96                                 // FIXME
97                                 return 0;
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;
105                         default:
106                                 Console.WriteLine ("Counter type {0} not handled", newSample.CounterType);
107                                 return 0;
108                         }
109                 }
110         }
111 }
112