##// END OF EJS Templates
improved tracing...
cin -
r93:dc4942d09e74 v2
parent child
Show More
@@ -0,0 +1,8
1 using System;
2
3 namespace Implab {
4 public interface IComponentContainer {
5 void Add(IDisposable component);
6 }
7 }
8
@@ -0,0 +1,48
1 using System;
2 using Implab.Parallels;
3 using System.Threading;
4
5 namespace Implab {
6 public class MTComponentContainer : IComponentContainer, IDisposable {
7 static readonly MTComponentContainer _appContainer;
8
9 static MTComponentContainer() {
10 _appContainer = new MTComponentContainer();
11 AppDomain.CurrentDomain.ProcessExit += HandleProcessExit;
12 }
13
14 public static MTComponentContainer AppContainer {
15 get {
16 return _appContainer;
17 }
18 }
19
20 bool m_disposed;
21 readonly MTQueue<IDisposable> m_components = new MTQueue<IDisposable>();
22
23 public void Add(IDisposable item) {
24 Safe.ArgumentNotNull(item, "item");
25 Thread.MemoryBarrier();
26 if (m_disposed) {
27 item.Dispose();
28 } else {
29 m_components.Enqueue(item);
30 if (m_disposed && m_components.TryDequeue(out item))
31 item.Dispose();
32 }
33 }
34
35 public void Dispose() {
36 m_disposed = true;
37 IDisposable item;
38 while (m_components.TryDequeue(out item))
39 item.Dispose();
40 }
41
42 static void HandleProcessExit (object sender, EventArgs e)
43 {
44 _appContainer.Dispose();
45 }
46 }
47 }
48
@@ -0,0 +1,46
1 <?xml version="1.0" encoding="utf-8"?>
2 <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3 <PropertyGroup>
4 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6 <ProductVersion>8.0.30703</ProductVersion>
7 <SchemaVersion>2.0</SchemaVersion>
8 <ProjectGuid>{15DD7123-D504-4627-8B4F-D00C7F04D033}</ProjectGuid>
9 <OutputType>Exe</OutputType>
10 <RootNamespace>MonoPlay</RootNamespace>
11 <AssemblyName>MonoPlay</AssemblyName>
12 <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13 </PropertyGroup>
14 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
15 <DebugSymbols>true</DebugSymbols>
16 <DebugType>full</DebugType>
17 <Optimize>false</Optimize>
18 <OutputPath>bin\Debug</OutputPath>
19 <DefineConstants>DEBUG;TRACE;</DefineConstants>
20 <ErrorReport>prompt</ErrorReport>
21 <WarningLevel>4</WarningLevel>
22 <ConsolePause>false</ConsolePause>
23 </PropertyGroup>
24 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25 <DebugType>full</DebugType>
26 <Optimize>true</Optimize>
27 <OutputPath>bin\Release</OutputPath>
28 <ErrorReport>prompt</ErrorReport>
29 <WarningLevel>4</WarningLevel>
30 <Externalconsole>true</Externalconsole>
31 </PropertyGroup>
32 <ItemGroup>
33 <Reference Include="System" />
34 </ItemGroup>
35 <ItemGroup>
36 <Compile Include="Program.cs" />
37 <Compile Include="Properties\AssemblyInfo.cs" />
38 </ItemGroup>
39 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
40 <ItemGroup>
41 <ProjectReference Include="..\Implab\Implab.csproj">
42 <Project>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</Project>
43 <Name>Implab</Name>
44 </ProjectReference>
45 </ItemGroup>
46 </Project> No newline at end of file
@@ -0,0 +1,31
1 using System;
2 using Implab.Diagnostics;
3 using Implab.Parallels;
4 using Implab;
5
6 namespace MonoPlay {
7 class MainClass {
8 public static void Main(string[] args) {
9 var listener = new ConsoleTraceListener(true);
10 listener.Subscribe<TraceEvent>();
11
12 MTComponentContainer.AppContainer.Add(listener);
13
14 TraceLog.StartLogicalOperation("program");
15
16 Console.WriteLine("Hello World!");
17
18 TraceLog.StartLogicalOperation("async");
19 AsyncPool.Invoke(() => {
20 TraceLog.TraceInformation("Hello async");
21 TraceLog.StartLogicalOperation();
22 return 0;
23 })
24 .EndLogicalOperation()
25 .Join();
26
27 TraceLog.EndLogicalOperation();
28
29 }
30 }
31 }
@@ -0,0 +1,27
1 using System.Reflection;
2 using System.Runtime.CompilerServices;
3
4 // Information about this assembly is defined by the following attributes.
5 // Change them to the values specific to your project.
6
7 [assembly: AssemblyTitle("MonoPlay")]
8 [assembly: AssemblyDescription("")]
9 [assembly: AssemblyConfiguration("")]
10 [assembly: AssemblyCompany("")]
11 [assembly: AssemblyProduct("")]
12 [assembly: AssemblyCopyright("sergey")]
13 [assembly: AssemblyTrademark("")]
14 [assembly: AssemblyCulture("")]
15
16 // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
17 // The form "{Major}.{Minor}.*" will automatically update the build and revision,
18 // and "{Major}.{Minor}.{Build}.*" will update just the revision.
19
20 [assembly: AssemblyVersion("1.0.*")]
21
22 // The following attributes are used to specify the signing key for the assembly,
23 // if desired. See the Mono documentation for more information about signing.
24
25 //[assembly: AssemblyDelaySign(false)]
26 //[assembly: AssemblyKeyFile("")]
27
@@ -13,3 +13,4 Implab.Fx.Test/obj/
13 13 _ReSharper.Implab/
14 14 Implab.Diagnostics.Interactive/bin/
15 15 Implab.Diagnostics.Interactive/obj/
16 MonoPlay/bin/
@@ -16,6 +16,8 Project("{2150E333-8FDC-42A3-9474-1A3956
16 16 EndProject
17 17 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Implab.Test.mono", "Implab.Test\Implab.Test.mono.csproj", "{2BD05F84-E067-4B87-9477-FDC2676A21C6}"
18 18 EndProject
19 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonoPlay", "MonoPlay\MonoPlay.csproj", "{15DD7123-D504-4627-8B4F-D00C7F04D033}"
20 EndProject
19 21 Global
20 22 GlobalSection(SolutionConfigurationPlatforms) = preSolution
21 23 Debug|Any CPU = Debug|Any CPU
@@ -32,22 +34,30 Global
32 34 {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU
33 35 {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Release|Any CPU.ActiveCfg = Release|Any CPU
34 36 {06E706F8-6881-43EB-927E-FFC503AF6ABC}.Release|Any CPU.Build.0 = Release|Any CPU
35 {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU
37 {15DD7123-D504-4627-8B4F-D00C7F04D033}.Debug 4.5|Any CPU.ActiveCfg = Debug|Any CPU
38 {15DD7123-D504-4627-8B4F-D00C7F04D033}.Debug 4.5|Any CPU.Build.0 = Debug|Any CPU
39 {15DD7123-D504-4627-8B4F-D00C7F04D033}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
40 {15DD7123-D504-4627-8B4F-D00C7F04D033}.Debug|Any CPU.Build.0 = Debug|Any CPU
41 {15DD7123-D504-4627-8B4F-D00C7F04D033}.Release 4.5|Any CPU.ActiveCfg = Release|Any CPU
42 {15DD7123-D504-4627-8B4F-D00C7F04D033}.Release 4.5|Any CPU.Build.0 = Release|Any CPU
43 {15DD7123-D504-4627-8B4F-D00C7F04D033}.Release|Any CPU.ActiveCfg = Release|Any CPU
44 {15DD7123-D504-4627-8B4F-D00C7F04D033}.Release|Any CPU.Build.0 = Release|Any CPU
36 45 {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU
37 46 {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU
38 {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU
39 {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
40 47 {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
41 48 {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
42 {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
43 {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU
44 49 {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU
45 50 {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU
46 {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU
47 {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
48 51 {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
49 52 {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release|Any CPU.Build.0 = Release|Any CPU
50 {2BD05F84-E067-4B87-9477-FDC2676A21C6}.Release|Any CPU.Build.0 = Release|Any CPU
53 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU
54 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU
55 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
56 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.Build.0 = Debug|Any CPU
57 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU
58 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU
59 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.ActiveCfg = Release|Any CPU
60 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.Build.0 = Release|Any CPU
51 61 {2F31E405-E267-4195-A05D-574093C21209}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU
52 62 {2F31E405-E267-4195-A05D-574093C21209}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU
53 63 {2F31E405-E267-4195-A05D-574093C21209}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
@@ -64,20 +74,12 Global
64 74 {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU
65 75 {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
66 76 {63F92C0C-61BF-48C0-A377-8D67C3C661D0}.Release|Any CPU.Build.0 = Release|Any CPU
67 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug 4.5|Any CPU.ActiveCfg = Debug 4.5|Any CPU
68 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug 4.5|Any CPU.Build.0 = Debug 4.5|Any CPU
69 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
70 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Debug|Any CPU.Build.0 = Debug|Any CPU
71 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release 4.5|Any CPU.ActiveCfg = Release 4.5|Any CPU
72 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release 4.5|Any CPU.Build.0 = Release 4.5|Any CPU
73 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.ActiveCfg = Release|Any CPU
74 {F550F1F8-8746-4AD0-9614-855F4C4B7F05}.Release|Any CPU.Build.0 = Release|Any CPU
75 77 EndGlobalSection
76 78 GlobalSection(NestedProjects) = preSolution
77 79 {2BD05F84-E067-4B87-9477-FDC2676A21C6} = {BCA337C3-BFDC-4825-BBDB-E6D467E4E452}
78 80 EndGlobalSection
79 81 GlobalSection(MonoDevelopProperties) = preSolution
80 StartupItem = Implab\Implab.csproj
82 StartupItem = MonoPlay\MonoPlay.csproj
81 83 Policies = $0
82 84 $0.CSharpFormattingPolicy = $1
83 85 $1.IndentSwitchBody = True
@@ -28,22 +28,32
28 28 m_current = m_current.Parent;
29 29 else if (m_ownership)
30 30 m_current = LogicalOperation.EMPTY;
31 else
31 else {
32 TraceLog.TraceWarning("DetachLogicalOperation can't be applied in the current context");
32 33 detached = LogicalOperation.EMPTY;
34 }
35 } else {
36 TraceLog.TraceWarning("DetachLogicalOperation can't be applied in the current context");
33 37 }
34 TraceLog.TraceWarning("EndLogicalOperation can't be applied in the current context");
38
35 39 return detached;
36 40 }
37 41
38 42 public void EndLogicalOperation() {
39 43 if (m_current != m_initial) {
40 44 m_current = m_current.Parent;
41 } else if (m_current != null && m_ownership) {
42 m_current = null;
45 } else if (m_current != LogicalOperation.EMPTY && m_ownership) {
46 m_current = LogicalOperation.EMPTY;
43 47 } else {
44 48 TraceLog.TraceWarning("EndLogicalOperation can't be applied in the current context");
45 49 }
46 50 }
51
52 public void Leave() {
53
54 if ((m_ownership && m_current != LogicalOperation.EMPTY) || (!m_ownership && m_current != m_initial) )
55 TraceLog.TraceWarning("Trying to leave unfinished logical operation {0}", m_current.Name);
56 }
47 57 }
48 58 }
49 59
@@ -41,35 +41,36 namespace Implab.Diagnostics {
41 41 }
42 42
43 43 public void EnterLogicalOperation(LogicalOperation operation, bool takeOwnership) {
44 // TODO Emit event
44 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Attach, String.Format("{0} -> [{1}]", operation.Name, m_threadId)));
45 45 m_stack.Push(m_current);
46 46 m_current = new OperationContext(operation, takeOwnership);
47 47 }
48 48
49 49 public void StartLogicalOperation(string name) {
50 50 m_current.BeginLogicalOperation(name);
51 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationStarted, String.Format("+{0}",CurrentOperation.Name)));
51 52 }
52 53
53 54 public void StartLogicalOperation() {
54 // TODO Emit Event
55 m_current.BeginLogicalOperation(String.Empty);
55 StartLogicalOperation(String.Empty);
56 56 }
57 57
58 58 public void EndLogicalOperation() {
59 // TODO Emit event
59 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.OperationCompleted, String.Format("-{0} : {1}ms",CurrentOperation.Name, CurrentOperation.Duration)));
60 60 m_current.EndLogicalOperation();
61 61 }
62 62
63 63 public LogicalOperation DetachLogicalOperation() {
64 // TODO Emit event
65 return m_current.DetachLogicalOperation();
64 var op = m_current.DetachLogicalOperation();
65 LogChannel<TraceEvent>.Default.LogEvent(new TraceEvent(TraceEventType.Detach, String.Format("[{0}] -> {1}", m_threadId, op.Name)));
66 return op;
66 67 }
67 68
68 69 public void Leave() {
69 // TODO Emit event
70 if (m_stack.Count > 0)
70 if (m_stack.Count > 0) {
71 m_current.Leave();
71 72 m_current = m_stack.Pop();
72 else {
73 } else {
73 74 TraceLog.TraceWarning("Attemtp to leave the last operation context");
74 75 m_current = OperationContext.EMPTY;
75 76 }
@@ -1,7 +1,4
1 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 2
6 3 namespace Implab.Diagnostics {
7 4 public class TraceEvent {
@@ -21,10 +18,7 namespace Implab.Diagnostics {
21 18 }
22 19
23 20 public override string ToString() {
24 if (EventType == TraceEventType.Information)
25 return Message;
26 else
27 return String.Format("{0}: {1}", EventType, Message);
21 return EventType == TraceEventType.Information ? Message : String.Format("{0}: {1}", EventType, Message);
28 22 }
29 23
30 24 public static TraceEvent Create(TraceEventType type, string format, params object[] args) {
@@ -11,9 +11,7 namespace Implab.Diagnostics {
11 11 Error,
12 12 OperationStarted,
13 13 OperationCompleted,
14 Fork,
15 14 Attach,
16 15 Detach,
17 Created
18 16 }
19 17 }
@@ -7,6 +7,8
7 7 <OutputType>Library</OutputType>
8 8 <RootNamespace>Implab</RootNamespace>
9 9 <AssemblyName>Implab</AssemblyName>
10 <ProductVersion>8.0.30703</ProductVersion>
11 <SchemaVersion>2.0</SchemaVersion>
10 12 </PropertyGroup>
11 13 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
12 14 <DebugSymbols>true</DebugSymbols>
@@ -145,6 +147,8
145 147 <Compile Include="Diagnostics\LogEventArgs.cs" />
146 148 <Compile Include="Diagnostics\LogEventArgsT.cs" />
147 149 <Compile Include="Diagnostics\Extensions.cs" />
150 <Compile Include="IComponentContainer.cs" />
151 <Compile Include="MTComponentContainer.cs" />
148 152 </ItemGroup>
149 153 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
150 154 <ItemGroup />
@@ -7,9 +7,9 namespace Implab.Parallels {
7 7 readonly int m_maxThreadsLimit;
8 8 readonly int m_releaseTimeout = 1000; // the timeout while the working thread will wait for the new tasks before exit
9 9
10 int m_threads = 0; // the current size of the pool
11 int m_maxRunningThreads = 0; // the meximum reached size of the pool
12 int m_exit = 0; // the pool is going to shutdown, all unused workers are released
10 int m_threads; // the current size of the pool
11 int m_maxRunningThreads; // the meximum reached size of the pool
12 int m_exit; // the pool is going to shutdown, all unused workers are released
13 13
14 14 readonly object m_signal = new object(); // used to pulse waiting threads
15 15
@@ -1,8 +1,4
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading;
1 using System.Threading;
6 2
7 3 namespace Implab.Parallels {
8 4 public class MTQueue<T> {
@@ -34,7 +30,7 namespace Implab.Parallels {
34 30
35 31 public bool TryDequeue(out T value) {
36 32 Node first;
37 Node next = null;
33 Node next;
38 34 value = default(T);
39 35
40 36 Thread.MemoryBarrier();
@@ -64,11 +60,10 namespace Implab.Parallels {
64 60 Interlocked.CompareExchange(ref m_first, null, first);
65 61 break;
66 62
67 } else {
68 if (first == Interlocked.CompareExchange(ref m_first, next, first))
69 // head succesfully updated
70 break;
71 63 }
64 if (first == Interlocked.CompareExchange(ref m_first, next, first))
65 // head succesfully updated
66 break;
72 67 } while (true);
73 68
74 69 value = first.value;
General Comments 0
You need to be logged in to leave comments. Login now