forked from 47-studio-org/PostSharp.Samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSampleCollector.cs
120 lines (84 loc) · 3.18 KB
/
SampleCollector.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
namespace PostSharp.Samples.Profiling
{
internal class SampleCollector
{
// This is the maximal duration allowed to collect the metrics from all threads for a single method.
static readonly long timestampTolerance = Stopwatch.Frequency / 100;
private readonly ThreadLocal<ThreadLocalSampleCollector> _threadLocalCollectors;
public IList<ThreadLocalSampleCollector> ThreadLocalCollectors => this._threadLocalCollectors.Values;
private readonly object registrationLock = new object();
private MetricMetadata[] _metricsMetadata = new MetricMetadata[1024];
public int ProfiledMethodCount { get; private set; }
internal MetricMetadata[] MetricsMetadata { get => this._metricsMetadata; }
public SampleCollector()
{
this._threadLocalCollectors = new ThreadLocal<ThreadLocalSampleCollector>(() => new ThreadLocalSampleCollector(this), true);
}
public MetricMetadata RegisterMethod(MethodBase method)
{
lock (this.registrationLock)
{
var profiledMethod = new MetricMetadata(method, this.ProfiledMethodCount);
if (this.MetricsMetadata.Length <= this.ProfiledMethodCount)
{
Array.Resize(ref this._metricsMetadata, this.MetricsMetadata.Length * 2);
}
this.MetricsMetadata[profiledMethod.Index] = profiledMethod;
this.ProfiledMethodCount++;
return profiledMethod;
}
}
public ThreadLocalSampleCollector GetThreadLocalCollector() => this._threadLocalCollectors.Value;
public MetricData[] GetMetrics()
{
MetricMetadata[] profiledMethodsCopy;
MetricData[] metrics;
lock (this.registrationLock)
{
profiledMethodsCopy = this.MetricsMetadata;
metrics = new MetricData[this.ProfiledMethodCount];
}
var treadLocalCollectorsCopy = this._threadLocalCollectors.Values;
var timestamp = ProfilingServices.GetTimestamp();
for (var i = 0; i < metrics.Length; i++)
{
var method = profiledMethodsCopy[i];
var attempts = 0;
while (true)
{
metrics[i].Timestamp = timestamp;
foreach (var threadLocalCollector in treadLocalCollectorsCopy)
{
if (threadLocalCollector.GetSample(method, out var threadLocalData))
{
metrics[i].AddData(threadLocalData);
}
}
timestamp = ProfilingServices.GetTimestamp();
// Detect if our thread has been preempted, and retry if so.
if (timestamp - metrics[i].Timestamp < timestampTolerance)
{
break;
}
else
{
metrics[i] = default;
attempts++;
if (attempts > 3)
{
Console.WriteLine("Too many non-voluntary preemptions in the publisher thread. ");
metrics[i].SetInvalid();
break;
}
}
}
}
return metrics;
}
}
}