• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #![allow(dead_code)]
15 use std::hint::black_box;
16 use std::sync::Arc;
17 
18 use tokio::sync::RwLock;
19 #[cfg(feature = "multi_instance_runtime")]
20 use ylong_runtime::builder::RuntimeBuilder;
21 #[cfg(feature = "multi_instance_runtime")]
22 use ylong_runtime::executor::Runtime;
23 #[cfg(feature = "sync")]
24 use ylong_runtime::sync::RwLock as YlongRwlock;
25 
26 pub const TASK_NUM: usize = 10;
27 pub const FIBBO_ANS: u64 = 1346268;
28 
29 const BUFFER_SIZE: usize = 4096;
30 pub const READ_FILE: &str = "/dev/zero";
31 pub const WRITE_FILE: &str = "/dev/null";
32 pub static mut READ_BUFFER: [u8; BUFFER_SIZE] = [0_u8; BUFFER_SIZE];
33 pub static mut WRITE_BUFFER: [u8; BUFFER_SIZE] = [0_u8; BUFFER_SIZE];
34 
35 pub const YIELD_NUM: usize = 1_000;
36 
37 pub type FA<R> = fn() -> R;
38 
39 #[cfg(feature = "multi_instance_runtime")]
ylong_runtime() -> Runtime40 pub fn ylong_runtime() -> Runtime {
41     RuntimeBuilder::new_multi_thread().build().unwrap()
42 }
43 
44 #[cfg(feature = "multi_instance_runtime")]
ylong_runtime_set_threads(threads_count: usize) -> Runtime45 pub fn ylong_runtime_set_threads(threads_count: usize) -> Runtime {
46     RuntimeBuilder::new_multi_thread()
47         .worker_num(threads_count)
48         .build()
49         .unwrap()
50 }
51 
tokio_runtime() -> tokio::runtime::Runtime52 pub fn tokio_runtime() -> tokio::runtime::Runtime {
53     tokio::runtime::Builder::new_multi_thread()
54         .enable_all()
55         .build()
56         .unwrap()
57 }
58 
tokio_runtime_set_threads(threads_count: usize) -> tokio::runtime::Runtime59 pub fn tokio_runtime_set_threads(threads_count: usize) -> tokio::runtime::Runtime {
60     tokio::runtime::Builder::new_multi_thread()
61         .worker_threads(threads_count)
62         .enable_all()
63         .build()
64         .unwrap()
65 }
66 
work() -> u6467 pub fn work() -> u64 {
68     black_box(3)
69 }
70 
recur_fibbo(a: u64) -> u6471 pub fn recur_fibbo(a: u64) -> u64 {
72     if a == 0 || a == 1 {
73         return a;
74     }
75     recur_fibbo(a - 1) + recur_fibbo(a - 2)
76 }
77 
fibbo(upper: usize) -> u6478 pub fn fibbo(upper: usize) -> u64 {
79     let mut ret = 0;
80     for j in 1..upper {
81         ret += black_box(recur_fibbo(j as u64));
82     }
83     black_box(ret)
84 }
85 
init_write_buffer()86 pub fn init_write_buffer() {
87     let mut buffer = [0; BUFFER_SIZE];
88     for (i, tar) in buffer.iter_mut().enumerate().take(BUFFER_SIZE) {
89         *tar = (i % 256) as u8;
90     }
91     unsafe {
92         WRITE_BUFFER
93             .iter_mut()
94             .zip(buffer.as_slice().iter())
95             .for_each(|(tar, src)| *tar = *src);
96     }
97 }
98 
99 #[cfg(feature = "sync")]
ylong_rwlock_write_task(lock: Arc<YlongRwlock<()>>)100 pub async fn ylong_rwlock_write_task(lock: Arc<YlongRwlock<()>>) {
101     let _read = lock.write().await;
102 }
103 
104 #[cfg(feature = "sync")]
ylong_rwlock_task(lock: Arc<YlongRwlock<()>>)105 pub async fn ylong_rwlock_task(lock: Arc<YlongRwlock<()>>) {
106     let _read = lock.read().await;
107 }
108 
tokio_rwlock_write_task(lock: Arc<RwLock<()>>)109 pub async fn tokio_rwlock_write_task(lock: Arc<RwLock<()>>) {
110     let _read = lock.write().await;
111 }
112 
tokio_rwlock_task(lock: Arc<RwLock<()>>)113 pub async fn tokio_rwlock_task(lock: Arc<RwLock<()>>) {
114     let _read = lock.read().await;
115 }
116