Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CounterGroup.cs
Go to the documentation of this file.
4
6
7[UnsupportedOSPlatform("browser")]
8internal sealed class CounterGroup
9{
10 private readonly EventSource _eventSource;
11
13
14 private static readonly object s_counterGroupLock = new object();
15
17
19
21
23
24 private static Thread s_pollingThread;
25
27
29
30 internal CounterGroup(EventSource eventSource)
31 {
32 _eventSource = eventSource;
35 }
36
38 {
40 {
42 }
43 }
44
52
54 {
55 _eventSource.EventCommandExecuted += OnEventSourceCommand;
56 }
57
58 private void OnEventSourceCommand(object sender, EventCommandEventArgs e)
59 {
60 if (e.Command == EventCommand.Enable || e.Command == EventCommand.Update)
61 {
62 if (e.Arguments.TryGetValue("EventCounterIntervalSec", out string value) && float.TryParse(value, out var result))
63 {
65 {
66 EnableTimer(result);
67 }
68 }
69 }
70 else if (e.Command == EventCommand.Disable)
71 {
73 {
75 }
76 }
77 }
78
92
93 internal static CounterGroup GetCounterGroup(EventSource eventSource)
94 {
96 {
97 int num = EventListener.EventSourceIndex(eventSource);
100 if (weakReference == null || !weakReference.TryGetTarget(out var target))
101 {
102 target = new CounterGroup(eventSource);
104 }
105 return target;
106 }
107 }
108
110 {
111 if (pollingIntervalInSeconds <= 0f)
112 {
113 DisableTimer();
114 }
116 {
120 _nextPollingTimeStamp = DateTime.UtcNow + new TimeSpan(0, 0, (int)pollingIntervalInSeconds);
121 if (s_pollingThread == null)
122 {
123 s_pollingThreadSleepEvent = new AutoResetEvent(initialState: false);
126 {
127 IsBackground = true,
128 Name = ".NET Counter Poller"
129 };
130 s_pollingThread.UnsafeStart();
131 }
132 if (!s_counterGroupEnabledList.Contains(this))
133 {
135 }
137 }
138 }
139
140 private void DisableTimer()
141 {
143 s_counterGroupEnabledList?.Remove(this);
144 }
145
146 private void ResetCounters()
147 {
149 {
151 {
153 {
154 incrementingEventCounter.UpdateMetric();
155 }
157 {
158 incrementingPollingCounter.UpdateMetric();
159 }
161 {
162 eventCounter.ResetStatistics();
163 }
164 }
165 }
166 }
167
202
203 private static void PollForValues()
204 {
207 while (true)
208 {
209 int num = int.MaxValue;
211 {
214 {
216 if (s_counterGroupEnabled._nextPollingTimeStamp < utcNow + new TimeSpan(0, 0, 0, 0, 1))
217 {
219 }
220 int val = (int)(s_counterGroupEnabled._nextPollingTimeStamp - utcNow).TotalMilliseconds;
221 val = Math.Max(1, val);
222 num = Math.Min(num, val);
223 }
224 }
225 foreach (CounterGroup item in list)
226 {
227 item.OnTimer();
228 }
229 list.Clear();
230 if (num == int.MaxValue)
231 {
232 num = -1;
233 }
234 autoResetEvent?.WaitOne(num);
235 }
236 }
237}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
void Add(TKey key, TValue value)
readonly List< DiagnosticCounter > _counters
static CounterGroup GetCounterGroup(EventSource eventSource)
CounterGroup(EventSource eventSource)
void OnEventSourceCommand(object sender, EventCommandEventArgs e)
void Remove(DiagnosticCounter eventCounter)
static List< CounterGroup > s_counterGroupEnabledList
static AutoResetEvent s_pollingThreadSleepEvent
void EnableTimer(float pollingIntervalInSeconds)
static WeakReference< CounterGroup >[] s_counterGroups
static void EnsureEventSourceIndexAvailable(int eventSourceIndex)
static readonly object s_counterGroupLock
void Add(DiagnosticCounter eventCounter)
static int EventSourceIndex(EventSource eventSource)
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static double Ceiling(double a)
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static unsafe DateTime UtcNow
Definition DateTime.cs:142
static TimeSpan FromMilliseconds(double value)
Definition TimeSpan.cs:228