##// 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 [Fact]
92 [Fact]
93 public void JsonXmlReaderSimpleTest() {
93 public void JsonXmlReaderSimpleTest() {
94 var json = "\"some text\"";
94 //var json = "\"some text\"";
95 //Console.WriteLine($"JSON: {json}");
95 //Console.WriteLine($"JSON: {json}");
96 //Console.WriteLine("XML");
96 //Console.WriteLine("XML");
97 /*using (var xmlReader = new JsonXmlReader(new JSONParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", RootName = "string", NodesPrefix = "json" })) {
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 static TraceSource CreateChannel() {
19 static TraceSource CreateChannel() {
20 var id = Interlocked.Increment(ref _nextId);
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 public static TraceSource TraceSource { get { return _traceSource.Value; } }
28 public static TraceSource TraceSource { get { return _traceSource.Value; } }
25
29
26 public static IDisposable Subscribe() {
27
28 throw new NotImplementedException();
29 }
30
31 #if NETFX_TRACE_BUG
30 #if NETFX_TRACE_BUG
32 readonly static AsyncLocal<object> m_currentOperation = new AsyncLocal<object>();
31 readonly static AsyncLocal<object> m_currentOperation = new AsyncLocal<object>();
33 #endif
32 #endif
@@ -10,15 +10,16 using System.Threading.Tasks;
10 namespace Implab.Formats {
10 namespace Implab.Formats {
11
11
12 /// <summary>
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 /// </summary>
14 /// </summary>
15 /// <typeparam name="TTag"></typeparam>
15 /// <typeparam name="TTag"></typeparam>
16 /// <remarks>
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.
17 /// <para>
18 ///
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 /// Each entry is addressed as <c>(state << 8) | input</c> which make this quite fast to get the next state.
19 /// </para>
20 ///
20 /// <para>
21 /// Each input symbol below 255 is treated as 255.
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 /// </remarks>
23 /// </remarks>
23 public class FastInputScanner<TTag> {
24 public class FastInputScanner<TTag> {
24 const int StateShift = 8;
25 const int StateShift = 8;
@@ -4,6 +4,16 using System;
4 using System.Collections;
4 using System.Collections;
5
5
6 namespace Implab.Parallels {
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 public class SimpleAsyncQueue<T> : IEnumerable<T> {
17 public class SimpleAsyncQueue<T> : IEnumerable<T> {
8 class Node {
18 class Node {
9 public Node(T value) {
19 public Node(T value) {
@@ -13,11 +23,11 namespace Implab.Parallels {
13 public volatile Node next;
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 // the reader can read next item when m_first.next is not null
27 // the reader can read next item when m_first.next is not null
18 // the writer creates a new node, moves m_last to this node and
28 // the writer creates a new node, moves m_last to this node and
19 // only after that restores the reference from the previous node
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 volatile Node m_first; // position on the node which is already read
32 volatile Node m_first; // position on the node which is already read
23 volatile Node m_last; // position on the node which is already written
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 public bool TryDequeue(out T value) {
51 public bool TryDequeue(out T value) {
42 Node first = m_first; ;
52 Node first = m_first;
43 Node next = first.next; ;
53 Node next = first.next;
44
54
45 if (next == null) {
55 if (next == null) {
46 value = default(T);
56 value = default(T);
@@ -72,69 +82,34 namespace Implab.Parallels {
72 return true;
82 return true;
73 }
83 }
74
84
75 #region IEnumerable implementation
85 /// <summary>
76
86 /// Creates a thin copy of the current linked list.
77 class Enumerator : IEnumerator<T> {
87 /// </summary>
78 Node m_current;
88 /// <remarks>Iterating over the snapshot is thread safe and
79 Node m_first;
89 /// will produce repeatble results. Each snapshot stores only
80
90 /// two references one for the first and one for last elements
81 public Enumerator(Node first) {
91 /// from list.
82 m_first = first;
92 /// <returns>Enumerable collection.</returns>
83 }
93 public IEnumerable<T> Snapshot() {
84
94 var first = m_first;
85 #region IEnumerator implementation
95 var last = m_last;
86
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
96 object IEnumerator.Current {
97 var current = m_first;
97 get {
98 while(current != m_last) {
98 if (m_current == null)
99 current = current.next;
99 throw new InvalidOperationException();
100 yield return current.value;
100 return m_current.value;
101 }
102 }
103
104 #endregion
105
106 #region IDisposable implementation
107
108 public void Dispose() {
109 }
101 }
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;
120 }
121 }
122
123 #endregion
124 }
102 }
125
103
126 public IEnumerator<T> GetEnumerator() {
104 public IEnumerator<T> GetEnumerator() {
127 return new Enumerator(m_first);
105 for (var current = m_first.next; current != null; current = current.next) {
106 yield return current.value;
107 }
128 }
108 }
129
109
130 #endregion
131
132 #region IEnumerable implementation
133
134 IEnumerator IEnumerable.GetEnumerator() {
110 IEnumerator IEnumerable.GetEnumerator() {
135 return GetEnumerator();
111 return GetEnumerator();
136 }
112 }
137
113
138 #endregion
139 }
114 }
140 }
115 }
1 NO CONTENT: file was removed
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