Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "base/trace_event.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "base/file_path.h" |
michael@0 | 8 | #include "base/file_util.h" |
michael@0 | 9 | #include "base/path_service.h" |
michael@0 | 10 | #include "base/platform_thread.h" |
michael@0 | 11 | #include "base/process_util.h" |
michael@0 | 12 | #include "base/string_util.h" |
michael@0 | 13 | #include "base/time.h" |
michael@0 | 14 | |
michael@0 | 15 | #define USE_UNRELIABLE_NOW |
michael@0 | 16 | |
michael@0 | 17 | namespace base { |
michael@0 | 18 | |
michael@0 | 19 | static const char* kEventTypeNames[] = { |
michael@0 | 20 | "BEGIN", |
michael@0 | 21 | "END", |
michael@0 | 22 | "INSTANT" |
michael@0 | 23 | }; |
michael@0 | 24 | |
michael@0 | 25 | static const FilePath::CharType* kLogFileName = |
michael@0 | 26 | FILE_PATH_LITERAL("trace_%d.log"); |
michael@0 | 27 | |
michael@0 | 28 | TraceLog::TraceLog() : enabled_(false), log_file_(NULL) { |
michael@0 | 29 | base::ProcessHandle proc = base::GetCurrentProcessHandle(); |
michael@0 | 30 | process_metrics_.reset(base::ProcessMetrics::CreateProcessMetrics(proc)); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | TraceLog::~TraceLog() { |
michael@0 | 34 | Stop(); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | // static |
michael@0 | 38 | bool TraceLog::IsTracing() { |
michael@0 | 39 | TraceLog* trace = Singleton<TraceLog>::get(); |
michael@0 | 40 | return trace->enabled_; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | // static |
michael@0 | 44 | bool TraceLog::StartTracing() { |
michael@0 | 45 | TraceLog* trace = Singleton<TraceLog>::get(); |
michael@0 | 46 | return trace->Start(); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | bool TraceLog::Start() { |
michael@0 | 50 | if (enabled_) |
michael@0 | 51 | return true; |
michael@0 | 52 | enabled_ = OpenLogFile(); |
michael@0 | 53 | if (enabled_) { |
michael@0 | 54 | Log("var raw_trace_events = [\n"); |
michael@0 | 55 | trace_start_time_ = TimeTicks::Now(); |
michael@0 | 56 | timer_.Start(TimeDelta::FromMilliseconds(250), this, &TraceLog::Heartbeat); |
michael@0 | 57 | } |
michael@0 | 58 | return enabled_; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | // static |
michael@0 | 62 | void TraceLog::StopTracing() { |
michael@0 | 63 | TraceLog* trace = Singleton<TraceLog>::get(); |
michael@0 | 64 | return trace->Stop(); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | void TraceLog::Stop() { |
michael@0 | 68 | if (enabled_) { |
michael@0 | 69 | enabled_ = false; |
michael@0 | 70 | Log("];\n"); |
michael@0 | 71 | CloseLogFile(); |
michael@0 | 72 | timer_.Stop(); |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | void TraceLog::Heartbeat() { |
michael@0 | 77 | std::string cpu = StringPrintf("%d", process_metrics_->GetCPUUsage()); |
michael@0 | 78 | TRACE_EVENT_INSTANT("heartbeat.cpu", 0, cpu); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | void TraceLog::CloseLogFile() { |
michael@0 | 82 | if (log_file_) { |
michael@0 | 83 | file_util::CloseFile(log_file_); |
michael@0 | 84 | } |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | bool TraceLog::OpenLogFile() { |
michael@0 | 88 | FilePath::StringType pid_filename = |
michael@0 | 89 | StringPrintf(kLogFileName, base::GetCurrentProcId()); |
michael@0 | 90 | FilePath log_file_path; |
michael@0 | 91 | if (!PathService::Get(base::DIR_EXE, &log_file_path)) |
michael@0 | 92 | return false; |
michael@0 | 93 | log_file_path = log_file_path.Append(pid_filename); |
michael@0 | 94 | log_file_ = file_util::OpenFile(log_file_path, "a"); |
michael@0 | 95 | if (!log_file_) { |
michael@0 | 96 | // try the current directory |
michael@0 | 97 | log_file_ = file_util::OpenFile(FilePath(pid_filename), "a"); |
michael@0 | 98 | if (!log_file_) { |
michael@0 | 99 | return false; |
michael@0 | 100 | } |
michael@0 | 101 | } |
michael@0 | 102 | return true; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | void TraceLog::Trace(const std::string& name, |
michael@0 | 106 | EventType type, |
michael@0 | 107 | const void* id, |
michael@0 | 108 | const std::wstring& extra, |
michael@0 | 109 | const char* file, |
michael@0 | 110 | int line) { |
michael@0 | 111 | if (!enabled_) |
michael@0 | 112 | return; |
michael@0 | 113 | Trace(name, type, id, WideToUTF8(extra), file, line); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | void TraceLog::Trace(const std::string& name, |
michael@0 | 117 | EventType type, |
michael@0 | 118 | const void* id, |
michael@0 | 119 | const std::string& extra, |
michael@0 | 120 | const char* file, |
michael@0 | 121 | int line) { |
michael@0 | 122 | if (!enabled_) |
michael@0 | 123 | return; |
michael@0 | 124 | |
michael@0 | 125 | #ifdef USE_UNRELIABLE_NOW |
michael@0 | 126 | TimeTicks tick = TimeTicks::HighResNow(); |
michael@0 | 127 | #else |
michael@0 | 128 | TimeTicks tick = TimeTicks::Now(); |
michael@0 | 129 | #endif |
michael@0 | 130 | TimeDelta delta = tick - trace_start_time_; |
michael@0 | 131 | int64_t usec = delta.InMicroseconds(); |
michael@0 | 132 | std::string msg = |
michael@0 | 133 | StringPrintf("{'pid':'0x%lx', 'tid':'0x%lx', 'type':'%s', " |
michael@0 | 134 | "'name':'%s', 'id':'0x%lx', 'extra':'%s', 'file':'%s', " |
michael@0 | 135 | "'line_number':'%d', 'usec_begin': %I64d},\n", |
michael@0 | 136 | base::GetCurrentProcId(), |
michael@0 | 137 | PlatformThread::CurrentId(), |
michael@0 | 138 | kEventTypeNames[type], |
michael@0 | 139 | name.c_str(), |
michael@0 | 140 | id, |
michael@0 | 141 | extra.c_str(), |
michael@0 | 142 | file, |
michael@0 | 143 | line, |
michael@0 | 144 | usec); |
michael@0 | 145 | |
michael@0 | 146 | Log(msg); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | void TraceLog::Log(const std::string& msg) { |
michael@0 | 150 | AutoLock lock(file_lock_); |
michael@0 | 151 | |
michael@0 | 152 | fprintf(log_file_, "%s", msg.c_str()); |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | } // namespace base |