Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_TRACKED_OBJECTS_H_ |
michael@0 | 6 | #define BASE_TRACKED_OBJECTS_H_ |
michael@0 | 7 | |
michael@0 | 8 | //------------------------------------------------------------------------------ |
michael@0 | 9 | #include <map> |
michael@0 | 10 | #include <string> |
michael@0 | 11 | #include <vector> |
michael@0 | 12 | |
michael@0 | 13 | #include "base/lock.h" |
michael@0 | 14 | #include "base/message_loop.h" |
michael@0 | 15 | #include "base/thread_local_storage.h" |
michael@0 | 16 | #include "base/tracked.h" |
michael@0 | 17 | |
michael@0 | 18 | |
michael@0 | 19 | namespace tracked_objects { |
michael@0 | 20 | |
michael@0 | 21 | //------------------------------------------------------------------------------ |
michael@0 | 22 | // For a specific thread, and a specific birth place, the collection of all |
michael@0 | 23 | // death info (with tallies for each death thread, to prevent access conflicts). |
michael@0 | 24 | class ThreadData; |
michael@0 | 25 | class BirthOnThread { |
michael@0 | 26 | public: |
michael@0 | 27 | explicit BirthOnThread(const Location& location); |
michael@0 | 28 | |
michael@0 | 29 | const Location location() const { return location_; } |
michael@0 | 30 | const ThreadData* birth_thread() const { return birth_thread_; } |
michael@0 | 31 | |
michael@0 | 32 | private: |
michael@0 | 33 | // File/lineno of birth. This defines the essence of the type, as the context |
michael@0 | 34 | // of the birth (construction) often tell what the item is for. This field |
michael@0 | 35 | // is const, and hence safe to access from any thread. |
michael@0 | 36 | const Location location_; |
michael@0 | 37 | |
michael@0 | 38 | // The thread that records births into this object. Only this thread is |
michael@0 | 39 | // allowed to access birth_count_ (which changes over time). |
michael@0 | 40 | const ThreadData* birth_thread_; // The thread this birth took place on. |
michael@0 | 41 | |
michael@0 | 42 | DISALLOW_COPY_AND_ASSIGN(BirthOnThread); |
michael@0 | 43 | }; |
michael@0 | 44 | |
michael@0 | 45 | //------------------------------------------------------------------------------ |
michael@0 | 46 | // A class for accumulating counts of births (without bothering with a map<>). |
michael@0 | 47 | |
michael@0 | 48 | class Births: public BirthOnThread { |
michael@0 | 49 | public: |
michael@0 | 50 | explicit Births(const Location& location); |
michael@0 | 51 | |
michael@0 | 52 | int birth_count() const { return birth_count_; } |
michael@0 | 53 | |
michael@0 | 54 | // When we have a birth we update the count for this BirhPLace. |
michael@0 | 55 | void RecordBirth() { ++birth_count_; } |
michael@0 | 56 | |
michael@0 | 57 | // When a birthplace is changed (updated), we need to decrement the counter |
michael@0 | 58 | // for the old instance. |
michael@0 | 59 | void ForgetBirth() { --birth_count_; } // We corrected a birth place. |
michael@0 | 60 | |
michael@0 | 61 | private: |
michael@0 | 62 | // The number of births on this thread for our location_. |
michael@0 | 63 | int birth_count_; |
michael@0 | 64 | |
michael@0 | 65 | DISALLOW_COPY_AND_ASSIGN(Births); |
michael@0 | 66 | }; |
michael@0 | 67 | |
michael@0 | 68 | //------------------------------------------------------------------------------ |
michael@0 | 69 | // Basic info summarizing multiple destructions of an object with a single |
michael@0 | 70 | // birthplace (fixed Location). Used both on specific threads, and also used |
michael@0 | 71 | // in snapshots when integrating assembled data. |
michael@0 | 72 | |
michael@0 | 73 | class DeathData { |
michael@0 | 74 | public: |
michael@0 | 75 | // Default initializer. |
michael@0 | 76 | DeathData() : count_(0), square_duration_(0) {} |
michael@0 | 77 | |
michael@0 | 78 | // When deaths have not yet taken place, and we gather data from all the |
michael@0 | 79 | // threads, we create DeathData stats that tally the number of births without |
michael@0 | 80 | // a corrosponding death. |
michael@0 | 81 | explicit DeathData(int count) : count_(count), square_duration_(0) {} |
michael@0 | 82 | |
michael@0 | 83 | void RecordDeath(const base::TimeDelta& duration); |
michael@0 | 84 | |
michael@0 | 85 | // Metrics accessors. |
michael@0 | 86 | int count() const { return count_; } |
michael@0 | 87 | base::TimeDelta life_duration() const { return life_duration_; } |
michael@0 | 88 | int64_t square_duration() const { return square_duration_; } |
michael@0 | 89 | int AverageMsDuration() const; |
michael@0 | 90 | double StandardDeviation() const; |
michael@0 | 91 | |
michael@0 | 92 | // Accumulate metrics from other into this. |
michael@0 | 93 | void AddDeathData(const DeathData& other); |
michael@0 | 94 | |
michael@0 | 95 | // Simple print of internal state. |
michael@0 | 96 | void Write(std::string* output) const; |
michael@0 | 97 | |
michael@0 | 98 | void Clear(); |
michael@0 | 99 | |
michael@0 | 100 | private: |
michael@0 | 101 | int count_; // Number of destructions. |
michael@0 | 102 | base::TimeDelta life_duration_; // Sum of all lifetime durations. |
michael@0 | 103 | int64_t square_duration_; // Sum of squares in milliseconds. |
michael@0 | 104 | }; |
michael@0 | 105 | |
michael@0 | 106 | //------------------------------------------------------------------------------ |
michael@0 | 107 | // A temporary collection of data that can be sorted and summarized. It is |
michael@0 | 108 | // gathered (carefully) from many threads. Instances are held in arrays and |
michael@0 | 109 | // processed, filtered, and rendered. |
michael@0 | 110 | // The source of this data was collected on many threads, and is asynchronously |
michael@0 | 111 | // changing. The data in this instance is not asynchronously changing. |
michael@0 | 112 | |
michael@0 | 113 | class Snapshot { |
michael@0 | 114 | public: |
michael@0 | 115 | // When snapshotting a full life cycle set (birth-to-death), use this: |
michael@0 | 116 | Snapshot(const BirthOnThread& birth_on_thread, const ThreadData& death_thread, |
michael@0 | 117 | const DeathData& death_data); |
michael@0 | 118 | |
michael@0 | 119 | // When snapshotting a birth, with no death yet, use this: |
michael@0 | 120 | Snapshot(const BirthOnThread& birth_on_thread, int count); |
michael@0 | 121 | |
michael@0 | 122 | |
michael@0 | 123 | const ThreadData* birth_thread() const { return birth_->birth_thread(); } |
michael@0 | 124 | const Location location() const { return birth_->location(); } |
michael@0 | 125 | const BirthOnThread& birth() const { return *birth_; } |
michael@0 | 126 | const ThreadData* death_thread() const {return death_thread_; } |
michael@0 | 127 | const DeathData& death_data() const { return death_data_; } |
michael@0 | 128 | const std::string DeathThreadName() const; |
michael@0 | 129 | |
michael@0 | 130 | int count() const { return death_data_.count(); } |
michael@0 | 131 | base::TimeDelta life_duration() const { return death_data_.life_duration(); } |
michael@0 | 132 | int64_t square_duration() const { return death_data_.square_duration(); } |
michael@0 | 133 | int AverageMsDuration() const { return death_data_.AverageMsDuration(); } |
michael@0 | 134 | |
michael@0 | 135 | void Write(std::string* output) const; |
michael@0 | 136 | |
michael@0 | 137 | void Add(const Snapshot& other); |
michael@0 | 138 | |
michael@0 | 139 | private: |
michael@0 | 140 | const BirthOnThread* birth_; // Includes Location and birth_thread. |
michael@0 | 141 | const ThreadData* death_thread_; |
michael@0 | 142 | DeathData death_data_; |
michael@0 | 143 | }; |
michael@0 | 144 | //------------------------------------------------------------------------------ |
michael@0 | 145 | // DataCollector is a container class for Snapshot and BirthOnThread count |
michael@0 | 146 | // items. It protects the gathering under locks, so that it could be called via |
michael@0 | 147 | // Posttask on any threads, such as all the target threads in parallel. |
michael@0 | 148 | |
michael@0 | 149 | class DataCollector { |
michael@0 | 150 | public: |
michael@0 | 151 | typedef std::vector<Snapshot> Collection; |
michael@0 | 152 | |
michael@0 | 153 | // Construct with a list of how many threads should contribute. This helps us |
michael@0 | 154 | // determine (in the async case) when we are done with all contributions. |
michael@0 | 155 | DataCollector(); |
michael@0 | 156 | |
michael@0 | 157 | // Add all stats from the indicated thread into our arrays. This function is |
michael@0 | 158 | // mutex protected, and *could* be called from any threads (although current |
michael@0 | 159 | // implementation serialized calls to Append). |
michael@0 | 160 | void Append(const ThreadData& thread_data); |
michael@0 | 161 | |
michael@0 | 162 | // After the accumulation phase, the following access is to process data. |
michael@0 | 163 | Collection* collection(); |
michael@0 | 164 | |
michael@0 | 165 | // After collection of death data is complete, we can add entries for all the |
michael@0 | 166 | // remaining living objects. |
michael@0 | 167 | void AddListOfLivingObjects(); |
michael@0 | 168 | |
michael@0 | 169 | private: |
michael@0 | 170 | // This instance may be provided to several threads to contribute data. The |
michael@0 | 171 | // following counter tracks how many more threads will contribute. When it is |
michael@0 | 172 | // zero, then all asynchronous contributions are complete, and locked access |
michael@0 | 173 | // is no longer needed. |
michael@0 | 174 | int count_of_contributing_threads_; |
michael@0 | 175 | |
michael@0 | 176 | // The array that we collect data into. |
michael@0 | 177 | Collection collection_; |
michael@0 | 178 | |
michael@0 | 179 | // The total number of births recorded at each location for which we have not |
michael@0 | 180 | // seen a death count. |
michael@0 | 181 | typedef std::map<const BirthOnThread*, int> BirthCount; |
michael@0 | 182 | BirthCount global_birth_count_; |
michael@0 | 183 | |
michael@0 | 184 | Lock accumulation_lock_; // Protects access during accumulation phase. |
michael@0 | 185 | |
michael@0 | 186 | DISALLOW_COPY_AND_ASSIGN(DataCollector); |
michael@0 | 187 | }; |
michael@0 | 188 | |
michael@0 | 189 | //------------------------------------------------------------------------------ |
michael@0 | 190 | // Aggregation contains summaries (totals and subtotals) of groups of Snapshot |
michael@0 | 191 | // instances to provide printing of these collections on a single line. |
michael@0 | 192 | |
michael@0 | 193 | class Aggregation: public DeathData { |
michael@0 | 194 | public: |
michael@0 | 195 | Aggregation() : birth_count_(0) {} |
michael@0 | 196 | |
michael@0 | 197 | void AddDeathSnapshot(const Snapshot& snapshot); |
michael@0 | 198 | void AddBirths(const Births& births); |
michael@0 | 199 | void AddBirth(const BirthOnThread& birth); |
michael@0 | 200 | void AddBirthPlace(const Location& location); |
michael@0 | 201 | void Write(std::string* output) const; |
michael@0 | 202 | void Clear(); |
michael@0 | 203 | |
michael@0 | 204 | private: |
michael@0 | 205 | int birth_count_; |
michael@0 | 206 | std::map<std::string, int> birth_files_; |
michael@0 | 207 | std::map<Location, int> locations_; |
michael@0 | 208 | std::map<const ThreadData*, int> birth_threads_; |
michael@0 | 209 | DeathData death_data_; |
michael@0 | 210 | std::map<const ThreadData*, int> death_threads_; |
michael@0 | 211 | |
michael@0 | 212 | DISALLOW_COPY_AND_ASSIGN(Aggregation); |
michael@0 | 213 | }; |
michael@0 | 214 | |
michael@0 | 215 | //------------------------------------------------------------------------------ |
michael@0 | 216 | // Comparator does the comparison of Snapshot instances. It is |
michael@0 | 217 | // used to order the instances in a vector. It orders them into groups (for |
michael@0 | 218 | // aggregation), and can also order instances within the groups (for detailed |
michael@0 | 219 | // rendering of the instances). |
michael@0 | 220 | |
michael@0 | 221 | class Comparator { |
michael@0 | 222 | public: |
michael@0 | 223 | enum Selector { |
michael@0 | 224 | NIL = 0, |
michael@0 | 225 | BIRTH_THREAD = 1, |
michael@0 | 226 | DEATH_THREAD = 2, |
michael@0 | 227 | BIRTH_FILE = 4, |
michael@0 | 228 | BIRTH_FUNCTION = 8, |
michael@0 | 229 | BIRTH_LINE = 16, |
michael@0 | 230 | COUNT = 32, |
michael@0 | 231 | AVERAGE_DURATION = 64, |
michael@0 | 232 | TOTAL_DURATION = 128 |
michael@0 | 233 | }; |
michael@0 | 234 | |
michael@0 | 235 | explicit Comparator(); |
michael@0 | 236 | |
michael@0 | 237 | // Reset the comparator to a NIL selector. Reset() and recursively delete any |
michael@0 | 238 | // tiebreaker_ entries. NOTE: We can't use a standard destructor, because |
michael@0 | 239 | // the sort algorithm makes copies of this object, and then deletes them, |
michael@0 | 240 | // which would cause problems (either we'd make expensive deep copies, or we'd |
michael@0 | 241 | // do more thna one delete on a tiebreaker_. |
michael@0 | 242 | void Clear(); |
michael@0 | 243 | |
michael@0 | 244 | // The less() operator for sorting the array via std::sort(). |
michael@0 | 245 | bool operator()(const Snapshot& left, const Snapshot& right) const; |
michael@0 | 246 | |
michael@0 | 247 | void Sort(DataCollector::Collection* collection) const; |
michael@0 | 248 | |
michael@0 | 249 | // Check to see if the items are sort equivalents (should be aggregated). |
michael@0 | 250 | bool Equivalent(const Snapshot& left, const Snapshot& right) const; |
michael@0 | 251 | |
michael@0 | 252 | // Check to see if all required fields are present in the given sample. |
michael@0 | 253 | bool Acceptable(const Snapshot& sample) const; |
michael@0 | 254 | |
michael@0 | 255 | // A comparator can be refined by specifying what to do if the selected basis |
michael@0 | 256 | // for comparison is insufficient to establish an ordering. This call adds |
michael@0 | 257 | // the indicated attribute as the new "least significant" basis of comparison. |
michael@0 | 258 | void SetTiebreaker(Selector selector, const std::string required); |
michael@0 | 259 | |
michael@0 | 260 | // Indicate if this instance is set up to sort by the given Selector, thereby |
michael@0 | 261 | // putting that information in the SortGrouping, so it is not needed in each |
michael@0 | 262 | // printed line. |
michael@0 | 263 | bool IsGroupedBy(Selector selector) const; |
michael@0 | 264 | |
michael@0 | 265 | // Using the tiebreakers as set above, we mostly get an ordering, which |
michael@0 | 266 | // equivalent groups. If those groups are displayed (rather than just being |
michael@0 | 267 | // aggregated, then the following is used to order them (within the group). |
michael@0 | 268 | void SetSubgroupTiebreaker(Selector selector); |
michael@0 | 269 | |
michael@0 | 270 | // Output a header line that can be used to indicated what items will be |
michael@0 | 271 | // collected in the group. It lists all (potentially) tested attributes and |
michael@0 | 272 | // their values (in the sample item). |
michael@0 | 273 | bool WriteSortGrouping(const Snapshot& sample, std::string* output) const; |
michael@0 | 274 | |
michael@0 | 275 | // Output a sample, with SortGroup details not displayed. |
michael@0 | 276 | void WriteSnapshot(const Snapshot& sample, std::string* output) const; |
michael@0 | 277 | |
michael@0 | 278 | private: |
michael@0 | 279 | // The selector directs this instance to compare based on the specified |
michael@0 | 280 | // members of the tested elements. |
michael@0 | 281 | enum Selector selector_; |
michael@0 | 282 | |
michael@0 | 283 | // For filtering into acceptable and unacceptable snapshot instance, the |
michael@0 | 284 | // following is required to be a substring of the selector_ field. |
michael@0 | 285 | std::string required_; |
michael@0 | 286 | |
michael@0 | 287 | // If this instance can't decide on an ordering, we can consult a tie-breaker |
michael@0 | 288 | // which may have a different basis of comparison. |
michael@0 | 289 | Comparator* tiebreaker_; |
michael@0 | 290 | |
michael@0 | 291 | // We or together all the selectors we sort on (not counting sub-group |
michael@0 | 292 | // selectors), so that we can tell if we've decided to group on any given |
michael@0 | 293 | // criteria. |
michael@0 | 294 | int combined_selectors_; |
michael@0 | 295 | |
michael@0 | 296 | // Some tiebreakrs are for subgroup ordering, and not for basic ordering (in |
michael@0 | 297 | // preparation for aggregation). The subgroup tiebreakers are not consulted |
michael@0 | 298 | // when deciding if two items are in equivalent groups. This flag tells us |
michael@0 | 299 | // to ignore the tiebreaker when doing Equivalent() testing. |
michael@0 | 300 | bool use_tiebreaker_for_sort_only_; |
michael@0 | 301 | }; |
michael@0 | 302 | |
michael@0 | 303 | |
michael@0 | 304 | //------------------------------------------------------------------------------ |
michael@0 | 305 | // For each thread, we have a ThreadData that stores all tracking info generated |
michael@0 | 306 | // on this thread. This prevents the need for locking as data accumulates. |
michael@0 | 307 | |
michael@0 | 308 | class ThreadData { |
michael@0 | 309 | public: |
michael@0 | 310 | typedef std::map<Location, Births*> BirthMap; |
michael@0 | 311 | typedef std::map<const Births*, DeathData> DeathMap; |
michael@0 | 312 | |
michael@0 | 313 | ThreadData(); |
michael@0 | 314 | |
michael@0 | 315 | // Using Thread Local Store, find the current instance for collecting data. |
michael@0 | 316 | // If an instance does not exist, construct one (and remember it for use on |
michael@0 | 317 | // this thread. |
michael@0 | 318 | // If shutdown has already started, and we don't yet have an instance, then |
michael@0 | 319 | // return null. |
michael@0 | 320 | static ThreadData* current(); |
michael@0 | 321 | |
michael@0 | 322 | // In this thread's data, find a place to record a new birth. |
michael@0 | 323 | Births* FindLifetime(const Location& location); |
michael@0 | 324 | |
michael@0 | 325 | // Find a place to record a death on this thread. |
michael@0 | 326 | void TallyADeath(const Births& lifetimes, const base::TimeDelta& duration); |
michael@0 | 327 | |
michael@0 | 328 | // (Thread safe) Get start of list of instances. |
michael@0 | 329 | static ThreadData* first(); |
michael@0 | 330 | // Iterate through the null terminated list of instances. |
michael@0 | 331 | ThreadData* next() const { return next_; } |
michael@0 | 332 | |
michael@0 | 333 | MessageLoop* message_loop() const { return message_loop_; } |
michael@0 | 334 | const std::string ThreadName() const; |
michael@0 | 335 | |
michael@0 | 336 | // Using our lock, make a copy of the specified maps. These calls may arrive |
michael@0 | 337 | // from non-local threads. |
michael@0 | 338 | void SnapshotBirthMap(BirthMap *output) const; |
michael@0 | 339 | void SnapshotDeathMap(DeathMap *output) const; |
michael@0 | 340 | |
michael@0 | 341 | static void RunOnAllThreads(void (*Func)()); |
michael@0 | 342 | |
michael@0 | 343 | // Set internal status_ to either become ACTIVE, or later, to be SHUTDOWN, |
michael@0 | 344 | // based on argument being true or false respectively. |
michael@0 | 345 | // IF tracking is not compiled in, this function will return false. |
michael@0 | 346 | static bool StartTracking(bool status); |
michael@0 | 347 | static bool IsActive(); |
michael@0 | 348 | |
michael@0 | 349 | #ifdef OS_WIN |
michael@0 | 350 | // WARNING: ONLY call this function when all MessageLoops are still intact for |
michael@0 | 351 | // all registered threads. IF you call it later, you will crash. |
michael@0 | 352 | // Note: You don't need to call it at all, and you can wait till you are |
michael@0 | 353 | // single threaded (again) to do the cleanup via |
michael@0 | 354 | // ShutdownSingleThreadedCleanup(). |
michael@0 | 355 | // Start the teardown (shutdown) process in a multi-thread mode by disabling |
michael@0 | 356 | // further additions to thread database on all threads. First it makes a |
michael@0 | 357 | // local (locked) change to prevent any more threads from registering. Then |
michael@0 | 358 | // it Posts a Task to all registered threads to be sure they are aware that no |
michael@0 | 359 | // more accumulation can take place. |
michael@0 | 360 | static void ShutdownMultiThreadTracking(); |
michael@0 | 361 | #endif |
michael@0 | 362 | |
michael@0 | 363 | // WARNING: ONLY call this function when you are running single threaded |
michael@0 | 364 | // (again) and all message loops and threads have terminated. Until that |
michael@0 | 365 | // point some threads may still attempt to write into our data structures. |
michael@0 | 366 | // Delete recursively all data structures, starting with the list of |
michael@0 | 367 | // ThreadData instances. |
michael@0 | 368 | static void ShutdownSingleThreadedCleanup(); |
michael@0 | 369 | |
michael@0 | 370 | private: |
michael@0 | 371 | // Current allowable states of the tracking system. The states always |
michael@0 | 372 | // proceed towards SHUTDOWN, and never go backwards. |
michael@0 | 373 | enum Status { |
michael@0 | 374 | UNINITIALIZED, |
michael@0 | 375 | ACTIVE, |
michael@0 | 376 | SHUTDOWN |
michael@0 | 377 | }; |
michael@0 | 378 | |
michael@0 | 379 | // A class used to count down which is accessed by several threads. This is |
michael@0 | 380 | // used to make sure RunOnAllThreads() actually runs a task on the expected |
michael@0 | 381 | // count of threads. |
michael@0 | 382 | class ThreadSafeDownCounter { |
michael@0 | 383 | public: |
michael@0 | 384 | // Constructor sets the count, once and for all. |
michael@0 | 385 | explicit ThreadSafeDownCounter(size_t count); |
michael@0 | 386 | |
michael@0 | 387 | // Decrement the count, and return true if we hit zero. Also delete this |
michael@0 | 388 | // instance automatically when we hit zero. |
michael@0 | 389 | bool LastCaller(); |
michael@0 | 390 | |
michael@0 | 391 | private: |
michael@0 | 392 | size_t remaining_count_; |
michael@0 | 393 | Lock lock_; // protect access to remaining_count_. |
michael@0 | 394 | }; |
michael@0 | 395 | |
michael@0 | 396 | #ifdef OS_WIN |
michael@0 | 397 | // A Task class that runs a static method supplied, and checks to see if this |
michael@0 | 398 | // is the last tasks instance (on last thread) that will run the method. |
michael@0 | 399 | // IF this is the last run, then the supplied event is signalled. |
michael@0 | 400 | class RunTheStatic : public Task { |
michael@0 | 401 | public: |
michael@0 | 402 | typedef void (*FunctionPointer)(); |
michael@0 | 403 | RunTheStatic(FunctionPointer function, |
michael@0 | 404 | HANDLE completion_handle, |
michael@0 | 405 | ThreadSafeDownCounter* counter); |
michael@0 | 406 | // Run the supplied static method, and optionally set the event. |
michael@0 | 407 | void Run(); |
michael@0 | 408 | |
michael@0 | 409 | private: |
michael@0 | 410 | FunctionPointer function_; |
michael@0 | 411 | HANDLE completion_handle_; |
michael@0 | 412 | // Make sure enough tasks are called before completion is signaled. |
michael@0 | 413 | ThreadSafeDownCounter* counter_; |
michael@0 | 414 | |
michael@0 | 415 | DISALLOW_COPY_AND_ASSIGN(RunTheStatic); |
michael@0 | 416 | }; |
michael@0 | 417 | #endif |
michael@0 | 418 | |
michael@0 | 419 | // Each registered thread is called to set status_ to SHUTDOWN. |
michael@0 | 420 | // This is done redundantly on every registered thread because it is not |
michael@0 | 421 | // protected by a mutex. Running on all threads guarantees we get the |
michael@0 | 422 | // notification into the memory cache of all possible threads. |
michael@0 | 423 | static void ShutdownDisablingFurtherTracking(); |
michael@0 | 424 | |
michael@0 | 425 | // We use thread local store to identify which ThreadData to interact with. |
michael@0 | 426 | static TLSSlot tls_index_ ; |
michael@0 | 427 | |
michael@0 | 428 | // Link to the most recently created instance (starts a null terminated list). |
michael@0 | 429 | static ThreadData* first_; |
michael@0 | 430 | // Protection for access to first_. |
michael@0 | 431 | static Lock list_lock_; |
michael@0 | 432 | |
michael@0 | 433 | |
michael@0 | 434 | // We set status_ to SHUTDOWN when we shut down the tracking service. This |
michael@0 | 435 | // setting is redundantly established by all participating |
michael@0 | 436 | // threads so that we are *guaranteed* (without locking) that all threads |
michael@0 | 437 | // can "see" the status and avoid additional calls into the service. |
michael@0 | 438 | static Status status_; |
michael@0 | 439 | |
michael@0 | 440 | // Link to next instance (null terminated list). Used to globally track all |
michael@0 | 441 | // registered instances (corresponds to all registered threads where we keep |
michael@0 | 442 | // data). |
michael@0 | 443 | ThreadData* next_; |
michael@0 | 444 | |
michael@0 | 445 | // The message loop where tasks needing to access this instance's private data |
michael@0 | 446 | // should be directed. Since some threads have no message loop, some |
michael@0 | 447 | // instances have data that can't be (safely) modified externally. |
michael@0 | 448 | MessageLoop* message_loop_; |
michael@0 | 449 | |
michael@0 | 450 | // A map used on each thread to keep track of Births on this thread. |
michael@0 | 451 | // This map should only be accessed on the thread it was constructed on. |
michael@0 | 452 | // When a snapshot is needed, this structure can be locked in place for the |
michael@0 | 453 | // duration of the snapshotting activity. |
michael@0 | 454 | BirthMap birth_map_; |
michael@0 | 455 | |
michael@0 | 456 | // Similar to birth_map_, this records informations about death of tracked |
michael@0 | 457 | // instances (i.e., when a tracked instance was destroyed on this thread). |
michael@0 | 458 | DeathMap death_map_; |
michael@0 | 459 | |
michael@0 | 460 | // Lock to protect *some* access to BirthMap and DeathMap. We only use |
michael@0 | 461 | // locking protection when we are growing the maps, or using an iterator. We |
michael@0 | 462 | // only do writes to members from this thread, so the updates of values are |
michael@0 | 463 | // atomic. Folks can read from other threads, and get (via races) new or old |
michael@0 | 464 | // data, but that is considered acceptable errors (mis-information). |
michael@0 | 465 | Lock lock_; |
michael@0 | 466 | |
michael@0 | 467 | DISALLOW_COPY_AND_ASSIGN(ThreadData); |
michael@0 | 468 | }; |
michael@0 | 469 | |
michael@0 | 470 | |
michael@0 | 471 | //------------------------------------------------------------------------------ |
michael@0 | 472 | // Provide simple way to to start global tracking, and to tear down tracking |
michael@0 | 473 | // when done. Note that construction and destruction of this object must be |
michael@0 | 474 | // done when running in single threaded mode (before spawning a lot of threads |
michael@0 | 475 | // for construction, and after shutting down all the threads for destruction). |
michael@0 | 476 | |
michael@0 | 477 | class AutoTracking { |
michael@0 | 478 | public: |
michael@0 | 479 | AutoTracking() { ThreadData::StartTracking(true); } |
michael@0 | 480 | |
michael@0 | 481 | ~AutoTracking() { |
michael@0 | 482 | #ifndef NDEBUG // Don't call these in a Release build: they just waste time. |
michael@0 | 483 | // The following should ONLY be called when in single threaded mode. It is |
michael@0 | 484 | // unsafe to do this cleanup if other threads are still active. |
michael@0 | 485 | // It is also very unnecessary, so I'm only doing this in debug to satisfy |
michael@0 | 486 | // purify (if we need to!). |
michael@0 | 487 | ThreadData::ShutdownSingleThreadedCleanup(); |
michael@0 | 488 | #endif |
michael@0 | 489 | } |
michael@0 | 490 | |
michael@0 | 491 | private: |
michael@0 | 492 | DISALLOW_COPY_AND_ASSIGN(AutoTracking); |
michael@0 | 493 | }; |
michael@0 | 494 | |
michael@0 | 495 | |
michael@0 | 496 | } // namespace tracked_objects |
michael@0 | 497 | |
michael@0 | 498 | #endif // BASE_TRACKED_OBJECTS_H_ |