summaryrefslogtreecommitdiffstats
path: root/base/trace_event.h
diff options
context:
space:
mode:
authorerikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-02 16:42:15 +0000
committererikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-02 16:42:15 +0000
commitd49a6264617043a450e776984a76468b542ba411 (patch)
tree739da24590680b5c6e6e789db4d1b44e4ac8115a /base/trace_event.h
parentbcfad371249f11b922d3b7695286626331551b12 (diff)
downloadchromium_src-d49a6264617043a450e776984a76468b542ba411.zip
chromium_src-d49a6264617043a450e776984a76468b542ba411.tar.gz
chromium_src-d49a6264617043a450e776984a76468b542ba411.tar.bz2
Initial pass at simple event tracing. I added a few traces to tcp_socket and test_shell to get an idea of what a simple trace might look like.
Here's a sample output line: 11:24:19.660604 0x1e278:0x1e24c BEGIN url.load [0x5 http://mail.google.com/mail/] <E:\src\cr\src\webkit\tools\test_shell\test_webview_delegate.cc:189> format is:hh:mm:ss.us pid:tid TYPE NAME [id, extra] <file:line> git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1641 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/trace_event.h')
-rw-r--r--base/trace_event.h132
1 files changed, 132 insertions, 0 deletions
diff --git a/base/trace_event.h b/base/trace_event.h
new file mode 100644
index 0000000..b01a71e
--- /dev/null
+++ b/base/trace_event.h
@@ -0,0 +1,132 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Trace events to track application performance. Events consist of a name
+// a type (BEGIN, END or INSTANT), a tracking id and extra string data.
+// In addition, the current process id, thread id, a timestamp down to the
+// microsecond and a file and line number of the calling location.
+//
+// The current implementation logs these events into a log file of the form
+// trace_<pid>.log where it's designed to be post-processed to generate a
+// trace report. In the future, it may use another mechansim to facilitate
+// real-time analysis.
+
+#ifndef BASE_TRACE_EVENT_H_
+#define BASE_TRACE_EVENT_H_
+
+#include "build/build_config.h"
+
+#if defined(OS_WIN)
+#include <windows.h>
+#endif
+
+#include <string>
+
+#include "base/lock.h"
+#include "base/singleton.h"
+#include "base/time.h"
+
+// Use the following macros rather than using the TraceLog class directly as the
+// underlying implementation may change in the future. Here's a sample usage:
+// TRACE_EVENT_BEGIN("v8.run", documentId, scriptLocation);
+// RunScript(script);
+// TRACE_EVENT_END("v8.run", documentId, scriptLocation);
+
+#if defined(OS_WIN)
+
+// Record that an event (of name, id) has begun. All BEGIN events should have
+// corresponding END events with a matching (name, id).
+#define TRACE_EVENT_BEGIN(name, id, extra) \
+ Singleton<base::TraceLog>::get()->Trace(name, \
+ base::TraceLog::EVENT_BEGIN, \
+ reinterpret_cast<void*>(id), \
+ extra, \
+ __FILE__, \
+ __LINE__)
+
+// Record that an event (of name, id) has ended. All END events should have
+// corresponding BEGIN events with a matching (name, id).
+#define TRACE_EVENT_END(name, id, extra) \
+ Singleton<base::TraceLog>::get()->Trace(name, \
+ base::TraceLog::EVENT_END, \
+ reinterpret_cast<void*>(id), \
+ extra, \
+ __FILE__, \
+ __LINE__)
+
+// Record that an event (of name, id) with no duration has happened.
+#define TRACE_EVENT_INSTANT(name, id, extra) \
+ Singleton<base::TraceLog>::get()->Trace(name, \
+ base::TraceLog::EVENT_INSTANT, \
+ reinterpret_cast<void*>(id), \
+ extra, \
+ __FILE__, \
+ __LINE__)
+#else
+// TODO(erikkay): temporarily disable the macros on other platforms
+// until I can add the files to the other platform build files.
+#define TRACE_EVENT_BEGIN(name, id, extra)
+#define TRACE_EVENT_END(name, id, extra)
+#define TRACE_EVENT_INSTANT(name, id, extra)
+#endif
+
+#if defined(OS_WIN)
+typedef HANDLE FileHandle;
+#else
+typedef FILE* FileHandle;
+#endif
+
+namespace base {
+
+class TraceLog {
+ public:
+ enum EventType {
+ EVENT_BEGIN,
+ EVENT_END,
+ EVENT_INSTANT
+ };
+
+ // Is tracing currently enabled.
+ static bool IsTracing();
+ // Start logging trace events.
+ static bool StartTracing();
+ // Stop logging trace events.
+ static void StopTracing();
+
+ // Log a trace event of (name, type, id) with the optional extra string.
+ void Trace(const std::string& name,
+ EventType type,
+ void* id,
+ const std::wstring& extra,
+ const char* file,
+ int line);
+ void Trace(const std::string& name,
+ EventType type,
+ void* id,
+ const std::string& extra,
+ const char* file,
+ int line);
+
+ private:
+ // This allows constructor and destructor to be private and usable only
+ // by the Singleton class.
+ friend struct DefaultSingletonTraits<TraceLog>;
+
+ TraceLog();
+ ~TraceLog();
+ bool OpenLogFile();
+ void CloseLogFile();
+ bool Start();
+ void Stop();
+ void Log(const std::string& msg);
+
+ bool enabled_;
+ FileHandle log_file_;
+ Lock file_lock_;
+ TimeTicks trace_start_time_;
+};
+
+} // namespace base
+
+#endif // BASE_TRACE_EVENT_H_