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