michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: // Trace events to track application performance. Events consist of a name michael@0: // a type (BEGIN, END or INSTANT), a tracking id and extra string data. michael@0: // In addition, the current process id, thread id, a timestamp down to the michael@0: // microsecond and a file and line number of the calling location. michael@0: // michael@0: // The current implementation logs these events into a log file of the form michael@0: // trace_.log where it's designed to be post-processed to generate a michael@0: // trace report. In the future, it may use another mechansim to facilitate michael@0: // real-time analysis. michael@0: michael@0: #ifndef BASE_TRACE_EVENT_H_ michael@0: #define BASE_TRACE_EVENT_H_ michael@0: michael@0: #include "build/build_config.h" michael@0: michael@0: #if defined(OS_WIN) michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: michael@0: #include "base/lock.h" michael@0: #include "base/scoped_ptr.h" michael@0: #include "base/singleton.h" michael@0: #include "base/time.h" michael@0: #include "base/timer.h" michael@0: michael@0: // Use the following macros rather than using the TraceLog class directly as the michael@0: // underlying implementation may change in the future. Here's a sample usage: michael@0: // TRACE_EVENT_BEGIN("v8.run", documentId, scriptLocation); michael@0: // RunScript(script); michael@0: // TRACE_EVENT_END("v8.run", documentId, scriptLocation); michael@0: michael@0: // Record that an event (of name, id) has begun. All BEGIN events should have michael@0: // corresponding END events with a matching (name, id). michael@0: #define TRACE_EVENT_BEGIN(name, id, extra) \ michael@0: Singleton::get()->Trace(name, \ michael@0: base::TraceLog::EVENT_BEGIN, \ michael@0: reinterpret_cast(id), \ michael@0: extra, \ michael@0: __FILE__, \ michael@0: __LINE__) michael@0: michael@0: // Record that an event (of name, id) has ended. All END events should have michael@0: // corresponding BEGIN events with a matching (name, id). michael@0: #define TRACE_EVENT_END(name, id, extra) \ michael@0: Singleton::get()->Trace(name, \ michael@0: base::TraceLog::EVENT_END, \ michael@0: reinterpret_cast(id), \ michael@0: extra, \ michael@0: __FILE__, \ michael@0: __LINE__) michael@0: michael@0: // Record that an event (of name, id) with no duration has happened. michael@0: #define TRACE_EVENT_INSTANT(name, id, extra) \ michael@0: Singleton::get()->Trace(name, \ michael@0: base::TraceLog::EVENT_INSTANT, \ michael@0: reinterpret_cast(id), \ michael@0: extra, \ michael@0: __FILE__, \ michael@0: __LINE__) michael@0: michael@0: namespace base { michael@0: class ProcessMetrics; michael@0: } michael@0: michael@0: namespace base { michael@0: michael@0: class TraceLog { michael@0: public: michael@0: enum EventType { michael@0: EVENT_BEGIN, michael@0: EVENT_END, michael@0: EVENT_INSTANT michael@0: }; michael@0: michael@0: // Is tracing currently enabled. michael@0: static bool IsTracing(); michael@0: // Start logging trace events. michael@0: static bool StartTracing(); michael@0: // Stop logging trace events. michael@0: static void StopTracing(); michael@0: michael@0: // Log a trace event of (name, type, id) with the optional extra string. michael@0: void Trace(const std::string& name, michael@0: EventType type, michael@0: const void* id, michael@0: const std::wstring& extra, michael@0: const char* file, michael@0: int line); michael@0: void Trace(const std::string& name, michael@0: EventType type, michael@0: const void* id, michael@0: const std::string& extra, michael@0: const char* file, michael@0: int line); michael@0: michael@0: private: michael@0: // This allows constructor and destructor to be private and usable only michael@0: // by the Singleton class. michael@0: friend struct DefaultSingletonTraits; michael@0: michael@0: TraceLog(); michael@0: ~TraceLog(); michael@0: bool OpenLogFile(); michael@0: void CloseLogFile(); michael@0: bool Start(); michael@0: void Stop(); michael@0: void Heartbeat(); michael@0: void Log(const std::string& msg); michael@0: michael@0: bool enabled_; michael@0: FILE* log_file_; michael@0: Lock file_lock_; michael@0: TimeTicks trace_start_time_; michael@0: scoped_ptr process_metrics_; michael@0: RepeatingTimer timer_; michael@0: }; michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_TRACE_EVENT_H_