1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include "csignal"
17570af302Sopenharmony_ci#include "util.h"
18570af302Sopenharmony_ci
19570af302Sopenharmony_cistatic void SignalHandler(int i) {}
20570af302Sopenharmony_ci
21570af302Sopenharmony_cistatic void Bm_function_Sigaction(benchmark::State &state)
22570af302Sopenharmony_ci{
23570af302Sopenharmony_ci    struct sigaction sa;
24570af302Sopenharmony_ci    sa.sa_flags = 0;
25570af302Sopenharmony_ci    sa.sa_handler = SignalHandler;
26570af302Sopenharmony_ci
27570af302Sopenharmony_ci    for (auto _ : state) {
28570af302Sopenharmony_ci        benchmark::DoNotOptimize(sigaction(SIGUSR1, &sa, nullptr));
29570af302Sopenharmony_ci    }
30570af302Sopenharmony_ci}
31570af302Sopenharmony_ci
32570af302Sopenharmony_cistatic void Bm_function_Sigemptyset(benchmark::State &state)
33570af302Sopenharmony_ci{
34570af302Sopenharmony_ci    for (auto _ : state) {
35570af302Sopenharmony_ci        sigset_t set;
36570af302Sopenharmony_ci        benchmark::DoNotOptimize(sigemptyset(&set));
37570af302Sopenharmony_ci    }
38570af302Sopenharmony_ci}
39570af302Sopenharmony_ci
40570af302Sopenharmony_cistatic void Bm_function_Sigaltstack(benchmark::State &state)
41570af302Sopenharmony_ci{
42570af302Sopenharmony_ci    for (auto _ : state) {
43570af302Sopenharmony_ci        state.PauseTiming();
44570af302Sopenharmony_ci        stack_t ss, old_ss;
45570af302Sopenharmony_ci        ss.ss_sp = malloc(SIGSTKSZ);
46570af302Sopenharmony_ci        if (ss.ss_sp == nullptr) {
47570af302Sopenharmony_ci            perror("malloc");
48570af302Sopenharmony_ci        }
49570af302Sopenharmony_ci
50570af302Sopenharmony_ci        ss.ss_size = SIGSTKSZ;
51570af302Sopenharmony_ci        ss.ss_flags = 0;
52570af302Sopenharmony_ci        state.ResumeTiming();
53570af302Sopenharmony_ci        if (sigaltstack(&ss, &old_ss) == -1) {
54570af302Sopenharmony_ci            perror("sigaltstack");
55570af302Sopenharmony_ci        }
56570af302Sopenharmony_ci
57570af302Sopenharmony_ci        state.PauseTiming();
58570af302Sopenharmony_ci        if (sigaltstack(&old_ss, nullptr) == -1) {
59570af302Sopenharmony_ci            perror("sigaltstack");
60570af302Sopenharmony_ci        }
61570af302Sopenharmony_ci        free(ss.ss_sp);
62570af302Sopenharmony_ci        state.ResumeTiming();
63570af302Sopenharmony_ci    }
64570af302Sopenharmony_ci}
65570af302Sopenharmony_ci
66570af302Sopenharmony_cistatic void Bm_function_Sigdelset(benchmark::State &state)
67570af302Sopenharmony_ci{
68570af302Sopenharmony_ci    sigset_t set;
69570af302Sopenharmony_ci    for (auto _ : state) {
70570af302Sopenharmony_ci        sigaddset(&set, SIGINT);
71570af302Sopenharmony_ci        sigdelset(&set, SIGINT);
72570af302Sopenharmony_ci    }
73570af302Sopenharmony_ci}
74570af302Sopenharmony_ci
75570af302Sopenharmony_cistatic void Bm_function_Sigtimedwait(benchmark::State &state)
76570af302Sopenharmony_ci{
77570af302Sopenharmony_ci    sigset_t set;
78570af302Sopenharmony_ci    sigemptyset(&set);
79570af302Sopenharmony_ci    sigaddset(&set, SIGUSR1);
80570af302Sopenharmony_ci    sigaddset(&set, SIGTERM);
81570af302Sopenharmony_ci    siginfo_t info{0};
82570af302Sopenharmony_ci    timespec ts;
83570af302Sopenharmony_ci    ts.tv_sec = 0;
84570af302Sopenharmony_ci    ts.tv_nsec = 0;
85570af302Sopenharmony_ci    for (auto _ : state) {
86570af302Sopenharmony_ci        benchmark::DoNotOptimize(sigtimedwait(&set, &info, &ts));
87570af302Sopenharmony_ci    }
88570af302Sopenharmony_ci}
89570af302Sopenharmony_ci
90570af302Sopenharmony_ciMUSL_BENCHMARK(Bm_function_Sigaction);
91570af302Sopenharmony_ciMUSL_BENCHMARK(Bm_function_Sigemptyset);
92570af302Sopenharmony_ciMUSL_BENCHMARK(Bm_function_Sigaltstack);
93570af302Sopenharmony_ciMUSL_BENCHMARK(Bm_function_Sigdelset);
94570af302Sopenharmony_ciMUSL_BENCHMARK(Bm_function_Sigtimedwait);
95