tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
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
/****************************************************************************
* Copyright (C) 2014-2015 Intel Corporation.   All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
****************************************************************************/
 
#pragma once
#include "knobs.h"
 
#include "common/os.h"
#include "common/rdtsc_buckets.h"
 
#include <vector>
 
enum CORE_BUCKETS
{
    APIClearRenderTarget,
    APIDraw,
    APIDrawWakeAllThreads,
    APIDrawIndexed,
    APIDispatch,
    APIStoreTiles,
    APIGetDrawContext,
    APISync,
    APIWaitForIdle,
    FEProcessDraw,
    FEProcessDrawIndexed,
    FEFetchShader,
    FEVertexShader,
    FEHullShader,
    FETessellation,
    FEDomainShader,
    FEGeometryShader,
    FEStreamout,
    FEPAAssemble,
    FEBinPoints,
    FEBinLines,
    FEBinTriangles,
    FETriangleSetup,
    FEViewportCull,
    FEGuardbandClip,
    FEClipPoints,
    FEClipLines,
    FEClipTriangles,
    FECullZeroAreaAndBackface,
    FECullBetweenCenters,
    FEEarlyRastEnter,
    FEEarlyRastExit,
    FEProcessStoreTiles,
    FEProcessInvalidateTiles,
    WorkerWorkOnFifoBE,
    WorkerFoundWork,
    BELoadTiles,
    BEDispatch,
    BEClear,
    BERasterizeLine,
    BERasterizeTriangle,
    BETriangleSetup,
    BEStepSetup,
    BECullZeroArea,
    BEEmptyTriangle,
    BETrivialAccept,
    BETrivialReject,
    BERasterizePartial,
    BEPixelBackend,
    BESetup,
    BEBarycentric,
    BEEarlyDepthTest,
    BEPixelShader,
    BESingleSampleBackend,
    BEPixelRateBackend,
    BESampleRateBackend,
    BENullBackend,
    BELateDepthTest,
    BEOutputMerger,
    BEStoreTiles,
    BEEndTile,
 
    NumBuckets
};
 
void rdtscReset();
void rdtscInit(int threadId);
void rdtscStart(uint32_t bucketId);
void rdtscStop(uint32_t bucketId, uint32_t count, uint64_t drawId);
void rdtscEvent(uint32_t bucketId, uint32_t count1, uint32_t count2);
void rdtscEndFrame();
 
#ifdef KNOB_ENABLE_RDTSC
#define RDTSC_RESET() rdtscReset()
#define RDTSC_INIT(threadId) rdtscInit(threadId)
#define RDTSC_START(bucket) rdtscStart(bucket)
#define RDTSC_STOP(bucket, count, draw) rdtscStop(bucket, count, draw)
#define RDTSC_EVENT(bucket, count1, count2) rdtscEvent(bucket, count1, count2)
#define RDTSC_ENDFRAME() rdtscEndFrame()
#else
#define RDTSC_RESET()
#define RDTSC_INIT(threadId)
#define RDTSC_START(bucket)
#define RDTSC_STOP(bucket, count, draw)
#define RDTSC_EVENT(bucket, count1, count2)
#define RDTSC_ENDFRAME()
#endif
 
extern std::vector<uint32_t> gBucketMap;
extern BucketManager gBucketMgr;
extern BUCKET_DESC gCoreBuckets[];
extern uint32_t gCurrentFrame;
extern bool gBucketsInitialized;
 
INLINE void rdtscReset()
{
    gCurrentFrame = 0;
    gBucketMgr.ClearThreads();
}
 
INLINE void rdtscInit(int threadId)
{
    // register all the buckets once
    if (!gBucketsInitialized && (threadId == 0))
    {
        gBucketMap.resize(NumBuckets);
        for (uint32_t i = 0; i < NumBuckets; ++i)
        {
            gBucketMap[i] = gBucketMgr.RegisterBucket(gCoreBuckets[i]);
        }
        gBucketsInitialized = true;
    }
 
    std::string name = threadId == 0 ? "API" : "WORKER";
    gBucketMgr.RegisterThread(name);
}
 
INLINE void rdtscStart(uint32_t bucketId)
{
    uint32_t id = gBucketMap[bucketId];
    gBucketMgr.StartBucket(id);
}
 
INLINE void rdtscStop(uint32_t bucketId, uint32_t count, uint64_t drawId)
{
    uint32_t id = gBucketMap[bucketId];
    gBucketMgr.StopBucket(id);
}
 
INLINE void rdtscEvent(uint32_t bucketId, uint32_t count1, uint32_t count2)
{
    uint32_t id = gBucketMap[bucketId];
    gBucketMgr.AddEvent(id, count1);
}
 
INLINE void rdtscEndFrame()
{
    gCurrentFrame++;
 
    if (gCurrentFrame == KNOB_BUCKETS_START_FRAME && KNOB_BUCKETS_START_FRAME < KNOB_BUCKETS_END_FRAME)
    {
        gBucketMgr.StartCapture();
    }
 
    if (gCurrentFrame == KNOB_BUCKETS_END_FRAME && KNOB_BUCKETS_START_FRAME < KNOB_BUCKETS_END_FRAME)
    {
        gBucketMgr.StopCapture();
        gBucketMgr.PrintReport("rdtsc.txt");
    }
}