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 #ifndef BASE_TRACKED_OBJECTS_H_ | 5 #ifndef BASE_TRACKED_OBJECTS_H_ |
6 #define BASE_TRACKED_OBJECTS_H_ | 6 #define BASE_TRACKED_OBJECTS_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 #include <stack> | 11 #include <stack> |
12 #include <string> | 12 #include <string> |
13 #include <utility> | 13 #include <utility> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/base_export.h" | 16 #include "base/base_export.h" |
17 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
18 #include "base/lazy_instance.h" | 18 #include "base/lazy_instance.h" |
19 #include "base/location.h" | 19 #include "base/location.h" |
20 #include "base/profiler/alternate_timer.h" | 20 #include "base/profiler/alternate_timer.h" |
21 #include "base/profiler/tracked_time.h" | 21 #include "base/profiler/tracked_time.h" |
22 #include "base/time.h" | 22 #include "base/time.h" |
23 #include "base/synchronization/lock.h" | 23 #include "base/synchronization/lock.h" |
24 #include "base/threading/thread_local_storage.h" | 24 #include "base/threading/thread_local_storage.h" |
25 #include "base/tracking_info.h" | 25 #include "base/tracking_info.h" |
26 #include "base/values.h" | |
27 | 26 |
28 // TrackedObjects provides a database of stats about objects (generally Tasks) | 27 // TrackedObjects provides a database of stats about objects (generally Tasks) |
29 // that are tracked. Tracking means their birth, death, duration, birth thread, | 28 // that are tracked. Tracking means their birth, death, duration, birth thread, |
30 // death thread, and birth place are recorded. This data is carefully spread | 29 // death thread, and birth place are recorded. This data is carefully spread |
31 // across a series of objects so that the counts and times can be rapidly | 30 // across a series of objects so that the counts and times can be rapidly |
32 // updated without (usually) having to lock the data, and hence there is usually | 31 // updated without (usually) having to lock the data, and hence there is usually |
33 // very little contention caused by the tracking. The data can be viewed via | 32 // very little contention caused by the tracking. The data can be viewed via |
34 // the about:profiler URL, with a variety of sorting and filtering choices. | 33 // the about:profiler URL, with a variety of sorting and filtering choices. |
35 // | 34 // |
36 // These classes serve as the basis of a profiler of sorts for the Tasks system. | 35 // These classes serve as the basis of a profiler of sorts for the Tasks system. |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 // very little global performance impact. | 121 // very little global performance impact. |
123 // | 122 // |
124 // The above description tries to define the high performance (run time) | 123 // The above description tries to define the high performance (run time) |
125 // portions of these classes. After gathering statistics, calls instigated | 124 // portions of these classes. After gathering statistics, calls instigated |
126 // by visiting about:profiler will assemble and aggregate data for display. The | 125 // by visiting about:profiler will assemble and aggregate data for display. The |
127 // following data structures are used for producing such displays. They are | 126 // following data structures are used for producing such displays. They are |
128 // not performance critical, and their only major constraint is that they should | 127 // not performance critical, and their only major constraint is that they should |
129 // be able to run concurrently with ongoing augmentation of the birth and death | 128 // be able to run concurrently with ongoing augmentation of the birth and death |
130 // data. | 129 // data. |
131 // | 130 // |
131 // This header also exports collection of classes that provide serialized | |
132 // representations of the core tracked_objects:: classes. These serialized | |
133 // representations are designed for safe transmission of the tracked_objects:: | |
134 // data across process boundaries. Each consists of: | |
135 // (1) a default constructor, to support the IPC serialization macros, | |
136 // (2) a constructor that extracts data from the type being serialized, and | |
137 // (3) the serialized data. | |
138 // | |
132 // For a given birth location, information about births is spread across data | 139 // For a given birth location, information about births is spread across data |
133 // structures that are asynchronously changing on various threads. For display | 140 // structures that are asynchronously changing on various threads. For |
134 // purposes, we need to construct Snapshot instances for each combination of | 141 // serialization and display purposes, we need to construct SerializedSnapshot |
135 // birth thread, death thread, and location, along with the count of such | 142 // instances for each combination of birth thread, death thread, and location, |
136 // lifetimes. We gather such data into a Snapshot instances, so that such | 143 // along with the count of such lifetimes. We gather such data into a |
137 // instances can be sorted and aggregated (and remain frozen during our | 144 // SerializedSnapshot instances, so that such instances can be sorted and |
138 // processing). Snapshot instances use pointers to constant portions of the | 145 // aggregated (and remain frozen during our processing). |
139 // birth and death datastructures, but have local (frozen) copies of the actual | |
140 // statistics (birth count, durations, etc. etc.). | |
141 // | 146 // |
142 // A DataCollector is a container object that holds a set of Snapshots. The | 147 // The SerializedProcessData struct is a serialized representation of the list |
jar (doing other things)
2012/04/04 17:55:35
nit: The use of the word "Serialized..." as a pre
Ilya Sherman
2012/04/05 02:51:04
Went with 'Snapshot'
| |
143 // statistics in a snapshot are gathered asynhcronously relative to their | 148 // of ThreadData objects for a process. It holds a set of SerializedSnapshots |
144 // ongoing updates. It is possible, though highly unlikely, that stats could be | 149 // and tracks parent/child relationships for the executed tasks. The statistics |
145 // incorrectly recorded by this process (all data is held in 32 bit ints, but we | 150 // in a snapshot are gathered asynhcronously relative to their ongoing updates. |
146 // are not atomically collecting all data, so we could have count that does not, | 151 // It is possible, though highly unlikely, that stats could be incorrectly |
147 // for example, match with the number of durations we accumulated). The | 152 // recorded by this process (all data is held in 32 bit ints, but we are not |
148 // advantage to having fast (non-atomic) updates of the data outweighs the | 153 // atomically collecting all data, so we could have count that does not, for |
149 // minimal risk of a singular corrupt statistic snapshot (only the snapshot | 154 // example, match with the number of durations we accumulated). The advantage |
150 // could be corrupt, not the underlying and ongoing statistic). In constrast, | 155 // to having fast (non-atomic) updates of the data outweighs the minimal risk of |
151 // pointer data that is accessed during snapshotting is completely invariant, | 156 // a singular corrupt statistic snapshot (only the snapshot could be corrupt, |
152 // and hence is perfectly acquired (i.e., no potential corruption, and no risk | 157 // not the underlying and ongoing statistic). In constrast, pointer data that |
153 // of a bad memory reference). | 158 // is accessed during snapshotting is completely invariant, and hence is |
154 // | 159 // perfectly acquired (i.e., no potential corruption, and no risk of a bad |
155 // After an array of Snapshots instances are collected into a DataCollector, | 160 // memory reference). |
156 // they need to be prepared for displaying our output. We currently implement a | |
157 // serialization into a Value hierarchy, which is automatically translated to | |
158 // JSON when supplied to rendering Java Scirpt. | |
159 // | 161 // |
160 // TODO(jar): We can implement a Snapshot system that *tries* to grab the | 162 // TODO(jar): We can implement a Snapshot system that *tries* to grab the |
161 // snapshots on the source threads *when* they have MessageLoops available | 163 // snapshots on the source threads *when* they have MessageLoops available |
162 // (worker threads don't have message loops generally, and hence gathering from | 164 // (worker threads don't have message loops generally, and hence gathering from |
163 // them will continue to be asynchronous). We had an implementation of this in | 165 // them will continue to be asynchronous). We had an implementation of this in |
164 // the past, but the difficulty is dealing with message loops being terminated. | 166 // the past, but the difficulty is dealing with message loops being terminated. |
165 // We can *try* to spam the available threads via some message loop proxy to | 167 // We can *try* to spam the available threads via some message loop proxy to |
166 // achieve this feat, and it *might* be valuable when we are colecting data for | 168 // achieve this feat, and it *might* be valuable when we are colecting data for |
167 // upload via UMA (where correctness of data may be more significant than for a | 169 // upload via UMA (where correctness of data may be more significant than for a |
168 // single screen of about:profiler). | 170 // single screen of about:profiler). |
(...skipping 20 matching lines...) Expand all Loading... | |
189 namespace tracked_objects { | 191 namespace tracked_objects { |
190 | 192 |
191 //------------------------------------------------------------------------------ | 193 //------------------------------------------------------------------------------ |
192 // For a specific thread, and a specific birth place, the collection of all | 194 // For a specific thread, and a specific birth place, the collection of all |
193 // death info (with tallies for each death thread, to prevent access conflicts). | 195 // death info (with tallies for each death thread, to prevent access conflicts). |
194 class ThreadData; | 196 class ThreadData; |
195 class BASE_EXPORT BirthOnThread { | 197 class BASE_EXPORT BirthOnThread { |
196 public: | 198 public: |
197 BirthOnThread(const Location& location, const ThreadData& current); | 199 BirthOnThread(const Location& location, const ThreadData& current); |
198 | 200 |
199 const Location location() const; | 201 const Location location() const { return location_; } |
200 const ThreadData* birth_thread() const; | 202 const ThreadData* birth_thread() const { return birth_thread_; } |
201 | |
202 // Insert our state (location, and thread name) into the dictionary. | |
203 // Use the supplied |prefix| in front of "thread_name" and "location" | |
204 // respectively when defining keys. | |
205 void ToValue(const std::string& prefix, | |
206 base::DictionaryValue* dictionary) const; | |
207 | 203 |
208 private: | 204 private: |
209 // File/lineno of birth. This defines the essence of the task, as the context | 205 // File/lineno of birth. This defines the essence of the task, as the context |
210 // of the birth (construction) often tell what the item is for. This field | 206 // of the birth (construction) often tell what the item is for. This field |
211 // is const, and hence safe to access from any thread. | 207 // is const, and hence safe to access from any thread. |
212 const Location location_; | 208 const Location location_; |
213 | 209 |
214 // The thread that records births into this object. Only this thread is | 210 // The thread that records births into this object. Only this thread is |
215 // allowed to update birth_count_ (which changes over time). | 211 // allowed to update birth_count_ (which changes over time). |
216 const ThreadData* const birth_thread_; | 212 const ThreadData* const birth_thread_; |
217 | 213 |
218 DISALLOW_COPY_AND_ASSIGN(BirthOnThread); | 214 DISALLOW_COPY_AND_ASSIGN(BirthOnThread); |
219 }; | 215 }; |
220 | 216 |
221 //------------------------------------------------------------------------------ | 217 //------------------------------------------------------------------------------ |
218 // A serialized representation of the BirthOnThread class. | |
219 | |
220 struct BASE_EXPORT SerializedBirthOnThread { | |
221 SerializedBirthOnThread(); | |
222 explicit SerializedBirthOnThread(const BirthOnThread& birth); | |
223 ~SerializedBirthOnThread(); | |
224 | |
225 SerializedLocation location; | |
226 std::string thread_name; | |
227 }; | |
228 | |
229 //------------------------------------------------------------------------------ | |
222 // A class for accumulating counts of births (without bothering with a map<>). | 230 // A class for accumulating counts of births (without bothering with a map<>). |
223 | 231 |
224 class BASE_EXPORT Births: public BirthOnThread { | 232 class BASE_EXPORT Births: public BirthOnThread { |
225 public: | 233 public: |
226 Births(const Location& location, const ThreadData& current); | 234 Births(const Location& location, const ThreadData& current); |
227 | 235 |
228 int birth_count() const; | 236 int birth_count() const; |
229 | 237 |
230 // When we have a birth we update the count for this BirhPLace. | 238 // When we have a birth we update the count for this birthplace. |
231 void RecordBirth(); | 239 void RecordBirth(); |
232 | 240 |
233 // When a birthplace is changed (updated), we need to decrement the counter | 241 // When a birthplace is changed (updated), we need to decrement the counter |
234 // for the old instance. | 242 // for the old instance. |
235 void ForgetBirth(); | 243 void ForgetBirth(); |
236 | 244 |
237 // Hack to quickly reset all counts to zero. | 245 // Hack to quickly reset all counts to zero. |
238 void Clear(); | 246 void Clear(); |
239 | 247 |
240 private: | 248 private: |
(...skipping 17 matching lines...) Expand all Loading... | |
258 // threads, we create DeathData stats that tally the number of births without | 266 // threads, we create DeathData stats that tally the number of births without |
259 // a corresponding death. | 267 // a corresponding death. |
260 explicit DeathData(int count); | 268 explicit DeathData(int count); |
261 | 269 |
262 // Update stats for a task destruction (death) that had a Run() time of | 270 // Update stats for a task destruction (death) that had a Run() time of |
263 // |duration|, and has had a queueing delay of |queue_duration|. | 271 // |duration|, and has had a queueing delay of |queue_duration|. |
264 void RecordDeath(const DurationInt queue_duration, | 272 void RecordDeath(const DurationInt queue_duration, |
265 const DurationInt run_duration, | 273 const DurationInt run_duration, |
266 int random_number); | 274 int random_number); |
267 | 275 |
268 // Metrics accessors, used only in tests. | 276 // Metrics accessors, used only for serialization and in tests. |
269 int count() const; | 277 int count() const; |
270 DurationInt run_duration_sum() const; | 278 DurationInt run_duration_sum() const; |
271 DurationInt run_duration_max() const; | 279 DurationInt run_duration_max() const; |
272 DurationInt run_duration_sample() const; | 280 DurationInt run_duration_sample() const; |
273 DurationInt queue_duration_sum() const; | 281 DurationInt queue_duration_sum() const; |
274 DurationInt queue_duration_max() const; | 282 DurationInt queue_duration_max() const; |
275 DurationInt queue_duration_sample() const; | 283 DurationInt queue_duration_sample() const; |
276 | 284 |
277 // Construct a DictionaryValue instance containing all our stats. The caller | |
278 // assumes ownership of the returned instance. | |
279 base::DictionaryValue* ToValue() const; | |
280 | |
281 // Reset the max values to zero. | 285 // Reset the max values to zero. |
282 void ResetMax(); | 286 void ResetMax(); |
283 | 287 |
284 // Reset all tallies to zero. This is used as a hack on realtime data. | 288 // Reset all tallies to zero. This is used as a hack on realtime data. |
285 void Clear(); | 289 void Clear(); |
286 | 290 |
287 private: | 291 private: |
288 // Members are ordered from most regularly read and updated, to least | 292 // Members are ordered from most regularly read and updated, to least |
289 // frequently used. This might help a bit with cache lines. | 293 // frequently used. This might help a bit with cache lines. |
290 // Number of runs seen (divisor for calculating averages). | 294 // Number of runs seen (divisor for calculating averages). |
291 int count_; | 295 int count_; |
292 // Basic tallies, used to compute averages. | 296 // Basic tallies, used to compute averages. |
293 DurationInt run_duration_sum_; | 297 DurationInt run_duration_sum_; |
294 DurationInt queue_duration_sum_; | 298 DurationInt queue_duration_sum_; |
295 // Max values, used by local visualization routines. These are often read, | 299 // Max values, used by local visualization routines. These are often read, |
296 // but rarely updated. | 300 // but rarely updated. |
297 DurationInt run_duration_max_; | 301 DurationInt run_duration_max_; |
298 DurationInt queue_duration_max_; | 302 DurationInt queue_duration_max_; |
299 // Samples, used by by crowd sourcing gatherers. These are almost never read, | 303 // Samples, used by crowd sourcing gatherers. These are almost never read, |
300 // and rarely updated. | 304 // and rarely updated. |
301 DurationInt run_duration_sample_; | 305 DurationInt run_duration_sample_; |
302 DurationInt queue_duration_sample_; | 306 DurationInt queue_duration_sample_; |
303 }; | 307 }; |
304 | 308 |
305 //------------------------------------------------------------------------------ | 309 //------------------------------------------------------------------------------ |
310 // A serialized representation of the DeathData class. | |
311 | |
312 struct BASE_EXPORT SerializedDeathData { | |
313 SerializedDeathData(); | |
314 explicit SerializedDeathData(const DeathData& death_data); | |
315 ~SerializedDeathData(); | |
316 | |
317 int count; | |
318 DurationInt run_duration_sum; | |
319 DurationInt run_duration_max; | |
320 DurationInt run_duration_sample; | |
321 DurationInt queue_duration_sum; | |
322 DurationInt queue_duration_max; | |
323 DurationInt queue_duration_sample; | |
324 }; | |
325 | |
326 //------------------------------------------------------------------------------ | |
306 // A temporary collection of data that can be sorted and summarized. It is | 327 // A temporary collection of data that can be sorted and summarized. It is |
307 // gathered (carefully) from many threads. Instances are held in arrays and | 328 // gathered (carefully) from many threads. Instances are held in arrays and |
308 // processed, filtered, and rendered. | 329 // processed, filtered, and rendered. |
309 // The source of this data was collected on many threads, and is asynchronously | 330 // The source of this data was collected on many threads, and is asynchronously |
310 // changing. The data in this instance is not asynchronously changing. | 331 // changing. The data in this instance is not asynchronously changing. |
311 | 332 |
312 class BASE_EXPORT Snapshot { | 333 struct BASE_EXPORT SerializedSnapshot { |
jar (doing other things)
2012/04/04 17:55:35
nit: The historical name "Snapshot" is probably no
Ilya Sherman
2012/04/05 02:51:04
Went with just 'TaskSnapshot' -- does that sound o
| |
313 public: | 334 SerializedSnapshot(); |
314 // When snapshotting a full life cycle set (birth-to-death), use this: | 335 SerializedSnapshot(const BirthOnThread& birth, |
315 Snapshot(const BirthOnThread& birth_on_thread, | 336 const DeathData& death_data, |
316 const ThreadData& death_thread, | 337 const std::string& death_thread_name); |
317 const DeathData& death_data); | 338 ~SerializedSnapshot(); |
318 | 339 |
319 // When snapshotting a birth, with no death yet, use this: | 340 SerializedBirthOnThread birth; |
320 Snapshot(const BirthOnThread& birth_on_thread, int count); | 341 SerializedDeathData death_data; |
321 | 342 std::string death_thread_name; |
322 // Accessor, that provides default value when there is no death thread. | |
323 const std::string DeathThreadName() const; | |
324 | |
325 // Construct a DictionaryValue instance containing all our data recursively. | |
326 // The caller assumes ownership of the memory in the returned instance. | |
327 base::DictionaryValue* ToValue() const; | |
328 | |
329 private: | |
330 const BirthOnThread* birth_; // Includes Location and birth_thread. | |
331 const ThreadData* death_thread_; | |
332 DeathData death_data_; | |
333 }; | 343 }; |
334 | 344 |
335 //------------------------------------------------------------------------------ | 345 //------------------------------------------------------------------------------ |
336 // For each thread, we have a ThreadData that stores all tracking info generated | 346 // For each thread, we have a ThreadData that stores all tracking info generated |
337 // on this thread. This prevents the need for locking as data accumulates. | 347 // on this thread. This prevents the need for locking as data accumulates. |
338 // We use ThreadLocalStorage to quickly identfy the current ThreadData context. | 348 // We use ThreadLocalStorage to quickly identfy the current ThreadData context. |
339 // We also have a linked list of ThreadData instances, and that list is used to | 349 // We also have a linked list of ThreadData instances, and that list is used to |
340 // harvest data from all existing instances. | 350 // harvest data from all existing instances. |
341 | 351 |
352 struct SerializedProcessData; | |
342 class BASE_EXPORT ThreadData { | 353 class BASE_EXPORT ThreadData { |
343 public: | 354 public: |
344 // Current allowable states of the tracking system. The states can vary | 355 // Current allowable states of the tracking system. The states can vary |
345 // between ACTIVE and DEACTIVATED, but can never go back to UNINITIALIZED. | 356 // between ACTIVE and DEACTIVATED, but can never go back to UNINITIALIZED. |
346 enum Status { | 357 enum Status { |
347 UNINITIALIZED, // PRistine, link-time state before running. | 358 UNINITIALIZED, // PRistine, link-time state before running. |
348 DORMANT_DURING_TESTS, // Only used during testing. | 359 DORMANT_DURING_TESTS, // Only used during testing. |
349 DEACTIVATED, // No longer recording profling. | 360 DEACTIVATED, // No longer recording profling. |
350 PROFILING_ACTIVE, // Recording profiles (no parent-child links). | 361 PROFILING_ACTIVE, // Recording profiles (no parent-child links). |
351 PROFILING_CHILDREN_ACTIVE, // Fully active, recording parent-child links. | 362 PROFILING_CHILDREN_ACTIVE, // Fully active, recording parent-child links. |
(...skipping 10 matching lines...) Expand all Loading... | |
362 // set *before* any births on the threads have taken place. It is generally | 373 // set *before* any births on the threads have taken place. It is generally |
363 // only used by the message loop, which has a well defined thread name. | 374 // only used by the message loop, which has a well defined thread name. |
364 static void InitializeThreadContext(const std::string& suggested_name); | 375 static void InitializeThreadContext(const std::string& suggested_name); |
365 | 376 |
366 // Using Thread Local Store, find the current instance for collecting data. | 377 // Using Thread Local Store, find the current instance for collecting data. |
367 // If an instance does not exist, construct one (and remember it for use on | 378 // If an instance does not exist, construct one (and remember it for use on |
368 // this thread. | 379 // this thread. |
369 // This may return NULL if the system is disabled for any reason. | 380 // This may return NULL if the system is disabled for any reason. |
370 static ThreadData* Get(); | 381 static ThreadData* Get(); |
371 | 382 |
372 // Constructs a DictionaryValue instance containing all recursive results in | 383 // Fills |process_data| with all the recursive results in our process. |
373 // our process. The caller assumes ownership of the memory in the returned | 384 // During the scavenging, if |reset_max| is true, then the DeathData instances |
374 // instance. During the scavenging, if |reset_max| is true, then the | 385 // max-values are reset to zero during this scan. |
375 // DeathData instances max-values are reset to zero during this scan. | 386 static void ToSerializedProcessData(bool reset_max, |
376 static base::DictionaryValue* ToValue(bool reset_max); | 387 SerializedProcessData* process_data); |
377 | 388 |
378 // Finds (or creates) a place to count births from the given location in this | 389 // Finds (or creates) a place to count births from the given location in this |
379 // thread, and increment that tally. | 390 // thread, and increment that tally. |
380 // TallyABirthIfActive will returns NULL if the birth cannot be tallied. | 391 // TallyABirthIfActive will returns NULL if the birth cannot be tallied. |
381 static Births* TallyABirthIfActive(const Location& location); | 392 static Births* TallyABirthIfActive(const Location& location); |
382 | 393 |
383 // Records the end of a timed run of an object. The |completed_task| contains | 394 // Records the end of a timed run of an object. The |completed_task| contains |
384 // a pointer to a Births, the time_posted, and a delayed_start_time if any. | 395 // a pointer to a Births, the time_posted, and a delayed_start_time if any. |
385 // The |start_of_run| indicates when we started to perform the run of the | 396 // The |start_of_run| indicates when we started to perform the run of the |
386 // task. The delayed_start_time is non-null for tasks that were posted as | 397 // task. The delayed_start_time is non-null for tasks that were posted as |
(...skipping 21 matching lines...) Expand all Loading... | |
408 | 419 |
409 // Record the end of execution in region, generally corresponding to a scope | 420 // Record the end of execution in region, generally corresponding to a scope |
410 // being exited. | 421 // being exited. |
411 static void TallyRunInAScopedRegionIfTracking( | 422 static void TallyRunInAScopedRegionIfTracking( |
412 const Births* birth, | 423 const Births* birth, |
413 const TrackedTime& start_of_run, | 424 const TrackedTime& start_of_run, |
414 const TrackedTime& end_of_run); | 425 const TrackedTime& end_of_run); |
415 | 426 |
416 const std::string thread_name() const; | 427 const std::string thread_name() const; |
417 | 428 |
418 // Snapshot (under a lock) copies of the maps in each ThreadData instance. For | |
419 // each set of maps (BirthMap, DeathMap, and ParentChildSet) call the Append() | |
420 // method of the |target| DataCollector. If |reset_max| is true, then the max | |
421 // values in each DeathData instance should be reset during the scan. | |
422 static void SendAllMaps(bool reset_max, class DataCollector* target); | |
423 | |
424 // Hack: asynchronously clear all birth counts and death tallies data values | 429 // Hack: asynchronously clear all birth counts and death tallies data values |
425 // in all ThreadData instances. The numerical (zeroing) part is done without | 430 // in all ThreadData instances. The numerical (zeroing) part is done without |
426 // use of a locks or atomics exchanges, and may (for int64 values) produce | 431 // use of a locks or atomics exchanges, and may (for int64 values) produce |
427 // bogus counts VERY rarely. | 432 // bogus counts VERY rarely. |
428 static void ResetAllThreadData(); | 433 static void ResetAllThreadData(); |
429 | 434 |
430 // Initializes all statics if needed (this initialization call should be made | 435 // Initializes all statics if needed (this initialization call should be made |
431 // while we are single threaded). Returns false if unable to initialize. | 436 // while we are single threaded). Returns false if unable to initialize. |
432 static bool Initialize(); | 437 static bool Initialize(); |
433 | 438 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
511 | 516 |
512 | 517 |
513 // In this thread's data, record a new birth. | 518 // In this thread's data, record a new birth. |
514 Births* TallyABirth(const Location& location); | 519 Births* TallyABirth(const Location& location); |
515 | 520 |
516 // Find a place to record a death on this thread. | 521 // Find a place to record a death on this thread. |
517 void TallyADeath(const Births& birth, | 522 void TallyADeath(const Births& birth, |
518 DurationInt queue_duration, | 523 DurationInt queue_duration, |
519 DurationInt duration); | 524 DurationInt duration); |
520 | 525 |
526 // Snapshot (under a lock) the profiled data for the tasks in each ThreadData | |
527 // instance. Also updates the |birth_counts| tally for each task to keep | |
528 // track of the number of living instances of the task. If |reset_max| is | |
529 // true, then the max values in each DeathData instance should be reset during | |
530 // the scan. | |
531 static void SerializeAllExecutedTasks( | |
532 bool reset_max, | |
533 SerializedProcessData* process_data, | |
534 std::map<const BirthOnThread*, int>* birth_counts); | |
535 | |
536 // Snapshots (under a lock) the profiled data for the tasks for this thread | |
537 // and serializes all of the executed tasks' data -- i.e. the data for the | |
538 // tasks with with entries in the death_map_ -- into |process_data|. Also | |
539 // updates the |birth_counts| tally for each task to keep track of the number | |
540 // of living instances of the task. If |reset_max| is true, then the max | |
jar (doing other things)
2012/04/04 17:55:35
Comment should be clear that the count includes on
Ilya Sherman
2012/04/05 02:51:04
Done.
| |
541 // values in each DeathData instance should be reset during the scan. | |
542 void SerializeExecutedTasks( | |
543 bool reset_max, | |
544 SerializedProcessData* process_data, | |
545 std::map<const BirthOnThread*, int>* birth_counts); | |
546 | |
521 // Using our lock, make a copy of the specified maps. This call may be made | 547 // Using our lock, make a copy of the specified maps. This call may be made |
522 // on non-local threads, which necessitate the use of the lock to prevent | 548 // on non-local threads, which necessitate the use of the lock to prevent |
523 // the map(s) from being reallocaed while they are copied. If |reset_max| is | 549 // the map(s) from being reallocaed while they are copied. If |reset_max| is |
524 // true, then, just after we copy the DeathMap, we will set the max values to | 550 // true, then, just after we copy the DeathMap, we will set the max values to |
525 // zero in the active DeathMap (not the snapshot). | 551 // zero in the active DeathMap (not the snapshot). |
526 void SnapshotMaps(bool reset_max, | 552 void SnapshotMaps(bool reset_max, |
527 BirthMap* birth_map, | 553 BirthMap* birth_map, |
528 DeathMap* death_map, | 554 DeathMap* death_map, |
529 ParentChildSet* parent_child_set); | 555 ParentChildSet* parent_child_set); |
530 | 556 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
655 // Record of what the incarnation_counter_ was when this instance was created. | 681 // Record of what the incarnation_counter_ was when this instance was created. |
656 // If the incarnation_counter_ has changed, then we avoid pushing into the | 682 // If the incarnation_counter_ has changed, then we avoid pushing into the |
657 // pool (this is only critical in tests which go through multiple | 683 // pool (this is only critical in tests which go through multiple |
658 // incarnations). | 684 // incarnations). |
659 int incarnation_count_for_pool_; | 685 int incarnation_count_for_pool_; |
660 | 686 |
661 DISALLOW_COPY_AND_ASSIGN(ThreadData); | 687 DISALLOW_COPY_AND_ASSIGN(ThreadData); |
662 }; | 688 }; |
663 | 689 |
664 //------------------------------------------------------------------------------ | 690 //------------------------------------------------------------------------------ |
665 // DataCollector is a container class for Snapshot and BirthOnThread count | 691 // A serialized representation of a (parent, child) task pair, for tracking |
666 // items. | 692 // hierarchical profiles. |
667 | 693 |
668 class BASE_EXPORT DataCollector { | 694 struct BASE_EXPORT SerializedParentChildPair { |
669 public: | 695 public: |
670 typedef std::vector<Snapshot> Collection; | 696 SerializedParentChildPair(); |
697 explicit SerializedParentChildPair( | |
698 const ThreadData::ParentChildPair& parent_child); | |
699 ~SerializedParentChildPair(); | |
671 | 700 |
672 // Construct with a list of how many threads should contribute. This helps us | 701 SerializedBirthOnThread parent; |
673 // determine (in the async case) when we are done with all contributions. | 702 SerializedBirthOnThread child; |
674 DataCollector(); | 703 }; |
675 ~DataCollector(); | |
676 | 704 |
677 // Adds all stats from the indicated thread into our arrays. Accepts copies | 705 //------------------------------------------------------------------------------ |
678 // of the birth_map and death_map, so that the data will not change during the | 706 // A serialized representation of the list of ThreadData objects for a process. |
679 // iterations and processing. | |
680 void Append(const ThreadData &thread_data, | |
681 const ThreadData::BirthMap& birth_map, | |
682 const ThreadData::DeathMap& death_map, | |
683 const ThreadData::ParentChildSet& parent_child_set); | |
684 | 707 |
685 // After the accumulation phase, the following accessor is used to process the | 708 struct BASE_EXPORT SerializedProcessData { |
686 // data (i.e., sort it, filter it, etc.). | 709 public: |
687 Collection* collection(); | 710 SerializedProcessData(); |
711 ~SerializedProcessData(); | |
688 | 712 |
689 // Adds entries for all the remaining living objects (objects that have | 713 std::vector<SerializedSnapshot> snapshots; |
690 // tallied a birth, but have not yet tallied a matching death, and hence must | 714 std::vector<SerializedParentChildPair> descendants; |
691 // be either running, queued up, or being held in limbo for future posting). | 715 int process_id; |
692 // This should be called after all known ThreadData instances have been | |
693 // processed using Append(). | |
694 void AddListOfLivingObjects(); | |
695 | |
696 // Generates a ListValue representation of the vector of snapshots, and | |
697 // inserts the results into |dictionary|. | |
698 void ToValue(base::DictionaryValue* dictionary) const; | |
699 | |
700 private: | |
701 typedef std::map<const BirthOnThread*, int> BirthCount; | |
702 | |
703 // The array that we collect data into. | |
704 Collection collection_; | |
705 | |
706 // The total number of births recorded at each location for which we have not | |
707 // seen a death count. This map changes as we do Append() calls, and is later | |
708 // used by AddListOfLivingObjects() to gather up unaccounted for births. | |
709 BirthCount global_birth_count_; | |
710 | |
711 // The complete list of parent-child relationships among tasks. | |
712 ThreadData::ParentChildSet parent_child_set_; | |
713 | |
714 DISALLOW_COPY_AND_ASSIGN(DataCollector); | |
715 }; | 716 }; |
716 | 717 |
717 } // namespace tracked_objects | 718 } // namespace tracked_objects |
718 | 719 |
719 #endif // BASE_TRACKED_OBJECTS_H_ | 720 #endif // BASE_TRACKED_OBJECTS_H_ |
OLD | NEW |