##// END OF EJS Templates
working on diagnostics
cin -
r285:b6924f444abd v3
parent child
Show More
@@ -0,0 +1,22
1 using System.Diagnostics;
2
3 namespace Implab.Diagnostics
4 {
5 public class ChannelInfo
6 {
7 internal ChannelInfo(object id, TraceSource source) {
8 Debug.Assert(id != null);
9 Debug.Assert(source != null);
10
11 Id = id;
12 Source = source;
13
14 }
15
16 public object Id { get; private set; }
17
18 public TraceSource Source { get; private set; }
19
20
21 }
22 } No newline at end of file
@@ -0,0 +1,76
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using Implab.Parallels;
5
6 namespace Implab.Diagnostics {
7 public class TraceChannelRegistry {
8
9 class Subscription : IDisposable {
10 readonly Action<object> m_unsubscribe;
11
12 public Subscription(Action<object> unsubscribe) {
13 m_unsubscribe = unsubscribe;
14 }
15
16 public void Dispose() {
17 m_unsubscribe(this);
18 }
19 }
20
21 public static TraceChannelRegistry AllCannels { get; } = new TraceChannelRegistry();
22
23 readonly object m_lock = new object();
24
25 readonly Dictionary<object, Action<ChannelInfo>> m_subscriptions = new Dictionary<object, Action<ChannelInfo>>();
26 readonly SimpleAsyncQueue<ChannelInfo> m_channels = new SimpleAsyncQueue<ChannelInfo>();
27
28 internal void NotifyChannelCreated(ChannelInfo channel) {
29 // notifications can run in parallel
30 Action<ChannelInfo>[] handlers = null;
31
32 lock(m_lock) {
33 m_channels.Enqueue(channel);
34 if (m_subscriptions.Count > 0) {
35 handlers = new Action<ChannelInfo>[m_subscriptions.Count];
36 m_subscriptions.Values.CopyTo(handlers, 0);
37 }
38 }
39
40 if (handlers != null)
41 foreach(var h in handlers)
42 h(channel);
43 }
44
45 /// <summary>
46 /// Subscribes the specified handler to notifications about new trace
47 /// channels
48 /// </summary>
49 /// <param name="handler"></param>
50 /// <returns></returns>
51 public IDisposable Subscribe(Action<ChannelInfo> handler, bool existing) {
52 Safe.ArgumentNotNull(handler, nameof(handler));
53
54 var cookie = new Subscription(RemoveSubscription);
55
56 IEnumerable<ChannelInfo> snap;
57
58 lock(m_lock) {
59 m_subscriptions.Add(cookie, handler);
60 snap = m_channels.Snapshot();
61 }
62
63 if (existing) {
64 foreach(var c in snap)
65 handler(c);
66 }
67
68 return cookie;
69 }
70
71 void RemoveSubscription(object cookie) {
72 lock(m_lock)
73 m_subscriptions.Remove(cookie);
74 }
75 }
76 } No newline at end of file
@@ -91,7 +91,7 namespace Implab.Test {
91 91
92 92 [Fact]
93 93 public void JsonXmlReaderSimpleTest() {
94 var json = "\"some text\"";
94 //var json = "\"some text\"";
95 95 //Console.WriteLine($"JSON: {json}");
96 96 //Console.WriteLine("XML");
97 97 /*using (var xmlReader = new JsonXmlReader(new JSONParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", RootName = "string", NodesPrefix = "json" })) {
@@ -18,16 +18,15 namespace Implab.Diagnostics {
18 18
19 19 static TraceSource CreateChannel() {
20 20 var id = Interlocked.Increment(ref _nextId);
21 return new TraceSource(typeof(T).Name);
21 var trs = new TraceSource(typeof(T).Name);
22
23 TraceChannelRegistry.AllCannels.NotifyChannelCreated(new ChannelInfo(typeof(T), trs));
24
25 return trs;
22 26 }
23 27
24 28 public static TraceSource TraceSource { get { return _traceSource.Value; } }
25 29
26 public static IDisposable Subscribe() {
27
28 throw new NotImplementedException();
29 }
30
31 30 #if NETFX_TRACE_BUG
32 31 readonly static AsyncLocal<object> m_currentOperation = new AsyncLocal<object>();
33 32 #endif
@@ -10,15 +10,16 using System.Threading.Tasks;
10 10 namespace Implab.Formats {
11 11
12 12 /// <summary>
13 /// Fast input scanner for max 255 states and first 255 input chacters.
13 /// Fast input scanner for max 255 states and 255 input chacters.
14 14 /// </summary>
15 15 /// <typeparam name="TTag"></typeparam>
16 16 /// <remarks>
17 /// Creates a one rank array to store automa transition table, each entry in this table is byte, to make this table fit into L1 cache.
18 ///
19 /// Each entry is addressed as <c>(state << 8) | input</c> which make this quite fast to get the next state.
20 ///
21 /// Each input symbol below 255 is treated as 255.
17 /// <para>
18 /// Creates a one rank array to store automa transition table, each entry in this table is byte, to make this table small enough to fit L1 cache.
19 /// </para>
20 /// <para>
21 /// Each entry is addressed as <c>(state << 8) | input</c> which make this quite fast to get the next state. Each input symbol below 255 is treated as 255.
22 /// </para>
22 23 /// </remarks>
23 24 public class FastInputScanner<TTag> {
24 25 const int StateShift = 8;
@@ -4,6 +4,16 using System;
4 4 using System.Collections;
5 5
6 6 namespace Implab.Parallels {
7
8 /// <summary>
9 /// Very simple thred-safe FIFO queue based on the sinle linked list.
10 /// </summary>
11 /// <typeparam name="T"></typeparam>
12 /// <remarks>
13 /// This queue uses interlocked operations to add and remove nodes,
14 /// each node stores a single value. The queue provides mean performance,
15 /// moderate overhead and situable for a small amount of elements.
16 /// </remarks>
7 17 public class SimpleAsyncQueue<T> : IEnumerable<T> {
8 18 class Node {
9 19 public Node(T value) {
@@ -13,11 +23,11 namespace Implab.Parallels {
13 23 public volatile Node next;
14 24 }
15 25
16 // the reader and the writer are mainteined completely independent,
26 // the reader and the writer are maintained completely independent,
17 27 // the reader can read next item when m_first.next is not null
18 28 // the writer creates a new node, moves m_last to this node and
19 29 // only after that restores the reference from the previous node
20 // making the reader be able to read the new node.
30 // making the reader able to read the new node.
21 31
22 32 volatile Node m_first; // position on the node which is already read
23 33 volatile Node m_last; // position on the node which is already written
@@ -39,8 +49,8 namespace Implab.Parallels {
39 49 }
40 50
41 51 public bool TryDequeue(out T value) {
42 Node first = m_first; ;
43 Node next = first.next; ;
52 Node first = m_first;
53 Node next = first.next;
44 54
45 55 if (next == null) {
46 56 value = default(T);
@@ -72,69 +82,34 namespace Implab.Parallels {
72 82 return true;
73 83 }
74 84
75 #region IEnumerable implementation
76
77 class Enumerator : IEnumerator<T> {
78 Node m_current;
79 Node m_first;
80
81 public Enumerator(Node first) {
82 m_first = first;
83 }
84
85 #region IEnumerator implementation
85 /// <summary>
86 /// Creates a thin copy of the current linked list.
87 /// </summary>
88 /// <remarks>Iterating over the snapshot is thread safe and
89 /// will produce repeatble results. Each snapshot stores only
90 /// two references one for the first and one for last elements
91 /// from list.
92 /// <returns>Enumerable collection.</returns>
93 public IEnumerable<T> Snapshot() {
94 var first = m_first;
95 var last = m_last;
86 96
87 public bool MoveNext() {
88 m_current = m_current == null ? m_first : m_current.next;
89 return m_current != null;
90 }
91
92 public void Reset() {
93 m_current = null;
94 }
95
96 object IEnumerator.Current {
97 get {
98 if (m_current == null)
99 throw new InvalidOperationException();
100 return m_current.value;
97 var current = m_first;
98 while(current != m_last) {
99 current = current.next;
100 yield return current.value;
101 101 }
102 102 }
103 103
104 #endregion
105
106 #region IDisposable implementation
107
108 public void Dispose() {
109 }
110
111 #endregion
112
113 #region IEnumerator implementation
114
115 public T Current {
116 get {
117 if (m_current == null)
118 throw new InvalidOperationException();
119 return m_current.value;
104 public IEnumerator<T> GetEnumerator() {
105 for (var current = m_first.next; current != null; current = current.next) {
106 yield return current.value;
120 107 }
121 108 }
122 109
123 #endregion
124 }
125
126 public IEnumerator<T> GetEnumerator() {
127 return new Enumerator(m_first);
128 }
129
130 #endregion
131
132 #region IEnumerable implementation
133
134 110 IEnumerator IEnumerable.GetEnumerator() {
135 111 return GetEnumerator();
136 112 }
137 113
138 #endregion
139 114 }
140 115 }
1 NO CONTENT: file was removed
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

ok, latest stable version should be in default

You need to be logged in to leave comments. Login now