0

Un-duplicate EvictFileFromSystemCache.

Change required for http://codereview.chromium.org/9639 .

Review URL: http://codereview.chromium.org/12407
Patch from Pawel Hajdan Jr.

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6006 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
deanm@chromium.org
2008-11-25 23:10:46 +00:00
parent da4e34b86c
commit 60aac62ddf
33 changed files with 189 additions and 208 deletions

@ -134,6 +134,7 @@
829E365F0DC0FB1C00819EBF /* stats_table.cc in Sources */ = {isa = PBXBuildFile; fileRef = 825403790D92D2CF0006B936 /* stats_table.cc */; };
829E36730DC0FBAD00819EBF /* thread_local_storage_posix.cc in Sources */ = {isa = PBXBuildFile; fileRef = 829E36720DC0FBAD00819EBF /* thread_local_storage_posix.cc */; };
82D094550E5B892600FEC05C /* time_format.cc in Sources */ = {isa = PBXBuildFile; fileRef = 82D094540E5B892600FEC05C /* time_format.cc */; };
84B581DC0EDC715A00A6B5B3 /* test_file_util_mac.cc in Sources */ = {isa = PBXBuildFile; fileRef = 84B581950EDC6ECC00A6B5B3 /* test_file_util_mac.cc */; };
9301C03B0E54C839001EF103 /* waitable_event_generic.cc in Sources */ = {isa = PBXBuildFile; fileRef = 9301C0390E54C839001EF103 /* waitable_event_generic.cc */; };
93611ADF0E5A7FC500F9405D /* message_pump_default.cc in Sources */ = {isa = PBXBuildFile; fileRef = 93611ADC0E5A7FC500F9405D /* message_pump_default.cc */; };
93611AE10E5A7FE200F9405D /* message_loop.cc in Sources */ = {isa = PBXBuildFile; fileRef = 93611AE00E5A7FE200F9405D /* message_loop.cc */; };
@ -581,6 +582,8 @@
82D094530E5B892600FEC05C /* time_format.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = time_format.h; sourceTree = "<group>"; };
82D094540E5B892600FEC05C /* time_format.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = time_format.cc; sourceTree = "<group>"; };
82E23FCB0D9C219600F8B40A /* platform_thread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = platform_thread.h; sourceTree = "<group>"; };
84B581950EDC6ECC00A6B5B3 /* test_file_util_mac.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = test_file_util_mac.cc; sourceTree = "<group>"; };
84B5819B0EDC6FBC00A6B5B3 /* test_file_util.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = test_file_util.h; sourceTree = "<group>"; };
9301C0390E54C839001EF103 /* waitable_event_generic.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = waitable_event_generic.cc; sourceTree = "<group>"; };
9301C03A0E54C839001EF103 /* waitable_event_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = waitable_event_unittest.cc; sourceTree = "<group>"; };
93611ADA0E5A7FC500F9405D /* message_loop_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = message_loop_unittest.cc; sourceTree = "<group>"; };
@ -1014,6 +1017,8 @@
E4CE9D770E8C1FCA00D5378C /* system_monitor.h */,
E4CE9D760E8C1FCA00D5378C /* system_monitor_unittest.cc */,
8254037E0D92D2CF0006B936 /* task.h */,
84B5819B0EDC6FBC00A6B5B3 /* test_file_util.h */,
84B581950EDC6ECC00A6B5B3 /* test_file_util_mac.cc */,
93E703230E5D64F00046259B /* thread.cc */,
825403800D92D2CF0006B936 /* thread.h */,
7BAE38A80E6EFD9900C3F750 /* thread_local.h */,
@ -1411,6 +1416,7 @@
7B6AF6340E80211C00F9F9CF /* sys_info_posix.cc in Sources */,
7B4C5F4A0E4B6BF900679E8F /* sys_string_conversions_mac.mm in Sources */,
E4CE9D7A0E8C1FD400D5378C /* system_monitor.cc in Sources */,
84B581DC0EDC715A00A6B5B3 /* test_file_util_mac.cc in Sources */,
93E703240E5D64F00046259B /* thread.cc in Sources */,
7BAE38AC0E6EFDBA00C3F750 /* thread_local_posix.cc in Sources */,
829E36730DC0FBAD00819EBF /* thread_local_storage_posix.cc in Sources */,

@ -833,6 +833,14 @@
RelativePath="..\task.h"
>
</File>
<File
RelativePath="..\test_file_util.cc"
>
</File>
<File
RelativePath="..\test_file_util.h"
>
</File>
<File
RelativePath="..\thread.cc"
>

29
base/test_file_util.h Normal file

@ -0,0 +1,29 @@
// Copyright (c) 2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_TEST_FILE_UTIL_H_
#define BASE_TEST_FILE_UTIL_H_
// File utility functions used only by tests.
#include <string>
namespace file_util {
// Clear a specific file from the system cache. After this call, trying
// to access this file will result in a cold load from the hard drive.
bool EvictFileFromSystemCache(const wchar_t* file);
// Like CopyFileNoCache but recursively copies all files and subdirectories
// in the given input directory to the output directory. Any files in the
// destination that already exist will be overwritten.
//
// Returns true on success. False means there was some error copying, so the
// state of the destination is unknown.
bool CopyRecursiveDirNoCache(const std::wstring& source_dir,
const std::wstring& dest_dir);
} // namespace file_util
#endif // BASE_TEST_FILE_UTIL_H_

@ -0,0 +1,28 @@
// Copyright (c) 2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/test_file_util.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "base/file_path.h"
namespace file_util {
bool EvictFileFromSystemCache(const wchar_t* file) {
FilePath fpath = FilePath::FromWStringHack(file);
int fd = open(fpath.value().c_str(), O_RDONLY);
if (fd < 0)
return false;
if (fdatasync(fd) != 0)
return false;
if (posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) != 0)
return false;
close(fd);
return true;
}
} // namespace file_util

@ -0,0 +1,17 @@
// Copyright (c) 2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/test_file_util.h"
#include "base/logging.h"
namespace file_util {
bool EvictFileFromSystemCache(const wchar_t* file) {
// TODO(port): Implement.
NOTIMPLEMENTED();
return false;
}
} // namespace file_util

100
base/test_file_util_win.cc Normal file

@ -0,0 +1,100 @@
// Copyright (c) 2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/test_file_util.h"
#include <windows.h>
#include <vector>
#include "base/file_util.h"
namespace file_util {
bool EvictFileFromSystemCache(const wchar_t* file) {
// Request exclusive access to the file and overwrite it with no buffering.
win_util::ScopedHandle hfile(
CreateFile(file, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_FLAG_NO_BUFFERING,
NULL));
if (!hfile)
return false;
// Execute in chunks. It could be optimized. We want to do few of these since
// these opterations will be slow without the cache.
char buffer[4096];
int total_bytes = 0;
DWORD bytes_read;
for (;;) {
bytes_read = 0;
ReadFile(hfile, buffer, sizeof(buffer), &bytes_read, NULL);
if (bytes_read == 0)
break;
SetFilePointer(hfile, total_bytes, 0, FILE_BEGIN);
if (!WriteFile(hfile, buffer, bytes_read, &bytes_read, NULL))
return false;
total_bytes += bytes_read;
}
return true;
}
// Like CopyFileNoCache but recursively copies all files and subdirectories
// in the given input directory to the output directory.
bool CopyRecursiveDirNoCache(const std::wstring& source_dir,
const std::wstring& dest_dir) {
// Try to create the directory if it doesn't already exist.
if (!CreateDirectory(dest_dir)) {
if (GetLastError() != ERROR_ALREADY_EXISTS)
return false;
}
std::vector<std::wstring> files_copied;
std::wstring src(source_dir);
file_util::AppendToPath(&src, L"*");
WIN32_FIND_DATA fd;
HANDLE fh = FindFirstFile(src.c_str(), &fd);
if (fh == INVALID_HANDLE_VALUE)
return false;
do {
std::wstring cur_file(fd.cFileName);
if (cur_file == L"." || cur_file == L"..")
continue; // Skip these special entries.
std::wstring cur_source_path(source_dir);
file_util::AppendToPath(&cur_source_path, cur_file);
std::wstring cur_dest_path(dest_dir);
file_util::AppendToPath(&cur_dest_path, cur_file);
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
// Recursively copy a subdirectory. We stripped "." and ".." already.
if (!CopyRecursiveDirNoCache(cur_source_path, cur_dest_path)) {
FindClose(fh);
return false;
}
} else {
// Copy the file.
if (!::CopyFile(cur_source_path.c_str(), cur_dest_path.c_str(), false)) {
FindClose(fh);
return false;
}
// We don't check for errors from this function, often, we are copying
// files that are in the repository, and they will have read-only set.
// This will prevent us from evicting from the cache, but these don't
// matter anyway.
EvictFileFromSystemCache(cur_dest_path.c_str());
}
} while (FindNextFile(fh, &fd));
FindClose(fh);
return true;
}
} // namespace file_util

@ -66,7 +66,6 @@ if env['PLATFORM'] == 'win32':
input_files = [
'automated_ui_tests.cc',
'$CHROME_DIR/test/test_file_util$OBJSUFFIX',
'$CHROME_DIR/test/perf/mem_usage$OBJSUFFIX',
'$CHROME_DIR/test/ui/run_all_unittests$OBJSUFFIX',
'$CHROME_DIR/test/ui/ui_test$OBJSUFFIX',

@ -161,10 +161,6 @@
RelativePath="..\ui\run_all_unittests.cc"
>
</File>
<File
RelativePath="..\test_file_util.cc"
>
</File>
<File
RelativePath="..\testing_browser_process.h"
>

@ -170,10 +170,6 @@
RelativePath="..\ui\run_all_unittests.cc"
>
</File>
<File
RelativePath="..\test_file_util.cc"
>
</File>
<File
RelativePath="..\testing_profile.cc"
>

@ -95,7 +95,6 @@ input_files = [
'$CHROME_DIR/browser/views/constrained_window_impl_interactive_uitest.cc',
'$CHROME_DIR/browser/views/tabs/tab_dragging_test.cc',
'$CHROME_DIR/test/interactive_ui/npapi_interactive_test.cc',
'$CHROME_DIR/test/test_file_util$OBJSUFFIX',
'$CHROME_DIR/test/testing_profile$OBJSUFFIX',
'$CHROME_DIR/test/ui/npapi_test_helper$OBJSUFFIX',
'$CHROME_DIR/test/ui/run_all_unittests$OBJSUFFIX',

@ -59,7 +59,6 @@ input_files = [
'$CHROME_DIR/test/ui/run_all_unittests$OBJSUFFIX',
'$CHROME_DIR/test/ui/ui_test$OBJSUFFIX',
'$CHROME_DIR/test/ui/ui_test_suite$OBJSUFFIX',
'$CHROME_DIR/test/test_file_util$OBJSUFFIX',
'memory_test.cc',
]

@ -163,14 +163,6 @@
RelativePath="..\ui\run_all_unittests.cc"
>
</File>
<File
RelativePath="..\test_file_util.cc"
>
</File>
<File
RelativePath="..\test_file_util.h"
>
</File>
<File
RelativePath="..\testing_browser_process.h"
>

@ -65,7 +65,6 @@ input_files = [
'$CHROME_DIR/test/ui/run_all_unittests$OBJSUFFIX',
'$CHROME_DIR/test/ui/ui_test$OBJSUFFIX',
'$CHROME_DIR/test/ui/ui_test_suite$OBJSUFFIX',
'$CHROME_DIR/test/test_file_util$OBJSUFFIX',
'page_cycler_test.cc',
]

@ -163,10 +163,6 @@
RelativePath="..\ui\run_all_unittests.cc"
>
</File>
<File
RelativePath="..\test_file_util.cc"
>
</File>
<File
RelativePath="..\testing_browser_process.h"
>

@ -69,7 +69,6 @@ input_files = [
'$CHROME_DIR/browser/visitedlink_master$OBJSUFFIX',
'$CHROME_DIR/browser/visitedlink_perftest.cc',
'$CHROME_DIR/common/json_value_serializer_perftest.cc',
'$CHROME_DIR/test/test_file_util$OBJSUFFIX',
]
env.ChromeTestProgram('perf_tests', input_files)

@ -168,14 +168,6 @@
RelativePath="..\..\..\base\perftimer.cc"
>
</File>
<File
RelativePath="..\test_file_util.cc"
>
</File>
<File
RelativePath="..\test_file_util.h"
>
</File>
</Filter>
<Filter
Name="TestURLParser"

@ -67,7 +67,6 @@ input_files = [
'$CHROME_DIR/test/ui/run_all_unittests$OBJSUFFIX',
'$CHROME_DIR/test/ui/ui_test$OBJSUFFIX',
'$CHROME_DIR/test/ui/ui_test_suite$OBJSUFFIX',
'$CHROME_DIR/test/test_file_util$OBJSUFFIX',
'plugin_test.cpp',
]

@ -164,10 +164,6 @@
RelativePath="..\ui\run_all_unittests.cc"
>
</File>
<File
RelativePath="..\test_file_util.cc"
>
</File>
<File
RelativePath="..\testing_browser_process.h"
>

@ -66,7 +66,6 @@ input_files = [
'$CHROME_DIR/test/perf/mem_usage$OBJSUFFIX',
'$CHROME_DIR/test/ui/ui_test$OBJSUFFIX',
'$CHROME_DIR/test/ui/ui_test_suite$OBJSUFFIX',
'$CHROME_DIR/test/test_file_util$OBJSUFFIX',
'page_load_test.cc',
'run_all_unittests.cc',
]

@ -167,10 +167,6 @@
RelativePath=".\run_all_unittests.cc"
>
</File>
<File
RelativePath="..\test_file_util.cc"
>
</File>
<File
RelativePath="..\testing_browser_process.h"
>

@ -69,7 +69,6 @@ input_files = [
'$CHROME_DIR/test/ui/run_all_unittests$OBJSUFFIX',
'$CHROME_DIR/test/ui/ui_test$OBJSUFFIX',
'$CHROME_DIR/test/ui/ui_test_suite$OBJSUFFIX',
'$CHROME_DIR/test/test_file_util$OBJSUFFIX',
'selenium_test.cc',
]

@ -163,10 +163,6 @@
RelativePath="..\ui\run_all_unittests.cc"
>
</File>
<File
RelativePath="..\test_file_util.cc"
>
</File>
<File
RelativePath="..\testing_browser_process.h"
>

@ -63,7 +63,6 @@ input_files = [
'$CHROME_DIR/test/ui/run_all_unittests$OBJSUFFIX',
'$CHROME_DIR/test/ui/ui_test$OBJSUFFIX',
'$CHROME_DIR/test/ui/ui_test_suite$OBJSUFFIX',
'$CHROME_DIR/test/test_file_util$OBJSUFFIX',
'feature_startup_test.cc',
'startup_test.cc',
]

@ -163,14 +163,6 @@
RelativePath="..\ui\run_all_unittests.cc"
>
</File>
<File
RelativePath="..\test_file_util.cc"
>
</File>
<File
RelativePath="..\test_file_util.h"
>
</File>
<File
RelativePath="..\testing_browser_process.h"
>

@ -163,10 +163,6 @@
RelativePath="..\ui\run_all_unittests.cc"
>
</File>
<File
RelativePath="..\test_file_util.cc"
>
</File>
<File
RelativePath="..\testing_browser_process.h"
>

@ -64,7 +64,6 @@ input_files = [
'$CHROME_DIR/test/ui/run_all_unittests$OBJSUFFIX',
'$CHROME_DIR/test/ui/ui_test$OBJSUFFIX',
'$CHROME_DIR/test/ui/ui_test_suite$OBJSUFFIX',
'$CHROME_DIR/test/test_file_util$OBJSUFFIX',
'tab_switching_test.cc',
]

@ -1,100 +0,0 @@
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <vector>
#include <windows.h>
#include "chrome/test/test_file_util.h"
#include "base/file_util.h"
#include "chrome/common/win_util.h"
namespace file_util {
bool EvictFileFromSystemCache(const wchar_t* file) {
// Request exclusive access to the file and overwrite it with no buffering.
win_util::ScopedHandle hfile(
CreateFile(file, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_FLAG_NO_BUFFERING,
NULL));
if (!hfile)
return false;
// Execute in chunks. It could be optimized. We want to do few of these since
// these opterations will be slow without the cache.
char buffer[4096];
int total_bytes = 0;
DWORD bytes_read;
for (;;) {
bytes_read = 0;
ReadFile(hfile, buffer, sizeof(buffer), &bytes_read, NULL);
if (bytes_read == 0)
break;
SetFilePointer(hfile, total_bytes, 0, FILE_BEGIN);
if (!WriteFile(hfile, buffer, bytes_read, &bytes_read, NULL))
return false;
total_bytes += bytes_read;
}
return true;
}
// Like CopyFileNoCache but recursively copies all files and subdirectories
// in the given input directory to the output directory.
bool CopyRecursiveDirNoCache(const std::wstring& source_dir,
const std::wstring& dest_dir) {
// Try to create the directory if it doesn't already exist.
if (!CreateDirectory(dest_dir)) {
if (GetLastError() != ERROR_ALREADY_EXISTS)
return false;
}
std::vector<std::wstring> files_copied;
std::wstring src(source_dir);
file_util::AppendToPath(&src, L"*");
WIN32_FIND_DATA fd;
HANDLE fh = FindFirstFile(src.c_str(), &fd);
if (fh == INVALID_HANDLE_VALUE)
return false;
do {
std::wstring cur_file(fd.cFileName);
if (cur_file == L"." || cur_file == L"..")
continue; // Skip these special entries.
std::wstring cur_source_path(source_dir);
file_util::AppendToPath(&cur_source_path, cur_file);
std::wstring cur_dest_path(dest_dir);
file_util::AppendToPath(&cur_dest_path, cur_file);
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
// Recursively copy a subdirectory. We stripped "." and ".." already.
if (!CopyRecursiveDirNoCache(cur_source_path, cur_dest_path)) {
FindClose(fh);
return false;
}
} else {
// Copy the file.
if (!::CopyFile(cur_source_path.c_str(), cur_dest_path.c_str(), false)) {
FindClose(fh);
return false;
}
// We don't check for errors from this function, often, we are copying
// files that are in the repository, and they will have read-only set.
// This will prevent us from evicting from the cache, but these don't
// matter anyway.
EvictFileFromSystemCache(cur_dest_path.c_str());
}
} while (FindNextFile(fh, &fd));
FindClose(fh);
return true;
}
} // namespace file_util

@ -1,31 +0,0 @@
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_TEST_TEST_FILE_UTIL_H__
#define CHROME_TEST_TEST_FILE_UTIL_H__
// File utility functions used only by tests.
#include <string>
namespace file_util {
// Clear a specific file from the system cache using a neat trick. After this
// call, trying to access this file will result in a cold load from the hard
// drive.
bool EvictFileFromSystemCache(const wchar_t* file);
// Like CopyFileNoCache but recursively copies all files and subdirectories
// in the given input directory to the output directory. Any files in the
// destination that already exist will be overwritten.
//
// Returns true on success. False means there was some error copying, so the
// state of the destination is unknown.
bool CopyRecursiveDirNoCache(const std::wstring& source_dir,
const std::wstring& dest_dir);
} // namespace file_util
#endif // CHROME_TEST_TEST_FILE_UTIL_H__

@ -120,7 +120,6 @@ ui_test_files = [
'$CHROME_DIR/test/automation/automation_proxy_uitest.cc',
'$CHROME_DIR/test/perf/mem_usage.cc',
'$CHROME_DIR/test/reliability/page_load_test$OBJSUFFIX',
'$CHROME_DIR/test/test_file_util$OBJSUFFIX',
'$NET_DIR/url_request/url_request_test_job$OBJSUFFIX',
]

@ -173,10 +173,6 @@
RelativePath=".\run_all_unittests.cc"
>
</File>
<File
RelativePath="..\test_file_util.cc"
>
</File>
<File
RelativePath="..\testing_browser_process.h"
>

@ -8,7 +8,7 @@
#include "base/string_piece.h"
#include "base/process_util.h"
#include "base/sys_string_conversions.h"
#include "chrome/test/test_file_util.h"
#include "base/test_file_util.h"
int main(int argc, const char* argv[]) {
base::EnableTerminationOnHeapCorruption();

@ -37,7 +37,6 @@ if env['PLATFORM'] == 'win32':
)
input_files = [
'$CHROME_DIR/test/test_file_util$OBJSUFFIX',
'flush_cache.cc',
]

@ -142,14 +142,6 @@
RelativePath=".\flush_cache.cc"
>
</File>
<File
RelativePath="..\..\..\test\test_file_util.cc"
>
</File>
<File
RelativePath="..\..\..\test\test_file_util.h"
>
</File>
</Files>
<Globals>
</Globals>