File: SysprofAnnotator.h

package info (click to toggle)
webkit2gtk 2.48.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 429,620 kB
  • sloc: cpp: 3,696,936; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,276; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (487 lines) | stat: -rw-r--r-- 17,389 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (C) 2024 Igalia, S.L.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#pragma once

#include <sysprof-capture.h>
#include <wtf/HashMap.h>
#include <wtf/Lock.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/Vector.h>
#include <wtf/text/ASCIILiteral.h>

namespace WTF {

class SysprofAnnotator final {
    WTF_MAKE_NONCOPYABLE(SysprofAnnotator);

    using RawPointerPair = std::pair<const void*, const void*>;
    using TimestampAndString = std::pair<int64_t, Vector<char>>;

public:

    static SysprofAnnotator* singletonIfCreated()
    {
        return s_annotator;
    }

    void instantMark(std::span<const char> name, const char* description, ...) WTF_ATTRIBUTE_PRINTF(3, 4)
    {
        va_list args;
        va_start(args, description);
        sysprof_collector_mark_vprintf(SYSPROF_CAPTURE_CURRENT_TIME, 0, m_processName, name.data(), description, args);
        va_end(args);
    }

    void mark(Seconds timeDelta, std::span<const char> name, const char* description, ...) WTF_ATTRIBUTE_PRINTF(4, 5)
    {
        va_list args;
        va_start(args, description);
        sysprof_collector_mark_vprintf(SYSPROF_CAPTURE_CURRENT_TIME, timeDelta.microsecondsAs<int64_t>(), m_processName, name.data(), description, args);
        va_end(args);
    }

    void beginMark(const void* pointer, std::span<const char> name, const char* description, ...) WTF_ATTRIBUTE_PRINTF(4, 5)
    {
        auto key = std::make_pair(pointer, static_cast<const void*>(name.data()));

        Vector<char> buffer(1024);
        va_list args;
        va_start(args, description);
        vsnprintf(buffer.data(), buffer.size(), description, args);
        va_end(args);

        auto value = std::make_pair(SYSPROF_CAPTURE_CURRENT_TIME, WTFMove(buffer));

        Locker locker { m_lock };
        m_ongoingMarks.set(key, value);
    }

    void endMark(const void* pointer, std::span<const char> name, const char* description, ...) WTF_ATTRIBUTE_PRINTF(4, 5)
    {
        auto key = std::make_pair(pointer, static_cast<const void*>(name.data()));
        std::optional<TimestampAndString> value;

        {
            Locker locker { m_lock };

            TimestampAndString v = m_ongoingMarks.take(key);
            if (v.first)
                value = WTFMove(v);
        }

        if (value) {
            int64_t startTime = std::get<0>(*value);
            const Vector<char>& description = std::get<1>(*value);
            sysprof_collector_mark(startTime, SYSPROF_CAPTURE_CURRENT_TIME - startTime, m_processName, name.data(), description[0] ? description.data() : nullptr);
        } else {
            va_list args;
            va_start(args, description);
            sysprof_collector_mark_vprintf(SYSPROF_CAPTURE_CURRENT_TIME, 0, m_processName, name.data(), description, args);
            va_end(args);
        }
    }

    void tracePoint(TracePointCode code)
    {
        switch (code) {
        case VMEntryScopeStart:
        case WebAssemblyCompileStart:
        case WebAssemblyExecuteStart:
        case DumpJITMemoryStart:
        case FromJSStart:
        case IncrementalSweepStart:
        case MainResourceLoadDidStartProvisional:
        case SubresourceLoadWillStart:
        case FetchCookiesStart:
        case StyleRecalcStart:
        case RenderTreeBuildStart:
        case PerformLayoutStart:
        case PaintLayerStart:
        case AsyncImageDecodeStart:
        case RAFCallbackStart:
        case MemoryPressureHandlerStart:
        case UpdateTouchRegionsStart:
        case DisplayListRecordStart:
        case ComputeEventRegionsStart:
        case RenderingUpdateStart:
        case CompositingUpdateStart:
        case DispatchTouchEventsStart:
        case ParseHTMLStart:
        case DisplayListReplayStart:
        case ScrollingThreadRenderUpdateSyncStart:
        case ScrollingThreadDisplayDidRefreshStart:
        case RenderTreeLayoutStart:
        case PerformOpportunisticallyScheduledTasksStart:
        case WebXRLayerStartFrameStart:
        case WebXRLayerEndFrameStart:
        case WebXRSessionFrameCallbacksStart:
        case WebHTMLViewPaintStart:
        case BackingStoreFlushStart:
        case BuildTransactionStart:
        case WaitForCompositionCompletionStart:
        case RenderLayerTreeStart:
        case FlushPendingLayerChangesStart:
        case LayerFlushStart:
        case SyncMessageStart:
        case SyncTouchEventStart:
        case InitializeWebProcessStart:
        case RenderingUpdateRunLoopObserverStart:
        case LayerTreeFreezeStart:
        case FlushRemoteImageBufferStart:
        case CreateInjectedBundleStart:
        case PaintSnapshotStart:
        case RenderServerSnapshotStart:
        case TakeSnapshotStart:
        case SyntheticMomentumStart:
        case UpdateLayerContentBuffersStart:
        case CommitLayerTreeStart:
        case ProcessLaunchStart:
        case InitializeSandboxStart:
        case WebXRCPFrameWaitStart:
        case WebXRCPFrameStartSubmissionStart:
        case WebXRCPFrameEndSubmissionStart:
        case WakeUpAndApplyDisplayListStart:
            beginMark(nullptr, tracePointCodeName(code).spanIncludingNullTerminator(), "%s", "");
            break;

        case VMEntryScopeEnd:
        case WebAssemblyCompileEnd:
        case WebAssemblyExecuteEnd:
        case DumpJITMemoryStop:
        case FromJSStop:
        case IncrementalSweepEnd:
        case MainResourceLoadDidEnd:
        case SubresourceLoadDidEnd:
        case FetchCookiesEnd:
        case StyleRecalcEnd:
        case RenderTreeBuildEnd:
        case PerformLayoutEnd:
        case PaintLayerEnd:
        case AsyncImageDecodeEnd:
        case RAFCallbackEnd:
        case MemoryPressureHandlerEnd:
        case UpdateTouchRegionsEnd:
        case DisplayListRecordEnd:
        case ComputeEventRegionsEnd:
        case RenderingUpdateEnd:
        case CompositingUpdateEnd:
        case DispatchTouchEventsEnd:
        case ParseHTMLEnd:
        case DisplayListReplayEnd:
        case ScrollingThreadRenderUpdateSyncEnd:
        case ScrollingThreadDisplayDidRefreshEnd:
        case RenderTreeLayoutEnd:
        case PerformOpportunisticallyScheduledTasksEnd:
        case WebXRLayerStartFrameEnd:
        case WebXRLayerEndFrameEnd:
        case WebXRSessionFrameCallbacksEnd:
        case WebHTMLViewPaintEnd:
        case BackingStoreFlushEnd:
        case WaitForCompositionCompletionEnd:
        case RenderLayerTreeEnd:
        case FlushPendingLayerChangesEnd:
        case LayerFlushEnd:
        case BuildTransactionEnd:
        case SyncMessageEnd:
        case SyncTouchEventEnd:
        case InitializeWebProcessEnd:
        case RenderingUpdateRunLoopObserverEnd:
        case LayerTreeFreezeEnd:
        case FlushRemoteImageBufferEnd:
        case CreateInjectedBundleEnd:
        case PaintSnapshotEnd:
        case RenderServerSnapshotEnd:
        case TakeSnapshotEnd:
        case SyntheticMomentumEnd:
        case UpdateLayerContentBuffersEnd:
        case CommitLayerTreeEnd:
        case ProcessLaunchEnd:
        case InitializeSandboxEnd:
        case WebXRCPFrameWaitEnd:
        case WebXRCPFrameStartSubmissionEnd:
        case WebXRCPFrameEndSubmissionEnd:
        case WakeUpAndApplyDisplayListEnd:
            endMark(nullptr, tracePointCodeName(code).spanIncludingNullTerminator(), "%s", "");
            break;

        case DisplayRefreshDispatchingToMainThread:
        case ScheduleRenderingUpdate:
        case TriggerRenderingUpdate:
        case ScrollingTreeDisplayDidRefresh:
        case SyntheticMomentumEvent:
        case RemoteLayerTreeScheduleRenderingUpdate:
        case DisplayLinkUpdate:
            instantMark(tracePointCodeName(code).spanIncludingNullTerminator(), "%s", "");
            break;

        case WTFRange:
        case JavaScriptRange:
        case WebCoreRange:
        case WebKitRange:
        case WebKit2Range:
        case UIProcessRange:
        case GPUProcessRange:
        case GTKWPEPortRange:
            break;
        }
    }

    static void createIfNeeded(ASCIILiteral processName)
    {
        if (!getenv("SYSPROF_CONTROL_FD"))
            return;

        static LazyNeverDestroyed<SysprofAnnotator> instance;
        static std::once_flag onceFlag;
        std::call_once(onceFlag, [&] {
            instance.construct(processName);
        });
    }

private:
    friend class LazyNeverDestroyed<SysprofAnnotator>;

    static ASCIILiteral tracePointCodeName(TracePointCode code)
    {
        switch (code) {
        case VMEntryScopeStart:
        case VMEntryScopeEnd:
            return "VMEntryScope"_s;
        case WebAssemblyCompileStart:
        case WebAssemblyCompileEnd:
            return "WebAssemblyCompile"_s;
        case WebAssemblyExecuteStart:
        case WebAssemblyExecuteEnd:
            return "WebAssemblyExecute"_s;
        case DumpJITMemoryStart:
        case DumpJITMemoryStop:
            return "DumpJITMemory"_s;
        case FromJSStart:
        case FromJSStop:
            return "FromJS"_s;
        case IncrementalSweepStart:
        case IncrementalSweepEnd:
            return "IncrementalSweep"_s;

        case MainResourceLoadDidStartProvisional:
        case MainResourceLoadDidEnd:
            return "MainResourceLoad"_s;
        case SubresourceLoadWillStart:
        case SubresourceLoadDidEnd:
            return "SubresourceLoad"_s;
        case FetchCookiesStart:
        case FetchCookiesEnd:
            return "FetchCookies"_s;
        case StyleRecalcStart:
        case StyleRecalcEnd:
            return "StyleRecalc"_s;
        case RenderTreeBuildStart:
        case RenderTreeBuildEnd:
            return "RenderTreeBuild"_s;
        case PerformLayoutStart:
        case PerformLayoutEnd:
            return "PerformLayout"_s;
        case PaintLayerStart:
        case PaintLayerEnd:
            return "PaintLayer"_s;
        case AsyncImageDecodeStart:
        case AsyncImageDecodeEnd:
            return "AsyncImageDecode"_s;
        case RAFCallbackStart:
        case RAFCallbackEnd:
            return "RAFCallback"_s;
        case MemoryPressureHandlerStart:
        case MemoryPressureHandlerEnd:
            return "MemoryPressureHandler"_s;
        case UpdateTouchRegionsStart:
        case UpdateTouchRegionsEnd:
            return "UpdateTouchRegions"_s;
        case DisplayListRecordStart:
        case DisplayListRecordEnd:
            return "DisplayListRecord"_s;
        case DisplayRefreshDispatchingToMainThread:
            return "DisplayRefreshDispatchingToMainThread"_s;
        case ComputeEventRegionsStart:
        case ComputeEventRegionsEnd:
            return "ComputeEventRegions"_s;
        case ScheduleRenderingUpdate:
            return "ScheduleRenderingUpdate"_s;
        case TriggerRenderingUpdate:
            return "TriggerRenderingUpdate"_s;
        case RenderingUpdateStart:
        case RenderingUpdateEnd:
            return "RenderingUpdate"_s;
        case CompositingUpdateStart:
        case CompositingUpdateEnd:
            return "CompositingUpdate"_s;
        case DispatchTouchEventsStart:
        case DispatchTouchEventsEnd:
            return "DispatchTouchEvents"_s;
        case ParseHTMLStart:
        case ParseHTMLEnd:
            return "ParseHTML"_s;
        case DisplayListReplayStart:
        case DisplayListReplayEnd:
            return "DisplayListReplay"_s;
        case ScrollingThreadRenderUpdateSyncStart:
        case ScrollingThreadRenderUpdateSyncEnd:
            return "ScrollingThreadRenderUpdateSync"_s;
        case ScrollingThreadDisplayDidRefreshStart:
        case ScrollingThreadDisplayDidRefreshEnd:
            return "ScrollingThreadDisplayDidRefresh"_s;
        case ScrollingTreeDisplayDidRefresh:
            return "ScrollingTreeDisplayDidRefresh"_s;
        case RenderTreeLayoutStart:
        case RenderTreeLayoutEnd:
            return "RenderTreeLayout"_s;
        case PerformOpportunisticallyScheduledTasksStart:
        case PerformOpportunisticallyScheduledTasksEnd:
            return "PerformOpportunisticallyScheduledTasks"_s;
        case WebXRLayerStartFrameStart:
        case WebXRLayerStartFrameEnd:
            return "WebXRLayerStartFrame"_s;
        case WebXRLayerEndFrameStart:
        case WebXRLayerEndFrameEnd:
            return "WebXRLayerEndFrame"_s;
        case WebXRSessionFrameCallbacksStart:
        case WebXRSessionFrameCallbacksEnd:
            return "WebXRSessionFrameCallbacks"_s;

        case WebHTMLViewPaintStart:
        case WebHTMLViewPaintEnd:
            return "WebHTMLViewPaint"_s;

        case BackingStoreFlushStart:
        case BackingStoreFlushEnd:
            return "BackingStoreFlush"_s;
        case BuildTransactionStart:
        case BuildTransactionEnd:
            return "BuildTransaction"_s;
        case SyncMessageStart:
        case SyncMessageEnd:
            return "SyncMessage"_s;
        case SyncTouchEventStart:
        case SyncTouchEventEnd:
            return "SyncTouchEvent"_s;
        case InitializeWebProcessStart:
        case InitializeWebProcessEnd:
            return "InitializeWebProcess"_s;
        case RenderingUpdateRunLoopObserverStart:
        case RenderingUpdateRunLoopObserverEnd:
            return "RenderingUpdateRunLoopObserver"_s;
        case LayerTreeFreezeStart:
        case LayerTreeFreezeEnd:
            return "LayerTreeFreeze"_s;
        case FlushRemoteImageBufferStart:
        case FlushRemoteImageBufferEnd:
            return "FlushRemoteImageBuffer"_s;
        case CreateInjectedBundleStart:
        case CreateInjectedBundleEnd:
            return "CreateInjectedBundle"_s;
        case PaintSnapshotStart:
        case PaintSnapshotEnd:
            return "PaintSnapshot"_s;
        case RenderServerSnapshotStart:
        case RenderServerSnapshotEnd:
            return "RenderServerSnapshot"_s;
        case TakeSnapshotStart:
        case TakeSnapshotEnd:
            return "TakeSnapshot"_s;
        case SyntheticMomentumStart:
        case SyntheticMomentumEnd:
            return "SyntheticMomentum"_s;
        case SyntheticMomentumEvent:
            return "SyntheticMomentumEvent"_s;
        case RemoteLayerTreeScheduleRenderingUpdate:
            return "RemoteLayerTreeScheduleRenderingUpdate"_s;
        case DisplayLinkUpdate:
            return "DisplayLinkUpdate"_s;
        case UpdateLayerContentBuffersStart:
        case UpdateLayerContentBuffersEnd:
            return "UpdateLayerContentBuffers"_s;

        case CommitLayerTreeStart:
        case CommitLayerTreeEnd:
            return "CommitLayerTree"_s;
        case ProcessLaunchStart:
        case ProcessLaunchEnd:
            return "ProcessLaunch"_s;
        case InitializeSandboxStart:
        case InitializeSandboxEnd:
            return "InitializeSandbox"_s;
        case WebXRCPFrameWaitStart:
        case WebXRCPFrameWaitEnd:
            return "WebXRCPFrameWait"_s;
        case WebXRCPFrameStartSubmissionStart:
        case WebXRCPFrameStartSubmissionEnd:
            return "WebXRCPFrameStartSubmission"_s;
        case WebXRCPFrameEndSubmissionStart:
        case WebXRCPFrameEndSubmissionEnd:
            return "WebXRCPFrameEndSubmission"_s;

        case WakeUpAndApplyDisplayListStart:
        case WakeUpAndApplyDisplayListEnd:
            return "WakeUpAndApplyDisplayList"_s;

        case FlushPendingLayerChangesStart:
        case FlushPendingLayerChangesEnd:
            return "FlushPendingLayerChanges"_s;
        case WaitForCompositionCompletionStart:
        case WaitForCompositionCompletionEnd:
            return "WaitForCompositionCompletion"_s;
        case RenderLayerTreeStart:
        case RenderLayerTreeEnd:
            return "RenderLayerTree"_s;
        case LayerFlushStart:
        case LayerFlushEnd:
            return "LayerFlush"_s;

        case WTFRange:
        case JavaScriptRange:
        case WebCoreRange:
        case WebKitRange:
        case WebKit2Range:
        case UIProcessRange:
        case GPUProcessRange:
        case GTKWPEPortRange:
            return nullptr;
        }

        RELEASE_ASSERT_NOT_REACHED();
    }

    explicit SysprofAnnotator(ASCIILiteral processName)
        : m_processName(processName)
    {
        sysprof_collector_init();
        s_annotator = this;
    }

    ASCIILiteral m_processName;
    Lock m_lock;
    UncheckedKeyHashMap<RawPointerPair, TimestampAndString> m_ongoingMarks WTF_GUARDED_BY_LOCK(m_lock);
    static SysprofAnnotator* s_annotator;
};

inline SysprofAnnotator* SysprofAnnotator::s_annotator;

} // namespace WTF

using WTF::SysprofAnnotator;