# HG changeset patch # User cin # Date 1476405197 -10800 # Node ID 43b1017ce100bb1d76ae6fa113ea1a931d2b9bc0 # Parent ea485487a424c71d889a18b92a5595457a36931a# Parent b305c678923a8ab36f38923be7619053783bc09c Слияние с default diff -r b305c678923a -r 43b1017ce100 Implab/Diagnostics/EventText.cs --- a/Implab/Diagnostics/EventText.cs Mon Oct 10 03:14:00 2016 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace Implab.Diagnostics { - public struct EventText { - public int indent; - - public string content; - } -} diff -r b305c678923a -r 43b1017ce100 Implab/Diagnostics/ListenerBase.cs --- a/Implab/Diagnostics/ListenerBase.cs Mon Oct 10 03:14:00 2016 +0300 +++ b/Implab/Diagnostics/ListenerBase.cs Fri Oct 14 03:33:17 2016 +0300 @@ -59,8 +59,8 @@ public void UnsubscribeAll() { lock (m_subscriptions) { - foreach (var subscription in m_subscriptions.Values) - subscription(); + foreach (var remove in m_subscriptions.Values) + remove(); m_subscriptions.Clear(); } } diff -r b305c678923a -r 43b1017ce100 Implab/Diagnostics/LogChannel.cs --- a/Implab/Diagnostics/LogChannel.cs Mon Oct 10 03:14:00 2016 +0300 +++ b/Implab/Diagnostics/LogChannel.cs Fri Oct 14 03:33:17 2016 +0300 @@ -69,7 +69,7 @@ this, new LogEventArgs( data, - Name, + this, traceContext.ThreadId, traceContext.CurrentOperation, traceContext.CurrentOperation.Duration @@ -77,5 +77,9 @@ ); } } + + public override string ToString() { + return Name; + } } } diff -r b305c678923a -r 43b1017ce100 Implab/Diagnostics/LogEventArgs.cs --- a/Implab/Diagnostics/LogEventArgs.cs Mon Oct 10 03:14:00 2016 +0300 +++ b/Implab/Diagnostics/LogEventArgs.cs Fri Oct 14 03:33:17 2016 +0300 @@ -2,7 +2,7 @@ namespace Implab.Diagnostics { public class LogEventArgs : EventArgs { - public string ChannelName { + public object Channel { get; private set; } @@ -18,8 +18,8 @@ get; private set; } - public LogEventArgs(string channelName, int threadId, LogicalOperation operation, int timeOffset) { - ChannelName = channelName; + public LogEventArgs(object channel, int threadId, LogicalOperation operation, int timeOffset) { + Channel = channel; ThreadId = threadId; Operation = operation; OperationTimeOffset = timeOffset; diff -r b305c678923a -r 43b1017ce100 Implab/Diagnostics/LogEventArgsT.cs --- a/Implab/Diagnostics/LogEventArgsT.cs Mon Oct 10 03:14:00 2016 +0300 +++ b/Implab/Diagnostics/LogEventArgsT.cs Fri Oct 14 03:33:17 2016 +0300 @@ -5,7 +5,7 @@ private set; } - public LogEventArgs(TEvent value,string channelName, int threadId, LogicalOperation operation, int timeOffset) : base(channelName, threadId, operation, timeOffset) { + public LogEventArgs(TEvent value,object channel, int threadId, LogicalOperation operation, int timeOffset) : base(channel, threadId, operation, timeOffset) { Value = value; } } diff -r b305c678923a -r 43b1017ce100 Implab/Diagnostics/OperationContext.cs --- a/Implab/Diagnostics/OperationContext.cs Mon Oct 10 03:14:00 2016 +0300 +++ b/Implab/Diagnostics/OperationContext.cs Fri Oct 14 03:33:17 2016 +0300 @@ -30,11 +30,11 @@ else if (m_ownership) m_current = LogicalOperation.EMPTY; else { - TraceLog.TraceWarning("DetachLogicalOperation can't be applied in the current context"); + TraceLog.TraceWarning("DetachLogicalOperation can't be performed in the current context"); detached = LogicalOperation.EMPTY; } } else { - TraceLog.TraceWarning("DetachLogicalOperation can't be applied in the current context"); + TraceLog.TraceWarning("DetachLogicalOperation can't be performed in the current context"); } return detached; @@ -50,7 +50,7 @@ m_ownership = false; } } else { - TraceLog.TraceWarning("EndLogicalOperation can't be applied in the current context"); + TraceLog.TraceWarning("EndLogicalOperation can't be performed in the current context"); } return current; } diff -r b305c678923a -r 43b1017ce100 Implab/Diagnostics/TextFileListener.cs --- a/Implab/Diagnostics/TextFileListener.cs Mon Oct 10 03:14:00 2016 +0300 +++ b/Implab/Diagnostics/TextFileListener.cs Fri Oct 14 03:33:17 2016 +0300 @@ -18,7 +18,7 @@ var msg = new StringBuilder(); for (int i = 0; i < args.Operation.Level; i++) msg.Append(" "); - msg.AppendFormat("[{0}]:{1}: {2}", args.ThreadId, args.ChannelName, entry); + msg.AppendFormat("[{0}]:{1}: {2}", args.ThreadId, args.Channel, entry); lock (m_textWriter) { if (!IsDisposed) { diff -r b305c678923a -r 43b1017ce100 Implab/Diagnostics/TraceContext.cs --- a/Implab/Diagnostics/TraceContext.cs Mon Oct 10 03:14:00 2016 +0300 +++ b/Implab/Diagnostics/TraceContext.cs Fri Oct 14 03:33:17 2016 +0300 @@ -56,9 +56,8 @@ StartLogicalOperation(String.Empty); } - public void EndLogicalOperation() { - var op = m_current.EndLogicalOperation(); - LogChannel.Default.LogEvent(new TraceEvent(TraceEventType.OperationCompleted, String.Format("-{0} : {1}ms",op.Name, op.Duration))); + public LogicalOperation EndLogicalOperation() { + return m_current.EndLogicalOperation(); } public LogicalOperation DetachLogicalOperation() { diff -r b305c678923a -r 43b1017ce100 Implab/Diagnostics/TraceEvent.cs --- a/Implab/Diagnostics/TraceEvent.cs Mon Oct 10 03:14:00 2016 +0300 +++ b/Implab/Diagnostics/TraceEvent.cs Fri Oct 14 03:33:17 2016 +0300 @@ -12,18 +12,36 @@ private set; } - public TraceEvent(TraceEventType type, string message) { + /// + /// The logical operation this event belongs to. + /// + public LogicalOperation Operation { + get; + private set; + } + + /// + /// Gets the time offset in milliseconds from the start of the operation, if the operation is not specified the value is zero. + /// + public int OperationTime { + get; + private set; + } + + public TraceEvent(LogicalOperation operation, TraceEventType type, string message) { EventType = type; Message = message; + Operation = operation; + if (operation != null) + OperationTime = operation.Duration; } public override string ToString() { - /*return EventType == TraceEventType.Information ? Message : String.Format("{0}: {1}", EventType, Message);*/ return Message; } - public static TraceEvent Create(TraceEventType type, string format, params object[] args) { - return new TraceEvent(type, format == null ? String.Empty : String.Format(format, args)); + public static TraceEvent Create(LogicalOperation operation, TraceEventType type, string format, params object[] args) { + return new TraceEvent(operation, type, format == null ? String.Empty : String.Format(format, args)); } } } diff -r b305c678923a -r 43b1017ce100 Implab/Diagnostics/TraceLog.cs --- a/Implab/Diagnostics/TraceLog.cs Mon Oct 10 03:14:00 2016 +0300 +++ b/Implab/Diagnostics/TraceLog.cs Fri Oct 14 03:33:17 2016 +0300 @@ -7,31 +7,51 @@ namespace Implab.Diagnostics { /// - /// Класс для публикации событий выполнения программы, события публикуются через . - /// Журнал трассировки отражает логический ход выполнения программы и существует всегда, поскольку тесно связан с - /// контекстом трассировки. + /// This class is used to trace a logical flow of the application, it publishes events to the default channel. /// public static class TraceLog { + /// + /// Starts the logical operation nested to the current operation nested to the current one. + /// [Conditional("TRACE")] public static void StartLogicalOperation() { TraceContext.Instance.StartLogicalOperation(); + } + /// + /// Starts the logical operation with the specified name, this name is usefull in logs. + /// + /// Name. [Conditional("TRACE")] public static void StartLogicalOperation(string name) { TraceContext.Instance.StartLogicalOperation(name); } + /// + /// Ends the logical operation and restores the previous one. + /// [Conditional("TRACE")] public static void EndLogicalOperation() { - TraceContext.Instance.EndLogicalOperation(); + var op = TraceContext.Instance.EndLogicalOperation(); + LogChannel.Default.LogEvent(new TraceEvent(TraceEventType.OperationCompleted, String.Format("-{0} : {1}ms",op.Name, op.Duration))); } + /// + /// Writes an informational message. + /// + /// Format. + /// Arguments. [Conditional("TRACE")] public static void TraceInformation(string format, params object[] arguments) { LogChannel.Default.LogEvent(TraceEvent.Create(TraceEventType.Information, format, arguments)); } + /// + /// Writes a warning message. + /// + /// Format. + /// Arguments. [Conditional("TRACE")] public static void TraceWarning(string format, params object[] arguments) { LogChannel.Default.LogEvent(TraceEvent.Create(TraceEventType.Warning, format, arguments)); diff -r b305c678923a -r 43b1017ce100 Implab/Implab.csproj --- a/Implab/Implab.csproj Mon Oct 10 03:14:00 2016 +0300 +++ b/Implab/Implab.csproj Fri Oct 14 03:33:17 2016 +0300 @@ -77,7 +77,6 @@ - diff -r b305c678923a -r 43b1017ce100 MonoPlay/Program.cs --- a/MonoPlay/Program.cs Mon Oct 10 03:14:00 2016 +0300 +++ b/MonoPlay/Program.cs Fri Oct 14 03:33:17 2016 +0300 @@ -4,39 +4,42 @@ using Implab.Formats.JSON; using System.IO; using System.Text.Json; +using System.Diagnostics; +using Implab.Parallels; +using System.Threading; namespace MonoPlay { class MainClass { public static void Main(string[] args) { - if (args == null) - throw new ArgumentNullException("args"); - int t1, t2; + var pool = new WorkerPool(10); - for (int i = 0; i < 2; i++) { - t1 = Environment.TickCount; - int elements =0; - using (var reader = new JSONParser(File.OpenText("/home/sergey/temp/citylots.json"))) { - while (reader.Read()) - elements++; - } + var listerner = new ConsoleTraceListener(); + listerner.TraceOutputOptions = TraceOptions.LogicalOperationStack; + Trace.Listeners.Add(listerner); + + Trace.CorrelationManager.StartLogicalOperation("Main"); - t2 = Environment.TickCount; - Console.WriteLine("attempt {0} done: {1} ms, {2:.00} Mb, {3} GC, Elements: {4}",i+1, t2 - t1, GC.GetTotalMemory(false) / (1024*1024), GC.CollectionCount(0), elements ); - } + var d = pool.Invoke(() => { + Trace.CorrelationManager.StartLogicalOperation("Worker"); + Thread.Sleep(100); + Trace.TraceInformation("worker done"); + Trace.CorrelationManager.StopLogicalOperation(); + }); - Console.WriteLine("Syste.Text.Json"); - var paraser = new JsonParser(); - for (int i = 0; i < 2; i++) { - t1 = Environment.TickCount; - using (var reader = File.OpenText("/home/sergey/temp/citylots.json")) { - paraser.Parse(reader); - } + ThreadPool.QueueUserWorkItem((o) => { + Trace.CorrelationManager.StartLogicalOperation("Thread"); + Thread.Sleep(100); + Trace.TraceInformation("thread done"); + Trace.CorrelationManager.StopLogicalOperation(); + }); - t2 = Environment.TickCount; - Console.WriteLine("attempt {0} done: {1} ms, {2:.00} Mb, {3} GC, ",i+1, t2 - t1, GC.GetTotalMemory(false) / (1024*1024), GC.CollectionCount(0)); - } + Trace.TraceInformation("main done"); + Trace.CorrelationManager.StopLogicalOperation(); + + d.Join(); + }