1 /*
2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "pipe/p_state.h"
28 #include "util/u_string.h"
29 #include "util/u_memory.h"
30 #include "util/u_inlines.h"
31 #include "util/os_time.h"
32
33 #include "freedreno_query_sw.h"
34 #include "freedreno_context.h"
35 #include "freedreno_util.h"
36
37 /*
38 * SW Queries:
39 *
40 * In the core, we have some support for basic sw counters
41 */
42
43 static void
fd_sw_destroy_query(struct fd_context * ctx,struct fd_query * q)44 fd_sw_destroy_query(struct fd_context *ctx, struct fd_query *q)
45 {
46 struct fd_sw_query *sq = fd_sw_query(q);
47 free(sq);
48 }
49
50 static uint64_t
read_counter(struct fd_context * ctx,int type)51 read_counter(struct fd_context *ctx, int type)
52 {
53 switch (type) {
54 case PIPE_QUERY_PRIMITIVES_GENERATED:
55 return ctx->stats.prims_generated;
56 case PIPE_QUERY_PRIMITIVES_EMITTED:
57 return ctx->stats.prims_emitted;
58 case FD_QUERY_DRAW_CALLS:
59 return ctx->stats.draw_calls;
60 case FD_QUERY_BATCH_TOTAL:
61 return ctx->stats.batch_total;
62 case FD_QUERY_BATCH_SYSMEM:
63 return ctx->stats.batch_sysmem;
64 case FD_QUERY_BATCH_GMEM:
65 return ctx->stats.batch_gmem;
66 case FD_QUERY_BATCH_NONDRAW:
67 return ctx->stats.batch_nondraw;
68 case FD_QUERY_BATCH_RESTORE:
69 return ctx->stats.batch_restore;
70 case FD_QUERY_STAGING_UPLOADS:
71 return ctx->stats.staging_uploads;
72 case FD_QUERY_SHADOW_UPLOADS:
73 return ctx->stats.shadow_uploads;
74 case FD_QUERY_VS_REGS:
75 return ctx->stats.vs_regs;
76 case FD_QUERY_FS_REGS:
77 return ctx->stats.fs_regs;
78 }
79 return 0;
80 }
81
82 static bool
is_time_rate_query(struct fd_query * q)83 is_time_rate_query(struct fd_query *q)
84 {
85 switch (q->type) {
86 case FD_QUERY_BATCH_TOTAL:
87 case FD_QUERY_BATCH_SYSMEM:
88 case FD_QUERY_BATCH_GMEM:
89 case FD_QUERY_BATCH_NONDRAW:
90 case FD_QUERY_BATCH_RESTORE:
91 case FD_QUERY_STAGING_UPLOADS:
92 case FD_QUERY_SHADOW_UPLOADS:
93 return true;
94 default:
95 return false;
96 }
97 }
98
99 static bool
is_draw_rate_query(struct fd_query * q)100 is_draw_rate_query(struct fd_query *q)
101 {
102 switch (q->type) {
103 case FD_QUERY_VS_REGS:
104 case FD_QUERY_FS_REGS:
105 return true;
106 default:
107 return false;
108 }
109 }
110
111 static void
fd_sw_begin_query(struct fd_context * ctx,struct fd_query * q)112 fd_sw_begin_query(struct fd_context *ctx, struct fd_query *q)
113 {
114 struct fd_sw_query *sq = fd_sw_query(q);
115 sq->begin_value = read_counter(ctx, q->type);
116 if (is_time_rate_query(q)) {
117 sq->begin_time = os_time_get();
118 } else if (is_draw_rate_query(q)) {
119 sq->begin_time = ctx->stats.draw_calls;
120 }
121 }
122
123 static void
fd_sw_end_query(struct fd_context * ctx,struct fd_query * q)124 fd_sw_end_query(struct fd_context *ctx, struct fd_query *q)
125 {
126 struct fd_sw_query *sq = fd_sw_query(q);
127 sq->end_value = read_counter(ctx, q->type);
128 if (is_time_rate_query(q)) {
129 sq->end_time = os_time_get();
130 } else if (is_draw_rate_query(q)) {
131 sq->end_time = ctx->stats.draw_calls;
132 }
133 }
134
135 static bool
fd_sw_get_query_result(struct fd_context * ctx,struct fd_query * q,bool wait,union pipe_query_result * result)136 fd_sw_get_query_result(struct fd_context *ctx, struct fd_query *q,
137 bool wait, union pipe_query_result *result)
138 {
139 struct fd_sw_query *sq = fd_sw_query(q);
140
141 result->u64 = sq->end_value - sq->begin_value;
142
143 if (is_time_rate_query(q)) {
144 double fps = (result->u64 * 1000000) /
145 (double)(sq->end_time - sq->begin_time);
146 result->u64 = (uint64_t)fps;
147 } else if (is_draw_rate_query(q)) {
148 double avg = ((double)result->u64) /
149 (double)(sq->end_time - sq->begin_time);
150 result->f = avg;
151 }
152
153 return true;
154 }
155
156 static const struct fd_query_funcs sw_query_funcs = {
157 .destroy_query = fd_sw_destroy_query,
158 .begin_query = fd_sw_begin_query,
159 .end_query = fd_sw_end_query,
160 .get_query_result = fd_sw_get_query_result,
161 };
162
163 struct fd_query *
fd_sw_create_query(struct fd_context * ctx,unsigned query_type,unsigned index)164 fd_sw_create_query(struct fd_context *ctx, unsigned query_type, unsigned index)
165 {
166 struct fd_sw_query *sq;
167 struct fd_query *q;
168
169 switch (query_type) {
170 case PIPE_QUERY_PRIMITIVES_GENERATED:
171 case PIPE_QUERY_PRIMITIVES_EMITTED:
172 case FD_QUERY_DRAW_CALLS:
173 case FD_QUERY_BATCH_TOTAL:
174 case FD_QUERY_BATCH_SYSMEM:
175 case FD_QUERY_BATCH_GMEM:
176 case FD_QUERY_BATCH_NONDRAW:
177 case FD_QUERY_BATCH_RESTORE:
178 case FD_QUERY_STAGING_UPLOADS:
179 case FD_QUERY_SHADOW_UPLOADS:
180 case FD_QUERY_VS_REGS:
181 case FD_QUERY_FS_REGS:
182 break;
183 default:
184 return NULL;
185 }
186
187 sq = CALLOC_STRUCT(fd_sw_query);
188 if (!sq)
189 return NULL;
190
191 q = &sq->base;
192 q->funcs = &sw_query_funcs;
193 q->type = query_type;
194
195 return q;
196 }
197