1 /* 2 Copyright (c) 2020 Intel Corporation 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 #include "common/test.h" 18 #include "common/utils.h" 19 #include "common/cpu_usertime.h" 20 #include "common/utils_concurrency_limit.h" 21 #include "common/parallel_invoke_common.h" 22 #include "common/memory_usage.h" 23 24 #include <cstddef> 25 #include <atomic> 26 27 //! \file test_parallel_invoke.cpp 28 //! \brief Test for [algorithms.parallel_invoke] 29 30 //! Testing parallel_invoke memory usage 31 //! \brief \ref resource_usage \ref stress 32 TEST_CASE("Test memory leaks") { 33 std::size_t number_of_measurements = 500; 34 std::size_t current_memory_usage = 0, max_memory_usage = 0, stability_counter=0; 35 36 // Limit concurrency to prevent extra allocations not dependent on algorithm behavior 37 auto concurrency_limit = utils::get_platform_max_threads() < 8 ? utils::get_platform_max_threads() : 8; 38 tbb::global_control control(tbb::global_control::max_allowed_parallelism, concurrency_limit); 39 40 for (std::size_t i = 0; i < number_of_measurements; i++) { 41 { 42 // ~45000 workload tasks 43 invoke_tree</*LevelTaskCount*/6, /*Depth*/6, /*WorkSize*/10>::generate_and_run(); 44 } 45 46 current_memory_usage = utils::GetMemoryUsage(); 47 if (current_memory_usage > max_memory_usage) { 48 stability_counter = 0; 49 max_memory_usage = current_memory_usage; 50 } else { 51 stability_counter++; 52 } 53 // If the amount of used memory has not changed during 10% of executions, 54 // then we can assume that the check was successful 55 if (stability_counter > number_of_measurements / 10) return; 56 } 57 REQUIRE_MESSAGE(false, "Seems like we get memory leak here."); 58 } 59 60 template<typename Body> 61 void test_from_2_to_10_arguments(const Body& body, const std::atomic<std::size_t>& counter) { 62 tbb::parallel_invoke(body, body); 63 tbb::parallel_invoke(body, body, body); 64 tbb::parallel_invoke(body, body, body, body); 65 tbb::parallel_invoke(body, body, body, body, body); 66 tbb::parallel_invoke(body, body, body, body, body, body); 67 tbb::parallel_invoke(body, body, body, body, body, body, body); 68 tbb::parallel_invoke(body, body, body, body, body, body, body, body); 69 tbb::parallel_invoke(body, body, body, body, body, body, body, body, body); 70 tbb::parallel_invoke(body, body, body, body, body, body, body, body, body, body); 71 72 REQUIRE_MESSAGE(counter == (2 + 10) * 9 / 2, 73 "Parallel invoke correctness was broken during lambda support test execution."); 74 } 75 76 //! Testing lambdas support 77 //! \brief \ref error_guessing 78 TEST_CASE("Test lambda support") { 79 std::atomic<std::size_t> lambda_counter{0}; 80 auto body = [&]{ lambda_counter++; }; 81 82 test_from_2_to_10_arguments(body, lambda_counter); 83 } 84 85 std::atomic<std::size_t> func_counter{0}; 86 void func() { func_counter++; }; 87 88 //! Testing function pointers support 89 //! \brief \ref error_guessing 90 TEST_CASE("Test function pointers support") { 91 auto func_ptr = &func; 92 test_from_2_to_10_arguments(func_ptr, func_counter); 93 } 94 95 //! Testing workers going to sleep 96 //! \brief \ref error_guessing 97 TEST_CASE("Test that all workers sleep when no work") { 98 invoke_tree</*LevelTaskCount*/9, /*Depth*/6, /*WorkSize*/10>::generate_and_run(); 99 TestCPUUserTime(utils::get_platform_max_threads()); 100 } 101