Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  *  sync stress test: merging
0003  *  Copyright 2015-2016 Collabora Ltd.
0004  *
0005  *  Based on the implementation from the Android Open Source Project,
0006  *
0007  *  Copyright 2012 Google, Inc
0008  *
0009  *  Permission is hereby granted, free of charge, to any person obtaining a
0010  *  copy of this software and associated documentation files (the "Software"),
0011  *  to deal in the Software without restriction, including without limitation
0012  *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
0013  *  and/or sell copies of the Software, and to permit persons to whom the
0014  *  Software is furnished to do so, subject to the following conditions:
0015  *
0016  *  The above copyright notice and this permission notice shall be included in
0017  *  all copies or substantial portions of the Software.
0018  *
0019  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0020  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0021  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
0022  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
0023  *  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0024  *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0025  *  OTHER DEALINGS IN THE SOFTWARE.
0026  */
0027 
0028 #include <stdlib.h>
0029 #include <string.h>
0030 #include <time.h>
0031 
0032 #include "sync.h"
0033 #include "sw_sync.h"
0034 #include "synctest.h"
0035 
0036 int test_merge_stress_random_merge(void)
0037 {
0038     int i, size, ret;
0039     int timeline_count = 32;
0040     int merge_count = 1024 * 32;
0041     int timelines[timeline_count];
0042     int fence_map[timeline_count];
0043     int fence, tmpfence, merged, valid;
0044     int timeline, timeline_offset, sync_point;
0045 
0046     srand(time(NULL));
0047 
0048     for (i = 0; i < timeline_count; i++)
0049         timelines[i] = sw_sync_timeline_create();
0050 
0051     fence = sw_sync_fence_create(timelines[0], "fence", 0);
0052     valid = sw_sync_fence_is_valid(fence);
0053     ASSERT(valid, "Failure creating fence\n");
0054 
0055     memset(fence_map, -1, sizeof(fence_map));
0056     fence_map[0] = 0;
0057 
0058     /*
0059      * Randomly create sync_points out of a fixed set of timelines,
0060      * and merge them together
0061      */
0062     for (i = 0; i < merge_count; i++) {
0063         /* Generate sync_point. */
0064         timeline_offset = rand() % timeline_count;
0065         timeline = timelines[timeline_offset];
0066         sync_point = rand();
0067 
0068         /* Keep track of the latest sync_point in each timeline. */
0069         if (fence_map[timeline_offset] == -1)
0070             fence_map[timeline_offset] = sync_point;
0071         else if (fence_map[timeline_offset] < sync_point)
0072             fence_map[timeline_offset] = sync_point;
0073 
0074         /* Merge */
0075         tmpfence = sw_sync_fence_create(timeline, "fence", sync_point);
0076         merged = sync_merge("merge", tmpfence, fence);
0077         sw_sync_fence_destroy(tmpfence);
0078         sw_sync_fence_destroy(fence);
0079         fence = merged;
0080 
0081         valid = sw_sync_fence_is_valid(merged);
0082         ASSERT(valid, "Failure creating fence i\n");
0083     }
0084 
0085     size = 0;
0086     for (i = 0; i < timeline_count; i++)
0087         if (fence_map[i] != -1)
0088             size++;
0089 
0090     /* Confirm our map matches the fence. */
0091     ASSERT(sync_fence_size(fence) == size,
0092            "Quantity of elements not matching\n");
0093 
0094     /* Trigger the merged fence */
0095     for (i = 0; i < timeline_count; i++) {
0096         if (fence_map[i] != -1) {
0097             ret = sync_wait(fence, 0);
0098             ASSERT(ret == 0,
0099                    "Failure waiting on fence until timeout\n");
0100             /* Increment the timeline to the last sync_point */
0101             sw_sync_timeline_inc(timelines[i], fence_map[i]);
0102         }
0103     }
0104 
0105     /* Check that the fence is triggered. */
0106     ret = sync_wait(fence, 0);
0107     ASSERT(ret > 0, "Failure triggering fence\n");
0108 
0109     sw_sync_fence_destroy(fence);
0110 
0111     for (i = 0; i < timeline_count; i++)
0112         sw_sync_timeline_destroy(timelines[i]);
0113 
0114     return 0;
0115 }