|
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 // 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 |
|
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, \ |
|
43 reinterpret_cast<const void*>(id), \ |
|
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, \ |
|
53 reinterpret_cast<const void*>(id), \ |
|
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, \ |
|
62 reinterpret_cast<const void*>(id), \ |
|
63 extra, \ |
|
64 __FILE__, \ |
|
65 __LINE__) |
|
66 |
|
67 namespace base { |
|
68 class ProcessMetrics; |
|
69 } |
|
70 |
|
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. |
|
89 void Trace(const std::string& name, |
|
90 EventType type, |
|
91 const void* id, |
|
92 const std::wstring& extra, |
|
93 const char* file, |
|
94 int line); |
|
95 void Trace(const std::string& name, |
|
96 EventType type, |
|
97 const void* id, |
|
98 const std::string& extra, |
|
99 const char* file, |
|
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(); |
|
113 void Heartbeat(); |
|
114 void Log(const std::string& msg); |
|
115 |
|
116 bool enabled_; |
|
117 FILE* log_file_; |
|
118 Lock file_lock_; |
|
119 TimeTicks trace_start_time_; |
|
120 scoped_ptr<base::ProcessMetrics> process_metrics_; |
|
121 RepeatingTimer<TraceLog> timer_; |
|
122 }; |
|
123 |
|
124 } // namespace base |
|
125 |
|
126 #endif // BASE_TRACE_EVENT_H_ |