Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/tracked_objects.h" | 5 #include "base/tracked_objects.h" |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 | 9 |
| 10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 namespace { | 30 namespace { |
| 31 // TODO(jar): Evaluate the perf impact of enabling this. If the perf impact is | 31 // TODO(jar): Evaluate the perf impact of enabling this. If the perf impact is |
| 32 // negligible, enable by default. | 32 // negligible, enable by default. |
| 33 // Flag to compile out parent-child link recording. | 33 // Flag to compile out parent-child link recording. |
| 34 const bool kTrackParentChildLinks = false; | 34 const bool kTrackParentChildLinks = false; |
| 35 | 35 |
| 36 // When ThreadData is first initialized, should we start in an ACTIVE state to | 36 // When ThreadData is first initialized, should we start in an ACTIVE state to |
| 37 // record all of the startup-time tasks, or should we start up DEACTIVATED, so | 37 // record all of the startup-time tasks, or should we start up DEACTIVATED, so |
| 38 // that we only record after parsing the command line flag --enable-tracking. | 38 // that we only record after parsing the command line flag --enable-tracking. |
| 39 // Note that the flag may force either state, so this really controls only the | 39 // Note that the flag may force either state, so this really controls only the |
| 40 // period of time up until that flag is parsed. If there is no flag seen, then | 40 // period of time up until that flag is parsed. If there is no flag seen, then |
| 41 // this state may prevail for much or all of the process lifetime. | 41 // this state may prevail for much or all of the process lifetime. |
| 42 const ThreadData::Status kInitialStartupState = | 42 const ThreadData::Status kInitialStartupState = |
| 43 ThreadData::PROFILING_CHILDREN_ACTIVE; | 43 ThreadData::PROFILING_CHILDREN_ACTIVE; |
| 44 | 44 |
| 45 // Control whether an alternate time source (Now() function) is supported by | 45 // Control whether an alternate time source (Now() function) is supported by |
| 46 // the ThreadData class. This compile time flag should be set to true if we | 46 // the ThreadData class. This compile time flag should be set to true if we |
| 47 // want other modules (such as a memory allocator, or a thread-specific CPU time | 47 // want other modules (such as a memory allocator, or a thread-specific CPU time |
| 48 // clock) to be able to provide a thread-specific Now() function. Without this | 48 // clock) to be able to provide a thread-specific Now() function. Without this |
| 49 // compile-time flag, the code will only support the wall-clock time. This flag | 49 // compile-time flag, the code will only support the wall-clock time. This flag |
| 50 // can be flipped to efficiently disable this path (if there is a performance | 50 // can be flipped to efficiently disable this path (if there is a performance |
| 51 // problem with its presence). | 51 // problem with its presence). |
| 52 static const bool kAllowAlternateTimeSourceHandling = true; | 52 static const bool kAllowAlternateTimeSourceHandling = true; |
| 53 | 53 |
| 54 // Possible states of the profiler timing enabledness. | 54 // Possible states of the profiler timing enabledness. |
| 55 enum { | 55 enum { |
| 56 UNDEFINED_TIMING, | 56 UNDEFINED_TIMING, |
| 57 ENABLED_TIMING, | 57 ENABLED_TIMING, |
| 58 DISABLED_TIMING, | 58 DISABLED_TIMING, |
| 59 }; | 59 }; |
| 60 | 60 |
| 61 // State of the profiler timing enabledness. | 61 // State of the profiler timing enabledness. |
| 62 base::subtle::Atomic32 g_profiler_timing_enabled = UNDEFINED_TIMING; | 62 base::subtle::Atomic32 g_profiler_timing_enabled = UNDEFINED_TIMING; |
| 63 | 63 |
| 64 // Returns whether profiler timing is enabled. The default is true, but this may | 64 // Returns whether profiler timing is enabled. The default is true, but this |
| 65 // be overridden by a command-line flag. Some platforms may programmatically set | 65 // may be overridden by a command-line flag. Some platforms may |
| 66 // this command-line flag to the "off" value if it's not specified. | 66 // programmatically set this command-line flag to the "off" value if it's not |
| 67 // specified. | |
| 67 // This in turn can be overridden by explicitly calling | 68 // This in turn can be overridden by explicitly calling |
| 68 // ThreadData::EnableProfilerTiming, say, based on a field trial. | 69 // ThreadData::EnableProfilerTiming, say, based on a field trial. |
| 69 inline bool IsProfilerTimingEnabled() { | 70 inline bool IsProfilerTimingEnabled() { |
| 70 // Reading |g_profiler_timing_enabled| is done without barrier because | 71 // Reading |g_profiler_timing_enabled| is done without barrier because |
| 71 // multiple initialization is not an issue while the barrier can be relatively | 72 // multiple initialization is not an issue while the barrier can be relatively |
| 72 // costly given that this method is sometimes called in a tight loop. | 73 // costly given that this method is sometimes called in a tight loop. |
| 73 base::subtle::Atomic32 current_timing_enabled = | 74 base::subtle::Atomic32 current_timing_enabled = |
| 74 base::subtle::NoBarrier_Load(&g_profiler_timing_enabled); | 75 base::subtle::NoBarrier_Load(&g_profiler_timing_enabled); |
| 75 if (current_timing_enabled == UNDEFINED_TIMING) { | 76 if (current_timing_enabled == UNDEFINED_TIMING) { |
| 76 if (!base::CommandLine::InitializedForCurrentProcess()) | 77 if (!base::CommandLine::InitializedForCurrentProcess()) |
| 77 return true; | 78 return true; |
| 78 current_timing_enabled = | 79 current_timing_enabled = |
| 79 (base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 80 (base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 80 switches::kProfilerTiming) == | 81 switches::kProfilerTiming) == |
| 81 switches::kProfilerTimingDisabledValue) | 82 switches::kProfilerTimingDisabledValue) |
| 82 ? DISABLED_TIMING | 83 ? DISABLED_TIMING |
| 83 : ENABLED_TIMING; | 84 : ENABLED_TIMING; |
| 84 base::subtle::NoBarrier_Store(&g_profiler_timing_enabled, | 85 base::subtle::NoBarrier_Store(&g_profiler_timing_enabled, |
| 85 current_timing_enabled); | 86 current_timing_enabled); |
| 86 } | 87 } |
| 87 return current_timing_enabled == ENABLED_TIMING; | 88 return current_timing_enabled == ENABLED_TIMING; |
| 88 } | 89 } |
| 89 | 90 |
| 90 } // namespace | 91 } // namespace |
| 91 | 92 |
| 92 //------------------------------------------------------------------------------ | 93 //------------------------------------------------------------------------------ |
| 93 // DeathData tallies durations when a death takes place. | 94 // DeathData tallies durations when a death takes place. |
| 94 | 95 |
| 95 DeathData::DeathData() { | 96 DeathData::DeathData() |
| 96 Clear(); | 97 : count_(0), |
| 98 sample_probability_count_(0), | |
| 99 run_duration_sum_(0), | |
| 100 queue_duration_sum_(0), | |
| 101 run_duration_max_(0), | |
| 102 queue_duration_max_(0), | |
| 103 run_duration_sample_(0), | |
| 104 queue_duration_sample_(0), | |
| 105 last_phase_snapshot_(nullptr) { | |
| 97 } | 106 } |
| 98 | 107 |
| 99 DeathData::DeathData(int count) { | 108 DeathData::DeathData(const DeathData& other) |
| 100 Clear(); | 109 : count_(other.count_), |
| 101 count_ = count; | 110 sample_probability_count_(other.sample_probability_count_), |
| 111 run_duration_sum_(other.run_duration_sum_), | |
| 112 queue_duration_sum_(other.queue_duration_sum_), | |
| 113 run_duration_max_(other.run_duration_max_), | |
| 114 queue_duration_max_(other.queue_duration_max_), | |
| 115 run_duration_sample_(other.run_duration_sample_), | |
| 116 queue_duration_sample_(other.queue_duration_sample_), | |
| 117 last_phase_snapshot_(nullptr) { | |
| 118 // This constructor will be used by std::map when adding new DeathData values | |
| 119 // to the map. At that point, last_phase_snapshot_ is still NULL, so we don't | |
| 120 // need to worry about ownership transfer. | |
| 121 DCHECK(other.last_phase_snapshot_ == nullptr); | |
| 122 } | |
| 123 | |
| 124 DeathData::~DeathData() { | |
| 125 while (last_phase_snapshot_) { | |
| 126 DeathDataPhaseSnapshot* snapshot = last_phase_snapshot_; | |
| 127 last_phase_snapshot_ = snapshot->prev; | |
| 128 delete snapshot; | |
| 129 } | |
| 102 } | 130 } |
| 103 | 131 |
| 104 // TODO(jar): I need to see if this macro to optimize branching is worth using. | 132 // TODO(jar): I need to see if this macro to optimize branching is worth using. |
| 105 // | 133 // |
| 106 // This macro has no branching, so it is surely fast, and is equivalent to: | 134 // This macro has no branching, so it is surely fast, and is equivalent to: |
| 107 // if (assign_it) | 135 // if (assign_it) |
| 108 // target = source; | 136 // target = source; |
| 109 // We use a macro rather than a template to force this to inline. | 137 // We use a macro rather than a template to force this to inline. |
| 110 // Related code for calculating max is discussed on the web. | 138 // Related code for calculating max is discussed on the web. |
| 111 #define CONDITIONAL_ASSIGN(assign_it, target, source) \ | 139 #define CONDITIONAL_ASSIGN(assign_it, target, source) \ |
| 112 ((target) ^= ((target) ^ (source)) & -static_cast<int32>(assign_it)) | 140 ((target) ^= ((target) ^ (source)) & -static_cast<int32>(assign_it)) |
| 113 | 141 |
| 114 void DeathData::RecordDeath(const int32 queue_duration, | 142 void DeathData::RecordDeath(const int32 queue_duration, |
| 115 const int32 run_duration, | 143 const int32 run_duration, |
| 116 const uint32 random_number) { | 144 const uint32 random_number) { |
| 117 // We'll just clamp at INT_MAX, but we should note this in the UI as such. | 145 // We'll just clamp at INT_MAX, but we should note this in the UI as such. |
| 118 if (count_ < INT_MAX) | 146 if (count_ < INT_MAX) |
| 119 ++count_; | 147 ++count_; |
| 148 if (sample_probability_count_ < INT_MAX) | |
| 149 ++sample_probability_count_; | |
| 120 queue_duration_sum_ += queue_duration; | 150 queue_duration_sum_ += queue_duration; |
| 121 run_duration_sum_ += run_duration; | 151 run_duration_sum_ += run_duration; |
| 122 | 152 |
| 123 if (queue_duration_max_ < queue_duration) | 153 if (queue_duration_max_ < queue_duration) |
| 124 queue_duration_max_ = queue_duration; | 154 queue_duration_max_ = queue_duration; |
| 125 if (run_duration_max_ < run_duration) | 155 if (run_duration_max_ < run_duration) |
| 126 run_duration_max_ = run_duration; | 156 run_duration_max_ = run_duration; |
| 127 | 157 |
| 128 // Take a uniformly distributed sample over all durations ever supplied. | 158 // Take a uniformly distributed sample over all durations ever supplied during |
| 129 // The probability that we (instead) use this new sample is 1/count_. This | 159 // the current profiling phase. |
| 130 // results in a completely uniform selection of the sample (at least when we | 160 // The probability that we (instead) use this new sample is |
| 131 // don't clamp count_... but that should be inconsequentially likely). | 161 // 1/sample_probability_count_. This results in a completely uniform selection |
| 132 // We ignore the fact that we correlated our selection of a sample to the run | 162 // of the sample (at least when we don't clamp sample_probability_count_... |
| 133 // and queue times (i.e., we used them to generate random_number). | 163 // but that should be inconsequentially likely). We ignore the fact that we |
| 134 CHECK_GT(count_, 0); | 164 // correlated our selection of a sample to the run and queue times (i.e., we |
| 135 if (0 == (random_number % count_)) { | 165 // used them to generate random_number). |
| 166 CHECK_GT(sample_probability_count_, 0); | |
| 167 if (0 == (random_number % sample_probability_count_)) { | |
| 136 queue_duration_sample_ = queue_duration; | 168 queue_duration_sample_ = queue_duration; |
| 137 run_duration_sample_ = run_duration; | 169 run_duration_sample_ = run_duration; |
| 138 } | 170 } |
| 139 } | 171 } |
| 140 | 172 |
| 141 int DeathData::count() const { return count_; } | 173 int DeathData::count() const { return count_; } |
| 142 | 174 |
| 143 int32 DeathData::run_duration_sum() const { return run_duration_sum_; } | 175 int32 DeathData::run_duration_sum() const { return run_duration_sum_; } |
| 144 | 176 |
| 145 int32 DeathData::run_duration_max() const { return run_duration_max_; } | 177 int32 DeathData::run_duration_max() const { return run_duration_max_; } |
| 146 | 178 |
| 147 int32 DeathData::run_duration_sample() const { | 179 int32 DeathData::run_duration_sample() const { |
| 148 return run_duration_sample_; | 180 return run_duration_sample_; |
| 149 } | 181 } |
| 150 | 182 |
| 151 int32 DeathData::queue_duration_sum() const { | 183 int32 DeathData::queue_duration_sum() const { |
| 152 return queue_duration_sum_; | 184 return queue_duration_sum_; |
| 153 } | 185 } |
| 154 | 186 |
| 155 int32 DeathData::queue_duration_max() const { | 187 int32 DeathData::queue_duration_max() const { |
| 156 return queue_duration_max_; | 188 return queue_duration_max_; |
| 157 } | 189 } |
| 158 | 190 |
| 159 int32 DeathData::queue_duration_sample() const { | 191 int32 DeathData::queue_duration_sample() const { |
| 160 return queue_duration_sample_; | 192 return queue_duration_sample_; |
| 161 } | 193 } |
| 162 | 194 |
| 163 void DeathData::Clear() { | 195 DeathDataPhaseSnapshot* DeathData::last_phase_snapshot() { |
| 164 count_ = 0; | 196 return last_phase_snapshot_; |
| 165 run_duration_sum_ = 0; | 197 } |
| 198 | |
| 199 void DeathData::OnProfilingPhaseCompleted(int profiling_phase) { | |
| 200 // Snapshotting and storing current state. | |
| 201 last_phase_snapshot_ = new DeathDataPhaseSnapshot( | |
| 202 profiling_phase, count_, run_duration_sum_, run_duration_max_, | |
| 203 run_duration_sample_, queue_duration_sum_, queue_duration_max_, | |
| 204 queue_duration_sample_, last_phase_snapshot_); | |
| 205 | |
| 206 // Not touching fields for which a delta can be computed by comparing with a | |
| 207 // snapshot from the previous phase. Resetting other fields. Sample values | |
| 208 // will be reset upon next death recording because sample_probability_count_ | |
| 209 // is set to 0. | |
| 210 // We avoid resetting to 0 in favor of deltas whenever possible. The reason | |
| 211 // is that for incrementable fields, resetting to 0 from the snapshot thread | |
| 212 // potentially in parallel with incrementing in the death thread may result in | |
| 213 // significant data corruption that has a potential to grow with time. Not | |
| 214 // resetting incrementable fields and using deltas will cause any | |
| 215 // off-by-little corruptions to be likely fixed at the next snapshot. | |
| 216 // The max values are not incrementable, and cannot be deduced using deltas | |
| 217 // for a given phase. Hence, we have to reset them to 0. But the potential | |
| 218 // damage is limited to getting the previous phase's max to apply for the next | |
| 219 // phase, and the error doesn't have a potential to keep growing with new | |
| 220 // resets. | |
| 221 // sample_probability_count_ is incrementable, but must be reset to 0 at the | |
| 222 // phase end, so that we start a new uniformly randomized sample selection | |
| 223 // after the reset. Corruptions due to race conditions are possible, but the | |
| 224 // damage is limited to selecting a wrong sample, which is not something that | |
| 225 // can cause accumulating or cascading effects. | |
| 226 // If there were no corruptions caused by race conditions, we never send a | |
| 227 // sample for the previous phase in the next phase's snapshot because | |
| 228 // ThreadData::SnapshotExecutedTasks doesn't send deltas with 0 count. | |
| 229 sample_probability_count_ = 0; | |
| 166 run_duration_max_ = 0; | 230 run_duration_max_ = 0; |
| 167 run_duration_sample_ = 0; | |
| 168 queue_duration_sum_ = 0; | |
| 169 queue_duration_max_ = 0; | 231 queue_duration_max_ = 0; |
| 170 queue_duration_sample_ = 0; | |
| 171 } | 232 } |
| 172 | 233 |
| 173 //------------------------------------------------------------------------------ | 234 //------------------------------------------------------------------------------ |
| 174 DeathDataSnapshot::DeathDataSnapshot() | 235 DeathDataSnapshot::DeathDataSnapshot() |
| 175 : count(-1), | 236 : count(-1), |
| 176 run_duration_sum(-1), | 237 run_duration_sum(-1), |
| 177 run_duration_max(-1), | 238 run_duration_max(-1), |
| 178 run_duration_sample(-1), | 239 run_duration_sample(-1), |
| 179 queue_duration_sum(-1), | 240 queue_duration_sum(-1), |
| 180 queue_duration_max(-1), | 241 queue_duration_max(-1), |
| 181 queue_duration_sample(-1) { | 242 queue_duration_sample(-1) { |
| 182 } | 243 } |
| 183 | 244 |
| 184 DeathDataSnapshot::DeathDataSnapshot( | 245 DeathDataSnapshot::DeathDataSnapshot(int count, |
| 185 const tracked_objects::DeathData& death_data) | 246 int32 run_duration_sum, |
| 186 : count(death_data.count()), | 247 int32 run_duration_max, |
| 187 run_duration_sum(death_data.run_duration_sum()), | 248 int32 run_duration_sample, |
| 188 run_duration_max(death_data.run_duration_max()), | 249 int32 queue_duration_sum, |
| 189 run_duration_sample(death_data.run_duration_sample()), | 250 int32 queue_duration_max, |
| 190 queue_duration_sum(death_data.queue_duration_sum()), | 251 int32 queue_duration_sample) |
| 191 queue_duration_max(death_data.queue_duration_max()), | 252 : count(count), |
| 192 queue_duration_sample(death_data.queue_duration_sample()) { | 253 run_duration_sum(run_duration_sum), |
| 254 run_duration_max(run_duration_max), | |
| 255 run_duration_sample(run_duration_sample), | |
| 256 queue_duration_sum(queue_duration_sum), | |
| 257 queue_duration_max(queue_duration_max), | |
| 258 queue_duration_sample(queue_duration_sample) { | |
| 193 } | 259 } |
| 194 | 260 |
| 195 DeathDataSnapshot::~DeathDataSnapshot() { | 261 DeathDataSnapshot::~DeathDataSnapshot() { |
| 196 } | 262 } |
| 197 | 263 |
| 264 DeathDataSnapshot DeathDataSnapshot::Delta( | |
| 265 const DeathDataSnapshot& older) const { | |
| 266 return DeathDataSnapshot(count - older.count, | |
| 267 run_duration_sum - older.run_duration_sum, | |
| 268 run_duration_max, run_duration_sample, | |
| 269 queue_duration_sum - older.queue_duration_sum, | |
| 270 queue_duration_max, queue_duration_sample); | |
| 271 } | |
| 272 | |
| 198 //------------------------------------------------------------------------------ | 273 //------------------------------------------------------------------------------ |
| 199 BirthOnThread::BirthOnThread(const Location& location, | 274 BirthOnThread::BirthOnThread(const Location& location, |
| 200 const ThreadData& current) | 275 const ThreadData& current) |
| 201 : location_(location), | 276 : location_(location), |
| 202 birth_thread_(¤t) { | 277 birth_thread_(¤t) { |
| 203 } | 278 } |
| 204 | 279 |
| 205 //------------------------------------------------------------------------------ | 280 //------------------------------------------------------------------------------ |
| 206 BirthOnThreadSnapshot::BirthOnThreadSnapshot() { | 281 BirthOnThreadSnapshot::BirthOnThreadSnapshot() { |
| 207 } | 282 } |
| 208 | 283 |
| 209 BirthOnThreadSnapshot::BirthOnThreadSnapshot( | 284 BirthOnThreadSnapshot::BirthOnThreadSnapshot(const BirthOnThread& birth) |
| 210 const tracked_objects::BirthOnThread& birth) | |
| 211 : location(birth.location()), | 285 : location(birth.location()), |
| 212 thread_name(birth.birth_thread()->thread_name()) { | 286 thread_name(birth.birth_thread()->thread_name()) { |
| 213 } | 287 } |
| 214 | 288 |
| 215 BirthOnThreadSnapshot::~BirthOnThreadSnapshot() { | 289 BirthOnThreadSnapshot::~BirthOnThreadSnapshot() { |
| 216 } | 290 } |
| 217 | 291 |
| 218 //------------------------------------------------------------------------------ | 292 //------------------------------------------------------------------------------ |
| 219 Births::Births(const Location& location, const ThreadData& current) | 293 Births::Births(const Location& location, const ThreadData& current) |
| 220 : BirthOnThread(location, current), | 294 : BirthOnThread(location, current), |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 231 // TODO(jar): We should pull all these static vars together, into a struct, and | 305 // TODO(jar): We should pull all these static vars together, into a struct, and |
| 232 // optimize layout so that we benefit from locality of reference during accesses | 306 // optimize layout so that we benefit from locality of reference during accesses |
| 233 // to them. | 307 // to them. |
| 234 | 308 |
| 235 // static | 309 // static |
| 236 NowFunction* ThreadData::now_function_ = NULL; | 310 NowFunction* ThreadData::now_function_ = NULL; |
| 237 | 311 |
| 238 // static | 312 // static |
| 239 bool ThreadData::now_function_is_time_ = false; | 313 bool ThreadData::now_function_is_time_ = false; |
| 240 | 314 |
| 241 // A TLS slot which points to the ThreadData instance for the current thread. We | 315 // A TLS slot which points to the ThreadData instance for the current thread. |
| 242 // do a fake initialization here (zeroing out data), and then the real in-place | 316 // We do a fake initialization here (zeroing out data), and then the real |
| 243 // construction happens when we call tls_index_.Initialize(). | 317 // in-place construction happens when we call tls_index_.Initialize(). |
| 244 // static | 318 // static |
| 245 base::ThreadLocalStorage::StaticSlot ThreadData::tls_index_ = TLS_INITIALIZER; | 319 base::ThreadLocalStorage::StaticSlot ThreadData::tls_index_ = TLS_INITIALIZER; |
| 246 | 320 |
| 247 // static | 321 // static |
| 248 int ThreadData::worker_thread_data_creation_count_ = 0; | 322 int ThreadData::worker_thread_data_creation_count_ = 0; |
| 249 | 323 |
| 250 // static | 324 // static |
| 251 int ThreadData::cleanup_count_ = 0; | 325 int ThreadData::cleanup_count_ = 0; |
| 252 | 326 |
| 253 // static | 327 // static |
| 254 int ThreadData::incarnation_counter_ = 0; | 328 int ThreadData::incarnation_counter_ = 0; |
| 255 | 329 |
| 256 // static | 330 // static |
| 257 ThreadData* ThreadData::all_thread_data_list_head_ = NULL; | 331 ThreadData* ThreadData::all_thread_data_list_head_ = NULL; |
| 258 | 332 |
| 259 // static | 333 // static |
| 260 ThreadData* ThreadData::first_retired_worker_ = NULL; | 334 ThreadData* ThreadData::first_retired_worker_ = NULL; |
| 261 | 335 |
| 262 // static | 336 // static |
| 263 base::LazyInstance<base::Lock>::Leaky | 337 base::LazyInstance<base::Lock>::Leaky |
| 264 ThreadData::list_lock_ = LAZY_INSTANCE_INITIALIZER; | 338 ThreadData::list_lock_ = LAZY_INSTANCE_INITIALIZER; |
| 265 | 339 |
| 266 // static | 340 // static |
| 341 base::LazyInstance<base::ThreadChecker>::Leaky | |
| 342 ThreadData::snapshot_thread_checker_ = LAZY_INSTANCE_INITIALIZER; | |
| 343 | |
| 344 // static | |
| 267 ThreadData::Status ThreadData::status_ = ThreadData::UNINITIALIZED; | 345 ThreadData::Status ThreadData::status_ = ThreadData::UNINITIALIZED; |
| 268 | 346 |
| 269 ThreadData::ThreadData(const std::string& suggested_name) | 347 ThreadData::ThreadData(const std::string& suggested_name) |
| 270 : next_(NULL), | 348 : next_(NULL), |
| 271 next_retired_worker_(NULL), | 349 next_retired_worker_(NULL), |
| 272 worker_thread_number_(0), | 350 worker_thread_number_(0), |
| 273 incarnation_count_for_pool_(-1), | 351 incarnation_count_for_pool_(-1), |
| 274 current_stopwatch_(NULL) { | 352 current_stopwatch_(NULL) { |
| 275 DCHECK_GE(suggested_name.size(), 0u); | 353 DCHECK_GE(suggested_name.size(), 0u); |
| 276 thread_name_ = suggested_name; | 354 thread_name_ = suggested_name; |
| 277 PushToHeadOfList(); // Which sets real incarnation_count_for_pool_. | 355 PushToHeadOfList(); // Which sets real incarnation_count_for_pool_. |
| 278 } | 356 } |
| 279 | 357 |
| 280 ThreadData::ThreadData(int thread_number) | 358 ThreadData::ThreadData(int thread_number) |
| 281 : next_(NULL), | 359 : next_(NULL), |
| 282 next_retired_worker_(NULL), | 360 next_retired_worker_(NULL), |
| 283 worker_thread_number_(thread_number), | 361 worker_thread_number_(thread_number), |
| 284 incarnation_count_for_pool_(-1), | 362 incarnation_count_for_pool_(-1), |
| 285 current_stopwatch_(NULL) { | 363 current_stopwatch_(NULL) { |
| 286 CHECK_GT(thread_number, 0); | 364 CHECK_GT(thread_number, 0); |
| 287 base::StringAppendF(&thread_name_, "WorkerThread-%d", thread_number); | 365 base::StringAppendF(&thread_name_, "WorkerThread-%d", thread_number); |
| 288 PushToHeadOfList(); // Which sets real incarnation_count_for_pool_. | 366 PushToHeadOfList(); // Which sets real incarnation_count_for_pool_. |
| 289 } | 367 } |
| 290 | 368 |
| 291 ThreadData::~ThreadData() {} | 369 ThreadData::~ThreadData() { |
| 370 DCHECK(snapshot_thread_checker_.Get().CalledOnValidThread()); | |
| 371 } | |
| 292 | 372 |
| 293 void ThreadData::PushToHeadOfList() { | 373 void ThreadData::PushToHeadOfList() { |
| 294 // Toss in a hint of randomness (atop the uniniitalized value). | 374 // Toss in a hint of randomness (atop the uniniitalized value). |
| 295 (void)VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(&random_number_, | 375 (void)VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(&random_number_, |
| 296 sizeof(random_number_)); | 376 sizeof(random_number_)); |
| 297 MSAN_UNPOISON(&random_number_, sizeof(random_number_)); | 377 MSAN_UNPOISON(&random_number_, sizeof(random_number_)); |
| 298 random_number_ += static_cast<uint32>(this - static_cast<ThreadData*>(0)); | 378 random_number_ += static_cast<uint32>(this - static_cast<ThreadData*>(0)); |
| 299 random_number_ ^= (Now() - TrackedTime()).InMilliseconds(); | 379 random_number_ ^= (Now() - TrackedTime()).InMilliseconds(); |
| 300 | 380 |
| 301 DCHECK(!next_); | 381 DCHECK(!next_); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 354 } | 434 } |
| 355 DCHECK_GT(worker_thread_data->worker_thread_number_, 0); | 435 DCHECK_GT(worker_thread_data->worker_thread_number_, 0); |
| 356 | 436 |
| 357 tls_index_.Set(worker_thread_data); | 437 tls_index_.Set(worker_thread_data); |
| 358 return worker_thread_data; | 438 return worker_thread_data; |
| 359 } | 439 } |
| 360 | 440 |
| 361 // static | 441 // static |
| 362 void ThreadData::OnThreadTermination(void* thread_data) { | 442 void ThreadData::OnThreadTermination(void* thread_data) { |
| 363 DCHECK(thread_data); // TLS should *never* call us with a NULL. | 443 DCHECK(thread_data); // TLS should *never* call us with a NULL. |
| 364 // We must NOT do any allocations during this callback. There is a chance | 444 // We must NOT do any allocations during this callback. There is a chance |
| 365 // that the allocator is no longer active on this thread. | 445 // that the allocator is no longer active on this thread. |
| 366 reinterpret_cast<ThreadData*>(thread_data)->OnThreadTerminationCleanup(); | 446 reinterpret_cast<ThreadData*>(thread_data)->OnThreadTerminationCleanup(); |
| 367 } | 447 } |
| 368 | 448 |
| 369 void ThreadData::OnThreadTerminationCleanup() { | 449 void ThreadData::OnThreadTerminationCleanup() { |
| 370 // The list_lock_ was created when we registered the callback, so it won't be | 450 // The list_lock_ was created when we registered the callback, so it won't be |
| 371 // allocated here despite the lazy reference. | 451 // allocated here despite the lazy reference. |
| 372 base::AutoLock lock(*list_lock_.Pointer()); | 452 base::AutoLock lock(*list_lock_.Pointer()); |
| 373 if (incarnation_counter_ != incarnation_count_for_pool_) | 453 if (incarnation_counter_ != incarnation_count_for_pool_) |
| 374 return; // ThreadData was constructed in an earlier unit test. | 454 return; // ThreadData was constructed in an earlier unit test. |
| 375 ++cleanup_count_; | 455 ++cleanup_count_; |
| 376 // Only worker threads need to be retired and reused. | 456 // Only worker threads need to be retired and reused. |
| 377 if (!worker_thread_number_) { | 457 if (!worker_thread_number_) { |
| 378 return; | 458 return; |
| 379 } | 459 } |
| 380 // We must NOT do any allocations during this callback. | 460 // We must NOT do any allocations during this callback. |
| 381 // Using the simple linked lists avoids all allocations. | 461 // Using the simple linked lists avoids all allocations. |
| 382 DCHECK_EQ(this->next_retired_worker_, reinterpret_cast<ThreadData*>(NULL)); | 462 DCHECK_EQ(this->next_retired_worker_, reinterpret_cast<ThreadData*>(NULL)); |
| 383 this->next_retired_worker_ = first_retired_worker_; | 463 this->next_retired_worker_ = first_retired_worker_; |
| 384 first_retired_worker_ = this; | 464 first_retired_worker_ = this; |
| 385 } | 465 } |
| 386 | 466 |
| 387 // static | 467 // static |
| 388 void ThreadData::Snapshot(ProcessDataSnapshot* process_data_snapshot) { | 468 void ThreadData::Snapshot(int current_profiling_phase, |
| 389 ThreadData::SnapshotCurrentPhase( | 469 ProcessDataSnapshot* process_data_snapshot) { |
| 390 &process_data_snapshot->phased_process_data_snapshots[0]); | 470 DCHECK(snapshot_thread_checker_.Get().CalledOnValidThread()); |
| 471 | |
| 472 // Get an unchanging copy of a ThreadData list. | |
| 473 ThreadData* my_list = ThreadData::first(); | |
| 474 | |
| 475 // Gather data serially. | |
| 476 // This hackish approach *can* get some slightly corrupt tallies, as we are | |
| 477 // grabbing values without the protection of a lock, but it has the advantage | |
| 478 // of working even with threads that don't have message loops. If a user | |
| 479 // sees any strangeness, they can always just run their stats gathering a | |
| 480 // second time. | |
| 481 BirthCountMap birth_counts; | |
| 482 for (ThreadData* thread_data = my_list; thread_data; | |
| 483 thread_data = thread_data->next()) { | |
| 484 thread_data->SnapshotExecutedTasks(current_profiling_phase, | |
| 485 &process_data_snapshot->phased_snapshots, | |
| 486 &birth_counts); | |
| 487 } | |
| 488 | |
| 489 // Add births that are still active -- i.e. objects that have tallied a birth, | |
| 490 // but have not yet tallied a matching death, and hence must be either | |
| 491 // running, queued up, or being held in limbo for future posting. | |
| 492 auto current_phase_tasks = | |
| 493 &process_data_snapshot->phased_snapshots[current_profiling_phase].tasks; | |
| 494 for (const auto& birth_count : birth_counts) { | |
| 495 if (birth_count.second > 0) { | |
| 496 current_phase_tasks->push_back( | |
| 497 TaskSnapshot(BirthOnThreadSnapshot(*birth_count.first), | |
| 498 DeathDataSnapshot(birth_count.second, 0, 0, 0, 0, 0, 0), | |
| 499 "Still_Alive")); | |
| 500 } | |
| 501 } | |
| 502 } | |
| 503 | |
| 504 // static | |
| 505 void ThreadData::OnProfilingPhaseCompleted(int profiling_phase) { | |
| 506 DCHECK(snapshot_thread_checker_.Get().CalledOnValidThread()); | |
| 507 // Get an unchanging copy of a ThreadData list. | |
| 508 ThreadData* my_list = ThreadData::first(); | |
| 509 | |
| 510 // Add snapshots for all instances of death data in all threads serially. | |
| 511 // This hackish approach *can* get some slightly corrupt tallies, as we are | |
| 512 // grabbing values without the protection of a lock, but it has the advantage | |
| 513 // of working even with threads that don't have message loops. Any corruption | |
| 514 // shouldn't cause "cascading damage" to anything else (in later phases). | |
| 515 for (ThreadData* thread_data = my_list; thread_data; | |
| 516 thread_data = thread_data->next()) { | |
| 517 thread_data->OnProfilingPhaseCompletedOnThread(profiling_phase); | |
| 518 } | |
| 391 } | 519 } |
| 392 | 520 |
| 393 Births* ThreadData::TallyABirth(const Location& location) { | 521 Births* ThreadData::TallyABirth(const Location& location) { |
| 394 BirthMap::iterator it = birth_map_.find(location); | 522 BirthMap::iterator it = birth_map_.find(location); |
| 395 Births* child; | 523 Births* child; |
| 396 if (it != birth_map_.end()) { | 524 if (it != birth_map_.end()) { |
| 397 child = it->second; | 525 child = it->second; |
| 398 child->RecordBirth(); | 526 child->RecordBirth(); |
| 399 } else { | 527 } else { |
| 400 child = new Births(location, *this); // Leak this. | 528 child = new Births(location, *this); // Leak this. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 412 // Lock since the map may get relocated now, and other threads sometimes | 540 // Lock since the map may get relocated now, and other threads sometimes |
| 413 // snapshot it (but they lock before copying it). | 541 // snapshot it (but they lock before copying it). |
| 414 base::AutoLock lock(map_lock_); | 542 base::AutoLock lock(map_lock_); |
| 415 parent_child_set_.insert(pair); | 543 parent_child_set_.insert(pair); |
| 416 } | 544 } |
| 417 } | 545 } |
| 418 | 546 |
| 419 return child; | 547 return child; |
| 420 } | 548 } |
| 421 | 549 |
| 422 void ThreadData::TallyADeath(const Births& birth, | 550 void ThreadData::TallyADeath(const Births& births, |
| 423 int32 queue_duration, | 551 int32 queue_duration, |
| 424 const TaskStopwatch& stopwatch) { | 552 const TaskStopwatch& stopwatch) { |
| 425 int32 run_duration = stopwatch.RunDurationMs(); | 553 int32 run_duration = stopwatch.RunDurationMs(); |
| 426 | 554 |
| 427 // Stir in some randomness, plus add constant in case durations are zero. | 555 // Stir in some randomness, plus add constant in case durations are zero. |
| 428 const uint32 kSomePrimeNumber = 2147483647; | 556 const uint32 kSomePrimeNumber = 2147483647; |
| 429 random_number_ += queue_duration + run_duration + kSomePrimeNumber; | 557 random_number_ += queue_duration + run_duration + kSomePrimeNumber; |
| 430 // An address is going to have some randomness to it as well ;-). | 558 // An address is going to have some randomness to it as well ;-). |
| 431 random_number_ ^= static_cast<uint32>(&birth - reinterpret_cast<Births*>(0)); | 559 random_number_ ^= static_cast<uint32>(&births - reinterpret_cast<Births*>(0)); |
| 432 | 560 |
| 433 // We don't have queue durations without OS timer. OS timer is automatically | 561 // We don't have queue durations without OS timer. OS timer is automatically |
| 434 // used for task-post-timing, so the use of an alternate timer implies all | 562 // used for task-post-timing, so the use of an alternate timer implies all |
| 435 // queue times are invalid, unless it was explicitly said that we can trust | 563 // queue times are invalid, unless it was explicitly said that we can trust |
| 436 // the alternate timer. | 564 // the alternate timer. |
| 437 if (kAllowAlternateTimeSourceHandling && | 565 if (kAllowAlternateTimeSourceHandling && |
| 438 now_function_ && | 566 now_function_ && |
| 439 !now_function_is_time_) { | 567 !now_function_is_time_) { |
| 440 queue_duration = 0; | 568 queue_duration = 0; |
| 441 } | 569 } |
| 442 | 570 |
| 443 DeathMap::iterator it = death_map_.find(&birth); | 571 DeathMap::iterator it = death_map_.find(&births); |
| 444 DeathData* death_data; | 572 DeathData* death_data; |
| 445 if (it != death_map_.end()) { | 573 if (it != death_map_.end()) { |
| 446 death_data = &it->second; | 574 death_data = &it->second; |
| 447 } else { | 575 } else { |
| 448 base::AutoLock lock(map_lock_); // Lock as the map may get relocated now. | 576 base::AutoLock lock(map_lock_); // Lock as the map may get relocated now. |
| 449 death_data = &death_map_[&birth]; | 577 death_data = &death_map_[&births]; |
| 450 } // Release lock ASAP. | 578 } // Release lock ASAP. |
| 451 death_data->RecordDeath(queue_duration, run_duration, random_number_); | 579 death_data->RecordDeath(queue_duration, run_duration, random_number_); |
| 452 | 580 |
| 453 if (!kTrackParentChildLinks) | 581 if (!kTrackParentChildLinks) |
| 454 return; | 582 return; |
| 455 if (!parent_stack_.empty()) { // We might get turned off. | 583 if (!parent_stack_.empty()) { // We might get turned off. |
| 456 DCHECK_EQ(parent_stack_.top(), &birth); | 584 DCHECK_EQ(parent_stack_.top(), &births); |
| 457 parent_stack_.pop(); | 585 parent_stack_.pop(); |
| 458 } | 586 } |
| 459 } | 587 } |
| 460 | 588 |
| 461 // static | 589 // static |
| 462 Births* ThreadData::TallyABirthIfActive(const Location& location) { | 590 Births* ThreadData::TallyABirthIfActive(const Location& location) { |
| 463 if (!TrackingStatus()) | 591 if (!TrackingStatus()) |
| 464 return NULL; | 592 return NULL; |
| 465 ThreadData* current_thread_data = Get(); | 593 ThreadData* current_thread_data = Get(); |
| 466 if (!current_thread_data) | 594 if (!current_thread_data) |
| 467 return NULL; | 595 return NULL; |
| 468 return current_thread_data->TallyABirth(location); | 596 return current_thread_data->TallyABirth(location); |
| 469 } | 597 } |
| 470 | 598 |
| 471 // static | 599 // static |
| 472 void ThreadData::TallyRunOnNamedThreadIfTracking( | 600 void ThreadData::TallyRunOnNamedThreadIfTracking( |
| 473 const base::TrackingInfo& completed_task, | 601 const base::TrackingInfo& completed_task, |
| 474 const TaskStopwatch& stopwatch) { | 602 const TaskStopwatch& stopwatch) { |
| 475 // Even if we have been DEACTIVATED, we will process any pending births so | 603 // Even if we have been DEACTIVATED, we will process any pending births so |
| 476 // that our data structures (which counted the outstanding births) remain | 604 // that our data structures (which counted the outstanding births) remain |
| 477 // consistent. | 605 // consistent. |
| 478 const Births* birth = completed_task.birth_tally; | 606 const Births* births = completed_task.birth_tally; |
| 479 if (!birth) | 607 if (!births) |
| 480 return; | 608 return; |
| 481 ThreadData* current_thread_data = stopwatch.GetThreadData(); | 609 ThreadData* current_thread_data = stopwatch.GetThreadData(); |
| 482 if (!current_thread_data) | 610 if (!current_thread_data) |
| 483 return; | 611 return; |
| 484 | 612 |
| 485 // Watch out for a race where status_ is changing, and hence one or both | 613 // Watch out for a race where status_ is changing, and hence one or both |
| 486 // of start_of_run or end_of_run is zero. In that case, we didn't bother to | 614 // of start_of_run or end_of_run is zero. In that case, we didn't bother to |
| 487 // get a time value since we "weren't tracking" and we were trying to be | 615 // get a time value since we "weren't tracking" and we were trying to be |
| 488 // efficient by not calling for a genuine time value. For simplicity, we'll | 616 // efficient by not calling for a genuine time value. For simplicity, we'll |
| 489 // use a default zero duration when we can't calculate a true value. | 617 // use a default zero duration when we can't calculate a true value. |
| 490 TrackedTime start_of_run = stopwatch.StartTime(); | 618 TrackedTime start_of_run = stopwatch.StartTime(); |
| 491 int32 queue_duration = 0; | 619 int32 queue_duration = 0; |
| 492 if (!start_of_run.is_null()) { | 620 if (!start_of_run.is_null()) { |
| 493 queue_duration = (start_of_run - completed_task.EffectiveTimePosted()) | 621 queue_duration = (start_of_run - completed_task.EffectiveTimePosted()) |
| 494 .InMilliseconds(); | 622 .InMilliseconds(); |
| 495 } | 623 } |
| 496 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); | 624 current_thread_data->TallyADeath(*births, queue_duration, stopwatch); |
| 497 } | 625 } |
| 498 | 626 |
| 499 // static | 627 // static |
| 500 void ThreadData::TallyRunOnWorkerThreadIfTracking( | 628 void ThreadData::TallyRunOnWorkerThreadIfTracking( |
| 501 const Births* birth, | 629 const Births* births, |
| 502 const TrackedTime& time_posted, | 630 const TrackedTime& time_posted, |
| 503 const TaskStopwatch& stopwatch) { | 631 const TaskStopwatch& stopwatch) { |
| 504 // Even if we have been DEACTIVATED, we will process any pending births so | 632 // Even if we have been DEACTIVATED, we will process any pending births so |
| 505 // that our data structures (which counted the outstanding births) remain | 633 // that our data structures (which counted the outstanding births) remain |
| 506 // consistent. | 634 // consistent. |
| 507 if (!birth) | 635 if (!births) |
| 508 return; | 636 return; |
| 509 | 637 |
| 510 // TODO(jar): Support the option to coalesce all worker-thread activity under | 638 // TODO(jar): Support the option to coalesce all worker-thread activity under |
| 511 // one ThreadData instance that uses locks to protect *all* access. This will | 639 // one ThreadData instance that uses locks to protect *all* access. This will |
| 512 // reduce memory (making it provably bounded), but run incrementally slower | 640 // reduce memory (making it provably bounded), but run incrementally slower |
| 513 // (since we'll use locks on TallyABirth and TallyADeath). The good news is | 641 // (since we'll use locks on TallyABirth and TallyADeath). The good news is |
| 514 // that the locks on TallyADeath will be *after* the worker thread has run, | 642 // that the locks on TallyADeath will be *after* the worker thread has run, |
| 515 // and hence nothing will be waiting for the completion (... besides some | 643 // and hence nothing will be waiting for the completion (... besides some |
| 516 // other thread that might like to run). Also, the worker threads tasks are | 644 // other thread that might like to run). Also, the worker threads tasks are |
| 517 // generally longer, and hence the cost of the lock may perchance be amortized | 645 // generally longer, and hence the cost of the lock may perchance be amortized |
| 518 // over the long task's lifetime. | 646 // over the long task's lifetime. |
| 519 ThreadData* current_thread_data = stopwatch.GetThreadData(); | 647 ThreadData* current_thread_data = stopwatch.GetThreadData(); |
| 520 if (!current_thread_data) | 648 if (!current_thread_data) |
| 521 return; | 649 return; |
| 522 | 650 |
| 523 TrackedTime start_of_run = stopwatch.StartTime(); | 651 TrackedTime start_of_run = stopwatch.StartTime(); |
| 524 int32 queue_duration = 0; | 652 int32 queue_duration = 0; |
| 525 if (!start_of_run.is_null()) { | 653 if (!start_of_run.is_null()) { |
| 526 queue_duration = (start_of_run - time_posted).InMilliseconds(); | 654 queue_duration = (start_of_run - time_posted).InMilliseconds(); |
| 527 } | 655 } |
| 528 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); | 656 current_thread_data->TallyADeath(*births, queue_duration, stopwatch); |
| 529 } | 657 } |
| 530 | 658 |
| 531 // static | 659 // static |
| 532 void ThreadData::TallyRunInAScopedRegionIfTracking( | 660 void ThreadData::TallyRunInAScopedRegionIfTracking( |
| 533 const Births* birth, | 661 const Births* births, |
| 534 const TaskStopwatch& stopwatch) { | 662 const TaskStopwatch& stopwatch) { |
| 535 // Even if we have been DEACTIVATED, we will process any pending births so | 663 // Even if we have been DEACTIVATED, we will process any pending births so |
| 536 // that our data structures (which counted the outstanding births) remain | 664 // that our data structures (which counted the outstanding births) remain |
| 537 // consistent. | 665 // consistent. |
| 538 if (!birth) | 666 if (!births) |
| 539 return; | 667 return; |
| 540 | 668 |
| 541 ThreadData* current_thread_data = stopwatch.GetThreadData(); | 669 ThreadData* current_thread_data = stopwatch.GetThreadData(); |
| 542 if (!current_thread_data) | 670 if (!current_thread_data) |
| 543 return; | 671 return; |
| 544 | 672 |
| 545 int32 queue_duration = 0; | 673 int32 queue_duration = 0; |
| 546 current_thread_data->TallyADeath(*birth, queue_duration, stopwatch); | 674 current_thread_data->TallyADeath(*births, queue_duration, stopwatch); |
| 547 } | |
| 548 | |
| 549 // static | |
| 550 void ThreadData::SnapshotAllExecutedTasks( | |
| 551 ProcessDataPhaseSnapshot* process_data_phase, | |
| 552 BirthCountMap* birth_counts) { | |
| 553 // Get an unchanging copy of a ThreadData list. | |
| 554 ThreadData* my_list = ThreadData::first(); | |
| 555 | |
| 556 // Gather data serially. | |
| 557 // This hackish approach *can* get some slighly corrupt tallies, as we are | |
| 558 // grabbing values without the protection of a lock, but it has the advantage | |
| 559 // of working even with threads that don't have message loops. If a user | |
| 560 // sees any strangeness, they can always just run their stats gathering a | |
| 561 // second time. | |
| 562 for (ThreadData* thread_data = my_list; | |
| 563 thread_data; | |
| 564 thread_data = thread_data->next()) { | |
| 565 thread_data->SnapshotExecutedTasks(process_data_phase, birth_counts); | |
| 566 } | |
| 567 } | |
| 568 | |
| 569 // static | |
| 570 void ThreadData::SnapshotCurrentPhase( | |
| 571 ProcessDataPhaseSnapshot* process_data_phase) { | |
| 572 // Add births that have run to completion to |collected_data|. | |
| 573 // |birth_counts| tracks the total number of births recorded at each location | |
| 574 // for which we have not seen a death count. | |
| 575 BirthCountMap birth_counts; | |
| 576 ThreadData::SnapshotAllExecutedTasks(process_data_phase, &birth_counts); | |
| 577 | |
| 578 // Add births that are still active -- i.e. objects that have tallied a birth, | |
| 579 // but have not yet tallied a matching death, and hence must be either | |
| 580 // running, queued up, or being held in limbo for future posting. | |
| 581 for (const auto& birth_count : birth_counts) { | |
| 582 if (birth_count.second > 0) { | |
| 583 process_data_phase->tasks.push_back(TaskSnapshot( | |
| 584 *birth_count.first, DeathData(birth_count.second), "Still_Alive")); | |
| 585 } | |
| 586 } | |
| 587 } | 675 } |
| 588 | 676 |
| 589 void ThreadData::SnapshotExecutedTasks( | 677 void ThreadData::SnapshotExecutedTasks( |
| 590 ProcessDataPhaseSnapshot* process_data_phase, | 678 int current_profiling_phase, |
| 679 PhasedProcessDataSnapshotMap* phased_snapshots, | |
| 591 BirthCountMap* birth_counts) { | 680 BirthCountMap* birth_counts) { |
| 592 // Get copy of data, so that the data will not change during the iterations | 681 // Get copy of data, so that the data will not change during the iterations |
| 593 // and processing. | 682 // and processing. |
| 594 ThreadData::BirthMap birth_map; | 683 BirthMap birth_map; |
| 595 ThreadData::DeathMap death_map; | 684 DeathsSnapshot deaths; |
| 596 ThreadData::ParentChildSet parent_child_set; | 685 ParentChildSet parent_child_set; |
| 597 SnapshotMaps(&birth_map, &death_map, &parent_child_set); | 686 SnapshotMaps(current_profiling_phase, &birth_map, &deaths, &parent_child_set); |
| 598 | |
| 599 for (const auto& death : death_map) { | |
| 600 process_data_phase->tasks.push_back( | |
| 601 TaskSnapshot(*death.first, death.second, thread_name())); | |
| 602 (*birth_counts)[death.first] -= death.first->birth_count(); | |
| 603 } | |
| 604 | 687 |
| 605 for (const auto& birth : birth_map) { | 688 for (const auto& birth : birth_map) { |
| 606 (*birth_counts)[birth.second] += birth.second->birth_count(); | 689 (*birth_counts)[birth.second] += birth.second->birth_count(); |
| 607 } | 690 } |
| 608 | 691 |
| 609 if (!kTrackParentChildLinks) | 692 for (const auto& death : deaths) { |
| 610 return; | 693 (*birth_counts)[death.first] -= death.first->birth_count(); |
| 611 | 694 |
| 612 for (const auto& parent_child : parent_child_set) { | 695 // For the current death data, walk through all its snapshots, starting from |
| 613 process_data_phase->descendants.push_back( | 696 // the current one, then from the previous profiling phase etc., and for |
| 614 ParentChildPairSnapshot(parent_child)); | 697 // each snapshot calculate the delta between the snapshot and the previous |
| 698 // phase, if any. Store the deltas in the result. | |
| 699 for (const DeathDataPhaseSnapshot* phase = &death.second; phase; | |
| 700 phase = phase->prev) { | |
| 701 const DeathDataSnapshot& death_data = | |
| 702 phase->prev ? phase->death_data.Delta(phase->prev->death_data) | |
| 703 : phase->death_data; | |
| 704 | |
| 705 if (death_data.count > 0) { | |
| 706 (*phased_snapshots)[phase->profiling_phase].tasks.push_back( | |
| 707 TaskSnapshot(BirthOnThreadSnapshot(*death.first), death_data, | |
| 708 thread_name())); | |
| 709 } | |
| 710 } | |
| 615 } | 711 } |
| 616 } | 712 } |
| 617 | 713 |
| 618 // This may be called from another thread. | 714 // This may be called from another thread. |
| 619 void ThreadData::SnapshotMaps(BirthMap* birth_map, | 715 void ThreadData::SnapshotMaps(int profiling_phase, |
| 620 DeathMap* death_map, | 716 BirthMap* birth_map, |
| 717 DeathsSnapshot* deaths, | |
| 621 ParentChildSet* parent_child_set) { | 718 ParentChildSet* parent_child_set) { |
| 622 base::AutoLock lock(map_lock_); | 719 base::AutoLock lock(map_lock_); |
| 720 | |
| 623 for (const auto& birth : birth_map_) | 721 for (const auto& birth : birth_map_) |
| 624 (*birth_map)[birth.first] = birth.second; | 722 (*birth_map)[birth.first] = birth.second; |
| 625 for (const auto& death : death_map_) | 723 |
| 626 (*death_map)[death.first] = death.second; | 724 for (auto& death : death_map_) { |
| 725 deaths->push_back(DeathsSnapshot::value_type( | |
|
dcheng
2015/04/20 17:34:07
std::make_pair would be slightly shorter to write
vadimt
2015/04/20 19:43:27
Done.
| |
| 726 death.first, | |
| 727 DeathDataPhaseSnapshot(profiling_phase, death.second.count(), | |
| 728 death.second.run_duration_sum(), | |
| 729 death.second.run_duration_max(), | |
| 730 death.second.run_duration_sample(), | |
| 731 death.second.queue_duration_sum(), | |
| 732 death.second.queue_duration_max(), | |
| 733 death.second.queue_duration_sample(), | |
| 734 death.second.last_phase_snapshot()))); | |
| 735 } | |
| 627 | 736 |
| 628 if (!kTrackParentChildLinks) | 737 if (!kTrackParentChildLinks) |
| 629 return; | 738 return; |
| 630 | 739 |
| 631 for (const auto& parent_child : parent_child_set_) | 740 for (const auto& parent_child : parent_child_set_) |
| 632 parent_child_set->insert(parent_child); | 741 parent_child_set->insert(parent_child); |
| 633 } | 742 } |
| 634 | 743 |
| 744 void ThreadData::OnProfilingPhaseCompletedOnThread(int profiling_phase) { | |
| 745 base::AutoLock lock(map_lock_); | |
| 746 | |
| 747 for (auto& death : death_map_) { | |
| 748 death.second.OnProfilingPhaseCompleted(profiling_phase); | |
| 749 } | |
| 750 } | |
| 751 | |
| 635 static void OptionallyInitializeAlternateTimer() { | 752 static void OptionallyInitializeAlternateTimer() { |
| 636 NowFunction* alternate_time_source = GetAlternateTimeSource(); | 753 NowFunction* alternate_time_source = GetAlternateTimeSource(); |
| 637 if (alternate_time_source) | 754 if (alternate_time_source) |
| 638 ThreadData::SetAlternateTimeSource(alternate_time_source); | 755 ThreadData::SetAlternateTimeSource(alternate_time_source); |
| 639 } | 756 } |
| 640 | 757 |
| 641 bool ThreadData::Initialize() { | 758 bool ThreadData::Initialize() { |
| 642 if (status_ >= DEACTIVATED) | 759 if (status_ >= DEACTIVATED) |
| 643 return true; // Someone else did the initialization. | 760 return true; // Someone else did the initialization. |
| 644 // Due to racy lazy initialization in tests, we'll need to recheck status_ | 761 // Due to racy lazy initialization in tests, we'll need to recheck status_ |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 667 return false; | 784 return false; |
| 668 } else { | 785 } else { |
| 669 // TLS was initialzed for us earlier. | 786 // TLS was initialzed for us earlier. |
| 670 DCHECK_EQ(status_, DORMANT_DURING_TESTS); | 787 DCHECK_EQ(status_, DORMANT_DURING_TESTS); |
| 671 } | 788 } |
| 672 | 789 |
| 673 // Incarnation counter is only significant to testing, as it otherwise will | 790 // Incarnation counter is only significant to testing, as it otherwise will |
| 674 // never again change in this process. | 791 // never again change in this process. |
| 675 ++incarnation_counter_; | 792 ++incarnation_counter_; |
| 676 | 793 |
| 677 // The lock is not critical for setting status_, but it doesn't hurt. It also | 794 // The lock is not critical for setting status_, but it doesn't hurt. It also |
| 678 // ensures that if we have a racy initialization, that we'll bail as soon as | 795 // ensures that if we have a racy initialization, that we'll bail as soon as |
| 679 // we get the lock earlier in this method. | 796 // we get the lock earlier in this method. |
| 680 status_ = kInitialStartupState; | 797 status_ = kInitialStartupState; |
| 681 if (!kTrackParentChildLinks && | 798 if (!kTrackParentChildLinks && |
| 682 kInitialStartupState == PROFILING_CHILDREN_ACTIVE) | 799 kInitialStartupState == PROFILING_CHILDREN_ACTIVE) |
| 683 status_ = PROFILING_ACTIVE; | 800 status_ = PROFILING_ACTIVE; |
| 684 DCHECK(status_ != UNINITIALIZED); | 801 DCHECK(status_ != UNINITIALIZED); |
| 685 return true; | 802 return true; |
| 686 } | 803 } |
| 687 | 804 |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 902 | 1019 |
| 903 ThreadData* TaskStopwatch::GetThreadData() const { | 1020 ThreadData* TaskStopwatch::GetThreadData() const { |
| 904 #if DCHECK_IS_ON() | 1021 #if DCHECK_IS_ON() |
| 905 DCHECK(state_ != CREATED); | 1022 DCHECK(state_ != CREATED); |
| 906 #endif | 1023 #endif |
| 907 | 1024 |
| 908 return current_thread_data_; | 1025 return current_thread_data_; |
| 909 } | 1026 } |
| 910 | 1027 |
| 911 //------------------------------------------------------------------------------ | 1028 //------------------------------------------------------------------------------ |
| 1029 // DeathDataPhaseSnapshot | |
| 1030 | |
| 1031 DeathDataPhaseSnapshot::DeathDataPhaseSnapshot(int profiling_phase, | |
| 1032 int count, | |
| 1033 int32 run_duration_sum, | |
| 1034 int32 run_duration_max, | |
| 1035 int32 run_duration_sample, | |
| 1036 int32 queue_duration_sum, | |
| 1037 int32 queue_duration_max, | |
| 1038 int32 queue_duration_sample, | |
| 1039 DeathDataPhaseSnapshot* prev) | |
| 1040 : profiling_phase(profiling_phase), | |
| 1041 death_data(count, | |
| 1042 run_duration_sum, | |
| 1043 run_duration_max, | |
| 1044 run_duration_sample, | |
| 1045 queue_duration_sum, | |
| 1046 queue_duration_max, | |
| 1047 queue_duration_sample), | |
| 1048 prev(prev) { | |
| 1049 } | |
| 1050 | |
| 1051 //------------------------------------------------------------------------------ | |
| 1052 // TaskSnapshot | |
| 1053 | |
| 912 TaskSnapshot::TaskSnapshot() { | 1054 TaskSnapshot::TaskSnapshot() { |
| 913 } | 1055 } |
| 914 | 1056 |
| 915 TaskSnapshot::TaskSnapshot(const BirthOnThread& birth, | 1057 TaskSnapshot::TaskSnapshot(const BirthOnThreadSnapshot& birth, |
| 916 const DeathData& death_data, | 1058 const DeathDataSnapshot& death_data, |
| 917 const std::string& death_thread_name) | 1059 const std::string& death_thread_name) |
| 918 : birth(birth), | 1060 : birth(birth), |
| 919 death_data(death_data), | 1061 death_data(death_data), |
| 920 death_thread_name(death_thread_name) { | 1062 death_thread_name(death_thread_name) { |
| 921 } | 1063 } |
| 922 | 1064 |
| 923 TaskSnapshot::~TaskSnapshot() { | 1065 TaskSnapshot::~TaskSnapshot() { |
| 924 } | 1066 } |
| 925 | 1067 |
| 926 //------------------------------------------------------------------------------ | 1068 //------------------------------------------------------------------------------ |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 955 : process_id(base::GetCurrentProcId()) { | 1097 : process_id(base::GetCurrentProcId()) { |
| 956 #else | 1098 #else |
| 957 : process_id(base::kNullProcessId) { | 1099 : process_id(base::kNullProcessId) { |
| 958 #endif | 1100 #endif |
| 959 } | 1101 } |
| 960 | 1102 |
| 961 ProcessDataSnapshot::~ProcessDataSnapshot() { | 1103 ProcessDataSnapshot::~ProcessDataSnapshot() { |
| 962 } | 1104 } |
| 963 | 1105 |
| 964 } // namespace tracked_objects | 1106 } // namespace tracked_objects |
| OLD | NEW |