OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/base_export.h" | 13 #include "base/base_export.h" |
| 14 #include "base/location.h" |
| 15 #include "base/time.h" |
14 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
15 #include "base/tracked.h" | |
16 #include "base/threading/thread_local_storage.h" | 17 #include "base/threading/thread_local_storage.h" |
17 | 18 |
18 // TrackedObjects provides a database of stats about objects (generally Tasks) | 19 // TrackedObjects provides a database of stats about objects (generally Tasks) |
19 // that are tracked. Tracking means their birth, death, duration, birth thread, | 20 // that are tracked. Tracking means their birth, death, duration, birth thread, |
20 // death thread, and birth place are recorded. This data is carefully spread | 21 // death thread, and birth place are recorded. This data is carefully spread |
21 // across a series of objects so that the counts and times can be rapidly | 22 // across a series of objects so that the counts and times can be rapidly |
22 // updated without (usually) having to lock the data, and hence there is usually | 23 // updated without (usually) having to lock the data, and hence there is usually |
23 // very little contention caused by the tracking. The data can be viewed via | 24 // very little contention caused by the tracking. The data can be viewed via |
24 // the about:tasks URL, with a variety of sorting and filtering choices. | 25 // the about:tasks URL, with a variety of sorting and filtering choices. |
25 // | 26 // |
(...skipping 24 matching lines...) Expand all Loading... |
50 // BirthOnThread) references to the static data provided in a Location instance, | 51 // BirthOnThread) references to the static data provided in a Location instance, |
51 // as well as a pointer specifying the thread on which the birth takes place. | 52 // as well as a pointer specifying the thread on which the birth takes place. |
52 // Hence there is at most one Births instance for each Location on each thread. | 53 // Hence there is at most one Births instance for each Location on each thread. |
53 // The derived Births class contains slots for recording statistics about all | 54 // The derived Births class contains slots for recording statistics about all |
54 // instances born at the same location. Statistics currently include only the | 55 // instances born at the same location. Statistics currently include only the |
55 // count of instances constructed. | 56 // count of instances constructed. |
56 // Since the base class BirthOnThread contains only constant data, it can be | 57 // Since the base class BirthOnThread contains only constant data, it can be |
57 // freely accessed by any thread at any time (i.e., only the statistic needs to | 58 // freely accessed by any thread at any time (i.e., only the statistic needs to |
58 // be handled carefully, and it is ONLY read or written by the birth thread). | 59 // be handled carefully, and it is ONLY read or written by the birth thread). |
59 // | 60 // |
60 // Having now either constructed or found the Births instance described above, a | 61 // For Tasks, having now either constructed or found the Births instance |
61 // pointer to the Births instance is then embedded in a base class of the | 62 // described above, a pointer to the Births instance is then recorded into the |
62 // instance we're tracking (usually a Task). This fact alone is very useful in | 63 // PendingTask structure in MessageLoop. This fact alone is very useful in |
63 // debugging, when there is a question of where an instance came from. In | 64 // debugging, when there is a question of where an instance came from. In |
64 // addition, the birth time is also embedded in the base class Tracked (see | 65 // addition, the birth time is also recorded and used to later evaluate the |
65 // tracked.h), and used to later evaluate the lifetime duration. | 66 // lifetime duration of the whole Task. As a result of the above embedding, we |
66 // As a result of the above embedding, we can (for any tracked instance) find | 67 // can find out a Task's location of birth, and thread of birth, without using |
67 // out its location of birth, and thread of birth, without using any locks, as | 68 // any locks, as all that data is constant across the life of the process. |
68 // all that data is constant across the life of the process. | 69 // |
| 70 // This can also be done for any other object as well by calling |
| 71 // TallyABirthIfActive() and TallyADeathIfActive() as appropriate. |
69 // | 72 // |
70 // The amount of memory used in the above data structures depends on how many | 73 // The amount of memory used in the above data structures depends on how many |
71 // threads there are, and how many Locations of construction there are. | 74 // threads there are, and how many Locations of construction there are. |
72 // Fortunately, we don't use memory that is the product of those two counts, but | 75 // Fortunately, we don't use memory that is the product of those two counts, but |
73 // rather we only need one Births instance for each thread that constructs an | 76 // rather we only need one Births instance for each thread that constructs an |
74 // instance at a Location. In many cases, instances (such as Tasks) are only | 77 // instance at a Location. In many cases, instances are only created on one |
75 // created on one thread, so the memory utilization is actually fairly | 78 // thread, so the memory utilization is actually fairly restrained. |
76 // restrained. | |
77 // | 79 // |
78 // Lastly, when an instance is deleted, the final tallies of statistics are | 80 // Lastly, when an instance is deleted, the final tallies of statistics are |
79 // carefully accumulated. That tallying wrties into slots (members) in a | 81 // carefully accumulated. That tallying wrties into slots (members) in a |
80 // collection of DeathData instances. For each birth place Location that is | 82 // collection of DeathData instances. For each birth place Location that is |
81 // destroyed on a thread, there is a DeathData instance to record the additional | 83 // destroyed on a thread, there is a DeathData instance to record the additional |
82 // death count, as well as accumulate the lifetime duration of the instance as | 84 // death count, as well as accumulate the lifetime duration of the instance as |
83 // it is destroyed (dies). By maintaining a single place to aggregate this | 85 // it is destroyed (dies). By maintaining a single place to aggregate this |
84 // addition *only* for the given thread, we avoid the need to lock such | 86 // addition *only* for the given thread, we avoid the need to lock such |
85 // DeathData instances. | 87 // DeathData instances. |
86 // | 88 // |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 // snapshot instances, and are used to print sub-totals in an about:tasks page. | 142 // snapshot instances, and are used to print sub-totals in an about:tasks page. |
141 // | 143 // |
142 // TODO(jar): I need to store DataCollections, and provide facilities for taking | 144 // TODO(jar): I need to store DataCollections, and provide facilities for taking |
143 // the difference between two gathered DataCollections. For now, I'm just | 145 // the difference between two gathered DataCollections. For now, I'm just |
144 // adding a hack that Reset()'s to zero all counts and stats. This is also | 146 // adding a hack that Reset()'s to zero all counts and stats. This is also |
145 // done in a slighly thread-unsafe fashion, as the reseting is done | 147 // done in a slighly thread-unsafe fashion, as the reseting is done |
146 // asynchronously relative to ongoing updates, and worse yet, some data fields | 148 // asynchronously relative to ongoing updates, and worse yet, some data fields |
147 // are 64bit quantities, and are not atomicly accessed (reset or incremented | 149 // are 64bit quantities, and are not atomicly accessed (reset or incremented |
148 // etc.). For basic profiling, this will work "most of the time," and should be | 150 // etc.). For basic profiling, this will work "most of the time," and should be |
149 // sufficient... but storing away DataCollections is the "right way" to do this. | 151 // sufficient... but storing away DataCollections is the "right way" to do this. |
150 // | 152 |
151 class MessageLoop; | 153 class MessageLoop; |
152 | 154 |
153 | |
154 namespace tracked_objects { | 155 namespace tracked_objects { |
155 | 156 |
156 //------------------------------------------------------------------------------ | 157 //------------------------------------------------------------------------------ |
157 // For a specific thread, and a specific birth place, the collection of all | 158 // For a specific thread, and a specific birth place, the collection of all |
158 // death info (with tallies for each death thread, to prevent access conflicts). | 159 // death info (with tallies for each death thread, to prevent access conflicts). |
159 class ThreadData; | 160 class ThreadData; |
160 class BASE_EXPORT BirthOnThread { | 161 class BASE_EXPORT BirthOnThread { |
161 public: | 162 public: |
162 explicit BirthOnThread(const Location& location); | 163 explicit BirthOnThread(const Location& location); |
163 | 164 |
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 }; | 667 }; |
667 static State state_; | 668 static State state_; |
668 | 669 |
669 DISALLOW_COPY_AND_ASSIGN(AutoTracking); | 670 DISALLOW_COPY_AND_ASSIGN(AutoTracking); |
670 }; | 671 }; |
671 | 672 |
672 | 673 |
673 } // namespace tracked_objects | 674 } // namespace tracked_objects |
674 | 675 |
675 #endif // BASE_TRACKED_OBJECTS_H_ | 676 #endif // BASE_TRACKED_OBJECTS_H_ |
OLD | NEW |