summaryrefslogtreecommitdiffstats
path: root/mapped_access_perf_test.c
blob: 8f8ae43e4db5fd2ac8ca85e1502b9919c1885f17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * Copyright 2021 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/*
 * This test evaluates the speed at which BOs of various USE flags can be
 * accessed when mmap()ped. To represent Chrome graphics buffers uses, a naive
 * rotation operation is implemented here in C90. This also factors out the use
 * or not of SIMD instructions and/or sophisticated access patterns like those
 * employed by libyuv: this is OK here since we're only interested in relative
 * measurements comparing one BO USE flag set with another.
 * See https://tinyurl.com/cros-video-capture-buffers and b/169302186 for more
 * context.
 */

#include <assert.h>
#include <getopt.h>
#include <linux/dma-buf.h>
#include <sys/ioctl.h>
#include <time.h>

#include "bs_drm.h"

#define HANDLE_EINTR_AND_EAGAIN(x)                                 \
  ({                                                               \
    int result;                                                    \
    do {                                                           \
      result = (x);                                                \
    } while (result != -1 && (errno == EINTR || errno == EAGAIN)); \
    result;                                                        \
  })

int dma_sync(int fd, __u64 flags) {
  struct dma_buf_sync sync_point = {0};
  sync_point.flags = flags;
  return HANDLE_EINTR_AND_EAGAIN(ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync_point));
}

// N.B. This function actually does a clockwise 90-degree rotation and then a
// horizontal flip.
void NV12Rotate90(const uint8_t* src_y, int src_stride_y,
                  const uint8_t* src_uv, int src_stride_uv,
                  uint8_t* dst_y, int dst_stride_y,
                  uint8_t* dst_uv, int dst_stride_uv,
                  int src_width, int src_height) {
  // This loop walks the |src_y| samples in scanout order, but writes them in
  // the rotated order, hence doing big jumps in the destination space.
  for (int row = 0; row < src_height; ++row) {
    for (int col = 0; col < src_width; ++col) {
      const uint8_t* src_sample = src_y + row * src_stride_y + col;
      *(dst_y + col * dst_stride_y + row) = *src_sample;
    }
  }
  // Same idea but note the halving of |src_height| for the UV planes.
  const int uv_src_height = (src_height + 1) / 2;
  const int uv_src_width = 2 * ((src_width + 1) / 2);
  for (int row = 0; row < uv_src_height; ++row) {
    for (int col = 0; col < uv_src_width; ++col) {
      const uint8_t* src_sample = src_uv + row * src_stride_uv + col;
      *(dst_uv + col * dst_stride_uv + row) = *src_sample;
    }
  }
}


struct test_case {
  uint32_t format; /* format for allocating buffer object from GBM */
  enum gbm_bo_transfer_flags read_write;
  enum gbm_bo_flags usage;
};

static void print_format_and_use_flags(FILE* out,
                                       const struct test_case* tcase) {
  fprintf(out, "format: ");
  switch (tcase->format) {
    case GBM_FORMAT_NV12:
      fprintf(out, "GBM_FORMAT_NV12");
      break;
    default:
      fprintf(out, "GBM_FORMAT_????????");
  }

  fprintf(out, ", access: %s%s",
          (tcase->read_write & GBM_BO_TRANSFER_READ ? "R" : ""),
          (tcase->read_write & GBM_BO_TRANSFER_WRITE ? "W" : ""));

  fprintf(out, ", use flags: ");
  bool first = true;
  if (tcase->usage & GBM_BO_USE_SCANOUT) {
    fprintf(out, "%sGBM_BO_USE_SCANOUT", first ? "" : " | ");
    first = false;
  }
  if (tcase->usage & GBM_BO_USE_LINEAR) {
    fprintf(out, "%sGBM_BO_USE_LINEAR", first ? "" : " | ");
    first = false;
  }
  if (tcase->usage & GBM_BO_USE_TEXTURING) {
    fprintf(out, "%sGBM_BO_USE_TEXTURING", first ? "" : " | ");
    first = false;
  }
  if (tcase->usage & GBM_BO_USE_CAMERA_READ) {
    fprintf(out, "%sGBM_BO_USE_CAMERA_READ", first ? "" : " | ");
    first = false;
  }
  if (tcase->usage & GBM_BO_USE_CAMERA_WRITE) {
    fprintf(out, "%sGBM_BO_USE_CAMERA_WRITE", first ? "" : " | ");
    first = false;
  }
  if (tcase->usage & GBM_BO_USE_SW_READ_OFTEN) {
    fprintf(out, "%sGBM_BO_USE_SW_READ_OFTEN", first ? "" : " | ");
    first = false;
  }
  if (tcase->usage & GBM_BO_USE_SW_WRITE_OFTEN) {
    fprintf(out, "%sGBM_BO_USE_SW_WRITE_OFTEN", first ? "" : " | ");
    first = false;
  }
}

static const struct option longopts[] = {
    {"help", no_argument, NULL, 'h'},
    {0, 0, 0, 0},
};

static void print_help(const char* argv0) {
  printf("Usage: %s [OPTIONS]\n", argv0);
  printf(" -h, --help     Print help.\n");
}

int main(int argc, char** argv) {
  // TODO(mcasas): Consider adding other formats/other operations.
  // TODO(mcasas): Transform this list into a cartesian product like GTest does.
  // TODO(mcasas): add command line flags to run test cases individually/by
  // groups, and to list them.
  const struct test_case tcases[] = {
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ, GBM_BO_USE_SCANOUT},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ, GBM_BO_USE_LINEAR},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ, GBM_BO_USE_TEXTURING},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ, GBM_BO_USE_CAMERA_READ},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ, GBM_BO_USE_CAMERA_WRITE},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ, GBM_BO_USE_SW_READ_OFTEN},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ, GBM_BO_USE_SW_WRITE_OFTEN},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ,
       GBM_BO_USE_LINEAR | GBM_BO_USE_SCANOUT},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ,
       GBM_BO_USE_LINEAR | GBM_BO_USE_SW_READ_OFTEN},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ,
       GBM_BO_USE_LINEAR | GBM_BO_USE_SW_WRITE_OFTEN},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ,
       GBM_BO_USE_LINEAR | GBM_BO_USE_SW_READ_OFTEN |
           GBM_BO_USE_SW_WRITE_OFTEN},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_WRITE, GBM_BO_USE_SCANOUT},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_WRITE, GBM_BO_USE_LINEAR},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_WRITE, GBM_BO_USE_TEXTURING},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_WRITE, GBM_BO_USE_CAMERA_READ},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_WRITE, GBM_BO_USE_CAMERA_WRITE},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_WRITE, GBM_BO_USE_SW_READ_OFTEN},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_WRITE, GBM_BO_USE_SW_WRITE_OFTEN},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_WRITE,
       GBM_BO_USE_LINEAR | GBM_BO_USE_SCANOUT},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_WRITE,
       GBM_BO_USE_LINEAR | GBM_BO_USE_SW_READ_OFTEN},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_WRITE,
       GBM_BO_USE_LINEAR | GBM_BO_USE_SW_WRITE_OFTEN},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_WRITE,
       GBM_BO_USE_LINEAR | GBM_BO_USE_SW_READ_OFTEN |
           GBM_BO_USE_SW_WRITE_OFTEN},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_WRITE, GBM_BO_USE_SCANOUT},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ_WRITE, GBM_BO_USE_SCANOUT},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ_WRITE, GBM_BO_USE_LINEAR},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ_WRITE, GBM_BO_USE_TEXTURING},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ_WRITE, GBM_BO_USE_CAMERA_READ},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ_WRITE, GBM_BO_USE_CAMERA_WRITE},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ_WRITE, GBM_BO_USE_SW_READ_OFTEN},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ_WRITE, GBM_BO_USE_SW_WRITE_OFTEN},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ_WRITE,
       GBM_BO_USE_LINEAR | GBM_BO_USE_SCANOUT},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ_WRITE,
       GBM_BO_USE_LINEAR | GBM_BO_USE_SW_READ_OFTEN},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ_WRITE,
       GBM_BO_USE_LINEAR | GBM_BO_USE_SW_WRITE_OFTEN},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ_WRITE,
       GBM_BO_USE_LINEAR | GBM_BO_USE_SW_READ_OFTEN |
           GBM_BO_USE_SW_WRITE_OFTEN},
      {GBM_FORMAT_NV12, GBM_BO_TRANSFER_READ_WRITE, GBM_BO_USE_SCANOUT},
  };
  const size_t tcases_size = BS_ARRAY_LEN(tcases);

  // Make sure that the clock resolution is at least 1ms.
  struct timespec clock_resolution;
  clock_getres(CLOCK_MONOTONIC, &clock_resolution);
  assert(clock_resolution.tv_sec == 0 && clock_resolution.tv_nsec <= 1000000);

  int c;
  while ((c = getopt_long(argc, argv, "h", longopts, NULL)) != -1) {
    switch (c) {
      case 'h':
      default:
        print_help(argv[0]);
        return EXIT_SUCCESS;
    }
  }

  int display_fd = bs_drm_open_main_display();
  if (display_fd < 0) {
    bs_debug_error("failed to open card for display");
    return EXIT_FAILURE;
  }

  struct gbm_device* gbm = gbm_create_device(display_fd);
  if (!gbm) {
    bs_debug_error("failed to create gbm device");
    return EXIT_FAILURE;
  }

  // bs_mapper_dma_buf_new() is expected to use mmap().
  struct bs_mapper* mapper = bs_mapper_dma_buf_new();
  if (mapper == NULL) {
    bs_debug_error("failed to create mapper object");
    return EXIT_FAILURE;
  }

  const uint32_t width = 1920;
  const uint32_t height = 1080;

// We allocate NUM_BOS to replicate a bit what is done in video capture.
#define NUM_BOS 5
  struct gbm_bo* bos[NUM_BOS];
  uint8_t* ptr_y[NUM_BOS];
  uint8_t* ptr_uv[NUM_BOS];

  uint32_t stride_y[NUM_BOS];
  void* map_data_y[NUM_BOS];
  uint32_t stride_uv[NUM_BOS];
  void* map_data_uv[NUM_BOS];

#define NUM_PLANES 2
  int gbm_bo_fds[NUM_PLANES][NUM_BOS];
#define NUM_ITERS 10
  printf("Running %d iterations. %d BOs allocated (%dx%d)\n", NUM_ITERS,
         NUM_BOS, width, height);

  // |draft_canvas| is allocated as if to be an ARGB buffer, and can fit NV12
  // data of the same |width| and |height|.
  uint8_t* draft_canvas = malloc(width * height * 4);
  // This is not so much for clearing it as it is for accessing it once.
  memset(draft_canvas, 0, width * height * 4);

  for (size_t i = 0; i < tcases_size; i++) {
    const struct test_case* tcase = &tcases[i];
    print_format_and_use_flags(stdout, tcase);
    printf(": ");

    for (size_t j = 0; j < NUM_BOS; j++) {
      bos[j] = gbm_bo_create(gbm, width, height, tcase->format, tcase->usage);
      if (!bos[j]) {
        printf(
            "gbm_bo_create() failed (probably format or usage is not "
            "supported.\n");
        continue;
      }

      const int expected_num_planes = NUM_PLANES;
      const int num_planes = gbm_bo_get_plane_count(bos[j]);
      if (expected_num_planes != num_planes) {
        printf("Incorrect number of planes, expected %d, got %d\n",
               expected_num_planes, num_planes);
        return EXIT_FAILURE;
      }

      ptr_y[j] = bs_mapper_map(mapper, bos[j], 0, &map_data_y[j], &stride_y[j]);
      if (ptr_y[j] == MAP_FAILED) {
        bs_debug_error("failed to mmap gbm bo plane 0 (Y)");
        return EXIT_FAILURE;
      }

      ptr_uv[j] =
          bs_mapper_map(mapper, bos[j], 1, &map_data_uv[j], &stride_uv[j]);
      if (ptr_uv[j] == MAP_FAILED) {
        bs_debug_error("failed to mmap gbm bo plane 1 (UV)");
        return EXIT_FAILURE;
      }

      for (size_t plane = 0; plane < NUM_PLANES; plane++) {
        gbm_bo_fds[plane][j] = gbm_bo_get_fd_for_plane(bos[j], plane);
        if (gbm_bo_fds[plane][j] < 0) {
          bs_debug_error("failed to get BO fd");
          return EXIT_FAILURE;
        }
      }
    }

    struct timespec start, stop;
    clock_gettime(CLOCK_MONOTONIC, &start);
    for (size_t j = 0; j < NUM_ITERS; j++) {
      const uint32_t bo_index = j % NUM_BOS;

      if (tcase->read_write & GBM_BO_TRANSFER_READ) {
        assert(dma_sync(gbm_bo_fds[0][bo_index],
                        DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ) == 0);
        assert(dma_sync(gbm_bo_fds[1][bo_index],
                        DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ) == 0);

        // Typical Chrome access patterns like e.g. libyuv NV12ToARGB/NV12Scale
        // are asymmetric in the sense that they create scattered read/writes
        // (e.g. pixel packing/unpacking operations) or simply more of those on
        // either source or destination. A rotation operation is chosen here to
        // avoid part of that asymmetry.
        // TODO(mcasas): investigate other functions which might cause other
        // memory access patterns.
        NV12Rotate90(ptr_y[bo_index], stride_y[bo_index],
                     ptr_uv[bo_index], stride_uv[bo_index],
                     draft_canvas, height,
                     draft_canvas + (height * width), height,
                     width, height);

        assert(dma_sync(gbm_bo_fds[0][bo_index],
                        DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ) == 0);
        assert(dma_sync(gbm_bo_fds[1][bo_index],
                        DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ) == 0);
      }

      // When writing, use the next BO index so that nobody will try to optimize
      // the whole operation chain away when having READ-then-WRITE.
      const uint32_t next_bo_index = (j + 1) % NUM_BOS;
      if (tcase->read_write & GBM_BO_TRANSFER_WRITE) {
        assert(dma_sync(gbm_bo_fds[0][next_bo_index],
                        DMA_BUF_SYNC_START | DMA_BUF_SYNC_WRITE) == 0);
        assert(dma_sync(gbm_bo_fds[1][next_bo_index],
                        DMA_BUF_SYNC_START | DMA_BUF_SYNC_WRITE) == 0);

        // We pretend |draft_canvas| has portrait orientation, so the
        // destination of the rotation fits into a landscape orientation BO.
        NV12Rotate90(draft_canvas, height,
                     draft_canvas + (height * width), height,
                     ptr_y[bo_index], stride_y[bo_index],
                     ptr_uv[bo_index], stride_uv[bo_index],
                     height, width);

        assert(dma_sync(gbm_bo_fds[0][next_bo_index],
                        DMA_BUF_SYNC_END | DMA_BUF_SYNC_WRITE) == 0);
        assert(dma_sync(gbm_bo_fds[1][next_bo_index],
                        DMA_BUF_SYNC_END | DMA_BUF_SYNC_WRITE) == 0);
      }
    }

    clock_gettime(CLOCK_MONOTONIC, &stop);
    const double elapsed_ns =
        (stop.tv_sec - start.tv_sec) * 1e9 + (stop.tv_nsec - start.tv_nsec);
    // TODO(mcasas): find a standardized way to produce results.
    printf("%f ms\n", elapsed_ns / 1000000.0);

    for (size_t j = 0; j < NUM_BOS; j++) {
      bs_mapper_unmap(mapper, bos[j], map_data_y[j]);
      bs_mapper_unmap(mapper, bos[j], map_data_uv[j]);

      for (size_t plane = 0; plane < NUM_PLANES; plane++)
        close(gbm_bo_fds[plane][j]);
      gbm_bo_destroy(bos[j]);
    }
  }

  free(draft_canvas);

  // Not really needed, but good to destroy things properly.
  bs_mapper_destroy(mapper);
  gbm_device_destroy(gbm);

  return EXIT_SUCCESS;
}