Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / disk_cache / blockfile / trace.cc
blobcea0eba16b7e8ca2b34538df89046ddcb0c5c826
1 // Copyright (c) 2011 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.
5 #include "net/disk_cache/blockfile/trace.h"
7 #include <stdarg.h>
8 #include <stdio.h>
9 #if defined(OS_WIN)
10 #include <windows.h>
11 #endif
13 #include "base/lazy_instance.h"
14 #include "base/logging.h"
15 #include "base/synchronization/lock.h"
16 #include "net/disk_cache/blockfile/stress_support.h"
18 // Change this value to 1 to enable tracing on a release build. By default,
19 // tracing is enabled only on debug builds.
20 #define ENABLE_TRACING 0
22 #ifndef NDEBUG
23 #undef ENABLE_TRACING
24 #define ENABLE_TRACING 1
25 #endif
27 namespace {
29 const int kEntrySize = 12 * sizeof(size_t);
30 #if defined(NET_BUILD_STRESS_CACHE)
31 const int kNumberOfEntries = 500000;
32 #else
33 const int kNumberOfEntries = 5000; // 240 KB on 32bit, 480 KB on 64bit
34 #endif
36 bool s_trace_enabled = false;
37 base::LazyInstance<base::Lock>::Leaky s_lock = LAZY_INSTANCE_INITIALIZER;
39 struct TraceBuffer {
40 int num_traces;
41 int current;
42 char buffer[kNumberOfEntries][kEntrySize];
45 #if ENABLE_TRACING
46 void DebugOutput(const char* msg) {
47 #if defined(OS_WIN)
48 OutputDebugStringA(msg);
49 #else
50 NOTIMPLEMENTED();
51 #endif
53 #endif // ENABLE_TRACING
55 } // namespace
57 namespace disk_cache {
59 // s_trace_buffer and s_trace_object are not singletons because I want the
60 // buffer to be destroyed and re-created when the last user goes away, and it
61 // must be straightforward to access the buffer from the debugger.
62 static TraceObject* s_trace_object = NULL;
64 // Static.
65 TraceObject* TraceObject::GetTraceObject() {
66 base::AutoLock lock(s_lock.Get());
68 if (s_trace_object)
69 return s_trace_object;
71 s_trace_object = new TraceObject();
72 return s_trace_object;
75 TraceObject::TraceObject() {
76 InitTrace();
79 TraceObject::~TraceObject() {
80 DestroyTrace();
83 void TraceObject::EnableTracing(bool enable) {
84 base::AutoLock lock(s_lock.Get());
85 s_trace_enabled = enable;
88 #if ENABLE_TRACING
90 static TraceBuffer* s_trace_buffer = NULL;
92 void InitTrace(void) {
93 s_trace_enabled = true;
94 if (s_trace_buffer)
95 return;
97 s_trace_buffer = new TraceBuffer;
98 memset(s_trace_buffer, 0, sizeof(*s_trace_buffer));
101 void DestroyTrace(void) {
102 base::AutoLock lock(s_lock.Get());
104 delete s_trace_buffer;
105 s_trace_buffer = NULL;
106 s_trace_object = NULL;
109 void Trace(const char* format, ...) {
110 if (!s_trace_buffer || !s_trace_enabled)
111 return;
113 va_list ap;
114 va_start(ap, format);
115 char line[kEntrySize + 2];
117 #if defined(OS_WIN)
118 vsprintf_s(line, format, ap);
119 #else
120 vsnprintf(line, kEntrySize, format, ap);
121 #endif
123 #if defined(DISK_CACHE_TRACE_TO_LOG)
124 line[kEntrySize] = '\0';
125 LOG(INFO) << line;
126 #endif
128 va_end(ap);
131 base::AutoLock lock(s_lock.Get());
132 if (!s_trace_buffer || !s_trace_enabled)
133 return;
135 memcpy(s_trace_buffer->buffer[s_trace_buffer->current], line, kEntrySize);
137 s_trace_buffer->num_traces++;
138 s_trace_buffer->current++;
139 if (s_trace_buffer->current == kNumberOfEntries)
140 s_trace_buffer->current = 0;
144 // Writes the last num_traces to the debugger output.
145 void DumpTrace(int num_traces) {
146 DCHECK(s_trace_buffer);
147 DebugOutput("Last traces:\n");
149 if (num_traces > kNumberOfEntries || num_traces < 0)
150 num_traces = kNumberOfEntries;
152 if (s_trace_buffer->num_traces) {
153 char line[kEntrySize + 2];
155 int current = s_trace_buffer->current - num_traces;
156 if (current < 0)
157 current += kNumberOfEntries;
159 for (int i = 0; i < num_traces; i++) {
160 memcpy(line, s_trace_buffer->buffer[current], kEntrySize);
161 line[kEntrySize] = '\0';
162 size_t length = strlen(line);
163 if (length) {
164 line[length] = '\n';
165 line[length + 1] = '\0';
166 DebugOutput(line);
169 current++;
170 if (current == kNumberOfEntries)
171 current = 0;
175 DebugOutput("End of Traces\n");
178 #else // ENABLE_TRACING
180 void InitTrace(void) {
181 return;
184 void DestroyTrace(void) {
185 s_trace_object = NULL;
188 void Trace(const char* format, ...) {
191 #endif // ENABLE_TRACING
193 } // namespace disk_cache