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