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 FFRT_TESTCASE_UTIL_HPP
17 #define FFRT_TESTCASE_UTIL_HPP
18
19 #include <chrono>
20 #include <functional>
21
stall_us(size_t us)22 static inline void stall_us(size_t us)
23 {
24 auto start = std::chrono::steady_clock::now();
25 size_t passed = 0;
26 while (passed < us) {
27 passed =
28 std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count();
29 }
30 }
31 /**
32 * @brief Get the proc memory
33 *
34 * @param pid
35 * @return uint32_t KB
36 */
37 uint32_t get_proc_memory(pid_t pid); // KB
38
39 void set_cur_process_cpu_affinity(int cpuid);
40
41 void set_cur_thread_cpu_affinity(int cpuid);
42
43 /*
44 * perf event counter
45 */
46 struct perf_event2_read_format {
47 uint64_t nr; // counter num=2
48 uint64_t values[2]; // counter value
49 };
50 int perf_event2(std::function<void()> func, struct perf_event2_read_format& data, uint32_t event1, uint32_t event2);
51
52 int perf_single_event(std::function<void()> func, size_t& count, uint32_t event);
53
54 int perf_event_instructions(std::function<void()> func, size_t& count);
55
56 int perf_event_cycles(std::function<void()> func, size_t& count);
57
58 int perf_event_branch_instructions(std::function<void()> func, size_t& count);
59
60 int perf_event_branch_misses(std::function<void()> func, size_t& count);
61
62 #endif