1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef BENCHMARKS_COMMON
17 #define BENCHMARKS_COMMON
18
19 #include <chrono>
20 #include <cstdlib>
21 #include <inttypes.h>
22 #include "ffrt_inner.h"
23
24 size_t COMPUTE_TIME_US = 0;
25 uint64_t REPEAT = 1;
26 uint64_t PREHOT_FFRT = 1;
27 uint64_t FIB_NUM = 0;
28
29 #define CLOCK std::chrono::steady_clock::now()
30 #define TIME_BEGIN(t) auto __##t##_start = CLOCK
31 #define TIME_END_INFO(t, info) \
32 do { \
33 decltype(__##t##_start) __##t##_cur = CLOCK; \
34 printf("%-4d %s %6lu us\n", __LINE__, info, \
35 long(std::chrono::duration_cast<std::chrono::microseconds>(__##t##_cur - __##t##_start).count())); \
36 } while (0)
37
38 #define GET_ENV(name, var, default) \
39 do { \
40 auto __get_env_##name = getenv(#name); \
41 var = __get_env_##name ? atoi(__get_env_##name) : default; \
42 printf(#name " = %" PRIu64 "\n", (uint64_t)(var)); \
43 } while (0)
44
45 #define EXPECT(cond) \
46 if (!(cond)) { \
47 printf(#cond " check failed\n"); \
48 }
49
simulate_task_compute_time(size_t us)50 static inline void simulate_task_compute_time(size_t us)
51 {
52 auto start = std::chrono::steady_clock::now();
53 size_t passed = 0;
54 while (passed < us) {
55 passed =
56 std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count();
57 }
58 }
59
FibFFRT(int x,int & y)60 static void FibFFRT(int x, int& y)
61 {
62 if (x <= 1) {
63 y = x;
64 } else {
65 int y1;
66 int y2;
67 ffrt::submit([&]() { FibFFRT(x - 1, y1); }, {&x}, {&y1});
68 ffrt::submit([&]() { FibFFRT(x - 2, y2); }, {&x}, {&y2});
69 ffrt::wait({&y1, &y2});
70 y = y1 + y2;
71 }
72 simulate_task_compute_time(COMPUTE_TIME_US);
73 }
74
PreHotFFRT(void)75 void PreHotFFRT(void)
76 {
77 if (!PREHOT_FFRT) {
78 return;
79 }
80 int output;
81 ffrt::submit([&]() { FibFFRT(20, output); }, {}, {&output});
82 ffrt::wait({&output});
83 }
84
GetEnvs(void)85 void GetEnvs(void)
86 {
87 GET_ENV(COMPUTE_TIME_US, COMPUTE_TIME_US, 0);
88 GET_ENV(REPEAT, REPEAT, 1);
89 GET_ENV(PREHOT_FFRT, PREHOT_FFRT, 0);
90 GET_ENV(FIB_NUM, FIB_NUM, 5);
91 }
92
completely_paralle(uint32_t count,uint32_t duration,int64_t & time)93 static inline void completely_paralle(uint32_t count, uint32_t duration, int64_t& time)
94 {
95 uint32_t loop = count;
96 auto start = std::chrono::steady_clock::now();
97 while (loop--) {
98 ffrt::submit([&]() { simulate_task_compute_time(duration); }, {}, {});
99 }
100 ffrt::wait();
101 time = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count();
102 }
103
completely_serial(uint32_t count,uint32_t duration,int64_t & time)104 static inline void completely_serial(uint32_t count, uint32_t duration, int64_t& time)
105 {
106 uint32_t x = 0;
107 uint32_t loop = count;
108 auto start = std::chrono::steady_clock::now();
109 while (loop--) {
110 ffrt::submit([&]() { simulate_task_compute_time(duration); }, {&x}, {&x});
111 }
112 ffrt::wait();
113 time = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count();
114 }
single_thread(uint32_t count,uint32_t duration,int64_t & time)115 static inline void single_thread(uint32_t count, uint32_t duration, int64_t& time)
116 {
117 uint32_t loop = count;
118 auto start = std::chrono::steady_clock::now();
119 while (loop--) {
120 simulate_task_compute_time(duration);
121 }
122 time = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count();
123 }
124 #endif