Repository navigation
Expand file tree
/
Copy pathCodeStatProcessor.cs
More file actions
141 lines (118 loc) · 4.83 KB
/
Copy pathCodeStatProcessor.cs
File metadata and controls
141 lines (118 loc) · 4.83 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
/*----------------------------------------------------------
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.Concurrent;
using System.Diagnostics;
using System.Linq;
using System.Threading;
namespace ScriptEngine.Machine
{
/// <summary>
/// Сборщик статистики исполнения кода. Его вызывают все потоки, где исполняется код,
/// в том числе фоновые задания: коллекции потокобезопасные, а время каждый поток меряет сам.
/// </summary>
public class CodeStatProcessor : ICodeStatCollector
{
private readonly ConcurrentDictionary<CodeStatEntry, EntryStat> _stats =
new ConcurrentDictionary<CodeStatEntry, EntryStat>();
private readonly ConcurrentDictionary<string, bool> _preparedScripts =
new ConcurrentDictionary<string, bool>();
// Точка, которую сейчас исполняет поток: ей идет время до перехода к следующей точке
private readonly ThreadLocal<ActiveEntry> _activeEntry =
new ThreadLocal<ActiveEntry>(() => new ActiveEntry());
// Делегат создается один раз: лямбда в вызове GetOrAdd выделяла бы память на каждую строку
private readonly Func<CodeStatEntry, EntryStat> _newStat;
// Отчет идет в порядке регистрации точек
private long _registrationOrder;
public CodeStatProcessor()
{
_newStat = _ => new EntryStat(Interlocked.Increment(ref _registrationOrder));
}
public bool IsPrepared(string ScriptFileName)
{
return _preparedScripts.ContainsKey(ScriptFileName);
}
public void MarkEntryReached(CodeStatEntry entry, int count = 1)
{
var stat = _stats.GetOrAdd(entry, _newStat);
if (count == 0)
return;
Interlocked.Add(ref stat.Count, count);
SwitchTo(stat);
}
public void MarkPrepared(string scriptFileName)
{
_preparedScripts.TryAdd(scriptFileName, true);
}
/// <summary>
/// Снимок статистики. Можно брать, пока код еще исполняется в других потоках.
/// </summary>
public CodeStatDataCollection GetStatData()
{
var data = new CodeStatDataCollection();
foreach (var item in _stats.ToArray().OrderBy(x => x.Value.Order))
{
if (!IsPrepared(item.Key.ScriptFileName))
{
continue;
}
data.Add(new CodeStatData(item.Key, item.Value.ElapsedMilliseconds, Volatile.Read(ref item.Value.Count)));
}
return data;
}
/// <summary>
/// Завершает замер времени в текущем потоке
/// </summary>
public void EndCodeStat()
{
StopCurrentWatch();
}
public void StopCurrentWatch()
{
SwitchTo(null);
}
public void StopWatch(CodeStatEntry entry)
{
if (_stats.TryGetValue(entry, out var stat) && _activeEntry.Value.Stat == stat)
{
SwitchTo(null);
}
}
public void ResumeWatch(CodeStatEntry entry)
{
_stats.TryGetValue(entry, out var stat);
SwitchTo(stat);
}
private void SwitchTo(EntryStat next)
{
var active = _activeEntry.Value;
var now = Stopwatch.GetTimestamp();
active.Stat?.AddElapsed(now - active.Since);
active.Stat = next;
active.Since = now;
}
private sealed class EntryStat
{
public readonly long Order;
public int Count;
private long _elapsedTicks;
public EntryStat(long order)
{
Order = order;
}
public void AddElapsed(long ticks) => Interlocked.Add(ref _elapsedTicks, ticks);
// Так же, как Stopwatch.ElapsedMilliseconds
public long ElapsedMilliseconds =>
Stopwatch.GetElapsedTime(0, Interlocked.Read(ref _elapsedTicks)).Ticks / TimeSpan.TicksPerMillisecond;
}
private sealed class ActiveEntry
{
public EntryStat Stat;
public long Since;
}
}
}