1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2020 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
8bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
9bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifndef INTEL_MEASURE_H
25bf215546Sopenharmony_ci#define INTEL_MEASURE_H
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include <pthread.h>
28bf215546Sopenharmony_ci#include <stdio.h>
29bf215546Sopenharmony_ci#include <stdbool.h>
30bf215546Sopenharmony_ci#include <stdint.h>
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "util/list.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_cienum intel_measure_snapshot_type {
35bf215546Sopenharmony_ci   INTEL_SNAPSHOT_UNDEFINED,
36bf215546Sopenharmony_ci   INTEL_SNAPSHOT_BLIT,
37bf215546Sopenharmony_ci   INTEL_SNAPSHOT_CCS_AMBIGUATE,
38bf215546Sopenharmony_ci   INTEL_SNAPSHOT_CCS_COLOR_CLEAR,
39bf215546Sopenharmony_ci   INTEL_SNAPSHOT_CCS_PARTIAL_RESOLVE,
40bf215546Sopenharmony_ci   INTEL_SNAPSHOT_CCS_RESOLVE,
41bf215546Sopenharmony_ci   INTEL_SNAPSHOT_COMPUTE,
42bf215546Sopenharmony_ci   INTEL_SNAPSHOT_COPY,
43bf215546Sopenharmony_ci   INTEL_SNAPSHOT_DRAW,
44bf215546Sopenharmony_ci   INTEL_SNAPSHOT_HIZ_AMBIGUATE,
45bf215546Sopenharmony_ci   INTEL_SNAPSHOT_HIZ_CLEAR,
46bf215546Sopenharmony_ci   INTEL_SNAPSHOT_HIZ_RESOLVE,
47bf215546Sopenharmony_ci   INTEL_SNAPSHOT_MCS_COLOR_CLEAR,
48bf215546Sopenharmony_ci   INTEL_SNAPSHOT_MCS_PARTIAL_RESOLVE,
49bf215546Sopenharmony_ci   INTEL_SNAPSHOT_SLOW_COLOR_CLEAR,
50bf215546Sopenharmony_ci   INTEL_SNAPSHOT_SLOW_DEPTH_CLEAR,
51bf215546Sopenharmony_ci   INTEL_SNAPSHOT_SECONDARY_BATCH,
52bf215546Sopenharmony_ci   INTEL_SNAPSHOT_END,
53bf215546Sopenharmony_ci};
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_cienum intel_measure_events {
56bf215546Sopenharmony_ci   INTEL_MEASURE_DRAW       = (1 << 0),
57bf215546Sopenharmony_ci   INTEL_MEASURE_RENDERPASS = (1 << 1),
58bf215546Sopenharmony_ci   INTEL_MEASURE_SHADER     = (1 << 2),
59bf215546Sopenharmony_ci   INTEL_MEASURE_BATCH      = (1 << 3),
60bf215546Sopenharmony_ci   INTEL_MEASURE_FRAME      = (1 << 4),
61bf215546Sopenharmony_ci};
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_cistruct intel_measure_config {
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci   /* Stderr, or optionally set with INTEL_MEASURE=file={path{ */
66bf215546Sopenharmony_ci   FILE                      *file;
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci   /* Events that will be measured.  Set only one flag, with
69bf215546Sopenharmony_ci    * INTEL_MEASURE=[draw,rt,shader,batch,frame] */
70bf215546Sopenharmony_ci   enum intel_measure_events  flags;
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   /* Optionally set with INTEL_MEASURE=start={num} */
73bf215546Sopenharmony_ci   unsigned                   start_frame;
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   /* Optionally calculated with INTEL_MEASURE=count={num} based on
76bf215546Sopenharmony_ci    * start_frame
77bf215546Sopenharmony_ci    */
78bf215546Sopenharmony_ci   unsigned                   end_frame;
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   /* Number of events to combine per line of output. Optionally set with
81bf215546Sopenharmony_ci    * INTEL_MEASURE=interval={num}
82bf215546Sopenharmony_ci    */
83bf215546Sopenharmony_ci   unsigned                   event_interval;
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   /* Max snapshots per batch.  Set with
86bf215546Sopenharmony_ci    * INTEL_MEASURE=batch_size={num}. Additional snapshots will be dropped.
87bf215546Sopenharmony_ci    */
88bf215546Sopenharmony_ci   unsigned                   batch_size;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   /* Max number of batch measurements that can be buffered, for combining
91bf215546Sopenharmony_ci    * snapshots into frame or interval data.
92bf215546Sopenharmony_ci    */
93bf215546Sopenharmony_ci   unsigned                   buffer_size;
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci   /* Fifo which will be read to enable measurements at run-time.  Set with
96bf215546Sopenharmony_ci    * INTEL_MEASURE=control={path}.  `echo {num} > {path}` will collect num
97bf215546Sopenharmony_ci    * frames of measurements, beginning with the next frame boundary.
98bf215546Sopenharmony_ci    */
99bf215546Sopenharmony_ci   int                        control_fh;
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   /* true when snapshots are currently being collected */
102bf215546Sopenharmony_ci   bool                       enabled;
103bf215546Sopenharmony_ci};
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_cistruct intel_measure_batch;
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_cistruct intel_measure_snapshot {
108bf215546Sopenharmony_ci   enum intel_measure_snapshot_type type;
109bf215546Sopenharmony_ci   unsigned count, event_count;
110bf215546Sopenharmony_ci   const char* event_name;
111bf215546Sopenharmony_ci   uintptr_t framebuffer, vs, tcs, tes, gs, fs, cs;
112bf215546Sopenharmony_ci   /* for vulkan secondary command buffers */
113bf215546Sopenharmony_ci   struct intel_measure_batch *secondary;
114bf215546Sopenharmony_ci};
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_cistruct intel_measure_buffered_result {
117bf215546Sopenharmony_ci   struct intel_measure_snapshot snapshot;
118bf215546Sopenharmony_ci   uint64_t start_ts, end_ts, idle_duration;
119bf215546Sopenharmony_ci   unsigned frame, batch_count, event_index;
120bf215546Sopenharmony_ci};
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_cistruct intel_measure_ringbuffer {
123bf215546Sopenharmony_ci   unsigned head, tail;
124bf215546Sopenharmony_ci   struct intel_measure_buffered_result results[0];
125bf215546Sopenharmony_ci};
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci/* This function will be called when enqueued snapshots have been processed */
128bf215546Sopenharmony_citypedef void (*intel_measure_release_batch_cb)(struct intel_measure_batch *base);
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_cistruct intel_measure_device {
131bf215546Sopenharmony_ci   struct intel_measure_config *config;
132bf215546Sopenharmony_ci   unsigned frame;
133bf215546Sopenharmony_ci   intel_measure_release_batch_cb release_batch;
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci   /* Holds the list of (iris/anv)_measure_batch snapshots that have been
136bf215546Sopenharmony_ci    * submitted for rendering, but have not completed.
137bf215546Sopenharmony_ci    */
138bf215546Sopenharmony_ci   pthread_mutex_t mutex;
139bf215546Sopenharmony_ci   struct list_head queued_snapshots;
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci   /* Holds completed snapshots that may need to be combined before being
142bf215546Sopenharmony_ci    * written out
143bf215546Sopenharmony_ci    */
144bf215546Sopenharmony_ci   struct intel_measure_ringbuffer *ringbuffer;
145bf215546Sopenharmony_ci};
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_cistruct intel_measure_batch {
148bf215546Sopenharmony_ci   struct list_head link;
149bf215546Sopenharmony_ci   unsigned index;
150bf215546Sopenharmony_ci   unsigned frame, batch_count, event_count;
151bf215546Sopenharmony_ci   uintptr_t framebuffer;
152bf215546Sopenharmony_ci   uint64_t *timestamps;
153bf215546Sopenharmony_ci   struct intel_measure_snapshot snapshots[0];
154bf215546Sopenharmony_ci};
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_civoid intel_measure_init(struct intel_measure_device *device);
157bf215546Sopenharmony_ciconst char * intel_measure_snapshot_string(enum intel_measure_snapshot_type type);
158bf215546Sopenharmony_cibool intel_measure_state_changed(const struct intel_measure_batch *batch,
159bf215546Sopenharmony_ci                                 uintptr_t vs, uintptr_t tcs, uintptr_t tes,
160bf215546Sopenharmony_ci                                 uintptr_t gs, uintptr_t fs, uintptr_t cs);
161bf215546Sopenharmony_civoid intel_measure_frame_transition(unsigned frame);
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_cibool intel_measure_ready(struct intel_measure_batch *batch);
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_cistruct intel_device_info;
166bf215546Sopenharmony_civoid intel_measure_gather(struct intel_measure_device *device,
167bf215546Sopenharmony_ci                          struct intel_device_info *info);
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_ci#endif /* INTEL_MEASURE_H */
170