[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
| 4 | |
| 5 | // Trace events to track application performance. Events consist of a name |
| 6 | // a type (BEGIN, END or INSTANT), a tracking id and extra string data. |
| 7 | // In addition, the current process id, thread id, a timestamp down to the |
| 8 | // microsecond and a file and line number of the calling location. |
| 9 | // |
| 10 | // The current implementation logs these events into a log file of the form |
| 11 | // trace_<pid>.log where it's designed to be post-processed to generate a |
| 12 | // trace report. In the future, it may use another mechansim to facilitate |
| 13 | // real-time analysis. |
| 14 | |
| 15 | #ifndef BASE_TRACE_EVENT_H_ |
| 16 | #define BASE_TRACE_EVENT_H_ |
| 17 | |
| 18 | #include "build/build_config.h" |
| 19 | |
| 20 | #if defined(OS_WIN) |
| 21 | #include <windows.h> |
| 22 | #endif |
| 23 | |
| 24 | #include <string> |
| 25 | |
| 26 | #include "base/lock.h" |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 27 | #include "base/scoped_ptr.h" |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 28 | #include "base/singleton.h" |
| 29 | #include "base/time.h" |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 30 | #include "base/timer.h" |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 31 | |
| 32 | // Use the following macros rather than using the TraceLog class directly as the |
| 33 | // underlying implementation may change in the future. Here's a sample usage: |
| 34 | // TRACE_EVENT_BEGIN("v8.run", documentId, scriptLocation); |
| 35 | // RunScript(script); |
| 36 | // TRACE_EVENT_END("v8.run", documentId, scriptLocation); |
| 37 | |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 38 | // Record that an event (of name, id) has begun. All BEGIN events should have |
| 39 | // corresponding END events with a matching (name, id). |
| 40 | #define TRACE_EVENT_BEGIN(name, id, extra) \ |
| 41 | Singleton<base::TraceLog>::get()->Trace(name, \ |
| 42 | base::TraceLog::EVENT_BEGIN, \ |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 43 | reinterpret_cast<const void*>(id), \ |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 44 | extra, \ |
| 45 | __FILE__, \ |
| 46 | __LINE__) |
| 47 | |
| 48 | // Record that an event (of name, id) has ended. All END events should have |
| 49 | // corresponding BEGIN events with a matching (name, id). |
| 50 | #define TRACE_EVENT_END(name, id, extra) \ |
| 51 | Singleton<base::TraceLog>::get()->Trace(name, \ |
| 52 | base::TraceLog::EVENT_END, \ |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 53 | reinterpret_cast<const void*>(id), \ |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 54 | extra, \ |
| 55 | __FILE__, \ |
| 56 | __LINE__) |
| 57 | |
| 58 | // Record that an event (of name, id) with no duration has happened. |
| 59 | #define TRACE_EVENT_INSTANT(name, id, extra) \ |
| 60 | Singleton<base::TraceLog>::get()->Trace(name, \ |
| 61 | base::TraceLog::EVENT_INSTANT, \ |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 62 | reinterpret_cast<const void*>(id), \ |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 63 | extra, \ |
| 64 | __FILE__, \ |
| 65 | __LINE__) |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 66 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 67 | namespace base { |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 68 | class ProcessMetrics; |
| 69 | } |
| 70 | |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 71 | namespace base { |
| 72 | |
| 73 | class TraceLog { |
| 74 | public: |
| 75 | enum EventType { |
| 76 | EVENT_BEGIN, |
| 77 | EVENT_END, |
| 78 | EVENT_INSTANT |
| 79 | }; |
| 80 | |
| 81 | // Is tracing currently enabled. |
| 82 | static bool IsTracing(); |
| 83 | // Start logging trace events. |
| 84 | static bool StartTracing(); |
| 85 | // Stop logging trace events. |
| 86 | static void StopTracing(); |
| 87 | |
| 88 | // Log a trace event of (name, type, id) with the optional extra string. |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 89 | void Trace(const std::string& name, |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 90 | EventType type, |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 91 | const void* id, |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 92 | const std::wstring& extra, |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 93 | const char* file, |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 94 | int line); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 95 | void Trace(const std::string& name, |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 96 | EventType type, |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 97 | const void* id, |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 98 | const std::string& extra, |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 99 | const char* file, |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 100 | int line); |
| 101 | |
| 102 | private: |
| 103 | // This allows constructor and destructor to be private and usable only |
| 104 | // by the Singleton class. |
| 105 | friend struct DefaultSingletonTraits<TraceLog>; |
| 106 | |
| 107 | TraceLog(); |
| 108 | ~TraceLog(); |
| 109 | bool OpenLogFile(); |
| 110 | void CloseLogFile(); |
| 111 | bool Start(); |
| 112 | void Stop(); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 113 | void Heartbeat(); |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 114 | void Log(const std::string& msg); |
| 115 | |
| 116 | bool enabled_; |
[email protected] | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 117 | FILE* log_file_; |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 118 | Lock file_lock_; |
| 119 | TimeTicks trace_start_time_; |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 120 | scoped_ptr<base::ProcessMetrics> process_metrics_; |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 121 | RepeatingTimer<TraceLog> timer_; |
[email protected] | d49a626 | 2008-09-02 16:42:15 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } // namespace base |
| 125 | |
| 126 | #endif // BASE_TRACE_EVENT_H_ |