chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Threading;
 
namespace OpenTap.Diagnostic
{
    /// <summary>
    /// Generic log message queue.
    /// </summary>
    internal class LogQueue
    {
        /// <summary>
        /// Fixed length log buffer allowing atomic lock-free insertion.
        /// </summary>
        internal class LogBuffer
        {
            /// <summary> How many log messages to make room for in the buffer.  </summary>
            const int Capacity = 1024 * 16; 
 
            public LogBuffer Next = null;
 
            readonly Event[] logEvents = new Event[Capacity];
            readonly bool[] written = new bool[Capacity];
 
            int first;
            int last;
 
            int lastRead
            {
                get
                {
                    for (int i = 0; i < Capacity; i++)
                    {
                        if (written[i] == false)
                            return i;
                    }
                    return Capacity;
                }
            }
 
            public bool Empty
            {
                get { return (last <= first) || (first >= Capacity); }
            }
 
            public bool Done
            {
                get { return last >= (Capacity - 1); }
            }
 
            public bool PushMessage(string source, string message, long time, long duration, int eventType)
            {
                var index = Interlocked.Increment(ref last) - 1;
 
                if (index > (Capacity - 1))
                    return false;
 
                logEvents[index] = new Event
                {
                    Source = source, Message = message, Timestamp = time, DurationNS = duration, EventType = eventType
                };
                
                written[index] = true;
 
                return true;
            }
 
            public bool PushEvent(Event evt)
            {
                var index = Interlocked.Increment(ref last) - 1;
 
                if (index > (Capacity - 1))
                    return false;
 
                logEvents[index] = evt;
                
                written[index] = true;
 
                return true;
            }
 
            public ArraySegment<Event> PopCurrent()
            {
                int oldFirst = first;
                first = lastRead;
                int newFirst = first;
 
                if (newFirst > Capacity)
                    newFirst = Capacity;
 
                if (newFirst > oldFirst)
                    return new ArraySegment<Event>(logEvents, oldFirst, newFirst - oldFirst);
 
                return new ArraySegment<Event>(logEvents, 0, 0);
            }
        }
 
        private LogBuffer _first;
        private LogBuffer _last;
        private long _postedMessages;
 
        public long PostedMessages
        {
            get
            {
                return _postedMessages;
            }
 
            set
            {
                _postedMessages = value;
            }
        }
 
        readonly object lck = new object();
 
        /// <summary> Prevent too much data from being written to the buffer.</summary>
        void maybeWaitForProcessing()
        {
            // Check Capacity for the size of each buffer.
            // if there are more than 3 x Capacity we wait for them to be processed.
            
            if (_first?.Next?.Next?.Next != null)
                TapThread.Sleep(10);
        }
        
        public void Enqueue(string source, string message, long time, long duration, int eventType)
        {
            maybeWaitForProcessing();
            Interlocked.Increment(ref _postedMessages);
 
            while (true)
            {
                LogBuffer buf = _last;
 
                if (!buf.PushMessage(source, message, time, duration, eventType))
                {
                    lock (lck)
                    {
                        if (!buf.Done)
                            continue;
                        LogBuffer nb = new LogBuffer();
                        if (Interlocked.CompareExchange(ref _last, nb, buf) == buf)
                            buf.Next = nb;
                    }
                }
                else
                {
                    break;
                }
            }
        }
        public void Enqueue(Event evt)
        {
            maybeWaitForProcessing();
            Interlocked.Increment(ref _postedMessages);
 
            while (true)
            {
                LogBuffer buf = _last;
 
                if (!buf.PushEvent(evt))
                {
                    lock (lck)
                    {
                        if (!buf.Done)
                            continue;
                        LogBuffer nb = new LogBuffer();
                        if (Interlocked.CompareExchange(ref _last, nb, buf) == buf)
                            buf.Next = nb;
                    }
                }
                else
                {
                    break;
                }
            }
        }
 
        public Event[] DequeueBunch()
        {
            LogBuffer buf = _first;
            if (buf.Empty)
            {
                if (buf.Done)
                {
                    var oldFirst = _first;
                    var next = oldFirst.Next;
 
                    if (next != null)
                        Interlocked.CompareExchange(ref _first, next, oldFirst);
                }
 
                return null;
            }
 
            var res = buf.PopCurrent();
 
            if (buf.Done && buf.Empty)
            {
                var oldFirst1 = _first;
                var next1 = oldFirst1.Next;
 
                if (next1 != null)
                    Interlocked.CompareExchange(ref _first, next1, oldFirst1);
            }
 
            if ((res.Offset == 0) && (res.Count == res.Array.Length))
            {
                return res.Array;
            }
 
            Event[] res2 = new Event[res.Count];
            Array.Copy(res.Array, res.Offset, res2, 0, res.Count);
            return res2;
        }
 
        public int DequeueBunch(ref Event[] into)
        {
            LogBuffer buf = _first;
            if (buf.Empty)
            {
                if (buf.Done)
                {
                    var oldFirst = _first;
                    var next = oldFirst.Next;
 
                    if (next != null)
                        Interlocked.CompareExchange(ref _first, next, oldFirst);
                }
                return 0;
            }
 
            var res = buf.PopCurrent();
 
            if (buf.Done && buf.Empty)
            {
                var oldFirst1 = _first;
                var next1 = oldFirst1.Next;
 
                if (next1 != null)
                    Interlocked.CompareExchange(ref _first, next1, oldFirst1);
            }
 
            if (res.Count != into.Length)
            {
                Array.Resize(ref into, res.Count);
            }
 
            Array.Copy(res.Array, res.Offset, into, 0, res.Count);
            return res.Count;
        }
 
        public LogQueue()
        {
            _first = new LogBuffer();
            _last = _first;
        }
 
        public bool IsEmpty
        {
            get
            {
                LogBuffer buf = _first;
 
                return buf.Empty && (buf.Next == null);
            }
        }
    }
}