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 | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 32 | |
Brennan Coslett | 7a8533f | 2023-04-13 13:08:17 -0500 | [diff] [blame^] | 33 | // Stolen from aos/events/logging/DummyEncoder |
| 34 | class 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 | |
| 46 | void trap_sig(int signum) { exit(signum); } |
| 47 | |
| 48 | void cleanup() { |
| 49 | // Delete FLAGS_file at shutdown |
| 50 | PCHECK(std::filesystem::remove(FLAGS_file) != 0) << "Failed to cleanup file"; |
| 51 | } |
| 52 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 53 | int main(int argc, char **argv) { |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 54 | aos::InitGoogle(&argc, &argv); |
Brennan Coslett | 7a8533f | 2023-04-13 13:08:17 -0500 | [diff] [blame^] | 55 | // 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 Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 65 | |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 66 | { |
Brennan Coslett | 7a8533f | 2023-04-13 13:08:17 -0500 | [diff] [blame^] | 67 | // 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 Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 69 | 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 Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 83 | std::vector<struct iovec> iovec; |
Brennan Coslett | 7a8533f | 2023-04-13 13:08:17 -0500 | [diff] [blame^] | 84 | if (FLAGS_writev) { |
| 85 | iovec.resize(FLAGS_chunks); |
| 86 | CHECK_LE(FLAGS_chunks * FLAGS_chunk_size, FLAGS_write_size); |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 87 | |
Brennan Coslett | 7a8533f | 2023-04-13 13:08:17 -0500 | [diff] [blame^] | 88 | 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 Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 96 | } |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 97 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 98 | int fd = |
| 99 | open(FLAGS_file.c_str(), |
| 100 | O_RDWR | O_CLOEXEC | O_CREAT | (FLAGS_direct ? O_DIRECT : 0), 0774); |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 101 | PCHECK(fd != -1); |
| 102 | |
| 103 | const aos::monotonic_clock::time_point start_time = |
| 104 | aos::monotonic_clock::now(); |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 105 | aos::monotonic_clock::time_point last_time = start_time; |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 106 | size_t last_written_data = 0; |
| 107 | size_t written_data = 0; |
| 108 | |
James Kuszmaul | 4b20807 | 2023-05-03 09:39:32 -0500 | [diff] [blame] | 109 | if (FLAGS_nice != 0) { |
| 110 | PCHECK(-1 != setpriority(PRIO_PROCESS, 0, FLAGS_nice)) |
| 111 | << ": Renicing to " << FLAGS_nice << " failed"; |
| 112 | } |
| 113 | |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 114 | while (true) { |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 115 | 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 Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 128 | |
| 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 | } |