-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathProfiler.cs
More file actions
757 lines (662 loc) · 19.9 KB
/
Profiler.cs
File metadata and controls
757 lines (662 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using System.Xml;
using Waher.Runtime.Collections;
using Waher.Runtime.Profiling.Events;
using Waher.Runtime.Profiling.Export;
namespace Waher.Runtime.Profiling
{
/// <summary>
/// Options for presenting time in reports.
/// </summary>
public enum TimeUnit
{
/// <summary>
/// A time unit is selected based on amount of time, for particular event.
/// </summary>
DynamicPerEvent,
/// <summary>
/// A time unit is selected based on amount of time, for particular thread.
/// </summary>
DynamicPerThread,
/// <summary>
/// A time unit is selected based on amount of time, for entire profiling.
/// </summary>
DynamicPerProfiling,
/// <summary>
/// Time is presented in microseconds
/// </summary>
MicroSeconds,
/// <summary>
/// Time is presented in milliseconds
/// </summary>
MilliSeconds,
/// <summary>
/// Time is presented in seconds
/// </summary>
Seconds,
/// <summary>
/// Time is presented in minutes
/// </summary>
Minutes,
/// <summary>
/// Time is presented in hours
/// </summary>
Hours,
/// <summary>
/// Time is presented in days
/// </summary>
Days
}
/// <summary>
/// Class that keeps track of events and timing.
/// </summary>
public class Profiler
{
private readonly SortedDictionary<string, int> exceptionOrdinals = new SortedDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private readonly SortedDictionary<string, int> eventOrdinals = new SortedDictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
private readonly ChunkedList<ProfilerThread> threads = new ChunkedList<ProfilerThread>();
private readonly SortedDictionary<int, object> notes = new SortedDictionary<int, object>();
private readonly Dictionary<string, ProfilerThread> threadsByName = new Dictionary<string, ProfilerThread>();
private readonly ProfilerThread mainThread;
private readonly Stopwatch watch;
private DateTime started = DateTime.MinValue;
private double timeScale = 1;
private int threadOrder = 0;
/// <summary>
/// Class that keeps track of events and timing.
/// </summary>
public Profiler()
: this("Main", ProfilerThreadType.Sequential)
{
}
/// <summary>
/// Class that keeps track of events and timing.
/// </summary>
/// <param name="Name">Name of main thread.</param>
public Profiler(string Name)
: this(Name, ProfilerThreadType.Sequential)
{
}
/// <summary>
/// Class that keeps track of events and timing.
/// </summary>
/// <param name="Type">Type of profiler thread.</param>
public Profiler(ProfilerThreadType Type)
: this("Main", Type)
{
}
/// <summary>
/// Class that keeps track of events and timing.
/// </summary>
/// <param name="Name">Name of main thread.</param>
/// <param name="Type">Type of profiler thread.</param>
public Profiler(string Name, ProfilerThreadType Type)
{
this.mainThread = this.CreateThread(Name, Type);
this.watch = new Stopwatch();
}
/// <summary>
/// Main thread.
/// </summary>
public ProfilerThread MainThread => this.mainThread;
/// <summary>
/// Creates a new profiler thread.
/// </summary>
/// <param name="Name">Name of profiler thread.</param>
/// <param name="Type">Type of profiler thread.</param>
/// <returns>Profiler thread reference.</returns>
public ProfilerThread CreateThread(string Name, ProfilerThreadType Type)
{
lock (this.threads)
{
ProfilerThread Result = new ProfilerThread(Name, ++this.threadOrder, Type, this);
this.threads.Add(Result);
this.threadsByName[Name] = Result;
return Result;
}
}
/// <summary>
/// Gets a profiler thread. If none is available, a new is created.
/// </summary>
/// <param name="Name">Name of profiler thread.</param>
/// <param name="Type">Type of profiler thread.</param>
/// <returns>Profiler thread reference.</returns>
public ProfilerThread GetThread(string Name, ProfilerThreadType Type)
{
lock (this.threads)
{
if (this.threadsByName.TryGetValue(Name, out ProfilerThread Result))
return Result;
Result = new ProfilerThread(Name, ++this.threadOrder, Type, this);
this.threads.Add(Result);
this.threadsByName[Name] = Result;
return Result;
}
}
/// <summary>
/// Tries to get an existing profiler thread. If none is available, null is returned.
/// </summary>
/// <param name="Name">Name of profiler thread.</param>
/// <returns>Profiler thread reference, if exists, null otherwise.</returns>
public ProfilerThread TryGetThread(string Name)
{
lock (this.threads)
{
if (this.threadsByName.TryGetValue(Name, out ProfilerThread Result))
return Result;
return null;
}
}
/// <summary>
/// Creates a new profiler thread.
/// </summary>
/// <param name="Name">Name of profiler thread.</param>
/// <param name="Type">Type of profiler thread.</param>
/// <param name="Parent">Parent thread.</param>
/// <returns>Profiler thread reference.</returns>
internal ProfilerThread CreateThread(string Name, ProfilerThreadType Type, ProfilerThread Parent)
{
lock (this.threads)
{
ProfilerThread Result = new ProfilerThread(Name, ++this.threadOrder, Type, Parent);
this.threads.Add(Result);
this.threadsByName[Name] = Result;
return Result;
}
}
/// <summary>
/// Gets a profiler thread. If none is available, a new is created.
/// </summary>
/// <param name="Name">Name of profiler thread.</param>
/// <param name="Type">Type of profiler thread.</param>
/// <param name="Parent">Parent thread.</param>
/// <returns>Profiler thread reference.</returns>
internal ProfilerThread GetThread(string Name, ProfilerThreadType Type, ProfilerThread Parent)
{
lock (this.threads)
{
if (this.threadsByName.TryGetValue(Name, out ProfilerThread Result))
return Result;
Result = new ProfilerThread(Name, ++this.threadOrder, Type, Parent);
this.threads.Add(Result);
this.threadsByName[Name] = Result;
return Result;
}
}
/// <summary>
/// Starts measuring time.
/// </summary>
public void Start()
{
this.started = DateTime.Now;
this.watch.Start();
this.mainThread.Start();
}
/// <summary>
/// Stops measuring time.
/// </summary>
public void Stop()
{
this.mainThread.Stop();
this.watch.Stop();
}
/// <summary>
/// Elapsed ticks since start.
/// </summary>
public long ElapsedTicks => this.watch.ElapsedTicks;
/// <summary>
/// Elapsed seconds since start.
/// </summary>
public double ElapsedSeconds => this.ToSeconds(this.ElapsedTicks);
/// <summary>
/// When profiling was started.
/// </summary>
public DateTime Started => this.started;
/// <summary>
/// Converts a <see cref="System.DateTime"/> to number of ticks, since start
/// of profiling.
/// </summary>
/// <param name="Timepoint">Timepoint.</param>
/// <returns>Number of ticks, since start of profiling.</returns>
public long GetTicks(DateTime Timepoint)
{
if (Timepoint.Kind == DateTimeKind.Utc)
Timepoint = Timepoint.ToLocalTime();
double Seconds = (Timepoint - this.started).TotalSeconds;
return (long)(Seconds * Stopwatch.Frequency + 0.5);
}
/// <summary>
/// Main Thread changes state.
/// </summary>
/// <param name="State">String representation of the new state.</param>
public void NewState(string State)
{
this.mainThread.NewState(State);
}
/// <summary>
/// A new sample value has been recored
/// </summary>
/// <param name="Sample">New sample value.</param>
public void NewSample(double Sample)
{
this.mainThread.NewSample(Sample);
}
/// <summary>
/// Main Thread goes idle.
/// </summary>
public void Idle()
{
this.mainThread.Idle();
}
/// <summary>
/// Sets the (binary) state of the Main Thread to "high".
/// </summary>
public void High()
{
this.mainThread.High();
}
/// <summary>
/// Sets the (binary) state Main Thread to "low".
/// </summary>
public void Low()
{
this.mainThread.Low();
}
/// <summary>
/// Records an interval in the main thread.
/// </summary>
/// <param name="From">Starting timepoint.</param>
/// <param name="To">Ending timepoint.</param>
/// <param name="Label">Interval label.</param>
public void Interval(DateTime From, DateTime To, string Label)
{
this.mainThread.Interval(this.GetTicks(From), this.GetTicks(To), Label);
}
/// <summary>
/// Records an interval in the main thread.
/// </summary>
/// <param name="From">Starting timepoint, in ticks.</param>
/// <param name="To">Ending timepoint, in ticks.</param>
/// <param name="Label">Interval label.</param>
public void Interval(long From, long To, string Label)
{
this.mainThread.Interval(From, To, Label);
}
/// <summary>
/// Event occurred on main thread
/// </summary>
/// <param name="Name">Name of event.</param>
public void Event(string Name)
{
this.mainThread.Event(Name);
}
/// <summary>
/// Event occurred on main thread
/// </summary>
/// <param name="Name">Name of event.</param>
/// <param name="Label">Optional label.</param>
public void Event(string Name, string Label)
{
this.mainThread.Event(Name, Label);
}
/// <summary>
/// Event occurred on main thread
/// </summary>
/// <param name="Exception">Exception object.</param>
public void Exception(System.Exception Exception)
{
this.mainThread.Exception(Exception);
}
/// <summary>
/// Event occurred on main thread
/// </summary>
/// <param name="Exception">Exception object.</param>
/// <param name="Label">Optional label.</param>
public void Exception(System.Exception Exception, string Label)
{
this.mainThread.Exception(Exception, Label);
}
/// <summary>
/// Number of seconds, corresponding to a measured number of high-frequency clock ticks.
/// </summary>
/// <param name="Ticks">Ticks</param>
/// <returns>Number of seconds.</returns>
public double ToSeconds(long Ticks)
{
return ((double)Ticks) / Stopwatch.Frequency;
}
/// <summary>
/// Time (amount, unit), corresponding to a measured number of high-frequency clock ticks.
/// </summary>
/// <param name="Ticks">Ticks</param>
/// <param name="Thread">Thread associated with event.</param>
/// <param name="TimeUnit">Time unit to use.</param>
/// <returns>Corresponding time.</returns>
public KeyValuePair<double, string> ToTime(long Ticks, ProfilerThread Thread, TimeUnit TimeUnit)
{
double Amount = this.ToSeconds(Ticks);
double Reference;
switch (TimeUnit)
{
case TimeUnit.MicroSeconds:
return new KeyValuePair<double, string>(Amount * 1e6, "μs");
case TimeUnit.MilliSeconds:
return new KeyValuePair<double, string>(Amount * 1e3, "ms");
case TimeUnit.Seconds:
return new KeyValuePair<double, string>(Amount, "s");
case TimeUnit.Minutes:
return new KeyValuePair<double, string>(Amount / 60, "min");
case TimeUnit.Hours:
return new KeyValuePair<double, string>(Amount / (60 * 60), "h");
case TimeUnit.Days:
return new KeyValuePair<double, string>(Amount / (24 * 60 * 60), "d");
case TimeUnit.DynamicPerProfiling:
Reference = this.ToSeconds(this.MainThread.StoppedAt ?? this.ElapsedTicks);
break;
case TimeUnit.DynamicPerThread:
Reference = this.ToSeconds(Thread.StoppedAt ?? this.ElapsedTicks);
break;
case TimeUnit.DynamicPerEvent:
default:
Reference = Amount;
break;
}
Reference *= this.timeScale;
if (Reference < 1)
{
Amount *= 1e3;
Reference *= 1e3;
if (Reference < 1)
{
Amount *= 1e3;
return new KeyValuePair<double, string>(Amount, "μs");
}
else
return new KeyValuePair<double, string>(Amount, "ms");
}
else if (Reference > 100)
{
Amount /= 60;
Reference /= 60;
if (Reference > 100)
{
Amount /= 60;
Reference /= 60;
if (Reference > 100)
{
Amount /= 24;
return new KeyValuePair<double, string>(Amount, "d");
}
else
return new KeyValuePair<double, string>(Amount, "h");
}
else
return new KeyValuePair<double, string>(Amount, "min");
}
else
return new KeyValuePair<double, string>(Amount, "s");
}
/// <summary>
/// String representation of time, corresponding to a measured number of high-frequency clock ticks.
/// </summary>
/// <param name="Ticks">Ticks</param>
/// <param name="Thread">Thread associated with event.</param>
/// <param name="TimeUnit">Time unit to use.</param>
/// <param name="NrDecimals">Number of decimals.</param>
/// <returns>Corresponding time as a string.</returns>
public string ToTimeStr(long Ticks, ProfilerThread Thread, TimeUnit TimeUnit, int NrDecimals)
{
KeyValuePair<double, string> Time = this.ToTime(Ticks, Thread, TimeUnit);
return Time.Key.ToString("F" + NrDecimals.ToString()) + " " + Time.Value;
}
/// <summary>
/// Exports events to XML.
/// </summary>
/// <param name="TimeUnit">Time unit to use.</param>
/// <returns>XML</returns>
public string ExportXml(TimeUnit TimeUnit)
{
StringBuilder sb = new StringBuilder();
XmlWriterSettings Settings = new XmlWriterSettings()
{
Indent = true,
IndentChars = "\t",
NewLineChars = "\r\n",
NewLineHandling = NewLineHandling.Replace,
NewLineOnAttributes = false,
OmitXmlDeclaration = true
};
using (XmlWriter Output = XmlWriter.Create(sb, Settings))
{
this.ExportXml(Output, TimeUnit);
}
return sb.ToString();
}
/// <summary>
/// Exports events to XML.
/// </summary>
/// <param name="Output">XML output.</param>
/// <param name="TimeUnit">Time unit to use.</param>
public void ExportXml(XmlWriter Output, TimeUnit TimeUnit)
{
Output.WriteStartElement("Profiler", "http://waher.se/schema/Profiler.xsd");
Output.WriteAttributeString("ticksPerSecond", Stopwatch.Frequency.ToString());
Output.WriteAttributeString("timePerTick", this.ToTimeStr(1, this.mainThread, TimeUnit.DynamicPerEvent, 7));
ProfilerThread[] Threads;
lock (this.threads)
{
Threads = this.threads.ToArray();
}
foreach (ProfilerThread Thread in Threads)
{
if (Thread.Parent is null)
Thread.ExportXml(Output, TimeUnit);
}
Output.WriteEndElement();
}
/// <summary>
/// Exports events to PlantUML.
/// </summary>
/// <param name="TimeUnit">Time unit to use.</param>
/// <returns>PlantUML</returns>
public string ExportPlantUml(TimeUnit TimeUnit)
{
StringBuilder sb = new StringBuilder();
this.ExportPlantUml(sb, TimeUnit);
return sb.ToString();
}
/// <summary>
/// Exports events to PlantUML.
/// </summary>
/// <param name="Output">PlantUML output.</param>
/// <param name="TimeUnit">Time unit to use.</param>
public void ExportPlantUml(StringBuilder Output, TimeUnit TimeUnit)
{
this.ExportPlantUml(Output, TimeUnit, 1000);
}
/// <summary>
/// Exports events to PlantUML.
/// </summary>
/// <param name="Output">PlantUML output.</param>
/// <param name="TimeUnit">Time unit to use.</param>
/// <param name="GoalWidth">Goal width of diagram, in pixels.</param>
public void ExportPlantUml(StringBuilder Output, TimeUnit TimeUnit, int GoalWidth)
{
switch (TimeUnit)
{
case TimeUnit.DynamicPerEvent:
case TimeUnit.DynamicPerThread:
throw new InvalidOperationException("Diagram requires the same time base to be used through-out.");
}
Output.AppendLine("@startuml");
ProfilerThread[] Threads;
lock (this.threads)
{
Threads = this.threads.ToArray();
}
foreach (ProfilerThread Thread in Threads)
{
if (Thread.Parent is null)
Thread.ExportPlantUmlDescription(Output, TimeUnit);
}
lock (this.eventOrdinals)
{
foreach (KeyValuePair<string, int> P in this.eventOrdinals)
{
Output.Append("concise \"");
Output.Append(EscapeLabel(P.Key));
Output.Append("\" as E");
Output.AppendLine(P.Value.ToString());
}
}
lock (this.exceptionOrdinals)
{
foreach (KeyValuePair<string, int> P in this.exceptionOrdinals)
{
Output.Append("concise \"");
Output.Append(EscapeLabel(P.Key));
Output.Append("\" as X");
Output.AppendLine(P.Value.ToString());
}
}
double TimeSpan;
double StepSize;
int NrSteps;
do
{
KeyValuePair<double, string> TotalTime = this.ToTime(this.mainThread.StoppedAt ?? this.ElapsedTicks, this.mainThread, TimeUnit);
TimeSpan = TotalTime.Key;
StepSize = Math.Pow(10, Math.Round(Math.Log10(TimeSpan / 10)));
NrSteps = (int)Math.Floor(TimeSpan / StepSize);
if (NrSteps >= 50)
StepSize *= 5;
else if (NrSteps >= 25)
StepSize *= 2.5;
else if (NrSteps >= 20)
StepSize *= 2;
else if (NrSteps <= 2)
StepSize /= 5;
else if (NrSteps <= 4)
StepSize /= 2.5;
else if (NrSteps <= 5)
StepSize /= 2;
if (StepSize < 1)
{
if (TimeUnit != TimeUnit.DynamicPerProfiling &&
TimeUnit != TimeUnit.DynamicPerEvent &&
TimeUnit != TimeUnit.DynamicPerThread)
{
break;
}
this.timeScale *= 1e-3;
}
}
while (StepSize < 1 && StepSize > 0);
StepSize = Math.Ceiling(StepSize);
NrSteps = Math.Max(1, (int)Math.Floor(TimeSpan / StepSize));
int PixelsPerStep = Math.Max(1, GoalWidth / NrSteps);
Output.Append("scale ");
Output.Append(Math.Ceiling(StepSize).ToString("F0"));
Output.Append(" as ");
Output.Append(PixelsPerStep);
Output.AppendLine(" pixels");
PlantUmlStates States = new PlantUmlStates(TimeUnit);
foreach (ProfilerThread Thread in Threads)
{
if (Thread.Parent is null)
Thread.ExportPlantUmlEvents(States);
}
foreach (KeyValuePair<long, StringBuilder> P in States.ByTime)
{
KeyValuePair<double, string> Time = this.ToTime(P.Key, null, TimeUnit);
Output.Append('@');
Output.AppendLine(Time.Key.ToString("F7").Replace(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, "."));
Output.Append(P.Value.ToString());
}
Output.Append(States.Summary.ToString());
Output.AppendLine("@enduml");
}
internal static string EscapeLabel(string s)
{
return s.
Replace("\\", "\\\\").
Replace('"', '\'').
Replace("\r", "\\r").
Replace("\n", "\\n");
}
/// <summary>
/// Gets the ordinal for an event.
/// </summary>
/// <param name="Event">Event</param>
/// <returns>Event Ordinal</returns>
public int GetEventOrdinal(string Event)
{
return GetOrdinal(this.eventOrdinals, Event);
}
private static int GetOrdinal(SortedDictionary<string, int> Ordinals, string Key)
{
lock (Ordinals)
{
if (Ordinals.TryGetValue(Key, out int Ordinal))
return Ordinal;
Ordinal = Ordinals.Count;
Ordinals[Key] = Ordinal;
return Ordinal;
}
}
/// <summary>
/// Gets the ordinal for a type of exception.
/// </summary>
/// <param name="Exception">Exception object.</param>
/// <returns>Exception Type Ordinal</returns>
public int GetExceptionOrdinal(System.Exception Exception)
{
return GetOrdinal(this.exceptionOrdinals, Exception.GetType().FullName);
}
/// <summary>
/// Adds a note to the profile.
/// </summary>
/// <param name="Note">Note to add.</param>
/// <returns>Note index. First note added receives index 1.</returns>
public int AddNote(object Note)
{
int Result;
lock (this.notes)
{
Result = this.notes.Count + 1;
this.notes[Result] = Note;
}
return Result;
}
/// <summary>
/// Number of notes added.
/// </summary>
public int NoteCount
{
get
{
lock (this.notes)
{
return this.notes.Count;
}
}
}
/// <summary>
/// Tries to get a note from the profile.
/// </summary>
/// <param name="Index">1-based note index.</param>
/// <param name="Note">Note, if found.</param>
/// <returns>If a note was found with the corresponding index.</returns>
public bool TryGetNote(int Index, out object Note)
{
lock (this.notes)
{
return this.notes.TryGetValue(Index, out Note);
}
}
}
}