annotate Implab/Diagnostics/TraceContext.cs @ 108:f3bdb7ba59b9 v2

sync
author cin
date Tue, 11 Nov 2014 18:37:35 +0300
parents a43745f81f10
children 2573b562e328
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
1 using System;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
2 using System.Collections.Generic;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
3 using System.Threading;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
4
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
5 namespace Implab.Diagnostics {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
6 /// <summary>
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
7 /// Trace context is bound to the specific thread, each thread has it's own ThreadContext.
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
8 /// </summary>
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
9 /// <remarks>
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
10 /// ThreadContext manages relations between logical operations and threads.
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
11 /// </remarks>
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
12 public class TraceContext {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
13
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
14 [ThreadStatic]
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
15 static TraceContext _instance;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
16
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
17 OperationContext m_current = OperationContext.EMPTY;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
18 readonly Stack<OperationContext> m_stack = new Stack<OperationContext>();
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
19 readonly int m_threadId;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
20
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
21 public static TraceContext Instance {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
22 get {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
23 if (_instance == null)
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
24 _instance = new TraceContext();
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
25 return _instance;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
26 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
27 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
28
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
29 public TraceContext() {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
30 m_threadId = Thread.CurrentThread.ManagedThreadId;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
31 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
32
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
33 public int ThreadId {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
34 get { return m_threadId; }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
35 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
36
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
37 public LogicalOperation CurrentOperation {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
38 get {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
39 return m_current.CurrentOperation;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
40 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
41 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
42
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
43 public void EnterLogicalOperation(LogicalOperation operation, bool takeOwnership) {
94
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
44 var prev = CurrentOperation;
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
45 m_stack.Push(m_current);
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
46 m_current = new OperationContext(operation, takeOwnership);
94
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
47 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(takeOwnership ? TraceEventType.Attach : TraceEventType.Enter, String.Format("{0} -> {1}",prev.Name, operation.Name)));
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
48 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
49
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
50 public void StartLogicalOperation(string name) {
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
51 m_current.BeginLogicalOperation(name);
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
52 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationStarted, String.Format("+{0}",CurrentOperation.Name)));
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
53 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
54
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
55 public void StartLogicalOperation() {
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
56 StartLogicalOperation(String.Empty);
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
57 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
58
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
59 public void EndLogicalOperation() {
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
60 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationCompleted, String.Format("-{0} : {1}ms",CurrentOperation.Name, CurrentOperation.Duration)));
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
61 m_current.EndLogicalOperation();
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
62 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
63
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
64 public LogicalOperation DetachLogicalOperation() {
94
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
65 var prev = m_current.DetachLogicalOperation();
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
66 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Detach, String.Format("{0} -> {1}",prev.Name, CurrentOperation.Name)));
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
67 return prev;
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
68 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
69
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
70 public void Leave() {
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
71 if (m_stack.Count > 0) {
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
72 m_current.Leave();
94
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
73 var prev = CurrentOperation;
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
74 m_current = m_stack.Pop();
94
a43745f81f10 minor fixes
cin
parents: 93
diff changeset
75 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Leave, String.Format("{0} -> {1}", prev.Name, CurrentOperation.Name)));
93
dc4942d09e74 improved tracing
cin
parents: 92
diff changeset
76 } else {
92
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
77 TraceLog.TraceWarning("Attemtp to leave the last operation context");
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
78 m_current = OperationContext.EMPTY;
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
79 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
80 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
81 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
82 }
4c0e5ef99986 rewritten tracing
cin
parents: 76
diff changeset
83