| OLD | NEW |
| 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 // This is a simple application that stress-tests the crash recovery of the disk | 5 // This is a simple application that stress-tests the crash recovery of the disk |
| 6 // cache. The main application starts a copy of itself on a loop, checking the | 6 // cache. The main application starts a copy of itself on a loop, checking the |
| 7 // exit code of the child process. When the child dies in an unexpected way, | 7 // exit code of the child process. When the child dies in an unexpected way, |
| 8 // the main application quits. | 8 // the main application quits. |
| 9 | 9 |
| 10 // The child application has two threads: one to exercise the cache in an | 10 // The child application has two threads: one to exercise the cache in an |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 void StressTheCache(int iteration) { | 101 void StressTheCache(int iteration) { |
| 102 int cache_size = 0x2000000; // 32MB. | 102 int cache_size = 0x2000000; // 32MB. |
| 103 uint32 mask = 0xfff; // 4096 entries. | 103 uint32 mask = 0xfff; // 4096 entries. |
| 104 | 104 |
| 105 base::FilePath path; | 105 base::FilePath path; |
| 106 PathService::Get(base::DIR_TEMP, &path); | 106 PathService::Get(base::DIR_TEMP, &path); |
| 107 path = path.AppendASCII("cache_test_stress"); | 107 path = path.AppendASCII("cache_test_stress"); |
| 108 | 108 |
| 109 base::Thread cache_thread("CacheThread"); | 109 base::Thread cache_thread("CacheThread"); |
| 110 if (!cache_thread.StartWithOptions( | 110 if (!cache_thread.StartWithOptions( |
| 111 base::Thread::Options(MessageLoop::TYPE_IO, 0))) | 111 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) |
| 112 return; | 112 return; |
| 113 | 113 |
| 114 disk_cache::BackendImpl* cache = | 114 disk_cache::BackendImpl* cache = |
| 115 new disk_cache::BackendImpl(path, mask, cache_thread.message_loop_proxy(), | 115 new disk_cache::BackendImpl(path, mask, |
| 116 cache_thread.message_loop_proxy().get(), |
| 116 NULL); | 117 NULL); |
| 117 cache->SetMaxSize(cache_size); | 118 cache->SetMaxSize(cache_size); |
| 118 cache->SetFlags(disk_cache::kNoLoadProtection); | 119 cache->SetFlags(disk_cache::kNoLoadProtection); |
| 119 | 120 |
| 120 net::TestCompletionCallback cb; | 121 net::TestCompletionCallback cb; |
| 121 int rv = cache->Init(cb.callback()); | 122 int rv = cache->Init(cb.callback()); |
| 122 | 123 |
| 123 if (cb.GetResult(rv) != net::OK) { | 124 if (cb.GetResult(rv) != net::OK) { |
| 124 printf("Unable to initialize cache.\n"); | 125 printf("Unable to initialize cache.\n"); |
| 125 return; | 126 return; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 net::TestCompletionCallback cb; | 162 net::TestCompletionCallback cb; |
| 162 rv = cache->OpenEntry(keys[key], &entries[slot], cb.callback()); | 163 rv = cache->OpenEntry(keys[key], &entries[slot], cb.callback()); |
| 163 if (cb.GetResult(rv) != net::OK) { | 164 if (cb.GetResult(rv) != net::OK) { |
| 164 rv = cache->CreateEntry(keys[key], &entries[slot], cb.callback()); | 165 rv = cache->CreateEntry(keys[key], &entries[slot], cb.callback()); |
| 165 CHECK_EQ(net::OK, cb.GetResult(rv)); | 166 CHECK_EQ(net::OK, cb.GetResult(rv)); |
| 166 } | 167 } |
| 167 | 168 |
| 168 base::snprintf(buffer->data(), kSize, | 169 base::snprintf(buffer->data(), kSize, |
| 169 "i: %d iter: %d, size: %d, truncate: %d ", i, iteration, | 170 "i: %d iter: %d, size: %d, truncate: %d ", i, iteration, |
| 170 size, truncate ? 1 : 0); | 171 size, truncate ? 1 : 0); |
| 171 rv = entries[slot]->WriteData(0, 0, buffer, size, cb.callback(), truncate); | 172 rv = entries[slot]->WriteData(0, 0, buffer.get(), size, cb.callback(), |
| 173 truncate); |
| 172 CHECK_EQ(size, cb.GetResult(rv)); | 174 CHECK_EQ(size, cb.GetResult(rv)); |
| 173 | 175 |
| 174 if (rand() % 100 > 80) { | 176 if (rand() % 100 > 80) { |
| 175 key = rand() % kNumKeys; | 177 key = rand() % kNumKeys; |
| 176 net::TestCompletionCallback cb2; | 178 net::TestCompletionCallback cb2; |
| 177 rv = cache->DoomEntry(keys[key], cb2.callback()); | 179 rv = cache->DoomEntry(keys[key], cb2.callback()); |
| 178 cb2.GetResult(rv); | 180 cb2.GetResult(rv); |
| 179 } | 181 } |
| 180 | 182 |
| 181 if (!(i % 100)) | 183 if (!(i % 100)) |
| 182 printf("Entries: %d \r", i); | 184 printf("Entries: %d \r", i); |
| 183 } | 185 } |
| 184 } | 186 } |
| 185 | 187 |
| 186 // We want to prevent the timer thread from killing the process while we are | 188 // We want to prevent the timer thread from killing the process while we are |
| 187 // waiting for the debugger to attach. | 189 // waiting for the debugger to attach. |
| 188 bool g_crashing = false; | 190 bool g_crashing = false; |
| 189 | 191 |
| 190 // RunSoon() and CrashCallback() reference each other, unfortunately. | 192 // RunSoon() and CrashCallback() reference each other, unfortunately. |
| 191 void RunSoon(MessageLoop* target_loop); | 193 void RunSoon(base::MessageLoop* target_loop); |
| 192 | 194 |
| 193 void CrashCallback() { | 195 void CrashCallback() { |
| 194 // Keep trying to run. | 196 // Keep trying to run. |
| 195 RunSoon(MessageLoop::current()); | 197 RunSoon(base::MessageLoop::current()); |
| 196 | 198 |
| 197 if (g_crashing) | 199 if (g_crashing) |
| 198 return; | 200 return; |
| 199 | 201 |
| 200 if (rand() % 100 > 30) { | 202 if (rand() % 100 > 30) { |
| 201 printf("sweet death...\n"); | 203 printf("sweet death...\n"); |
| 202 #if defined(OS_WIN) | 204 #if defined(OS_WIN) |
| 203 // Windows does more work on _exit() that we would like, so we use Kill. | 205 // Windows does more work on _exit() that we would like, so we use Kill. |
| 204 base::KillProcessById(base::GetCurrentProcId(), kExpectedCrash, false); | 206 base::KillProcessById(base::GetCurrentProcId(), kExpectedCrash, false); |
| 205 #elif defined(OS_POSIX) | 207 #elif defined(OS_POSIX) |
| 206 // On POSIX, _exit() will terminate the process with minimal cleanup, | 208 // On POSIX, _exit() will terminate the process with minimal cleanup, |
| 207 // and it is cleaner than killing. | 209 // and it is cleaner than killing. |
| 208 _exit(kExpectedCrash); | 210 _exit(kExpectedCrash); |
| 209 #endif | 211 #endif |
| 210 } | 212 } |
| 211 } | 213 } |
| 212 | 214 |
| 213 void RunSoon(MessageLoop* target_loop) { | 215 void RunSoon(base::MessageLoop* target_loop) { |
| 214 const base::TimeDelta kTaskDelay = base::TimeDelta::FromSeconds(10); | 216 const base::TimeDelta kTaskDelay = base::TimeDelta::FromSeconds(10); |
| 215 target_loop->PostDelayedTask( | 217 target_loop->PostDelayedTask( |
| 216 FROM_HERE, base::Bind(&CrashCallback), kTaskDelay); | 218 FROM_HERE, base::Bind(&CrashCallback), kTaskDelay); |
| 217 } | 219 } |
| 218 | 220 |
| 219 // We leak everything here :) | 221 // We leak everything here :) |
| 220 bool StartCrashThread() { | 222 bool StartCrashThread() { |
| 221 base::Thread* thread = new base::Thread("party_crasher"); | 223 base::Thread* thread = new base::Thread("party_crasher"); |
| 222 if (!thread->Start()) | 224 if (!thread->Start()) |
| 223 return false; | 225 return false; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 logging::LogEventProvider::Initialize(kStressCacheTraceProviderName); | 270 logging::LogEventProvider::Initialize(kStressCacheTraceProviderName); |
| 269 #else | 271 #else |
| 270 CommandLine::Init(argc, argv); | 272 CommandLine::Init(argc, argv); |
| 271 logging::InitLogging(NULL, logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG, | 273 logging::InitLogging(NULL, logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG, |
| 272 logging::LOCK_LOG_FILE, logging::DELETE_OLD_LOG_FILE, | 274 logging::LOCK_LOG_FILE, logging::DELETE_OLD_LOG_FILE, |
| 273 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); | 275 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); |
| 274 #endif | 276 #endif |
| 275 | 277 |
| 276 // Some time for the memory manager to flush stuff. | 278 // Some time for the memory manager to flush stuff. |
| 277 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(3)); | 279 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(3)); |
| 278 MessageLoop message_loop(MessageLoop::TYPE_IO); | 280 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 279 | 281 |
| 280 char* end; | 282 char* end; |
| 281 long int iteration = strtol(argv[1], &end, 0); | 283 long int iteration = strtol(argv[1], &end, 0); |
| 282 | 284 |
| 283 if (!StartCrashThread()) { | 285 if (!StartCrashThread()) { |
| 284 printf("failed to start thread\n"); | 286 printf("failed to start thread\n"); |
| 285 return kError; | 287 return kError; |
| 286 } | 288 } |
| 287 | 289 |
| 288 StressTheCache(iteration); | 290 StressTheCache(iteration); |
| 289 return 0; | 291 return 0; |
| 290 } | 292 } |
| OLD | NEW |