Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 1 | #include <fcntl.h> |
| 2 | #include <sys/stat.h> |
| 3 | #include <sys/types.h> |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 4 | #include <sys/uio.h> |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 5 | |
| 6 | #include <chrono> |
| 7 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 8 | #include "gflags/gflags.h" |
| 9 | #include "glog/logging.h" |
| 10 | |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 11 | #include "aos/init.h" |
| 12 | #include "aos/realtime.h" |
| 13 | #include "aos/time/time.h" |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 14 | |
| 15 | namespace chrono = std::chrono; |
| 16 | |
| 17 | DEFINE_string(file, "/media/sda1/foo", "File to write to."); |
| 18 | |
| 19 | DEFINE_uint32(write_size, 4096, "Size of hunk to write"); |
| 20 | 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] | 21 | DEFINE_bool(writev, false, "If true, use writev."); |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 22 | DEFINE_bool(direct, false, "If true, O_DIRECT."); |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 23 | DEFINE_uint32(chunks, 1, "Chunks to write using writev."); |
| 24 | DEFINE_uint32(chunk_size, 512, "Chunk size to write using writev."); |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 25 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 26 | int main(int argc, char **argv) { |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 27 | aos::InitGoogle(&argc, &argv); |
| 28 | |
| 29 | std::vector<uint8_t> data; |
| 30 | |
| 31 | // We want uncompressible data. The easiest way to do this is to grab a good |
| 32 | // sized block from /dev/random, and then reuse it. |
| 33 | { |
| 34 | int random_fd = open("/dev/random", O_RDONLY | O_CLOEXEC); |
| 35 | PCHECK(random_fd != -1) << ": Failed to open /dev/random"; |
| 36 | data.resize(FLAGS_write_size); |
| 37 | size_t written = 0; |
| 38 | while (written < data.size()) { |
| 39 | const size_t result = |
| 40 | read(random_fd, data.data() + written, data.size() - written); |
| 41 | PCHECK(result > 0); |
| 42 | written += result; |
| 43 | } |
| 44 | |
| 45 | PCHECK(close(random_fd) == 0); |
| 46 | } |
| 47 | |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 48 | std::vector<struct iovec> iovec; |
| 49 | iovec.resize(FLAGS_chunks); |
| 50 | CHECK_LE(FLAGS_chunks * FLAGS_chunk_size, FLAGS_write_size); |
| 51 | |
| 52 | for (size_t i = 0; i < FLAGS_chunks; ++i) { |
| 53 | iovec[i].iov_base = &data[i * FLAGS_chunk_size]; |
| 54 | iovec[i].iov_len = FLAGS_chunk_size; |
| 55 | } |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 56 | iovec[FLAGS_chunks - 1].iov_base = |
| 57 | &data[(FLAGS_chunks - 1) * FLAGS_chunk_size]; |
| 58 | iovec[FLAGS_chunks - 1].iov_len = |
| 59 | data.size() - (FLAGS_chunks - 1) * FLAGS_chunk_size; |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 60 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 61 | int fd = |
| 62 | open(FLAGS_file.c_str(), |
| 63 | O_RDWR | O_CLOEXEC | O_CREAT | (FLAGS_direct ? O_DIRECT : 0), 0774); |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 64 | PCHECK(fd != -1); |
| 65 | |
| 66 | const aos::monotonic_clock::time_point start_time = |
| 67 | aos::monotonic_clock::now(); |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 68 | aos::monotonic_clock::time_point last_time = start_time; |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 69 | size_t last_written_data = 0; |
| 70 | size_t written_data = 0; |
| 71 | |
| 72 | while (true) { |
Austin Schuh | f97b463 | 2023-02-11 16:52:58 -0800 | [diff] [blame] | 73 | if (FLAGS_writev) { |
| 74 | PCHECK(writev(fd, iovec.data(), iovec.size()) == |
| 75 | static_cast<ssize_t>(data.size())) |
| 76 | << ": Failed after " |
| 77 | << chrono::duration<double>(aos::monotonic_clock::now() - start_time) |
| 78 | .count(); |
| 79 | } else { |
| 80 | PCHECK(write(fd, data.data(), data.size()) == |
| 81 | static_cast<ssize_t>(data.size())) |
| 82 | << ": Failed after " |
| 83 | << chrono::duration<double>(aos::monotonic_clock::now() - start_time) |
| 84 | .count(); |
| 85 | } |
Austin Schuh | e6b2b88 | 2023-02-04 11:42:40 -0800 | [diff] [blame] | 86 | |
| 87 | // Trigger a flush if asked. |
| 88 | if (FLAGS_sync) { |
| 89 | const aos::monotonic_clock::time_point monotonic_now = |
| 90 | aos::monotonic_clock::now(); |
| 91 | sync_file_range(fd, written_data, data.size(), SYNC_FILE_RANGE_WRITE); |
| 92 | |
| 93 | // Now, blocking flush the previous page so we don't get too far ahead. |
| 94 | // This is Linus' recommendation. |
| 95 | if (written_data > 0) { |
| 96 | sync_file_range(fd, written_data - data.size(), data.size(), |
| 97 | SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE | |
| 98 | SYNC_FILE_RANGE_WAIT_AFTER); |
| 99 | posix_fadvise(fd, written_data - data.size(), data.size(), |
| 100 | POSIX_FADV_DONTNEED); |
| 101 | } |
| 102 | VLOG(1) << "Took " |
| 103 | << chrono::duration<double>(aos::monotonic_clock::now() - |
| 104 | monotonic_now) |
| 105 | .count(); |
| 106 | } |
| 107 | |
| 108 | written_data += data.size(); |
| 109 | |
| 110 | const aos::monotonic_clock::time_point monotonic_now = |
| 111 | aos::monotonic_clock::now(); |
| 112 | // Print out MB/s once it has been at least 1 second since last time. |
| 113 | if (monotonic_now > last_time + chrono::seconds(1)) { |
| 114 | LOG(INFO) |
| 115 | << ((written_data - last_written_data) / |
| 116 | chrono::duration<double>(monotonic_now - last_time).count() / |
| 117 | 1024. / 1024.) |
| 118 | << " MB/s"; |
| 119 | last_time = monotonic_now; |
| 120 | last_written_data = written_data; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |