-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathMethodCallData.cs
61 lines (43 loc) · 1.78 KB
/
MethodCallData.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
namespace PostSharp.Samples.Profiling
{
public struct MethodCallData
{
internal MetricData MetricData;
private long _kernelTimestamp;
private long _threadTimestamp;
private long _userTimestamp;
private long _asyncTimestamp;
ThreadLocalSampleCollector _threadLocalCollector;
internal void Start(MetricMetadata metadata)
{
this._asyncTimestamp = ProfilingServices.GetTimestamp();
this.Metadata = metadata;
this.MetricData.ExecutionCount = 1;
this.Resume();
}
internal bool IsNull => this._asyncTimestamp == 0;
internal MetricMetadata Metadata { get; private set; }
internal void Resume()
{
this._threadTimestamp = ProfilingServices.GetTimestamp();
Win32.GetThreadTimes(Win32.GetCurrentThread(), out _, out _, out this._kernelTimestamp, out this._userTimestamp);
this._threadLocalCollector = ProfilingServices.Collector.GetThreadLocalCollector();
this._threadLocalCollector.EnterMethod(this.Metadata);
}
internal void Stop()
{
this.Pause();
this.MetricData.AsyncTime = ProfilingServices.GetTimestamp() - this._asyncTimestamp;
}
internal void Pause()
{
Win32.GetThreadTimes(Win32.GetCurrentThread(), out _, out _, out var kernelTime, out var userTime);
var cpuTime = (kernelTime - this._kernelTimestamp) + (userTime - this._userTimestamp);
var threadTime = ProfilingServices.GetTimestamp() - this._threadTimestamp;
this.MetricData.CpuTime += cpuTime;
this.MetricData.ThreadTime += threadTime;
this._threadLocalCollector.ExitMethod(this.Metadata, new ExcludedTimeData(cpuTime, threadTime));
}
internal void AddException() => this.MetricData.ExceptionCount++;
}
}