Debian Codegraph @

unittest/gunit/innodb/os0file-t.cc

raw symbols SCIP
1 /*****************************************************************************
2
3 Copyright (c) 2020, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27 #include <gtest/gtest.h>
28 #include <chrono>
29
30 #include "os0file.h"
31
32 extern bool srv_use_fdatasync;
33
34 class os0file_t : public ::testing::Test {
35 protected:
36 void SetUp() override {
37 bool success;
38 os_file_delete_if_exists_func(TEST_FILE_NAME, nullptr);
39 test_file = os_file_create_func(TEST_FILE_NAME, OS_FILE_CREATE,
40 OS_BUFFERED_FILE, false, &success);
41 EXPECT_TRUE(success);
42 }
43
44 void TearDown() override {
45 os_file_close_func(test_file.m_file);
46 os_file_delete_func(TEST_FILE_NAME);
47 }
48
49 dberr_t write_test_data(const void *data, size_t len,
50 std::chrono::nanoseconds &duration) {
51 IORequest request(IORequest::WRITE);
52
53 auto name = TEST_FILE_NAME;
54 auto begin = std::chrono::high_resolution_clock::now();
55 auto db_err =
56 os_file_write_func(request, name, test_file.m_file, data, 0, len);
57 auto end = std::chrono::high_resolution_clock::now();
58
59 duration = end - begin;
60 return db_err;
61 }
62
63 dberr_t read_test_data(void *data, size_t len,
64 std::chrono::nanoseconds &duration) {
65 IORequest read_request(IORequest::READ);
66 read_request.disable_compression();
67 read_request.clear_encrypted();
68
69 auto name = TEST_FILE_NAME;
70 auto begin = std::chrono::high_resolution_clock::now();
71 auto db_err =
72 os_file_read_func(read_request, name, test_file.m_file, data, 0, len);
73 auto end = std::chrono::high_resolution_clock::now();
74
75 duration = end - begin;
76 return db_err;
77 }
78
79 bool flush_test_data(std::chrono::nanoseconds &duration) {
80 auto begin = std::chrono::high_resolution_clock::now();
81 auto success = os_file_flush_func(test_file.m_file);
82 auto end = std::chrono::high_resolution_clock::now();
83
84 duration = end - begin;
85 return success;
86 }
87
88 void write_read_flush(const void *data, void *buffer, const size_t len,
89 std::chrono::nanoseconds &write_duration_total,
90 std::chrono::nanoseconds &read_duration_total,
91 std::chrono::nanoseconds &flush_duration_total) {
92 using namespace std::chrono;
93 nanoseconds write_duration(0);
94 auto write_err = write_test_data(data, len, write_duration);
95 EXPECT_EQ(write_err, DB_SUCCESS);
96 write_duration_total += write_duration;
97
98 nanoseconds read_duration(0);
99 auto read_err = read_test_data(buffer, len, read_duration);
100 EXPECT_EQ(read_err, DB_SUCCESS);
101 EXPECT_FALSE(memcmp(buffer, data, len));
102 read_duration_total += read_duration;
103
104 nanoseconds flush_duration(0);
105 auto success = flush_test_data(flush_duration);
106 EXPECT_TRUE(success);
107 flush_duration_total += flush_duration;
108 }
109
110 void write_read_flush_loop(const void *data, void *buffer, const size_t len,
111 const int loops) {
112 using namespace std::chrono;
113 nanoseconds flushes(0);
114 nanoseconds writes(0);
115 nanoseconds reads(0);
116
117 for (int i = 0; i < loops; ++i) {
118 write_read_flush(data, buffer, len, writes, reads, flushes);
119 }
120 auto writes_ms = duration_cast<milliseconds>(writes).count();
121 auto reads_ms = duration_cast<milliseconds>(reads).count();
122 auto flushes_ms = duration_cast<milliseconds>(flushes).count();
123 std::cout << "Write duration total: " << writes_ms << " ms" << std::endl;
124 std::cout << "Read duration total: " << reads_ms << " ms" << std::endl;
125 std::cout << "Flush duration total: " << flushes_ms << " ms" << std::endl;
126 }
127
128 pfs_os_file_t test_file;
129 static constexpr char TEST_FILE_NAME[] = "os0file-t-temp.txt";
130 };
131
132 TEST_F(os0file_t, hundred_10_byte_writes_reads_flushes_with_fsync) {
133 srv_use_fdatasync = false;
134 static constexpr char TEST_DATA[] = "testdata42";
135 static constexpr size_t LEN = sizeof(TEST_DATA);
136 char buffer[LEN];
137 write_read_flush_loop(TEST_DATA, buffer, LEN, 100);
138 }
139
140 TEST_F(os0file_t, hundred_10_byte_writes_reads_flushes_with_fdatasync) {
141 srv_use_fdatasync = true;
142 static constexpr char TEST_DATA[] = "testdata42";
143 static constexpr size_t LEN = sizeof(TEST_DATA);
144 char buffer[LEN];
145 write_read_flush_loop(TEST_DATA, buffer, LEN, 100);
146 }
147
148 /* The tests below were used to measure execution times in various scenarios.
149 They perform loops of large writes and many fsyncs/fdatasyncs so they last a
150 while. Disabled prefix makes it so that they don't execute with the
151 merge_innodb_tests-t suite, but we can manually run them by providing the:
152 --gtest_also_run_disabled_tests flag during execution */
153 TEST_F(os0file_t,
154 DISABLED_ten_thousand_1_byte_writes_reads_flushes_with_fsync) {
155 srv_use_fdatasync = false;
156 static constexpr char TEST_DATA[] = "!";
157 static constexpr size_t LEN = sizeof(TEST_DATA);
158 char buffer[LEN];
159 write_read_flush_loop(TEST_DATA, buffer, LEN, 10000);
160 }
161
162 TEST_F(os0file_t,
163 DISABLED_ten_thousand_1_byte_writes_reads_flushes_with_fdatasync) {
164 srv_use_fdatasync = true;
165 static constexpr char TEST_DATA[] = "testdata42";
166 static constexpr size_t LEN = sizeof(TEST_DATA);
167 char buffer[LEN];
168 write_read_flush_loop(TEST_DATA, buffer, LEN, 10000);
169 }
170
171 TEST_F(os0file_t, DISABLED_thousand_10_byte_writes_reads_flushes_with_fsync) {
172 srv_use_fdatasync = false;
173 static constexpr char TEST_DATA[] = "testdata42";
174 static constexpr size_t LEN = sizeof(TEST_DATA);
175 char buffer[LEN];
176 write_read_flush_loop(TEST_DATA, buffer, LEN, 1000);
177 }
178
179 TEST_F(os0file_t,
180 DISABLED_thousand_10_byte_writes_reads_flushes_with_fdatasync) {
181 srv_use_fdatasync = true;
182 static constexpr char TEST_DATA[] = "testdata42";
183 static constexpr size_t LEN = sizeof(TEST_DATA);
184 char buffer[LEN];
185 write_read_flush_loop(TEST_DATA, buffer, LEN, 1000);
186 }
187
188 TEST_F(os0file_t, DISABLED_thousand_1000_byte_writes_reads_flushes_with_fsync) {
189 srv_use_fdatasync = false;
190
191 constexpr int LEN = 1000;
192 char data[LEN];
193 char buffer[LEN];
194 for (int i = 0; i < LEN; ++i) {
195 data[i] = 'a' + i % ('z' - 'a' + 1);
196 }
197
198 write_read_flush_loop(data, buffer, LEN, 1000);
199 }
200
201 TEST_F(os0file_t,
202 DISABLED_thousand_1000_byte_writes_reads_flushes_with_fdatasync) {
203 srv_use_fdatasync = true;
204
205 constexpr int LEN = 1000;
206 char data[LEN];
207 char buffer[LEN];
208 for (int i = 0; i < LEN; ++i) {
209 data[i] = 'a' + i % ('z' - 'a' + 1);
210 }
211
212 write_read_flush_loop(data, buffer, LEN, 1000);
213 }
214
215 TEST_F(os0file_t, DISABLED_thousand_1M_byte_writes_reads_flushes_with_fsync) {
216 srv_use_fdatasync = false;
217
218 constexpr int LEN = 1000000;
219 char data[LEN];
220 char buffer[LEN];
221 for (int i = 0; i < LEN; ++i) {
222 data[i] = 'a' + i % ('z' - 'a' + 1);
223 }
224
225 write_read_flush_loop(data, buffer, LEN, 1000);
226 }
227
228 TEST_F(os0file_t,
229 DISABLED_thousand_1M_byte_writes_reads_flushes_with_fdatasync) {
230 srv_use_fdatasync = true;
231
232 constexpr int LEN = 1000000;
233 char data[LEN];
234 char buffer[LEN];
235 for (int i = 0; i < LEN; ++i) {
236 data[i] = 'a' + i % ('z' - 'a' + 1);
237 }
238
239 write_read_flush_loop(data, buffer, LEN, 1000);
240 }