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 "csignal"
17 #include "util.h"
18 
SignalHandler(int i)19 static void SignalHandler(int i) {}
20 
Bm_function_Sigaction(benchmark::State &state)21 static void Bm_function_Sigaction(benchmark::State &state)
22 {
23     struct sigaction sa;
24     sa.sa_flags = 0;
25     sa.sa_handler = SignalHandler;
26 
27     for (auto _ : state) {
28         benchmark::DoNotOptimize(sigaction(SIGUSR1, &sa, nullptr));
29     }
30 }
31 
Bm_function_Sigemptyset(benchmark::State &state)32 static void Bm_function_Sigemptyset(benchmark::State &state)
33 {
34     for (auto _ : state) {
35         sigset_t set;
36         benchmark::DoNotOptimize(sigemptyset(&set));
37     }
38 }
39 
Bm_function_Sigaltstack(benchmark::State &state)40 static void Bm_function_Sigaltstack(benchmark::State &state)
41 {
42     for (auto _ : state) {
43         state.PauseTiming();
44         stack_t ss, old_ss;
45         ss.ss_sp = malloc(SIGSTKSZ);
46         if (ss.ss_sp == nullptr) {
47             perror("malloc");
48         }
49 
50         ss.ss_size = SIGSTKSZ;
51         ss.ss_flags = 0;
52         state.ResumeTiming();
53         if (sigaltstack(&ss, &old_ss) == -1) {
54             perror("sigaltstack");
55         }
56 
57         state.PauseTiming();
58         if (sigaltstack(&old_ss, nullptr) == -1) {
59             perror("sigaltstack");
60         }
61         free(ss.ss_sp);
62         state.ResumeTiming();
63     }
64 }
65 
Bm_function_Sigdelset(benchmark::State &state)66 static void Bm_function_Sigdelset(benchmark::State &state)
67 {
68     sigset_t set;
69     for (auto _ : state) {
70         sigaddset(&set, SIGINT);
71         sigdelset(&set, SIGINT);
72     }
73 }
74 
Bm_function_Sigtimedwait(benchmark::State &state)75 static void Bm_function_Sigtimedwait(benchmark::State &state)
76 {
77     sigset_t set;
78     sigemptyset(&set);
79     sigaddset(&set, SIGUSR1);
80     sigaddset(&set, SIGTERM);
81     siginfo_t info{0};
82     timespec ts;
83     ts.tv_sec = 0;
84     ts.tv_nsec = 0;
85     for (auto _ : state) {
86         benchmark::DoNotOptimize(sigtimedwait(&set, &info, &ts));
87     }
88 }
89 
90 MUSL_BENCHMARK(Bm_function_Sigaction);
91 MUSL_BENCHMARK(Bm_function_Sigemptyset);
92 MUSL_BENCHMARK(Bm_function_Sigaltstack);
93 MUSL_BENCHMARK(Bm_function_Sigdelset);
94 MUSL_BENCHMARK(Bm_function_Sigtimedwait);
95