xref: /aosp_15_r20/external/igt-gpu-tools/benchmarks/intel_upload_blit_large_gtt.c (revision d83cc019efdc2edc6c4b16e9034a3ceb8d35d77c)
1 /*
2  * Copyright © 2009 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  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <[email protected]>
25  *
26  */
27 
28 /**
29  * Roughly simulates repeatedly uploading frames of images, by uploading
30  * the data all at once with pwrite, and then blitting it to another buffer.
31  *
32  * You might think of this like a movie player, but that wouldn't be entirely
33  * accurate, since the access patterns of the memory would be different
34  * (generally, smaller source image, upscaled, an thus different memory access
35  * pattern in both texel fetch for the stretching and the destination writes).
36  * However, some things like swfdec would be doing something like this since
37  * they compute their data in host memory and upload the full sw rendered
38  * frame.
39  *
40  * Additionally, those applications should be rendering at the screen refresh
41  * rate, while this test has no limits, and so can get itself into the
42  * working set larger than aperture size performance disaster.
43  *
44  * The current workload doing this path is pixmap upload in 2D with KMS.
45  */
46 
47 #include "igt.h"
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <assert.h>
52 #include <fcntl.h>
53 #include <inttypes.h>
54 #include <errno.h>
55 #include <sys/stat.h>
56 #include <sys/time.h>
57 
58 #define OBJECT_WIDTH	1280
59 #define OBJECT_HEIGHT	720
60 
61 static double
get_time_in_secs(void)62 get_time_in_secs(void)
63 {
64 	struct timeval tv;
65 
66 	gettimeofday(&tv, NULL);
67 
68 	return (double)tv.tv_sec + tv.tv_usec / 1000000.0;
69 }
70 
71 static void
do_render(drm_intel_bufmgr * bufmgr,struct intel_batchbuffer * batch,drm_intel_bo * dst_bo,int width,int height)72 do_render(drm_intel_bufmgr *bufmgr, struct intel_batchbuffer *batch,
73 	  drm_intel_bo *dst_bo, int width, int height)
74 {
75 	uint32_t *data;
76 	drm_intel_bo *src_bo;
77 	int i;
78 	static uint32_t seed = 1;
79 
80 	src_bo = drm_intel_bo_alloc(bufmgr, "src", width * height * 4, 4096);
81 
82 	drm_intel_gem_bo_map_gtt(src_bo);
83 
84 	data = src_bo->virtual;
85 	for (i = 0; i < width * height; i++) {
86 		data[i] = seed++;
87 	}
88 
89 	drm_intel_gem_bo_unmap_gtt(src_bo);
90 
91 	/* Render the junk to the dst. */
92 	BLIT_COPY_BATCH_START(0);
93 	OUT_BATCH((3 << 24) | /* 32 bits */
94 		  (0xcc << 16) | /* copy ROP */
95 		  (width * 4) /* dst pitch */);
96 	OUT_BATCH(0); /* dst x1,y1 */
97 	OUT_BATCH((height << 16) | width); /* dst x2,y2 */
98 	OUT_RELOC(dst_bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, 0);
99 	OUT_BATCH(0); /* src x1,y1 */
100 	OUT_BATCH(width * 4); /* src pitch */
101 	OUT_RELOC(src_bo, I915_GEM_DOMAIN_RENDER, 0, 0);
102 	ADVANCE_BATCH();
103 
104 	intel_batchbuffer_flush(batch);
105 
106 	drm_intel_bo_unreference(src_bo);
107 }
108 
main(int argc,char ** argv)109 int main(int argc, char **argv)
110 {
111 	int fd;
112 	int object_size = OBJECT_WIDTH * OBJECT_HEIGHT * 4;
113 	double start_time, end_time;
114 	drm_intel_bo *dst_bo;
115 	drm_intel_bufmgr *bufmgr;
116 	struct intel_batchbuffer *batch;
117 	int i;
118 
119 	fd = drm_open_driver(DRIVER_INTEL);
120 
121 	bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
122 	drm_intel_bufmgr_gem_enable_reuse(bufmgr);
123 
124 	batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
125 
126 	dst_bo = drm_intel_bo_alloc(bufmgr, "dst", object_size, 4096);
127 
128 	/* Prep loop to get us warmed up. */
129 	for (i = 0; i < 60; i++) {
130 		do_render(bufmgr, batch, dst_bo, OBJECT_WIDTH, OBJECT_HEIGHT);
131 	}
132 	drm_intel_bo_wait_rendering(dst_bo);
133 
134 	/* Do the actual timing. */
135 	start_time = get_time_in_secs();
136 	for (i = 0; i < 200; i++) {
137 		do_render(bufmgr, batch, dst_bo, OBJECT_WIDTH, OBJECT_HEIGHT);
138 	}
139 	drm_intel_bo_wait_rendering(dst_bo);
140 	end_time = get_time_in_secs();
141 
142 	printf("%d iterations in %.03f secs: %.01f MB/sec\n", i,
143 	       end_time - start_time,
144 	       (double)i * OBJECT_WIDTH * OBJECT_HEIGHT * 4 / 1024.0 / 1024.0 /
145 	       (end_time - start_time));
146 
147 	intel_batchbuffer_free(batch);
148 	drm_intel_bufmgr_destroy(bufmgr);
149 
150 	close(fd);
151 
152 	return 0;
153 }
154