• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/epoll.h>
17 #include <cerrno>
18 #include <cstdlib>
19 #include <cstring>
20 #include <cstdio>
21 #include <fcntl.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include "util.h"
26 
27 using namespace std;
28 constexpr int EVENTSIZE = 10;
29 struct epoll_event g_events[EVENTSIZE], g_event;
30 
31 // Used to create an epoll object to record events for files to be listened to
Bm_function_Epoll_createl(benchmark::State & state)32 static void Bm_function_Epoll_createl(benchmark::State &state)
33 {
34     for (auto _ : state) {
35         int epollFd = epoll_create1(0);
36         if (epollFd == -1) {
37             perror("epoll_createl");
38         }
39         benchmark::DoNotOptimize(epollFd);
40         close(epollFd);
41     }
42 
43     state.SetBytesProcessed(state.iterations());
44 }
45 
46 // Used to add, modify, or delete events to an epoll object
Bm_function_Epoll_ctl(benchmark::State & state)47 static void Bm_function_Epoll_ctl(benchmark::State &state)
48 {
49     int epollFd = epoll_create1(0);
50     if (epollFd == -1) {
51         perror("epoll_createl");
52     }
53     int fd = open("/dev/zero", O_RDONLY, OPEN_MODE);
54     if (fd == -1) {
55         perror("open epoll_ctl");
56     }
57     g_event.events = EPOLLIN | EPOLLET;
58     g_event.data.fd = fd;
59     for (auto _ : state) {
60         benchmark::DoNotOptimize(epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &g_event));
61     }
62     close(epollFd);
63     close(fd);
64     state.SetBytesProcessed(state.iterations());
65 }
66 
67 // Used to wait for the file descriptor in the epoll object to appear for the event
Bm_function_Epoll_wait(benchmark::State & state)68 static void Bm_function_Epoll_wait(benchmark::State &state)
69 {
70     int epollFd = epoll_create1(0);
71     if (epollFd == -1) {
72         perror("epoll_createl");
73     }
74 
75     int fd = open("/dev/zero", O_RDONLY, OPEN_MODE);
76     if (fd == -1) {
77         perror("open epoll_wait");
78     }
79 
80     g_event.events = EPOLLIN | EPOLLET;
81     g_event.data.fd = fd;
82     for (auto _ : state) {
83         epoll_wait(epollFd, g_events, EVENTSIZE, 0);
84     }
85     close(epollFd);
86     close(fd);
87     state.SetBytesProcessed(state.iterations());
88 }
89 
90 MUSL_BENCHMARK(Bm_function_Epoll_createl);
91 MUSL_BENCHMARK(Bm_function_Epoll_ctl);
92 MUSL_BENCHMARK(Bm_function_Epoll_wait);