summaryrefslogtreecommitdiff
path: root/src/vm/sampleprofiler.cpp
blob: 563139897addf51b55c0cef8923f9acc31611c8b (plain)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#include "common.h"
#include "eventpipebuffermanager.h"
#include "eventpipeeventinstance.h"
#include "sampleprofiler.h"
#include "hosting.h"
#include "threadsuspend.h"

#ifdef FEATURE_PERFTRACING

#ifndef FEATURE_PAL
#include <mmsystem.h>
#endif //FEATURE_PAL

const unsigned long NUM_NANOSECONDS_IN_1_MS = 1000000;

Volatile<BOOL> SampleProfiler::s_profilingEnabled = false;
Thread *SampleProfiler::s_pSamplingThread = NULL;
const WCHAR *SampleProfiler::s_providerName = W("Microsoft-DotNETCore-SampleProfiler");
EventPipeProvider *SampleProfiler::s_pEventPipeProvider = NULL;
EventPipeEvent *SampleProfiler::s_pThreadTimeEvent = NULL;
SampleProfiler::SampleProfilerPayload SampleProfiler::s_ExternalPayload = {SampleProfilerSampleType::External};
SampleProfiler::SampleProfilerPayload SampleProfiler::s_ManagedPayload = {SampleProfilerSampleType::Managed};
CLREventStatic SampleProfiler::s_threadShutdownEvent;
unsigned long SampleProfiler::s_samplingRateInNs = NUM_NANOSECONDS_IN_1_MS; // 1ms
bool SampleProfiler::s_timePeriodIsSet = FALSE;
int32_t SampleProfiler::s_RefCount = 0;

#ifndef FEATURE_PAL
PVOID SampleProfiler::s_timeBeginPeriodFn = NULL;
PVOID SampleProfiler::s_timeEndPeriodFn = NULL;
HINSTANCE SampleProfiler::s_hMultimediaLib = NULL;

typedef MMRESULT(WINAPI *TimePeriodFnPtr)(UINT uPeriod);
#endif //FEATURE_PAL

void SampleProfiler::Initialize(EventPipeProviderCallbackDataQueue* pEventPipeProviderCallbackDataQueue)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        PRECONDITION(EventPipe::IsLockOwnedByCurrentThread());
    }
    CONTRACTL_END;

    if (s_pEventPipeProvider == nullptr)
    {
        s_pEventPipeProvider = EventPipe::CreateProvider(SL(s_providerName), nullptr, nullptr, pEventPipeProviderCallbackDataQueue);
        s_pThreadTimeEvent = s_pEventPipeProvider->AddEvent(
            0, /* eventID */
            0, /* keywords */
            0, /* eventVersion */
            EventPipeEventLevel::Informational,
            false /* NeedStack */);
    }
}

void SampleProfiler::Enable(EventPipeProviderCallbackDataQueue *pEventPipeProviderCallbackDataQueue)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        PRECONDITION(s_pEventPipeProvider != nullptr);
        PRECONDITION(s_pThreadTimeEvent != nullptr);
        PRECONDITION((s_pSamplingThread == NULL && s_RefCount == 0) || ((s_pSamplingThread != NULL && s_RefCount > 0)));
        // Synchronization of multiple callers occurs in EventPipe::Enable.
        PRECONDITION(EventPipe::IsLockOwnedByCurrentThread());
    }
    CONTRACTL_END;

    const bool fSuccess = LoadDependencies();

#ifndef FEATURE_PAL
    _ASSERTE(fSuccess);
    // TODO: Stress log on failure?
#else
    _ASSERTE(!fSuccess);
#endif

    // Check to see if the sample profiler event is enabled.  If it is not, do not spin up the sampling thread.
    if (!s_pThreadTimeEvent->IsEnabled())
        return;

    if (!s_profilingEnabled)
    {
        s_profilingEnabled = true;
        s_pSamplingThread = SetupUnstartedThread();
        if (s_pSamplingThread->CreateNewThread(0, ThreadProc, NULL))
        {
            // Start the sampling thread.
            s_pSamplingThread->SetBackground(TRUE);
            s_pSamplingThread->StartThread();
        }
        else
        {
            _ASSERT(!"Unable to create sample profiler thread.");
        }

        s_threadShutdownEvent.CreateManualEvent(FALSE);

        SetTimeGranularity();
    }
    ++s_RefCount;
}

void SampleProfiler::Disable()
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        // Synchronization of multiple callers occurs in EventPipe::Disable.
        PRECONDITION(EventPipe::IsLockOwnedByCurrentThread());
        PRECONDITION(s_RefCount >= 0);
    }
    CONTRACTL_END;

    // Bail early if profiling is not enabled.
    if (!s_profilingEnabled)
        return;

    if (s_RefCount == 1)
    {
        _ASSERTE(!g_fProcessDetach);

        // The sampling thread will watch this value and exit
        // when profiling is disabled.
        s_profilingEnabled = false;

        // Wait for the sampling thread to clean itself up.
        s_threadShutdownEvent.Wait(INFINITE, FALSE /* bAlertable */);
        s_threadShutdownEvent.CloseEvent();

        if (s_timePeriodIsSet)
            ResetTimeGranularity();
        UnloadDependencies();
    }
    --s_RefCount;
}

void SampleProfiler::SetSamplingRate(unsigned long nanoseconds)
{
    LIMITED_METHOD_CONTRACT;

    // 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 (s_timePeriodIsSet)
    {
        ResetTimeGranularity();
    }

    s_samplingRateInNs = nanoseconds;

    if (!s_timePeriodIsSet)
    {
        SetTimeGranularity();
    }
}

DWORD WINAPI SampleProfiler::ThreadProc(void *args)
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
        MODE_PREEMPTIVE;
        PRECONDITION(s_pSamplingThread != NULL);
    }
    CONTRACTL_END;

    // Complete thread initialization and start the profiling loop.
    if (s_pSamplingThread->HasStarted())
    {
        // Switch to pre-emptive mode so that this thread doesn't starve the GC.
        GCX_PREEMP();

        while (s_profilingEnabled)
        {
            // Check to see if we can suspend managed execution.
            if (ThreadSuspend::SysIsSuspendInProgress() || (ThreadSuspend::GetSuspensionThread() != 0))
            {
                // Skip the current sample.
                PlatformSleep(s_samplingRateInNs);
                continue;
            }

            // Actually suspend managed execution.
            ThreadSuspend::SuspendEE(ThreadSuspend::SUSPEND_REASON::SUSPEND_OTHER);

            // Walk all managed threads and capture stacks.
            WalkManagedThreads();

            // Resume managed execution.
            ThreadSuspend::RestartEE(FALSE /* bFinishedGC */, TRUE /* SuspendSucceeded */);

            // Wait until it's time to sample again.
            PlatformSleep(s_samplingRateInNs);
        }
    }

    // Destroy the sampling thread when it is done running.
    DestroyThread(s_pSamplingThread);
    s_pSamplingThread = NULL;

    // Signal Disable() that the thread has been destroyed.
    s_threadShutdownEvent.Set();

    return S_OK;
}

// The thread store lock must already be held by the thread before this function
// is called.  ThreadSuspend::SuspendEE acquires the thread store lock.
void SampleProfiler::WalkManagedThreads()
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
        MODE_PREEMPTIVE;
    }
    CONTRACTL_END;

    Thread *pTargetThread = NULL;

    // Iterate over all managed threads.
    // Assumes that the ThreadStoreLock is held because we've suspended all threads.
    while ((pTargetThread = ThreadStore::GetThreadList(pTargetThread)) != NULL)
    {
        StackContents stackContents;

        // Walk the stack and write it out as an event.
        if (EventPipe::WalkManagedStackForThread(pTargetThread, stackContents) && !stackContents.IsEmpty())
        {
            // Set the payload.  If the GC mode on suspension > 0, then the thread was in cooperative mode.
            // Even though there are some cases where this is not managed code, we assume it is managed code here.
            // If the GC mode on suspension == 0 then the thread was in preemptive mode, which we qualify as external here.

            // Write the sample.
            EventPipe::WriteSampleProfileEvent(
                s_pSamplingThread,
                s_pThreadTimeEvent,
                pTargetThread,
                stackContents,
                pTargetThread->GetGCModeOnSuspension() ? s_ManagedPayload.Rawdata : s_ExternalPayload.Rawdata,
                c_payloadSize);
        }

        // Reset the GC mode.
        pTargetThread->ClearGCModeOnSuspension();
    }
}

void SampleProfiler::PlatformSleep(unsigned long nanoseconds)
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
        MODE_ANY;
    }
    CONTRACTL_END;

#ifdef FEATURE_PAL
    PAL_nanosleep(nanoseconds);
#else  //FEATURE_PAL
    ClrSleepEx(s_samplingRateInNs / NUM_NANOSECONDS_IN_1_MS, FALSE);
#endif //FEATURE_PAL
}

void SampleProfiler::SetTimeGranularity()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

#ifndef FEATURE_PAL
    // 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 (s_timeBeginPeriodFn != NULL)
    {
        if (((TimePeriodFnPtr)s_timeBeginPeriodFn)(s_samplingRateInNs / NUM_NANOSECONDS_IN_1_MS) == TIMERR_NOERROR)
        {
            s_timePeriodIsSet = TRUE;
        }
    }
#endif //FEATURE_PAL
}

void SampleProfiler::ResetTimeGranularity()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

#ifndef FEATURE_PAL
    // End the modifications we had to the timer period in Enable
    if (s_timeEndPeriodFn != NULL)
    {
        if (((TimePeriodFnPtr)s_timeEndPeriodFn)(s_samplingRateInNs / NUM_NANOSECONDS_IN_1_MS) == TIMERR_NOERROR)
        {
            s_timePeriodIsSet = FALSE;
        }
    }
#endif //FEATURE_PAL
}

bool SampleProfiler::LoadDependencies()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

#ifndef FEATURE_PAL
    if (s_RefCount > 0)
        return true; // Already loaded.

    s_hMultimediaLib = WszLoadLibrary(W("winmm.dll"));

    if (s_hMultimediaLib != NULL)
    {
        s_timeBeginPeriodFn = (PVOID)GetProcAddress(s_hMultimediaLib, "timeBeginPeriod");
        s_timeEndPeriodFn = (PVOID)GetProcAddress(s_hMultimediaLib, "timeEndPeriod");
    }

    return s_hMultimediaLib != NULL && s_timeBeginPeriodFn != NULL && s_timeEndPeriodFn != NULL;
#else
    return FALSE;
#endif //FEATURE_PAL
}

void SampleProfiler::UnloadDependencies()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

#ifndef FEATURE_PAL
    if (s_hMultimediaLib != NULL)
    {
        FreeLibrary(s_hMultimediaLib);
        s_hMultimediaLib = NULL;
        s_timeBeginPeriodFn = NULL;
        s_timeEndPeriodFn = NULL;
    }
#endif //FEATURE_PAL
}

#endif // FEATURE_PERFTRACING