Path: blob/21.2-virgl/src/gallium/drivers/iris/iris_monitor.c
4565 views
/*1* Copyright © 2019 Intel Corporation2*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 shall be included11* in all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING18* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER19* DEALINGS IN THE SOFTWARE.20*/2122#include "iris_monitor.h"2324#include <xf86drm.h>2526#include "iris_screen.h"27#include "iris_context.h"28#include "iris_perf.h"2930struct iris_monitor_object {31int num_active_counters;32int *active_counters;3334size_t result_size;35unsigned char *result_buffer;3637struct intel_perf_query_object *query;38};3940int41iris_get_monitor_info(struct pipe_screen *pscreen, unsigned index,42struct pipe_driver_query_info *info)43{44const struct iris_screen *screen = (struct iris_screen *)pscreen;45const struct intel_perf_config *perf_cfg = screen->perf_cfg;46assert(perf_cfg);47if (!perf_cfg)48return 0;4950if (!info) {51/* return the number of metrics */52return perf_cfg->n_counters;53}5455struct intel_perf_query_counter_info *counter_info = &perf_cfg->counter_infos[index];56struct intel_perf_query_counter *counter = counter_info->counter;5758info->group_id = counter_info->location.group_idx;59info->name = counter->name;60info->query_type = PIPE_QUERY_DRIVER_SPECIFIC + index;6162if (counter->type == INTEL_PERF_COUNTER_TYPE_THROUGHPUT)63info->result_type = PIPE_DRIVER_QUERY_RESULT_TYPE_AVERAGE;64else65info->result_type = PIPE_DRIVER_QUERY_RESULT_TYPE_CUMULATIVE;66switch (counter->data_type) {67case INTEL_PERF_COUNTER_DATA_TYPE_BOOL32:68case INTEL_PERF_COUNTER_DATA_TYPE_UINT32:69info->type = PIPE_DRIVER_QUERY_TYPE_UINT;70assert(counter->raw_max <= UINT32_MAX);71info->max_value.u32 = (uint32_t)counter->raw_max;72break;73case INTEL_PERF_COUNTER_DATA_TYPE_UINT64:74info->type = PIPE_DRIVER_QUERY_TYPE_UINT64;75info->max_value.u64 = counter->raw_max;76break;77case INTEL_PERF_COUNTER_DATA_TYPE_FLOAT:78case INTEL_PERF_COUNTER_DATA_TYPE_DOUBLE:79info->type = PIPE_DRIVER_QUERY_TYPE_FLOAT;80info->max_value.f = counter->raw_max;81break;82default:83assert(false);84break;85}8687/* indicates that this is an OA query, not a pipeline statistics query */88info->flags = PIPE_DRIVER_QUERY_FLAG_BATCH;89return 1;90}9192static bool93iris_monitor_init_metrics(struct iris_screen *screen)94{95struct intel_perf_config *perf_cfg = intel_perf_new(screen);96if (unlikely(!perf_cfg))97return false;9899screen->perf_cfg = perf_cfg;100101iris_perf_init_vtbl(perf_cfg);102103intel_perf_init_metrics(perf_cfg, &screen->devinfo, screen->fd,104true /* pipeline stats*/,105true /* register snapshots */);106107return perf_cfg->n_counters > 0;108}109110int111iris_get_monitor_group_info(struct pipe_screen *pscreen,112unsigned group_index,113struct pipe_driver_query_group_info *info)114{115struct iris_screen *screen = (struct iris_screen *)pscreen;116if (!screen->perf_cfg) {117if (!iris_monitor_init_metrics(screen))118return 0;119}120121const struct intel_perf_config *perf_cfg = screen->perf_cfg;122123if (!info) {124/* return the count that can be queried */125return perf_cfg->n_queries;126}127128if (group_index >= perf_cfg->n_queries) {129/* out of range */130return 0;131}132133struct intel_perf_query_info *query = &perf_cfg->queries[group_index];134135info->name = query->name;136info->max_active_queries = query->n_counters;137info->num_queries = query->n_counters;138139return 1;140}141142static void143iris_init_monitor_ctx(struct iris_context *ice)144{145struct iris_screen *screen = (struct iris_screen *) ice->ctx.screen;146147ice->perf_ctx = intel_perf_new_context(ice);148if (unlikely(!ice->perf_ctx))149return;150151struct intel_perf_context *perf_ctx = ice->perf_ctx;152struct intel_perf_config *perf_cfg = screen->perf_cfg;153intel_perf_init_context(perf_ctx,154perf_cfg,155ice,156ice,157screen->bufmgr,158&screen->devinfo,159ice->batches[IRIS_BATCH_RENDER].hw_ctx_id,160screen->fd);161}162163/* entry point for GenPerfMonitorsAMD */164struct iris_monitor_object *165iris_create_monitor_object(struct iris_context *ice,166unsigned num_queries,167unsigned *query_types)168{169struct iris_screen *screen = (struct iris_screen *) ice->ctx.screen;170struct intel_perf_config *perf_cfg = screen->perf_cfg;171struct intel_perf_query_object *query_obj = NULL;172173/* initialize perf context if this has not already been done. This174* function is the first entry point that carries the gl context.175*/176if (ice->perf_ctx == NULL) {177iris_init_monitor_ctx(ice);178}179struct intel_perf_context *perf_ctx = ice->perf_ctx;180181assert(num_queries > 0);182int query_index = query_types[0] - PIPE_QUERY_DRIVER_SPECIFIC;183assert(query_index <= perf_cfg->n_counters);184const int group = perf_cfg->counter_infos[query_index].location.group_idx;185186struct iris_monitor_object *monitor =187calloc(1, sizeof(struct iris_monitor_object));188if (unlikely(!monitor))189goto allocation_failure;190191monitor->num_active_counters = num_queries;192monitor->active_counters = calloc(num_queries, sizeof(int));193if (unlikely(!monitor->active_counters))194goto allocation_failure;195196for (int i = 0; i < num_queries; ++i) {197unsigned current_query = query_types[i];198unsigned current_query_index = current_query - PIPE_QUERY_DRIVER_SPECIFIC;199200/* all queries must be in the same group */201assert(current_query_index <= perf_cfg->n_counters);202assert(perf_cfg->counter_infos[current_query_index].location.group_idx == group);203monitor->active_counters[i] =204perf_cfg->counter_infos[current_query_index].location.counter_idx;205}206207/* create the intel_perf_query */208query_obj = intel_perf_new_query(perf_ctx, group);209if (unlikely(!query_obj))210goto allocation_failure;211212monitor->query = query_obj;213monitor->result_size = perf_cfg->queries[group].data_size;214monitor->result_buffer = calloc(1, monitor->result_size);215if (unlikely(!monitor->result_buffer))216goto allocation_failure;217218return monitor;219220allocation_failure:221if (monitor) {222free(monitor->active_counters);223free(monitor->result_buffer);224}225free(query_obj);226free(monitor);227return NULL;228}229230void231iris_destroy_monitor_object(struct pipe_context *ctx,232struct iris_monitor_object *monitor)233{234struct iris_context *ice = (struct iris_context *)ctx;235236intel_perf_delete_query(ice->perf_ctx, monitor->query);237free(monitor->result_buffer);238monitor->result_buffer = NULL;239free(monitor->active_counters);240monitor->active_counters = NULL;241free(monitor);242}243244bool245iris_begin_monitor(struct pipe_context *ctx,246struct iris_monitor_object *monitor)247{248struct iris_context *ice = (void *) ctx;249struct intel_perf_context *perf_ctx = ice->perf_ctx;250251return intel_perf_begin_query(perf_ctx, monitor->query);252}253254bool255iris_end_monitor(struct pipe_context *ctx,256struct iris_monitor_object *monitor)257{258struct iris_context *ice = (void *) ctx;259struct intel_perf_context *perf_ctx = ice->perf_ctx;260261intel_perf_end_query(perf_ctx, monitor->query);262return true;263}264265bool266iris_get_monitor_result(struct pipe_context *ctx,267struct iris_monitor_object *monitor,268bool wait,269union pipe_numeric_type_union *result)270{271struct iris_context *ice = (void *) ctx;272struct intel_perf_context *perf_ctx = ice->perf_ctx;273struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];274275bool monitor_ready =276intel_perf_is_query_ready(perf_ctx, monitor->query, batch);277278if (!monitor_ready) {279if (!wait)280return false;281intel_perf_wait_query(perf_ctx, monitor->query, batch);282}283284assert(intel_perf_is_query_ready(perf_ctx, monitor->query, batch));285286unsigned bytes_written;287intel_perf_get_query_data(perf_ctx, monitor->query, batch,288monitor->result_size,289(unsigned*) monitor->result_buffer,290&bytes_written);291if (bytes_written != monitor->result_size)292return false;293294/* copy metrics into the batch result */295for (int i = 0; i < monitor->num_active_counters; ++i) {296int current_counter = monitor->active_counters[i];297const struct intel_perf_query_info *info =298intel_perf_query_info(monitor->query);299const struct intel_perf_query_counter *counter =300&info->counters[current_counter];301assert(intel_perf_query_counter_get_size(counter));302switch (counter->data_type) {303case INTEL_PERF_COUNTER_DATA_TYPE_UINT64:304result[i].u64 = *(uint64_t*)(monitor->result_buffer + counter->offset);305break;306case INTEL_PERF_COUNTER_DATA_TYPE_FLOAT:307result[i].f = *(float*)(monitor->result_buffer + counter->offset);308break;309case INTEL_PERF_COUNTER_DATA_TYPE_UINT32:310case INTEL_PERF_COUNTER_DATA_TYPE_BOOL32:311result[i].u64 = *(uint32_t*)(monitor->result_buffer + counter->offset);312break;313case INTEL_PERF_COUNTER_DATA_TYPE_DOUBLE: {314double v = *(double*)(monitor->result_buffer + counter->offset);315result[i].f = v;316break;317}318default:319unreachable("unexpected counter data type");320}321}322return true;323}324325326