Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Accumulator.cs
4
6{
10 public class Accumulator
11 {
12 private readonly Dictionary<string, long> sumTicks = new Dictionary<string, long>();
13 private readonly SortedDictionary<long, ChunkedList<ProfilerEvent>> timeline = new SortedDictionary<long, ChunkedList<ProfilerEvent>>();
14 private readonly ChunkedList<string> order = new ChunkedList<string>();
15 private readonly ProfilerThread thread;
16 private readonly object synchObj = new object();
17 private long startTick = 0;
18 private long lastTick = 0;
19 private string lastName = null;
20
26 {
27 this.thread = Thread;
28 }
29
35 {
36 long Tick = Event.Ticks;
37
38 lock (this.synchObj)
39 {
40 this.CheckNameLocked(Tick);
41 this.lastTick = Tick;
42 this.AddLocked(Tick, Event);
43 }
44 }
45
46 private void AddLocked(long Tick, ProfilerEvent Event)
47 {
48 if (!this.timeline.TryGetValue(Tick, out ChunkedList<ProfilerEvent> Events))
49 {
50 Events = new ChunkedList<ProfilerEvent>();
51 this.timeline[Tick] = Events;
52 }
53
54 Events.Add(Event);
55 }
56
57 private void CheckNameLocked(long Tick)
58 {
59 if (!string.IsNullOrEmpty(this.lastName))
60 {
61 long Diff = Tick - this.lastTick;
62
63 if (this.sumTicks.TryGetValue(this.lastName, out long l))
64 this.sumTicks[this.lastName] = l + Diff;
65 else
66 {
67 this.sumTicks[this.lastName] = Diff;
68 this.order.Add(this.lastName);
69 }
70
71 this.lastName = null;
72 }
73 }
74
79 public void Start(Start Event)
80 {
81 this.startTick = Event.Ticks;
82 this.AddAsIs(Event);
83 }
84
89 public void Idle(Idle Event)
90 {
91 long Tick = Event.Ticks;
92
93 lock (this.synchObj)
94 {
95 this.CheckNameLocked(Tick);
96 this.lastTick = Tick;
97 }
98 }
99
104 public void Sum(NewState Event)
105 {
106 long Tick = Event.Ticks;
107
108 lock (this.synchObj)
109 {
110 this.CheckNameLocked(Tick);
111 this.lastTick = Tick;
112 this.lastName = Event.State;
113 }
114 }
115
121 {
122 lock (this.synchObj)
123 {
124 long Tick = this.startTick;
125
126 foreach (string Name in this.order)
127 {
128 if (this.sumTicks.TryGetValue(Name, out long Ticks))
129 {
130 this.AddLocked(Tick, new NewState(Tick, Name, this.thread));
131 Tick += Ticks;
132 }
133 }
134
135 this.AddLocked(Tick, new Idle(Tick, this.thread));
136
137 foreach (ChunkedList<ProfilerEvent> Events in this.timeline.Values)
138 Result.AddRange(Events);
139 }
140 }
141
142 }
143}
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void AddRange(IEnumerable< T > Collection)
Adds a range of elements (last) to the list.
Abstract base class for profiler events.
void Sum(NewState Event)
Sums time for a given state.
Definition: Accumulator.cs:104
void Idle(Idle Event)
Reports thread to be idle.
Definition: Accumulator.cs:89
void Report(ChunkedList< ProfilerEvent > Result)
Reports accumulated events to a list of events.
Definition: Accumulator.cs:120
void Start(Start Event)
Adds an event, as-is.
Definition: Accumulator.cs:79
Accumulator(ProfilerThread Thread)
Accumulates events
Definition: Accumulator.cs:25
void AddAsIs(ProfilerEvent Event)
Adds an event, as-is.
Definition: Accumulator.cs:34
Class that keeps track of events and timing for one thread.