1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 ****************************************************************************/
23
24 #pragma once
25 #include "knobs.h"
26
27 #include "common/os.h"
28 #include "common/rdtsc_buckets.h"
29
30 #include <vector>
31
32 enum CORE_BUCKETS
33 {
34 APIClearRenderTarget,
35 APIDraw,
36 APIDrawWakeAllThreads,
37 APIDrawIndexed,
38 APIDispatch,
39 APIStoreTiles,
40 APIGetDrawContext,
41 APISync,
42 APIWaitForIdle,
43 FEProcessDraw,
44 FEProcessDrawIndexed,
45 FEFetchShader,
46 FEVertexShader,
47 FEHullShader,
48 FETessellation,
49 FEDomainShader,
50 FEGeometryShader,
51 FEStreamout,
52 FEPAAssemble,
53 FEBinPoints,
54 FEBinLines,
55 FEBinTriangles,
56 FETriangleSetup,
57 FEViewportCull,
58 FEGuardbandClip,
59 FEClipPoints,
60 FEClipLines,
61 FEClipTriangles,
62 FECullZeroAreaAndBackface,
63 FECullBetweenCenters,
64 FEProcessStoreTiles,
65 FEProcessInvalidateTiles,
66 WorkerWorkOnFifoBE,
67 WorkerFoundWork,
68 BELoadTiles,
69 BEDispatch,
70 BEClear,
71 BERasterizeLine,
72 BERasterizeTriangle,
73 BETriangleSetup,
74 BEStepSetup,
75 BECullZeroArea,
76 BEEmptyTriangle,
77 BETrivialAccept,
78 BETrivialReject,
79 BERasterizePartial,
80 BEPixelBackend,
81 BESetup,
82 BEBarycentric,
83 BEEarlyDepthTest,
84 BEPixelShader,
85 BESingleSampleBackend,
86 BEPixelRateBackend,
87 BESampleRateBackend,
88 BENullBackend,
89 BELateDepthTest,
90 BEOutputMerger,
91 BEStoreTiles,
92 BEEndTile,
93
94 NumBuckets
95 };
96
97 void rdtscReset();
98 void rdtscInit(int threadId);
99 void rdtscStart(uint32_t bucketId);
100 void rdtscStop(uint32_t bucketId, uint32_t count, uint64_t drawId);
101 void rdtscEvent(uint32_t bucketId, uint32_t count1, uint32_t count2);
102 void rdtscEndFrame();
103
104 #ifdef KNOB_ENABLE_RDTSC
105 #define RDTSC_RESET() rdtscReset()
106 #define RDTSC_INIT(threadId) rdtscInit(threadId)
107 #define RDTSC_START(bucket) rdtscStart(bucket)
108 #define RDTSC_STOP(bucket, count, draw) rdtscStop(bucket, count, draw)
109 #define RDTSC_EVENT(bucket, count1, count2) rdtscEvent(bucket, count1, count2)
110 #define RDTSC_ENDFRAME() rdtscEndFrame()
111 #else
112 #define RDTSC_RESET()
113 #define RDTSC_INIT(threadId)
114 #define RDTSC_START(bucket)
115 #define RDTSC_STOP(bucket, count, draw)
116 #define RDTSC_EVENT(bucket, count1, count2)
117 #define RDTSC_ENDFRAME()
118 #endif
119
120 extern std::vector<uint32_t> gBucketMap;
121 extern BucketManager gBucketMgr;
122 extern BUCKET_DESC gCoreBuckets[];
123 extern uint32_t gCurrentFrame;
124 extern bool gBucketsInitialized;
125
rdtscReset()126 INLINE void rdtscReset()
127 {
128 gCurrentFrame = 0;
129 gBucketMgr.ClearThreads();
130 }
131
rdtscInit(int threadId)132 INLINE void rdtscInit(int threadId)
133 {
134 // register all the buckets once
135 if (!gBucketsInitialized && (threadId == 0))
136 {
137 gBucketMap.resize(NumBuckets);
138 for (uint32_t i = 0; i < NumBuckets; ++i)
139 {
140 gBucketMap[i] = gBucketMgr.RegisterBucket(gCoreBuckets[i]);
141 }
142 gBucketsInitialized = true;
143 }
144
145 std::string name = threadId == 0 ? "API" : "WORKER";
146 gBucketMgr.RegisterThread(name);
147 }
148
rdtscStart(uint32_t bucketId)149 INLINE void rdtscStart(uint32_t bucketId)
150 {
151 uint32_t id = gBucketMap[bucketId];
152 gBucketMgr.StartBucket(id);
153 }
154
rdtscStop(uint32_t bucketId,uint32_t count,uint64_t drawId)155 INLINE void rdtscStop(uint32_t bucketId, uint32_t count, uint64_t drawId)
156 {
157 uint32_t id = gBucketMap[bucketId];
158 gBucketMgr.StopBucket(id);
159 }
160
rdtscEvent(uint32_t bucketId,uint32_t count1,uint32_t count2)161 INLINE void rdtscEvent(uint32_t bucketId, uint32_t count1, uint32_t count2)
162 {
163 uint32_t id = gBucketMap[bucketId];
164 gBucketMgr.AddEvent(id, count1);
165 }
166
rdtscEndFrame()167 INLINE void rdtscEndFrame()
168 {
169 gCurrentFrame++;
170
171 if (gCurrentFrame == KNOB_BUCKETS_START_FRAME && KNOB_BUCKETS_START_FRAME < KNOB_BUCKETS_END_FRAME)
172 {
173 gBucketMgr.StartCapture();
174 }
175
176 if (gCurrentFrame == KNOB_BUCKETS_END_FRAME && KNOB_BUCKETS_START_FRAME < KNOB_BUCKETS_END_FRAME)
177 {
178 gBucketMgr.StopCapture();
179 gBucketMgr.PrintReport("rdtsc.txt");
180 }
181 }
182