freedreno: add prims-emitted driver query
[mesa.git] / src / gallium / drivers / freedreno / freedreno_query.c
1 /* -*- mode: C; c-file-style: "k&r"; ttxab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include "pipe/p_state.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_inlines.h"
33 #include "os/os_time.h"
34
35 #include "freedreno_query.h"
36 #include "freedreno_context.h"
37 #include "freedreno_util.h"
38
39 #define FD_QUERY_DRAW_CALLS (PIPE_QUERY_DRIVER_SPECIFIC + 0)
40 #define FD_QUERY_BATCH_TOTAL (PIPE_QUERY_DRIVER_SPECIFIC + 1) /* total # of batches (submits) */
41 #define FD_QUERY_BATCH_SYSMEM (PIPE_QUERY_DRIVER_SPECIFIC + 2) /* batches using system memory (GMEM bypass) */
42 #define FD_QUERY_BATCH_GMEM (PIPE_QUERY_DRIVER_SPECIFIC + 3) /* batches using GMEM */
43 #define FD_QUERY_BATCH_RESTORE (PIPE_QUERY_DRIVER_SPECIFIC + 4) /* batches requiring GMEM restore */
44
45 /* Currently just simple cpu query's supported.. probably need
46 * to refactor this a bit when I'm eventually ready to add gpu
47 * queries:
48 */
49 struct fd_query {
50 int type;
51 /* storage for the collected data */
52 union pipe_query_result data;
53 bool active;
54 uint64_t begin_value, end_value;
55 uint64_t begin_time, end_time;
56 };
57
58 static inline struct fd_query *
59 fd_query(struct pipe_query *pq)
60 {
61 return (struct fd_query *)pq;
62 }
63
64 static struct pipe_query *
65 fd_create_query(struct pipe_context *pctx, unsigned query_type)
66 {
67 struct fd_query *q;
68
69 switch (query_type) {
70 case PIPE_QUERY_PRIMITIVES_GENERATED:
71 case PIPE_QUERY_PRIMITIVES_EMITTED:
72 case FD_QUERY_DRAW_CALLS:
73 case FD_QUERY_BATCH_TOTAL:
74 case FD_QUERY_BATCH_SYSMEM:
75 case FD_QUERY_BATCH_GMEM:
76 case FD_QUERY_BATCH_RESTORE:
77 break;
78 default:
79 return NULL;
80 }
81
82 q = CALLOC_STRUCT(fd_query);
83 if (!q)
84 return NULL;
85
86 q->type = query_type;
87
88 return (struct pipe_query *) q;
89 }
90
91 static void
92 fd_destroy_query(struct pipe_context *pctx, struct pipe_query *pq)
93 {
94 struct fd_query *q = fd_query(pq);
95 free(q);
96 }
97
98 static uint64_t
99 read_counter(struct pipe_context *pctx, int type)
100 {
101 struct fd_context *ctx = fd_context(pctx);
102 switch (type) {
103 case PIPE_QUERY_PRIMITIVES_GENERATED:
104 /* for now same thing as _PRIMITIVES_EMITTED */
105 case PIPE_QUERY_PRIMITIVES_EMITTED:
106 return ctx->stats.prims_emitted;
107 case FD_QUERY_DRAW_CALLS:
108 return ctx->stats.draw_calls;
109 case FD_QUERY_BATCH_TOTAL:
110 return ctx->stats.batch_total;
111 case FD_QUERY_BATCH_SYSMEM:
112 return ctx->stats.batch_sysmem;
113 case FD_QUERY_BATCH_GMEM:
114 return ctx->stats.batch_gmem;
115 case FD_QUERY_BATCH_RESTORE:
116 return ctx->stats.batch_restore;
117 }
118 return 0;
119 }
120
121 static bool
122 is_rate_query(struct fd_query *q)
123 {
124 switch (q->type) {
125 case FD_QUERY_BATCH_TOTAL:
126 case FD_QUERY_BATCH_SYSMEM:
127 case FD_QUERY_BATCH_GMEM:
128 case FD_QUERY_BATCH_RESTORE:
129 return true;
130 default:
131 return false;
132 }
133 }
134
135 static void
136 fd_begin_query(struct pipe_context *pctx, struct pipe_query *pq)
137 {
138 struct fd_query *q = fd_query(pq);
139 q->active = true;
140 q->begin_value = read_counter(pctx, q->type);
141 if (is_rate_query(q))
142 q->begin_time = os_time_get();
143 }
144
145 static void
146 fd_end_query(struct pipe_context *pctx, struct pipe_query *pq)
147 {
148 struct fd_query *q = fd_query(pq);
149 q->active = false;
150 q->end_value = read_counter(pctx, q->type);
151 if (is_rate_query(q))
152 q->end_time = os_time_get();
153 }
154
155 static boolean
156 fd_get_query_result(struct pipe_context *pctx, struct pipe_query *pq,
157 boolean wait, union pipe_query_result *result)
158 {
159 struct fd_query *q = fd_query(pq);
160
161 if (q->active)
162 return false;
163
164 util_query_clear_result(result, q->type);
165
166 result->u64 = q->end_value - q->begin_value;
167
168 if (is_rate_query(q)) {
169 double fps = (result->u64 * 1000000) /
170 (double)(q->end_time - q->begin_time);
171 result->u64 = (uint64_t)fps;
172 }
173
174 return true;
175 }
176
177 static int
178 fd_get_driver_query_info(struct pipe_screen *pscreen,
179 unsigned index, struct pipe_driver_query_info *info)
180 {
181 struct pipe_driver_query_info list[] = {
182 {"draw-calls", FD_QUERY_DRAW_CALLS, 0},
183 {"batches", FD_QUERY_BATCH_TOTAL, 0},
184 {"batches-sysmem", FD_QUERY_BATCH_SYSMEM, 0},
185 {"batches-gmem", FD_QUERY_BATCH_GMEM, 0},
186 {"restores", FD_QUERY_BATCH_RESTORE, 0},
187 {"prims-emitted", PIPE_QUERY_PRIMITIVES_EMITTED, 0},
188 };
189
190 if (!info)
191 return ARRAY_SIZE(list);
192
193 if (index >= ARRAY_SIZE(list))
194 return 0;
195
196 *info = list[index];
197 return 1;
198 }
199
200 void
201 fd_query_screen_init(struct pipe_screen *pscreen)
202 {
203 pscreen->get_driver_query_info = fd_get_driver_query_info;
204 }
205
206 void
207 fd_query_context_init(struct pipe_context *pctx)
208 {
209 pctx->create_query = fd_create_query;
210 pctx->destroy_query = fd_destroy_query;
211 pctx->begin_query = fd_begin_query;
212 pctx->end_query = fd_end_query;
213 pctx->get_query_result = fd_get_query_result;
214 }