1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved. 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 #include "sched.h" 17 #include "cstdlib" 18 #include "unistd.h" 19 #include "util.h" 20 21 constexpr long MALLOC_SIZE = (1024 * 8192); 22 Bm_function_sched_yield(benchmark::State & state)23static void Bm_function_sched_yield(benchmark::State &state) 24 { 25 for (auto _ : state) { 26 benchmark::DoNotOptimize(sched_yield()); 27 } 28 } 29 ThreadWaitFunc(void * arg)30int ThreadWaitFunc(void* arg) 31 { 32 return 0; 33 } 34 35 // Used to create a new process Bm_function_Clone(benchmark::State & state)36static void Bm_function_Clone(benchmark::State &state) 37 { 38 void *stack = malloc(MALLOC_SIZE); 39 if (stack == nullptr) { 40 perror("malloc clone"); 41 } 42 int flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM | CLONE_SETTLS; 43 int pid = -1; 44 for (auto _ : state) { 45 pid = clone(ThreadWaitFunc, (char*)stack + MALLOC_SIZE, flags, nullptr); 46 if (pid == -1) { 47 perror("clone proc"); 48 } 49 sleep(1); 50 } 51 free(stack); 52 } 53 54 MUSL_BENCHMARK(Bm_function_sched_yield); 55 MUSL_BENCHMARK(Bm_function_Clone);