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