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/trace.h"
12 #include "base/lazy_instance.h"
13 #include "base/logging.h"
14 #include "base/synchronization/lock.h"
15 #include "net/disk_cache/stress_support.h"
17 // Change this value to 1 to enable tracing on a release build. By default,
18 // tracing is enabled only on debug builds.
19 #define ENABLE_TRACING 0
23 #define ENABLE_TRACING 1
28 const int kEntrySize
= 12 * sizeof(size_t);
29 #if defined(NET_BUILD_STRESS_CACHE)
30 const int kNumberOfEntries
= 500000;
32 const int kNumberOfEntries
= 5000; // 240 KB on 32bit, 480 KB on 64bit
35 bool s_trace_enabled
= false;
36 base::LazyInstance
<base::Lock
>::Leaky s_lock
= LAZY_INSTANCE_INITIALIZER
;
41 char buffer
[kNumberOfEntries
][kEntrySize
];
45 void DebugOutput(const char* msg
) {
47 OutputDebugStringA(msg
);
52 #endif // ENABLE_TRACING
56 namespace disk_cache
{
58 // s_trace_buffer and s_trace_object are not singletons because I want the
59 // buffer to be destroyed and re-created when the last user goes away, and it
60 // must be straightforward to access the buffer from the debugger.
61 static TraceObject
* s_trace_object
= NULL
;
64 TraceObject
* TraceObject::GetTraceObject() {
65 base::AutoLock
lock(s_lock
.Get());
68 return s_trace_object
;
70 s_trace_object
= new TraceObject();
71 return s_trace_object
;
74 TraceObject::TraceObject() {
78 TraceObject::~TraceObject() {
82 void TraceObject::EnableTracing(bool enable
) {
83 base::AutoLock
lock(s_lock
.Get());
84 s_trace_enabled
= enable
;
89 static TraceBuffer
* s_trace_buffer
= NULL
;
91 void InitTrace(void) {
92 s_trace_enabled
= true;
96 s_trace_buffer
= new TraceBuffer
;
97 memset(s_trace_buffer
, 0, sizeof(*s_trace_buffer
));
100 void DestroyTrace(void) {
101 base::AutoLock
lock(s_lock
.Get());
103 delete s_trace_buffer
;
104 s_trace_buffer
= NULL
;
105 s_trace_object
= NULL
;
108 void Trace(const char* format
, ...) {
109 if (!s_trace_buffer
|| !s_trace_enabled
)
113 va_start(ap
, format
);
114 char line
[kEntrySize
+ 2];
117 vsprintf_s(line
, format
, ap
);
119 vsnprintf(line
, kEntrySize
, format
, ap
);
122 #if defined(DISK_CACHE_TRACE_TO_LOG)
123 line
[kEntrySize
] = '\0';
130 base::AutoLock
lock(s_lock
.Get());
131 if (!s_trace_buffer
|| !s_trace_enabled
)
134 memcpy(s_trace_buffer
->buffer
[s_trace_buffer
->current
], line
, kEntrySize
);
136 s_trace_buffer
->num_traces
++;
137 s_trace_buffer
->current
++;
138 if (s_trace_buffer
->current
== kNumberOfEntries
)
139 s_trace_buffer
->current
= 0;
143 // Writes the last num_traces to the debugger output.
144 void DumpTrace(int num_traces
) {
145 DCHECK(s_trace_buffer
);
146 DebugOutput("Last traces:\n");
148 if (num_traces
> kNumberOfEntries
|| num_traces
< 0)
149 num_traces
= kNumberOfEntries
;
151 if (s_trace_buffer
->num_traces
) {
152 char line
[kEntrySize
+ 2];
154 int current
= s_trace_buffer
->current
- num_traces
;
156 current
+= kNumberOfEntries
;
158 for (int i
= 0; i
< num_traces
; i
++) {
159 memcpy(line
, s_trace_buffer
->buffer
[current
], kEntrySize
);
160 line
[kEntrySize
] = '\0';
161 size_t length
= strlen(line
);
164 line
[length
+ 1] = '\0';
169 if (current
== kNumberOfEntries
)
174 DebugOutput("End of Traces\n");
177 #else // ENABLE_TRACING
179 void InitTrace(void) {
183 void DestroyTrace(void) {
184 s_trace_object
= NULL
;
187 void Trace(const char* format
, ...) {
190 #endif // ENABLE_TRACING
192 } // namespace disk_cache