diff options
Diffstat (limited to 'net/tools/crash_cache/crash_cache.cc')
-rw-r--r-- | net/tools/crash_cache/crash_cache.cc | 337 |
1 files changed, 337 insertions, 0 deletions
diff --git a/net/tools/crash_cache/crash_cache.cc b/net/tools/crash_cache/crash_cache.cc new file mode 100644 index 0000000..859cf94 --- /dev/null +++ b/net/tools/crash_cache/crash_cache.cc @@ -0,0 +1,337 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// This command-line program generates the set of files needed for the crash- +// cache unit tests (DiskCacheTest,CacheBackend_Recover*). This program only +// works properly on debug mode, because the crash functionality is not compiled +// on release builds of the cache. + +#include <windows.h> +#include <string> + +#include "base/file_util.h" +#include "base/logging.h" +#include "base/message_loop.h" +#include "base/path_service.h" +#include "base/string_util.h" + +#include "net/disk_cache/backend_impl.h" +#include "net/disk_cache/disk_cache.h" +#include "net/disk_cache/disk_cache_test_util.h" +#include "net/disk_cache/rankings.h" + +enum Errors { + GENERIC = -1, + ALL_GOOD = 0, + INVALID_ARGUMENT = 1, + CRASH_OVERWRITE, + NOT_REACHED +}; + +using disk_cache::RankCrashes; + +// Starts a new process, to generate the files. +int RunSlave(RankCrashes action) { + std::wstring exe; + PathService::Get(base::FILE_EXE, &exe); + + std::wstring command = StringPrintf(L"%s %d", exe.c_str(), action); + + STARTUPINFO startup_info = {0}; + startup_info.cb = sizeof(startup_info); + PROCESS_INFORMATION process_info; + + // I really don't care about this call modifying the string. + if (!::CreateProcess(exe.c_str(), const_cast<wchar_t*>(command.c_str()), NULL, + NULL, FALSE, 0, NULL, NULL, &startup_info, + &process_info)) { + printf("Unable to run test %d\n", action); + return GENERIC; + } + + DWORD reason = ::WaitForSingleObject(process_info.hProcess, INFINITE); + + int code; + bool ok = ::GetExitCodeProcess(process_info.hProcess, + reinterpret_cast<PDWORD>(&code)) ? true : + false; + + ::CloseHandle(process_info.hProcess); + ::CloseHandle(process_info.hThread); + + if (!ok) { + printf("Unable to get return code, test %d\n", action); + return GENERIC; + } + + if (ALL_GOOD != code) + printf("Test %d failed, code %d\n", action, code); + + return code; +} + +// Main loop for the master process. +int MasterCode() { + for (int i = disk_cache::NO_CRASH + 1; i < disk_cache::MAX_CRASH; i++) { + int ret = RunSlave(static_cast<RankCrashes>(i)); + if (ALL_GOOD != ret) + return ret; + } + + return ALL_GOOD; +} + +// ----------------------------------------------------------------------- + +extern RankCrashes g_rankings_crash; +const char* kCrashEntryName = "the first key"; + +// Creates the destinaton folder for this run, and returns it on full_path. +bool CreateTargetFolder(const std::wstring& path, RankCrashes action, + std::wstring* full_path) { + const wchar_t* folders[] = { + L"", + L"insert_empty1", + L"insert_empty2", + L"insert_empty3", + L"insert_one1", + L"insert_one2", + L"insert_one3", + L"insert_load1", + L"insert_load2", + L"remove_one1", + L"remove_one2", + L"remove_one3", + L"remove_one4", + L"remove_head1", + L"remove_head2", + L"remove_head3", + L"remove_head4", + L"remove_tail1", + L"remove_tail2", + L"remove_tail3", + L"remove_load1", + L"remove_load2", + L"remove_load3" + }; + COMPILE_ASSERT(arraysize(folders) == disk_cache::MAX_CRASH, sync_folders); + DCHECK(action > disk_cache::NO_CRASH && action < disk_cache::MAX_CRASH); + + *full_path = path; + file_util::AppendToPath(full_path, folders[action]); + + return file_util::CreateDirectory(*full_path); +} + +// Generates the files for an empty and one item cache. +int SimpleInsert(const std::wstring& path, RankCrashes action) { + disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false, 0); + if (!cache || cache->GetEntryCount()) + return GENERIC; + + const char* test_name = "some other key"; + + if (action <= disk_cache::INSERT_EMPTY_3) { + test_name = kCrashEntryName; + g_rankings_crash = action; + } + + disk_cache::Entry* entry; + if (!cache->CreateEntry(test_name, &entry)) + return GENERIC; + + entry->Close(); + + DCHECK(action <= disk_cache::INSERT_ONE_3); + g_rankings_crash = action; + test_name = kCrashEntryName; + + if (!cache->CreateEntry(test_name, &entry)) + return GENERIC; + + return NOT_REACHED; +} + +// Generates the files for a one item cache, and removing the head. +int SimpleRemove(const std::wstring& path, RankCrashes action) { + DCHECK(action >= disk_cache::REMOVE_ONE_1); + DCHECK(action <= disk_cache::REMOVE_TAIL_3); + + disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false, 0); + if (!cache || cache->GetEntryCount()) + return GENERIC; + + disk_cache::Entry* entry; + if (!cache->CreateEntry(kCrashEntryName, &entry)) + return GENERIC; + + entry->Close(); + + if (action >= disk_cache::REMOVE_TAIL_1) { + if (!cache->CreateEntry("some other key", &entry)) + return GENERIC; + + entry->Close(); + } + + if (!cache->OpenEntry(kCrashEntryName, &entry)) + return GENERIC; + + g_rankings_crash = action; + entry->Doom(); + entry->Close(); + + return NOT_REACHED; +} + +int HeadRemove(const std::wstring& path, RankCrashes action) { + DCHECK(action >= disk_cache::REMOVE_HEAD_1); + DCHECK(action <= disk_cache::REMOVE_HEAD_4); + + disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false, 0); + if (!cache || cache->GetEntryCount()) + return GENERIC; + + disk_cache::Entry* entry; + if (!cache->CreateEntry("some other key", &entry)) + return GENERIC; + + entry->Close(); + if (!cache->CreateEntry(kCrashEntryName, &entry)) + return GENERIC; + + entry->Close(); + + if (!cache->OpenEntry(kCrashEntryName, &entry)) + return GENERIC; + + g_rankings_crash = action; + entry->Doom(); + entry->Close(); + + return NOT_REACHED; +} + +// Generates the files for insertion and removals on heavy loaded caches. +int LoadOperations(const std::wstring& path, RankCrashes action) { + DCHECK(action >= disk_cache::INSERT_LOAD_1); + + // Work with a tiny index table (16 entries) + disk_cache::BackendImpl* cache = new disk_cache::BackendImpl(path, 0xf); + if (!cache || !cache->SetMaxSize(0x100000) || !cache->Init() || + cache->GetEntryCount()) + return GENERIC; + + int seed = static_cast<int>(Time::Now().ToInternalValue()); + srand(seed); + + disk_cache::Entry* entry; + for (int i = 0; i < 100; i++) { + std::string key = GenerateKey(true); + if (!cache->CreateEntry(key, &entry)) + return GENERIC; + entry->Close(); + if (50 == i && action >= disk_cache::REMOVE_LOAD_1) { + if (!cache->CreateEntry(kCrashEntryName, &entry)) + return GENERIC; + entry->Close(); + } + } + + if (action <= disk_cache::INSERT_LOAD_2) { + g_rankings_crash = action; + + if (!cache->CreateEntry(kCrashEntryName, &entry)) + return GENERIC; + } + + if (!cache->OpenEntry(kCrashEntryName, &entry)) + return GENERIC; + + g_rankings_crash = action; + + entry->Doom(); + entry->Close(); + + return NOT_REACHED; +} + +// Main function on the child process. +int SlaveCode(const std::wstring& path, RankCrashes action) { + MessageLoop message_loop; + + std::wstring full_path; + if (!CreateTargetFolder(path, action, &full_path)) { + printf("Destination folder found, please remove it.\n"); + return CRASH_OVERWRITE; + } + + if (action <= disk_cache::INSERT_ONE_3) + return SimpleInsert(full_path, action); + + if (action <= disk_cache::INSERT_LOAD_2) + return LoadOperations(full_path, action); + + if (action <= disk_cache::REMOVE_ONE_4) + return SimpleRemove(full_path, action); + + if (action <= disk_cache::REMOVE_HEAD_4) + return HeadRemove(full_path, action); + + if (action <= disk_cache::REMOVE_TAIL_3) + return SimpleRemove(full_path, action); + + if (action <= disk_cache::REMOVE_LOAD_3) + return LoadOperations(full_path, action); + + return NOT_REACHED; +} + +// ----------------------------------------------------------------------- + +int main(int argc, const char* argv[]) { + if (argc < 2) + return MasterCode(); + + char* end; + RankCrashes action = static_cast<RankCrashes>(strtol(argv[1], &end, 0)); + if (action <= disk_cache::NO_CRASH || action >= disk_cache::MAX_CRASH) { + printf("Invalid action\n"); + return INVALID_ARGUMENT; + } + + std::wstring path; + PathService::Get(base::DIR_SOURCE_ROOT, &path); + file_util::AppendToPath(&path, L"net"); + file_util::AppendToPath(&path, L"data"); + file_util::AppendToPath(&path, L"cache_tests"); + file_util::AppendToPath(&path, L"new_crashes"); + + return SlaveCode(path, action); +} |