@@ -0,0 +1,15 | |||
|
1 | namespace Implab.Diagnostics { | |
|
2 | public static class Extensions { | |
|
3 | public static IPromise<T> EndLogicalOperation<T>(this IPromise<T> promise) { | |
|
4 | Safe.ArgumentNotNull(promise, "promise"); | |
|
5 | var op = TraceContext.Instance.DetachLogicalOperation(); | |
|
6 | ||
|
7 | return promise.Anyway(() => { | |
|
8 | TraceContext.Instance.EnterLogicalOperation(op,true); | |
|
9 | TraceLog.EndLogicalOperation(); | |
|
10 | TraceContext.Instance.Leave(); | |
|
11 | }); | |
|
12 | } | |
|
13 | } | |
|
14 | } | |
|
15 |
@@ -0,0 +1,24 | |||
|
1 | using System; | |
|
2 | ||
|
3 | namespace Implab.Diagnostics { | |
|
4 | public class LogEventArgs : EventArgs { | |
|
5 | public int ThreadId { | |
|
6 | get; | |
|
7 | private set; | |
|
8 | } | |
|
9 | public LogicalOperation Operation { | |
|
10 | get; | |
|
11 | private set; | |
|
12 | } | |
|
13 | public int OperationTimeOffset { | |
|
14 | get; | |
|
15 | private set; | |
|
16 | } | |
|
17 | public LogEventArgs(int threadId, LogicalOperation operation, int timeOffset) { | |
|
18 | ThreadId = threadId; | |
|
19 | Operation = operation; | |
|
20 | OperationTimeOffset = timeOffset; | |
|
21 | } | |
|
22 | } | |
|
23 | } | |
|
24 |
@@ -0,0 +1,13 | |||
|
1 | namespace Implab.Diagnostics { | |
|
2 | public class LogEventArgs<TEvent> : LogEventArgs { | |
|
3 | public TEvent Value { | |
|
4 | get; | |
|
5 | private set; | |
|
6 | } | |
|
7 | ||
|
8 | public LogEventArgs(TEvent value, int threadId, LogicalOperation operation, int timeOffset) : base(threadId, operation, timeOffset) { | |
|
9 | Value = value; | |
|
10 | } | |
|
11 | } | |
|
12 | } | |
|
13 |
@@ -0,0 +1,49 | |||
|
1 | namespace Implab.Diagnostics { | |
|
2 | struct OperationContext { | |
|
3 | readonly LogicalOperation m_initial; | |
|
4 | public readonly static OperationContext EMPTY = new OperationContext(LogicalOperation.EMPTY, false); | |
|
5 | LogicalOperation m_current; | |
|
6 | readonly bool m_ownership; | |
|
7 | ||
|
8 | public OperationContext(LogicalOperation operation, bool ownership) { | |
|
9 | Safe.ArgumentNotNull(operation, "operation"); | |
|
10 | ||
|
11 | m_initial = operation; | |
|
12 | m_current = operation; | |
|
13 | m_ownership = ownership; | |
|
14 | } | |
|
15 | ||
|
16 | public LogicalOperation CurrentOperation { | |
|
17 | get { return m_current; } | |
|
18 | } | |
|
19 | ||
|
20 | public void BeginLogicalOperation(string name) { | |
|
21 | m_current = new LogicalOperation(name, m_current); | |
|
22 | } | |
|
23 | ||
|
24 | public LogicalOperation DetachLogicalOperation() { | |
|
25 | var detached = m_current; | |
|
26 | if (m_current != LogicalOperation.EMPTY) { | |
|
27 | if (m_current != m_initial) | |
|
28 | m_current = m_current.Parent; | |
|
29 | else if (m_ownership) | |
|
30 | m_current = LogicalOperation.EMPTY; | |
|
31 | else | |
|
32 | detached = LogicalOperation.EMPTY; | |
|
33 | } | |
|
34 | TraceLog.TraceWarning("EndLogicalOperation can't be applied in the current context"); | |
|
35 | return detached; | |
|
36 | } | |
|
37 | ||
|
38 | public void EndLogicalOperation() { | |
|
39 | if (m_current != m_initial) { | |
|
40 | m_current = m_current.Parent; | |
|
41 | } else if (m_current != null && m_ownership) { | |
|
42 | m_current = null; | |
|
43 | } else { | |
|
44 | TraceLog.TraceWarning("EndLogicalOperation can't be applied in the current context"); | |
|
45 | } | |
|
46 | } | |
|
47 | } | |
|
48 | } | |
|
49 |
@@ -107,11 +107,11 namespace Implab.Diagnostics.Interactive | |||
|
107 | 107 | base.Dispose(disposing); |
|
108 | 108 | } |
|
109 | 109 | |
|
110 |
protected override void WriteEntry( |
|
|
110 | protected override void WriteEntry(LogEventArgs args, EventText text, string channel) { | |
|
111 | 111 | var item = new TraceViewItem { |
|
112 | 112 | Indent = text.indent, |
|
113 | 113 | Message = text.content, |
|
114 |
Thread = |
|
|
114 | Thread = args.ThreadId, | |
|
115 | 115 | Channel = channel, |
|
116 | 116 | Timestamp = Environment.TickCount |
|
117 | 117 | }; |
@@ -18,16 +18,16 namespace Implab.Diagnostics { | |||
|
18 | 18 | |
|
19 | 19 | } |
|
20 | 20 | |
|
21 |
protected override void WriteEntry( |
|
|
21 | protected override void WriteEntry(LogEventArgs args, EventText text, string channel) { | |
|
22 | 22 | var msg = new StringBuilder(); |
|
23 | 23 | |
|
24 | 24 | for (int i = 0; i < text.indent; i++) |
|
25 | 25 | msg.Append(" "); |
|
26 |
msg.AppendFormat("[{0}]:{1}: {2}", |
|
|
26 | msg.AppendFormat("[{0}]:{1}: {2}", args.ThreadId, channel, text.content); | |
|
27 | 27 | |
|
28 | 28 | lock (_consoleLock) { |
|
29 |
Console.ForegroundColor = (ConsoleColor)( |
|
|
30 |
Console.WriteLine(msg |
|
|
29 | Console.ForegroundColor = (ConsoleColor)(args.ThreadId % 15 + 1); | |
|
30 | Console.WriteLine(msg); | |
|
31 | 31 | } |
|
32 | 32 | } |
|
33 | 33 | } |
@@ -1,10 +1,5 | |||
|
1 | using System; | |
|
2 | using System.Collections.Generic; | |
|
3 | using System.Linq; | |
|
4 | using System.Text; | |
|
5 | ||
|
6 | namespace Implab.Diagnostics { | |
|
1 | namespace Implab.Diagnostics { | |
|
7 | 2 | public interface IEventTextFormatter<in TEvent> { |
|
8 |
EventText Format( |
|
|
3 | EventText Format(LogEventArgs args, TEvent data); | |
|
9 | 4 | } |
|
10 | 5 | } |
@@ -27,8 +27,8 namespace Implab.Diagnostics { | |||
|
27 | 27 | /// <summary> |
|
28 | 28 | /// Π‘ΠΎΠ±ΡΡΠΈΠ΅ ΠΏΠΎΡΠ²Π»Π΅Π½ΠΈΠ΅ Π½ΠΎΠ²ΠΎΠΉ Π·Π°ΠΏΠΈΡΠΈ Π² ΠΆΡΡΠ½Π°Π»Π΅, Π½Π° ΡΡΠΎ ΡΠΎΠ±ΡΡΠΈΠ΅ ΠΏΠΎΠ΄ΠΏΠΈΡΡΠ²Π°ΡΡΡΡ ΡΠ»ΡΡΠ°ΡΠ΅Π»ΠΈ. |
|
29 | 29 | /// </summary> |
|
30 |
public event EventHandler< |
|
|
31 | ||
|
30 | public event EventHandler<LogEventArgs<TEvent>> Events; | |
|
31 | ||
|
32 | 32 | /// <summary> |
|
33 | 33 | /// ΠΠΌΡ ΠΊΠ°Π½Π°Π»Π°, ΠΏΠΎΠ»Π΅Π·Π½ΠΎ Π΄Π»Ρ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Π² ΠΆΡΡΠ½Π°Π»Π΅ |
|
34 | 34 | /// </summary> |
@@ -63,19 +63,18 namespace Implab.Diagnostics { | |||
|
63 | 63 | /// </remarks> |
|
64 | 64 | public void LogEvent(TEvent data) { |
|
65 | 65 | var t = Events; |
|
66 | if (t!= null) | |
|
67 | t(TraceContext.Current,new ValueEventArgs<TEvent>(data)); | |
|
68 | } | |
|
69 | ||
|
70 | /// <summary> | |
|
71 | /// ΠΡΠΏΡΠ°Π²Π»ΡΠ΅Ρ Π·Π°ΠΏΠΈΡΡ ΠΆΡΡΠ½Π°Π»Π° ΡΠ΅ΡΠ΅Π· ΠΊΠ°Π½Π°Π» ΠΏΠΎΠ΄ΠΏΠΈΡΡΠΈΠΊΠ°ΠΌ. | |
|
72 | /// </summary> | |
|
73 | /// <param name="data">ΠΠ°ΠΏΠΈΡΡ ΠΆΡΡΠ½Π°Π»Π°.</param> | |
|
74 | /// <param name="context">ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΎΡ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΡΠ°ΡΡΡΠ»Π°Π΅ΡΡΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅/</param> | |
|
75 | public void LogEvent(TraceContext context,TEvent data) { | |
|
76 |
|
|
|
77 |
|
|
|
78 | t(context, new ValueEventArgs<TEvent>(data)); | |
|
66 | if (t != null) { | |
|
67 | var traceContext = TraceContext.Instance; | |
|
68 | t( | |
|
69 | this, | |
|
70 | new LogEventArgs<TEvent>( | |
|
71 | data, | |
|
72 | traceContext.ThreadId, | |
|
73 | traceContext.CurrentOperation, | |
|
74 | traceContext.CurrentOperation.Duration | |
|
75 | ) | |
|
76 | ); | |
|
77 | } | |
|
79 | 78 | } |
|
80 | 79 | } |
|
81 | 80 | } |
@@ -1,11 +1,9 | |||
|
1 | 1 | using System; |
|
2 | using System.Collections.Generic; | |
|
3 | using System.Linq; | |
|
4 | using System.Text; | |
|
5 | using System.Threading.Tasks; | |
|
6 | 2 | |
|
7 | 3 | namespace Implab.Diagnostics { |
|
8 | 4 | public class LogicalOperation { |
|
5 | public static readonly LogicalOperation EMPTY = new LogicalOperation("__EMPTY__", null); | |
|
6 | ||
|
9 | 7 | readonly LogicalOperation m_parent; |
|
10 | 8 | readonly string m_name; |
|
11 | 9 | readonly int m_level; |
@@ -1,7 +1,5 | |||
|
1 | 1 | using System; |
|
2 | using System.Collections.Generic; | |
|
3 | 2 | using System.IO; |
|
4 | using System.Linq; | |
|
5 | 3 | using System.Text; |
|
6 | 4 | |
|
7 | 5 | namespace Implab.Diagnostics { |
@@ -16,16 +14,16 namespace Implab.Diagnostics { | |||
|
16 | 14 | Register(this); |
|
17 | 15 | } |
|
18 | 16 | |
|
19 |
protected override void WriteEntry( |
|
|
17 | protected override void WriteEntry(LogEventArgs args, EventText text, string channel) { | |
|
20 | 18 | var msg = new StringBuilder(); |
|
21 | 19 | for (int i = 0; i < text.indent; i++) |
|
22 | 20 | msg.Append(" "); |
|
23 |
msg.AppendFormat("[{0}]:{1}: {2}", |
|
|
21 | msg.AppendFormat("[{0}]:{1}: {2}", args.ThreadId, channel, text.content); | |
|
24 | 22 | |
|
25 | 23 | lock (m_textWriter) { |
|
26 | 24 | if (!IsDisposed) { |
|
27 | 25 | // ΡΡΡ Π³Π°ΡΠ°Π½ΡΠΈΡΠΎΠ²Π°Π½ΠΎ Π΅ΡΠ΅ Π½Π΅ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ m_textWriter |
|
28 |
m_textWriter.WriteLine(msg |
|
|
26 | m_textWriter.WriteLine(msg); | |
|
29 | 27 | m_textWriter.Flush(); |
|
30 | 28 | } |
|
31 | 29 | } |
@@ -13,7 +13,7 namespace Implab.Diagnostics { | |||
|
13 | 13 | protected TextListenerBase(bool global) { |
|
14 | 14 | Register(this); |
|
15 | 15 | if (!global) { |
|
16 |
m_boundOperation = TraceContext. |
|
|
16 | m_boundOperation = TraceContext.Instance.CurrentOperation; | |
|
17 | 17 | m_baseIndent = Math.Max(0, m_boundOperation.Level - 1); |
|
18 | 18 | } |
|
19 | 19 | } |
@@ -38,13 +38,12 namespace Implab.Diagnostics { | |||
|
38 | 38 | var formatter = GetService<IEventTextFormatter<TEvent>>(); |
|
39 | 39 | var channelName = channel.Name; |
|
40 | 40 | |
|
41 |
EventHandler< |
|
|
42 | TraceContext context = (TraceContext)sender; | |
|
43 | var text = formatter.Format(context, args.Value); | |
|
41 | EventHandler<LogEventArgs<TEvent>> handler = (sender, args) => { | |
|
42 | var text = formatter.Format(args, args.Value); | |
|
44 | 43 | text.indent -= m_baseIndent; |
|
45 | 44 | |
|
46 |
if (IsRelated( |
|
|
47 |
WriteEntry( |
|
|
45 | if (IsRelated(args.Operation)) | |
|
46 | WriteEntry(args, text, channelName); | |
|
48 | 47 | }; |
|
49 | 48 | |
|
50 | 49 | if (m_subscriptions.ContainsKey(channel)) |
@@ -97,19 +96,19 namespace Implab.Diagnostics { | |||
|
97 | 96 | /// ΠΠ°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ ΠΌΠΎΠΆΠ΅Ρ Π²ΡΠ·Π²Π°ΡΡΡΡ ΠΈΠ· ΡΠ°Π·Π½ΡΡ ΠΏΠΎΡΠΎΠΊΠΎΠ² ΠΎΠ΄Π½ΠΎΠ²ΡΠ΅ΠΌΠ΅Π½Π½ΠΎ. ΠΠΎΠ·ΠΌΠΎΠΆΠ½Π° ΡΠΈΡΡΠ°ΡΠΈΡ, ΠΊΠΎΠ³Π΄Π° |
|
98 | 97 | /// Π΄Π°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΡΠΆΠ΅ ΠΏΠΎΡΠ»Π΅ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΡ ΠΎΠΎΠ±ΡΠ΅ΠΊΡΠ° ΠΌΠ΅ΡΠΎΠ΄ΠΎΠΌ <see cref="Dispose()"/>. |
|
99 | 98 | /// </remarks> |
|
100 | /// <param name="context">ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ.</param> | |
|
101 | 99 | /// <param name="text">Π’Π΅ΠΊΡΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ.</param> |
|
102 | protected abstract void WriteEntry(TraceContext context, EventText text, string channel); | |
|
100 | /// <param name = "channel"></param> | |
|
101 | protected abstract void WriteEntry(LogEventArgs args, EventText text, string channel); | |
|
103 | 102 | |
|
104 |
public EventText Format( |
|
|
103 | public EventText Format(LogEventArgs args, object data) { | |
|
105 | 104 | return new EventText { |
|
106 |
indent = |
|
|
105 | indent = args.Operation.Level, | |
|
107 | 106 | content = data.ToString() |
|
108 | 107 | }; |
|
109 | 108 | } |
|
110 | 109 | |
|
111 |
public EventText Format( |
|
|
112 |
var level = |
|
|
110 | public EventText Format(LogEventArgs args, TraceEvent data) { | |
|
111 | var level = args.Operation.Level; | |
|
113 | 112 | if (data.EventType == TraceEventType.OperationCompleted || data.EventType == TraceEventType.OperationStarted) |
|
114 | 113 | level--; |
|
115 | 114 |
@@ -1,238 +1,79 | |||
|
1 | using System; | |
|
2 | using System.Collections.Generic; | |
|
3 |
using System. |
|
|
4 | using System.Text; | |
|
5 | using System.Threading; | |
|
6 | using System.Threading.Tasks; | |
|
7 | ||
|
8 | namespace Implab.Diagnostics { | |
|
9 |
/// < |
|
|
10 | /// ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, ΠΏΡΠΈΠ²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΠΊ ΠΏΠΎΡΠΎΠΊΡ ΠΈ ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ Π² ΡΠ΅Π±Π΅ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ ΡΡΠ΅ΠΊΠ΅ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ. | |
|
11 |
/// </ |
|
|
12 | /// <remarks> | |
|
13 | /// ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΡΡΡ ΡΠ»ΡΡΠ°ΡΠ΅Π»ΡΠΌ ΡΠΎΠ±ΡΡΠΈΠΉ Π΄Π»Ρ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΡ ΠΌΠ΅ΡΡΠ°, Π³Π΄Π΅ Π²ΠΎΠ·Π½ΠΈΠΊΠ»ΠΎ ΡΠΎΠ±ΡΡΠΈΠ΅. | |
|
14 | /// </remarks> | |
|
15 | public class TraceContext { | |
|
16 | LogicalOperation m_currentOperation; | |
|
17 | readonly LogicalOperation m_bound; | |
|
18 | readonly int m_threadId; | |
|
19 | ||
|
20 | [ThreadStatic] | |
|
21 |
static TraceContext |
|
|
22 | ||
|
23 | /// <summary> | |
|
24 | /// Π’Π΅ΠΊΡΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ Π΄Π»Ρ ΠΏΠΎΡΠΎΠΊΠ°, ΡΠΎΠ·Π΄Π°Π΅ΡΡΡ Π°ΡΡΠΎΠΌΠ°ΡΠΈΡΠ΅ΡΠΊΠΈ ΠΏΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ. | |
|
25 | /// </summary> | |
|
26 | public static TraceContext Current { | |
|
27 |
|
|
|
28 | if (_current == null) { | |
|
29 | _current = new TraceContext(); | |
|
30 | _current.LogEvent(TraceEventType.Created,"[{0}]", _current.ThreadId); | |
|
31 |
|
|
|
32 | return _current; | |
|
33 | } | |
|
34 | } | |
|
35 | ||
|
36 | TraceContext(TraceContext context) | |
|
37 | : this(context, false) { | |
|
38 |
|
|
|
39 | ||
|
40 | TraceContext(TraceContext context, bool attach) { | |
|
41 | if (context == null) | |
|
42 | throw new ArgumentNullException("context"); | |
|
43 | ||
|
44 | m_currentOperation = context.CurrentOperation; | |
|
45 | m_bound = attach ? context.BoundOperation : context.CurrentOperation; | |
|
46 | m_threadId = Thread.CurrentThread.ManagedThreadId; | |
|
47 | } | |
|
48 | ||
|
49 | TraceContext() { | |
|
50 |
m_current |
|
|
51 | m_bound = m_currentOperation; | |
|
52 | m_threadId = Thread.CurrentThread.ManagedThreadId; | |
|
53 | } | |
|
54 | ||
|
55 | /// <summary> | |
|
56 | /// ΠΡΠΈ Π½Π΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌΠΎΡΡΠΈ ΠΊΠΎΠΏΠΈΡΡΠ΅Ρ ΡΠΎΡΡΠΎΡΠ½ΠΈΠ΅ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΠ²ΡΠΎΠ²ΠΊΠΈ Π² ΡΠ΅ΠΊΡΡΠΈΠΉ ΠΏΠΎΡΠΎΠΊ. | |
|
57 | /// </summary> | |
|
58 | /// <param name="from">ΠΡΡ ΠΎΠ΄Π½ΡΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΠ΅ΡΠ΅Π΄Π°Π΅ΡΡΡ.</param> | |
|
59 | /// <remarks> | |
|
60 | /// <para> | |
|
61 | /// ΠΠΎΠΏΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ ΠΏΡΠΎΠΈΡΡ ΠΎΠ΄ΠΈΡ Π·Π° ΡΡΠ΅Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΈ Π·Π°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ΠΌ Π΅Π³ΠΎ | |
|
62 | /// ΡΠΎΡΡΠΎΡΠ½ΠΈΡ ΠΈΠ· ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΠΎΠ³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°. ΠΡΠΈ ΡΡΠΎΠΌ ΠΊΠΎΠΏΠΈΡΡΠ΅ΡΡΡ ΡΡΠ΅ΠΊ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ, ΠΎΠ΄Π½Π°ΠΊΠΎ Π² Π½ΠΎΠ²ΠΎΠΌ | |
|
63 | /// ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ ΡΠ°Π½Π΅Π΅ Π½Π°ΡΠ°ΡΡΠ΅ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ Π½Π΅ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½Ρ. | |
|
64 | /// </para> | |
|
65 | /// <para> | |
|
66 | /// ΠΡΠ»ΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠ° ΡΠΎΡΡΠΎΡΠ½ΠΈΡ ΡΠΎΡΡΠΎΡΠ»Π°ΡΡ, ΡΠΎ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ ΡΠΎΠ±ΡΡΠΈΠ΅ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ <see cref="TraceEventType.Fork"/>. | |
|
67 | /// </para> | |
|
68 | /// </remarks> | |
|
69 | public static void Fork(TraceContext from) { | |
|
70 |
if (_cu |
|
|
71 |
|
|
|
72 |
|
|
|
73 | var context = new TraceContext(from); | |
|
74 | context.LogEvent(TraceEventType.Fork, "[{0}]-->[{1}]",from.ThreadId, context.ThreadId); | |
|
75 | _current = context; | |
|
76 |
|
|
|
77 | _current = new TraceContext(); | |
|
78 | } | |
|
79 | } | |
|
80 | ||
|
81 | /// <summary> | |
|
82 | /// ΠΠ°Π΄Π°Π΅Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΌΡ ΠΏΠΎΡΠΎΠΊΡ ΡΠΊΠ°Π·Π°Π½Π½ΡΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ, ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΏΠΎΡΠΎΠΊ ΠΌΠΎΠΆΠ΅Ρ Π·Π°ΠΊΠ°Π½ΡΠΈΠ²Π°ΡΡ ΡΠ°Π½Π΅Π΅ Π½Π°ΡΠ°ΡΡΠ΅ | |
|
83 | /// Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ Π² ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. | |
|
84 | /// </summary> | |
|
85 | /// <param name="source"></param> | |
|
86 | public static void Attach(TraceContext source) { | |
|
87 | if (_current == source) | |
|
88 | return; | |
|
89 | if (source != null) { | |
|
90 | var context = new TraceContext(source, true); | |
|
91 | context.LogEvent(TraceEventType.Attach, "[{0}]-->[{1}]", source.ThreadId, context.ThreadId); | |
|
92 | _current = context; | |
|
93 | } else { | |
|
94 | _current = new TraceContext(); | |
|
95 | } | |
|
96 | } | |
|
97 | ||
|
98 | /// <summary> | |
|
99 | /// ΠΡΡΠΎΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ ΡΠ΅ΠΊΡΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΎΡ ΠΏΠΎΡΠΎΠΊΠ°, Π΄Π»Ρ Π΄Π°Π»ΡΠ½Π΅ΠΉΡΠ΅ΠΉ Π΅Π³ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠΈ Π΄ΡΡΠ³ΠΎΠΌΡ ΠΏΠΎΡΠΎΠΊΡ | |
|
100 | /// <see cref="Attach(TraceContext)"/>. | |
|
101 | /// </summary> | |
|
102 | /// <returns>ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΏΠΎΡΠΎΠΊΠ°</returns> | |
|
103 | /// <remarks> | |
|
104 | /// ΠΠΎΡΠ»Π΅ ΠΎΡΡΠΎΠ΅Π΄ΠΈΠ½Π΅Π½ΠΈΡ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΎΡ ΠΏΠΎΡΠΎΠΊΠ°, ΠΏΡΠΈ ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΎΠ±ΡΠ°ΡΠ΅Π½ΠΈΠΈ ΠΊ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠ΅ Π² ΡΡΠΎΠΌ | |
|
105 | /// ΠΏΠΎΡΠΎΠΊΠ΅ Π±ΡΠ΄Π΅Ρ ΡΠΎΠ·Π΄Π°Π½ Π½ΠΎΠ²ΡΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ. | |
|
106 | /// </remarks> | |
|
107 | public static TraceContext Detach() { | |
|
108 | var context = Current; | |
|
109 | context.LogEvent(TraceEventType.Detach, null); | |
|
110 | _current = null; | |
|
111 | return context; | |
|
112 | } | |
|
113 | ||
|
114 | /// <summary> | |
|
115 | /// Π‘ΠΎΠ·Π΄Π°Π΅Ρ ΠΏΠΎΡΡΠΎΡΠ½Π½ΡΡ ΠΊΠΎΠΏΠΈΡ ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°, Π΄Π°Π½Π½ΡΡ ΠΊΠΎΠΏΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ Ρ ΡΠ°Π½ΠΈΡΡ ΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ Π΄Π»Ρ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΠΈ ΡΠ΅ΡΠ΅Π· <see cref="Fork(TraceContext)"/> | |
|
116 | /// </summary> | |
|
117 | /// <returns>ΠΠΎΠΏΠΈΡ ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ.</returns> | |
|
118 | public static TraceContext Snapshot() { | |
|
119 | return _current == null ? new TraceContext() : new TraceContext(_current,false); | |
|
120 | } | |
|
121 | ||
|
122 | /// <summary> | |
|
123 | /// ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΠΎΠ΅ Π΄Π΅ΠΉΡΡΠ²ΠΈΠ΅ Π² ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, ΠΏΠΎ ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΠΈ Π²ΠΎΡΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅Ρ ΠΏΡΠ΅Π΄ΡΠ΄ΡΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ ΠΏΠΎΡΠΎΠΊΠ°. | |
|
124 | /// </summary> | |
|
125 | /// <param name="action"></param> | |
|
126 | public void Invoke(Action action) { | |
|
127 | if (action == null) | |
|
128 | throw new ArgumentNullException("action"); | |
|
129 | var old = _current; | |
|
130 | Fork(this); | |
|
131 | try { | |
|
132 | action(); | |
|
133 | } finally { | |
|
134 | if(_current != null) | |
|
135 | _current.EndAllOperations(); | |
|
136 | _current = old; | |
|
137 | } | |
|
138 | } | |
|
139 | ||
|
140 | /// <summary> | |
|
141 | /// Π’Π΅ΠΊΡΡΠ°Ρ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠ°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ. | |
|
142 | /// </summary> | |
|
143 | public LogicalOperation CurrentOperation { | |
|
144 | get { | |
|
145 | return m_currentOperation; | |
|
146 | } | |
|
147 | } | |
|
148 | ||
|
149 | /// <summary> | |
|
150 | /// ΠΠΏΠ΅ΡΠ°ΡΠΈΡ Π½ΠΈΠΆΠ΅ ΠΊΠΎΡΠΎΡΠΎΠΉ Π½Π΅Π»ΡΠ·Ρ ΠΎΠΏΡΡΠΊΠ°ΡΡΡΡ Π² ΡΡΠ΅ΠΊΠ΅ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ, Ρ.Π΅. ΠΎΠ½Π° Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ Π·Π°Π²Π΅ΡΡΠ΅Π½Π° Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. | |
|
151 | /// </summary> | |
|
152 | public LogicalOperation BoundOperation { | |
|
153 | get { | |
|
154 | return m_bound; | |
|
155 | } | |
|
156 | } | |
|
157 | ||
|
158 | /// <summary> | |
|
159 | /// ΠΠΎΡΠΎΠΊ, Π² ΠΊΠΎΡΠΎΡΠΎΠΌ ΡΠΎΠ·Π΄Π°Π½ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ. | |
|
160 | /// </summary> | |
|
161 | public int ThreadId { | |
|
162 | get { | |
|
163 | return m_threadId; | |
|
164 | } | |
|
165 | } | |
|
166 | ||
|
167 | /// <summary> | |
|
168 | /// ΠΠ°ΡΠΈΠ½Π°Π΅Ρ Π±Π΅Π·ΡΠΌΡΠ½Π½ΡΡ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ. | |
|
169 | /// </summary> | |
|
170 | public void StartLogicalOperation() { | |
|
171 | StartLogicalOperation(null); | |
|
172 | } | |
|
173 | ||
|
174 | /// <summary> | |
|
175 | /// ΠΠ°ΡΠΈΠ½Π°Π΅Ρ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Ρ ΡΠΊΠ°Π·Π°Π½Π½ΡΠΌ ΠΈΠΌΠ΅Π½Π΅ΠΌ. Π‘ΠΎΠ·Π΄Π°Π½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ Π΄ΠΎΠ±Π²Π°Π»Π΅Π½Π° Π² ΡΡΠ΅ΠΊ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΈΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°, Π·Π°ΡΠ΅ΠΌ Π±ΡΠ΄Π΅Ρ ΡΠΎΠ·Π΄Π°Π½ΠΎ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠ΅Π΅ ΡΠΎΠ±ΡΡΠΈΠ΅. | |
|
176 | /// </summary> | |
|
177 | /// <param name="name">ΠΠΌΡ Π½Π°ΡΠΈΠ½Π°Π΅ΠΌΠΎΠΉ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ.</param> | |
|
178 | public void StartLogicalOperation(string name) { | |
|
179 | m_currentOperation = new LogicalOperation(name, m_currentOperation); | |
|
180 | LogEvent(TraceEventType.OperationStarted, name); | |
|
181 | } | |
|
182 | ||
|
183 | /// <summary> | |
|
184 | /// ΠΠ°ΠΊΠ°Π½ΡΠΈΠ²Π°Π΅Ρ Π»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π½Π°ΡΠ°ΡΡΡ Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. ΠΠΏΠ΅ΡΠ°ΡΠΈΠΈ, Π½Π°ΡΠ°ΡΡΠ΅ Π² Π΄ΡΡΠ³ΠΈΡ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ°Ρ Π½Π΅ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ Π·Π°ΠΊΠΎΠ½ΡΠ΅Π½Ρ Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. | |
|
185 | /// </summary> | |
|
186 | /// <remarks> | |
|
187 | /// ΠΡΠΈ Π²ΡΠ·ΠΎΠ²Π΅ Π΄Π°Π½Π½ΠΎΠ³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Π° ΡΠΎΠ·Π΄Π°Π΅ΡΡΡ ΡΠΎΠ±ΡΡΠΈΠ΅ ΠΆΡΡΠ½Π°Π»Π° ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, Π»ΠΈΠ±ΠΎ ΠΎ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ, Π»ΠΈΠ±ΠΎ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠΈ, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ Π΄Π°Π½Π½Π°Ρ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ | |
|
188 | /// Π½Π°ΡΠ°ΡΠ° Π² Π΄ΡΡΠ³ΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅. | |
|
189 | /// </remarks> | |
|
190 | public void EndLogicalOperation() { | |
|
191 | if (m_bound == m_currentOperation) { | |
|
192 | LogEvent(TraceEventType.Error, "Trying to end the operation which isn't belongs to current trace"); | |
|
193 | } else { | |
|
194 | var op = m_currentOperation; | |
|
195 | LogEvent(TraceEventType.OperationCompleted, "{0} {1} ms", op.Name, op.Duration); | |
|
196 | m_currentOperation = m_currentOperation.Parent; | |
|
197 | } | |
|
198 | } | |
|
199 | ||
|
200 | /// <summary> | |
|
201 | /// Π‘ΠΎΠ·Π΄Π°Π΅Ρ ΠΊΠΎΠΏΠΈΡ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ° ΠΈ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΡΡΡ Π½Π° ΠΏΡΠ΅Π΄ΡΠ΄ΡΡΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π² ΡΠ΅ΠΊΡΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅, ΡΡΠΎ ΠΏΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ Π½Π°ΡΠ°ΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ Π² ΠΎΠ΄Π½ΠΎΠΌ ΠΏΠΎΡΠΎΠΊΠ΅, Π° Π·Π°Π²Π΅ΡΡΠΈΡΡ - Π² Π΄ΡΡΠ³ΠΎΠΌ. | |
|
202 | /// </summary> | |
|
203 | /// <returns>ΠΠΎΠ½ΡΠ΅ΠΊΡΡ ΡΡΠ°ΡΡΠΈΡΠΎΠ²ΠΊΠΈ, ΠΊΠΎΡΠΎΡΡΠΉ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠΈΡΠΎΠ΅Π΄ΠΈΠ½ΠΈΡΡ ΠΊ Π΄ΡΡΠ³ΠΎΠΌΡ ΠΏΠΎΡΠΎΠΊΡ.</returns> | |
|
204 | public TraceContext DetachLogicalOperation() { | |
|
205 | if (m_bound == m_currentOperation) { | |
|
206 | return new TraceContext(); | |
|
207 | } else { | |
|
208 | var detached = new TraceContext(this, true); | |
|
209 | m_currentOperation = m_currentOperation.Parent; | |
|
210 | return detached; | |
|
211 | } | |
|
212 | } | |
|
213 | ||
|
214 | public void BindLogicalOperationToPromise(IPromise promise) { | |
|
215 | Safe.ArgumentNotNull(promise, "promise"); | |
|
216 | ||
|
217 | var ctx = DetachLogicalOperation(); | |
|
218 | promise.Anyway(() => { | |
|
219 | var old = _current; | |
|
220 | TraceContext.Attach(ctx); | |
|
221 | TraceContext.Current.EndLogicalOperation(); | |
|
222 | _current = old; | |
|
223 | }); | |
|
224 | } | |
|
225 | ||
|
226 | /// <summary> | |
|
227 | /// ΠΠ°Π²ΡΠ΅ΡΠ°Π΅Ρ Π²ΡΠ΅ Π½Π°ΡΠ°ΡΡΠ΅ Π² ΡΡΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΈ | |
|
228 | /// </summary> | |
|
229 | public void EndAllOperations() { | |
|
230 | while (m_bound != m_currentOperation) | |
|
231 | EndLogicalOperation(); | |
|
232 | } | |
|
233 | ||
|
234 | void LogEvent(TraceEventType type, string format, params object[] args) { | |
|
235 | LogChannel<TraceEvent>.Default.LogEvent(this, TraceEvent.Create(type, format, args)); | |
|
236 | } | |
|
237 | } | |
|
238 | } | |
|
1 | using System; | |
|
2 | using System.Collections.Generic; | |
|
3 | using System.Threading; | |
|
4 | ||
|
5 | namespace Implab.Diagnostics { | |
|
6 | /// <summary> | |
|
7 | /// Trace context is bound to the specific thread, each thread has it's own ThreadContext. | |
|
8 | /// </summary> | |
|
9 | /// <remarks> | |
|
10 | /// ThreadContext manages relations between logical operations and threads. | |
|
11 | /// </remarks> | |
|
12 | public class TraceContext { | |
|
13 | ||
|
14 | [ThreadStatic] | |
|
15 | static TraceContext _instance; | |
|
16 | ||
|
17 | OperationContext m_current = OperationContext.EMPTY; | |
|
18 | readonly Stack<OperationContext> m_stack = new Stack<OperationContext>(); | |
|
19 | readonly int m_threadId; | |
|
20 | ||
|
21 | public static TraceContext Instance { | |
|
22 | get { | |
|
23 | if (_instance == null) | |
|
24 | _instance = new TraceContext(); | |
|
25 | return _instance; | |
|
26 | } | |
|
27 | } | |
|
28 | ||
|
29 | public TraceContext() { | |
|
30 | m_threadId = Thread.CurrentThread.ManagedThreadId; | |
|
31 | } | |
|
32 | ||
|
33 | public int ThreadId { | |
|
34 | get { return m_threadId; } | |
|
35 | } | |
|
36 | ||
|
37 | public LogicalOperation CurrentOperation { | |
|
38 | get { | |
|
39 | return m_current.CurrentOperation; | |
|
40 | } | |
|
41 | } | |
|
42 | ||
|
43 | public void EnterLogicalOperation(LogicalOperation operation, bool takeOwnership) { | |
|
44 | // TODO Emit event | |
|
45 | m_stack.Push(m_current); | |
|
46 | m_current = new OperationContext(operation, takeOwnership); | |
|
47 | } | |
|
48 | ||
|
49 | public void StartLogicalOperation(string name) { | |
|
50 | m_current.BeginLogicalOperation(name); | |
|
51 | } | |
|
52 | ||
|
53 | public void StartLogicalOperation() { | |
|
54 | // TODO Emit Event | |
|
55 | m_current.BeginLogicalOperation(String.Empty); | |
|
56 | } | |
|
57 | ||
|
58 | public void EndLogicalOperation() { | |
|
59 | // TODO Emit event | |
|
60 | m_current.EndLogicalOperation(); | |
|
61 | } | |
|
62 | ||
|
63 | public LogicalOperation DetachLogicalOperation() { | |
|
64 | // TODO Emit event | |
|
65 | return m_current.DetachLogicalOperation(); | |
|
66 | } | |
|
67 | ||
|
68 | public void Leave() { | |
|
69 | // TODO Emit event | |
|
70 | if (m_stack.Count > 0) | |
|
71 | m_current = m_stack.Pop(); | |
|
72 | else { | |
|
73 | TraceLog.TraceWarning("Attemtp to leave the last operation context"); | |
|
74 | m_current = OperationContext.EMPTY; | |
|
75 | } | |
|
76 | } | |
|
77 | } | |
|
78 | } | |
|
79 |
@@ -14,22 +14,17 namespace Implab.Diagnostics { | |||
|
14 | 14 | public static class TraceLog { |
|
15 | 15 | [Conditional("TRACE")] |
|
16 | 16 | public static void StartLogicalOperation() { |
|
17 |
TraceContext. |
|
|
17 | TraceContext.Instance.StartLogicalOperation(); | |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | [Conditional("TRACE")] |
|
21 | 21 | public static void StartLogicalOperation(string name) { |
|
22 |
TraceContext. |
|
|
22 | TraceContext.Instance.StartLogicalOperation(name); | |
|
23 | 23 | } |
|
24 | 24 | |
|
25 | 25 | [Conditional("TRACE")] |
|
26 | 26 | public static void EndLogicalOperation() { |
|
27 |
TraceContext. |
|
|
28 | } | |
|
29 | ||
|
30 | [Conditional("TRACE")] | |
|
31 | public static void BindLogicalOperationToPromise(IPromise promise) { | |
|
32 | TraceContext.Current.BindLogicalOperationToPromise(promise); | |
|
27 | TraceContext.Instance.EndLogicalOperation(); | |
|
33 | 28 | } |
|
34 | 29 | |
|
35 | 30 | [Conditional("TRACE")] |
@@ -80,7 +80,6 | |||
|
80 | 80 | <Compile Include="Diagnostics\TextFileListener.cs" /> |
|
81 | 81 | <Compile Include="Diagnostics\TextListenerBase.cs" /> |
|
82 | 82 | <Compile Include="Diagnostics\TraceLog.cs" /> |
|
83 | <Compile Include="Diagnostics\TraceContext.cs" /> | |
|
84 | 83 | <Compile Include="Diagnostics\TraceEvent.cs" /> |
|
85 | 84 | <Compile Include="Diagnostics\TraceEventType.cs" /> |
|
86 | 85 | <Compile Include="Disposable.cs" /> |
@@ -141,6 +140,11 | |||
|
141 | 140 | <Compile Include="TransientPromiseException.cs" /> |
|
142 | 141 | <Compile Include="SyncContextPromise.cs" /> |
|
143 | 142 | <Compile Include="ObjectPool.cs" /> |
|
143 | <Compile Include="Diagnostics\OperationContext.cs" /> | |
|
144 | <Compile Include="Diagnostics\TraceContext.cs" /> | |
|
145 | <Compile Include="Diagnostics\LogEventArgs.cs" /> | |
|
146 | <Compile Include="Diagnostics\LogEventArgsT.cs" /> | |
|
147 | <Compile Include="Diagnostics\Extensions.cs" /> | |
|
144 | 148 | </ItemGroup> |
|
145 | 149 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |
|
146 | 150 | <ItemGroup /> |
@@ -12,7 +12,7 namespace Implab.Parallels { | |||
|
12 | 12 | readonly Action<TSrc> m_action; |
|
13 | 13 | readonly TSrc[] m_source; |
|
14 | 14 | readonly Promise<int> m_promise = new Promise<int>(); |
|
15 |
readonly |
|
|
15 | readonly LogicalOperation m_logicalOperation; | |
|
16 | 16 | |
|
17 | 17 | int m_pending; |
|
18 | 18 | int m_next; |
@@ -23,7 +23,7 namespace Implab.Parallels { | |||
|
23 | 23 | Debug.Assert(source != null); |
|
24 | 24 | Debug.Assert(action != null); |
|
25 | 25 | |
|
26 |
m_ |
|
|
26 | m_logicalOperation = TraceContext.Instance.CurrentOperation; | |
|
27 | 27 | m_next = 0; |
|
28 | 28 | m_source = source; |
|
29 | 29 | m_pending = source.Length; |
@@ -41,8 +41,12 namespace Implab.Parallels { | |||
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | protected override void Worker() { |
|
44 | TraceContext.Fork(m_traceContext); | |
|
45 |
|
|
|
44 | TraceContext.Instance.EnterLogicalOperation(m_logicalOperation, false); | |
|
45 | try { | |
|
46 | base.Worker(); | |
|
47 | } finally { | |
|
48 | TraceContext.Instance.Leave(); | |
|
49 | } | |
|
46 | 50 | } |
|
47 | 51 | |
|
48 | 52 | protected override bool TryDequeue(out int unit) { |
@@ -67,7 +71,7 namespace Implab.Parallels { | |||
|
67 | 71 | readonly TSrc[] m_source; |
|
68 | 72 | readonly TDst[] m_dest; |
|
69 | 73 | readonly Promise<TDst[]> m_promise = new Promise<TDst[]>(); |
|
70 |
readonly |
|
|
74 | readonly LogicalOperation m_logicalOperation; | |
|
71 | 75 | |
|
72 | 76 | int m_pending; |
|
73 | 77 | int m_next; |
@@ -83,7 +87,7 namespace Implab.Parallels { | |||
|
83 | 87 | m_dest = new TDst[source.Length]; |
|
84 | 88 | m_pending = source.Length; |
|
85 | 89 | m_transform = transform; |
|
86 |
m_ |
|
|
90 | m_logicalOperation = TraceContext.Instance.CurrentOperation; | |
|
87 | 91 | |
|
88 | 92 | m_promise.Anyway(Dispose); |
|
89 | 93 | |
@@ -97,13 +101,17 namespace Implab.Parallels { | |||
|
97 | 101 | } |
|
98 | 102 | |
|
99 | 103 | protected override void Worker() { |
|
100 |
TraceContext. |
|
|
101 |
|
|
|
104 | TraceContext.Instance.EnterLogicalOperation(m_logicalOperation,false); | |
|
105 | try { | |
|
106 | base.Worker(); | |
|
107 | } finally { | |
|
108 | TraceContext.Instance.Leave(); | |
|
109 | } | |
|
102 | 110 | } |
|
103 | 111 | |
|
104 | 112 | protected override bool TryDequeue(out int unit) { |
|
105 | 113 | unit = Interlocked.Increment(ref m_next) - 1; |
|
106 |
return unit |
|
|
114 | return unit < m_source.Length; | |
|
107 | 115 | } |
|
108 | 116 | |
|
109 | 117 | protected override void InvokeUnit(int unit) { |
@@ -14,15 +14,17 namespace Implab.Parallels { | |||
|
14 | 14 | |
|
15 | 15 | public static IPromise<T> Invoke<T>(Func<T> func) { |
|
16 | 16 | var p = new Promise<T>(); |
|
17 |
var caller = TraceContext. |
|
|
17 | var caller = TraceContext.Instance.CurrentOperation; | |
|
18 | 18 | |
|
19 | 19 | ThreadPool.QueueUserWorkItem(param => { |
|
20 |
TraceContext. |
|
|
20 | TraceContext.Instance.EnterLogicalOperation(caller,false); | |
|
21 | 21 | try { |
|
22 | 22 | p.Resolve(func()); |
|
23 | 23 | } catch(Exception e) { |
|
24 | 24 | p.Reject(e); |
|
25 | } | |
|
25 | } finally { | |
|
26 | TraceContext.Instance.Leave(); | |
|
27 | } | |
|
26 | 28 | }); |
|
27 | 29 | |
|
28 | 30 | return p; |
@@ -31,14 +33,16 namespace Implab.Parallels { | |||
|
31 | 33 | public static IPromise<T> InvokeNewThread<T>(Func<T> func) { |
|
32 | 34 | var p = new Promise<T>(); |
|
33 | 35 | |
|
34 |
var caller = TraceContext. |
|
|
36 | var caller = TraceContext.Instance.CurrentOperation; | |
|
35 | 37 | |
|
36 | 38 | var worker = new Thread(() => { |
|
37 |
TraceContext. |
|
|
39 | TraceContext.Instance.EnterLogicalOperation(caller,false); | |
|
38 | 40 | try { |
|
39 | 41 | p.Resolve(func()); |
|
40 | 42 | } catch (Exception e) { |
|
41 | 43 | p.Reject(e); |
|
44 | } finally { | |
|
45 | TraceContext.Instance.Leave(); | |
|
42 | 46 | } |
|
43 | 47 | }); |
|
44 | 48 | worker.IsBackground = true; |
@@ -51,15 +55,17 namespace Implab.Parallels { | |||
|
51 | 55 | public static IPromise InvokeNewThread(Action func) { |
|
52 | 56 | var p = new Promise<object>(); |
|
53 | 57 | |
|
54 |
var caller = TraceContext. |
|
|
58 | var caller = TraceContext.Instance.CurrentOperation; | |
|
55 | 59 | |
|
56 | 60 | var worker = new Thread(() => { |
|
57 |
TraceContext. |
|
|
61 | TraceContext.Instance.EnterLogicalOperation(caller,false); | |
|
58 | 62 | try { |
|
59 | 63 | func(); |
|
60 | 64 | p.Resolve(); |
|
61 | 65 | } catch (Exception e) { |
|
62 | 66 | p.Reject(e); |
|
67 | } finally { | |
|
68 | TraceContext.Instance.Leave(); | |
|
63 | 69 | } |
|
64 | 70 | }); |
|
65 | 71 | worker.IsBackground = true; |
@@ -1,9 +1,5 | |||
|
1 | 1 | using System; |
|
2 | using System.Collections.Generic; | |
|
3 | using System.Linq; | |
|
4 | using System.Text; | |
|
5 | 2 | using System.Threading; |
|
6 | using System.Diagnostics; | |
|
7 | 3 | |
|
8 | 4 | namespace Implab.Parallels { |
|
9 | 5 | public abstract class DispatchPool<TUnit> : IDisposable { |
@@ -150,14 +146,13 namespace Implab.Parallels { | |||
|
150 | 146 | protected bool StartWorker() { |
|
151 | 147 | if (AllocateThreadSlot()) { |
|
152 | 148 | // slot successfully allocated |
|
153 |
var worker = new Thread( |
|
|
149 | var worker = new Thread(Worker); | |
|
154 | 150 | worker.IsBackground = true; |
|
155 | 151 | worker.Start(); |
|
156 | 152 | |
|
157 | 153 | return true; |
|
158 | } else { | |
|
159 | return false; | |
|
160 | 154 | } |
|
155 | return false; | |
|
161 | 156 | } |
|
162 | 157 | |
|
163 | 158 | protected abstract void InvokeUnit(TUnit unit); |
@@ -1,7 +1,4 | |||
|
1 | 1 | using System; |
|
2 | using System.Collections.Generic; | |
|
3 | using System.Linq; | |
|
4 | using System.Text; | |
|
5 | 2 | using System.Threading; |
|
6 | 3 | using System.Diagnostics; |
|
7 | 4 | using Implab.Diagnostics; |
@@ -12,29 +9,24 namespace Implab.Parallels { | |||
|
12 | 9 | MTQueue<Action> m_queue = new MTQueue<Action>(); |
|
13 | 10 | int m_queueLength = 0; |
|
14 | 11 | readonly int m_threshold = 1; |
|
15 | int m_workers = 0; | |
|
16 | 12 | |
|
17 | 13 | public WorkerPool(int minThreads, int maxThreads, int threshold) |
|
18 | 14 | : base(minThreads, maxThreads) { |
|
19 | 15 | m_threshold = threshold; |
|
20 | m_workers = minThreads; | |
|
21 | 16 | InitPool(); |
|
22 | 17 | } |
|
23 | 18 | |
|
24 | 19 | public WorkerPool(int minThreads, int maxThreads) : |
|
25 | 20 | base(minThreads, maxThreads) { |
|
26 | m_workers = minThreads; | |
|
27 | 21 | InitPool(); |
|
28 | 22 | } |
|
29 | 23 | |
|
30 | 24 | public WorkerPool(int threads) |
|
31 | 25 | : base(threads) { |
|
32 | m_workers = threads; | |
|
33 | 26 | InitPool(); |
|
34 | 27 | } |
|
35 | 28 | |
|
36 | public WorkerPool() | |
|
37 | : base() { | |
|
29 | public WorkerPool() { | |
|
38 | 30 | InitPool(); |
|
39 | 31 | } |
|
40 | 32 | |
@@ -46,16 +38,17 namespace Implab.Parallels { | |||
|
46 | 38 | |
|
47 | 39 | var promise = new Promise<T>(); |
|
48 | 40 | |
|
49 |
var |
|
|
41 | var lop = TraceContext.Instance.CurrentOperation; | |
|
50 | 42 | |
|
51 | 43 | EnqueueTask(delegate() { |
|
52 | caller.Invoke(delegate() { | |
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
|
57 |
|
|
|
58 | }); | |
|
44 | TraceContext.Instance.EnterLogicalOperation(lop, false); | |
|
45 | try { | |
|
46 | promise.Resolve(task()); | |
|
47 | } catch (Exception e) { | |
|
48 | promise.Reject(e); | |
|
49 | } finally { | |
|
50 | TraceContext.Instance.Leave(); | |
|
51 | } | |
|
59 | 52 | }); |
|
60 | 53 | |
|
61 | 54 | return promise; |
General Comments 0
You need to be logged in to leave comments.
Login now