18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *  sync stress test: merging
38c2ecf20Sopenharmony_ci *  Copyright 2015-2016 Collabora Ltd.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Based on the implementation from the Android Open Source Project,
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  Copyright 2012 Google, Inc
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *  Permission is hereby granted, free of charge, to any person obtaining a
108c2ecf20Sopenharmony_ci *  copy of this software and associated documentation files (the "Software"),
118c2ecf20Sopenharmony_ci *  to deal in the Software without restriction, including without limitation
128c2ecf20Sopenharmony_ci *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
138c2ecf20Sopenharmony_ci *  and/or sell copies of the Software, and to permit persons to whom the
148c2ecf20Sopenharmony_ci *  Software is furnished to do so, subject to the following conditions:
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *  The above copyright notice and this permission notice shall be included in
178c2ecf20Sopenharmony_ci *  all copies or substantial portions of the Software.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
208c2ecf20Sopenharmony_ci *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
218c2ecf20Sopenharmony_ci *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
228c2ecf20Sopenharmony_ci *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
238c2ecf20Sopenharmony_ci *  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
248c2ecf20Sopenharmony_ci *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
258c2ecf20Sopenharmony_ci *  OTHER DEALINGS IN THE SOFTWARE.
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <stdlib.h>
298c2ecf20Sopenharmony_ci#include <string.h>
308c2ecf20Sopenharmony_ci#include <time.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include "sync.h"
338c2ecf20Sopenharmony_ci#include "sw_sync.h"
348c2ecf20Sopenharmony_ci#include "synctest.h"
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ciint test_merge_stress_random_merge(void)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	int i, size, ret;
398c2ecf20Sopenharmony_ci	int timeline_count = 32;
408c2ecf20Sopenharmony_ci	int merge_count = 1024 * 32;
418c2ecf20Sopenharmony_ci	int timelines[timeline_count];
428c2ecf20Sopenharmony_ci	int fence_map[timeline_count];
438c2ecf20Sopenharmony_ci	int fence, tmpfence, merged, valid;
448c2ecf20Sopenharmony_ci	int timeline, timeline_offset, sync_point;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	srand(time(NULL));
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	for (i = 0; i < timeline_count; i++)
498c2ecf20Sopenharmony_ci		timelines[i] = sw_sync_timeline_create();
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	fence = sw_sync_fence_create(timelines[0], "fence", 0);
528c2ecf20Sopenharmony_ci	valid = sw_sync_fence_is_valid(fence);
538c2ecf20Sopenharmony_ci	ASSERT(valid, "Failure creating fence\n");
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	memset(fence_map, -1, sizeof(fence_map));
568c2ecf20Sopenharmony_ci	fence_map[0] = 0;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	/*
598c2ecf20Sopenharmony_ci	 * Randomly create sync_points out of a fixed set of timelines,
608c2ecf20Sopenharmony_ci	 * and merge them together
618c2ecf20Sopenharmony_ci	 */
628c2ecf20Sopenharmony_ci	for (i = 0; i < merge_count; i++) {
638c2ecf20Sopenharmony_ci		/* Generate sync_point. */
648c2ecf20Sopenharmony_ci		timeline_offset = rand() % timeline_count;
658c2ecf20Sopenharmony_ci		timeline = timelines[timeline_offset];
668c2ecf20Sopenharmony_ci		sync_point = rand();
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci		/* Keep track of the latest sync_point in each timeline. */
698c2ecf20Sopenharmony_ci		if (fence_map[timeline_offset] == -1)
708c2ecf20Sopenharmony_ci			fence_map[timeline_offset] = sync_point;
718c2ecf20Sopenharmony_ci		else if (fence_map[timeline_offset] < sync_point)
728c2ecf20Sopenharmony_ci			fence_map[timeline_offset] = sync_point;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci		/* Merge */
758c2ecf20Sopenharmony_ci		tmpfence = sw_sync_fence_create(timeline, "fence", sync_point);
768c2ecf20Sopenharmony_ci		merged = sync_merge("merge", tmpfence, fence);
778c2ecf20Sopenharmony_ci		sw_sync_fence_destroy(tmpfence);
788c2ecf20Sopenharmony_ci		sw_sync_fence_destroy(fence);
798c2ecf20Sopenharmony_ci		fence = merged;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci		valid = sw_sync_fence_is_valid(merged);
828c2ecf20Sopenharmony_ci		ASSERT(valid, "Failure creating fence i\n");
838c2ecf20Sopenharmony_ci	}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	size = 0;
868c2ecf20Sopenharmony_ci	for (i = 0; i < timeline_count; i++)
878c2ecf20Sopenharmony_ci		if (fence_map[i] != -1)
888c2ecf20Sopenharmony_ci			size++;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	/* Confirm our map matches the fence. */
918c2ecf20Sopenharmony_ci	ASSERT(sync_fence_size(fence) == size,
928c2ecf20Sopenharmony_ci	       "Quantity of elements not matching\n");
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	/* Trigger the merged fence */
958c2ecf20Sopenharmony_ci	for (i = 0; i < timeline_count; i++) {
968c2ecf20Sopenharmony_ci		if (fence_map[i] != -1) {
978c2ecf20Sopenharmony_ci			ret = sync_wait(fence, 0);
988c2ecf20Sopenharmony_ci			ASSERT(ret == 0,
998c2ecf20Sopenharmony_ci			       "Failure waiting on fence until timeout\n");
1008c2ecf20Sopenharmony_ci			/* Increment the timeline to the last sync_point */
1018c2ecf20Sopenharmony_ci			sw_sync_timeline_inc(timelines[i], fence_map[i]);
1028c2ecf20Sopenharmony_ci		}
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	/* Check that the fence is triggered. */
1068c2ecf20Sopenharmony_ci	ret = sync_wait(fence, 0);
1078c2ecf20Sopenharmony_ci	ASSERT(ret > 0, "Failure triggering fence\n");
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	sw_sync_fence_destroy(fence);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	for (i = 0; i < timeline_count; i++)
1128c2ecf20Sopenharmony_ci		sw_sync_timeline_destroy(timelines[i]);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return 0;
1158c2ecf20Sopenharmony_ci}
116