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/stat.h"
17 #include "sys/types.h"
18 #include "dirent.h"
19 #include "unistd.h"
20 #include "fcntl.h"
21 #include "util.h"
22
23 using namespace std;
24
Bm_function_Opendir(benchmark::State & state)25 static void Bm_function_Opendir(benchmark::State &state)
26 {
27 for (auto _ : state) {
28 DIR *dir = opendir("/dev/block");
29 if (dir == nullptr) {
30 perror("opendir proc");
31 }
32 benchmark::DoNotOptimize(dir);
33 state.PauseTiming();
34 closedir(dir);
35 state.ResumeTiming();
36 }
37 }
38
Bm_function_ScanDir(benchmark::State & state)39 static void Bm_function_ScanDir(benchmark::State &state)
40 {
41 const char* dir = "/dev/block";
42 struct dirent **entry = nullptr;
43 for (auto _ : state) {
44 int n = scandir(dir, &entry, nullptr, alphasort);
45 if (n <= 0) {
46 continue;
47 }
48 state.PauseTiming();
49 for (int i = 0; i < n; i++) {
50 free(entry[i]);
51 }
52 free(entry);
53 state.ResumeTiming();
54 }
55 }
56
Bm_function_Closedir(benchmark::State & state)57 static void Bm_function_Closedir(benchmark::State &state)
58 {
59 for (auto _ : state) {
60 state.PauseTiming();
61 DIR *dir = opendir("/dev/block");
62 if (dir == nullptr) {
63 perror("opendir proc");
64 }
65 state.ResumeTiming();
66
67 benchmark::DoNotOptimize(closedir(dir));
68 }
69 }
70
Bm_function_Readdir(benchmark::State & state)71 static void Bm_function_Readdir(benchmark::State &state)
72 {
73 DIR *dir = opendir("/dev/block");
74 if (dir == nullptr) {
75 perror("opendir proc");
76 }
77
78 for (auto _ : state) {
79 struct dirent *entry = nullptr;
80 do {
81 entry = readdir(dir);
82 benchmark::DoNotOptimize(entry);
83 } while (entry != nullptr);
84 }
85 closedir(dir);
86 }
87
88 // Open the table of contents
Bm_function_Fdopendir(benchmark::State & state)89 static void Bm_function_Fdopendir(benchmark::State &state)
90 {
91 int fd = open("/dev/", O_RDONLY, OPEN_MODE);
92 if (fd == -1) {
93 perror("open fdopendir");
94 }
95 for (auto _ : state) {
96 DIR *dir = fdopendir(fd);
97 if (dir == nullptr) {
98 perror("fdopendir proc");
99 }
100 benchmark::DoNotOptimize(dir);
101 }
102 close(fd);
103 }
104
Bm_function_Rewinddir(benchmark::State & state)105 static void Bm_function_Rewinddir(benchmark::State &state)
106 {
107 DIR *dir = opendir("/data/local");
108 if (dir == nullptr) {
109 perror("opendir rewinddir");
110 }
111 while (readdir(dir) != nullptr) {}
112 for (auto _ : state) {
113 rewinddir(dir);
114 }
115 closedir(dir);
116 }
117
Bm_function_Open_Dir(benchmark::State & state)118 static void Bm_function_Open_Dir(benchmark::State &state)
119 {
120 const char *filename = "/dev/block";
121 for (auto _ : state) {
122 int fd = open(filename, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
123 if (fd == -1) {
124 perror("open_dir proc");
125 }
126 benchmark::DoNotOptimize(fd);
127 state.PauseTiming();
128 close(fd);
129 state.ResumeTiming();
130 }
131 state.SetItemsProcessed(state.iterations());
132 }
133
134 MUSL_BENCHMARK(Bm_function_Opendir);
135 MUSL_BENCHMARK(Bm_function_ScanDir);
136 MUSL_BENCHMARK(Bm_function_Closedir);
137 MUSL_BENCHMARK(Bm_function_Readdir);
138 MUSL_BENCHMARK(Bm_function_Fdopendir);
139 MUSL_BENCHMARK(Bm_function_Rewinddir);
140 MUSL_BENCHMARK(Bm_function_Open_Dir);
141