162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci *  sync stress test: producer/consumer
362306a36Sopenharmony_ci *  Copyright 2015-2016 Collabora Ltd.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  Based on the implementation from the Android Open Source Project,
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci *  Copyright 2012 Google, Inc
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci *  Permission is hereby granted, free of charge, to any person obtaining a
1062306a36Sopenharmony_ci *  copy of this software and associated documentation files (the "Software"),
1162306a36Sopenharmony_ci *  to deal in the Software without restriction, including without limitation
1262306a36Sopenharmony_ci *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
1362306a36Sopenharmony_ci *  and/or sell copies of the Software, and to permit persons to whom the
1462306a36Sopenharmony_ci *  Software is furnished to do so, subject to the following conditions:
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci *  The above copyright notice and this permission notice shall be included in
1762306a36Sopenharmony_ci *  all copies or substantial portions of the Software.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2062306a36Sopenharmony_ci *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2162306a36Sopenharmony_ci *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2262306a36Sopenharmony_ci *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
2362306a36Sopenharmony_ci *  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2462306a36Sopenharmony_ci *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2562306a36Sopenharmony_ci *  OTHER DEALINGS IN THE SOFTWARE.
2662306a36Sopenharmony_ci */
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#include <pthread.h>
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#include "sync.h"
3162306a36Sopenharmony_ci#include "sw_sync.h"
3262306a36Sopenharmony_ci#include "synctest.h"
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci/* IMPORTANT NOTE: if you see this test failing on your system, it may be
3562306a36Sopenharmony_ci * due to a shortage of file descriptors. Please ensure your system has
3662306a36Sopenharmony_ci * a sensible limit for this test to finish correctly.
3762306a36Sopenharmony_ci */
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci/* Returns 1 on error, 0 on success */
4062306a36Sopenharmony_cistatic int busy_wait_on_fence(int fence)
4162306a36Sopenharmony_ci{
4262306a36Sopenharmony_ci	int error, active;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	do {
4562306a36Sopenharmony_ci		error = sync_fence_count_with_status(fence, FENCE_STATUS_ERROR);
4662306a36Sopenharmony_ci		ASSERT(error == 0, "Error occurred on fence\n");
4762306a36Sopenharmony_ci		active = sync_fence_count_with_status(fence,
4862306a36Sopenharmony_ci						      FENCE_STATUS_ACTIVE);
4962306a36Sopenharmony_ci	} while (active);
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	return 0;
5262306a36Sopenharmony_ci}
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_cistatic struct {
5562306a36Sopenharmony_ci	int iterations;
5662306a36Sopenharmony_ci	int threads;
5762306a36Sopenharmony_ci	int counter;
5862306a36Sopenharmony_ci	int consumer_timeline;
5962306a36Sopenharmony_ci	int *producer_timelines;
6062306a36Sopenharmony_ci	pthread_mutex_t lock;
6162306a36Sopenharmony_ci} test_data_mpsc;
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_cistatic int mpsc_producer_thread(void *d)
6462306a36Sopenharmony_ci{
6562306a36Sopenharmony_ci	int id = (long)d;
6662306a36Sopenharmony_ci	int fence, valid, i;
6762306a36Sopenharmony_ci	int *producer_timelines = test_data_mpsc.producer_timelines;
6862306a36Sopenharmony_ci	int consumer_timeline = test_data_mpsc.consumer_timeline;
6962306a36Sopenharmony_ci	int iterations = test_data_mpsc.iterations;
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	for (i = 0; i < iterations; i++) {
7262306a36Sopenharmony_ci		fence = sw_sync_fence_create(consumer_timeline, "fence", i);
7362306a36Sopenharmony_ci		valid = sw_sync_fence_is_valid(fence);
7462306a36Sopenharmony_ci		ASSERT(valid, "Failure creating fence\n");
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci		/*
7762306a36Sopenharmony_ci		 * Wait for the consumer to finish. Use alternate
7862306a36Sopenharmony_ci		 * means of waiting on the fence
7962306a36Sopenharmony_ci		 */
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci		if ((iterations + id) % 8 != 0) {
8262306a36Sopenharmony_ci			ASSERT(sync_wait(fence, -1) > 0,
8362306a36Sopenharmony_ci			       "Failure waiting on fence\n");
8462306a36Sopenharmony_ci		} else {
8562306a36Sopenharmony_ci			ASSERT(busy_wait_on_fence(fence) == 0,
8662306a36Sopenharmony_ci			       "Failure waiting on fence\n");
8762306a36Sopenharmony_ci		}
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci		/*
9062306a36Sopenharmony_ci		 * Every producer increments the counter, the consumer
9162306a36Sopenharmony_ci		 * checks and erases it
9262306a36Sopenharmony_ci		 */
9362306a36Sopenharmony_ci		pthread_mutex_lock(&test_data_mpsc.lock);
9462306a36Sopenharmony_ci		test_data_mpsc.counter++;
9562306a36Sopenharmony_ci		pthread_mutex_unlock(&test_data_mpsc.lock);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci		ASSERT(sw_sync_timeline_inc(producer_timelines[id], 1) == 0,
9862306a36Sopenharmony_ci		       "Error advancing producer timeline\n");
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci		sw_sync_fence_destroy(fence);
10162306a36Sopenharmony_ci	}
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	return 0;
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cistatic int mpcs_consumer_thread(void)
10762306a36Sopenharmony_ci{
10862306a36Sopenharmony_ci	int fence, merged, tmp, valid, it, i;
10962306a36Sopenharmony_ci	int *producer_timelines = test_data_mpsc.producer_timelines;
11062306a36Sopenharmony_ci	int consumer_timeline = test_data_mpsc.consumer_timeline;
11162306a36Sopenharmony_ci	int iterations = test_data_mpsc.iterations;
11262306a36Sopenharmony_ci	int n = test_data_mpsc.threads;
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	for (it = 1; it <= iterations; it++) {
11562306a36Sopenharmony_ci		fence = sw_sync_fence_create(producer_timelines[0], "name", it);
11662306a36Sopenharmony_ci		for (i = 1; i < n; i++) {
11762306a36Sopenharmony_ci			tmp = sw_sync_fence_create(producer_timelines[i],
11862306a36Sopenharmony_ci						   "name", it);
11962306a36Sopenharmony_ci			merged = sync_merge("name", tmp, fence);
12062306a36Sopenharmony_ci			sw_sync_fence_destroy(tmp);
12162306a36Sopenharmony_ci			sw_sync_fence_destroy(fence);
12262306a36Sopenharmony_ci			fence = merged;
12362306a36Sopenharmony_ci		}
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci		valid = sw_sync_fence_is_valid(fence);
12662306a36Sopenharmony_ci		ASSERT(valid, "Failure merging fences\n");
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci		/*
12962306a36Sopenharmony_ci		 * Make sure we see an increment from every producer thread.
13062306a36Sopenharmony_ci		 * Vary the means by which we wait.
13162306a36Sopenharmony_ci		 */
13262306a36Sopenharmony_ci		if (iterations % 8 != 0) {
13362306a36Sopenharmony_ci			ASSERT(sync_wait(fence, -1) > 0,
13462306a36Sopenharmony_ci			       "Producers did not increment as expected\n");
13562306a36Sopenharmony_ci		} else {
13662306a36Sopenharmony_ci			ASSERT(busy_wait_on_fence(fence) == 0,
13762306a36Sopenharmony_ci			       "Producers did not increment as expected\n");
13862306a36Sopenharmony_ci		}
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci		ASSERT(test_data_mpsc.counter == n * it,
14162306a36Sopenharmony_ci		       "Counter value mismatch!\n");
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci		/* Release the producer threads */
14462306a36Sopenharmony_ci		ASSERT(sw_sync_timeline_inc(consumer_timeline, 1) == 0,
14562306a36Sopenharmony_ci		       "Failure releasing producer threads\n");
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci		sw_sync_fence_destroy(fence);
14862306a36Sopenharmony_ci	}
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	return 0;
15162306a36Sopenharmony_ci}
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ciint test_consumer_stress_multi_producer_single_consumer(void)
15462306a36Sopenharmony_ci{
15562306a36Sopenharmony_ci	int iterations = 1 << 12;
15662306a36Sopenharmony_ci	int n = 5;
15762306a36Sopenharmony_ci	long i, ret;
15862306a36Sopenharmony_ci	int producer_timelines[n];
15962306a36Sopenharmony_ci	int consumer_timeline;
16062306a36Sopenharmony_ci	pthread_t threads[n];
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	consumer_timeline = sw_sync_timeline_create();
16362306a36Sopenharmony_ci	for (i = 0; i < n; i++)
16462306a36Sopenharmony_ci		producer_timelines[i] = sw_sync_timeline_create();
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	test_data_mpsc.producer_timelines = producer_timelines;
16762306a36Sopenharmony_ci	test_data_mpsc.consumer_timeline = consumer_timeline;
16862306a36Sopenharmony_ci	test_data_mpsc.iterations = iterations;
16962306a36Sopenharmony_ci	test_data_mpsc.threads = n;
17062306a36Sopenharmony_ci	test_data_mpsc.counter = 0;
17162306a36Sopenharmony_ci	pthread_mutex_init(&test_data_mpsc.lock, NULL);
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	for (i = 0; i < n; i++) {
17462306a36Sopenharmony_ci		pthread_create(&threads[i], NULL, (void * (*)(void *))
17562306a36Sopenharmony_ci			       mpsc_producer_thread, (void *)i);
17662306a36Sopenharmony_ci	}
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	/* Consumer thread runs here */
17962306a36Sopenharmony_ci	ret = mpcs_consumer_thread();
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	for (i = 0; i < n; i++)
18262306a36Sopenharmony_ci		pthread_join(threads[i], NULL);
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci	return ret;
18562306a36Sopenharmony_ci}
186