summaryrefslogtreecommitdiff
path: root/src/vm/eventpipeconfiguration.cpp
blob: 412c447b261de589e86eaed083692f95933ca574 (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// 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 "eventpipe.h"
#include "eventpipeconfiguration.h"
#include "eventpipeeventinstance.h"
#include "eventpipesessionprovider.h"
#include "eventpipeprovider.h"
#include "eventpipesession.h"

#ifdef FEATURE_PERFTRACING

const WCHAR *EventPipeConfiguration::s_configurationProviderName = W("Microsoft-DotNETCore-EventPipeConfiguration");

void EventPipeConfiguration::Initialize()
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_PREEMPTIVE;
        PRECONDITION(m_pProviderList == nullptr);
        PRECONDITION(m_pConfigProvider == nullptr);
        PRECONDITION(m_pMetadataEvent == nullptr);
    }
    CONTRACTL_END;

    m_pProviderList = new SList<SListElem<EventPipeProvider *>>();

    EventPipe::RunWithCallbackPostponed([&](EventPipeProviderCallbackDataQueue *pEventPipeProviderCallbackDataQueue) {
        // Create the configuration provider.
        m_pConfigProvider = CreateProvider(SL(s_configurationProviderName), NULL, NULL, pEventPipeProviderCallbackDataQueue);
    });

    // Create the metadata event.
    m_pMetadataEvent = m_pConfigProvider->AddEvent(
        0, /* eventID */
        0, /* keywords */
        0, /* eventVersion */
        EventPipeEventLevel::LogAlways,
        false); /* needStack */
}

void EventPipeConfiguration::Shutdown()
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
        MODE_ANY;
    }
    CONTRACTL_END;

    if (m_pConfigProvider != nullptr)
    {
        // This unregisters the provider, which takes a
        // HOST_BREAKABLE lock
        EX_TRY
        {
            DeleteProvider(m_pConfigProvider);
        }
        EX_CATCH {}
        EX_END_CATCH(SwallowAllExceptions);

        m_pConfigProvider = nullptr;
    }

    if (m_pProviderList != nullptr)
    {
        // We swallow exceptions here because the HOST_BREAKABLE
        // lock may throw and this destructor gets called in throw
        // intolerant places. If that happens the provider list will leak
        EX_TRY
        {
            // Take the lock before manipulating the list.
            CrstHolder _crst(EventPipe::GetLock());

            SListElem<EventPipeProvider *> *pElem = m_pProviderList->GetHead();
            while (pElem != nullptr)
            {
                // We don't delete provider itself because it can be in-use
                SListElem<EventPipeProvider *> *pCurElem = pElem;
                pElem = m_pProviderList->GetNext(pElem);
                delete pCurElem;
            }

            delete m_pProviderList;
        }
        EX_CATCH {}
        EX_END_CATCH(SwallowAllExceptions);

        m_pProviderList = nullptr;
    }
}

EventPipeProvider *EventPipeConfiguration::CreateProvider(const SString &providerName, EventPipeCallback pCallbackFunction, void *pCallbackData, EventPipeProviderCallbackDataQueue *pEventPipeProviderCallbackDataQueue)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_PREEMPTIVE;
        PRECONDITION(EventPipe::IsLockOwnedByCurrentThread());
    }
    CONTRACTL_END;

    // Allocate a new provider.
    EventPipeProvider *pProvider = new EventPipeProvider(this, providerName, pCallbackFunction, pCallbackData);

    // Register the provider with the configuration system.
    RegisterProvider(*pProvider, pEventPipeProviderCallbackDataQueue);

    return pProvider;
}

void EventPipeConfiguration::DeleteProvider(EventPipeProvider *pProvider)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        PRECONDITION(pProvider != NULL);
    }
    CONTRACTL_END;

    if (pProvider == NULL)
        return;

    // Unregister the provider.
    UnregisterProvider(*pProvider);

    // Free the provider itself.
    delete pProvider;
}

bool EventPipeConfiguration::RegisterProvider(EventPipeProvider &provider, EventPipeProviderCallbackDataQueue *pEventPipeProviderCallbackDataQueue)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_PREEMPTIVE;
        PRECONDITION(EventPipe::IsLockOwnedByCurrentThread());
    }
    CONTRACTL_END;

    // See if we've already registered this provider.
    EventPipeProvider *pExistingProvider = GetProviderNoLock(provider.GetProviderName());
    if (pExistingProvider != nullptr)
        return false;

    // The provider list should be non-NULL, but can be NULL on shutdown.
    if (m_pProviderList != nullptr)
    {
        // The provider has not been registered, so register it.
        m_pProviderList->InsertTail(new SListElem<EventPipeProvider *>(&provider));
    }

    INT64 keywordForAllSessions;
    EventPipeEventLevel levelForAllSessions;
    ComputeKeywordAndLevel(provider, /* out */ keywordForAllSessions, /* out */ levelForAllSessions);

    EventPipe::ForEachSession([&](EventPipeSession &session) {
        // Set the provider configuration and enable it if it has been requested by a session.
        EventPipeSessionProvider *pSessionProvider = GetSessionProvider(session, &provider);
        if (pSessionProvider == nullptr)
            return;

        EventPipeProviderCallbackData eventPipeProviderCallbackData = provider.SetConfiguration(
            keywordForAllSessions,
            levelForAllSessions,
            session.GetMask(),
            pSessionProvider->GetKeywords(),
            pSessionProvider->GetLevel(),
            pSessionProvider->GetFilterData());
        pEventPipeProviderCallbackDataQueue->Enqueue(&eventPipeProviderCallbackData);
    });

    return true;
}

bool EventPipeConfiguration::UnregisterProvider(EventPipeProvider &provider)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
    }
    CONTRACTL_END;

    // Take the lock before manipulating the provider list.
    CrstHolder _crst(EventPipe::GetLock());

    // The provider list should be non-NULL, but can be NULL on shutdown.
    if (m_pProviderList != NULL)
    {
        // Find the provider.
        SListElem<EventPipeProvider *> *pElem = m_pProviderList->GetHead();
        while (pElem != NULL)
        {
            if (pElem->GetValue() == &provider)
            {
                break;
            }

            pElem = m_pProviderList->GetNext(pElem);
        }

        // If we found the provider, remove it.
        if (pElem != NULL)
        {
            if (m_pProviderList->FindAndRemove(pElem) != NULL)
            {
                delete pElem;
                return true;
            }
        }
    }

    return false;
}

EventPipeProvider *EventPipeConfiguration::GetProvider(const SString &providerName)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

    // Take the lock before touching the provider list to ensure no one tries to
    // modify the list.
    CrstHolder _crst(EventPipe::GetLock());

    return GetProviderNoLock(providerName);
}

EventPipeProvider *EventPipeConfiguration::GetProviderNoLock(const SString &providerName)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(EventPipe::IsLockOwnedByCurrentThread());
    }
    CONTRACTL_END;

    // The provider list should be non-NULL, but can be NULL on shutdown.
    if (m_pProviderList != NULL)
    {
        SListElem<EventPipeProvider *> *pElem = m_pProviderList->GetHead();
        while (pElem != NULL)
        {
            EventPipeProvider *pProvider = pElem->GetValue();
            if (pProvider->GetProviderName().Equals(providerName))
            {
                return pProvider;
            }

            pElem = m_pProviderList->GetNext(pElem);
        }
    }

    return NULL;
}

EventPipeSessionProvider *EventPipeConfiguration::GetSessionProvider(const EventPipeSession &session, const EventPipeProvider *pProvider) const
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_PREEMPTIVE;
        PRECONDITION(EventPipe::IsLockOwnedByCurrentThread());
    }
    CONTRACTL_END;

    return session.GetSessionProvider(pProvider);
}

void EventPipeConfiguration::ComputeKeywordAndLevel(const EventPipeProvider& provider, INT64& keywordForAllSessions, EventPipeEventLevel& levelForAllSessions) const
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_PREEMPTIVE;
        PRECONDITION(EventPipe::IsLockOwnedByCurrentThread());
    }
    CONTRACTL_END;
    keywordForAllSessions = 0;
    levelForAllSessions = EventPipeEventLevel::LogAlways;
    EventPipe::ForEachSession([&](EventPipeSession &session) {
        EventPipeSessionProvider *pSessionProvider = GetSessionProvider(session, &provider);
        if (pSessionProvider != nullptr)
        {
            INT64 sessionKeyword = pSessionProvider->GetKeywords();
            EventPipeEventLevel sessionLevel = pSessionProvider->GetLevel();
            keywordForAllSessions = keywordForAllSessions | sessionKeyword;
            levelForAllSessions = (sessionLevel > levelForAllSessions) ? sessionLevel : levelForAllSessions;
        }
    });
}

INT64 EventPipeConfiguration::ComputeEventEnabledMask(const EventPipeProvider& provider, INT64 keywords, EventPipeEventLevel eventLevel) const
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_PREEMPTIVE;
        PRECONDITION(EventPipe::IsLockOwnedByCurrentThread());
    }
    CONTRACTL_END;
    INT64 result = 0;
    EventPipe::ForEachSession([&](EventPipeSession &session) {
        EventPipeSessionProvider *pSessionProvider = GetSessionProvider(session, &provider);
        if (pSessionProvider != nullptr)
        {
            INT64 sessionKeyword = pSessionProvider->GetKeywords();
            EventPipeEventLevel sessionLevel = pSessionProvider->GetLevel();
            // The event is enabled if:
            //  - The provider is enabled.
            //  - The event keywords are unspecified in the manifest (== 0) or when masked with the enabled config are != 0.
            //  - The event level is LogAlways or the provider's verbosity level is set to greater than the event's verbosity level in the manifest.
            bool providerEnabled = provider.Enabled();
            bool keywordEnabled = (keywords == 0) || ((sessionKeyword & keywords) != 0);
            bool levelEnabled = ((eventLevel == EventPipeEventLevel::LogAlways) || (sessionLevel >= eventLevel));
            if (providerEnabled && keywordEnabled && levelEnabled)
            {
                result = result | session.GetMask();
            }
        }
    });
    return result;
}

void EventPipeConfiguration::Enable(EventPipeSession &session, EventPipeProviderCallbackDataQueue *pEventPipeProviderCallbackDataQueue)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_PREEMPTIVE;
        PRECONDITION(EventPipe::IsLockOwnedByCurrentThread());
    }
    CONTRACTL_END;

    // The provider list should be non-NULL, but can be NULL on shutdown.
    if (m_pProviderList != NULL)
    {
        SListElem<EventPipeProvider *> *pElem = m_pProviderList->GetHead();
        while (pElem != NULL)
        {
            EventPipeProvider *pProvider = pElem->GetValue();

            // Enable the provider if it has been configured.
            EventPipeSessionProvider *pSessionProvider = GetSessionProvider(session, pProvider);
            if (pSessionProvider != NULL)
            {
                INT64 keywordForAllSessions;
                EventPipeEventLevel levelForAllSessions;
                ComputeKeywordAndLevel(*pProvider, /* out */ keywordForAllSessions, /* out */ levelForAllSessions);

                EventPipeProviderCallbackData eventPipeProviderCallbackData =
                    pProvider->SetConfiguration(
                        keywordForAllSessions,
                        levelForAllSessions,
                        session.GetMask(),
                        pSessionProvider->GetKeywords(),
                        pSessionProvider->GetLevel(),
                        pSessionProvider->GetFilterData());
                pEventPipeProviderCallbackDataQueue->Enqueue(&eventPipeProviderCallbackData);
            }

            pElem = m_pProviderList->GetNext(pElem);
        }
    }
}

void EventPipeConfiguration::Disable(const EventPipeSession &session, EventPipeProviderCallbackDataQueue *pEventPipeProviderCallbackDataQueue)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_PREEMPTIVE;
        PRECONDITION(EventPipe::IsLockOwnedByCurrentThread());
    }
    CONTRACTL_END;

    // The provider list should be non-NULL, but can be NULL on shutdown.
    if (m_pProviderList != NULL)
    {
        SListElem<EventPipeProvider *> *pElem = m_pProviderList->GetHead();
        while (pElem != NULL)
        {
            EventPipeProvider *pProvider = pElem->GetValue();
            if (pProvider->IsEnabled(session.GetMask()))
            {
                EventPipeSessionProvider *pSessionProvider = GetSessionProvider(session, pProvider);
                if (pSessionProvider != nullptr)
                {
                    INT64 keywordForAllSessions;
                    EventPipeEventLevel levelForAllSessions;
                    ComputeKeywordAndLevel(*pProvider, /* out */ keywordForAllSessions, /* out */ levelForAllSessions);

                    EventPipeProviderCallbackData eventPipeProviderCallbackData = pProvider->UnsetConfiguration(
                        keywordForAllSessions,
                        levelForAllSessions,
                        session.GetMask(),
                        pSessionProvider->GetKeywords(),
                        pSessionProvider->GetLevel(),
                        pSessionProvider->GetFilterData());
                    pEventPipeProviderCallbackDataQueue->Enqueue(&eventPipeProviderCallbackData);
                }
            }

            pElem = m_pProviderList->GetNext(pElem);
        }
    }
}

EventPipeEventInstance *EventPipeConfiguration::BuildEventMetadataEvent(EventPipeEventInstance &sourceInstance, unsigned int metadataId)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

    // The payload of the event should contain:
    // - Metadata ID
    // - GUID ProviderID.
    // - Optional event description payload.

    // Calculate the size of the event.
    EventPipeEvent &sourceEvent = *sourceInstance.GetEvent();
    const SString &providerName = sourceEvent.GetProvider()->GetProviderName();
    BYTE *pPayloadData = sourceEvent.GetMetadata();
    unsigned int payloadLength = sourceEvent.GetMetadataLength();
    unsigned int providerNameLength = (providerName.GetCount() + 1) * sizeof(WCHAR);
    unsigned int instancePayloadSize = sizeof(metadataId) + providerNameLength + payloadLength;

    // Allocate the payload.
    BYTE *pInstancePayload = new BYTE[instancePayloadSize];

    // Fill the buffer with the payload.
    BYTE *currentPtr = pInstancePayload;

    memcpy(currentPtr, &metadataId, sizeof(metadataId));
    currentPtr += sizeof(metadataId);

    memcpy(currentPtr, (BYTE *)providerName.GetUnicode(), providerNameLength);
    currentPtr += providerNameLength;

    // Write the incoming payload data.
    memcpy(currentPtr, pPayloadData, payloadLength);

    // Construct the event instance.
    EventPipeEventInstance *pInstance = new EventPipeEventInstance(
        *m_pMetadataEvent,
        EventPipe::GetCurrentProcessorNumber(),
#ifdef FEATURE_PAL
        PAL_GetCurrentOSThreadId(),
#else
        GetCurrentThreadId(),
#endif
        pInstancePayload,
        instancePayloadSize,
        NULL /* pActivityId */,
        NULL /* pRelatedActivityId */);
    _ASSERTE(!m_pMetadataEvent->NeedStack());

    // Set the timestamp to match the source event, because the metadata event
    // will be emitted right before the source event.
    pInstance->SetTimeStamp(*sourceInstance.GetTimeStamp());

    return pInstance;
}

void EventPipeConfiguration::DeleteDeferredProviders()
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_PREEMPTIVE;
        // Lock must be held by EventPipe::Disable.
        PRECONDITION(EventPipe::IsLockOwnedByCurrentThread());
    }
    CONTRACTL_END;

    // The provider list should be non-NULL, but can be NULL on shutdown.
    if (m_pProviderList != NULL)
    {
        SListElem<EventPipeProvider *> *pElem = m_pProviderList->GetHead();
        while (pElem != NULL)
        {
            EventPipeProvider *pProvider = pElem->GetValue();
            pElem = m_pProviderList->GetNext(pElem);
            if (pProvider->GetDeleteDeferred())
            {
                DeleteProvider(pProvider);
            }
        }
    }
}
#endif // FEATURE_PERFTRACING