Path: blob/21.2-virgl/src/gallium/drivers/freedreno/freedreno_query_sw.c
4570 views
/*1* Copyright (C) 2014 Rob Clark <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Rob Clark <[email protected]>24*/2526#include "pipe/p_state.h"27#include "util/os_time.h"28#include "util/u_inlines.h"29#include "util/u_memory.h"30#include "util/u_string.h"3132#include "freedreno_context.h"33#include "freedreno_query_sw.h"34#include "freedreno_util.h"3536/*37* SW Queries:38*39* In the core, we have some support for basic sw counters40*/4142static void43fd_sw_destroy_query(struct fd_context *ctx, struct fd_query *q)44{45struct fd_sw_query *sq = fd_sw_query(q);46free(sq);47}4849static uint64_t50read_counter(struct fd_context *ctx, int type) assert_dt51{52switch (type) {53case PIPE_QUERY_PRIMITIVES_GENERATED:54return ctx->stats.prims_generated;55case PIPE_QUERY_PRIMITIVES_EMITTED:56return ctx->stats.prims_emitted;57case FD_QUERY_DRAW_CALLS:58return ctx->stats.draw_calls;59case FD_QUERY_BATCH_TOTAL:60return ctx->stats.batch_total;61case FD_QUERY_BATCH_SYSMEM:62return ctx->stats.batch_sysmem;63case FD_QUERY_BATCH_GMEM:64return ctx->stats.batch_gmem;65case FD_QUERY_BATCH_NONDRAW:66return ctx->stats.batch_nondraw;67case FD_QUERY_BATCH_RESTORE:68return ctx->stats.batch_restore;69case FD_QUERY_STAGING_UPLOADS:70return ctx->stats.staging_uploads;71case FD_QUERY_SHADOW_UPLOADS:72return ctx->stats.shadow_uploads;73case FD_QUERY_VS_REGS:74return ctx->stats.vs_regs;75case FD_QUERY_FS_REGS:76return ctx->stats.fs_regs;77}78return 0;79}8081static bool82is_time_rate_query(struct fd_query *q)83{84switch (q->type) {85case FD_QUERY_BATCH_TOTAL:86case FD_QUERY_BATCH_SYSMEM:87case FD_QUERY_BATCH_GMEM:88case FD_QUERY_BATCH_NONDRAW:89case FD_QUERY_BATCH_RESTORE:90case FD_QUERY_STAGING_UPLOADS:91case FD_QUERY_SHADOW_UPLOADS:92return true;93default:94return false;95}96}9798static bool99is_draw_rate_query(struct fd_query *q)100{101switch (q->type) {102case FD_QUERY_VS_REGS:103case FD_QUERY_FS_REGS:104return true;105default:106return false;107}108}109110static void111fd_sw_begin_query(struct fd_context *ctx, struct fd_query *q) assert_dt112{113struct fd_sw_query *sq = fd_sw_query(q);114115ctx->stats_users++;116117sq->begin_value = read_counter(ctx, q->type);118if (is_time_rate_query(q)) {119sq->begin_time = os_time_get();120} else if (is_draw_rate_query(q)) {121sq->begin_time = ctx->stats.draw_calls;122}123}124125static void126fd_sw_end_query(struct fd_context *ctx, struct fd_query *q) assert_dt127{128struct fd_sw_query *sq = fd_sw_query(q);129130assert(ctx->stats_users > 0);131ctx->stats_users--;132133sq->end_value = read_counter(ctx, q->type);134if (is_time_rate_query(q)) {135sq->end_time = os_time_get();136} else if (is_draw_rate_query(q)) {137sq->end_time = ctx->stats.draw_calls;138}139}140141static bool142fd_sw_get_query_result(struct fd_context *ctx, struct fd_query *q, bool wait,143union pipe_query_result *result)144{145struct fd_sw_query *sq = fd_sw_query(q);146147result->u64 = sq->end_value - sq->begin_value;148149if (is_time_rate_query(q)) {150double fps =151(result->u64 * 1000000) / (double)(sq->end_time - sq->begin_time);152result->u64 = (uint64_t)fps;153} else if (is_draw_rate_query(q)) {154double avg =155((double)result->u64) / (double)(sq->end_time - sq->begin_time);156result->f = avg;157}158159return true;160}161162static const struct fd_query_funcs sw_query_funcs = {163.destroy_query = fd_sw_destroy_query,164.begin_query = fd_sw_begin_query,165.end_query = fd_sw_end_query,166.get_query_result = fd_sw_get_query_result,167};168169struct fd_query *170fd_sw_create_query(struct fd_context *ctx, unsigned query_type, unsigned index)171{172struct fd_sw_query *sq;173struct fd_query *q;174175switch (query_type) {176case PIPE_QUERY_PRIMITIVES_GENERATED:177case PIPE_QUERY_PRIMITIVES_EMITTED:178case FD_QUERY_DRAW_CALLS:179case FD_QUERY_BATCH_TOTAL:180case FD_QUERY_BATCH_SYSMEM:181case FD_QUERY_BATCH_GMEM:182case FD_QUERY_BATCH_NONDRAW:183case FD_QUERY_BATCH_RESTORE:184case FD_QUERY_STAGING_UPLOADS:185case FD_QUERY_SHADOW_UPLOADS:186case FD_QUERY_VS_REGS:187case FD_QUERY_FS_REGS:188break;189default:190return NULL;191}192193sq = CALLOC_STRUCT(fd_sw_query);194if (!sq)195return NULL;196197q = &sq->base;198q->funcs = &sw_query_funcs;199q->type = query_type;200201return q;202}203204205