OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "gpu/common/gpu_trace_event.h" |
| 6 |
| 7 #include "base/format_macros.h" |
| 8 #include "base/process_util.h" |
| 9 #include "base/stringprintf.h" |
| 10 #include "base/utf_string_conversions.h" |
| 11 #include "base/time.h" |
| 12 |
| 13 #define USE_UNRELIABLE_NOW |
| 14 |
| 15 using namespace base; |
| 16 |
| 17 namespace gpu { |
| 18 |
| 19 // Controls the number of trace events we will buffer in-memory |
| 20 // before flushing them. |
| 21 #define TRACE_EVENT_BUFFER_SIZE 16384 |
| 22 |
| 23 //////////////////////////////////////////////////////////////////////////////// |
| 24 // |
| 25 // TraceLog::Category |
| 26 // |
| 27 //////////////////////////////////////////////////////////////////////////////// |
| 28 TraceCategory::TraceCategory(const char* name, bool enabled) |
| 29 : name_(name) { |
| 30 base::subtle::NoBarrier_Store(&enabled_, |
| 31 static_cast<base::subtle::Atomic32>(enabled)); |
| 32 } |
| 33 |
| 34 TraceCategory::~TraceCategory() { |
| 35 base::subtle::NoBarrier_Store(&enabled_, |
| 36 static_cast<base::subtle::Atomic32>(0)); |
| 37 } |
| 38 |
| 39 //////////////////////////////////////////////////////////////////////////////// |
| 40 // |
| 41 // TraceEvent |
| 42 // |
| 43 //////////////////////////////////////////////////////////////////////////////// |
| 44 |
| 45 namespace { |
| 46 const char* GetPhaseStr(TraceEventPhase phase) { |
| 47 if (phase == GPU_TRACE_EVENT_PHASE_BEGIN) { |
| 48 return "B"; |
| 49 } else if (phase == GPU_TRACE_EVENT_PHASE_INSTANT) { |
| 50 return "I"; |
| 51 } else if (phase == GPU_TRACE_EVENT_PHASE_END) { |
| 52 return "E"; |
| 53 } else { |
| 54 DCHECK(false); |
| 55 return "?"; |
| 56 } |
| 57 } |
| 58 } |
| 59 |
| 60 void TraceEvent::AppendAsJSON(std::string* out, |
| 61 const std::vector<TraceEvent>& events) { |
| 62 *out += "["; |
| 63 for (size_t i = 0; i < events.size(); ++i) { |
| 64 if (i > 0) |
| 65 *out += ","; |
| 66 events[i].AppendAsJSON(out); |
| 67 } |
| 68 *out += "]"; |
| 69 } |
| 70 |
| 71 void TraceEvent::AppendAsJSON(std::string* out) const { |
| 72 int nargs = 0; |
| 73 for (int i = 0; i < TRACE_MAX_NUM_ARGS; ++i) { |
| 74 if (argNames[i] == NULL) |
| 75 break; |
| 76 nargs += 1; |
| 77 } |
| 78 |
| 79 const char* phaseStr = GetPhaseStr(phase); |
| 80 int64 time_int64 = timestamp.ToInternalValue(); |
| 81 long long unsigned int time_llui = |
| 82 static_cast<long long unsigned int>(time_int64); |
| 83 StringAppendF(out, |
| 84 "{cat:'%s',pid:%i,tid:%i,ts:0x%llx,ph:'%s',name:'%s',args:{", |
| 85 category->name(), |
| 86 static_cast<int>(processId), |
| 87 static_cast<int>(threadId), |
| 88 time_llui, |
| 89 phaseStr, |
| 90 name); |
| 91 for (int i = 0; i < nargs; ++i) { |
| 92 if (i > 0) |
| 93 *out += ","; |
| 94 *out += argNames[i]; |
| 95 *out += ":'"; |
| 96 *out += argValues[i]; |
| 97 *out += "'"; |
| 98 } |
| 99 *out += "}}"; |
| 100 } |
| 101 |
| 102 //////////////////////////////////////////////////////////////////////////////// |
| 103 // |
| 104 // TraceLog |
| 105 // |
| 106 //////////////////////////////////////////////////////////////////////////////// |
| 107 |
| 108 // static |
| 109 TraceLog* TraceLog::GetInstance() { |
| 110 return Singleton<TraceLog, StaticMemorySingletonTraits<TraceLog> >::get(); |
| 111 } |
| 112 |
| 113 TraceLog::TraceLog() |
| 114 : enabled_(false) |
| 115 { |
| 116 logged_events_.reserve(1024); |
| 117 } |
| 118 |
| 119 TraceLog::~TraceLog() { |
| 120 } |
| 121 |
| 122 TraceCategory* TraceLog::GetCategory(const char* name) { |
| 123 AutoLock lock(lock_); |
| 124 // TODO(nduca): replace with a hash_map. |
| 125 for (int i = categories_.size() - 1; i >= 0; i-- ) { |
| 126 if (strcmp(categories_[i]->name(), name) == 0) |
| 127 return categories_[i]; |
| 128 } |
| 129 TraceCategory* category = new TraceCategory(name, enabled_); |
| 130 categories_.push_back(category); |
| 131 return category; |
| 132 } |
| 133 |
| 134 void TraceLog::SetEnabled(bool enabled) { |
| 135 AutoLock lock(lock_); |
| 136 if (enabled == enabled_) |
| 137 return; |
| 138 if (enabled) { |
| 139 // Enable all categories. |
| 140 enabled_ = true; |
| 141 for (size_t i = 0; i < categories_.size(); i++) { |
| 142 base::subtle::NoBarrier_Store(&categories_[i]->enabled_, |
| 143 static_cast<base::subtle::Atomic32>(1)); |
| 144 } |
| 145 } else { |
| 146 // Disable all categories. |
| 147 for (size_t i = 0; i < categories_.size(); i++) { |
| 148 base::subtle::NoBarrier_Store(&categories_[i]->enabled_, |
| 149 static_cast<base::subtle::Atomic32>(0)); |
| 150 } |
| 151 enabled_ = false; |
| 152 FlushWithLockAlreadyHeld(); |
| 153 } |
| 154 } |
| 155 |
| 156 void TraceLog::SetOutputCallback(TraceLog::OutputCallback* cb) { |
| 157 AutoLock lock(lock_); |
| 158 if (enabled_) { |
| 159 FlushWithLockAlreadyHeld(); |
| 160 } |
| 161 output_callback_.reset(cb); |
| 162 } |
| 163 |
| 164 void TraceLog::AddRemotelyCollectedData(const std::string& json_events) { |
| 165 AutoLock lock(lock_); |
| 166 if (output_callback_.get()) |
| 167 output_callback_->Run(json_events); |
| 168 } |
| 169 |
| 170 void TraceLog::Flush() { |
| 171 AutoLock lock(lock_); |
| 172 FlushWithLockAlreadyHeld(); |
| 173 } |
| 174 |
| 175 void TraceLog::FlushWithLockAlreadyHeld() { |
| 176 if (output_callback_.get() && logged_events_.size()) { |
| 177 std::string json_events; |
| 178 TraceEvent::AppendAsJSON(&json_events, logged_events_); |
| 179 output_callback_->Run(json_events); |
| 180 } |
| 181 logged_events_.erase(logged_events_.begin(), logged_events_.end()); |
| 182 } |
| 183 |
| 184 void TraceLog::AddTraceEvent(TraceEventPhase phase, |
| 185 const char* file, int line, |
| 186 TraceCategory* category, |
| 187 const char* name, |
| 188 const char* arg1name, const char* arg1val, |
| 189 const char* arg2name, const char* arg2val) { |
| 190 DCHECK(file && name); |
| 191 #ifdef USE_UNRELIABLE_NOW |
| 192 TimeTicks now = TimeTicks::HighResNow(); |
| 193 #else |
| 194 TimeTicks now = TimeTicks::Now(); |
| 195 #endif |
| 196 //static_cast<unsigned long>(base::GetCurrentProcId()), |
| 197 AutoLock lock(lock_); |
| 198 logged_events_.push_back(TraceEvent()); |
| 199 TraceEvent& event = logged_events_.back(); |
| 200 event.processId = static_cast<unsigned long>(base::GetCurrentProcId()); |
| 201 event.threadId = PlatformThread::CurrentId(); |
| 202 event.timestamp = now; |
| 203 event.phase = phase; |
| 204 event.category = category; |
| 205 event.name = name; |
| 206 event.argNames[0] = arg1name; |
| 207 event.argValues[0] = arg1name ? arg1val : ""; |
| 208 event.argNames[1] = arg2name; |
| 209 event.argValues[1] = arg2name ? arg2val : ""; |
| 210 COMPILE_ASSERT(TRACE_MAX_NUM_ARGS == 2, TraceEvent_arc_count_out_of_sync); |
| 211 |
| 212 if (logged_events_.size() > TRACE_EVENT_BUFFER_SIZE) |
| 213 FlushWithLockAlreadyHeld(); |
| 214 } |
| 215 |
| 216 } // namespace gpu |
OLD | NEW |