Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/ByteBuffer.h>
28#include <AK/String.h>
29#include <AK/Types.h>
30#include <AK/Vector.h>
31#include <LibCore/ElapsedTimer.h>
32#include <getopt.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <fcntl.h>
36#include <unistd.h>
37#include <sys/stat.h>
38
39struct Result {
40 u64 write_bps;
41 u64 read_bps;
42};
43
44Result average_result(const Vector<Result>& results)
45{
46 Result average;
47
48 for (auto& res : results) {
49 average.write_bps += res.write_bps;
50 average.read_bps += res.read_bps;
51 }
52
53 average.write_bps /= results.size();
54 average.read_bps /= results.size();
55
56 return average;
57}
58
59void exit_with_usage(int rc)
60{
61 fprintf(stderr, "Usage: disk_benchmark [-h] [-d directory] [-t time_per_benchmark] [-f file_size1,file_size2,...] [-b block_size1,block_size2,...]\n");
62 exit(rc);
63}
64
65Result benchmark(const String& filename, int file_size, int block_size, ByteBuffer& buffer, bool allow_cache);
66
67int main(int argc, char** argv)
68{
69 char* directory = strdup(".");
70 int time_per_benchmark = 10;
71 Vector<int> file_sizes;
72 Vector<int> block_sizes;
73 bool allow_cache = false;
74
75 int opt;
76 while ((opt = getopt(argc, argv, "chd:t:f:b:")) != -1) {
77 switch (opt) {
78 case 'h':
79 exit_with_usage(0);
80 break;
81 case 'c':
82 allow_cache = true;
83 break;
84 case 'd':
85 directory = strdup(optarg);
86 break;
87 case 't':
88 time_per_benchmark = atoi(optarg);
89 break;
90 case 'f':
91 for (auto size : String(optarg).split(','))
92 file_sizes.append(atoi(size.characters()));
93 break;
94 case 'b':
95 for (auto size : String(optarg).split(','))
96 block_sizes.append(atoi(size.characters()));
97 break;
98 }
99 }
100
101 if (file_sizes.size() == 0) {
102 file_sizes = { 131072, 262144, 524288, 1048576, 5242880 };
103 }
104 if (block_sizes.size() == 0) {
105 block_sizes = { 8192, 32768, 65536 };
106 }
107
108 umask(0644);
109
110 auto filename = String::format("%s/disk_benchmark.tmp", directory);
111
112 for (auto file_size : file_sizes) {
113 for (auto block_size : block_sizes) {
114 if (block_size > file_size)
115 continue;
116
117 auto buffer = ByteBuffer::create_uninitialized(block_size);
118
119 Vector<Result> results;
120
121 printf("Running: file_size=%d block_size=%d\n", file_size, block_size);
122 Core::ElapsedTimer timer;
123 timer.start();
124 while (timer.elapsed() < time_per_benchmark * 1000) {
125 printf(".");
126 fflush(stdout);
127 results.append(benchmark(filename, file_size, block_size, buffer, allow_cache));
128 usleep(100);
129 }
130 auto average = average_result(results);
131 printf("\nFinished: runs=%zu time=%dms write_bps=%llu read_bps=%llu\n", results.size(), timer.elapsed(), average.write_bps, average.read_bps);
132
133 sleep(1);
134 }
135 }
136
137 if (isatty(0)) {
138 printf("Press any key to exit...\n");
139 fgetc(stdin);
140 }
141}
142
143Result benchmark(const String& filename, int file_size, int block_size, ByteBuffer& buffer, bool allow_cache)
144{
145 int flags = O_CREAT | O_TRUNC | O_RDWR;
146 if (!allow_cache)
147 flags |= O_DIRECT;
148
149 int fd = open(filename.characters(), flags, 0644);
150 if (fd == -1) {
151 perror("open");
152 exit(1);
153 }
154
155 auto cleanup_and_exit = [fd, filename]() {
156 close(fd);
157 unlink(filename.characters());
158 exit(1);
159 };
160
161 Result res;
162
163 Core::ElapsedTimer timer;
164
165 timer.start();
166 int nwrote = 0;
167 for (int j = 0; j < file_size; j += block_size) {
168 int n = write(fd, buffer.data(), block_size);
169 if (n < 0) {
170 perror("write");
171 cleanup_and_exit();
172 }
173 nwrote += n;
174 }
175
176 res.write_bps = (u64)(timer.elapsed() ? (file_size / timer.elapsed()) : file_size) * 1000;
177
178 if (lseek(fd, 0, SEEK_SET) < 0) {
179 perror("lseek");
180 cleanup_and_exit();
181 }
182
183 timer.start();
184 int nread = 0;
185 while (nread < file_size) {
186 int n = read(fd, buffer.data(), block_size);
187 if (n < 0) {
188 perror("read");
189 cleanup_and_exit();
190 }
191 nread += n;
192 }
193
194 res.read_bps = (u64)(timer.elapsed() ? (file_size / timer.elapsed()) : file_size) * 1000;
195
196 if (close(fd) != 0) {
197 perror("close");
198 cleanup_and_exit();
199 }
200
201 if (unlink(filename.characters()) != 0) {
202 perror("unlink");
203 cleanup_and_exit();
204 }
205
206 return res;
207}