Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
EventCounter.cs
Go to the documentation of this file.
4
6
7[UnsupportedOSPlatform("browser")]
9{
10 private int _count;
11
12 private double _sum;
13
14 private double _sumSquared;
15
16 private double _min;
17
18 private double _max;
19
20 private readonly double[] _bufferedValues;
21
22 private volatile int _bufferedValuesIndex;
23
24 public EventCounter(string name, EventSource eventSource)
25 : base(name, eventSource)
26 {
27 _min = double.PositiveInfinity;
28 _max = double.NegativeInfinity;
29 double[] array = new double[10];
30 for (int i = 0; i < array.Length; i++)
31 {
32 array[i] = double.NegativeInfinity;
33 }
35 Publish();
36 }
37
38 public void WriteMetric(float value)
39 {
41 }
42
43 public void WriteMetric(double value)
44 {
46 }
47
48 public override string ToString()
49 {
50 int num = Volatile.Read(ref _count);
51 if (num != 0)
52 {
53 return $"EventCounter '{base.Name}' Count {num} Mean {_sum / (double)num:n3}";
54 }
55 return "EventCounter '" + base.Name + "' Count 0";
56 }
57
58 internal void OnMetricWritten(double value)
59 {
60 _sum += value;
62 if (value > _max)
63 {
64 _max = value;
65 }
66 if (value < _min)
67 {
68 _min = value;
69 }
70 _count++;
71 }
72
73 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "The DynamicDependency will preserve the properties of CounterPayload")]
74 [DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties, typeof(CounterPayload))]
75 internal override void WritePayload(float intervalSec, int pollingIntervalMillisec)
76 {
77 lock (this)
78 {
79 Flush();
80 CounterPayload counterPayload = new CounterPayload();
81 counterPayload.Count = _count;
82 counterPayload.IntervalSec = intervalSec;
83 if (0 < _count)
84 {
85 counterPayload.Mean = _sum / (double)_count;
86 counterPayload.StandardDeviation = Math.Sqrt(_sumSquared / (double)_count - _sum * _sum / (double)_count / (double)_count);
87 }
88 else
89 {
90 counterPayload.Mean = 0.0;
91 counterPayload.StandardDeviation = 0.0;
92 }
93 counterPayload.Min = _min;
94 counterPayload.Max = _max;
95 counterPayload.Series = $"Interval={pollingIntervalMillisec}";
96 counterPayload.CounterType = "Mean";
97 counterPayload.Metadata = GetMetadataString();
98 counterPayload.DisplayName = base.DisplayName ?? "";
99 counterPayload.DisplayUnits = base.DisplayUnits ?? "";
100 counterPayload.Name = base.Name;
102 base.EventSource.Write("EventCounters", new EventSourceOptions
103 {
104 Level = EventLevel.LogAlways
105 }, new CounterPayloadType(counterPayload));
106 }
107 }
108
109 internal void ResetStatistics()
110 {
111 lock (this)
112 {
113 _count = 0;
114 _sum = 0.0;
115 _sumSquared = 0.0;
116 _min = double.PositiveInfinity;
117 _max = double.NegativeInfinity;
118 }
119 }
120
121 private void Enqueue(double value)
122 {
123 int num = _bufferedValuesIndex;
124 double num2;
125 do
126 {
127 num2 = Interlocked.CompareExchange(ref _bufferedValues[num], value, double.NegativeInfinity);
128 num++;
129 if (_bufferedValues.Length <= num)
130 {
131 lock (this)
132 {
133 Flush();
134 }
135 num = 0;
136 }
137 }
138 while (num2 != double.NegativeInfinity);
140 }
141
142 protected void Flush()
143 {
144 for (int i = 0; i < _bufferedValues.Length; i++)
145 {
146 double num = Interlocked.Exchange(ref _bufferedValues[i], double.NegativeInfinity);
147 if (num != double.NegativeInfinity)
148 {
149 OnMetricWritten(num);
150 }
151 }
153 }
154}
override void WritePayload(float intervalSec, int pollingIntervalMillisec)
EventCounter(string name, EventSource eventSource)
static double Sqrt(double d)
static int CompareExchange(ref int location1, int value, int comparand)
static int Exchange(ref int location1, int value)
static bool Read(ref bool location)
Definition Volatile.cs:67