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> |
| 8 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 9 | #include "gflags/gflags.h" |
| 10 | #include "glog/logging.h" |
| 11 | |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 12 | #include "aos/init.h" |
| 13 | #include "aos/realtime.h" |
| 14 | #include "aos/time/time.h" |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 15 | |
| 16 | namespace chrono = std::chrono; |
| 17 | |
| 18 | DEFINE_string(file, "/media/sda1/foo", "File to write to."); |
| 19 | |
| 20 | DEFINE_uint32(write_size, 4096, "Size of hunk to write"); |
James Kuszmaul | 4b20807 | 2023-05-03 09:39:32 -0500 | [diff] [blame^] | 21 | DEFINE_int32(nice, -20, |
| 22 | "Priority to nice to. Set to 0 to not change the priority."); |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 23 | 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] | 24 | DEFINE_bool(writev, false, "If true, use writev."); |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 25 | DEFINE_bool(direct, false, "If true, O_DIRECT."); |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 26 | DEFINE_uint32(chunks, 1, "Chunks to write using writev."); |
| 27 | DEFINE_uint32(chunk_size, 512, "Chunk size to write using writev."); |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 28 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 29 | int main(int argc, char **argv) { |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 30 | 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 Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 51 | 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 Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 59 | 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 Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 63 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 64 | int fd = |
| 65 | open(FLAGS_file.c_str(), |
| 66 | O_RDWR | O_CLOEXEC | O_CREAT | (FLAGS_direct ? O_DIRECT : 0), 0774); |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 67 | PCHECK(fd != -1); |
| 68 | |
| 69 | const aos::monotonic_clock::time_point start_time = |
| 70 | aos::monotonic_clock::now(); |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 71 | aos::monotonic_clock::time_point last_time = start_time; |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 72 | size_t last_written_data = 0; |
| 73 | size_t written_data = 0; |
| 74 | |
James Kuszmaul | 4b20807 | 2023-05-03 09:39:32 -0500 | [diff] [blame^] | 75 | if (FLAGS_nice != 0) { |
| 76 | PCHECK(-1 != setpriority(PRIO_PROCESS, 0, FLAGS_nice)) |
| 77 | << ": Renicing to " << FLAGS_nice << " failed"; |
| 78 | } |
| 79 | |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 80 | while (true) { |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 81 | 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 Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 94 | |
| 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 | } |