Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 1 | #include <fcntl.h> |
James Kuszmaul | 4b20807 | 2023-05-03 09:39:32 -0500 | [diff] [blame] | 2 | #include <sys/resource.h> |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 3 | #include <sys/stat.h> |
| 4 | #include <sys/types.h> |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 5 | #include <sys/uio.h> |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 6 | |
| 7 | #include <chrono> |
Brennan Coslett | 7a8533f | 2023-04-13 13:08:17 -0500 | [diff] [blame] | 8 | #include <csignal> |
| 9 | #include <filesystem> |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 10 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 11 | #include "gflags/gflags.h" |
| 12 | #include "glog/logging.h" |
| 13 | |
Brennan Coslett | 7a8533f | 2023-04-13 13:08:17 -0500 | [diff] [blame] | 14 | #include "aos/containers/resizeable_buffer.h" |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 15 | #include "aos/init.h" |
| 16 | #include "aos/realtime.h" |
| 17 | #include "aos/time/time.h" |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 18 | |
| 19 | namespace chrono = std::chrono; |
| 20 | |
| 21 | DEFINE_string(file, "/media/sda1/foo", "File to write to."); |
| 22 | |
| 23 | DEFINE_uint32(write_size, 4096, "Size of hunk to write"); |
Brennan Coslett | 7a8533f | 2023-04-13 13:08:17 -0500 | [diff] [blame] | 24 | DEFINE_bool(cleanup, true, "If true, delete the created file"); |
James Kuszmaul | 4b20807 | 2023-05-03 09:39:32 -0500 | [diff] [blame] | 25 | DEFINE_int32(nice, -20, |
| 26 | "Priority to nice to. Set to 0 to not change the priority."); |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 27 | DEFINE_bool(sync, false, "If true, sync the file after each written block."); |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 28 | DEFINE_bool(writev, false, "If true, use writev."); |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 29 | DEFINE_bool(direct, false, "If true, O_DIRECT."); |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 30 | DEFINE_uint32(chunks, 1, "Chunks to write using writev."); |
| 31 | DEFINE_uint32(chunk_size, 512, "Chunk size to write using writev."); |
Austin Schuh | 6049d34 | 2023-06-01 12:20:51 -0700 | [diff] [blame] | 32 | DEFINE_uint64(overall_size, 0, |
| 33 | "If nonzero, write this many bytes and then stop. Must be a " |
| 34 | "multiple of --write_size"); |
Sanjay Narayanan | 2cefd5c | 2023-06-01 16:52:03 -0700 | [diff] [blame] | 35 | DEFINE_bool(rate_limit, false, |
| 36 | "If true, kick off writes every 100ms to mimic logger write " |
| 37 | "patterns more correctly."); |
| 38 | DEFINE_double(write_bandwidth, 120.0, |
| 39 | "Write speed in MB/s to simulate. This is only used when " |
| 40 | "--rate_limit is specified."); |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 41 | |
Brennan Coslett | 7a8533f | 2023-04-13 13:08:17 -0500 | [diff] [blame] | 42 | // Stolen from aos/events/logging/DummyEncoder |
| 43 | class AlignedReallocator { |
| 44 | public: |
| 45 | static void *Realloc(void *old, size_t old_size, size_t new_capacity) { |
| 46 | void *new_memory = std::aligned_alloc(512, new_capacity); |
| 47 | if (old) { |
| 48 | memcpy(new_memory, old, old_size); |
| 49 | free(old); |
| 50 | } |
| 51 | return new_memory; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | void trap_sig(int signum) { exit(signum); } |
| 56 | |
Austin Schuh | 6049d34 | 2023-06-01 12:20:51 -0700 | [diff] [blame] | 57 | aos::monotonic_clock::time_point start_time = aos::monotonic_clock::min_time; |
| 58 | std::atomic<size_t> written_data = 0; |
| 59 | |
Brennan Coslett | 7a8533f | 2023-04-13 13:08:17 -0500 | [diff] [blame] | 60 | void cleanup() { |
Austin Schuh | 6049d34 | 2023-06-01 12:20:51 -0700 | [diff] [blame] | 61 | LOG(INFO) << "Overall average write speed: " |
| 62 | << ((written_data) / |
| 63 | chrono::duration<double>(aos::monotonic_clock::now() - |
| 64 | start_time) |
| 65 | .count() / |
| 66 | 1024. / 1024.) |
| 67 | << " MB/s for " << static_cast<double>(written_data) / 1024. / 1024. |
| 68 | << "MB"; |
| 69 | |
Brennan Coslett | 7a8533f | 2023-04-13 13:08:17 -0500 | [diff] [blame] | 70 | // Delete FLAGS_file at shutdown |
| 71 | PCHECK(std::filesystem::remove(FLAGS_file) != 0) << "Failed to cleanup file"; |
| 72 | } |
| 73 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 74 | int main(int argc, char **argv) { |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 75 | aos::InitGoogle(&argc, &argv); |
Brennan Coslett | 7a8533f | 2023-04-13 13:08:17 -0500 | [diff] [blame] | 76 | // c++ needs bash's trap <fcn> EXIT |
| 77 | // instead we get this mess :( |
| 78 | if (FLAGS_cleanup) { |
| 79 | std::signal(SIGINT, trap_sig); |
| 80 | std::signal(SIGTERM, trap_sig); |
| 81 | std::signal(SIGKILL, trap_sig); |
| 82 | std::signal(SIGHUP, trap_sig); |
| 83 | std::atexit(cleanup); |
| 84 | } |
| 85 | aos::AllocatorResizeableBuffer<AlignedReallocator> data; |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 86 | |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 87 | { |
Brennan Coslett | 7a8533f | 2023-04-13 13:08:17 -0500 | [diff] [blame] | 88 | // We want uncompressible data. The easiest way to do this is to grab a |
| 89 | // good sized block from /dev/random, and then reuse it. |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 90 | int random_fd = open("/dev/random", O_RDONLY | O_CLOEXEC); |
| 91 | PCHECK(random_fd != -1) << ": Failed to open /dev/random"; |
| 92 | data.resize(FLAGS_write_size); |
| 93 | size_t written = 0; |
| 94 | while (written < data.size()) { |
| 95 | const size_t result = |
| 96 | read(random_fd, data.data() + written, data.size() - written); |
| 97 | PCHECK(result > 0); |
| 98 | written += result; |
| 99 | } |
| 100 | |
| 101 | PCHECK(close(random_fd) == 0); |
| 102 | } |
| 103 | |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 104 | std::vector<struct iovec> iovec; |
Brennan Coslett | 7a8533f | 2023-04-13 13:08:17 -0500 | [diff] [blame] | 105 | if (FLAGS_writev) { |
| 106 | iovec.resize(FLAGS_chunks); |
| 107 | CHECK_LE(FLAGS_chunks * FLAGS_chunk_size, FLAGS_write_size); |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 108 | |
Brennan Coslett | 7a8533f | 2023-04-13 13:08:17 -0500 | [diff] [blame] | 109 | for (size_t i = 0; i < FLAGS_chunks; ++i) { |
| 110 | iovec[i].iov_base = &data.at(i * FLAGS_chunk_size); |
| 111 | iovec[i].iov_len = FLAGS_chunk_size; |
| 112 | } |
| 113 | iovec[FLAGS_chunks - 1].iov_base = |
| 114 | &data.at((FLAGS_chunks - 1) * FLAGS_chunk_size); |
| 115 | iovec[FLAGS_chunks - 1].iov_len = |
| 116 | data.size() - (FLAGS_chunks - 1) * FLAGS_chunk_size; |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 117 | } |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 118 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 119 | int fd = |
| 120 | open(FLAGS_file.c_str(), |
| 121 | O_RDWR | O_CLOEXEC | O_CREAT | (FLAGS_direct ? O_DIRECT : 0), 0774); |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 122 | PCHECK(fd != -1); |
| 123 | |
Austin Schuh | 6049d34 | 2023-06-01 12:20:51 -0700 | [diff] [blame] | 124 | start_time = aos::monotonic_clock::now(); |
Sanjay Narayanan | 2cefd5c | 2023-06-01 16:52:03 -0700 | [diff] [blame] | 125 | aos::monotonic_clock::time_point last_print_time = start_time; |
| 126 | aos::monotonic_clock::time_point cycle_start_time = start_time; |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 127 | size_t last_written_data = 0; |
Austin Schuh | 6049d34 | 2023-06-01 12:20:51 -0700 | [diff] [blame] | 128 | written_data = 0; |
Sanjay Narayanan | 2cefd5c | 2023-06-01 16:52:03 -0700 | [diff] [blame] | 129 | // Track how much data we write per cycle. When --rate_limit is specified, |
| 130 | // --write_bandwidth is the amount of data we want to write per second, and we |
| 131 | // want to write it in cycles of 100ms to simulate the logger. |
| 132 | size_t cycle_written_data = 0; |
| 133 | size_t data_per_cycle = std::numeric_limits<size_t>::max(); |
| 134 | if (FLAGS_rate_limit) { |
| 135 | data_per_cycle = |
| 136 | static_cast<size_t>((FLAGS_write_bandwidth * 1024 * 1024) / 10); |
| 137 | } |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 138 | |
James Kuszmaul | 4b20807 | 2023-05-03 09:39:32 -0500 | [diff] [blame] | 139 | if (FLAGS_nice != 0) { |
| 140 | PCHECK(-1 != setpriority(PRIO_PROCESS, 0, FLAGS_nice)) |
| 141 | << ": Renicing to " << FLAGS_nice << " failed"; |
| 142 | } |
| 143 | |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 144 | while (true) { |
Austin Schuh | 6049d34 | 2023-06-01 12:20:51 -0700 | [diff] [blame] | 145 | // Bail if we have written our limit. |
| 146 | if (written_data >= FLAGS_overall_size && FLAGS_overall_size != 0) { |
| 147 | break; |
| 148 | } |
| 149 | |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 150 | if (FLAGS_writev) { |
| 151 | PCHECK(writev(fd, iovec.data(), iovec.size()) == |
| 152 | static_cast<ssize_t>(data.size())) |
| 153 | << ": Failed after " |
| 154 | << chrono::duration<double>(aos::monotonic_clock::now() - start_time) |
| 155 | .count(); |
| 156 | } else { |
| 157 | PCHECK(write(fd, data.data(), data.size()) == |
| 158 | static_cast<ssize_t>(data.size())) |
| 159 | << ": Failed after " |
| 160 | << chrono::duration<double>(aos::monotonic_clock::now() - start_time) |
| 161 | .count(); |
| 162 | } |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 163 | |
| 164 | // Trigger a flush if asked. |
| 165 | if (FLAGS_sync) { |
| 166 | const aos::monotonic_clock::time_point monotonic_now = |
| 167 | aos::monotonic_clock::now(); |
| 168 | sync_file_range(fd, written_data, data.size(), SYNC_FILE_RANGE_WRITE); |
| 169 | |
| 170 | // Now, blocking flush the previous page so we don't get too far ahead. |
| 171 | // This is Linus' recommendation. |
| 172 | if (written_data > 0) { |
| 173 | sync_file_range(fd, written_data - data.size(), data.size(), |
| 174 | SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE | |
| 175 | SYNC_FILE_RANGE_WAIT_AFTER); |
| 176 | posix_fadvise(fd, written_data - data.size(), data.size(), |
| 177 | POSIX_FADV_DONTNEED); |
| 178 | } |
| 179 | VLOG(1) << "Took " |
| 180 | << chrono::duration<double>(aos::monotonic_clock::now() - |
| 181 | monotonic_now) |
| 182 | .count(); |
| 183 | } |
| 184 | |
| 185 | written_data += data.size(); |
Sanjay Narayanan | 2cefd5c | 2023-06-01 16:52:03 -0700 | [diff] [blame] | 186 | cycle_written_data += data.size(); |
| 187 | |
| 188 | // Simulate the logger by writing the specified amount of data in periods of |
| 189 | // 100ms. |
| 190 | bool reset_cycle = false; |
| 191 | if (cycle_written_data > data_per_cycle && FLAGS_rate_limit) { |
| 192 | // Check how much data we should have already written based on |
| 193 | // --write_bandwidth. |
| 194 | const size_t current_target = |
| 195 | FLAGS_write_bandwidth * 1024 * 1024 * |
| 196 | chrono::duration<double>(aos::monotonic_clock::now() - start_time) |
| 197 | .count(); |
| 198 | const bool caught_up = written_data > current_target; |
| 199 | if (caught_up) { |
| 200 | // If we're on track, sleep for the rest of this cycle, as long as we |
| 201 | // didn't use up all the cycle time writing. |
| 202 | const aos::monotonic_clock::time_point monotonic_now = |
| 203 | aos::monotonic_clock::now(); |
| 204 | const auto sleep_duration = |
| 205 | (cycle_start_time + chrono::milliseconds(100)) - monotonic_now; |
| 206 | if (sleep_duration.count() > 0) { |
| 207 | VLOG(2) << "Sleeping for " << sleep_duration.count(); |
| 208 | std::this_thread::sleep_for(sleep_duration); |
| 209 | } else { |
| 210 | LOG(WARNING) << "It took longer than 100ms to write " |
| 211 | << data_per_cycle << " bytes."; |
| 212 | } |
| 213 | reset_cycle = true; |
| 214 | } else { |
| 215 | // If we aren't on track, don't sleep. |
| 216 | LOG(WARNING) << "Still catching up to target write rate."; |
| 217 | } |
| 218 | // Either way, reset the data we're counting for this "cycle". If we're |
| 219 | // still behind, let's check again after writing another data_per_cycle |
| 220 | // bytes. |
| 221 | cycle_written_data = 0; |
| 222 | } |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 223 | |
| 224 | const aos::monotonic_clock::time_point monotonic_now = |
| 225 | aos::monotonic_clock::now(); |
| 226 | // Print out MB/s once it has been at least 1 second since last time. |
Sanjay Narayanan | 2cefd5c | 2023-06-01 16:52:03 -0700 | [diff] [blame] | 227 | if (monotonic_now > last_print_time + chrono::seconds(1)) { |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 228 | LOG(INFO) |
| 229 | << ((written_data - last_written_data) / |
Sanjay Narayanan | 2cefd5c | 2023-06-01 16:52:03 -0700 | [diff] [blame] | 230 | chrono::duration<double>(monotonic_now - last_print_time) |
| 231 | .count() / |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 232 | 1024. / 1024.) |
Austin Schuh | 6049d34 | 2023-06-01 12:20:51 -0700 | [diff] [blame] | 233 | << " MB/s, average of " |
| 234 | << (written_data / |
| 235 | chrono::duration<double>(monotonic_now - start_time).count() / |
| 236 | 1024. / 1024.) |
| 237 | << " MB/s for " << static_cast<double>(written_data) / 1024. / 1024. |
| 238 | << "MB"; |
Sanjay Narayanan | 2cefd5c | 2023-06-01 16:52:03 -0700 | [diff] [blame] | 239 | last_print_time = monotonic_now; |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 240 | last_written_data = written_data; |
| 241 | } |
Sanjay Narayanan | 2cefd5c | 2023-06-01 16:52:03 -0700 | [diff] [blame] | 242 | |
| 243 | // Do this at the end so that we're setting the next cycle start time as |
| 244 | // accurately as possible. |
| 245 | if (reset_cycle) { |
| 246 | cycle_start_time = monotonic_now; |
| 247 | VLOG(1) << cycle_start_time; |
| 248 | } |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | return 0; |
| 252 | } |