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"
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
24 #define ENABLE_TRACING 1
29 const int kEntrySize
= 12 * sizeof(size_t);
30 #if defined(NET_BUILD_STRESS_CACHE)
31 const int kNumberOfEntries
= 500000;
33 const int kNumberOfEntries
= 5000; // 240 KB on 32bit, 480 KB on 64bit
36 bool s_trace_enabled
= false;
37 base::LazyInstance
<base::Lock
>::Leaky s_lock
= LAZY_INSTANCE_INITIALIZER
;
42 char buffer
[kNumberOfEntries
][kEntrySize
];
46 void DebugOutput(const char* msg
) {
48 OutputDebugStringA(msg
);
53 #endif // ENABLE_TRACING
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
;
65 TraceObject
* TraceObject::GetTraceObject() {
66 base::AutoLock
lock(s_lock
.Get());
69 return s_trace_object
;
71 s_trace_object
= new TraceObject();
72 return s_trace_object
;
75 TraceObject::TraceObject() {
79 TraceObject::~TraceObject() {
83 void TraceObject::EnableTracing(bool enable
) {
84 base::AutoLock
lock(s_lock
.Get());
85 s_trace_enabled
= enable
;
90 static TraceBuffer
* s_trace_buffer
= NULL
;
92 void InitTrace(void) {
93 s_trace_enabled
= true;
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
)
114 va_start(ap
, format
);
115 char line
[kEntrySize
+ 2];
118 vsprintf_s(line
, format
, ap
);
120 vsnprintf(line
, kEntrySize
, format
, ap
);
123 #if defined(DISK_CACHE_TRACE_TO_LOG)
124 line
[kEntrySize
] = '\0';
131 base::AutoLock
lock(s_lock
.Get());
132 if (!s_trace_buffer
|| !s_trace_enabled
)
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
;
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
);
165 line
[length
+ 1] = '\0';
170 if (current
== kNumberOfEntries
)
175 DebugOutput("End of Traces\n");
178 #else // ENABLE_TRACING
180 void InitTrace(void) {
184 void DestroyTrace(void) {
185 s_trace_object
= NULL
;
188 void Trace(const char* format
, ...) {
191 #endif // ENABLE_TRACING
193 } // namespace disk_cache