Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ProfilerThread.cs
1using System;
3using System.Globalization;
4using System.Text;
5using System.Xml;
9
11{
16 {
20 Sequential,
21
25 StateMachine,
26
30 Accumulating,
31
35 Binary,
36
40 Analog,
41
45 AnalogDelta
46 }
47
51 public class ProfilerThread
52 {
53 private readonly ChunkedList<ProfilerThread> subThreads = new ChunkedList<ProfilerThread>();
54 private readonly ChunkedList<ProfilerEvent> events = new ChunkedList<ProfilerEvent>();
55 private readonly string name;
56 private readonly int order;
57 private readonly bool sampleDelta;
58 private readonly ProfilerThreadType type;
59 private readonly Profiler profiler;
60 private readonly ProfilerThread parent;
61 private string label;
62 private double lastSample = 0;
63 private long? startedAt = null;
64 private long? stoppedAt = null;
65
74 {
75 this.name = this.label = Name;
76 this.order = Order;
77 this.type = Type;
78 this.profiler = Profiler;
79 this.parent = null;
80 this.sampleDelta = Type == ProfilerThreadType.AnalogDelta;
81 }
82
91 {
92 this.name = this.label = Name;
93 this.order = Order;
94 this.type = Type;
95 this.parent = Parent;
96 this.profiler = this.parent.Profiler;
97 this.sampleDelta = Type == ProfilerThreadType.AnalogDelta;
98 }
99
103 public string Name => this.name;
104
109 public string Label
110 {
111 get => this.label;
112 set => this.label = value;
113 }
114
118 public int Order => this.order;
119
123 public ProfilerThreadType Type => this.type;
124
128 public Profiler Profiler => this.profiler;
129
133 public ProfilerThread Parent => this.parent;
134
138 public long? StartedAt => this.startedAt;
139
143 public long? StoppedAt => this.stoppedAt;
144
152 {
153 ProfilerThread Result = this.profiler.CreateThread(Name, Type, this);
154 this.subThreads.Add(Result);
155 return Result;
156 }
157
162 public void NewState(string State)
163 {
164 this.events.Add(new NewState(this.profiler.ElapsedTicks, State, this));
165 }
166
171 public void NewSample(double Sample)
172 {
173 if (this.sampleDelta)
174 {
175 Sample += this.lastSample;
176 this.events.Add(new NewSample(this.profiler.ElapsedTicks, Sample, this));
177 this.lastSample = Sample;
178 }
179 else
180 this.events.Add(new NewSample(this.profiler.ElapsedTicks, Sample, this));
181 }
182
186 public void Idle()
187 {
188 this.events.Add(new Idle(this.profiler.ElapsedTicks, this));
189 }
190
194 public void High()
195 {
196 this.NewState("high");
197 }
198
202 public void Low()
203 {
204 this.NewState("low");
205 }
206
213 public void Interval(DateTime From, DateTime To, string Label)
214 {
215 this.events.Add(new Interval(this.profiler.GetTicks(From), this.profiler.GetTicks(To), Label, this));
216 }
217
224 public void Interval(long From, long To, string Label)
225 {
226 this.events.Add(new Interval(From, To, Label, this));
227 }
228
233 public void Event(string Name)
234 {
235 this.events.Add(new Event(this.profiler.ElapsedTicks, Name, this));
236 }
237
243 public void Event(string Name, string Label)
244 {
245 this.events.Add(new Event(this.profiler.ElapsedTicks, Name, Label, this));
246 }
247
252 public void Exception(System.Exception Exception)
253 {
254 this.events.Add(new Events.Exception(this.profiler.ElapsedTicks, Exception, this));
255 }
256
262 public void Exception(System.Exception Exception, string Label)
263 {
264 this.events.Add(new Events.Exception(this.profiler.ElapsedTicks, Exception, Label, this));
265 }
266
270 public void Start()
271 {
272 long Ticks = this.profiler.ElapsedTicks;
273 this.startedAt = Ticks;
274 this.events.Add(new Start(Ticks, this));
275 }
276
280 public void Stop()
281 {
282 long Ticks = this.profiler.ElapsedTicks;
283 this.stoppedAt = Ticks;
284 this.events.Add(new Stop(Ticks, this));
285 }
286
292 public void ExportXml(XmlWriter Output, TimeUnit TimeUnit)
293 {
294 Output.WriteStartElement("Thread");
295 Output.WriteAttributeString("name", this.name);
296 Output.WriteAttributeString("label", this.label);
297 Output.WriteAttributeString("type", this.type.ToString());
298
299 Output.WriteStartElement("Events");
300
301 ProfilerEvent Prev = null;
302
303 foreach (ProfilerEvent Event in this.events)
304 {
305 Event.ExportXml(Output, Prev, TimeUnit);
306 Prev = Event;
307 }
308
309 Output.WriteEndElement();
310
311 foreach (ProfilerThread Thread in this.subThreads)
312 Thread.ExportXml(Output, TimeUnit);
313
314 Output.WriteEndElement();
315 }
316
322 public void ExportPlantUmlDescription(StringBuilder Output, TimeUnit TimeUnit)
323 {
324 switch (this.type)
325 {
326 case ProfilerThreadType.StateMachine:
327 Output.Append("robust \"");
328 break;
329
330 case ProfilerThreadType.Binary:
331 Output.Append("binary \"");
332 break;
333
334 case ProfilerThreadType.Analog:
335 case ProfilerThreadType.AnalogDelta:
336 Output.Append("analog \"");
337 break;
338
339 case ProfilerThreadType.Sequential:
340 case ProfilerThreadType.Accumulating:
341 default:
342 Output.Append("concise \"");
343 break;
344 }
345
346 Output.Append(this.label);
347 Output.Append("\" as T");
348 Output.AppendLine(this.order.ToString());
349
350 if (this.type == ProfilerThreadType.Accumulating)
351 {
353 ProfilerEvent[] Events = this.events.ToArray();
354 this.events.Clear();
355
356 foreach (ProfilerEvent Event in Events)
358
359 Accumulator.Report(this.events);
360 }
361
362 foreach (ProfilerEvent Event in this.events)
364
365 foreach (ProfilerThread Thread in this.subThreads)
366 Thread.ExportPlantUmlDescription(Output, TimeUnit);
367 }
368
372 public string Key => "T" + this.order.ToString();
373
379 {
380 foreach (ProfilerEvent Event in this.events)
381 Event.ExportPlantUml(States);
382
383 foreach (ProfilerThread Thread in this.subThreads)
384 Thread.ExportPlantUmlEvents(States);
385
386 if (this.startedAt.HasValue && this.stoppedAt.HasValue)
387 {
388 StringBuilder Output = States.Summary;
389 long Ticks = this.stoppedAt.Value - this.startedAt.Value;
390 string ElapsedStr = this.profiler.ToTimeStr(Ticks, this, States.TimeUnit, 3);
391 KeyValuePair<double, string> Time;
392
393 Output.Append(this.Key);
394 Output.Append('@');
395
396 Time = this.profiler.ToTime(this.startedAt.Value, this, States.TimeUnit);
397 Output.Append(Time.Key.ToString("F0", CultureInfo.InvariantCulture));
398
399 Output.Append(" <-> @");
400
401 Time = this.profiler.ToTime(this.stoppedAt.Value, this, States.TimeUnit);
402 Output.Append(Time.Key.ToString("F0", CultureInfo.InvariantCulture));
403
404 Output.Append(" : ");
405 Output.AppendLine(ElapsedStr);
406 }
407 }
408
415 public KeyValuePair<double, string> ToTime(long Ticks, TimeUnit TimeUnit)
416 {
417 return this.profiler.ToTime(Ticks, this, TimeUnit);
418 }
419
426 public string ToTimeStr(long Ticks, TimeUnit TimeUnit)
427 {
428 return this.profiler.ToTimeStr(Ticks, this, TimeUnit, 7);
429 }
430
431 }
432}
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
override void ExportPlantUml(PlantUmlStates States)
Exports events to PlantUML.
Definition: Event.cs:66
override void ExportPlantUmlPreparation()
Prepares the event for export to PlantUML
Definition: Event.cs:89
Contains a new sample value.
Definition: NewSample.cs:10
Abstract base class for profiler events.
virtual void Accumulate(Accumulator Accumulator)
Accumulates the event.
virtual void ExportXml(XmlWriter Output, ProfilerEvent Previous, TimeUnit TimeUnit)
Exports the event to XML.
void Report(ChunkedList< ProfilerEvent > Result)
Reports accumulated events to a list of events.
Definition: Accumulator.cs:120
Contains internal states used during generation of PlantUML diagram.s
Class that keeps track of events and timing.
Definition: Profiler.cs:68
Class that keeps track of events and timing for one thread.
ProfilerThread CreateSubThread(string Name, ProfilerThreadType Type)
Creates a new profiler thread.
long? StoppedAt
Ticks when thread was stopped.
void NewSample(double Sample)
A new sample value has been recored
void Exception(System.Exception Exception)
Exception occurred
void Low()
Sets the (binary) state to "low".
void ExportXml(XmlWriter Output, TimeUnit TimeUnit)
Exports events to XML.
string ToTimeStr(long Ticks, TimeUnit TimeUnit)
String representation of time, corresponding to a measured number of high-frequency clock ticks.
void Event(string Name, string Label)
Event occurred
void Interval(DateTime From, DateTime To, string Label)
Records an interval in the profiler thread.
ProfilerThreadType Type
Type of profiler thread.
void ExportPlantUmlEvents(PlantUmlStates States)
Exports events to PlantUML.
void High()
Sets the (binary) state to "high".
KeyValuePair< double, string > ToTime(long Ticks, TimeUnit TimeUnit)
Time (amount, unit), corresponding to a measured number of high-frequency clock ticks.
void Event(string Name)
Event occurred
long? StartedAt
Ticks when thread was started.
ProfilerThread(string Name, int Order, ProfilerThreadType Type, Profiler Profiler)
Class that keeps track of events and timing for one thread.
Profiler Profiler
Profiler reference.
string Key
Thread key in diagrams.
ProfilerThread Parent
Parent profiler thread, if any.
void Exception(System.Exception Exception, string Label)
Exception occurred
ProfilerThread(string Name, int Order, ProfilerThreadType Type, ProfilerThread Parent)
Class that keeps track of events and timing for one thread.
void ExportPlantUmlDescription(StringBuilder Output, TimeUnit TimeUnit)
Exports events to PlantUML.
void NewState(string State)
Thread changes state.
string Label
Displayable label, equal to the name by default, but can be set later during the profiling stage.
void Interval(long From, long To, string Label)
Records an interval in the profiler thread.
TimeUnit
Options for presenting time in reports.
Definition: Profiler.cs:17
ProfilerThreadType
Type of profiler thread.