-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathCodeStatHub.cs
More file actions
159 lines (139 loc) · 4.75 KB
/
Copy pathCodeStatHub.cs
File metadata and controls
159 lines (139 loc) · 4.75 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
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using System;
using System.Collections.Generic;
namespace ScriptEngine.Machine
{
public sealed class CodeStatHub : ICodeStatCollector
{
private readonly object _lock = new object();
private readonly List<CodeStatEntry> _knownEntries = new List<CodeStatEntry>();
private readonly HashSet<CodeStatEntry> _knownSet = new HashSet<CodeStatEntry>();
private readonly HashSet<string> _preparedScripts = new HashSet<string>();
private CodeStatProcessor[] _alive = Array.Empty<CodeStatProcessor>();
private CodeStatProcessor[] _active = Array.Empty<CodeStatProcessor>();
public CodeStatProcessor StartSession()
{
var session = new CodeStatProcessor(this);
lock (_lock)
{
_alive = Append(_alive, session);
_active = Append(_active, session);
}
return session;
}
public void PauseSession(CodeStatProcessor session)
{
lock (_lock)
{
session.StopActiveWatch();
_active = Remove(_active, session);
}
}
public void ResumeSession(CodeStatProcessor session)
{
lock (_lock)
{
if (Array.IndexOf(_alive, session) < 0)
return;
if (Array.IndexOf(_active, session) >= 0)
return;
_active = Append(_active, session);
}
}
public void FinishSession(CodeStatProcessor session, bool excludeZeros = false)
{
lock (_lock)
{
session.EndCodeStat();
if (excludeZeros)
session.FinishWithoutCatalog();
else
session.FreezeCatalog(_knownEntries.ToArray(), new HashSet<string>(_preparedScripts));
_active = Remove(_active, session);
_alive = Remove(_alive, session);
}
}
internal CodeStatDataCollection GetLiveStatData(CodeStatProcessor session)
{
lock (_lock)
{
return session.BuildFromCatalog(_knownEntries, _knownEntries.Count, _preparedScripts);
}
}
public bool IsPrepared(string ScriptFileName)
{
lock (_lock)
{
return _preparedScripts.Contains(ScriptFileName);
}
}
internal HashSet<string> SnapshotPreparedScripts()
{
lock (_lock)
{
return new HashSet<string>(_preparedScripts);
}
}
public void MarkEntryReached(CodeStatEntry entry, int count = 1)
{
lock (_lock)
{
if (_knownSet.Add(entry))
_knownEntries.Add(entry);
if (count == 0)
return;
foreach (var session in _active)
session.MarkEntryReached(entry, count);
}
}
public void MarkPrepared(string scriptFileName)
{
lock (_lock)
{
_preparedScripts.Add(scriptFileName);
}
}
public void StopWatch(CodeStatEntry entry)
{
lock (_lock)
{
foreach (var session in _active)
session.StopWatch(entry);
}
}
public void ResumeWatch(CodeStatEntry entry)
{
lock (_lock)
{
foreach (var session in _active)
session.ResumeWatch(entry);
}
}
private static T[] Append<T>(T[] source, T item)
{
var result = new T[source.Length + 1];
Array.Copy(source, result, source.Length);
result[source.Length] = item;
return result;
}
private static T[] Remove<T>(T[] source, T item) where T : class
{
var index = Array.IndexOf(source, item);
if (index < 0)
return source;
if (source.Length == 1)
return Array.Empty<T>();
var result = new T[source.Length - 1];
if (index > 0)
Array.Copy(source, 0, result, 0, index);
if (index < source.Length - 1)
Array.Copy(source, index + 1, result, index, source.Length - index - 1);
return result;
}
}
}