blob: 569f079e48f39c83985becfca9beb397a02de8e2 [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>
Brennan Coslett7a8533f2023-04-13 13:08:17 -05008#include <csignal>
9#include <filesystem>
Austin Schuhe6b2b882023-02-04 11:42:40 -080010
Philipp Schrader790cb542023-07-05 21:06:52 -070011#include "gflags/gflags.h"
12#include "glog/logging.h"
13
Brennan Coslett7a8533f2023-04-13 13:08:17 -050014#include "aos/containers/resizeable_buffer.h"
Austin Schuhe6b2b882023-02-04 11:42:40 -080015#include "aos/init.h"
16#include "aos/realtime.h"
17#include "aos/time/time.h"
Austin Schuhe6b2b882023-02-04 11:42:40 -080018
19namespace chrono = std::chrono;
20
21DEFINE_string(file, "/media/sda1/foo", "File to write to.");
22
23DEFINE_uint32(write_size, 4096, "Size of hunk to write");
Brennan Coslett7a8533f2023-04-13 13:08:17 -050024DEFINE_bool(cleanup, true, "If true, delete the created file");
James Kuszmaul4b208072023-05-03 09:39:32 -050025DEFINE_int32(nice, -20,
26 "Priority to nice to. Set to 0 to not change the priority.");
Austin Schuhe6b2b882023-02-04 11:42:40 -080027DEFINE_bool(sync, false, "If true, sync the file after each written block.");
Austin Schuhf97b4632023-02-11 16:52:58 -080028DEFINE_bool(writev, false, "If true, use writev.");
Austin Schuhe6b2b882023-02-04 11:42:40 -080029DEFINE_bool(direct, false, "If true, O_DIRECT.");
Austin Schuhf97b4632023-02-11 16:52:58 -080030DEFINE_uint32(chunks, 1, "Chunks to write using writev.");
31DEFINE_uint32(chunk_size, 512, "Chunk size to write using writev.");
Austin Schuhe6b2b882023-02-04 11:42:40 -080032
Brennan Coslett7a8533f2023-04-13 13:08:17 -050033// Stolen from aos/events/logging/DummyEncoder
34class AlignedReallocator {
35 public:
36 static void *Realloc(void *old, size_t old_size, size_t new_capacity) {
37 void *new_memory = std::aligned_alloc(512, new_capacity);
38 if (old) {
39 memcpy(new_memory, old, old_size);
40 free(old);
41 }
42 return new_memory;
43 }
44};
45
46void trap_sig(int signum) { exit(signum); }
47
48void cleanup() {
49 // Delete FLAGS_file at shutdown
50 PCHECK(std::filesystem::remove(FLAGS_file) != 0) << "Failed to cleanup file";
51}
52
Philipp Schrader790cb542023-07-05 21:06:52 -070053int main(int argc, char **argv) {
Austin Schuhe6b2b882023-02-04 11:42:40 -080054 aos::InitGoogle(&argc, &argv);
Brennan Coslett7a8533f2023-04-13 13:08:17 -050055 // c++ needs bash's trap <fcn> EXIT
56 // instead we get this mess :(
57 if (FLAGS_cleanup) {
58 std::signal(SIGINT, trap_sig);
59 std::signal(SIGTERM, trap_sig);
60 std::signal(SIGKILL, trap_sig);
61 std::signal(SIGHUP, trap_sig);
62 std::atexit(cleanup);
63 }
64 aos::AllocatorResizeableBuffer<AlignedReallocator> data;
Austin Schuhe6b2b882023-02-04 11:42:40 -080065
Austin Schuhe6b2b882023-02-04 11:42:40 -080066 {
Brennan Coslett7a8533f2023-04-13 13:08:17 -050067 // We want uncompressible data. The easiest way to do this is to grab a
68 // good sized block from /dev/random, and then reuse it.
Austin Schuhe6b2b882023-02-04 11:42:40 -080069 int random_fd = open("/dev/random", O_RDONLY | O_CLOEXEC);
70 PCHECK(random_fd != -1) << ": Failed to open /dev/random";
71 data.resize(FLAGS_write_size);
72 size_t written = 0;
73 while (written < data.size()) {
74 const size_t result =
75 read(random_fd, data.data() + written, data.size() - written);
76 PCHECK(result > 0);
77 written += result;
78 }
79
80 PCHECK(close(random_fd) == 0);
81 }
82
Austin Schuhf97b4632023-02-11 16:52:58 -080083 std::vector<struct iovec> iovec;
Brennan Coslett7a8533f2023-04-13 13:08:17 -050084 if (FLAGS_writev) {
85 iovec.resize(FLAGS_chunks);
86 CHECK_LE(FLAGS_chunks * FLAGS_chunk_size, FLAGS_write_size);
Austin Schuhf97b4632023-02-11 16:52:58 -080087
Brennan Coslett7a8533f2023-04-13 13:08:17 -050088 for (size_t i = 0; i < FLAGS_chunks; ++i) {
89 iovec[i].iov_base = &data.at(i * FLAGS_chunk_size);
90 iovec[i].iov_len = FLAGS_chunk_size;
91 }
92 iovec[FLAGS_chunks - 1].iov_base =
93 &data.at((FLAGS_chunks - 1) * FLAGS_chunk_size);
94 iovec[FLAGS_chunks - 1].iov_len =
95 data.size() - (FLAGS_chunks - 1) * FLAGS_chunk_size;
Austin Schuhf97b4632023-02-11 16:52:58 -080096 }
Austin Schuhf97b4632023-02-11 16:52:58 -080097
Philipp Schrader790cb542023-07-05 21:06:52 -070098 int fd =
99 open(FLAGS_file.c_str(),
100 O_RDWR | O_CLOEXEC | O_CREAT | (FLAGS_direct ? O_DIRECT : 0), 0774);
Austin Schuhe6b2b882023-02-04 11:42:40 -0800101 PCHECK(fd != -1);
102
103 const aos::monotonic_clock::time_point start_time =
104 aos::monotonic_clock::now();
Austin Schuhf97b4632023-02-11 16:52:58 -0800105 aos::monotonic_clock::time_point last_time = start_time;
Austin Schuhe6b2b882023-02-04 11:42:40 -0800106 size_t last_written_data = 0;
107 size_t written_data = 0;
108
James Kuszmaul4b208072023-05-03 09:39:32 -0500109 if (FLAGS_nice != 0) {
110 PCHECK(-1 != setpriority(PRIO_PROCESS, 0, FLAGS_nice))
111 << ": Renicing to " << FLAGS_nice << " failed";
112 }
113
Austin Schuhe6b2b882023-02-04 11:42:40 -0800114 while (true) {
Austin Schuhf97b4632023-02-11 16:52:58 -0800115 if (FLAGS_writev) {
116 PCHECK(writev(fd, iovec.data(), iovec.size()) ==
117 static_cast<ssize_t>(data.size()))
118 << ": Failed after "
119 << chrono::duration<double>(aos::monotonic_clock::now() - start_time)
120 .count();
121 } else {
122 PCHECK(write(fd, data.data(), data.size()) ==
123 static_cast<ssize_t>(data.size()))
124 << ": Failed after "
125 << chrono::duration<double>(aos::monotonic_clock::now() - start_time)
126 .count();
127 }
Austin Schuhe6b2b882023-02-04 11:42:40 -0800128
129 // Trigger a flush if asked.
130 if (FLAGS_sync) {
131 const aos::monotonic_clock::time_point monotonic_now =
132 aos::monotonic_clock::now();
133 sync_file_range(fd, written_data, data.size(), SYNC_FILE_RANGE_WRITE);
134
135 // Now, blocking flush the previous page so we don't get too far ahead.
136 // This is Linus' recommendation.
137 if (written_data > 0) {
138 sync_file_range(fd, written_data - data.size(), data.size(),
139 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE |
140 SYNC_FILE_RANGE_WAIT_AFTER);
141 posix_fadvise(fd, written_data - data.size(), data.size(),
142 POSIX_FADV_DONTNEED);
143 }
144 VLOG(1) << "Took "
145 << chrono::duration<double>(aos::monotonic_clock::now() -
146 monotonic_now)
147 .count();
148 }
149
150 written_data += data.size();
151
152 const aos::monotonic_clock::time_point monotonic_now =
153 aos::monotonic_clock::now();
154 // Print out MB/s once it has been at least 1 second since last time.
155 if (monotonic_now > last_time + chrono::seconds(1)) {
156 LOG(INFO)
157 << ((written_data - last_written_data) /
158 chrono::duration<double>(monotonic_now - last_time).count() /
159 1024. / 1024.)
160 << " MB/s";
161 last_time = monotonic_now;
162 last_written_data = written_data;
163 }
164 }
165
166 return 0;
167}