-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ep-sample-profiler.c
342 lines (274 loc) · 8.57 KB
/
ep-sample-profiler.c
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "ep-rt-config.h"
#ifdef ENABLE_PERFTRACING
#if !defined(EP_INCLUDE_SOURCE_FILES) || defined(EP_FORCE_INCLUDE_SOURCE_FILES)
#define EP_IMPL_SAMPLE_PROFILER_GETTER_SETTER
#include "ep.h"
#include "ep-sample-profiler.h"
#include "ep-event.h"
#include "ep-provider-internals.h"
#include "ep-rt.h"
#define NUM_NANOSECONDS_IN_1_MS 1000000
static volatile uint32_t _profiling_enabled = (uint32_t)false;
static EventPipeProvider *_sampling_provider = NULL;
static EventPipeEvent *_thread_time_event = NULL;
static ep_rt_wait_event_handle_t _thread_shutdown_event;
static uint64_t _sampling_rate_in_ns = NUM_NANOSECONDS_IN_1_MS; // 1ms
static bool _time_period_is_set = false;
static volatile uint32_t _can_start_sampling = (uint32_t)false;
static int32_t _ref_count = 0;
#ifdef HOST_WIN32
#include <mmsystem.h>
static PVOID _time_begin_period_func = NULL;
static PVOID _time_end_period_func = NULL;
static HINSTANCE _multimedia_library_handle = NULL;
typedef MMRESULT(WINAPI *time_period_func)(UINT uPeriod);
#endif
/*
* Forward declares of all static functions.
*/
EP_RT_DEFINE_THREAD_FUNC (sampling_thread);
static
void
sample_profiler_set_time_granularity (void);
static
void
sample_profiler_reset_time_granularity (void);
static
bool
sample_profiler_load_dependencies (void);
static
void
sample_profiler_unload_dependencies (void);
static
void
sample_profiler_enable (void);
/*
* EventPipeSampleProfiler.
*/
static
inline
bool
sample_profiler_load_profiling_enabled (void)
{
return (ep_rt_volatile_load_uint32_t (&_profiling_enabled) != 0) ? true : false;
}
static
inline
void
sample_profiler_store_profiling_enabled (bool enabled)
{
ep_rt_volatile_store_uint32_t (&_profiling_enabled, enabled ? 1 : 0);
}
static
inline
void
sample_profiler_store_can_start_sampling (bool start_sampling)
{
ep_rt_volatile_store_uint32_t (&_can_start_sampling, start_sampling ? 1 : 0);
}
EP_RT_DEFINE_THREAD_FUNC (sampling_thread)
{
EP_ASSERT (data != NULL);
if (data == NULL)
return 1;
ep_rt_thread_params_t *thread_params = (ep_rt_thread_params_t *)data;
if (thread_params->thread && ep_rt_thread_has_started (thread_params->thread)) {
EP_GCX_PREEMP_ENTER
while (sample_profiler_load_profiling_enabled ()) {
// Sample all threads.
ep_rt_sample_profiler_write_sampling_event_for_threads (thread_params->thread, _thread_time_event);
// Wait until it's time to sample again.
ep_rt_thread_sleep (_sampling_rate_in_ns);
}
EP_GCX_PREEMP_EXIT
}
// Signal disable () that the thread has been destroyed.
ep_rt_wait_event_set (&_thread_shutdown_event);
return (ep_rt_thread_start_func_return_t)0;
}
static
void
sample_profiler_set_time_granularity (void)
{
#ifdef HOST_WIN32
// Attempt to set the systems minimum timer period to the sampling rate
// If the sampling rate is lower than the current system setting (16ms by default),
// this will cause the OS to wake more often for scheduling descsion, allowing us to take samples
// Note that is effects a system-wide setting and when set low will increase the amount of time
// the OS is on-CPU, decreasing overall system performance and increasing power consumption
if (_time_begin_period_func != NULL) {
if (((time_period_func)_time_begin_period_func)((uint32_t)(_sampling_rate_in_ns / NUM_NANOSECONDS_IN_1_MS)) == TIMERR_NOERROR) {
_time_period_is_set = true;
}
}
#endif //HOST_WIN32
}
static
void
sample_profiler_reset_time_granularity (void)
{
#ifdef HOST_WIN32
// End the modifications we had to the timer period in enable.
if (_time_end_period_func != NULL) {
if (((time_period_func)_time_end_period_func)((uint32_t)(_sampling_rate_in_ns / NUM_NANOSECONDS_IN_1_MS)) == TIMERR_NOERROR) {
_time_period_is_set = false;
}
}
#endif //HOST_WIN32
}
static
bool
sample_profiler_load_dependencies (void)
{
#ifdef HOST_WIN32
if (_ref_count > 0)
return true; // Already loaded.
#ifdef WszLoadLibrary
_multimedia_library_handle = WszLoadLibrary (L"winmm.dll");
#else
_multimedia_library_handle = LoadLibraryW (L"winmm.dll");
#endif
if (_multimedia_library_handle != NULL) {
_time_begin_period_func = (PVOID)GetProcAddress (_multimedia_library_handle, "timeBeginPeriod");
_time_end_period_func = (PVOID)GetProcAddress (_multimedia_library_handle, "timeEndPeriod");
}
return _multimedia_library_handle != NULL && _time_begin_period_func != NULL && _time_end_period_func != NULL;
#else
return true;
#endif //HOST_WIN32
}
static
void
sample_profiler_unload_dependencies (void)
{
#ifdef HOST_WIN32
if (_multimedia_library_handle != NULL) {
FreeLibrary (_multimedia_library_handle);
_multimedia_library_handle = NULL;
_time_begin_period_func = NULL;
_time_end_period_func = NULL;
}
#endif //HOST_WIN32
}
static
void
sample_profiler_enable (void)
{
EP_ASSERT (_sampling_provider != NULL);
EP_ASSERT (_thread_time_event != NULL);
ep_requires_lock_held ();
if (!sample_profiler_load_profiling_enabled ()) {
sample_profiler_store_profiling_enabled (true);
EP_ASSERT (!ep_rt_wait_event_is_valid (&_thread_shutdown_event));
ep_rt_wait_event_alloc (&_thread_shutdown_event, true, false);
if (!ep_rt_wait_event_is_valid (&_thread_shutdown_event))
EP_UNREACHABLE ("Unable to create sample profiler event.");
ep_rt_thread_id_t thread_id = ep_rt_uint64_t_to_thread_id_t (0);
if (!ep_rt_thread_create ((void *)sampling_thread, NULL, EP_THREAD_TYPE_SAMPLING, &thread_id))
EP_UNREACHABLE ("Unable to create sample profiler thread.");
sample_profiler_set_time_granularity ();
}
}
void
ep_sample_profiler_init (EventPipeProviderCallbackDataQueue *provider_callback_data_queue)
{
ep_requires_lock_held ();
if (!_sampling_provider) {
_sampling_provider = provider_create_register (ep_config_get_sample_profiler_provider_name_utf8 (), NULL, NULL, provider_callback_data_queue);
ep_raise_error_if_nok (_sampling_provider != NULL);
_thread_time_event = provider_add_event (
_sampling_provider,
0, /* eventID */
0, /* keywords */
0, /* eventVersion */
EP_EVENT_LEVEL_INFORMATIONAL,
false /* NeedStack */,
NULL,
0);
ep_raise_error_if_nok (_thread_time_event != NULL);
}
ep_on_exit:
ep_requires_lock_held ();
return;
ep_on_error:
ep_exit_error_handler ();
}
void
ep_sample_profiler_shutdown (void)
{
ep_requires_lock_held ();
EP_ASSERT (_ref_count == 0);
provider_unregister_delete (_sampling_provider);
_sampling_provider = NULL;
_thread_time_event = NULL;
_can_start_sampling = false;
}
void
ep_sample_profiler_enable (void)
{
EP_ASSERT (_sampling_provider != NULL);
EP_ASSERT (_thread_time_event != NULL);
ep_requires_lock_held ();
// Check to see if the sample profiler event is enabled. If it is not, do not spin up the sampling thread.
if (!ep_event_is_enabled (_thread_time_event))
return;
sample_profiler_load_dependencies ();
if (_can_start_sampling)
sample_profiler_enable ();
++_ref_count;
EP_ASSERT (_ref_count > 0);
}
void
ep_sample_profiler_disable (void)
{
EP_ASSERT (_ref_count > 0);
ep_requires_lock_held ();
// Bail early if profiling is not enabled.
if (!sample_profiler_load_profiling_enabled ())
return;
if (_ref_count == 1) {
EP_ASSERT (!ep_rt_process_detach ());
// The sampling thread will watch this value and exit
// when profiling is disabled.
sample_profiler_store_profiling_enabled (false);
// Wait for the sampling thread to clean itself up.
ep_rt_wait_event_wait (&_thread_shutdown_event, EP_INFINITE_WAIT, false);
ep_rt_wait_event_free (&_thread_shutdown_event);
if (_time_period_is_set)
sample_profiler_reset_time_granularity ();
sample_profiler_unload_dependencies ();
}
--_ref_count;
EP_ASSERT (_ref_count >= 0);
}
void
ep_sample_profiler_can_start_sampling (void)
{
ep_requires_lock_held ();
sample_profiler_store_can_start_sampling (true);
if (_ref_count > 0)
sample_profiler_enable ();
}
void
ep_sample_profiler_set_sampling_rate (uint64_t nanoseconds)
{
// If the time period setting was modified by us,
// make sure to change it back before changing our period
// and losing track of what we set it to
if (_time_period_is_set)
sample_profiler_reset_time_granularity ();
_sampling_rate_in_ns = nanoseconds;
if (!_time_period_is_set)
sample_profiler_set_time_granularity ();
}
uint64_t
ep_sample_profiler_get_sampling_rate (void)
{
return _sampling_rate_in_ns;
}
#endif /* !defined(EP_INCLUDE_SOURCE_FILES) || defined(EP_FORCE_INCLUDE_SOURCE_FILES) */
#endif /* ENABLE_PERFTRACING */
#if !defined(ENABLE_PERFTRACING) || (defined(EP_INCLUDE_SOURCE_FILES) && !defined(EP_FORCE_INCLUDE_SOURCE_FILES))
extern const char quiet_linker_empty_file_warning_eventpipe_sample_profiler;
const char quiet_linker_empty_file_warning_eventpipe_sample_profiler = 0;
#endif