erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 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 | #include "base/trace_event.h" |
| 6 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 7 | #include "base/file_path.h" |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 8 | #include "base/file_util.h" |
| 9 | #include "base/path_service.h" |
| 10 | #include "base/platform_thread.h" |
| 11 | #include "base/process_util.h" |
| 12 | #include "base/string_util.h" |
| 13 | #include "base/time.h" |
| 14 | |
| 15 | #define USE_UNRELIABLE_NOW |
| 16 | |
| 17 | namespace base { |
| 18 | |
| 19 | static const char* kEventTypeNames[] = { |
| 20 | "BEGIN", |
| 21 | "END", |
| 22 | "INSTANT" |
| 23 | }; |
| 24 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 25 | static const FilePath::CharType* kLogFileName = |
| 26 | FILE_PATH_LITERAL("trace_%d.log"); |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 27 | |
| 28 | TraceLog::TraceLog() : enabled_(false), log_file_(NULL) { |
brettw@google.com | c60d989 | 2008-11-14 12:25:15 +0900 | [diff] [blame] | 29 | base::ProcessHandle proc = base::GetCurrentProcessHandle(); |
| 30 | process_metrics_.reset(base::ProcessMetrics::CreateProcessMetrics(proc)); |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | TraceLog::~TraceLog() { |
| 34 | Stop(); |
| 35 | } |
| 36 | |
| 37 | // static |
| 38 | bool TraceLog::IsTracing() { |
| 39 | TraceLog* trace = Singleton<TraceLog>::get(); |
| 40 | return trace->enabled_; |
| 41 | } |
| 42 | |
| 43 | // static |
| 44 | bool TraceLog::StartTracing() { |
| 45 | TraceLog* trace = Singleton<TraceLog>::get(); |
| 46 | return trace->Start(); |
| 47 | } |
| 48 | |
| 49 | bool TraceLog::Start() { |
| 50 | if (enabled_) |
| 51 | return true; |
| 52 | enabled_ = OpenLogFile(); |
erikkay@google.com | 66a4c3e | 2008-09-19 05:42:55 +0900 | [diff] [blame] | 53 | if (enabled_) { |
phajdan.jr@chromium.org | 862ff1f | 2009-01-06 03:51:30 +0900 | [diff] [blame] | 54 | Log("var raw_trace_events = [\n"); |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 55 | trace_start_time_ = TimeTicks::Now(); |
erikkay@google.com | 66a4c3e | 2008-09-19 05:42:55 +0900 | [diff] [blame] | 56 | timer_.Start(TimeDelta::FromMilliseconds(250), this, &TraceLog::Heartbeat); |
| 57 | } |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 58 | return enabled_; |
| 59 | } |
| 60 | |
| 61 | // static |
| 62 | void TraceLog::StopTracing() { |
| 63 | TraceLog* trace = Singleton<TraceLog>::get(); |
| 64 | return trace->Stop(); |
| 65 | } |
| 66 | |
| 67 | void TraceLog::Stop() { |
| 68 | if (enabled_) { |
| 69 | enabled_ = false; |
phajdan.jr@chromium.org | 862ff1f | 2009-01-06 03:51:30 +0900 | [diff] [blame] | 70 | Log("];\n"); |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 71 | CloseLogFile(); |
erikkay@google.com | 66a4c3e | 2008-09-19 05:42:55 +0900 | [diff] [blame] | 72 | timer_.Stop(); |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
erikkay@google.com | 66a4c3e | 2008-09-19 05:42:55 +0900 | [diff] [blame] | 76 | void TraceLog::Heartbeat() { |
| 77 | std::string cpu = StringPrintf("%d", process_metrics_->GetCPUUsage()); |
| 78 | TRACE_EVENT_INSTANT("heartbeat.cpu", 0, cpu); |
| 79 | } |
| 80 | |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 81 | void TraceLog::CloseLogFile() { |
| 82 | if (log_file_) { |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 83 | file_util::CloseFile(log_file_); |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | bool TraceLog::OpenLogFile() { |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 88 | FilePath::StringType pid_filename = |
brettw@google.com | c60d989 | 2008-11-14 12:25:15 +0900 | [diff] [blame] | 89 | StringPrintf(kLogFileName, base::GetCurrentProcId()); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 90 | FilePath log_file_path; |
| 91 | if (!PathService::Get(base::DIR_EXE, &log_file_path)) |
| 92 | return false; |
| 93 | log_file_path = log_file_path.Append(pid_filename); |
| 94 | log_file_ = file_util::OpenFile(log_file_path, "a"); |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 95 | if (!log_file_) { |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 96 | // try the current directory |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 97 | log_file_ = file_util::OpenFile(FilePath(pid_filename), "a"); |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 98 | if (!log_file_) { |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | } |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 102 | return true; |
| 103 | } |
| 104 | |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 105 | void TraceLog::Trace(const std::string& name, |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 106 | EventType type, |
erikkay@google.com | 66a4c3e | 2008-09-19 05:42:55 +0900 | [diff] [blame] | 107 | const void* id, |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 108 | const std::wstring& extra, |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 109 | const char* file, |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 110 | int line) { |
| 111 | if (!enabled_) |
| 112 | return; |
| 113 | Trace(name, type, id, WideToUTF8(extra), file, line); |
| 114 | } |
| 115 | |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 116 | void TraceLog::Trace(const std::string& name, |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 117 | EventType type, |
erikkay@google.com | 66a4c3e | 2008-09-19 05:42:55 +0900 | [diff] [blame] | 118 | const void* id, |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 119 | const std::string& extra, |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 120 | const char* file, |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 121 | int line) { |
| 122 | if (!enabled_) |
| 123 | return; |
| 124 | |
| 125 | #ifdef USE_UNRELIABLE_NOW |
mbelshe@google.com | 556bc84 | 2008-09-26 12:00:00 +0900 | [diff] [blame] | 126 | TimeTicks tick = TimeTicks::HighResNow(); |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 127 | #else |
| 128 | TimeTicks tick = TimeTicks::Now(); |
| 129 | #endif |
| 130 | TimeDelta delta = tick - trace_start_time_; |
| 131 | int64 usec = delta.InMicroseconds(); |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 132 | std::string msg = |
erikkay@google.com | 66a4c3e | 2008-09-19 05:42:55 +0900 | [diff] [blame] | 133 | StringPrintf("{'pid':'0x%lx', 'tid':'0x%lx', 'type':'%s', " |
| 134 | "'name':'%s', 'id':'0x%lx', 'extra':'%s', 'file':'%s', " |
phajdan.jr@chromium.org | 862ff1f | 2009-01-06 03:51:30 +0900 | [diff] [blame] | 135 | "'line_number':'%d', 'usec_begin': %I64d},\n", |
brettw@google.com | c60d989 | 2008-11-14 12:25:15 +0900 | [diff] [blame] | 136 | base::GetCurrentProcId(), |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 137 | PlatformThread::CurrentId(), |
| 138 | kEventTypeNames[type], |
| 139 | name.c_str(), |
| 140 | id, |
| 141 | extra.c_str(), |
maruel@chromium.org | e574344 | 2009-03-05 21:46:38 +0900 | [diff] [blame] | 142 | file, |
erikkay@google.com | 66a4c3e | 2008-09-19 05:42:55 +0900 | [diff] [blame] | 143 | line, |
| 144 | usec); |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 145 | |
| 146 | Log(msg); |
| 147 | } |
| 148 | |
| 149 | void TraceLog::Log(const std::string& msg) { |
| 150 | AutoLock lock(file_lock_); |
| 151 | |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 152 | fprintf(log_file_, "%s", msg.c_str()); |
erikkay@google.com | 1fb73ba | 2008-09-03 01:42:15 +0900 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | } // namespace base |