1484543d1Sopenharmony_ci/* 2484543d1Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3484543d1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4484543d1Sopenharmony_ci * you may not use this file except in compliance with the License. 5484543d1Sopenharmony_ci * You may obtain a copy of the License at 6484543d1Sopenharmony_ci * 7484543d1Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8484543d1Sopenharmony_ci * 9484543d1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10484543d1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11484543d1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12484543d1Sopenharmony_ci * See the License for the specific language governing permissions and 13484543d1Sopenharmony_ci * limitations under the License. 14484543d1Sopenharmony_ci */ 15484543d1Sopenharmony_ci 16484543d1Sopenharmony_ci#ifndef BENCHMARKS_COMMON 17484543d1Sopenharmony_ci#define BENCHMARKS_COMMON 18484543d1Sopenharmony_ci 19484543d1Sopenharmony_ci#include <chrono> 20484543d1Sopenharmony_ci#include <cstdlib> 21484543d1Sopenharmony_ci#include <inttypes.h> 22484543d1Sopenharmony_ci#include "ffrt_inner.h" 23484543d1Sopenharmony_ci 24484543d1Sopenharmony_cisize_t COMPUTE_TIME_US = 0; 25484543d1Sopenharmony_ciuint64_t REPEAT = 1; 26484543d1Sopenharmony_ciuint64_t PREHOT_FFRT = 1; 27484543d1Sopenharmony_ciuint64_t FIB_NUM = 0; 28484543d1Sopenharmony_ci 29484543d1Sopenharmony_ci#define CLOCK std::chrono::steady_clock::now() 30484543d1Sopenharmony_ci#define TIME_BEGIN(t) auto __##t##_start = CLOCK 31484543d1Sopenharmony_ci#define TIME_END(t) \ 32484543d1Sopenharmony_ci do { \ 33484543d1Sopenharmony_ci decltype(__##t##_start) __##t##_cur = CLOCK; \ 34484543d1Sopenharmony_ci ("%-12s:%-4d %6lu us\n", __FILE__, __LINE__, \ 35484543d1Sopenharmony_ci long(std::chrono::duration_cast<std::chrono::microseconds>(__##t##_cur - __##t##_start).count())); \ 36484543d1Sopenharmony_ci } while (0) 37484543d1Sopenharmony_ci#define TIME_END_INFO(t, info) \ 38484543d1Sopenharmony_ci do { \ 39484543d1Sopenharmony_ci decltype(__##t##_start) __##t##_cur = CLOCK; \ 40484543d1Sopenharmony_ci printf("%-12s:%-4d %s %6lu us\n", __FILE__, __LINE__, info, \ 41484543d1Sopenharmony_ci long(std::chrono::duration_cast<std::chrono::microseconds>(__##t##_cur - __##t##_start).count())); \ 42484543d1Sopenharmony_ci } while (0) 43484543d1Sopenharmony_ci 44484543d1Sopenharmony_ci#define GET_ENV(name, var, default) \ 45484543d1Sopenharmony_ci do { \ 46484543d1Sopenharmony_ci auto __get_env_##name = getenv(#name); \ 47484543d1Sopenharmony_ci var = __get_env_##name ? atoi(__get_env_##name) : default; \ 48484543d1Sopenharmony_ci printf(#name " = %" PRIu64 "\n", (uint64_t)(var)); \ 49484543d1Sopenharmony_ci } while (0) 50484543d1Sopenharmony_ci 51484543d1Sopenharmony_ci#define EXPECT(cond) \ 52484543d1Sopenharmony_ci if (!(cond)) { \ 53484543d1Sopenharmony_ci printf(#cond " check failed\n"); \ 54484543d1Sopenharmony_ci } 55484543d1Sopenharmony_ci 56484543d1Sopenharmony_cistatic inline void simulate_task_compute_time(size_t us) 57484543d1Sopenharmony_ci{ 58484543d1Sopenharmony_ci auto start = std::chrono::steady_clock::now(); 59484543d1Sopenharmony_ci size_t passed = 0; 60484543d1Sopenharmony_ci while (passed < us) { 61484543d1Sopenharmony_ci passed = 62484543d1Sopenharmony_ci std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count(); 63484543d1Sopenharmony_ci } 64484543d1Sopenharmony_ci} 65484543d1Sopenharmony_ci 66484543d1Sopenharmony_cistatic void FibFFRT(int x, int& y) 67484543d1Sopenharmony_ci{ 68484543d1Sopenharmony_ci if (x <= 1) { 69484543d1Sopenharmony_ci y = x; 70484543d1Sopenharmony_ci } else { 71484543d1Sopenharmony_ci int y1; 72484543d1Sopenharmony_ci int y2; 73484543d1Sopenharmony_ci ffrt::submit([&]() { FibFFRT(x - 1, y1); }, {&x}, {&y1}); 74484543d1Sopenharmony_ci ffrt::submit([&]() { FibFFRT(x - 2, y2); }, {&x}, {&y2}); 75484543d1Sopenharmony_ci ffrt::wait({&y1, &y2}); 76484543d1Sopenharmony_ci y = y1 + y2; 77484543d1Sopenharmony_ci } 78484543d1Sopenharmony_ci simulate_task_compute_time(COMPUTE_TIME_US); 79484543d1Sopenharmony_ci} 80484543d1Sopenharmony_ci 81484543d1Sopenharmony_civoid PreHotFFRT(void) 82484543d1Sopenharmony_ci{ 83484543d1Sopenharmony_ci if (!PREHOT_FFRT) { 84484543d1Sopenharmony_ci return; 85484543d1Sopenharmony_ci } 86484543d1Sopenharmony_ci int output; 87484543d1Sopenharmony_ci ffrt::submit([&]() { FibFFRT(20, output); }, {}, {&output}); 88484543d1Sopenharmony_ci ffrt::wait({&output}); 89484543d1Sopenharmony_ci} 90484543d1Sopenharmony_ci 91484543d1Sopenharmony_civoid GetEnvs(void) 92484543d1Sopenharmony_ci{ 93484543d1Sopenharmony_ci GET_ENV(COMPUTE_TIME_US, COMPUTE_TIME_US, 0); 94484543d1Sopenharmony_ci GET_ENV(REPEAT, REPEAT, 1); 95484543d1Sopenharmony_ci GET_ENV(PREHOT_FFRT, PREHOT_FFRT, 0); 96484543d1Sopenharmony_ci GET_ENV(FIB_NUM, FIB_NUM, 5); 97484543d1Sopenharmony_ci} 98484543d1Sopenharmony_ci 99484543d1Sopenharmony_cistatic inline void completely_paralle(uint32_t count, uint32_t duration, int64_t& time) 100484543d1Sopenharmony_ci{ 101484543d1Sopenharmony_ci uint32_t loop = count; 102484543d1Sopenharmony_ci auto start = std::chrono::steady_clock::now(); 103484543d1Sopenharmony_ci while (loop--) { 104484543d1Sopenharmony_ci ffrt::submit([&]() { simulate_task_compute_time(duration); }, {}, {}); 105484543d1Sopenharmony_ci } 106484543d1Sopenharmony_ci ffrt::wait(); 107484543d1Sopenharmony_ci time = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count(); 108484543d1Sopenharmony_ci} 109484543d1Sopenharmony_ci 110484543d1Sopenharmony_cistatic inline void completely_serial(uint32_t count, uint32_t duration, int64_t& time) 111484543d1Sopenharmony_ci{ 112484543d1Sopenharmony_ci uint32_t x = 0; 113484543d1Sopenharmony_ci uint32_t loop = count; 114484543d1Sopenharmony_ci auto start = std::chrono::steady_clock::now(); 115484543d1Sopenharmony_ci while (loop--) { 116484543d1Sopenharmony_ci ffrt::submit([&]() { simulate_task_compute_time(duration); }, {&x}, {&x}); 117484543d1Sopenharmony_ci } 118484543d1Sopenharmony_ci ffrt::wait(); 119484543d1Sopenharmony_ci time = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count(); 120484543d1Sopenharmony_ci} 121484543d1Sopenharmony_cistatic inline void single_thread(uint32_t count, uint32_t duration, int64_t& time) 122484543d1Sopenharmony_ci{ 123484543d1Sopenharmony_ci uint32_t loop = count; 124484543d1Sopenharmony_ci auto start = std::chrono::steady_clock::now(); 125484543d1Sopenharmony_ci while (loop--) { 126484543d1Sopenharmony_ci simulate_task_compute_time(duration); 127484543d1Sopenharmony_ci } 128484543d1Sopenharmony_ci time = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count(); 129484543d1Sopenharmony_ci} 130484543d1Sopenharmony_ci#endif