blob: d2b6ef116f4fce97300afe308155abd7d9948992 [file] [log] [blame]
[email protected]d49a6262008-09-02 16:42:151// 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]113ab132008-09-18 20:42:5527#include "base/scoped_ptr.h"
[email protected]d49a6262008-09-02 16:42:1528#include "base/singleton.h"
29#include "base/time.h"
[email protected]113ab132008-09-18 20:42:5530#include "base/timer.h"
[email protected]d49a6262008-09-02 16:42:1531
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]d49a6262008-09-02 16:42:1538// 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]113ab132008-09-18 20:42:5543 reinterpret_cast<const void*>(id), \
[email protected]d49a6262008-09-02 16:42:1544 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]113ab132008-09-18 20:42:5553 reinterpret_cast<const void*>(id), \
[email protected]d49a6262008-09-02 16:42:1554 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]113ab132008-09-18 20:42:5562 reinterpret_cast<const void*>(id), \
[email protected]d49a6262008-09-02 16:42:1563 extra, \
64 __FILE__, \
65 __LINE__)
[email protected]d49a6262008-09-02 16:42:1566
[email protected]176aa482008-11-14 03:25:1567namespace base {
[email protected]113ab132008-09-18 20:42:5568class ProcessMetrics;
69}
70
[email protected]d49a6262008-09-02 16:42:1571namespace base {
72
73class 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]52a261f2009-03-03 15:01:1289 void Trace(const std::string& name,
[email protected]d49a6262008-09-02 16:42:1590 EventType type,
[email protected]113ab132008-09-18 20:42:5591 const void* id,
[email protected]d49a6262008-09-02 16:42:1592 const std::wstring& extra,
[email protected]52a261f2009-03-03 15:01:1293 const char* file,
[email protected]d49a6262008-09-02 16:42:1594 int line);
[email protected]52a261f2009-03-03 15:01:1295 void Trace(const std::string& name,
[email protected]d49a6262008-09-02 16:42:1596 EventType type,
[email protected]113ab132008-09-18 20:42:5597 const void* id,
[email protected]d49a6262008-09-02 16:42:1598 const std::string& extra,
[email protected]52a261f2009-03-03 15:01:1299 const char* file,
[email protected]d49a6262008-09-02 16:42:15100 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]113ab132008-09-18 20:42:55113 void Heartbeat();
[email protected]d49a6262008-09-02 16:42:15114 void Log(const std::string& msg);
115
116 bool enabled_;
[email protected]836f1342008-10-01 17:40:13117 FILE* log_file_;
[email protected]d49a6262008-09-02 16:42:15118 Lock file_lock_;
119 TimeTicks trace_start_time_;
[email protected]176aa482008-11-14 03:25:15120 scoped_ptr<base::ProcessMetrics> process_metrics_;
[email protected]113ab132008-09-18 20:42:55121 RepeatingTimer<TraceLog> timer_;
[email protected]d49a6262008-09-02 16:42:15122};
123
124} // namespace base
125
126#endif // BASE_TRACE_EVENT_H_