blob: e40f81aa3d510694ab46418933acf682a5e428dd [file] [log] [blame]
Austin Schuhe6b2b882023-02-04 11:42:40 -08001#include <fcntl.h>
2#include <sys/stat.h>
3#include <sys/types.h>
Austin Schuhf97b4632023-02-11 16:52:58 -08004#include <sys/uio.h>
Austin Schuhe6b2b882023-02-04 11:42:40 -08005
6#include <chrono>
7
Philipp Schrader790cb542023-07-05 21:06:52 -07008#include "gflags/gflags.h"
9#include "glog/logging.h"
10
Austin Schuhe6b2b882023-02-04 11:42:40 -080011#include "aos/init.h"
12#include "aos/realtime.h"
13#include "aos/time/time.h"
Austin Schuhe6b2b882023-02-04 11:42:40 -080014
15namespace chrono = std::chrono;
16
17DEFINE_string(file, "/media/sda1/foo", "File to write to.");
18
19DEFINE_uint32(write_size, 4096, "Size of hunk to write");
20DEFINE_bool(sync, false, "If true, sync the file after each written block.");
Austin Schuhf97b4632023-02-11 16:52:58 -080021DEFINE_bool(writev, false, "If true, use writev.");
Austin Schuhe6b2b882023-02-04 11:42:40 -080022DEFINE_bool(direct, false, "If true, O_DIRECT.");
Austin Schuhf97b4632023-02-11 16:52:58 -080023DEFINE_uint32(chunks, 1, "Chunks to write using writev.");
24DEFINE_uint32(chunk_size, 512, "Chunk size to write using writev.");
Austin Schuhe6b2b882023-02-04 11:42:40 -080025
Philipp Schrader790cb542023-07-05 21:06:52 -070026int main(int argc, char **argv) {
Austin Schuhe6b2b882023-02-04 11:42:40 -080027 aos::InitGoogle(&argc, &argv);
28
29 std::vector<uint8_t> data;
30
31 // We want uncompressible data. The easiest way to do this is to grab a good
32 // sized block from /dev/random, and then reuse it.
33 {
34 int random_fd = open("/dev/random", O_RDONLY | O_CLOEXEC);
35 PCHECK(random_fd != -1) << ": Failed to open /dev/random";
36 data.resize(FLAGS_write_size);
37 size_t written = 0;
38 while (written < data.size()) {
39 const size_t result =
40 read(random_fd, data.data() + written, data.size() - written);
41 PCHECK(result > 0);
42 written += result;
43 }
44
45 PCHECK(close(random_fd) == 0);
46 }
47
Austin Schuhf97b4632023-02-11 16:52:58 -080048 std::vector<struct iovec> iovec;
49 iovec.resize(FLAGS_chunks);
50 CHECK_LE(FLAGS_chunks * FLAGS_chunk_size, FLAGS_write_size);
51
52 for (size_t i = 0; i < FLAGS_chunks; ++i) {
53 iovec[i].iov_base = &data[i * FLAGS_chunk_size];
54 iovec[i].iov_len = FLAGS_chunk_size;
55 }
Philipp Schrader790cb542023-07-05 21:06:52 -070056 iovec[FLAGS_chunks - 1].iov_base =
57 &data[(FLAGS_chunks - 1) * FLAGS_chunk_size];
58 iovec[FLAGS_chunks - 1].iov_len =
59 data.size() - (FLAGS_chunks - 1) * FLAGS_chunk_size;
Austin Schuhf97b4632023-02-11 16:52:58 -080060
Philipp Schrader790cb542023-07-05 21:06:52 -070061 int fd =
62 open(FLAGS_file.c_str(),
63 O_RDWR | O_CLOEXEC | O_CREAT | (FLAGS_direct ? O_DIRECT : 0), 0774);
Austin Schuhe6b2b882023-02-04 11:42:40 -080064 PCHECK(fd != -1);
65
66 const aos::monotonic_clock::time_point start_time =
67 aos::monotonic_clock::now();
Austin Schuhf97b4632023-02-11 16:52:58 -080068 aos::monotonic_clock::time_point last_time = start_time;
Austin Schuhe6b2b882023-02-04 11:42:40 -080069 size_t last_written_data = 0;
70 size_t written_data = 0;
71
72 while (true) {
Austin Schuhf97b4632023-02-11 16:52:58 -080073 if (FLAGS_writev) {
74 PCHECK(writev(fd, iovec.data(), iovec.size()) ==
75 static_cast<ssize_t>(data.size()))
76 << ": Failed after "
77 << chrono::duration<double>(aos::monotonic_clock::now() - start_time)
78 .count();
79 } else {
80 PCHECK(write(fd, data.data(), data.size()) ==
81 static_cast<ssize_t>(data.size()))
82 << ": Failed after "
83 << chrono::duration<double>(aos::monotonic_clock::now() - start_time)
84 .count();
85 }
Austin Schuhe6b2b882023-02-04 11:42:40 -080086
87 // Trigger a flush if asked.
88 if (FLAGS_sync) {
89 const aos::monotonic_clock::time_point monotonic_now =
90 aos::monotonic_clock::now();
91 sync_file_range(fd, written_data, data.size(), SYNC_FILE_RANGE_WRITE);
92
93 // Now, blocking flush the previous page so we don't get too far ahead.
94 // This is Linus' recommendation.
95 if (written_data > 0) {
96 sync_file_range(fd, written_data - data.size(), data.size(),
97 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE |
98 SYNC_FILE_RANGE_WAIT_AFTER);
99 posix_fadvise(fd, written_data - data.size(), data.size(),
100 POSIX_FADV_DONTNEED);
101 }
102 VLOG(1) << "Took "
103 << chrono::duration<double>(aos::monotonic_clock::now() -
104 monotonic_now)
105 .count();
106 }
107
108 written_data += data.size();
109
110 const aos::monotonic_clock::time_point monotonic_now =
111 aos::monotonic_clock::now();
112 // Print out MB/s once it has been at least 1 second since last time.
113 if (monotonic_now > last_time + chrono::seconds(1)) {
114 LOG(INFO)
115 << ((written_data - last_written_data) /
116 chrono::duration<double>(monotonic_now - last_time).count() /
117 1024. / 1024.)
118 << " MB/s";
119 last_time = monotonic_now;
120 last_written_data = written_data;
121 }
122 }
123
124 return 0;
125}