Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(547)

Side by Side Diff: sql/connection_unittest.cc

Issue 1349863003: [sql] Use memory-mapped I/O for sql::Connection. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: style nit, rebase Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/files/file_util.h" 6 #include "base/files/file_util.h"
7 #include "base/files/memory_mapped_file.h"
7 #include "base/files/scoped_file.h" 8 #include "base/files/scoped_file.h"
8 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
9 #include "base/logging.h" 10 #include "base/logging.h"
10 #include "base/metrics/statistics_recorder.h" 11 #include "base/metrics/statistics_recorder.h"
12 #include "base/strings/stringprintf.h"
11 #include "base/test/histogram_tester.h" 13 #include "base/test/histogram_tester.h"
12 #include "sql/connection.h" 14 #include "sql/connection.h"
13 #include "sql/correct_sql_test_base.h" 15 #include "sql/correct_sql_test_base.h"
14 #include "sql/meta_table.h" 16 #include "sql/meta_table.h"
15 #include "sql/statement.h" 17 #include "sql/statement.h"
16 #include "sql/test/error_callback_support.h" 18 #include "sql/test/error_callback_support.h"
17 #include "sql/test/scoped_error_ignorer.h" 19 #include "sql/test/scoped_error_ignorer.h"
18 #include "sql/test/test_helpers.h" 20 #include "sql/test/test_helpers.h"
19 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/sqlite/sqlite3.h" 22 #include "third_party/sqlite/sqlite3.h"
(...skipping 1270 matching lines...) Expand 10 before | Expand all | Expand 10 after
1291 1293
1292 samples = tester.GetHistogramSamplesSinceCreation(kCommitTime); 1294 samples = tester.GetHistogramSamplesSinceCreation(kCommitTime);
1293 ASSERT_TRUE(samples); 1295 ASSERT_TRUE(samples);
1294 // 100 for commit adjust, 1 for measuring COMMIT. 1296 // 100 for commit adjust, 1 for measuring COMMIT.
1295 EXPECT_EQ(101, samples->sum()); 1297 EXPECT_EQ(101, samples->sum());
1296 1298
1297 samples = tester.GetHistogramSamplesSinceCreation(kAutoCommitTime); 1299 samples = tester.GetHistogramSamplesSinceCreation(kAutoCommitTime);
1298 EXPECT_EQ(0, samples->sum()); 1300 EXPECT_EQ(0, samples->sum());
1299 } 1301 }
1300 1302
1301 } // namespace 1303 // Make sure that OS file writes to a mmap'ed file are reflected in the memory
1304 // mapping of a memory-mapped file. Normally SQLite writes to memory-mapped
1305 // files using memcpy(), which should stay consistent. Our SQLite is slightly
1306 // patched to mmap read only, then write using OS file writes. If the
1307 // memory-mapped version doesn't reflect the OS file writes, SQLite's
1308 // memory-mapped I/O should be disabled on this platform.
1309 TEST_F(SQLConnectionTest, MmapTest) {
1310 {
1311 sql::Statement s(db().GetUniqueStatement("PRAGMA mmap_size"));
1312 ASSERT_TRUE(s.Step());
1313 EXPECT_GT(s.ColumnInt64(0), 0);
1314 }
1315 db().Close();
1316 const uint32 kFlags =
1317 base::File::FLAG_OPEN_ALWAYS|base::File::FLAG_READ|base::File::FLAG_WRITE;
1318 base::File f(db_path(), kFlags);
1319 ASSERT_TRUE(f.IsValid());
1320
1321 // Create a file with a block of '0', a block of '1', and a block of '2'.
1322 char buf[4096];
1323 memset(buf, '0', sizeof(buf));
1324 ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf));
1325
1326 memset(buf, '1', sizeof(buf));
1327 ASSERT_EQ(f.Write(1*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf));
1328
1329 memset(buf, '2', sizeof(buf));
1330 ASSERT_EQ(f.Write(2*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf));
1331
1332 // mmap the file and verify that everything looks right.
1333 base::MemoryMappedFile m;
1334 ASSERT_TRUE(m.Initialize(db_path()));
1335
1336 memset(buf, '0', sizeof(buf));
1337 ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf)));
1338
1339 memset(buf, '1', sizeof(buf));
1340 ASSERT_EQ(0, memcmp(buf, m.data() + 1*sizeof(buf), sizeof(buf)));
1341
1342 memset(buf, '2', sizeof(buf));
1343 ASSERT_EQ(0, memcmp(buf, m.data() + 2*sizeof(buf), sizeof(buf)));
1344
1345 // Scribble some '3' into the first page of the file, and verify that it looks
1346 // the same in the memory mapping.
1347 memset(buf, '3', sizeof(buf));
1348 ASSERT_EQ(f.Write(0*sizeof(buf), buf, sizeof(buf)), (int)sizeof(buf));
1349 ASSERT_EQ(0, memcmp(buf, m.data() + 0*sizeof(buf), sizeof(buf)));
rmcilroy 2015/09/17 17:17:44 Are we sure that this atomic on all platforms we c
Scott Hess - ex-Googler 2015/09/17 21:14:37 Ooooh, thanks for pointing this out, I hadn't thou
1350
1351 }
1352
1353 } // namespace sql
OLDNEW
« sql/connection.cc ('K') | « sql/connection.cc ('k') | sql/statement.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698