| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Trace events to track application performance. Events consist of a name | 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. | 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 | 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. | 8 // microsecond and a file and line number of the calling location. |
| 9 // | 9 // |
| 10 // The current implementation logs these events into a log file of the form | 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 | 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 | 12 // trace report. In the future, it may use another mechansim to facilitate |
| 13 // real-time analysis. | 13 // real-time analysis. |
| 14 | 14 |
| 15 #ifndef BASE_TRACE_EVENT_H_ | 15 #ifndef BASE_TRACE_EVENT_H_ |
| 16 #define BASE_TRACE_EVENT_H_ | 16 #define BASE_TRACE_EVENT_H_ |
| 17 | 17 |
| 18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 | 19 |
| 20 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
| 21 #include <windows.h> | 21 #include <windows.h> |
| 22 #endif | 22 #endif |
| 23 | 23 |
| 24 #include <string> | 24 #include <string> |
| 25 | 25 |
| 26 #include "base/lock.h" | 26 #include "base/lock.h" |
| 27 #include "base/scoped_ptr.h" |
| 27 #include "base/singleton.h" | 28 #include "base/singleton.h" |
| 28 #include "base/time.h" | 29 #include "base/time.h" |
| 30 #include "base/timer.h" |
| 29 | 31 |
| 30 // Use the following macros rather than using the TraceLog class directly as the | 32 // Use the following macros rather than using the TraceLog class directly as the |
| 31 // underlying implementation may change in the future. Here's a sample usage: | 33 // underlying implementation may change in the future. Here's a sample usage: |
| 32 // TRACE_EVENT_BEGIN("v8.run", documentId, scriptLocation); | 34 // TRACE_EVENT_BEGIN("v8.run", documentId, scriptLocation); |
| 33 // RunScript(script); | 35 // RunScript(script); |
| 34 // TRACE_EVENT_END("v8.run", documentId, scriptLocation); | 36 // TRACE_EVENT_END("v8.run", documentId, scriptLocation); |
| 35 | 37 |
| 36 #if defined(OS_WIN) | 38 #if defined(OS_WIN) |
| 37 | 39 |
| 38 // Record that an event (of name, id) has begun. All BEGIN events should have | 40 // Record that an event (of name, id) has begun. All BEGIN events should have |
| 39 // corresponding END events with a matching (name, id). | 41 // corresponding END events with a matching (name, id). |
| 40 #define TRACE_EVENT_BEGIN(name, id, extra) \ | 42 #define TRACE_EVENT_BEGIN(name, id, extra) \ |
| 41 Singleton<base::TraceLog>::get()->Trace(name, \ | 43 Singleton<base::TraceLog>::get()->Trace(name, \ |
| 42 base::TraceLog::EVENT_BEGIN, \ | 44 base::TraceLog::EVENT_BEGIN, \ |
| 43 reinterpret_cast<void*>(id), \ | 45 reinterpret_cast<const void*>(id), \ |
| 44 extra, \ | 46 extra, \ |
| 45 __FILE__, \ | 47 __FILE__, \ |
| 46 __LINE__) | 48 __LINE__) |
| 47 | 49 |
| 48 // Record that an event (of name, id) has ended. All END events should have | 50 // Record that an event (of name, id) has ended. All END events should have |
| 49 // corresponding BEGIN events with a matching (name, id). | 51 // corresponding BEGIN events with a matching (name, id). |
| 50 #define TRACE_EVENT_END(name, id, extra) \ | 52 #define TRACE_EVENT_END(name, id, extra) \ |
| 51 Singleton<base::TraceLog>::get()->Trace(name, \ | 53 Singleton<base::TraceLog>::get()->Trace(name, \ |
| 52 base::TraceLog::EVENT_END, \ | 54 base::TraceLog::EVENT_END, \ |
| 53 reinterpret_cast<void*>(id), \ | 55 reinterpret_cast<const void*>(id), \ |
| 54 extra, \ | 56 extra, \ |
| 55 __FILE__, \ | 57 __FILE__, \ |
| 56 __LINE__) | 58 __LINE__) |
| 57 | 59 |
| 58 // Record that an event (of name, id) with no duration has happened. | 60 // Record that an event (of name, id) with no duration has happened. |
| 59 #define TRACE_EVENT_INSTANT(name, id, extra) \ | 61 #define TRACE_EVENT_INSTANT(name, id, extra) \ |
| 60 Singleton<base::TraceLog>::get()->Trace(name, \ | 62 Singleton<base::TraceLog>::get()->Trace(name, \ |
| 61 base::TraceLog::EVENT_INSTANT, \ | 63 base::TraceLog::EVENT_INSTANT, \ |
| 62 reinterpret_cast<void*>(id), \ | 64 reinterpret_cast<const void*>(id), \ |
| 63 extra, \ | 65 extra, \ |
| 64 __FILE__, \ | 66 __FILE__, \ |
| 65 __LINE__) | 67 __LINE__) |
| 66 #else | 68 #else |
| 67 // TODO(erikkay): temporarily disable the macros on other platforms | 69 // TODO(erikkay): temporarily disable the macros on other platforms |
| 68 // until I can add the files to the other platform build files. | 70 // until I can add the files to the other platform build files. |
| 69 #define TRACE_EVENT_BEGIN(name, id, extra) | 71 #define TRACE_EVENT_BEGIN(name, id, extra) |
| 70 #define TRACE_EVENT_END(name, id, extra) | 72 #define TRACE_EVENT_END(name, id, extra) |
| 71 #define TRACE_EVENT_INSTANT(name, id, extra) | 73 #define TRACE_EVENT_INSTANT(name, id, extra) |
| 72 #endif | 74 #endif |
| 73 | 75 |
| 74 #if defined(OS_WIN) | 76 #if defined(OS_WIN) |
| 75 typedef HANDLE FileHandle; | 77 typedef HANDLE FileHandle; |
| 76 #else | 78 #else |
| 77 typedef FILE* FileHandle; | 79 typedef FILE* FileHandle; |
| 78 #endif | 80 #endif |
| 79 | 81 |
| 82 namespace process_util { |
| 83 class ProcessMetrics; |
| 84 } |
| 85 |
| 80 namespace base { | 86 namespace base { |
| 81 | 87 |
| 82 class TraceLog { | 88 class TraceLog { |
| 83 public: | 89 public: |
| 84 enum EventType { | 90 enum EventType { |
| 85 EVENT_BEGIN, | 91 EVENT_BEGIN, |
| 86 EVENT_END, | 92 EVENT_END, |
| 87 EVENT_INSTANT | 93 EVENT_INSTANT |
| 88 }; | 94 }; |
| 89 | 95 |
| 90 // Is tracing currently enabled. | 96 // Is tracing currently enabled. |
| 91 static bool IsTracing(); | 97 static bool IsTracing(); |
| 92 // Start logging trace events. | 98 // Start logging trace events. |
| 93 static bool StartTracing(); | 99 static bool StartTracing(); |
| 94 // Stop logging trace events. | 100 // Stop logging trace events. |
| 95 static void StopTracing(); | 101 static void StopTracing(); |
| 96 | 102 |
| 97 // Log a trace event of (name, type, id) with the optional extra string. | 103 // Log a trace event of (name, type, id) with the optional extra string. |
| 98 void Trace(const std::string& name, | 104 void Trace(const std::string& name, |
| 99 EventType type, | 105 EventType type, |
| 100 void* id, | 106 const void* id, |
| 101 const std::wstring& extra, | 107 const std::wstring& extra, |
| 102 const char* file, | 108 const char* file, |
| 103 int line); | 109 int line); |
| 104 void Trace(const std::string& name, | 110 void Trace(const std::string& name, |
| 105 EventType type, | 111 EventType type, |
| 106 void* id, | 112 const void* id, |
| 107 const std::string& extra, | 113 const std::string& extra, |
| 108 const char* file, | 114 const char* file, |
| 109 int line); | 115 int line); |
| 110 | 116 |
| 111 private: | 117 private: |
| 112 // This allows constructor and destructor to be private and usable only | 118 // This allows constructor and destructor to be private and usable only |
| 113 // by the Singleton class. | 119 // by the Singleton class. |
| 114 friend struct DefaultSingletonTraits<TraceLog>; | 120 friend struct DefaultSingletonTraits<TraceLog>; |
| 115 | 121 |
| 116 TraceLog(); | 122 TraceLog(); |
| 117 ~TraceLog(); | 123 ~TraceLog(); |
| 118 bool OpenLogFile(); | 124 bool OpenLogFile(); |
| 119 void CloseLogFile(); | 125 void CloseLogFile(); |
| 120 bool Start(); | 126 bool Start(); |
| 121 void Stop(); | 127 void Stop(); |
| 128 void Heartbeat(); |
| 122 void Log(const std::string& msg); | 129 void Log(const std::string& msg); |
| 123 | 130 |
| 124 bool enabled_; | 131 bool enabled_; |
| 125 FileHandle log_file_; | 132 FileHandle log_file_; |
| 126 Lock file_lock_; | 133 Lock file_lock_; |
| 127 TimeTicks trace_start_time_; | 134 TimeTicks trace_start_time_; |
| 135 scoped_ptr<process_util::ProcessMetrics> process_metrics_; |
| 136 RepeatingTimer<TraceLog> timer_; |
| 128 }; | 137 }; |
| 129 | 138 |
| 130 } // namespace base | 139 } // namespace base |
| 131 | 140 |
| 132 #endif // BASE_TRACE_EVENT_H_ | 141 #endif // BASE_TRACE_EVENT_H_ |
| OLD | NEW |