18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * sync stress test: producer/consumer 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 <pthread.h> 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#include "sync.h" 318c2ecf20Sopenharmony_ci#include "sw_sync.h" 328c2ecf20Sopenharmony_ci#include "synctest.h" 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci/* IMPORTANT NOTE: if you see this test failing on your system, it may be 358c2ecf20Sopenharmony_ci * due to a shortage of file descriptors. Please ensure your system has 368c2ecf20Sopenharmony_ci * a sensible limit for this test to finish correctly. 378c2ecf20Sopenharmony_ci */ 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci/* Returns 1 on error, 0 on success */ 408c2ecf20Sopenharmony_cistatic int busy_wait_on_fence(int fence) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci int error, active; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci do { 458c2ecf20Sopenharmony_ci error = sync_fence_count_with_status(fence, FENCE_STATUS_ERROR); 468c2ecf20Sopenharmony_ci ASSERT(error == 0, "Error occurred on fence\n"); 478c2ecf20Sopenharmony_ci active = sync_fence_count_with_status(fence, 488c2ecf20Sopenharmony_ci FENCE_STATUS_ACTIVE); 498c2ecf20Sopenharmony_ci } while (active); 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci return 0; 528c2ecf20Sopenharmony_ci} 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_cistatic struct { 558c2ecf20Sopenharmony_ci int iterations; 568c2ecf20Sopenharmony_ci int threads; 578c2ecf20Sopenharmony_ci int counter; 588c2ecf20Sopenharmony_ci int consumer_timeline; 598c2ecf20Sopenharmony_ci int *producer_timelines; 608c2ecf20Sopenharmony_ci pthread_mutex_t lock; 618c2ecf20Sopenharmony_ci} test_data_mpsc; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_cistatic int mpsc_producer_thread(void *d) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci int id = (long)d; 668c2ecf20Sopenharmony_ci int fence, valid, i; 678c2ecf20Sopenharmony_ci int *producer_timelines = test_data_mpsc.producer_timelines; 688c2ecf20Sopenharmony_ci int consumer_timeline = test_data_mpsc.consumer_timeline; 698c2ecf20Sopenharmony_ci int iterations = test_data_mpsc.iterations; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci for (i = 0; i < iterations; i++) { 728c2ecf20Sopenharmony_ci fence = sw_sync_fence_create(consumer_timeline, "fence", i); 738c2ecf20Sopenharmony_ci valid = sw_sync_fence_is_valid(fence); 748c2ecf20Sopenharmony_ci ASSERT(valid, "Failure creating fence\n"); 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci /* 778c2ecf20Sopenharmony_ci * Wait for the consumer to finish. Use alternate 788c2ecf20Sopenharmony_ci * means of waiting on the fence 798c2ecf20Sopenharmony_ci */ 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci if ((iterations + id) % 8 != 0) { 828c2ecf20Sopenharmony_ci ASSERT(sync_wait(fence, -1) > 0, 838c2ecf20Sopenharmony_ci "Failure waiting on fence\n"); 848c2ecf20Sopenharmony_ci } else { 858c2ecf20Sopenharmony_ci ASSERT(busy_wait_on_fence(fence) == 0, 868c2ecf20Sopenharmony_ci "Failure waiting on fence\n"); 878c2ecf20Sopenharmony_ci } 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci /* 908c2ecf20Sopenharmony_ci * Every producer increments the counter, the consumer 918c2ecf20Sopenharmony_ci * checks and erases it 928c2ecf20Sopenharmony_ci */ 938c2ecf20Sopenharmony_ci pthread_mutex_lock(&test_data_mpsc.lock); 948c2ecf20Sopenharmony_ci test_data_mpsc.counter++; 958c2ecf20Sopenharmony_ci pthread_mutex_unlock(&test_data_mpsc.lock); 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci ASSERT(sw_sync_timeline_inc(producer_timelines[id], 1) == 0, 988c2ecf20Sopenharmony_ci "Error advancing producer timeline\n"); 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci sw_sync_fence_destroy(fence); 1018c2ecf20Sopenharmony_ci } 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci return 0; 1048c2ecf20Sopenharmony_ci} 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_cistatic int mpcs_consumer_thread(void) 1078c2ecf20Sopenharmony_ci{ 1088c2ecf20Sopenharmony_ci int fence, merged, tmp, valid, it, i; 1098c2ecf20Sopenharmony_ci int *producer_timelines = test_data_mpsc.producer_timelines; 1108c2ecf20Sopenharmony_ci int consumer_timeline = test_data_mpsc.consumer_timeline; 1118c2ecf20Sopenharmony_ci int iterations = test_data_mpsc.iterations; 1128c2ecf20Sopenharmony_ci int n = test_data_mpsc.threads; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci for (it = 1; it <= iterations; it++) { 1158c2ecf20Sopenharmony_ci fence = sw_sync_fence_create(producer_timelines[0], "name", it); 1168c2ecf20Sopenharmony_ci for (i = 1; i < n; i++) { 1178c2ecf20Sopenharmony_ci tmp = sw_sync_fence_create(producer_timelines[i], 1188c2ecf20Sopenharmony_ci "name", it); 1198c2ecf20Sopenharmony_ci merged = sync_merge("name", tmp, fence); 1208c2ecf20Sopenharmony_ci sw_sync_fence_destroy(tmp); 1218c2ecf20Sopenharmony_ci sw_sync_fence_destroy(fence); 1228c2ecf20Sopenharmony_ci fence = merged; 1238c2ecf20Sopenharmony_ci } 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci valid = sw_sync_fence_is_valid(fence); 1268c2ecf20Sopenharmony_ci ASSERT(valid, "Failure merging fences\n"); 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci /* 1298c2ecf20Sopenharmony_ci * Make sure we see an increment from every producer thread. 1308c2ecf20Sopenharmony_ci * Vary the means by which we wait. 1318c2ecf20Sopenharmony_ci */ 1328c2ecf20Sopenharmony_ci if (iterations % 8 != 0) { 1338c2ecf20Sopenharmony_ci ASSERT(sync_wait(fence, -1) > 0, 1348c2ecf20Sopenharmony_ci "Producers did not increment as expected\n"); 1358c2ecf20Sopenharmony_ci } else { 1368c2ecf20Sopenharmony_ci ASSERT(busy_wait_on_fence(fence) == 0, 1378c2ecf20Sopenharmony_ci "Producers did not increment as expected\n"); 1388c2ecf20Sopenharmony_ci } 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci ASSERT(test_data_mpsc.counter == n * it, 1418c2ecf20Sopenharmony_ci "Counter value mismatch!\n"); 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci /* Release the producer threads */ 1448c2ecf20Sopenharmony_ci ASSERT(sw_sync_timeline_inc(consumer_timeline, 1) == 0, 1458c2ecf20Sopenharmony_ci "Failure releasing producer threads\n"); 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci sw_sync_fence_destroy(fence); 1488c2ecf20Sopenharmony_ci } 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci return 0; 1518c2ecf20Sopenharmony_ci} 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ciint test_consumer_stress_multi_producer_single_consumer(void) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci int iterations = 1 << 12; 1568c2ecf20Sopenharmony_ci int n = 5; 1578c2ecf20Sopenharmony_ci long i, ret; 1588c2ecf20Sopenharmony_ci int producer_timelines[n]; 1598c2ecf20Sopenharmony_ci int consumer_timeline; 1608c2ecf20Sopenharmony_ci pthread_t threads[n]; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci consumer_timeline = sw_sync_timeline_create(); 1638c2ecf20Sopenharmony_ci for (i = 0; i < n; i++) 1648c2ecf20Sopenharmony_ci producer_timelines[i] = sw_sync_timeline_create(); 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci test_data_mpsc.producer_timelines = producer_timelines; 1678c2ecf20Sopenharmony_ci test_data_mpsc.consumer_timeline = consumer_timeline; 1688c2ecf20Sopenharmony_ci test_data_mpsc.iterations = iterations; 1698c2ecf20Sopenharmony_ci test_data_mpsc.threads = n; 1708c2ecf20Sopenharmony_ci test_data_mpsc.counter = 0; 1718c2ecf20Sopenharmony_ci pthread_mutex_init(&test_data_mpsc.lock, NULL); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci for (i = 0; i < n; i++) { 1748c2ecf20Sopenharmony_ci pthread_create(&threads[i], NULL, (void * (*)(void *)) 1758c2ecf20Sopenharmony_ci mpsc_producer_thread, (void *)i); 1768c2ecf20Sopenharmony_ci } 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci /* Consumer thread runs here */ 1798c2ecf20Sopenharmony_ci ret = mpcs_consumer_thread(); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci for (i = 0; i < n; i++) 1828c2ecf20Sopenharmony_ci pthread_join(threads[i], NULL); 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci return ret; 1858c2ecf20Sopenharmony_ci} 186