summaryrefslogtreecommitdiff
path: root/sync-basic/sync-basic.c
blob: 4c0167ee10fa36b2930cdf0badb2758f3dcdbfa3 (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
/*******************************************************************************
 * Copyright (c) 2013 Linaro
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Linaro <linaro-dev@lists.linaro.org>
 *******************************************************************************/

/* sync framework test */

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>

#ifdef ANDROID
/* kernel headers on android */
#include <linux/sync.h>
#include <linux/sw_sync.h>
#else
/* set KDIR in Makefile to point to these headers */
#include "sync.h"
#include "sw_sync.h"
#endif /* ANDROID */

#define SW_SYNC_DEV "/dev/sw_sync"

/* current test run */
int testno = -1;
char testname[32];

/* fd of SW_SYNC_DEV */
int sw_fd;

#define RED    "\x1B[31m"
#define GREEN  "\x1B[32m"
#define NORMAL "\x1B[0m"

void fatal (int error, const char *fmt, ...)
{
    va_list ap;

    fprintf (stderr, "Test %d: ", testno);
    va_start (ap, fmt);
    vfprintf (stderr, fmt, ap);
    if (error)
	fprintf (stderr, " (error %d [%s])", error, strerror (error));
    va_end (ap);
    fprintf (stderr, "\n");
/*    printf(RED); */
    printf ("%d [sync_basic_%s]: test failed\n", testno, testname);
/*    printf(NORMAL); */
    exit (1);
}

void pass (const char *fmt, ...)
{
    va_list ap;

    fprintf (stderr, "Test %d: ", testno);
    if (fmt == NULL)
	    fprintf (stderr, "Passed");
    else {
	    va_start (ap, fmt);
	    vfprintf (stderr, fmt, ap);
	    va_end (ap);
    }
    fprintf (stderr, "\n");
/*    printf(GREEN);  */
    printf ("%d [sync_basic_%s]: test passed\n", testno, testname);
/*    printf(NORMAL); */
}

void create_and_delete_fence (void)
{
  struct sw_sync_create_fence_data f;
  int ret;

  testno++;
  strcpy (testname, __func__);

  strncpy (f.name, "test fence 0", sizeof (f.name));
  f.value = 0;

  ret = ioctl (sw_fd, SW_SYNC_IOC_CREATE_FENCE, &f);
  if (ret < 0)
    fatal (errno, "can't create fence");
  ret = close (f.fence);
  if (ret < 0)
    fatal (errno, "can't delete fence");
  
  pass(NULL);
}

void create_fence_invalid (void)
{
  struct sw_sync_create_fence_data *f = (void *)0xdeaddead;
  int ret;

  testno++;
  strcpy (testname, __func__);

  ret = ioctl (sw_fd, SW_SYNC_IOC_CREATE_FENCE, f);
  if (ret >= 0 || errno != EFAULT)
    fatal (0, "expect -1 and EFAULT, but got %d and %d", ret, errno);

  pass(NULL);
}

#define TOOMANY 100000 /* should be larger than max fds per process */

void create_many_fences (void)
{
  struct sw_sync_create_fence_data f;
  static int fencefds[TOOMANY];
  int ret, nr, i;

  testno++;
  strcpy (testname, __func__);

  for (nr = 0; nr < TOOMANY; nr++) {
    sprintf (f.name, "fence %d", nr);
    f.value = nr;
    ret = ioctl (sw_fd, SW_SYNC_IOC_CREATE_FENCE, &f);
    if (ret < 0)
      break;
    else
      fencefds[nr] = f.fence;
  }

  if (errno != EMFILE)
    fatal (0, "expect EMFILE but got %d", errno);

  for (i = 0; i < nr; i++) {
    ret = close (fencefds[i]);
    if (ret < 0)
      fatal (errno, "error closing fence %d of %d", i, nr);
  }

  pass("%d fences were used", nr);
}

void fence_info (void)
{
  struct sw_sync_create_fence_data f;
  struct sync_fence_info_data *d;
  int ret;

  testno++;
  strcpy (testname, __func__);

  strncpy (f.name, "test fence 1", sizeof (f.name));
  f.value = 0;

  ret = ioctl (sw_fd, SW_SYNC_IOC_CREATE_FENCE, &f);
  if (ret < 0)
    fatal (errno, "can't create fence");

  d = malloc (128);
  if (!d)
    abort ();
  d->len = 128;
  ret = ioctl (f.fence, SYNC_IOC_FENCE_INFO, d);
  if (ret < 0)
    fatal (errno, "can't get fence %d info", f.fence);

  if (strcmp (d->name, "test fence 1"))
    fatal (0, "invalid fence name");
  if (d->status != 1)
    fatal (0, "invalid fence status");

  free (d);
  ret = close (f.fence);
  if (ret < 0)
    fatal (errno, "can't delete fence");

  pass(NULL);
}

void fence_info_invalid (void)
{
  struct sync_fence_info_data *d;
  int ret;

  testno++;
  strcpy (testname, __func__);

  d = malloc (128);
  if (!d)
    abort ();
  d->len = 128;

  ret = ioctl (fileno (stdout), SYNC_IOC_FENCE_INFO, d);
  if (ret >= 0 || errno != ENOTTY)
    fatal (0, "expect -1 and ENOTTY, but got %d and %d", ret, errno);

  free (d);

  pass(NULL);
}

void fence_merge (void)
{
  struct sw_sync_create_fence_data f1, f2;
  struct sync_merge_data f3;
  struct sync_fence_info_data *d;
  int ret;

  testno++;
  strcpy (testname, __func__);

  strncpy (f1.name, "test fence 1", sizeof (f1.name));
  f1.value = 1;
  ret = ioctl (sw_fd, SW_SYNC_IOC_CREATE_FENCE, &f1);
  if (ret < 0)
    fatal (errno, "can't create fence 1");

  strncpy (f2.name, "test fence 2", sizeof (f2.name));
  f2.value = 2;
  ret = ioctl (sw_fd, SW_SYNC_IOC_CREATE_FENCE, &f2);
  if (ret < 0)
    fatal (errno, "can't create fence 2");

  f3.fd2 = f2.fence;
  strncpy (f3.name, "merged fence 1 + 2", sizeof (f3.name));
  ret = ioctl (f1.fence, SYNC_IOC_MERGE, &f3);
  if (ret < 0)
    fatal (errno, "can't merge fences");

  d = malloc (128);
  if (!d)
    abort ();
  d->len = 128;
  ret = ioctl (f3.fence, SYNC_IOC_FENCE_INFO, d);
  if (ret < 0)
    fatal (errno, "can't get information about merged fence");
  if (strcmp (d->name, "merged fence 1 + 2"))
    fatal (0, "invalid merged fence name");
  free (d);

  if (f1.fence == f2.fence ||
      f2.fence == f3.fence ||
      f3.fence == f1.fence)
    fatal (0, "impossible fence fds: %d %d %d", f1.fence, f2.fence, f3.fence);

  ret = close (f3.fence);
  if (ret < 0)
    fatal (errno, "can't close merged fence");
  ret = close (f2.fence);
  if (ret < 0)
    fatal (errno, "can't close fence 2");
  ret = close (f1.fence);
  if (ret < 0)
    fatal (errno, "can't close fence 1");

  pass(NULL);
}

void fence_merge_itself (void)
{
  struct sw_sync_create_fence_data f1;
  struct sync_merge_data f2;
  int ret;

  testno++;
  strcpy (testname, __func__);

  strncpy (f1.name, "test fence 1", sizeof (f1.name));
  f1.value = 1;
  ret = ioctl (sw_fd, SW_SYNC_IOC_CREATE_FENCE, &f1);
  if (ret < 0)
    fatal (errno, "can't create fence 1");

  f2.fd2 = f1.fence;
  strncpy (f2.name, "copy of fence 1", sizeof (f2.name));
  ret = ioctl (f1.fence, SYNC_IOC_MERGE, &f2);
  if (ret < 0)
    fatal (errno, "can't merge fence with itself");

  ret = close (f2.fence);
  if (ret < 0)
    fatal (errno, "can't close copy fence");
  ret = close (f1.fence);
  if (ret < 0)
    fatal (errno, "can't close fence 1");

  pass(NULL);
}

void fence_merge_invalid (void)
{
  struct sw_sync_create_fence_data f1;
  struct sync_merge_data f2;
  int ret;

  testno++;
  strcpy (testname, __func__);

  strncpy (f1.name, "test fence 1", sizeof (f1.name));
  f1.value = 1;
  ret = ioctl (sw_fd, SW_SYNC_IOC_CREATE_FENCE, &f1);
  if (ret < 0)
    fatal (errno, "can't create fence 1");

  f2.fd2 = fileno (stdout);
  strncpy (f2.name, "impossible fence 2", sizeof (f2.name));
  ret = ioctl (f1.fence, SYNC_IOC_MERGE, &f2);

  if (ret >= 0 || errno != ENOENT)
    fatal (0, "expect -1 and ENOENT, but got %d and %d", ret, errno);

  ret = close (f1.fence);
  if (ret < 0)
    fatal (errno, "can't close fence 1");

  pass(NULL);
}

int main (int argc, char *argv[])
{
  /* 0 - just try to open */
  testno++;
  strcpy (testname, "try_open");
  sw_fd = open (SW_SYNC_DEV, O_RDWR);
  if (sw_fd < 0)
    fatal (errno, "can't open '%s'", SW_SYNC_DEV);
  pass(NULL);

  /* 1 - create and delete just one fence */
  create_and_delete_fence ();

  /* 2 - try to create fence fron invalid data */
  create_fence_invalid ();

  /* 3 - try to create as much as possible */
  create_many_fences ();

  /* 4 - basic fence info */
  fence_info ();

  /* 5 - invalid fence info */
  fence_info_invalid ();

  /* 6 - basic merge */
  fence_merge ();

  /* 7 - merge with itself (e.g. copy) */
  fence_merge_itself ();

  /* 8 - merge with invalid fence */
  fence_merge_invalid ();

  close (sw_fd);
  return 0;
}