-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathyieldprocessornormalized.cpp
328 lines (278 loc) · 10.6 KB
/
yieldprocessornormalized.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "common.h"
#include "yieldprocessornormalized.h"
#include "finalizerthread.h"
enum class NormalizationState : UINT8
{
Uninitialized,
PartiallyInitialized,
Initialized,
Failed
};
static const int NsPerYieldMeasurementCount = 8;
static const int PartialInitializationMeasurementCount = 2; // number of measurements to be done during partial initialization
static const unsigned int MeasurementPeriodMs = 4000;
static const unsigned int NsPerS = 1000 * 1000 * 1000;
static NormalizationState s_normalizationState = NormalizationState::Uninitialized;
static unsigned int s_previousNormalizationTimeMs;
static UINT64 s_performanceCounterTicksPerS;
static double s_nsPerYieldMeasurements[NsPerYieldMeasurementCount];
static int s_nextMeasurementIndex;
static double s_establishedNsPerYield = YieldProcessorNormalization::TargetNsPerNormalizedYield;
static unsigned int DetermineMeasureDurationUs()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_PREEMPTIVE;
}
CONTRACTL_END;
_ASSERTE(s_normalizationState != NormalizationState::Failed);
// On some systems, querying the high performance counter has relatively significant overhead. Increase the measure duration
// if the overhead seems high relative to the measure duration.
unsigned int measureDurationUs = 1;
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
UINT64 startTicks = li.QuadPart;
QueryPerformanceCounter(&li);
UINT64 elapsedTicks = li.QuadPart - startTicks;
if (elapsedTicks >= s_performanceCounterTicksPerS * measureDurationUs * (1000 / 4) / NsPerS) // elapsed >= 1/4 of the measure duration
{
measureDurationUs *= 4;
}
return measureDurationUs;
}
static double MeasureNsPerYield(unsigned int measureDurationUs)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_PREEMPTIVE;
}
CONTRACTL_END;
_ASSERTE(s_normalizationState != NormalizationState::Failed);
int yieldCount = (int)(measureDurationUs * 1000 / s_establishedNsPerYield) + 1;
UINT64 ticksPerS = s_performanceCounterTicksPerS;
UINT64 measureDurationTicks = ticksPerS * measureDurationUs / (1000 * 1000);
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
UINT64 startTicks = li.QuadPart;
for (int i = 0; i < yieldCount; ++i)
{
System_YieldProcessor();
}
QueryPerformanceCounter(&li);
UINT64 elapsedTicks = li.QuadPart - startTicks;
while (elapsedTicks < measureDurationTicks)
{
int nextYieldCount =
Max(4,
elapsedTicks == 0
? yieldCount / 4
: (int)(yieldCount * (measureDurationTicks - elapsedTicks) / (double)elapsedTicks) + 1);
for (int i = 0; i < nextYieldCount; ++i)
{
System_YieldProcessor();
}
QueryPerformanceCounter(&li);
elapsedTicks = li.QuadPart - startTicks;
yieldCount += nextYieldCount;
}
// Limit the minimum to a reasonable value considering that on some systems a yield may be implemented as a no-op
const double MinNsPerYield = 0.1;
// Measured values higher than this don't affect values calculated for normalization, and it's very unlikely for a yield to
// really take this long. Limit the maximum to keep the recorded values reasonable.
const double MaxNsPerYield = YieldProcessorNormalization::TargetMaxNsPerSpinIteration / 1.5 + 1;
return Max(MinNsPerYield, Min((double)elapsedTicks * NsPerS / ((double)yieldCount * ticksPerS), MaxNsPerYield));
}
void YieldProcessorNormalization::PerformMeasurement()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_PREEMPTIVE;
}
CONTRACTL_END;
_ASSERTE(s_isMeasurementScheduled ^ (s_normalizationState == NormalizationState::Uninitialized));
double latestNsPerYield;
if (s_normalizationState == NormalizationState::Initialized)
{
if (GetTickCount() - s_previousNormalizationTimeMs < MeasurementPeriodMs)
{
return;
}
int nextMeasurementIndex = s_nextMeasurementIndex;
latestNsPerYield = MeasureNsPerYield(DetermineMeasureDurationUs());
AtomicStore(&s_nsPerYieldMeasurements[nextMeasurementIndex], latestNsPerYield);
if (++nextMeasurementIndex >= NsPerYieldMeasurementCount)
{
nextMeasurementIndex = 0;
}
s_nextMeasurementIndex = nextMeasurementIndex;
}
else if (s_normalizationState == NormalizationState::Uninitialized || s_normalizationState == NormalizationState::PartiallyInitialized)
{
int startIndex, endIndex;
if (s_normalizationState == NormalizationState::Uninitialized)
{
LARGE_INTEGER li;
if (!QueryPerformanceFrequency(&li) || li.QuadPart < 1000 * 1000)
{
// High precision clock not available or clock resolution is too low, resort to defaults
s_normalizationState = NormalizationState::Failed;
return;
}
s_performanceCounterTicksPerS = li.QuadPart;
startIndex = 0;
endIndex = PartialInitializationMeasurementCount;
}
else
{
startIndex = PartialInitializationMeasurementCount;
endIndex = NsPerYieldMeasurementCount;
}
unsigned int measureDurationUs = DetermineMeasureDurationUs();
latestNsPerYield = 0;
for (int i = startIndex; i < endIndex; ++i)
{
latestNsPerYield = MeasureNsPerYield(measureDurationUs);
AtomicStore(&s_nsPerYieldMeasurements[i], latestNsPerYield);
if (i == 0 || latestNsPerYield < s_establishedNsPerYield)
{
AtomicStore(&s_establishedNsPerYield, latestNsPerYield);
}
if (i < endIndex - 1)
{
FireEtwYieldProcessorMeasurement(GetClrInstanceId(), latestNsPerYield, s_establishedNsPerYield);
}
}
}
else
{
_ASSERTE(s_normalizationState == NormalizationState::Failed);
return;
}
double establishedNsPerYield = s_nsPerYieldMeasurements[0];
int endIndex =
(s_normalizationState == NormalizationState::Uninitialized) ?
PartialInitializationMeasurementCount :
NsPerYieldMeasurementCount;
for (int i = 1; i < endIndex; ++i)
{
double nsPerYield = s_nsPerYieldMeasurements[i];
if (nsPerYield < establishedNsPerYield)
{
establishedNsPerYield = nsPerYield;
}
}
if (establishedNsPerYield != s_establishedNsPerYield)
{
AtomicStore(&s_establishedNsPerYield, establishedNsPerYield);
}
FireEtwYieldProcessorMeasurement(GetClrInstanceId(), latestNsPerYield, s_establishedNsPerYield);
// Calculate the number of yields required to span the duration of a normalized yield
unsigned int yieldsPerNormalizedYield = Max(1u, (unsigned int)(TargetNsPerNormalizedYield / establishedNsPerYield + 0.5));
_ASSERTE(yieldsPerNormalizedYield <= MaxYieldsPerNormalizedYield);
s_yieldsPerNormalizedYield = yieldsPerNormalizedYield;
// Calculate the maximum number of yields that would be optimal for a late spin iteration. Typically, we would not want to
// spend excessive amounts of time (thousands of cycles) doing only YieldProcessor, as SwitchToThread/Sleep would do a
// better job of allowing other work to run.
s_optimalMaxNormalizedYieldsPerSpinIteration =
Max(1u, (unsigned int)(TargetMaxNsPerSpinIteration / (yieldsPerNormalizedYield * establishedNsPerYield) + 0.5));
_ASSERTE(s_optimalMaxNormalizedYieldsPerSpinIteration <= MaxOptimalMaxNormalizedYieldsPerSpinIteration);
GCHeapUtilities::GetGCHeap()->SetYieldProcessorScalingFactor((float)yieldsPerNormalizedYield);
if (s_normalizationState != NormalizationState::Uninitialized)
{
s_previousNormalizationTimeMs = GetTickCount();
}
s_normalizationState =
(s_normalizationState == NormalizationState::Uninitialized) ?
NormalizationState::PartiallyInitialized :
NormalizationState::Initialized;
s_isMeasurementScheduled = false;
}
void YieldProcessorNormalization::ScheduleMeasurementIfNecessary()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
NormalizationState normalizationState = VolatileLoadWithoutBarrier(&s_normalizationState);
if (normalizationState == NormalizationState::Initialized)
{
if (GetTickCount() - s_previousNormalizationTimeMs < MeasurementPeriodMs)
{
return;
}
}
else if (normalizationState == NormalizationState::Uninitialized || normalizationState == NormalizationState::PartiallyInitialized)
{
}
else
{
_ASSERTE(normalizationState == NormalizationState::Failed);
return;
}
// !g_fEEStarted is required for FinalizerThread::EnableFinalization() below
if (s_isMeasurementScheduled || !g_fEEStarted)
{
return;
}
s_isMeasurementScheduled = true;
FinalizerThread::EnableFinalization();
}
void YieldProcessorNormalization::FireMeasurementEvents()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
if (!EventEnabledYieldProcessorMeasurement())
{
return;
}
// This function may be called at any time to fire events about recorded measurements. There is no synchronization for the
// recorded information, so try to enumerate the array with some care.
double establishedNsPerYield = AtomicLoad(&s_establishedNsPerYield);
int nextIndex = VolatileLoadWithoutBarrier(&s_nextMeasurementIndex);
for (int i = 0; i < NsPerYieldMeasurementCount; ++i)
{
double nsPerYield = AtomicLoad(&s_nsPerYieldMeasurements[nextIndex]);
if (nsPerYield != 0) // the array may not be fully initialized yet
{
FireEtwYieldProcessorMeasurement(GetClrInstanceId(), nsPerYield, establishedNsPerYield);
}
if (++nextIndex >= NsPerYieldMeasurementCount)
{
nextIndex = 0;
}
}
}
double YieldProcessorNormalization::AtomicLoad(double *valueRef)
{
WRAPPER_NO_CONTRACT;
#ifdef TARGET_64BIT
return VolatileLoadWithoutBarrier(valueRef);
#else
return InterlockedCompareExchangeT(valueRef, 0.0, 0.0);
#endif
}
void YieldProcessorNormalization::AtomicStore(double *valueRef, double value)
{
WRAPPER_NO_CONTRACT;
#ifdef TARGET_64BIT
*valueRef = value;
#else
InterlockedExchangeT(valueRef, value);
#endif
}