license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
| 5 | //------------------------------------------------------------------------------ |
| 6 | // Tracked is the base class for all tracked objects. During construction, it |
| 7 | // registers the fact that an instance was created, and at destruction time, it |
| 8 | // records that event. The instance may be tagged with a name, which is refered |
| 9 | // to as its Location. The Location is a file and line number, most |
| 10 | // typically indicated where the object was constructed. In some cases, as the |
| 11 | // object's significance is refined (for example, a Task object is augmented to |
| 12 | // do additonal things), its Location may be redefined to that later location. |
| 13 | |
| 14 | // Tracking includes (for each instance) recording the birth thread, death |
| 15 | // thread, and duration of life (from construction to destruction). All this |
| 16 | // data is accumulated and filtered for review at about:objects. |
| 17 | |
maruel@google.com | 01f5cfd | 2008-08-16 10:07:30 +0900 | [diff] [blame] | 18 | #ifndef BASE_TRACKED_H_ |
| 19 | #define BASE_TRACKED_H_ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 20 | |
| 21 | #include <string> |
| 22 | |
| 23 | #include "base/time.h" |
| 24 | |
| 25 | #ifndef NDEBUG |
jar@google.com | 0985629 | 2008-08-02 06:55:17 +0900 | [diff] [blame] | 26 | #ifndef TRACK_ALL_TASK_OBJECTS |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 27 | #define TRACK_ALL_TASK_OBJECTS |
jar@google.com | 0985629 | 2008-08-02 06:55:17 +0900 | [diff] [blame] | 28 | #endif // TRACK_ALL_TASK_OBJECTS |
| 29 | #endif // NDEBUG |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 30 | |
| 31 | namespace tracked_objects { |
| 32 | |
| 33 | //------------------------------------------------------------------------------ |
| 34 | // Location provides basic info where of an object was constructed, or was |
| 35 | // significantly brought to life. |
| 36 | |
| 37 | class Location { |
| 38 | public: |
| 39 | // Constructor should be called with a long-lived char*, such as __FILE__. |
| 40 | // It assumes the provided value will persist as a global constant, and it |
| 41 | // will not make a copy of it. |
| 42 | Location(const char* function_name, const char* file_name, int line_number) |
| 43 | : function_name_(function_name), |
| 44 | file_name_(file_name), |
| 45 | line_number_(line_number) { } |
| 46 | |
| 47 | // Provide a default constructor for easy of debugging. |
| 48 | Location() |
| 49 | : function_name_("Unknown"), |
| 50 | file_name_("Unknown"), |
| 51 | line_number_(-1) { } |
| 52 | |
| 53 | // Comparison operator for insertion into a std::map<> hash tables. |
| 54 | // All we need is *some* (any) hashing distinction. Strings should already |
| 55 | // be unique, so we don't bother with strcmp or such. |
| 56 | // Use line number as the primary key (because it is fast, and usually gets us |
| 57 | // a difference), and then pointers as secondary keys (just to get some |
| 58 | // distinctions). |
| 59 | bool operator < (const Location& other) const { |
| 60 | if (line_number_ != other.line_number_) |
| 61 | return line_number_ < other.line_number_; |
| 62 | if (file_name_ != other.file_name_) |
maruel@google.com | 01f5cfd | 2008-08-16 10:07:30 +0900 | [diff] [blame] | 63 | return file_name_ < other.file_name_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 64 | return function_name_ < other.function_name_; |
| 65 | } |
| 66 | |
| 67 | const char* function_name() const { return function_name_; } |
| 68 | const char* file_name() const { return file_name_; } |
| 69 | int line_number() const { return line_number_; } |
| 70 | |
| 71 | void Write(bool display_filename, bool display_function_name, |
maruel@google.com | 01f5cfd | 2008-08-16 10:07:30 +0900 | [diff] [blame] | 72 | std::string* output) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 73 | |
| 74 | // Write function_name_ in HTML with '<' and '>' properly encoded. |
| 75 | void WriteFunctionName(std::string* output) const; |
| 76 | |
| 77 | private: |
| 78 | const char* const function_name_; |
| 79 | const char* const file_name_; |
| 80 | const int line_number_; |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | //------------------------------------------------------------------------------ |
darin@google.com | d3b9a19 | 2008-07-30 13:38:38 +0900 | [diff] [blame] | 85 | // Define a macro to record the current source location. |
| 86 | |
| 87 | #define FROM_HERE tracked_objects::Location(__FUNCTION__, __FILE__, __LINE__) |
| 88 | |
| 89 | |
| 90 | //------------------------------------------------------------------------------ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 91 | |
| 92 | |
| 93 | class Births; |
| 94 | |
| 95 | class Tracked { |
| 96 | public: |
| 97 | Tracked(); |
| 98 | virtual ~Tracked(); |
jar@google.com | 0985629 | 2008-08-02 06:55:17 +0900 | [diff] [blame] | 99 | |
| 100 | // Used to record the FROM_HERE location of a caller. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 101 | void SetBirthPlace(const Location& from_here); |
| 102 | |
jar@google.com | 0985629 | 2008-08-02 06:55:17 +0900 | [diff] [blame] | 103 | // When a task sits around a long time, such as in a timer, or object watcher, |
| 104 | // this method should be called when the task becomes active, and its |
| 105 | // significant lifetime begins (and its waiting to be woken up has passed). |
| 106 | void ResetBirthTime(); |
| 107 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 108 | bool MissingBirthplace() const; |
| 109 | |
| 110 | private: |
jar@google.com | 0985629 | 2008-08-02 06:55:17 +0900 | [diff] [blame] | 111 | // Pointer to instance were counts of objects with the same birth location |
| 112 | // (on the same thread) are stored. |
| 113 | Births* tracked_births_; |
| 114 | // The time this object was constructed. If its life consisted of a long |
| 115 | // waiting period, and then it became active, then this value is generally |
| 116 | // reset before the object begins it active life. |
| 117 | Time tracked_birth_time_; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 118 | |
maruel@google.com | 01f5cfd | 2008-08-16 10:07:30 +0900 | [diff] [blame] | 119 | DISALLOW_COPY_AND_ASSIGN(Tracked); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // namespace tracked_objects |
| 123 | |
maruel@google.com | 01f5cfd | 2008-08-16 10:07:30 +0900 | [diff] [blame] | 124 | #endif // BASE_TRACKED_H_ |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 125 | |