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