| OLD | NEW |
| (Empty) |
| 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 #pragma once | |
| 18 | |
| 19 #include "build/build_config.h" | |
| 20 | |
| 21 #if defined(OS_WIN) | |
| 22 // On Windows we always pull in an alternative implementation | |
| 23 // which logs to Event Tracing for Windows. | |
| 24 // | |
| 25 // Note that the Windows implementation is always enabled, irrespective the | |
| 26 // value of the CHROMIUM_ENABLE_TRACE_EVENT define. The Windows implementation | |
| 27 // is controlled by Event Tracing for Windows, which will turn tracing on only | |
| 28 // if there is someone listening for the events it generates. | |
| 29 #include "base/trace_event_win.h" | |
| 30 #else // defined(OS_WIN) | |
| 31 | |
| 32 #include <string> | |
| 33 | |
| 34 #include "base/lock.h" | |
| 35 #include "base/scoped_ptr.h" | |
| 36 #include "base/singleton.h" | |
| 37 #include "base/time.h" | |
| 38 #include "base/timer.h" | |
| 39 | |
| 40 #ifndef CHROMIUM_ENABLE_TRACE_EVENT | |
| 41 #define TRACE_EVENT_BEGIN(name, id, extra) ((void) 0) | |
| 42 #define TRACE_EVENT_END(name, id, extra) ((void) 0) | |
| 43 #define TRACE_EVENT_INSTANT(name, id, extra) ((void) 0) | |
| 44 | |
| 45 #else // CHROMIUM_ENABLE_TRACE_EVENT | |
| 46 // Use the following macros rather than using the TraceLog class directly as the | |
| 47 // underlying implementation may change in the future. Here's a sample usage: | |
| 48 // TRACE_EVENT_BEGIN("v8.run", documentId, scriptLocation); | |
| 49 // RunScript(script); | |
| 50 // TRACE_EVENT_END("v8.run", documentId, scriptLocation); | |
| 51 | |
| 52 // Record that an event (of name, id) has begun. All BEGIN events should have | |
| 53 // corresponding END events with a matching (name, id). | |
| 54 #define TRACE_EVENT_BEGIN(name, id, extra) \ | |
| 55 Singleton<base::TraceLog>::get()->Trace(name, \ | |
| 56 base::TraceLog::EVENT_BEGIN, \ | |
| 57 reinterpret_cast<const void*>(id), \ | |
| 58 extra, \ | |
| 59 __FILE__, \ | |
| 60 __LINE__) | |
| 61 | |
| 62 // Record that an event (of name, id) has ended. All END events should have | |
| 63 // corresponding BEGIN events with a matching (name, id). | |
| 64 #define TRACE_EVENT_END(name, id, extra) \ | |
| 65 Singleton<base::TraceLog>::get()->Trace(name, \ | |
| 66 base::TraceLog::EVENT_END, \ | |
| 67 reinterpret_cast<const void*>(id), \ | |
| 68 extra, \ | |
| 69 __FILE__, \ | |
| 70 __LINE__) | |
| 71 | |
| 72 // Record that an event (of name, id) with no duration has happened. | |
| 73 #define TRACE_EVENT_INSTANT(name, id, extra) \ | |
| 74 Singleton<base::TraceLog>::get()->Trace(name, \ | |
| 75 base::TraceLog::EVENT_INSTANT, \ | |
| 76 reinterpret_cast<const void*>(id), \ | |
| 77 extra, \ | |
| 78 __FILE__, \ | |
| 79 __LINE__) | |
| 80 #endif // CHROMIUM_ENABLE_TRACE_EVENT | |
| 81 | |
| 82 namespace base { | |
| 83 class ProcessMetrics; | |
| 84 } | |
| 85 | |
| 86 namespace base { | |
| 87 | |
| 88 class TraceLog { | |
| 89 public: | |
| 90 enum EventType { | |
| 91 EVENT_BEGIN, | |
| 92 EVENT_END, | |
| 93 EVENT_INSTANT | |
| 94 }; | |
| 95 | |
| 96 // Is tracing currently enabled. | |
| 97 static bool IsTracing(); | |
| 98 // Start logging trace events. | |
| 99 static bool StartTracing(); | |
| 100 // Stop logging trace events. | |
| 101 static void StopTracing(); | |
| 102 | |
| 103 // Log a trace event of (name, type, id) with the optional extra string. | |
| 104 void Trace(const std::string& name, | |
| 105 EventType type, | |
| 106 const void* id, | |
| 107 const std::wstring& extra, | |
| 108 const char* file, | |
| 109 int line); | |
| 110 void Trace(const std::string& name, | |
| 111 EventType type, | |
| 112 const void* id, | |
| 113 const std::string& extra, | |
| 114 const char* file, | |
| 115 int line); | |
| 116 | |
| 117 private: | |
| 118 // This allows constructor and destructor to be private and usable only | |
| 119 // by the Singleton class. | |
| 120 friend struct DefaultSingletonTraits<TraceLog>; | |
| 121 | |
| 122 TraceLog(); | |
| 123 ~TraceLog(); | |
| 124 bool OpenLogFile(); | |
| 125 void CloseLogFile(); | |
| 126 bool Start(); | |
| 127 void Stop(); | |
| 128 void Heartbeat(); | |
| 129 void Log(const std::string& msg); | |
| 130 | |
| 131 bool enabled_; | |
| 132 FILE* log_file_; | |
| 133 Lock file_lock_; | |
| 134 TimeTicks trace_start_time_; | |
| 135 scoped_ptr<base::ProcessMetrics> process_metrics_; | |
| 136 RepeatingTimer<TraceLog> timer_; | |
| 137 }; | |
| 138 | |
| 139 } // namespace base | |
| 140 #endif // defined(OS_WIN) | |
| 141 | |
| 142 #endif // BASE_TRACE_EVENT_H_ | |
| OLD | NEW |