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

Side by Side Diff: net/disk_cache/blockfile/stress_cache.cc

Issue 963163004: Move stress_cache to the tools directory (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 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
« no previous file with comments | « net/BUILD.gn ('k') | net/net.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
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
7 // exit code of the child process. When the child dies in an unexpected way,
8 // the main application quits.
9
10 // The child application has two threads: one to exercise the cache in an
11 // infinite loop, and another one to asynchronously kill the process.
12
13 // A regular build should never crash.
14 // To test that the disk cache doesn't generate critical errors with regular
15 // application level crashes, edit stress_support.h.
16
17 #include <string>
18 #include <vector>
19
20 #include "base/at_exit.h"
21 #include "base/bind.h"
22 #include "base/command_line.h"
23 #include "base/debug/debugger.h"
24 #include "base/files/file_path.h"
25 #include "base/logging.h"
26 #include "base/message_loop/message_loop.h"
27 #include "base/path_service.h"
28 #include "base/process/kill.h"
29 #include "base/process/launch.h"
30 #include "base/strings/string_number_conversions.h"
31 #include "base/strings/string_util.h"
32 #include "base/strings/utf_string_conversions.h"
33 #include "base/threading/platform_thread.h"
34 #include "base/threading/thread.h"
35 #include "net/base/io_buffer.h"
36 #include "net/base/net_errors.h"
37 #include "net/base/test_completion_callback.h"
38 #include "net/disk_cache/blockfile/backend_impl.h"
39 #include "net/disk_cache/blockfile/stress_support.h"
40 #include "net/disk_cache/blockfile/trace.h"
41 #include "net/disk_cache/disk_cache.h"
42 #include "net/disk_cache/disk_cache_test_util.h"
43
44 #if defined(OS_WIN)
45 #include "base/logging_win.h"
46 #endif
47
48 using base::Time;
49
50 const int kError = -1;
51 const int kExpectedCrash = 100;
52
53 // Starts a new process.
54 int RunSlave(int iteration) {
55 base::FilePath exe;
56 PathService::Get(base::FILE_EXE, &exe);
57
58 base::CommandLine cmdline(exe);
59 cmdline.AppendArg(base::IntToString(iteration));
60
61 base::Process process = base::LaunchProcess(cmdline, base::LaunchOptions());
62 if (!process.IsValid()) {
63 printf("Unable to run test\n");
64 return kError;
65 }
66
67 int exit_code;
68 if (!process.WaitForExit(&exit_code)) {
69 printf("Unable to get return code\n");
70 return kError;
71 }
72 return exit_code;
73 }
74
75 // Main loop for the master process.
76 int MasterCode() {
77 for (int i = 0; i < 100000; i++) {
78 int ret = RunSlave(i);
79 if (kExpectedCrash != ret)
80 return ret;
81 }
82
83 printf("More than enough...\n");
84
85 return 0;
86 }
87
88 // -----------------------------------------------------------------------
89
90 std::string GenerateStressKey() {
91 char key[20 * 1024];
92 size_t size = 50 + rand() % 20000;
93 CacheTestFillBuffer(key, size, true);
94
95 key[size - 1] = '\0';
96 return std::string(key);
97 }
98
99 // This thread will loop forever, adding and removing entries from the cache.
100 // iteration is the current crash cycle, so the entries on the cache are marked
101 // to know which instance of the application wrote them.
102 void StressTheCache(int iteration) {
103 int cache_size = 0x2000000; // 32MB.
104 uint32 mask = 0xfff; // 4096 entries.
105
106 base::FilePath path;
107 PathService::Get(base::DIR_TEMP, &path);
108 path = path.AppendASCII("cache_test_stress");
109
110 base::Thread cache_thread("CacheThread");
111 if (!cache_thread.StartWithOptions(
112 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)))
113 return;
114
115 disk_cache::BackendImpl* cache =
116 new disk_cache::BackendImpl(path, mask,
117 cache_thread.message_loop_proxy().get(),
118 NULL);
119 cache->SetMaxSize(cache_size);
120 cache->SetFlags(disk_cache::kNoLoadProtection);
121
122 net::TestCompletionCallback cb;
123 int rv = cache->Init(cb.callback());
124
125 if (cb.GetResult(rv) != net::OK) {
126 printf("Unable to initialize cache.\n");
127 return;
128 }
129 printf("Iteration %d, initial entries: %d\n", iteration,
130 cache->GetEntryCount());
131
132 int seed = static_cast<int>(Time::Now().ToInternalValue());
133 srand(seed);
134
135 // kNumKeys is meant to be enough to have about 3x or 4x iterations before
136 // the process crashes.
137 #ifdef NDEBUG
138 const int kNumKeys = 4000;
139 #else
140 const int kNumKeys = 1200;
141 #endif
142 const int kNumEntries = 30;
143 std::string keys[kNumKeys];
144 disk_cache::Entry* entries[kNumEntries] = {0};
145
146 for (int i = 0; i < kNumKeys; i++) {
147 keys[i] = GenerateStressKey();
148 }
149
150 const int kSize = 20000;
151 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
152 memset(buffer->data(), 'k', kSize);
153
154 for (int i = 0;; i++) {
155 int slot = rand() % kNumEntries;
156 int key = rand() % kNumKeys;
157 bool truncate = (rand() % 2 == 0);
158 int size = kSize - (rand() % 20) * kSize / 20;
159
160 if (entries[slot])
161 entries[slot]->Close();
162
163 net::TestCompletionCallback cb;
164 rv = cache->OpenEntry(keys[key], &entries[slot], cb.callback());
165 if (cb.GetResult(rv) != net::OK) {
166 rv = cache->CreateEntry(keys[key], &entries[slot], cb.callback());
167 CHECK_EQ(net::OK, cb.GetResult(rv));
168 }
169
170 base::snprintf(buffer->data(), kSize,
171 "i: %d iter: %d, size: %d, truncate: %d ", i, iteration,
172 size, truncate ? 1 : 0);
173 rv = entries[slot]->WriteData(0, 0, buffer.get(), size, cb.callback(),
174 truncate);
175 CHECK_EQ(size, cb.GetResult(rv));
176
177 if (rand() % 100 > 80) {
178 key = rand() % kNumKeys;
179 net::TestCompletionCallback cb2;
180 rv = cache->DoomEntry(keys[key], cb2.callback());
181 cb2.GetResult(rv);
182 }
183
184 if (!(i % 100))
185 printf("Entries: %d \r", i);
186 }
187 }
188
189 // We want to prevent the timer thread from killing the process while we are
190 // waiting for the debugger to attach.
191 bool g_crashing = false;
192
193 // RunSoon() and CrashCallback() reference each other, unfortunately.
194 void RunSoon(base::MessageLoop* target_loop);
195
196 void CrashCallback() {
197 // Keep trying to run.
198 RunSoon(base::MessageLoop::current());
199
200 if (g_crashing)
201 return;
202
203 if (rand() % 100 > 30) {
204 printf("sweet death...\n");
205 #if defined(OS_WIN)
206 // Windows does more work on _exit() that we would like, so we use Kill.
207 base::KillProcessById(base::GetCurrentProcId(), kExpectedCrash, false);
208 #elif defined(OS_POSIX)
209 // On POSIX, _exit() will terminate the process with minimal cleanup,
210 // and it is cleaner than killing.
211 _exit(kExpectedCrash);
212 #endif
213 }
214 }
215
216 void RunSoon(base::MessageLoop* target_loop) {
217 const base::TimeDelta kTaskDelay = base::TimeDelta::FromSeconds(10);
218 target_loop->PostDelayedTask(
219 FROM_HERE, base::Bind(&CrashCallback), kTaskDelay);
220 }
221
222 // We leak everything here :)
223 bool StartCrashThread() {
224 base::Thread* thread = new base::Thread("party_crasher");
225 if (!thread->Start())
226 return false;
227
228 RunSoon(thread->message_loop());
229 return true;
230 }
231
232 void CrashHandler(const std::string& str) {
233 g_crashing = true;
234 base::debug::BreakDebugger();
235 }
236
237 bool MessageHandler(int severity, const char* file, int line,
238 size_t message_start, const std::string& str) {
239 const size_t kMaxMessageLen = 48;
240 char message[kMaxMessageLen];
241 size_t len = std::min(str.length() - message_start, kMaxMessageLen - 1);
242
243 memcpy(message, str.c_str() + message_start, len);
244 message[len] = '\0';
245 #if !defined(DISK_CACHE_TRACE_TO_LOG)
246 disk_cache::Trace("%s", message);
247 #endif
248 return false;
249 }
250
251 // -----------------------------------------------------------------------
252
253 #if defined(OS_WIN)
254 // {B9A153D4-31C3-48e4-9ABF-D54383F14A0D}
255 const GUID kStressCacheTraceProviderName = {
256 0xb9a153d4, 0x31c3, 0x48e4,
257 { 0x9a, 0xbf, 0xd5, 0x43, 0x83, 0xf1, 0x4a, 0xd } };
258 #endif
259
260 int main(int argc, const char* argv[]) {
261 // Setup an AtExitManager so Singleton objects will be destructed.
262 base::AtExitManager at_exit_manager;
263
264 if (argc < 2)
265 return MasterCode();
266
267 logging::SetLogAssertHandler(CrashHandler);
268 logging::SetLogMessageHandler(MessageHandler);
269
270 #if defined(OS_WIN)
271 logging::LogEventProvider::Initialize(kStressCacheTraceProviderName);
272 #else
273 base::CommandLine::Init(argc, argv);
274 logging::LoggingSettings settings;
275 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
276 logging::InitLogging(settings);
277 #endif
278
279 // Some time for the memory manager to flush stuff.
280 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(3));
281 base::MessageLoopForIO message_loop;
282
283 char* end;
284 long int iteration = strtol(argv[1], &end, 0);
285
286 if (!StartCrashThread()) {
287 printf("failed to start thread\n");
288 return kError;
289 }
290
291 StressTheCache(iteration);
292 return 0;
293 }
OLDNEW
« no previous file with comments | « net/BUILD.gn ('k') | net/net.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698