blob: 667b026950fb18666a6635696e5cd676dc1ba8c6 [file] [log] [blame]
Austin Schuhe6b2b882023-02-04 11:42:40 -08001#include <fcntl.h>
James Kuszmaul4b208072023-05-03 09:39:32 -05002#include <sys/resource.h>
Austin Schuhe6b2b882023-02-04 11:42:40 -08003#include <sys/stat.h>
4#include <sys/types.h>
Austin Schuhf97b4632023-02-11 16:52:58 -08005#include <sys/uio.h>
Austin Schuhe6b2b882023-02-04 11:42:40 -08006
7#include <chrono>
8
Philipp Schrader790cb542023-07-05 21:06:52 -07009#include "gflags/gflags.h"
10#include "glog/logging.h"
11
Austin Schuhe6b2b882023-02-04 11:42:40 -080012#include "aos/init.h"
13#include "aos/realtime.h"
14#include "aos/time/time.h"
Austin Schuhe6b2b882023-02-04 11:42:40 -080015
16namespace chrono = std::chrono;
17
18DEFINE_string(file, "/media/sda1/foo", "File to write to.");
19
20DEFINE_uint32(write_size, 4096, "Size of hunk to write");
James Kuszmaul4b208072023-05-03 09:39:32 -050021DEFINE_int32(nice, -20,
22 "Priority to nice to. Set to 0 to not change the priority.");
Austin Schuhe6b2b882023-02-04 11:42:40 -080023DEFINE_bool(sync, false, "If true, sync the file after each written block.");
Austin Schuhf97b4632023-02-11 16:52:58 -080024DEFINE_bool(writev, false, "If true, use writev.");
Austin Schuhe6b2b882023-02-04 11:42:40 -080025DEFINE_bool(direct, false, "If true, O_DIRECT.");
Austin Schuhf97b4632023-02-11 16:52:58 -080026DEFINE_uint32(chunks, 1, "Chunks to write using writev.");
27DEFINE_uint32(chunk_size, 512, "Chunk size to write using writev.");
Austin Schuhe6b2b882023-02-04 11:42:40 -080028
Philipp Schrader790cb542023-07-05 21:06:52 -070029int main(int argc, char **argv) {
Austin Schuhe6b2b882023-02-04 11:42:40 -080030 aos::InitGoogle(&argc, &argv);
31
32 std::vector<uint8_t> data;
33
34 // We want uncompressible data. The easiest way to do this is to grab a good
35 // sized block from /dev/random, and then reuse it.
36 {
37 int random_fd = open("/dev/random", O_RDONLY | O_CLOEXEC);
38 PCHECK(random_fd != -1) << ": Failed to open /dev/random";
39 data.resize(FLAGS_write_size);
40 size_t written = 0;
41 while (written < data.size()) {
42 const size_t result =
43 read(random_fd, data.data() + written, data.size() - written);
44 PCHECK(result > 0);
45 written += result;
46 }
47
48 PCHECK(close(random_fd) == 0);
49 }
50
Austin Schuhf97b4632023-02-11 16:52:58 -080051 std::vector<struct iovec> iovec;
52 iovec.resize(FLAGS_chunks);
53 CHECK_LE(FLAGS_chunks * FLAGS_chunk_size, FLAGS_write_size);
54
55 for (size_t i = 0; i < FLAGS_chunks; ++i) {
56 iovec[i].iov_base = &data[i * FLAGS_chunk_size];
57 iovec[i].iov_len = FLAGS_chunk_size;
58 }
Philipp Schrader790cb542023-07-05 21:06:52 -070059 iovec[FLAGS_chunks - 1].iov_base =
60 &data[(FLAGS_chunks - 1) * FLAGS_chunk_size];
61 iovec[FLAGS_chunks - 1].iov_len =
62 data.size() - (FLAGS_chunks - 1) * FLAGS_chunk_size;
Austin Schuhf97b4632023-02-11 16:52:58 -080063
Philipp Schrader790cb542023-07-05 21:06:52 -070064 int fd =
65 open(FLAGS_file.c_str(),
66 O_RDWR | O_CLOEXEC | O_CREAT | (FLAGS_direct ? O_DIRECT : 0), 0774);
Austin Schuhe6b2b882023-02-04 11:42:40 -080067 PCHECK(fd != -1);
68
69 const aos::monotonic_clock::time_point start_time =
70 aos::monotonic_clock::now();
Austin Schuhf97b4632023-02-11 16:52:58 -080071 aos::monotonic_clock::time_point last_time = start_time;
Austin Schuhe6b2b882023-02-04 11:42:40 -080072 size_t last_written_data = 0;
73 size_t written_data = 0;
74
James Kuszmaul4b208072023-05-03 09:39:32 -050075 if (FLAGS_nice != 0) {
76 PCHECK(-1 != setpriority(PRIO_PROCESS, 0, FLAGS_nice))
77 << ": Renicing to " << FLAGS_nice << " failed";
78 }
79
Austin Schuhe6b2b882023-02-04 11:42:40 -080080 while (true) {
Austin Schuhf97b4632023-02-11 16:52:58 -080081 if (FLAGS_writev) {
82 PCHECK(writev(fd, iovec.data(), iovec.size()) ==
83 static_cast<ssize_t>(data.size()))
84 << ": Failed after "
85 << chrono::duration<double>(aos::monotonic_clock::now() - start_time)
86 .count();
87 } else {
88 PCHECK(write(fd, data.data(), data.size()) ==
89 static_cast<ssize_t>(data.size()))
90 << ": Failed after "
91 << chrono::duration<double>(aos::monotonic_clock::now() - start_time)
92 .count();
93 }
Austin Schuhe6b2b882023-02-04 11:42:40 -080094
95 // Trigger a flush if asked.
96 if (FLAGS_sync) {
97 const aos::monotonic_clock::time_point monotonic_now =
98 aos::monotonic_clock::now();
99 sync_file_range(fd, written_data, data.size(), SYNC_FILE_RANGE_WRITE);
100
101 // Now, blocking flush the previous page so we don't get too far ahead.
102 // This is Linus' recommendation.
103 if (written_data > 0) {
104 sync_file_range(fd, written_data - data.size(), data.size(),
105 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE |
106 SYNC_FILE_RANGE_WAIT_AFTER);
107 posix_fadvise(fd, written_data - data.size(), data.size(),
108 POSIX_FADV_DONTNEED);
109 }
110 VLOG(1) << "Took "
111 << chrono::duration<double>(aos::monotonic_clock::now() -
112 monotonic_now)
113 .count();
114 }
115
116 written_data += data.size();
117
118 const aos::monotonic_clock::time_point monotonic_now =
119 aos::monotonic_clock::now();
120 // Print out MB/s once it has been at least 1 second since last time.
121 if (monotonic_now > last_time + chrono::seconds(1)) {
122 LOG(INFO)
123 << ((written_data - last_written_data) /
124 chrono::duration<double>(monotonic_now - last_time).count() /
125 1024. / 1024.)
126 << " MB/s";
127 last_time = monotonic_now;
128 last_written_data = written_data;
129 }
130 }
131
132 return 0;
133}